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