Merge pull request #141 from LogosBible/surrogtate
[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 //
11 using System;
12 using System.Diagnostics;
13 using System.Xml;
14 using System.Collections;
15 using System.Reflection;
16 using System.IO;
17 using System.Runtime.InteropServices;
18 using System.Text;
19 using Mono.Unix;
20 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
21
22 class MakeBundle {
23         static string output = "a.out";
24         static string object_out = null;
25         static ArrayList link_paths = new ArrayList ();
26         static bool autodeps = false;
27         static bool keeptemp = false;
28         static bool compile_only = false;
29         static bool static_link = false;
30         static string config_file = null;
31         static string machine_config_file = null;
32         static string config_dir = null;
33         static string style = "linux";
34         static bool compress;
35         static bool nomain;
36         
37         static int Main (string [] args)
38         {
39                 ArrayList sources = new ArrayList ();
40                 int top = args.Length;
41                 link_paths.Add (".");
42
43                 DetectOS ();
44                 
45                 for (int i = 0; i < top; i++){
46                         switch (args [i]){
47                         case "--help": case "-h": case "-?":
48                                 Help ();
49                                 return 1;
50
51                         case "-c":
52                                 compile_only = true;
53                                 break;
54                                 
55                         case "-o": 
56                                 if (i+1 == top){
57                                         Help (); 
58                                         return 1;
59                                 }
60                                 output = args [++i];
61                                 break;
62
63                         case "-oo":
64                                 if (i+1 == top){
65                                         Help (); 
66                                         return 1;
67                                 }
68                                 object_out = args [++i];
69                                 break;
70
71                         case "-L":
72                                 if (i+1 == top){
73                                         Help (); 
74                                         return 1;
75                                 }
76                                 link_paths.Add (args [++i]);
77                                 break;
78
79                         case "--nodeps":
80                                 autodeps = false;
81                                 break;
82
83                         case "--deps":
84                                 autodeps = true;
85                                 break;
86
87                         case "--keeptemp":
88                                 keeptemp = true;
89                                 break;
90                         case "--static":
91                                 if (style == "windows") {
92                                         Console.Error.WriteLine ("The option `{0}' is not supported on this platform.", args [i]);
93                                         return 1;
94                                 }
95                                 static_link = true;
96                                 Console.WriteLine ("Note that statically linking the LGPL Mono runtime has more licensing restrictions than dynamically linking.");
97                                 Console.WriteLine ("See http://www.mono-project.com/Licensing for details on licensing.");
98                                 break;
99                         case "--config":
100                                 if (i+1 == top) {
101                                         Help ();
102                                         return 1;
103                                 }
104
105                                 config_file = args [++i];
106                                 break;
107                         case "--machine-config":
108                                 if (i+1 == top) {
109                                         Help ();
110                                         return 1;
111                                 }
112
113                                 machine_config_file = args [++i];
114
115                                 Console.WriteLine ("WARNING:\n  Check that the machine.config file you are bundling\n  doesn't contain sensitive information specific to this machine.");
116                                 break;
117                         case "--config-dir":
118                                 if (i+1 == top) {
119                                         Help ();
120                                         return 1;
121                                 }
122
123                                 config_dir = args [++i];
124                                 break;
125                         case "-z":
126                                 compress = true;
127                                 break;
128                         case "--nomain":
129                                 nomain = true;
130                                 break;
131                         default:
132                                 sources.Add (args [i]);
133                                 break;
134                         }
135                 }
136
137                 Console.WriteLine ("Sources: {0} Auto-dependencies: {1}", sources.Count, autodeps);
138                 if (sources.Count == 0 || output == null) {
139                         Help ();
140                         Environment.Exit (1);
141                 }
142
143                 ArrayList assemblies = LoadAssemblies (sources);
144                 ArrayList files = new ArrayList ();
145                 foreach (Assembly a in assemblies)
146                         QueueAssembly (files, a.CodeBase);
147
148                 GenerateBundles (files);
149                 //GenerateJitWrapper ();
150                 
151                 return 0;
152         }
153
154         static void WriteSymbol (StreamWriter sw, string name, long size)
155         {
156                 switch (style){
157                 case "linux":
158                         sw.WriteLine (
159                                 ".globl {0}\n" +
160                                 "\t.section .rodata\n" +
161                                 "\t.p2align 5\n" +
162                                 "\t.type {0}, @object\n" +
163                                 "\t.size {0}, {1}\n" +
164                                 "{0}:\n",
165                                 name, size);
166                         break;
167                 case "osx":
168                         sw.WriteLine (
169                                 "\t.section __TEXT,__text,regular,pure_instructions\n" + 
170                                 "\t.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32\n" + 
171                                 "\t.globl _{0}\n" +
172                                 "\t.data\n" +
173                                 "\t.align 4\n" +
174                                 "_{0}:\n",
175                                 name, size);
176                         break;
177                 case "windows":
178                         sw.WriteLine (
179                                 ".globl _{0}\n" +
180                                 "\t.section .rdata,\"dr\"\n" +
181                                 "\t.align 32\n" +
182                                 "_{0}:\n",
183                                 name, size);
184                         break;
185                 }
186         }
187         
188         static void GenerateBundles (ArrayList files)
189         {
190                 string temp_s = "temp.s"; // Path.GetTempFileName ();
191                 string temp_c = "temp.c";
192                 string temp_o = "temp.o";
193
194                 if (compile_only)
195                         temp_c = output;
196                 if (object_out != null)
197                         temp_o = object_out;
198                 
199                 try {
200                         ArrayList c_bundle_names = new ArrayList ();
201                         ArrayList config_names = new ArrayList ();
202                         byte [] buffer = new byte [8192];
203
204                         using (StreamWriter ts = new StreamWriter (File.Create (temp_s))) {
205                         using (StreamWriter tc = new StreamWriter (File.Create (temp_c))) {
206                         string prog = null;
207
208                         tc.WriteLine ("/* This source code was produced by mkbundle, do not edit */");
209                         tc.WriteLine ("#include <mono/metadata/mono-config.h>");
210                         tc.WriteLine ("#include <mono/metadata/assembly.h>\n");
211
212                         if (compress) {
213                                 tc.WriteLine ("typedef struct _compressed_data {");
214                                 tc.WriteLine ("\tMonoBundledAssembly assembly;");
215                                 tc.WriteLine ("\tint compressed_size;");
216                                 tc.WriteLine ("} CompressedAssembly;\n");
217                         }
218
219                         foreach (string url in files){
220                                 string fname = new Uri (url).LocalPath;
221                                 string aname = Path.GetFileName (fname);
222                                 string encoded = aname.Replace ("-", "_").Replace (".", "_");
223
224                                 if (prog == null)
225                                         prog = aname;
226                                 
227                                 Console.WriteLine ("   embedding: " + fname);
228                                 
229                                 Stream stream = File.OpenRead (fname);
230
231                                 long real_size = stream.Length;
232                                 int n;
233                                 if (compress) {
234                                         MemoryStream ms = new MemoryStream ();
235                                         DeflaterOutputStream deflate = new DeflaterOutputStream (ms);
236                                         while ((n = stream.Read (buffer, 0, 8192)) != 0){
237                                                 deflate.Write (buffer, 0, n);
238                                         }
239                                         stream.Close ();
240                                         deflate.Finish ();
241                                         byte [] bytes = ms.GetBuffer ();
242                                         stream = new MemoryStream (bytes, 0, (int) ms.Length, false, false);
243                                 }
244
245                                 WriteSymbol (ts, "assembly_data_" + encoded, stream.Length);
246
247                                 while ((n = stream.Read (buffer, 0, 8192)) != 0){
248                                         for (int i = 0; i < n; i++){
249                                                 ts.Write ("\t.byte {0}\n", buffer [i]);
250                                         }
251                                 }
252                                 ts.WriteLine ();
253
254                                 if (compress) {
255                                         tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
256                                         tc.WriteLine ("static CompressedAssembly assembly_bundle_{0} = {{{{\"{1}\"," +
257                                                         " assembly_data_{0}, {2}}}, {3}}};",
258                                                       encoded, aname, real_size, stream.Length);
259                                         double ratio = ((double) stream.Length * 100) / real_size;
260                                         Console.WriteLine ("   compression ratio: {0:.00}%", ratio);
261                                 } else {
262                                         tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
263                                         tc.WriteLine ("static const MonoBundledAssembly assembly_bundle_{0} = {{\"{1}\", assembly_data_{0}, {2}}};",
264                                                       encoded, aname, real_size);
265                                 }
266                                 stream.Close ();
267
268                                 c_bundle_names.Add ("assembly_bundle_" + encoded);
269
270                                 try {
271                                         FileStream cf = File.OpenRead (fname + ".config");
272                                         Console.WriteLine (" config from: " + fname + ".config");
273                                         tc.WriteLine ("extern const unsigned char assembly_config_{0} [];", encoded);
274                                         WriteSymbol (ts, "assembly_config_" + encoded, cf.Length);
275                                         while ((n = cf.Read (buffer, 0, 8192)) != 0){
276                                                 for (int i = 0; i < n; i++){
277                                                         ts.Write ("\t.byte {0}\n", buffer [i]);
278                                                 }
279                                         }
280                                         ts.WriteLine ();
281                                         config_names.Add (new string[] {aname, encoded});
282                                 } catch (FileNotFoundException) {
283                                         /* we ignore if the config file doesn't exist */
284                                 }
285
286                         }
287                         if (config_file != null){
288                                 FileStream conf;
289                                 try {
290                                         conf = File.OpenRead (config_file);
291                                 } catch {
292                                         Error (String.Format ("Failure to open {0}", config_file));
293                                         return;
294                                 }
295                                 Console.WriteLine ("System config from: " + config_file);
296                                 tc.WriteLine ("extern const unsigned char system_config;");
297                                 WriteSymbol (ts, "system_config", config_file.Length);
298
299                                 int n;
300                                 while ((n = conf.Read (buffer, 0, 8192)) != 0){
301                                         for (int i = 0; i < n; i++){
302                                                 ts.Write ("\t.byte {0}\n", buffer [i]);
303                                         }
304                                 }
305                                 // null terminator
306                                 ts.Write ("\t.byte 0\n");
307                                 ts.WriteLine ();
308                         }
309
310                         if (machine_config_file != null){
311                                 FileStream conf;
312                                 try {
313                                         conf = File.OpenRead (machine_config_file);
314                                 } catch {
315                                         Error (String.Format ("Failure to open {0}", machine_config_file));
316                                         return;
317                                 }
318                                 Console.WriteLine ("Machine config from: " + machine_config_file);
319                                 tc.WriteLine ("extern const unsigned char machine_config;");
320                                 WriteSymbol (ts, "machine_config", machine_config_file.Length);
321
322                                 int n;
323                                 while ((n = conf.Read (buffer, 0, 8192)) != 0){
324                                         for (int i = 0; i < n; i++){
325                                                 ts.Write ("\t.byte {0}\n", buffer [i]);
326                                         }
327                                 }
328                                 // null terminator
329                                 ts.Write ("\t.byte 0\n");
330                                 ts.WriteLine ();
331                         }
332                         ts.Close ();
333                         
334                         Console.WriteLine ("Compiling:");
335                         string cmd = String.Format ("{0} -o {1} {2} ", GetEnv ("AS", "as"), temp_o, temp_s);
336                         int ret = Execute (cmd);
337                         if (ret != 0){
338                                 Error ("[Fail]");
339                                 return;
340                         }
341
342                         if (compress)
343                                 tc.WriteLine ("\nstatic const CompressedAssembly *compressed [] = {");
344                         else
345                                 tc.WriteLine ("\nstatic const MonoBundledAssembly *bundled [] = {");
346
347                         foreach (string c in c_bundle_names){
348                                 tc.WriteLine ("\t&{0},", c);
349                         }
350                         tc.WriteLine ("\tNULL\n};\n");
351                         tc.WriteLine ("static char *image_name = \"{0}\";", prog);
352
353                         tc.WriteLine ("\nstatic void install_dll_config_files (void) {\n");
354                         foreach (string[] ass in config_names){
355                                 tc.WriteLine ("\tmono_register_config_for_assembly (\"{0}\", assembly_config_{1});\n", ass [0], ass [1]);
356                         }
357                         if (config_file != null)
358                                 tc.WriteLine ("\tmono_config_parse_memory (&system_config);\n");
359                         if (machine_config_file != null)
360                                 tc.WriteLine ("\tmono_register_machine_config (&machine_config);\n");
361                         tc.WriteLine ("}\n");
362
363                         if (config_dir != null)
364                                 tc.WriteLine ("static const char *config_dir = \"{0}\";", config_dir);
365                         else
366                                 tc.WriteLine ("static const char *config_dir = NULL;");
367
368                         Stream template_stream;
369                         if (compress) {
370                                 template_stream = Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_z.c");
371                         } else {
372                                 template_stream = Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template.c");
373                         }
374
375                         StreamReader s = new StreamReader (template_stream);
376                         string template = s.ReadToEnd ();
377                         tc.Write (template);
378
379                         if (!nomain) {
380                                 Stream template_main_stream = Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_main.c");
381                                 StreamReader st = new StreamReader (template_main_stream);
382                                 string maintemplate = st.ReadToEnd ();
383                                 tc.Write (maintemplate);
384                         }
385                         
386                         tc.Close ();
387
388                         if (compile_only)
389                                 return;
390
391                         string zlib = (compress ? "-lz" : "");
392                         string debugging = "-g";
393                         string cc = GetEnv ("CC", IsUnix ? "cc" : "gcc -mno-cygwin");
394
395                         if (style == "linux")
396                                 debugging = "-ggdb";
397                         if (static_link) {
398                                 string smonolib;
399                                 if (style == "osx")
400                                         smonolib = "`pkg-config --variable=libdir mono-2`/libmono-2.0.a ";
401                                 else
402                                         smonolib = "-Wl,-Bstatic -lmono-2.0 -Wl,-Bdynamic ";
403                                 cmd = String.Format ("{4} -o {2} -Wall `pkg-config --cflags mono-2` {0} {3} " +
404                                                      "`pkg-config --libs-only-L mono-2` " + smonolib +
405                                                      "`pkg-config --libs-only-l mono-2 | sed -e \"s/\\-lmono-2.0 //\"` {1}",
406                                                      temp_c, temp_o, output, zlib, cc);
407                         } else {
408                                 
409                                 cmd = String.Format ("{4} " + debugging + " -o {2} -Wall {0} `pkg-config --cflags --libs mono-2` {3} {1}",
410                                                      temp_c, temp_o, output, zlib, cc);
411                         }
412                             
413                         ret = Execute (cmd);
414                         if (ret != 0){
415                                 Error ("[Fail]");
416                                 return;
417                         }
418                         Console.WriteLine ("Done");
419                         }
420                         }
421                 } finally {
422                         if (!keeptemp){
423                                 if (object_out == null){
424                                         File.Delete (temp_o);
425                                 }
426                                 if (!compile_only){
427                                         File.Delete (temp_c);
428                                 }
429                                 File.Delete (temp_s);
430                         }
431                 }
432         }
433         
434         static ArrayList LoadAssemblies (ArrayList sources)
435         {
436                 ArrayList assemblies = new ArrayList ();
437                 bool error = false;
438                 
439                 foreach (string name in sources){
440                         Assembly a = LoadAssembly (name);
441
442                         if (a == null){
443                                 error = true;
444                                 continue;
445                         }
446                         
447                         assemblies.Add (a);
448                 }
449
450                 if (error)
451                         Environment.Exit (1);
452
453                 return assemblies;
454         }
455         
456         static void QueueAssembly (ArrayList files, string codebase)
457         {
458                 if (files.Contains (codebase))
459                         return;
460
461                 files.Add (codebase);
462                 Assembly a = Assembly.LoadFrom (new Uri(codebase).LocalPath);
463
464                 if (!autodeps)
465                         return;
466                 
467                 foreach (AssemblyName an in a.GetReferencedAssemblies ()) {
468                         a = Assembly.Load (an);
469                         QueueAssembly (files, a.CodeBase);
470                 }
471         }
472
473         static Assembly LoadAssembly (string assembly)
474         {
475                 Assembly a;
476                 
477                 try {
478                         char[] path_chars = { '/', '\\' };
479                         
480                         if (assembly.IndexOfAny (path_chars) != -1) {
481                                 a = Assembly.LoadFrom (assembly);
482                         } else {
483                                 string ass = assembly;
484                                 if (ass.EndsWith (".dll"))
485                                         ass = assembly.Substring (0, assembly.Length - 4);
486                                 a = Assembly.Load (ass);
487                         }
488                         return a;
489                 } catch (FileNotFoundException){
490                         string total_log = "";
491                         
492                         foreach (string dir in link_paths){
493                                 string full_path = Path.Combine (dir, assembly);
494                                 if (!assembly.EndsWith (".dll") && !assembly.EndsWith (".exe"))
495                                         full_path += ".dll";
496                                 
497                                 try {
498                                         a = Assembly.LoadFrom (full_path);
499                                         return a;
500                                 } catch (FileNotFoundException ff) {
501                                         total_log += ff.FusionLog;
502                                         continue;
503                                 }
504                         }
505                         Error ("Cannot find assembly `" + assembly + "'" );
506                         Console.WriteLine ("Log: \n" + total_log);
507                 } catch (BadImageFormatException f) {
508                         Error ("Cannot load assembly (bad file format)" + f.FusionLog);
509                 } catch (FileLoadException f){
510                         Error ("Cannot load assembly " + f.FusionLog);
511                 } catch (ArgumentNullException){
512                         Error("Cannot load assembly (null argument)");
513                 }
514                 return null;
515         }
516
517         static void Error (string msg)
518         {
519                 Console.Error.WriteLine (msg);
520                 Environment.Exit (1);
521         }
522
523         static void Help ()
524         {
525                 Console.WriteLine ("Usage is: mkbundle [options] assembly1 [assembly2...]\n\n" +
526                                    "Options:\n" +
527                                    "    -c                  Produce stub only, do not compile\n" +
528                                    "    -o out              Specifies output filename\n" +
529                                    "    -oo obj             Specifies output filename for helper object file\n" +
530                                    "    -L path             Adds `path' to the search path for assemblies\n" +
531                                    "    --nodeps            Turns off automatic dependency embedding (default)\n" +
532                                    "    --deps              Turns on automatic dependency embedding\n" +
533                                    "    --keeptemp          Keeps the temporary files\n" +
534                                    "    --config F          Bundle system config file `F'\n" +
535                                    "    --config-dir D      Set MONO_CFG_DIR to `D'\n" +
536                                    "    --machine-config F  Use the given file as the machine.config for the application.\n" +
537                                    "    --static            Statically link to mono libs\n" +
538                                    "    --nomain            Don't include a main() function, for libraries\n" +
539                                    "    -z                  Compress the assemblies before embedding.\n" +
540                                    "                        You need zlib development headers and libraries.\n");
541         }
542
543         [DllImport ("libc")]
544         static extern int system (string s);
545         [DllImport ("libc")]
546         static extern int uname (IntPtr buf);
547                 
548         static void DetectOS ()
549         {
550                 if (!IsUnix) {
551                         Console.WriteLine ("OS is: Windows");
552                         style = "windows";
553                         return;
554                 }
555
556                 IntPtr buf = UnixMarshal.AllocHeap(8192);
557                 if (uname (buf) != 0){
558                         Console.WriteLine ("Warning: Unable to detect OS");
559                         UnixMarshal.FreeHeap(buf);
560                         return;
561                 }
562                 string os = Marshal.PtrToStringAnsi (buf);
563                 Console.WriteLine ("OS is: " + os);
564                 if (os == "Darwin")
565                         style = "osx";
566                 
567                 UnixMarshal.FreeHeap(buf);
568         }
569
570         static bool IsUnix {
571                 get {
572                         int p = (int) Environment.OSVersion.Platform;
573                         return ((p == 4) || (p == 128) || (p == 6));
574                 }
575         }
576
577         static int Execute (string cmdLine)
578         {
579                 if (IsUnix) {
580                         Console.WriteLine (cmdLine);
581                         return system (cmdLine);
582                 }
583
584                 // on Windows, we have to pipe the output of a
585                 // `cmd` interpolation to dos2unix, because the shell does not
586                 // strip the CRLFs generated by the native pkg-config distributed
587                 // with Mono.
588                 StringBuilder b = new StringBuilder ();
589                 int count = 0;
590                 for (int i = 0; i < cmdLine.Length; i++) {
591                         if (cmdLine [i] == '`') {
592                                 if (count % 2 != 0) {
593                                         b.Append ("|dos2unix");
594                                 }
595                                 count++;
596                         }
597                         b.Append (cmdLine [i]);
598                 }
599                 cmdLine = b.ToString ();
600                 Console.WriteLine (cmdLine);
601                         
602                 ProcessStartInfo psi = new ProcessStartInfo ();
603                 psi.UseShellExecute = false;
604                 psi.FileName = "sh";
605                 psi.Arguments = String.Format ("-c \"{0}\"", cmdLine);
606
607                 using (Process p = Process.Start (psi)) {
608                         p.WaitForExit ();
609                         return p.ExitCode;
610                 }
611         }
612
613         static string GetEnv (string name, string defaultValue) 
614         {
615                 string s = Environment.GetEnvironmentVariable (name);
616                 return s != null ? s : defaultValue;
617         }
618 }