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