2010-03-12 Jb Evain <jbevain@novell.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 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/assembly.h>\n");
210
211                         if (compress) {
212                                 tc.WriteLine ("typedef struct _compressed_data {");
213                                 tc.WriteLine ("\tMonoBundledAssembly assembly;");
214                                 tc.WriteLine ("\tint compressed_size;");
215                                 tc.WriteLine ("} CompressedAssembly;\n");
216                         }
217
218                         foreach (string url in files){
219                                 string fname = new Uri (url).LocalPath;
220                                 string aname = Path.GetFileName (fname);
221                                 string encoded = aname.Replace ("-", "_").Replace (".", "_");
222
223                                 if (prog == null)
224                                         prog = aname;
225                                 
226                                 Console.WriteLine ("   embedding: " + fname);
227                                 
228                                 Stream stream = File.OpenRead (fname);
229
230                                 long real_size = stream.Length;
231                                 int n;
232                                 if (compress) {
233                                         MemoryStream ms = new MemoryStream ();
234                                         DeflaterOutputStream deflate = new DeflaterOutputStream (ms);
235                                         while ((n = stream.Read (buffer, 0, 8192)) != 0){
236                                                 deflate.Write (buffer, 0, n);
237                                         }
238                                         stream.Close ();
239                                         deflate.Finish ();
240                                         byte [] bytes = ms.GetBuffer ();
241                                         stream = new MemoryStream (bytes, 0, (int) ms.Length, false, false);
242                                 }
243
244                                 WriteSymbol (ts, "assembly_data_" + encoded, stream.Length);
245
246                                 while ((n = stream.Read (buffer, 0, 8192)) != 0){
247                                         for (int i = 0; i < n; i++){
248                                                 ts.Write ("\t.byte {0}\n", buffer [i]);
249                                         }
250                                 }
251                                 ts.WriteLine ();
252
253                                 if (compress) {
254                                         tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
255                                         tc.WriteLine ("static CompressedAssembly assembly_bundle_{0} = {{{{\"{1}\"," +
256                                                         " assembly_data_{0}, {2}}}, {3}}};",
257                                                       encoded, aname, real_size, stream.Length);
258                                         double ratio = ((double) stream.Length * 100) / real_size;
259                                         Console.WriteLine ("   compression ratio: {0:.00}%", ratio);
260                                 } else {
261                                         tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
262                                         tc.WriteLine ("static const MonoBundledAssembly assembly_bundle_{0} = {{\"{1}\", assembly_data_{0}, {2}}};",
263                                                       encoded, aname, real_size);
264                                 }
265                                 stream.Close ();
266
267                                 c_bundle_names.Add ("assembly_bundle_" + encoded);
268
269                                 try {
270                                         FileStream cf = File.OpenRead (fname + ".config");
271                                         Console.WriteLine (" config from: " + fname + ".config");
272                                         tc.WriteLine ("extern const unsigned char assembly_config_{0} [];", encoded);
273                                         WriteSymbol (ts, "assembly_config_" + encoded, cf.Length);
274                                         while ((n = cf.Read (buffer, 0, 8192)) != 0){
275                                                 for (int i = 0; i < n; i++){
276                                                         ts.Write ("\t.byte {0}\n", buffer [i]);
277                                                 }
278                                         }
279                                         ts.WriteLine ();
280                                         config_names.Add (new string[] {aname, encoded});
281                                 } catch (FileNotFoundException) {
282                                         /* we ignore if the config file doesn't exist */
283                                 }
284
285                         }
286                         if (config_file != null){
287                                 FileStream conf;
288                                 try {
289                                         conf = File.OpenRead (config_file);
290                                 } catch {
291                                         Error (String.Format ("Failure to open {0}", config_file));
292                                         return;
293                                 }
294                                 Console.WriteLine ("System config from: " + config_file);
295                                 tc.WriteLine ("extern const unsigned char system_config;");
296                                 WriteSymbol (ts, "system_config", config_file.Length);
297
298                                 int n;
299                                 while ((n = conf.Read (buffer, 0, 8192)) != 0){
300                                         for (int i = 0; i < n; i++){
301                                                 ts.Write ("\t.byte {0}\n", buffer [i]);
302                                         }
303                                 }
304                                 // null terminator
305                                 ts.Write ("\t.byte 0\n");
306                                 ts.WriteLine ();
307                         }
308
309                         if (machine_config_file != null){
310                                 FileStream conf;
311                                 try {
312                                         conf = File.OpenRead (machine_config_file);
313                                 } catch {
314                                         Error (String.Format ("Failure to open {0}", machine_config_file));
315                                         return;
316                                 }
317                                 Console.WriteLine ("Machine config from: " + machine_config_file);
318                                 tc.WriteLine ("extern const unsigned char machine_config;");
319                                 WriteSymbol (ts, "machine_config", machine_config_file.Length);
320
321                                 int n;
322                                 while ((n = conf.Read (buffer, 0, 8192)) != 0){
323                                         for (int i = 0; i < n; i++){
324                                                 ts.Write ("\t.byte {0}\n", buffer [i]);
325                                         }
326                                 }
327                                 // null terminator
328                                 ts.Write ("\t.byte 0\n");
329                                 ts.WriteLine ();
330                         }
331                         ts.Close ();
332                         
333                         Console.WriteLine ("Compiling:");
334                         string cmd = String.Format ("{0} -o {1} {2} ", GetEnv ("AS", "as"), temp_o, temp_s);
335                         int ret = Execute (cmd);
336                         if (ret != 0){
337                                 Error ("[Fail]");
338                                 return;
339                         }
340
341                         if (compress)
342                                 tc.WriteLine ("\nstatic const CompressedAssembly *compressed [] = {");
343                         else
344                                 tc.WriteLine ("\nstatic const MonoBundledAssembly *bundled [] = {");
345
346                         foreach (string c in c_bundle_names){
347                                 tc.WriteLine ("\t&{0},", c);
348                         }
349                         tc.WriteLine ("\tNULL\n};\n");
350                         tc.WriteLine ("static char *image_name = \"{0}\";", prog);
351
352                         tc.WriteLine ("\nstatic void install_dll_config_files (void) {\n");
353                         foreach (string[] ass in config_names){
354                                 tc.WriteLine ("\tmono_register_config_for_assembly (\"{0}\", assembly_config_{1});\n", ass [0], ass [1]);
355                         }
356                         if (config_file != null)
357                                 tc.WriteLine ("\tmono_config_parse_memory (&system_config);\n");
358                         if (machine_config_file != null)
359                                 tc.WriteLine ("\tmono_register_machine_config (&machine_config);\n");
360                         tc.WriteLine ("}\n");
361
362                         if (config_dir != null)
363                                 tc.WriteLine ("static const char *config_dir = \"{0}\";", config_dir);
364                         else
365                                 tc.WriteLine ("static const char *config_dir = NULL;");
366
367                         Stream template_stream;
368                         if (compress) {
369                                 template_stream = Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_z.c");
370                         } else {
371                                 template_stream = Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template.c");
372                         }
373
374                         StreamReader s = new StreamReader (template_stream);
375                         string template = s.ReadToEnd ();
376                         tc.Write (template);
377
378                         if (!nomain) {
379                                 Stream template_main_stream = Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_main.c");
380                                 StreamReader st = new StreamReader (template_main_stream);
381                                 string maintemplate = st.ReadToEnd ();
382                                 tc.Write (maintemplate);
383                         }
384                         
385                         tc.Close ();
386
387                         if (compile_only)
388                                 return;
389
390                         string zlib = (compress ? "-lz" : "");
391                         string debugging = "-g";
392                         string cc = GetEnv ("CC", IsUnix ? "cc" : "gcc -mno-cygwin");
393
394                         if (style == "linux")
395                                 debugging = "-ggdb";
396                         if (static_link) {
397                                 string smonolib;
398                                 if (style == "osx")
399                                         smonolib = "`pkg-config --variable=libdir mono`/libmono.a ";
400                                 else
401                                         smonolib = "-Wl,-Bstatic -lmono -Wl,-Bdynamic ";
402                                 cmd = String.Format ("{4} -o {2} -Wall `pkg-config --cflags mono` {0} {3} " +
403                                                      "`pkg-config --libs-only-L mono` " + smonolib +
404                                                      "`pkg-config --libs-only-l mono | sed -e \"s/\\-lmono //\"` {1}",
405                                                      temp_c, temp_o, output, zlib, cc);
406                         } else {
407                                 
408                                 cmd = String.Format ("{4} " + debugging + " -o {2} -Wall {0} `pkg-config --cflags --libs mono` {3} {1}",
409                                                      temp_c, temp_o, output, zlib, cc);
410                         }
411                             
412                         ret = Execute (cmd);
413                         if (ret != 0){
414                                 Error ("[Fail]");
415                                 return;
416                         }
417                         Console.WriteLine ("Done");
418                         }
419                         }
420                 } finally {
421                         if (!keeptemp){
422                                 if (object_out == null){
423                                         File.Delete (temp_o);
424                                 }
425                                 if (!compile_only){
426                                         File.Delete (temp_c);
427                                 }
428                                 File.Delete (temp_s);
429                         }
430                 }
431         }
432         
433         static ArrayList LoadAssemblies (ArrayList sources)
434         {
435                 ArrayList assemblies = new ArrayList ();
436                 bool error = false;
437                 
438                 foreach (string name in sources){
439                         Assembly a = LoadAssembly (name);
440
441                         if (a == null){
442                                 error = true;
443                                 continue;
444                         }
445                         
446                         assemblies.Add (a);
447                 }
448
449                 if (error)
450                         Environment.Exit (1);
451
452                 return assemblies;
453         }
454         
455         static void QueueAssembly (ArrayList files, string codebase)
456         {
457                 if (files.Contains (codebase))
458                         return;
459
460                 files.Add (codebase);
461                 Assembly a = Assembly.LoadFrom (new Uri(codebase).LocalPath);
462
463                 if (!autodeps)
464                         return;
465                 
466                 foreach (AssemblyName an in a.GetReferencedAssemblies ()) {
467                         a = Assembly.Load (an);
468                         QueueAssembly (files, a.CodeBase);
469                 }
470         }
471
472         static Assembly LoadAssembly (string assembly)
473         {
474                 Assembly a;
475                 
476                 try {
477                         char[] path_chars = { '/', '\\' };
478                         
479                         if (assembly.IndexOfAny (path_chars) != -1) {
480                                 a = Assembly.LoadFrom (assembly);
481                         } else {
482                                 string ass = assembly;
483                                 if (ass.EndsWith (".dll"))
484                                         ass = assembly.Substring (0, assembly.Length - 4);
485                                 a = Assembly.Load (ass);
486                         }
487                         return a;
488                 } catch (FileNotFoundException){
489                         string total_log = "";
490                         
491                         foreach (string dir in link_paths){
492                                 string full_path = Path.Combine (dir, assembly);
493                                 if (!assembly.EndsWith (".dll") && !assembly.EndsWith (".exe"))
494                                         full_path += ".dll";
495                                 
496                                 try {
497                                         a = Assembly.LoadFrom (full_path);
498                                         return a;
499                                 } catch (FileNotFoundException ff) {
500                                         total_log += ff.FusionLog;
501                                         continue;
502                                 }
503                         }
504                         Error ("Cannot find assembly `" + assembly + "'" );
505                         Console.WriteLine ("Log: \n" + total_log);
506                 } catch (BadImageFormatException f) {
507                         Error ("Cannot load assembly (bad file format)" + f.FusionLog);
508                 } catch (FileLoadException f){
509                         Error ("Cannot load assembly " + f.FusionLog);
510                 } catch (ArgumentNullException){
511                         Error("Cannot load assembly (null argument)");
512                 }
513                 return null;
514         }
515
516         static void Error (string msg)
517         {
518                 Console.Error.WriteLine (msg);
519                 Environment.Exit (1);
520         }
521
522         static void Help ()
523         {
524                 Console.WriteLine ("Usage is: mkbundle [options] assembly1 [assembly2...]\n\n" +
525                                    "Options:\n" +
526                                    "    -c              Produce stub only, do not compile\n" +
527                                    "    -o out          Specifies output filename\n" +
528                                    "    -oo obj         Specifies output filename for helper object file\n" +
529                                    "    -L path         Adds `path' to the search path for assemblies\n" +
530                                    "    --nodeps        Turns off automatic dependency embedding (default)\n" +
531                                    "    --deps          Turns on automatic dependency embedding\n" +
532                                    "    --keeptemp      Keeps the temporary files\n" +
533                                    "    --config F      Bundle system config file `F'\n" +
534                                    "    --config-dir D  Set MONO_CFG_DIR to `D'\n" +
535                                    "    --static        Statically link to mono libs\n" +
536                                    "    --nomain        Don't include a main() function, for libraries\n" +
537                                    "    -z              Compress the assemblies before embedding.\n");
538         }
539
540         [DllImport ("libc")]
541         static extern int system (string s);
542         [DllImport ("libc")]
543         static extern int uname (IntPtr buf);
544                 
545         static void DetectOS ()
546         {
547                 if (!IsUnix) {
548                         Console.WriteLine ("OS is: Windows");
549                         style = "windows";
550                         return;
551                 }
552
553                 IntPtr buf = UnixMarshal.AllocHeap(8192);
554                 if (uname (buf) != 0){
555                         Console.WriteLine ("Warning: Unable to detect OS");
556                         return;
557                 }
558                 string os = Marshal.PtrToStringAnsi (buf);
559                 Console.WriteLine ("OS is: " + os);
560                 if (os == "Darwin")
561                         style = "osx";
562                 
563                 UnixMarshal.FreeHeap(buf);
564         }
565
566         static bool IsUnix {
567                 get {
568                         int p = (int) Environment.OSVersion.Platform;
569                         return ((p == 4) || (p == 128) || (p == 6));
570                 }
571         }
572
573         static int Execute (string cmdLine)
574         {
575                 if (IsUnix) {
576                         Console.WriteLine (cmdLine);
577                         return system (cmdLine);
578                 }
579
580                 // on Windows, we have to pipe the output of a
581                 // `cmd` interpolation to dos2unix, because the shell does not
582                 // strip the CRLFs generated by the native pkg-config distributed
583                 // with Mono.
584                 StringBuilder b = new StringBuilder ();
585                 int count = 0;
586                 for (int i = 0; i < cmdLine.Length; i++) {
587                         if (cmdLine [i] == '`') {
588                                 if (count % 2 != 0) {
589                                         b.Append ("|dos2unix");
590                                 }
591                                 count++;
592                         }
593                         b.Append (cmdLine [i]);
594                 }
595                 cmdLine = b.ToString ();
596                 Console.WriteLine (cmdLine);
597                         
598                 ProcessStartInfo psi = new ProcessStartInfo ();
599                 psi.UseShellExecute = false;
600                 psi.FileName = "sh";
601                 psi.Arguments = String.Format ("-c \"{0}\"", cmdLine);
602
603                 using (Process p = Process.Start (psi)) {
604                         p.WaitForExit ();
605                         return p.ExitCode;
606                 }
607         }
608
609         static string GetEnv (string name, string defaultValue) 
610         {
611                 string s = Environment.GetEnvironmentVariable (name);
612                 return s != null ? s : defaultValue;
613         }
614 }