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