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