2010-04-23 Jb Evain <jbevain@novell.com>
[mcs.git] / tools / mkbundle / mkbundle.cs
blob36a05394c537241404ee8bb84441fc3341e22105
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
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;
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;
37 static int Main (string [] args)
39 ArrayList sources = new ArrayList ();
40 int top = args.Length;
41 link_paths.Add (".");
43 DetectOS ();
45 for (int i = 0; i < top; i++){
46 switch (args [i]){
47 case "--help": case "-h": case "-?":
48 Help ();
49 return 1;
51 case "-c":
52 compile_only = true;
53 break;
55 case "-o":
56 if (i+1 == top){
57 Help ();
58 return 1;
60 output = args [++i];
61 break;
63 case "-oo":
64 if (i+1 == top){
65 Help ();
66 return 1;
68 object_out = args [++i];
69 break;
71 case "-L":
72 if (i+1 == top){
73 Help ();
74 return 1;
76 link_paths.Add (args [++i]);
77 break;
79 case "--nodeps":
80 autodeps = false;
81 break;
83 case "--deps":
84 autodeps = true;
85 break;
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;
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;
105 config_file = args [++i];
106 break;
107 case "--machine-config":
108 if (i+1 == top) {
109 Help ();
110 return 1;
113 machine_config_file = args [++i];
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;
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;
137 Console.WriteLine ("Sources: {0} Auto-dependencies: {1}", sources.Count, autodeps);
138 if (sources.Count == 0 || output == null) {
139 Help ();
140 Environment.Exit (1);
143 ArrayList assemblies = LoadAssemblies (sources);
144 ArrayList files = new ArrayList ();
145 foreach (Assembly a in assemblies)
146 QueueAssembly (files, a.CodeBase);
148 GenerateBundles (files);
149 //GenerateJitWrapper ();
151 return 0;
154 static void WriteSymbol (StreamWriter sw, string name, long size)
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;
188 static void GenerateBundles (ArrayList files)
190 string temp_s = "temp.s"; // Path.GetTempFileName ();
191 string temp_c = "temp.c";
192 string temp_o = "temp.o";
194 if (compile_only)
195 temp_c = output;
196 if (object_out != null)
197 temp_o = object_out;
199 try {
200 ArrayList c_bundle_names = new ArrayList ();
201 ArrayList config_names = new ArrayList ();
202 byte [] buffer = new byte [8192];
204 using (StreamWriter ts = new StreamWriter (File.Create (temp_s))) {
205 using (StreamWriter tc = new StreamWriter (File.Create (temp_c))) {
206 string prog = null;
208 tc.WriteLine ("/* This source code was produced by mkbundle, do not edit */");
209 tc.WriteLine ("#include <mono/metadata/assembly.h>\n");
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");
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 (".", "_");
223 if (prog == null)
224 prog = aname;
226 Console.WriteLine (" embedding: " + fname);
228 Stream stream = File.OpenRead (fname);
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);
238 stream.Close ();
239 deflate.Finish ();
240 byte [] bytes = ms.GetBuffer ();
241 stream = new MemoryStream (bytes, 0, (int) ms.Length, false, false);
244 WriteSymbol (ts, "assembly_data_" + encoded, stream.Length);
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]);
251 ts.WriteLine ();
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);
265 stream.Close ();
267 c_bundle_names.Add ("assembly_bundle_" + encoded);
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]);
279 ts.WriteLine ();
280 config_names.Add (new string[] {aname, encoded});
281 } catch (FileNotFoundException) {
282 /* we ignore if the config file doesn't exist */
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;
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);
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]);
304 // null terminator
305 ts.Write ("\t.byte 0\n");
306 ts.WriteLine ();
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;
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);
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]);
327 // null terminator
328 ts.Write ("\t.byte 0\n");
329 ts.WriteLine ();
331 ts.Close ();
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;
341 if (compress)
342 tc.WriteLine ("\nstatic const CompressedAssembly *compressed [] = {");
343 else
344 tc.WriteLine ("\nstatic const MonoBundledAssembly *bundled [] = {");
346 foreach (string c in c_bundle_names){
347 tc.WriteLine ("\t&{0},", c);
349 tc.WriteLine ("\tNULL\n};\n");
350 tc.WriteLine ("static char *image_name = \"{0}\";", prog);
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]);
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");
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;");
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");
374 StreamReader s = new StreamReader (template_stream);
375 string template = s.ReadToEnd ();
376 tc.Write (template);
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);
385 tc.Close ();
387 if (compile_only)
388 return;
390 string zlib = (compress ? "-lz" : "");
391 string debugging = "-g";
392 string cc = GetEnv ("CC", IsUnix ? "cc" : "gcc -mno-cygwin");
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-2`/libmono-2.0.a ";
400 else
401 smonolib = "-Wl,-Bstatic -lmono-2.0 -Wl,-Bdynamic ";
402 cmd = String.Format ("{4} -o {2} -Wall `pkg-config --cflags mono-2` {0} {3} " +
403 "`pkg-config --libs-only-L mono-2` " + smonolib +
404 "`pkg-config --libs-only-l mono-2 | sed -e \"s/\\-lmono-2.0 //\"` {1}",
405 temp_c, temp_o, output, zlib, cc);
406 } else {
408 cmd = String.Format ("{4} " + debugging + " -o {2} -Wall {0} `pkg-config --cflags --libs mono-2` {3} {1}",
409 temp_c, temp_o, output, zlib, cc);
412 ret = Execute (cmd);
413 if (ret != 0){
414 Error ("[Fail]");
415 return;
417 Console.WriteLine ("Done");
420 } finally {
421 if (!keeptemp){
422 if (object_out == null){
423 File.Delete (temp_o);
425 if (!compile_only){
426 File.Delete (temp_c);
428 File.Delete (temp_s);
433 static ArrayList LoadAssemblies (ArrayList sources)
435 ArrayList assemblies = new ArrayList ();
436 bool error = false;
438 foreach (string name in sources){
439 Assembly a = LoadAssembly (name);
441 if (a == null){
442 error = true;
443 continue;
446 assemblies.Add (a);
449 if (error)
450 Environment.Exit (1);
452 return assemblies;
455 static void QueueAssembly (ArrayList files, string codebase)
457 if (files.Contains (codebase))
458 return;
460 files.Add (codebase);
461 Assembly a = Assembly.LoadFrom (new Uri(codebase).LocalPath);
463 if (!autodeps)
464 return;
466 foreach (AssemblyName an in a.GetReferencedAssemblies ()) {
467 a = Assembly.Load (an);
468 QueueAssembly (files, a.CodeBase);
472 static Assembly LoadAssembly (string assembly)
474 Assembly a;
476 try {
477 char[] path_chars = { '/', '\\' };
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);
487 return a;
488 } catch (FileNotFoundException){
489 string total_log = "";
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";
496 try {
497 a = Assembly.LoadFrom (full_path);
498 return a;
499 } catch (FileNotFoundException ff) {
500 total_log += ff.FusionLog;
501 continue;
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)");
513 return null;
516 static void Error (string msg)
518 Console.Error.WriteLine (msg);
519 Environment.Exit (1);
522 static void Help ()
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 " --machine-config F Use the given file as the machine.config for the application.\n" +
536 " --static Statically link to mono libs\n" +
537 " --nomain Don't include a main() function, for libraries\n" +
538 " -z Compress the assemblies before embedding.\n");
541 [DllImport ("libc")]
542 static extern int system (string s);
543 [DllImport ("libc")]
544 static extern int uname (IntPtr buf);
546 static void DetectOS ()
548 if (!IsUnix) {
549 Console.WriteLine ("OS is: Windows");
550 style = "windows";
551 return;
554 IntPtr buf = UnixMarshal.AllocHeap(8192);
555 if (uname (buf) != 0){
556 Console.WriteLine ("Warning: Unable to detect OS");
557 return;
559 string os = Marshal.PtrToStringAnsi (buf);
560 Console.WriteLine ("OS is: " + os);
561 if (os == "Darwin")
562 style = "osx";
564 UnixMarshal.FreeHeap(buf);
567 static bool IsUnix {
568 get {
569 int p = (int) Environment.OSVersion.Platform;
570 return ((p == 4) || (p == 128) || (p == 6));
574 static int Execute (string cmdLine)
576 if (IsUnix) {
577 Console.WriteLine (cmdLine);
578 return system (cmdLine);
581 // on Windows, we have to pipe the output of a
582 // `cmd` interpolation to dos2unix, because the shell does not
583 // strip the CRLFs generated by the native pkg-config distributed
584 // with Mono.
585 StringBuilder b = new StringBuilder ();
586 int count = 0;
587 for (int i = 0; i < cmdLine.Length; i++) {
588 if (cmdLine [i] == '`') {
589 if (count % 2 != 0) {
590 b.Append ("|dos2unix");
592 count++;
594 b.Append (cmdLine [i]);
596 cmdLine = b.ToString ();
597 Console.WriteLine (cmdLine);
599 ProcessStartInfo psi = new ProcessStartInfo ();
600 psi.UseShellExecute = false;
601 psi.FileName = "sh";
602 psi.Arguments = String.Format ("-c \"{0}\"", cmdLine);
604 using (Process p = Process.Start (psi)) {
605 p.WaitForExit ();
606 return p.ExitCode;
610 static string GetEnv (string name, string defaultValue)
612 string s = Environment.GetEnvironmentVariable (name);
613 return s != null ? s : defaultValue;