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