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