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