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