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