Merge pull request #5330 from alexanderkyte/dedup_mkbundle
[mono.git] / mcs / tools / mkbundle / mkbundle.cs
1 //
2 // mkbundle: tool to create bundles.
3 //
4 // Based on the `make-bundle' Perl script written by Paolo Molaro (lupus@debian.org)
5 //
6 // TODO:
7 //   [x] Rename the paths for the zip file that is downloaded
8 //   [x] Update documentation with new flag
9 //   [x] Load internationalized assemblies
10 //   [x] Dependencies - if System.dll -> include Mono.Security.* (not needed, automatic)
11 //   [x] --list-targets should download from a different url
12 //   [x] --fetch-target should unpack zip file
13 //   [x] Update --cross to use not a runtime, but an SDK
14 //   [x] Update --local-targets to show the downloaded SDKs
15 //
16 // Author:
17 //   Miguel de Icaza
18 //
19 // (C) Novell, Inc 2004
20 // (C) 2016 Xamarin Inc
21 //
22 // Missing features:
23 // * Add support for packaging native libraries, extracting at runtime and setting the library path.
24 // * Implement --list-targets lists all the available remote targets
25 //
26 using System;
27 using System.Diagnostics;
28 using System.Xml;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.IO.Compression;
32 using System.Runtime.InteropServices;
33 using System.Text;
34 using IKVM.Reflection;
35 using System.Linq;
36 using System.Net;
37 using System.Threading.Tasks;
38
39 class MakeBundle {
40         static string output = "a.out";
41         static string object_out = null;
42         static List<string> link_paths = new List<string> ();
43         static List<string> aot_paths = new List<string> ();
44         static List<string> aot_names = new List<string> ();
45         static Dictionary<string,string> libraries = new Dictionary<string,string> ();
46         static bool autodeps = false;
47         static string in_tree = null;
48         static bool keeptemp = false;
49         static bool compile_only = false;
50         static bool static_link = false;
51         static string config_file = null;
52         static string machine_config_file = null;
53         static string config_dir = null;
54         static string style = "linux";
55         static bool bundled_header = false;
56         static string os_message = "";
57         static bool compress;
58         static bool nomain;
59         static string custom_main = null;
60         static bool? use_dos2unix = null;
61         static bool skip_scan;
62         static string ctor_func;
63         static bool quiet = true;
64         static string cross_target = null;
65         static string fetch_target = null;
66         static bool custom_mode = true;
67         static string embedded_options = null;
68
69         static string runtime_bin = null;
70
71         static string runtime {
72                 get {
73                         if (runtime_bin == null && IsUnix)
74                                 runtime_bin = Process.GetCurrentProcess().MainModule.FileName;
75                         return runtime_bin;
76                 }
77
78                 set { runtime_bin = value; }
79         }
80         
81         static bool aot_compile = false;
82         static string aot_args = "static";
83         static DirectoryInfo aot_temp_dir = null;
84         static string aot_mode = "";
85         static string aot_runtime = null;
86         static string aot_dedup_assembly = null;
87         static string cil_strip_path = null;
88         static string managed_linker_path = null;
89         static string sdk_path = null;
90         static string lib_path = null;
91         static Dictionary<string,string> environment = new Dictionary<string,string>();
92         static string [] i18n = new string [] {
93                 "West",
94                 ""
95         };
96         static string [] i18n_all = new string [] {
97                 "CJK", 
98                 "MidEast",
99                 "Other",
100                 "Rare",
101                 "West",
102                 ""
103         };
104         static string target_server = "https://download.mono-project.com/runtimes/raw/";
105         
106         static int Main (string [] args)
107         {
108                 List<string> sources = new List<string> ();
109                 int top = args.Length;
110                 link_paths.Add (".");
111
112                 DetectOS ();
113
114                 for (int i = 0; i < top; i++){
115                         switch (args [i]){
116                         case "--help": case "-h": case "-?":
117                                 Help ();
118                                 return 1;
119
120                         case "--simple":
121                                 custom_mode = false;
122                                 autodeps = true;
123                                 break;
124
125                         case "-v":
126                                 quiet = false;
127                                 break;
128                                 
129                         case "--i18n":
130                                 if (i+1 == top){
131                                         Help ();
132                                         return 1;
133                                 }
134                                 var iarg = args [++i];
135                                 if (iarg == "all")
136                                         i18n = i18n_all;
137                                 else if (iarg == "none")
138                                         i18n = new string [0];
139                                 else
140                                         i18n = iarg.Split (',');
141                                 break;
142                                 
143                         case "--custom":
144                                 custom_mode = true;
145                                 break;
146                                 
147                         case "-c":
148                                 compile_only = true;
149                                 break;
150
151                         case "--local-targets":
152                                 CommandLocalTargets ();
153                                 return 0;
154
155                         case "--cross":
156                                 if (i+1 == top){
157                                         Help (); 
158                                         return 1;
159                                 }
160                                 if (sdk_path != null || runtime != null)
161                                         Error ("You can only specify one of --runtime, --sdk or --cross");
162                                 custom_mode = false;
163                                 autodeps = true;
164                                 cross_target = args [++i];
165                                 break;
166
167                         case "--library":
168                                 if (i+1 == top){
169                                         Help (); 
170                                         return 1;
171                                 }
172                                 if (custom_mode){
173                                         Console.Error.WriteLine ("--library can only be used with --simple/--runtime/--cross mode");
174                                         Help ();
175                                         return 1;
176                                 }
177                                 var lspec = args [++i];
178                                 var p = lspec.IndexOf (",");
179                                 string alias, path;
180                                 if (p == -1){
181                                         alias = Path.GetFileName (lspec);
182                                         path = lspec;
183                                 } else {
184                                         alias = lspec.Substring (0, p);
185                                         path = lspec.Substring (p+1);
186                                 }
187                                 if (!File.Exists (path))
188                                         Error ($"The specified library file {path} does not exist");
189                                 libraries [alias] = path;
190                                 break;
191
192                         case "--fetch-target":
193                                 if (i+1 == top){
194                                         Help (); 
195                                         return 1;
196                                 }
197                                 fetch_target = args [++i];
198                                 break;
199
200                         case "--list-targets":
201                                 CommandLocalTargets ();
202                                 var wc = new WebClient ();
203                                 var s = wc.DownloadString (new Uri (target_server + "target-sdks.txt"));
204                                 Console.WriteLine ("Targets available for download with --fetch-target:\n" + s);
205                                 return 0;
206                                 
207                         case "--target-server":
208                                 if (i+1 == top){
209                                         Help (); 
210                                         return 1;
211                                 }
212                                 target_server = args [++i];
213                                 break;
214
215                         case "-o": 
216                                 if (i+1 == top){
217                                         Help (); 
218                                         return 1;
219                                 }
220                                 output = args [++i];
221                                 break;
222
223                         case "--options":
224                                 if (i+1 == top){
225                                         Help (); 
226                                         return 1;
227                                 }
228                                 embedded_options = args [++i];
229                                 break;
230                         case "--sdk":
231                                 if (i + 1 == top) {
232                                         Help ();
233                                         return 1;
234                                 }
235                                 custom_mode = false;
236                                 autodeps = true;
237                                 sdk_path = args [++i];
238                                 if (cross_target != null || runtime != null)
239                                         Error ("You can only specify one of --runtime, --sdk or --cross");
240                                 break;
241                         case "--runtime":
242                                 if (i+1 == top){
243                                         Help (); 
244                                         return 1;
245                                 }
246                                 if (sdk_path != null || cross_target != null)
247                                         Error ("You can only specify one of --runtime, --sdk or --cross");
248                                 custom_mode = false;
249                                 autodeps = true;
250                                 runtime = args [++i];
251                                 break;
252                         case "-oo":
253                                 if (i+1 == top){
254                                         Help (); 
255                                         return 1;
256                                 }
257                                 object_out = args [++i];
258                                 break;
259
260                         case "-L":
261                                 if (i+1 == top){
262                                         Help (); 
263                                         return 1;
264                                 }
265                                 link_paths.Add (args [++i]);
266                                 break;
267
268                         case "--nodeps":
269                                 autodeps = false;
270                                 break;
271
272                         case "--deps":
273                                 autodeps = true;
274                                 break;
275
276                         case "--keeptemp":
277                                 keeptemp = true;
278                                 break;
279                                 
280                         case "--static":
281                                 static_link = true;
282                                 break;
283                         case "--config":
284                                 if (i+1 == top) {
285                                         Help ();
286                                         return 1;
287                                 }
288
289                                 config_file = args [++i];
290                                 break;
291                         case "--machine-config":
292                                 if (i+1 == top) {
293                                         Help ();
294                                         return 1;
295                                 }
296
297                                 machine_config_file = args [++i];
298
299                                 if (!quiet)
300                                         Console.WriteLine ("WARNING:\n  Check that the machine.config file you are bundling\n  doesn't contain sensitive information specific to this machine.");
301                                 break;
302                         case "--config-dir":
303                                 if (i+1 == top) {
304                                         Help ();
305                                         return 1;
306                                 }
307
308                                 config_dir = args [++i];
309                                 break;
310                         case "-z":
311                                 compress = true;
312                                 break;
313                         case "--nomain":
314                                 nomain = true;
315                                 break;
316                         case "--custom-main":
317                                 if (i+1 == top) {
318                                         Help ();
319                                         return 1;
320                                 }
321                                 custom_main = args [++i];
322                                 break;
323                         case "--style":
324                                 if (i+1 == top) {
325                                         Help ();
326                                         return 1;
327                                 }
328                                 style = args [++i];
329                                 switch (style) {
330                                 case "windows":
331                                 case "mac":
332                                 case "linux":
333                                         break;
334                                 default:
335                                         Error ("Invalid style '{0}' - only 'windows', 'mac' and 'linux' are supported for --style argument", style);
336                                         return 1;
337                                 }
338                                         
339                                 break;
340                         case "--skip-scan":
341                                 skip_scan = true;
342                                 break;
343                         case "--static-ctor":
344                                 if (i+1 == top) {
345                                         Help ();
346                                         return 1;
347                                 }
348                                 ctor_func = args [++i];
349                                 break;
350                         case "--dos2unix":
351                         case "--dos2unix=true":
352                                 use_dos2unix = true;
353                                 break;
354                         case "--dos2unix=false":
355                                 use_dos2unix = false;
356                                 break;
357                         case "-q":
358                         case "--quiet":
359                                 quiet = true;
360                                 break;
361                         case "-e":
362                         case "--env":
363                                 if (i+1 == top) {
364                                         Help ();
365                                         return 1;
366                                 }
367                                 var env = args [++i];
368                                 p = env.IndexOf ('=');
369                                 if (p == -1)
370                                         environment.Add (env, "");
371                                 else
372                                         environment.Add (env.Substring (0, p), env.Substring (p+1));
373                                 break;
374                         case "--bundled-header":
375                                 bundled_header = true;
376                                 break;
377                         case "--in-tree":
378                                 if (i+1 == top) {
379                                         Console.WriteLine ("Usage: --in-tree <path/to/headers> ");
380                                         return 1;
381                                 }
382                                 in_tree = args [++i];
383                                 break;
384                         case "--managed-linker":
385                                 if (i+1 == top) {
386                                         Console.WriteLine ("Usage: --managed-linker <path/to/exe> ");
387                                         return 1;
388                                 }
389                                 managed_linker_path = args [++i];
390                                 break;
391                         case "--cil-strip":
392                                 if (i+1 == top) {
393                                         Console.WriteLine ("Usage: --cil-strip <path/to/exe> ");
394                                         return 1;
395                                 }
396                                 cil_strip_path = args [++i];
397                                 break;
398                         case "--aot-runtime":
399                                 if (i+1 == top) {
400                                         Console.WriteLine ("Usage: --aot-runtime <path/to/runtime> ");
401                                         return 1;
402                                 }
403                                 aot_runtime = args [++i];
404                                 aot_compile = true;
405                                 static_link = true;
406                                 break;
407                         case "--aot-dedup":
408                                 if (i+1 == top) {
409                                         Console.WriteLine ("Usage: --aot-dedup <container_dll> ");
410                                         return 1;
411                                 }
412                                 var rel_path = args [++i];
413                                 var asm = LoadAssembly (rel_path);
414                                 if (asm != null)
415                                         aot_dedup_assembly = new Uri(asm.CodeBase).LocalPath;
416
417                                 sources.Add (rel_path);
418                                 aot_compile = true;
419                                 static_link = true;
420                                 break;
421                         case "--aot-mode":
422                                 if (i+1 == top) {
423                                         Console.WriteLine ("Need string of aot mode (full, llvmonly). Omit for normal AOT.");
424                                         return 1;
425                                 }
426
427                                 aot_mode = args [++i];
428                                 if (aot_mode != "full" && aot_mode != "llvmonly") {
429                                         Console.WriteLine ("Need string of aot mode (full, llvmonly). Omit for normal AOT.");
430                                         return 1;
431                                 }
432
433                                 aot_compile = true;
434                                 static_link = true;
435                                 break;
436                         case "--aot-args":
437                                 if (i+1 == top) {
438                                         Console.WriteLine ("AOT arguments are passed as a comma-delimited list");
439                                         return 1;
440                                 }
441                                 if (args [i + 1].Contains ("outfile")) {
442                                         Console.WriteLine ("Per-aot-output arguments (ex: outfile, llvm-outfile) cannot be given");
443                                         return 1;
444                                 }
445                                 aot_args = String.Format("static,{0}", args [++i]);
446                                 aot_compile = true;
447                                 static_link = true;
448                                 break;
449                         default:
450                                 sources.Add (args [i]);
451                                 break;
452                         }
453
454                 }
455                 // Modern bundling starts here
456                 if (!custom_mode){
457                         if (runtime != null){
458                                 // Nothing to do here, the user has chosen to manually specify --runtime nad libraries
459                         } else if (sdk_path != null) {
460                                 VerifySdk (sdk_path);
461                         } else if (cross_target == "default" || cross_target == null){
462                                 sdk_path = Path.GetFullPath (Path.Combine (Process.GetCurrentProcess().MainModule.FileName, "..", ".."));
463                                 VerifySdk (sdk_path);
464                         } else {
465                                 sdk_path = Path.Combine (targets_dir, cross_target);
466                                 Console.WriteLine ("From: " + sdk_path);
467                                 VerifySdk (sdk_path);
468                         }
469                 }
470
471                 if (fetch_target != null){
472                         var directory = Path.Combine (targets_dir, fetch_target);
473                         var zip_download = Path.Combine (directory, "sdk.zip");
474                         Directory.CreateDirectory (directory);
475                         var wc = new WebClient ();
476                         var uri = new Uri ($"{target_server}{fetch_target}");
477                         try {
478                                 if (!quiet){
479                                         Console.WriteLine ($"Downloading runtime {uri} to {zip_download}");
480                                 }
481                                 
482                                 wc.DownloadFile (uri, zip_download);
483                                 ZipFile.ExtractToDirectory(zip_download, directory);
484                                 File.Delete (zip_download);
485                         } catch {
486                                 Console.Error.WriteLine ($"Failure to download the specified runtime from {uri}");
487                                 File.Delete (zip_download);
488                                 return 1;
489                         }
490                         return 0;
491                 }
492                 
493                 if (!quiet) {
494                         Console.WriteLine (os_message);
495                         Console.WriteLine ("Sources: {0} Auto-dependencies: {1}", sources.Count, autodeps);
496                 }
497
498                 if (sources.Count == 0 || output == null) {
499                         Help ();
500                         Environment.Exit (1);
501                 }
502
503                 List<string> assemblies = LoadAssemblies (sources);
504                 LoadLocalizedAssemblies (assemblies);
505                 List<string> files = new List<string> ();
506                 foreach (string file in assemblies)
507                         if (!QueueAssembly (files, file))
508                                 return 1;
509
510                 PreprocessAssemblies (assemblies, files);
511
512                 if (aot_compile)
513                         AotCompile (files);
514
515                 if (custom_mode)
516                         GenerateBundles (files);
517                 else 
518                         GeneratePackage (files);
519
520                 Console.WriteLine ("Generated {0}", output);
521
522                 return 0;
523         }
524
525         static void VerifySdk (string path)
526         {
527                 if (!Directory.Exists (path))
528                         Error ($"The specified SDK path does not exist: {path}");
529                 runtime = Path.Combine (sdk_path, "bin", "mono");
530                 if (!File.Exists (runtime))
531                         Error ($"The SDK location does not contain a {path}/bin/mono runtime");
532                 lib_path = Path.Combine (path, "lib", "mono", "4.5");
533                 if (!Directory.Exists (lib_path))
534                         Error ($"The SDK location does not contain a {path}/lib/mono/4.5 directory");
535                 link_paths.Add (lib_path);
536         }
537
538         static string targets_dir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), ".mono", "targets");
539         
540         static void CommandLocalTargets ()
541         {
542                 string [] targets;
543
544                 Console.WriteLine ("Available targets locally:");
545                 Console.WriteLine ("\tdefault\t- Current System Mono");
546                 try {
547                         targets = Directory.GetDirectories (targets_dir);
548                 } catch {
549                         return;
550                 }
551                 foreach (var target in targets){
552                         var p = Path.Combine (target, "bin", "mono");
553                         if (File.Exists (p))
554                                 Console.WriteLine ("\t{0}", Path.GetFileName (target));
555                 }
556         }
557
558         static void WriteSymbol (StreamWriter sw, string name, long size)
559         {
560                 switch (style){
561                 case "linux":
562                         sw.WriteLine (
563                                 ".globl {0}\n" +
564                                 "\t.section .rodata\n" +
565                                 "\t.p2align 5\n" +
566                                 "\t.type {0}, \"object\"\n" +
567                                 "\t.size {0}, {1}\n" +
568                                 "{0}:\n",
569                                 name, size);
570                         break;
571                 case "osx":
572                         sw.WriteLine (
573                                 "\t.section __TEXT,__text,regular,pure_instructions\n" + 
574                                 "\t.globl _{0}\n" +
575                                 "\t.data\n" +
576                                 "\t.align 4\n" +
577                                 "_{0}:\n",
578                                 name, size);
579                         break;
580                 case "windows":
581                         sw.WriteLine (
582                                 ".globl _{0}\n" +
583                                 "\t.section .rdata,\"dr\"\n" +
584                                 "\t.align 32\n" +
585                                 "_{0}:\n",
586                                 name, size);
587                         break;
588                 }
589         }
590         
591         static string [] chars = new string [256];
592         
593         static void WriteBuffer (StreamWriter ts, Stream stream, byte[] buffer)
594         {
595                 int n;
596                 
597                 // Preallocate the strings we need.
598                 if (chars [0] == null) {
599                         for (int i = 0; i < chars.Length; i++)
600                                 chars [i] = string.Format ("{0}", i.ToString ());
601                 }
602
603                 while ((n = stream.Read (buffer, 0, buffer.Length)) != 0) {
604                         int count = 0;
605                         for (int i = 0; i < n; i++) {
606                                 if (count % 32 == 0) {
607                                         ts.Write ("\n\t.byte ");
608                                 } else {
609                                         ts.Write (",");
610                                 }
611                                 ts.Write (chars [buffer [i]]);
612                                 count ++;
613                         }
614                 }
615
616                 ts.WriteLine ();
617         }
618
619         class PackageMaker {
620                 Dictionary<string, Tuple<long,int>> locations = new Dictionary<string, Tuple<long,int>> ();
621                 const int align = 4096;
622                 Stream package;
623                 
624                 public PackageMaker (string output)
625                 {
626                         package = File.Create (output, 128*1024);
627                         if (IsUnix){
628                                 File.SetAttributes (output, unchecked ((FileAttributes) 0x80000000));
629                         }
630                 }
631
632                 public int AddFile (string fname)
633                 {
634                         using (Stream fileStream = File.OpenRead (fname)){
635                                 var ret = fileStream.Length;
636
637                                 if (!quiet)
638                                         Console.WriteLine ("At {0:x} with input {1}", package.Position, fileStream.Length);
639                                 fileStream.CopyTo (package);
640                                 package.Position = package.Position + (align - (package.Position % align));
641                                 return (int) ret;
642                         }
643                 }
644                 
645                 public void Add (string entry, string fname)
646                 {
647                         var p = package.Position;
648                         var size = AddFile (fname);
649                         locations [entry] = Tuple.Create(p, size);
650                 }
651
652                 public void AddString (string entry, string text)
653                 {
654                         var bytes = Encoding.UTF8.GetBytes (text);
655                         locations [entry] = Tuple.Create (package.Position, bytes.Length);
656                         package.Write (bytes, 0, bytes.Length);
657                         package.Position = package.Position + (align - (package.Position % align));
658                 }
659
660                 public void AddStringPair (string entry, string key, string value)
661                 {
662                         var kbytes = Encoding.UTF8.GetBytes (key);
663                         var vbytes = Encoding.UTF8.GetBytes (value);
664
665                         Console.WriteLine ("ADDING {0} to {1}", key, value);
666                         if (kbytes.Length > 255){
667                                 Console.WriteLine ("The key value can not exceed 255 characters: " + key);
668                                 Environment.Exit (1);
669                         }
670                                 
671                         locations [entry] = Tuple.Create (package.Position, kbytes.Length+vbytes.Length+3);
672                         package.WriteByte ((byte)kbytes.Length);
673                         package.Write (kbytes, 0, kbytes.Length);
674                         package.WriteByte (0);
675                         package.Write (vbytes, 0, vbytes.Length);
676                         package.WriteByte (0);
677                         package.Position = package.Position + (align - (package.Position % align));
678                 }
679
680                 public void Dump ()
681                 {
682                         if (quiet)
683                                 return;
684                         foreach (var floc in locations.Keys){
685                                 Console.WriteLine ($"{floc} at {locations[floc]:x}");
686                         }
687                 }
688
689                 public void WriteIndex ()
690                 {
691                         var indexStart = package.Position;
692                         var binary = new BinaryWriter (package);
693
694                         binary.Write (locations.Count);
695                         foreach (var entry in from entry in locations orderby entry.Value.Item1 ascending select entry){
696                                 var bytes = Encoding.UTF8.GetBytes (entry.Key);
697                                 binary.Write (bytes.Length+1);
698                                 binary.Write (bytes);
699                                 binary.Write ((byte) 0);
700                                 binary.Write (entry.Value.Item1);
701                                 binary.Write (entry.Value.Item2);
702                         }
703                         binary.Write (indexStart);
704                         binary.Write (Encoding.UTF8.GetBytes ("xmonkeysloveplay"));
705                         binary.Flush ();
706                 }
707                 
708                 public void Close ()
709                 {
710                         WriteIndex ();
711                         package.Close ();
712                         package = null;
713                 }
714         }
715
716         static bool MaybeAddFile (PackageMaker maker, string code, string file)
717         {
718                 if (file == null)
719                         return true;
720                 
721                 if (!File.Exists (file)){
722                         Error ("The file {0} does not exist", file);
723                         return false;
724                 }
725                 maker.Add (code, file);
726                 return true;
727         }
728         
729         static bool GeneratePackage (List<string> files)
730         {
731                 if (runtime == null){
732                         Error ("You must specify at least one runtime with --runtime or --cross");
733                         Environment.Exit (1);
734                 }
735                 if (!File.Exists (runtime)){
736                         Error ($"The specified runtime at {runtime} does not exist");
737                         Environment.Exit (1);
738                 }
739                 
740                 if (ctor_func != null){
741                         Error ("--static-ctor not supported with package bundling, you must use native compilation for this");
742                         return false;
743                 }
744                 
745                 var maker = new PackageMaker (output);
746                 Console.WriteLine ("Using runtime: " + runtime);
747                 maker.AddFile (runtime);
748                 
749                 foreach (var url in files){
750                         string fname = LocateFile (new Uri (url).LocalPath);
751                         string aname = MakeBundle.GetAssemblyName (fname);
752
753                         maker.Add ("assembly:" + aname, fname);
754                         Console.WriteLine ("     Assembly: " + fname);
755                         if (File.Exists (fname + ".config")){
756                                 maker.Add ("config:" + aname, fname + ".config");
757                                 Console.WriteLine ("       Config: " + runtime);
758                         }
759                 }
760                 
761                 if (!MaybeAddFile (maker, "systemconfig:", config_file) || !MaybeAddFile (maker, "machineconfig:", machine_config_file))
762                         return false;
763
764                 if (config_dir != null)
765                         maker.Add ("config_dir:", config_dir);
766                 if (embedded_options != null)
767                         maker.AddString ("options:", embedded_options);
768                 if (environment.Count > 0){
769                         foreach (var key in environment.Keys)
770                                 maker.AddStringPair ("env:" + key, key, environment [key]);
771                 }
772                 if (libraries.Count > 0){
773                         foreach (var alias_and_path in libraries){
774                                 Console.WriteLine ("     Library:  " + alias_and_path.Value);
775                                 maker.Add ("library:" + alias_and_path.Key, alias_and_path.Value);
776                         }
777                 }
778                 maker.Dump ();
779                 maker.Close ();
780                 return true;
781         }
782         
783         static void GenerateBundles (List<string> files)
784         {
785                 string temp_s = "temp.s"; // Path.GetTempFileName ();
786                 string temp_c = "temp.c";
787                 string temp_o = "temp.o";
788
789                 if (compile_only)
790                         temp_c = output;
791                 if (object_out != null)
792                         temp_o = object_out;
793                 
794                 try {
795                         List<string> c_bundle_names = new List<string> ();
796                         List<string[]> config_names = new List<string[]> ();
797
798                         using (StreamWriter ts = new StreamWriter (File.Create (temp_s))) {
799                         using (StreamWriter tc = new StreamWriter (File.Create (temp_c))) {
800                         string prog = null;
801
802                         if (bundled_header) {
803                                 tc.WriteLine ("/* This source code was produced by mkbundle, do not edit */");
804                                 tc.WriteLine ("\n#ifndef NULL\n#define NULL (void *)0\n#endif");
805                                 tc.WriteLine (@"
806 typedef struct {
807         const char *name;
808         const unsigned char *data;
809         const unsigned int size;
810 } MonoBundledAssembly;
811 void          mono_register_bundled_assemblies (const MonoBundledAssembly **assemblies);
812 void          mono_register_config_for_assembly (const char* assembly_name, const char* config_xml);
813 ");
814                         } else {
815                                 tc.WriteLine ("#include <mono/metadata/mono-config.h>");
816                                 tc.WriteLine ("#include <mono/metadata/assembly.h>\n");
817
818                                 if (in_tree != null)
819                                         tc.WriteLine ("#include <mono/mini/jit.h>\n");
820                                 else
821                                         tc.WriteLine ("#include <mono/jit/jit.h>\n");
822                         }
823
824                         if (compress) {
825                                 tc.WriteLine ("typedef struct _compressed_data {");
826                                 tc.WriteLine ("\tMonoBundledAssembly assembly;");
827                                 tc.WriteLine ("\tint compressed_size;");
828                                 tc.WriteLine ("} CompressedAssembly;\n");
829                         }
830
831                         object monitor = new object ();
832
833                         var streams = new Dictionary<string, Stream> ();
834                         var sizes = new Dictionary<string, long> ();
835
836                         // Do the file reading and compression in parallel
837                         Action<string> body = delegate (string url) {
838                                 string fname = LocateFile (new Uri (url).LocalPath);
839                                 Stream stream = File.OpenRead (fname);
840
841                                 long real_size = stream.Length;
842                                 int n;
843                                 if (compress) {
844                                         byte[] cbuffer = new byte [8192];
845                                         MemoryStream ms = new MemoryStream ();
846                                         GZipStream deflate = new GZipStream (ms, CompressionMode.Compress, leaveOpen:true);
847                                         while ((n = stream.Read (cbuffer, 0, cbuffer.Length)) != 0){
848                                                 deflate.Write (cbuffer, 0, n);
849                                         }
850                                         stream.Close ();
851                                         deflate.Close ();
852                                         byte [] bytes = ms.GetBuffer ();
853                                         stream = new MemoryStream (bytes, 0, (int) ms.Length, false, false);
854                                 }
855
856                                 lock (monitor) {
857                                         streams [url] = stream;
858                                         sizes [url] = real_size;
859                                 }
860                         };
861
862                         //#if NET_4_5
863 #if FALSE
864                         Parallel.ForEach (files, body);
865 #else
866                         foreach (var url in files)
867                                 body (url);
868 #endif
869
870                         // The non-parallel part
871                         byte [] buffer = new byte [8192];
872                         // everything other than a-zA-Z0-9_ needs to be escaped in asm symbols.
873                         var symbolEscapeRE = new System.Text.RegularExpressions.Regex ("[^\\w_]");
874                         foreach (var url in files) {
875                                 string fname = LocateFile (new Uri (url).LocalPath);
876                                 string aname = MakeBundle.GetAssemblyName (fname);
877                                 string encoded = symbolEscapeRE.Replace (aname, "_");
878
879                                 if (prog == null)
880                                         prog = aname;
881
882                                 var stream = streams [url];
883                                 var real_size = sizes [url];
884
885                                 if (!quiet)
886                                         Console.WriteLine ("   embedding: " + fname);
887
888                                 WriteSymbol (ts, "assembly_data_" + encoded, stream.Length);
889                         
890                                 WriteBuffer (ts, stream, buffer);
891
892                                 if (compress) {
893                                         tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
894                                         tc.WriteLine ("static CompressedAssembly assembly_bundle_{0} = {{{{\"{1}\"," +
895                                                                   " assembly_data_{0}, {2}}}, {3}}};",
896                                                                   encoded, aname, real_size, stream.Length);
897                                         if (!quiet) {
898                                                 double ratio = ((double) stream.Length * 100) / real_size;
899                                                 Console.WriteLine ("   compression ratio: {0:.00}%", ratio);
900                                         }
901                                 } else {
902                                         tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
903                                         tc.WriteLine ("static const MonoBundledAssembly assembly_bundle_{0} = {{\"{1}\", assembly_data_{0}, {2}}};",
904                                                                   encoded, aname, real_size);
905                                 }
906                                 stream.Close ();
907
908                                 c_bundle_names.Add ("assembly_bundle_" + encoded);
909
910                                 try {
911                                         FileStream cf = File.OpenRead (fname + ".config");
912                                         if (!quiet)
913                                                 Console.WriteLine (" config from: " + fname + ".config");
914                                         tc.WriteLine ("extern const unsigned char assembly_config_{0} [];", encoded);
915                                         WriteSymbol (ts, "assembly_config_" + encoded, cf.Length);
916                                         WriteBuffer (ts, cf, buffer);
917                                         ts.WriteLine ();
918                                         config_names.Add (new string[] {aname, encoded});
919                                 } catch (FileNotFoundException) {
920                                         /* we ignore if the config file doesn't exist */
921                                 }
922                         }
923
924                         if (config_file != null){
925                                 FileStream conf;
926                                 try {
927                                         conf = File.OpenRead (config_file);
928                                 } catch {
929                                         Error ("Failure to open {0}", config_file);
930                                         return;
931                                 }
932                                 if (!quiet)
933                                         Console.WriteLine ("System config from: " + config_file);
934                                 tc.WriteLine ("extern const char system_config;");
935                                 WriteSymbol (ts, "system_config", config_file.Length);
936
937                                 WriteBuffer (ts, conf, buffer);
938                                 // null terminator
939                                 ts.Write ("\t.byte 0\n");
940                                 ts.WriteLine ();
941                         }
942
943                         if (machine_config_file != null){
944                                 FileStream conf;
945                                 try {
946                                         conf = File.OpenRead (machine_config_file);
947                                 } catch {
948                                         Error ("Failure to open {0}", machine_config_file);
949                                         return;
950                                 }
951                                 if (!quiet)
952                                         Console.WriteLine ("Machine config from: " + machine_config_file);
953                                 tc.WriteLine ("extern const char machine_config;");
954                                 WriteSymbol (ts, "machine_config", machine_config_file.Length);
955
956                                 WriteBuffer (ts, conf, buffer);
957                                 ts.Write ("\t.byte 0\n");
958                                 ts.WriteLine ();
959                         }
960                         ts.Close ();
961
962                         // Managed assemblies baked in
963                         if (compress)
964                                 tc.WriteLine ("\nstatic const CompressedAssembly *compressed [] = {");
965                         else
966                                 tc.WriteLine ("\nstatic const MonoBundledAssembly *bundled [] = {");
967
968                         foreach (string c in c_bundle_names){
969                                 tc.WriteLine ("\t&{0},", c);
970                         }
971                         tc.WriteLine ("\tNULL\n};\n");
972
973
974                         // AOT baked in plus loader
975                         foreach (string asm in aot_names){
976                                 tc.WriteLine ("\textern const void *mono_aot_module_{0}_info;", asm);
977                         }
978
979                         tc.WriteLine ("\nstatic void install_aot_modules (void) {\n");
980                         foreach (string asm in aot_names){
981                                 tc.WriteLine ("\tmono_aot_register_module (mono_aot_module_{0}_info);\n", asm);
982                         }
983
984                         string enum_aot_mode;
985                         switch (aot_mode) {
986                         case "full": 
987                                 enum_aot_mode = "MONO_AOT_MODE_FULL";
988                                 break;
989                         case "llvmonly": 
990                                 enum_aot_mode = "MONO_AOT_MODE_LLVMONLY";
991                                 break;
992                         case "": 
993                                 enum_aot_mode = "MONO_AOT_MODE_NORMAL";
994                                 break;
995                         default:
996                                 throw new Exception ("Unsupported AOT mode");
997                         }
998                         tc.WriteLine ("\tmono_jit_set_aot_mode ({0});", enum_aot_mode);
999
1000                         tc.WriteLine ("\n}\n");
1001
1002
1003                         tc.WriteLine ("static char *image_name = \"{0}\";", prog);
1004
1005                         if (ctor_func != null) {
1006                                 tc.WriteLine ("\nextern void {0} (void);", ctor_func);
1007                                 tc.WriteLine ("\n__attribute__ ((constructor)) static void mono_mkbundle_ctor (void)");
1008                                 tc.WriteLine ("{{\n\t{0} ();\n}}", ctor_func);
1009                         }
1010
1011                         tc.WriteLine ("\nstatic void install_dll_config_files (void) {\n");
1012                         foreach (string[] ass in config_names){
1013                                 tc.WriteLine ("\tmono_register_config_for_assembly (\"{0}\", assembly_config_{1});\n", ass [0], ass [1]);
1014                         }
1015                         if (config_file != null)
1016                                 tc.WriteLine ("\tmono_config_parse_memory (&system_config);\n");
1017                         if (machine_config_file != null)
1018                                 tc.WriteLine ("\tmono_register_machine_config (&machine_config);\n");
1019                         tc.WriteLine ("}\n");
1020
1021                         if (config_dir != null)
1022                                 tc.WriteLine ("static const char *config_dir = \"{0}\";", config_dir);
1023                         else
1024                                 tc.WriteLine ("static const char *config_dir = NULL;");
1025
1026                         Stream template_stream;
1027                         if (compress) {
1028                                 template_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_z.c");
1029                         } else {
1030                                 template_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template.c");
1031                         }
1032
1033                         StreamReader s = new StreamReader (template_stream);
1034                         string template = s.ReadToEnd ();
1035                         tc.Write (template);
1036
1037                         if (!nomain && custom_main == null) {
1038                                 Stream template_main_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_main.c");
1039                                 StreamReader st = new StreamReader (template_main_stream);
1040                                 string maintemplate = st.ReadToEnd ();
1041                                 tc.Write (maintemplate);
1042                         }
1043
1044                         tc.Close ();
1045
1046                         string assembler = GetEnv("AS", "as");
1047                         string as_cmd = String.Format("{0} -o {1} {2} ", assembler, temp_o, temp_s);
1048                         Execute(as_cmd);
1049
1050                         if (compile_only)
1051                                 return;
1052
1053                         if (!quiet)
1054                                 Console.WriteLine("Compiling:");
1055
1056                         if (style == "windows")
1057                         {
1058
1059                                 Func<string, string> quote = (pp) => { return "\"" + pp + "\""; };
1060
1061                                 string compiler = GetEnv("CC", "cl.exe");
1062                                 string winsdkPath = GetEnv("WINSDK", @"C:\Program Files (x86)\Windows Kits\8.1");
1063                                 string vsPath = GetEnv("VSINCLUDE", @"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC");
1064                                 string monoPath = GetEnv("MONOPREFIX", @"C:\Program Files (x86)\Mono");
1065
1066                                 string[] includes = new string[] {winsdkPath + @"\Include\um", winsdkPath + @"\Include\shared", vsPath + @"\include", monoPath + @"\include\mono-2.0", "." };
1067                                 // string[] libs = new string[] { winsdkPath + @"\Lib\winv6.3\um\x86" , vsPath + @"\lib" };
1068                                 var linkLibraries = new string[] {  "kernel32.lib",
1069                                                                                                 "version.lib",
1070                                                                                                 "Ws2_32.lib",
1071                                                                                                 "Mswsock.lib",
1072                                                                                                 "Psapi.lib",
1073                                                                                                 "shell32.lib",
1074                                                                                                 "OleAut32.lib",
1075                                                                                                 "ole32.lib",
1076                                                                                                 "winmm.lib",
1077                                                                                                 "user32.lib",
1078                                                                                                 "libvcruntime.lib",
1079                                                                                                 "advapi32.lib",
1080                                                                                                 "OLDNAMES.lib",
1081                                                                                                 "libucrt.lib" };
1082
1083                                 string glue_obj = "mkbundle_glue.obj";
1084                                 string monoLib;
1085
1086                                 if (static_link)
1087                                         monoLib = LocateFile (monoPath + @"\lib\monosgen-2.0-static.lib");
1088
1089                                 else {
1090                                         Console.WriteLine ("WARNING: Dynamically linking the Mono runtime on Windows is not a tested option.");
1091                                         monoLib = LocateFile (monoPath + @"\lib\monosgen-2.0.lib");
1092                                         LocateFile (monoPath + @"\lib\monosgen-2.0.dll"); // in this case, the .lib is just the import library, and the .dll is also needed
1093                                 }
1094
1095                                 var compilerArgs = new List<string>();
1096                                 compilerArgs.Add("/MT");
1097
1098                                 foreach (string include in includes)
1099                                         compilerArgs.Add(String.Format ("/I {0}", quote (include)));
1100
1101                                 if (!nomain || custom_main != null) {
1102                                         compilerArgs.Add(quote(temp_c));
1103                                         compilerArgs.Add(quote(temp_o));
1104                                         if (custom_main != null)
1105                                                 compilerArgs.Add(quote(custom_main));
1106                                         compilerArgs.Add(quote(monoLib));
1107                                         compilerArgs.Add("/link");
1108                                         compilerArgs.Add("/NODEFAULTLIB");
1109                                         compilerArgs.Add("/SUBSYSTEM:windows");
1110                                         compilerArgs.Add("/ENTRY:mainCRTStartup");
1111                                         compilerArgs.AddRange(linkLibraries);
1112                                         compilerArgs.Add("/out:"+ output);
1113
1114                                         string cl_cmd = String.Format("{0} {1}", compiler, String.Join(" ", compilerArgs.ToArray()));
1115                                         Execute (cl_cmd);
1116                                 }
1117                                 else
1118                                 {
1119                                         // we are just creating a .lib
1120                                         compilerArgs.Add("/c"); // compile only
1121                                         compilerArgs.Add(temp_c);
1122                                         compilerArgs.Add(String.Format("/Fo" + glue_obj)); // .obj output name
1123
1124                                         string cl_cmd = String.Format("{0} {1}", compiler, String.Join(" ", compilerArgs.ToArray()));
1125                                         Execute (cl_cmd);
1126
1127                                         string librarian = GetEnv ("LIB", "lib.exe");
1128                                         var librarianArgs = new List<string> ();
1129                                         librarianArgs.Add (String.Format ("/out:{0}.lib" + output));
1130                                         librarianArgs.Add (temp_o);
1131                                         librarianArgs.Add (glue_obj);
1132                                         librarianArgs.Add (monoLib);
1133                                         string lib_cmd = String.Format("{0} {1}", librarian, String.Join(" ", librarianArgs.ToArray()));
1134                                         Execute (lib_cmd);
1135                                 }
1136                         }
1137                         else
1138                         {
1139                                 string zlib = (compress ? "-lz" : "");
1140                                 string debugging = "-g";
1141                                 string cc = GetEnv("CC", "cc");
1142                                 string cmd = null;
1143
1144                                 if (style == "linux")
1145                                         debugging = "-ggdb";
1146                                 if (static_link)
1147                                 {
1148                                         string platform_libs;
1149                                         string smonolib;
1150                                         if (style == "osx") {
1151                                                 smonolib = "`pkg-config --variable=libdir mono-2`/libmono-2.0.a ";
1152                                                 platform_libs = "-liconv -framework Foundation ";
1153                                         } else {
1154                                                 smonolib = "-Wl,-Bstatic -lmono-2.0 -Wl,-Bdynamic ";
1155                                                 platform_libs = "";
1156                                         }
1157
1158                                         string in_tree_include = "";
1159                                         
1160                                         if (in_tree != null) {
1161                                                 smonolib = String.Format ("{0}/mono/mini/.libs/libmonosgen-2.0.a", in_tree);
1162                                                 in_tree_include = String.Format (" -I{0} ", in_tree);
1163                                         }
1164
1165                                         cmd = String.Format("{4} -o '{2}' -Wall `pkg-config --cflags mono-2` {7} {0} {3} " +
1166                                                 "`pkg-config --libs-only-L mono-2` {5} {6} " + platform_libs +
1167                                                 "`pkg-config --libs-only-l mono-2 | sed -e \"s/\\-lmono-2.0 //\"` {1} -g ",
1168                                                 temp_c, temp_o, output, zlib, cc, smonolib, String.Join (" ", aot_paths), in_tree_include);
1169                                 }
1170                                 else
1171                                 {
1172
1173                                         cmd = String.Format("{4} " + debugging + " -o '{2}' -Wall {0} `pkg-config --cflags --libs mono-2` {3} {1}",
1174                                                 temp_c, temp_o, output, zlib, cc);
1175                                 }
1176                                 Execute (cmd);
1177                         }
1178
1179                         if (!quiet)
1180                                 Console.WriteLine ("Done");
1181                 }
1182         }
1183                 } finally {
1184                         if (!keeptemp){
1185                                 if (object_out == null){
1186                                         File.Delete (temp_o);
1187                                 }
1188                                 if (!compile_only){
1189                                         File.Delete (temp_c);
1190                                 }
1191                                 if (aot_temp_dir != null)
1192                                         aot_temp_dir.Delete (true);
1193                                 File.Delete (temp_s);
1194                         }
1195                 }
1196         }
1197         
1198         static List<string> LoadAssemblies (List<string> sources)
1199         {
1200                 List<string> assemblies = new List<string> ();
1201                 bool error = false;
1202
1203                 foreach (string name in sources){
1204                         try {
1205                                 Assembly a = LoadAssemblyFile (name);
1206
1207                                 if (a == null){
1208                                         error = true;
1209                                         continue;
1210                                 }
1211                         
1212                                 assemblies.Add (a.CodeBase);
1213                         } catch (Exception) {
1214                                 if (skip_scan) {
1215                                         if (!quiet)
1216                                                 Console.WriteLine ("File will not be scanned: {0}", name);
1217                                         assemblies.Add (new Uri (new FileInfo (name).FullName).ToString ());
1218                                 } else {
1219                                         throw;
1220                                 }
1221                         }
1222                 }
1223
1224                 if (error) {
1225                         Error ("Couldn't load one or more of the assemblies.");
1226                         Environment.Exit (1);
1227                 }
1228
1229                 return assemblies;
1230         }
1231
1232         static void LoadLocalizedAssemblies (List<string> assemblies)
1233         {
1234                 var other = i18n.Select (x => "I18N." + x + (x.Length > 0 ? "." : "") + "dll");
1235                 string error = null;
1236
1237                 foreach (string name in other) {
1238                         try {
1239                                 Assembly a = LoadAssembly (name);
1240
1241                                 if (a == null) {
1242                                         error = "Failed to load " + name;
1243                                         continue;
1244                                 }
1245
1246                                 assemblies.Add (a.CodeBase);
1247                         } catch (Exception) {
1248                                 if (skip_scan) {
1249                                         if (!quiet)
1250                                                 Console.WriteLine ("File will not be scanned: {0}", name);
1251                                         assemblies.Add (new Uri (new FileInfo (name).FullName).ToString ());
1252                                 } else {
1253                                         throw;
1254                                 }
1255                         }
1256                 }
1257
1258                 if (error != null) {
1259                         Console.Error.WriteLine ("Failure to load i18n assemblies, the following directories were searched for the assemblies:");
1260                         foreach (var path in link_paths){
1261                                 Console.Error.WriteLine ("   Path: " + path);
1262                         }
1263                         if (custom_mode){
1264                                 Console.WriteLine ("In Custom mode, you need to provide the directory to lookup assemblies from using -L");
1265                         }
1266                         
1267                         Error ("Couldn't load one or more of the i18n assemblies: " + error);
1268                         Environment.Exit (1);
1269                 }
1270         }
1271
1272         
1273         static readonly Universe universe = new Universe ();
1274         static readonly Dictionary<string, string> loaded_assemblies = new Dictionary<string, string> ();
1275
1276         public static string GetAssemblyName (string path)
1277         {
1278                 string resourcePathSeparator = style == "windows" ? "\\\\" : "/";
1279                 string name = Path.GetFileName (path);
1280
1281                 // A bit of a hack to support satellite assemblies. They all share the same name but
1282                 // are placed in subdirectories named after the locale they implement. Also, all of
1283                 // them end in .resources.dll, therefore we can use that to detect the circumstances.
1284                 if (name.EndsWith (".resources.dll", StringComparison.OrdinalIgnoreCase)) {
1285                         string dir = Path.GetDirectoryName (path);
1286                         int idx = dir.LastIndexOf (Path.DirectorySeparatorChar);
1287                         if (idx >= 0) {
1288                                 name = dir.Substring (idx + 1) + resourcePathSeparator + name;
1289                                 Console.WriteLine ($"Storing satellite assembly '{path}' with name '{name}'");
1290                         } else if (!quiet)
1291                                 Console.WriteLine ($"Warning: satellite assembly {path} doesn't have locale path prefix, name conflicts possible");
1292                 }
1293
1294                 return name;
1295         }
1296
1297         static bool QueueAssembly (List<string> files, string codebase)
1298         {
1299                 //Console.WriteLine ("CODE BASE IS {0}", codebase);
1300                 if (files.Contains (codebase))
1301                         return true;
1302
1303                 var path = new Uri(codebase).LocalPath;
1304                 var name = GetAssemblyName (path);
1305                 string found;
1306                 if (loaded_assemblies.TryGetValue (name, out found)) {
1307                         Error ("Duplicate assembly name `{0}'. Both `{1}' and `{2}' use same assembly name.", name, path, found);
1308                         return false;
1309                 }
1310
1311                 loaded_assemblies.Add (name, path);
1312
1313                 files.Add (codebase);
1314                 if (!autodeps)
1315                         return true;
1316                 try {
1317                         Assembly a = universe.LoadFile (path);
1318                         if (a == null) {
1319                                 Error ("Unable to to load assembly `{0}'", path);
1320                                 return false;
1321                         }
1322
1323                         foreach (AssemblyName an in a.GetReferencedAssemblies ()) {
1324                                 a = LoadAssembly (an.Name);
1325                                 if (a == null) {
1326                                         Error ("Unable to load assembly `{0}' referenced by `{1}'", an.Name, path);
1327                                         return false;
1328                                 }
1329
1330                                 if (!QueueAssembly (files, a.CodeBase))
1331                                         return false;
1332                         }
1333                 } catch (Exception) {
1334                         if (!skip_scan)
1335                                 throw;
1336                 }
1337
1338                 return true;
1339         }
1340
1341         //
1342         // Loads an assembly from a specific path
1343         //
1344         static Assembly LoadAssemblyFile (string assembly)
1345         {
1346                 Assembly a = null;
1347                 
1348                 try {
1349                         if (!quiet)
1350                                 Console.WriteLine ("Attempting to load assembly: {0}", assembly);
1351                         a = universe.LoadFile (assembly);
1352                         if (!quiet)
1353                                 Console.WriteLine ("Assembly {0} loaded successfully.", assembly);
1354                         
1355                 } catch (FileNotFoundException){
1356                         Error ($"Cannot find assembly `{assembly}'");
1357                 } catch (IKVM.Reflection.BadImageFormatException f) {
1358                         if (skip_scan)
1359                                 throw;
1360                         Error ($"Cannot load assembly (bad file format) " + f.Message);
1361                 } catch (FileLoadException f){
1362                         Error ($"Cannot load assembly " + f.Message);
1363                 } catch (ArgumentNullException){
1364                         Error( $"Cannot load assembly (null argument)");
1365                 }
1366                 return a;
1367         }
1368
1369         //
1370         // Loads an assembly from any of the link directories provided
1371         //
1372         static Assembly LoadAssembly (string assembly)
1373         {
1374                 string total_log = "";
1375                 foreach (string dir in link_paths){
1376                         string full_path = Path.Combine (dir, assembly);
1377                         if (!quiet)
1378                                 Console.WriteLine ("Attempting to load assembly from: " + full_path);
1379                         if (!assembly.EndsWith (".dll") && !assembly.EndsWith (".exe"))
1380                                 full_path += ".dll";
1381                         
1382                         try {
1383                                 var a = universe.LoadFile (full_path);
1384                                 return a;
1385                         } catch (FileNotFoundException ff) {
1386                                 total_log += ff.FusionLog;
1387                                 continue;
1388                         }
1389                 }
1390                 if (!quiet)
1391                         Console.WriteLine ("Log: \n" + total_log);
1392                 return null;
1393         }
1394         
1395         static void Error (string msg, params object [] args)
1396         {
1397                 Console.Error.WriteLine ("ERROR: {0}", string.Format (msg, args));
1398                 Environment.Exit (1);
1399         }
1400
1401         static void Help ()
1402         {
1403                 Console.WriteLine ("Usage is: mkbundle [options] assembly1 [assembly2...]\n\n" +
1404                                    "Options:\n" +
1405                                    "    --config F           Bundle system config file `F'\n" +
1406                                    "    --config-dir D       Set MONO_CFG_DIR to `D'\n" +
1407                                    "    --deps               Turns on automatic dependency embedding (default on simple)\n" +
1408                                    "    -L path              Adds `path' to the search path for assemblies\n" +
1409                                    "    --machine-config F   Use the given file as the machine.config for the application.\n" +
1410                                    "    -o out               Specifies output filename\n" +
1411                                    "    --nodeps             Turns off automatic dependency embedding (default on custom)\n" +
1412                                    "    --skip-scan          Skip scanning assemblies that could not be loaded (but still embed them).\n" +
1413                                    "    --i18n ENCODING      none, all or comma separated list of CJK, MidWest, Other, Rare, West.\n" +
1414                                    "    -v                   Verbose output\n" + 
1415                                    "    --bundled-header     Do not attempt to include 'mono-config.h'. Define the entry points directly in the generated code\n" +
1416                                    "\n" + 
1417                                    "--simple   Simple mode does not require a C toolchain and can cross compile\n" + 
1418                                    "    --cross TARGET       Generates a binary for the given TARGET\n"+
1419                                    "    --env KEY=VALUE      Hardcodes an environment variable for the target\n" +
1420                                    "    --fetch-target NAME  Downloads the target SDK from the remote server\n" + 
1421                                    "    --library [LIB,]PATH Bundles the specified dynamic library to be used at runtime\n" +
1422                                    "                         LIB is optional shortname for file located at PATH\n" + 
1423                                    "    --list-targets       Lists available targets on the remote server\n" +
1424                                    "    --local-targets      Lists locally available targets\n" +
1425                                    "    --options OPTIONS    Embed the specified Mono command line options on target\n" +
1426                                    "    --runtime RUNTIME    Manually specifies the Mono runtime to use\n" +
1427                                    "    --sdk PATH           Use a Mono SDK root location instead of a target\n" + 
1428                                    "    --target-server URL  Specified a server to download targets from, default is " + target_server + "\n" +
1429                                    "\n" +
1430                                    "--custom   Builds a custom launcher, options for --custom\n" +
1431                                    "    -c                   Produce stub only, do not compile\n" +
1432                                    "    -oo obj              Specifies output filename for helper object file\n" +
1433                                    "    --dos2unix[=true|false]\n" +
1434                                    "                         When no value provided, or when `true` specified\n" +
1435                                    "                         `dos2unix` will be invoked to convert paths on Windows.\n" +
1436                                    "                         When `--dos2unix=false` used, dos2unix is NEVER used.\n" +
1437                                    "    --keeptemp           Keeps the temporary files\n" +
1438                                    "    --static             Statically link to mono libs\n" +
1439                                    "    --nomain             Don't include a main() function, for libraries\n" +
1440                                    "    --custom-main C      Link the specified compilation unit (.c or .obj) with entry point/init code\n" +
1441                                    "    -z                   Compress the assemblies before embedding.\n" +
1442                                    "    --static-ctor ctor   Add a constructor call to the supplied function.\n" +
1443                                    "                         You need zlib development headers and libraries.\n");
1444         }
1445
1446         [DllImport ("libc")]
1447         static extern int system (string s);
1448         [DllImport ("libc")]
1449         static extern int uname (IntPtr buf);
1450                 
1451         static void DetectOS ()
1452         {
1453                 if (!IsUnix) {
1454                         os_message = "OS is: Windows";
1455                         style = "windows";
1456                         return;
1457                 }
1458
1459                 IntPtr buf = Marshal.AllocHGlobal (8192);
1460                 if (uname (buf) != 0){
1461                         os_message = "Warning: Unable to detect OS";
1462                         Marshal.FreeHGlobal (buf);
1463                         return;
1464                 }
1465                 string os = Marshal.PtrToStringAnsi (buf);
1466                 os_message = "OS is: " + os;
1467                 if (os == "Darwin")
1468                         style = "osx";
1469                 
1470                 Marshal.FreeHGlobal (buf);
1471         }
1472
1473         static bool IsUnix {
1474                 get {
1475                         int p = (int) Environment.OSVersion.Platform;
1476                         return ((p == 4) || (p == 128) || (p == 6));
1477                 }
1478         }
1479
1480
1481         static string EncodeAotSymbol (string symbol)
1482         {
1483                 var sb = new StringBuilder ();
1484                 /* This mimics what the aot-compiler does */
1485                 foreach (var b in System.Text.Encoding.UTF8.GetBytes (symbol)) {
1486                         char c = (char) b;
1487                         if ((c >= '0' && c <= '9') ||
1488                                 (c >= 'a' && c <= 'z') ||
1489                                 (c >= 'A' && c <= 'Z')) {
1490                                 sb.Append (c);
1491                                 continue;
1492                         }
1493                         sb.Append ('_');
1494                 }
1495                 return sb.ToString ();
1496         }
1497
1498         static void AotCompile (List<string> files)
1499         {
1500                 if (aot_runtime == null)
1501                         aot_runtime = runtime;
1502
1503                 if (aot_runtime == null) {
1504                         Error ("You must specify at least one aot runtime with --runtime or --cross or --aot_runtime when AOT compiling");
1505                         Environment.Exit (1);
1506                 }
1507
1508                 var aot_mode_string = "";
1509                 if (aot_mode != null)
1510                         aot_mode_string = "," + aot_mode;
1511
1512                 var dedup_mode_string = "";
1513                 StringBuilder all_assemblies = null;
1514                 if (aot_dedup_assembly != null) {
1515                         dedup_mode_string = ",dedup-skip";
1516                         all_assemblies = new StringBuilder("");
1517                 }
1518
1519                 Console.WriteLine ("Aoting files:");
1520
1521                 for (int i=0; i < files.Count; i++) {
1522                         var file_name = files [i];
1523                         string path = LocateFile (new Uri (file_name).LocalPath);
1524                         string outPath = String.Format ("{0}.aot_out", path);
1525                         aot_paths.Add (outPath);
1526                         var name = System.Reflection.Assembly.LoadFrom(path).GetName().Name;
1527                         aot_names.Add (EncodeAotSymbol (name));
1528
1529                         if (aot_dedup_assembly != null) {
1530                                 all_assemblies.Append (path);
1531                                 all_assemblies.Append (" ");
1532                                 Execute (String.Format ("MONO_PATH={0} {1} --aot={2},outfile={3}{4}{5} {6}",
1533                                         Path.GetDirectoryName (path), aot_runtime, aot_args, outPath, aot_mode_string, dedup_mode_string, path));
1534                         } else {
1535                                 Execute (String.Format ("MONO_PATH={0} {1} --aot={2},outfile={3}{4} {5}",
1536                                         Path.GetDirectoryName (path), aot_runtime, aot_args, outPath, aot_mode_string, path));
1537                         }
1538                 }
1539                 if (aot_dedup_assembly != null) {
1540                         var filePath = new Uri (aot_dedup_assembly).LocalPath;
1541                         string path = LocateFile (filePath);
1542                         dedup_mode_string = String.Format (",dedup-include={0}", Path.GetFileName(filePath));
1543                         string outPath = String.Format ("{0}.aot_out", path);
1544                         Execute (String.Format ("MONO_PATH={7} {0} --aot={1},outfile={2}{3}{4} {5} {6}",
1545                                 aot_runtime, aot_args, outPath, aot_mode_string, dedup_mode_string, path, all_assemblies.ToString (), Path.GetDirectoryName (path)));
1546                 }
1547
1548                 if ((aot_mode == "full" || aot_mode == "llvmonly") && cil_strip_path != null) {
1549                         for (int i=0; i < files.Count; i++) {
1550                                 var in_name = new Uri (files [i]).LocalPath;
1551                                 var cmd = String.Format ("{0} {1} {2}", aot_runtime, cil_strip_path, in_name);
1552                                 Execute (cmd);
1553                         }
1554                 }
1555         }
1556
1557         static void LinkManaged (List <string> files, string outDir)
1558         {
1559                 if (managed_linker_path == null)
1560                         return;
1561
1562                 var paths = new StringBuilder ("");
1563                 foreach (var file in files) {
1564                         paths.Append (" -a  ");
1565                         paths.Append (new Uri (file).LocalPath);
1566                 }
1567
1568                 var cmd = String.Format ("{0} {1} -b true -out {2} {3} -c link -p copy ", runtime, managed_linker_path, outDir, paths.ToString ());
1569                 Execute (cmd);
1570         }
1571
1572         static void PreprocessAssemblies (List <string> chosenFiles, List <string> files)
1573         {
1574                 if (aot_mode == "" || (cil_strip_path == null && managed_linker_path == null))
1575                         return;
1576
1577                 var temp_dir_name = Path.Combine(Directory.GetCurrentDirectory(), "temp_assemblies");
1578                 aot_temp_dir = new DirectoryInfo (temp_dir_name);
1579                 if (aot_temp_dir.Exists) {
1580                         Console.WriteLine ("Removing previous build cache at {0}", temp_dir_name);
1581                         aot_temp_dir.Delete (true);
1582                 }
1583                 aot_temp_dir.Create ();
1584
1585                 //if (managed_linker_path != null) {
1586                         //LinkManaged (chosenFiles, temp_dir);
1587
1588                         //// Replace list with new list of files
1589                         //files.Clear ();
1590                         //Console.WriteLine ("Iterating {0}", temp_dir);
1591                         //aot_temp_dir = new DirectoryInfo (temp_dir);
1592                         //foreach (var file in aot_temp_dir.GetFiles ()) {
1593                                 //files.Append (String.Format ("file:///{0}", file));
1594                                 //Console.WriteLine (String.Format ("file:///{0}", file));
1595                         //}
1596                         //return;
1597                 //}
1598
1599                 // Fix file references
1600                 for (int i=0; i < files.Count; i++) {
1601                         var in_name = new Uri (files [i]).LocalPath;
1602                         var out_name = Path.Combine (temp_dir_name, Path.GetFileName (in_name));
1603                         File.Copy (in_name, out_name);
1604                         files [i] = out_name;
1605                         if (in_name == aot_dedup_assembly)
1606                                 aot_dedup_assembly = out_name;
1607                 }
1608         }
1609
1610
1611         static void Execute (string cmdLine)
1612         {
1613                 if (IsUnix) {
1614                         if (!quiet)
1615                                 Console.WriteLine ("[execute cmd]: " + cmdLine);
1616                         int ret = system (cmdLine);
1617                         if (ret != 0)
1618                         {
1619                                 Error(String.Format("[Fail] {0}", ret));
1620                         }
1621                         return;
1622                 }
1623
1624                 // on Windows, we have to pipe the output of a
1625                 // `cmd` interpolation to dos2unix, because the shell does not
1626                 // strip the CRLFs generated by the native pkg-config distributed
1627                 // with Mono.
1628                 //
1629                 // But if it's *not* on cygwin, just skip it.
1630
1631                 // check if dos2unix is applicable.
1632                 if (use_dos2unix == true)
1633                         try {
1634                         var info = new ProcessStartInfo ("dos2unix");
1635                         info.CreateNoWindow = true;
1636                         info.RedirectStandardInput = true;
1637                         info.UseShellExecute = false;
1638                         var dos2unix = Process.Start (info);
1639                         dos2unix.StandardInput.WriteLine ("aaa");
1640                         dos2unix.StandardInput.WriteLine ("\u0004");
1641                         dos2unix.StandardInput.Close ();
1642                         dos2unix.WaitForExit ();
1643                         if (dos2unix.ExitCode == 0)
1644                                 use_dos2unix = true;
1645                 } catch {
1646                         Console.WriteLine("Warning: dos2unix not found");
1647                         use_dos2unix = false;
1648                 }
1649
1650                 if (use_dos2unix == null)
1651                         use_dos2unix = false;
1652
1653                 ProcessStartInfo psi = new ProcessStartInfo();
1654                 psi.UseShellExecute = false;
1655
1656                 // if there is no dos2unix, just run cmd /c.
1657                 if (use_dos2unix == false)
1658                 {
1659                         psi.FileName = "cmd";
1660                         psi.Arguments = String.Format("/c \"{0}\"", cmdLine);
1661                 }
1662                 else
1663                 {
1664                         psi.FileName = "sh";
1665                         StringBuilder b = new StringBuilder();
1666                         int count = 0;
1667                         for (int i = 0; i < cmdLine.Length; i++)
1668                         {
1669                                 if (cmdLine[i] == '`')
1670                                 {
1671                                         if (count % 2 != 0)
1672                                         {
1673                                                 b.Append("|dos2unix");
1674                                         }
1675                                         count++;
1676                                 }
1677                                 b.Append(cmdLine[i]);
1678                         }
1679                         cmdLine = b.ToString();
1680                         psi.Arguments = String.Format("-c \"{0}\"", cmdLine);
1681                 }
1682
1683                 if (!quiet)
1684                         Console.WriteLine(cmdLine);
1685                 using (Process p = Process.Start (psi)) {
1686                         p.WaitForExit ();
1687                         int ret = p.ExitCode;
1688                         if (ret != 0){
1689                                 Error ("[Fail] {0}", ret);
1690                         }
1691                 }
1692         }
1693
1694         static string GetEnv(string name, string defaultValue)
1695         {
1696                 string val = Environment.GetEnvironmentVariable(name);
1697                 if (val != null)
1698                 {
1699                         if (!quiet)
1700                                 Console.WriteLine("{0} = {1}", name, val);
1701                 }
1702                 else
1703                 {
1704                         val = defaultValue;
1705                         if (!quiet)
1706                                 Console.WriteLine("{0} = {1} (default)", name, val);
1707                 }
1708                 return val;
1709         }
1710
1711         static string LocateFile(string default_path)
1712         {
1713                 var override_path = Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(default_path));
1714                 if (File.Exists(override_path))
1715                         return override_path;
1716                 else if (File.Exists(default_path))
1717                         return default_path;
1718                 else
1719                         throw new FileNotFoundException(default_path);
1720         }
1721 }