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