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