2 // mkbundle: tool to create bundles.
4 // Based on the `make-bundle' Perl script written by Paolo Molaro (lupus@debian.org)
9 // (C) Novell, Inc 2004
12 using System.Diagnostics;
14 using System.Collections.Generic;
16 using System.IO.Compression;
17 using System.Runtime.InteropServices;
19 using IKVM.Reflection;
22 using System.Threading.Tasks;
25 static string output = "a.out";
26 static string object_out = null;
27 static List<string> link_paths = new List<string> ();
28 static bool autodeps = false;
29 static bool keeptemp = false;
30 static bool compile_only = false;
31 static bool static_link = false;
32 static string config_file = null;
33 static string machine_config_file = null;
34 static string config_dir = null;
35 static string style = "linux";
36 static string os_message = "";
39 static string custom_main = null;
40 static bool? use_dos2unix = null;
41 static bool skip_scan;
42 static string ctor_func;
45 static int Main (string [] args)
47 List<string> sources = new List<string> ();
48 int top = args.Length;
53 for (int i = 0; i < top; i++){
55 case "--help": case "-h": case "-?":
76 object_out = args [++i];
84 link_paths.Add (args [++i]);
101 Console.WriteLine ("Note that statically linking the LGPL Mono runtime has more licensing restrictions than dynamically linking.");
102 Console.WriteLine ("See http://www.mono-project.com/Licensing for details on licensing.");
111 config_file = args [++i];
113 case "--machine-config":
119 machine_config_file = args [++i];
122 Console.WriteLine ("WARNING:\n Check that the machine.config file you are bundling\n doesn't contain sensitive information specific to this machine.");
130 config_dir = args [++i];
138 case "--custom-main":
143 custom_main = args [++i];
157 Console.Error.WriteLine ("Invalid style '{0}' - only 'windows', 'mac' and 'linux' are supported for --style argument", style);
165 case "--static-ctor":
170 ctor_func = args [++i];
173 case "--dos2unix=true":
176 case "--dos2unix=false":
177 use_dos2unix = false;
184 sources.Add (args [i]);
191 Console.WriteLine (os_message);
192 Console.WriteLine ("Sources: {0} Auto-dependencies: {1}", sources.Count, autodeps);
194 if (sources.Count == 0 || output == null) {
196 Environment.Exit (1);
199 List<string> assemblies = LoadAssemblies (sources);
200 List<string> files = new List<string> ();
201 foreach (string file in assemblies)
202 if (!QueueAssembly (files, file))
205 GenerateBundles (files);
206 //GenerateJitWrapper ();
211 static void WriteSymbol (StreamWriter sw, string name, long size)
217 "\t.section .rodata\n" +
219 "\t.type {0}, \"object\"\n" +
220 "\t.size {0}, {1}\n" +
226 "\t.section __TEXT,__text,regular,pure_instructions\n" +
236 "\t.section .rdata,\"dr\"\n" +
244 static string [] chars = new string [256];
246 static void WriteBuffer (StreamWriter ts, Stream stream, byte[] buffer)
250 // Preallocate the strings we need.
251 if (chars [0] == null) {
252 for (int i = 0; i < chars.Length; i++)
253 chars [i] = string.Format ("{0}", i.ToString ());
256 while ((n = stream.Read (buffer, 0, buffer.Length)) != 0) {
258 for (int i = 0; i < n; i++) {
259 if (count % 32 == 0) {
260 ts.Write ("\n\t.byte ");
264 ts.Write (chars [buffer [i]]);
272 static void GenerateBundles (List<string> files)
274 string temp_s = "temp.s"; // Path.GetTempFileName ();
275 string temp_c = "temp.c";
276 string temp_o = "temp.o";
280 if (object_out != null)
284 List<string> c_bundle_names = new List<string> ();
285 List<string[]> config_names = new List<string[]> ();
287 using (StreamWriter ts = new StreamWriter (File.Create (temp_s))) {
288 using (StreamWriter tc = new StreamWriter (File.Create (temp_c))) {
292 tc.WriteLine ("/* This source code was produced by mkbundle, do not edit */");
293 tc.WriteLine ("\n#ifndef NULL\n#define NULL (void *)0\n#endif");
297 const unsigned char *data;
298 const unsigned int size;
299 } MonoBundledAssembly;
300 void mono_register_bundled_assemblies (const MonoBundledAssembly **assemblies);
301 void mono_register_config_for_assembly (const char* assembly_name, const char* config_xml);
304 tc.WriteLine ("#include <mono/metadata/mono-config.h>");
305 tc.WriteLine ("#include <mono/metadata/assembly.h>\n");
309 tc.WriteLine ("typedef struct _compressed_data {");
310 tc.WriteLine ("\tMonoBundledAssembly assembly;");
311 tc.WriteLine ("\tint compressed_size;");
312 tc.WriteLine ("} CompressedAssembly;\n");
315 object monitor = new object ();
317 var streams = new Dictionary<string, Stream> ();
318 var sizes = new Dictionary<string, long> ();
320 // Do the file reading and compression in parallel
321 Action<string> body = delegate (string url) {
322 string fname = LocateFile (new Uri (url).LocalPath);
323 Stream stream = File.OpenRead (fname);
325 long real_size = stream.Length;
328 byte[] cbuffer = new byte [8192];
329 MemoryStream ms = new MemoryStream ();
330 GZipStream deflate = new GZipStream (ms, CompressionMode.Compress, leaveOpen:true);
331 while ((n = stream.Read (cbuffer, 0, cbuffer.Length)) != 0){
332 deflate.Write (cbuffer, 0, n);
336 byte [] bytes = ms.GetBuffer ();
337 stream = new MemoryStream (bytes, 0, (int) ms.Length, false, false);
341 streams [url] = stream;
342 sizes [url] = real_size;
348 Parallel.ForEach (files, body);
350 foreach (var url in files)
354 // The non-parallel part
355 byte [] buffer = new byte [8192];
356 // everything other than a-zA-Z0-9_ needs to be escaped in asm symbols.
357 var symbolEscapeRE = new System.Text.RegularExpressions.Regex ("[^\\w_]");
358 foreach (var url in files) {
359 string fname = LocateFile (new Uri (url).LocalPath);
360 string aname = Path.GetFileName (fname);
361 string encoded = symbolEscapeRE.Replace (aname, "_");
366 var stream = streams [url];
367 var real_size = sizes [url];
370 Console.WriteLine (" embedding: " + fname);
372 WriteSymbol (ts, "assembly_data_" + encoded, stream.Length);
374 WriteBuffer (ts, stream, buffer);
377 tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
378 tc.WriteLine ("static CompressedAssembly assembly_bundle_{0} = {{{{\"{1}\"," +
379 " assembly_data_{0}, {2}}}, {3}}};",
380 encoded, aname, real_size, stream.Length);
382 double ratio = ((double) stream.Length * 100) / real_size;
383 Console.WriteLine (" compression ratio: {0:.00}%", ratio);
386 tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
387 tc.WriteLine ("static const MonoBundledAssembly assembly_bundle_{0} = {{\"{1}\", assembly_data_{0}, {2}}};",
388 encoded, aname, real_size);
392 c_bundle_names.Add ("assembly_bundle_" + encoded);
395 FileStream cf = File.OpenRead (fname + ".config");
397 Console.WriteLine (" config from: " + fname + ".config");
398 tc.WriteLine ("extern const unsigned char assembly_config_{0} [];", encoded);
399 WriteSymbol (ts, "assembly_config_" + encoded, cf.Length);
400 WriteBuffer (ts, cf, buffer);
402 config_names.Add (new string[] {aname, encoded});
403 } catch (FileNotFoundException) {
404 /* we ignore if the config file doesn't exist */
408 if (config_file != null){
411 conf = File.OpenRead (config_file);
413 Error (String.Format ("Failure to open {0}", config_file));
417 Console.WriteLine ("System config from: " + config_file);
418 tc.WriteLine ("extern const char system_config;");
419 WriteSymbol (ts, "system_config", config_file.Length);
421 WriteBuffer (ts, conf, buffer);
423 ts.Write ("\t.byte 0\n");
427 if (machine_config_file != null){
430 conf = File.OpenRead (machine_config_file);
432 Error (String.Format ("Failure to open {0}", machine_config_file));
436 Console.WriteLine ("Machine config from: " + machine_config_file);
437 tc.WriteLine ("extern const char machine_config;");
438 WriteSymbol (ts, "machine_config", machine_config_file.Length);
440 WriteBuffer (ts, conf, buffer);
441 ts.Write ("\t.byte 0\n");
447 tc.WriteLine ("\nstatic const CompressedAssembly *compressed [] = {");
449 tc.WriteLine ("\nstatic const MonoBundledAssembly *bundled [] = {");
451 foreach (string c in c_bundle_names){
452 tc.WriteLine ("\t&{0},", c);
454 tc.WriteLine ("\tNULL\n};\n");
455 tc.WriteLine ("static char *image_name = \"{0}\";", prog);
457 if (ctor_func != null) {
458 tc.WriteLine ("\nextern void {0} (void);", ctor_func);
459 tc.WriteLine ("\n__attribute__ ((constructor)) static void mono_mkbundle_ctor (void)");
460 tc.WriteLine ("{{\n\t{0} ();\n}}", ctor_func);
463 tc.WriteLine ("\nstatic void install_dll_config_files (void) {\n");
464 foreach (string[] ass in config_names){
465 tc.WriteLine ("\tmono_register_config_for_assembly (\"{0}\", assembly_config_{1});\n", ass [0], ass [1]);
467 if (config_file != null)
468 tc.WriteLine ("\tmono_config_parse_memory (&system_config);\n");
469 if (machine_config_file != null)
470 tc.WriteLine ("\tmono_register_machine_config (&machine_config);\n");
471 tc.WriteLine ("}\n");
473 if (config_dir != null)
474 tc.WriteLine ("static const char *config_dir = \"{0}\";", config_dir);
476 tc.WriteLine ("static const char *config_dir = NULL;");
478 Stream template_stream;
480 template_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_z.c");
482 template_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template.c");
485 StreamReader s = new StreamReader (template_stream);
486 string template = s.ReadToEnd ();
489 if (!nomain && custom_main == null) {
490 Stream template_main_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_main.c");
491 StreamReader st = new StreamReader (template_main_stream);
492 string maintemplate = st.ReadToEnd ();
493 tc.Write (maintemplate);
498 string assembler = GetEnv("AS", "as");
499 string as_cmd = String.Format("{0} -o {1} {2} ", assembler, temp_o, temp_s);
506 Console.WriteLine("Compiling:");
508 if (style == "windows")
511 Func<string, string> quote = (pp) => { return "\"" + pp + "\""; };
513 string compiler = GetEnv("CC", "cl.exe");
514 string winsdkPath = GetEnv("WINSDK", @"C:\Program Files (x86)\Windows Kits\8.1");
515 string vsPath = GetEnv("VSINCLUDE", @"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC");
516 string monoPath = GetEnv("MONOPREFIX", @"C:\Program Files (x86)\Mono");
518 string[] includes = new string[] {winsdkPath + @"\Include\um", winsdkPath + @"\Include\shared", vsPath + @"\include", monoPath + @"\include\mono-2.0", "." };
519 string[] libs = new string[] { winsdkPath + @"\Lib\winv6.3\um\x86" , vsPath + @"\lib" };
520 var linkLibraries = new string[] { "kernel32.lib",
535 string glue_obj = "mkbundle_glue.obj";
539 monoLib = LocateFile (monoPath + @"\lib\monosgen-2.0-static.lib");
542 Console.WriteLine ("WARNING: Dynamically linking the Mono runtime on Windows is not a tested option.");
543 monoLib = LocateFile (monoPath + @"\lib\monosgen-2.0.lib");
544 LocateFile (monoPath + @"\lib\monosgen-2.0.dll"); // in this case, the .lib is just the import library, and the .dll is also needed
547 var compilerArgs = new List<string>();
548 compilerArgs.Add("/MT");
550 foreach (string include in includes)
551 compilerArgs.Add(String.Format ("/I {0}", quote (include)));
553 if (!nomain || custom_main != null) {
554 compilerArgs.Add(quote(temp_c));
555 compilerArgs.Add(quote(temp_o));
556 if (custom_main != null)
557 compilerArgs.Add(quote(custom_main));
558 compilerArgs.Add(quote(monoLib));
559 compilerArgs.Add("/link");
560 compilerArgs.Add("/NODEFAULTLIB");
561 compilerArgs.Add("/SUBSYSTEM:windows");
562 compilerArgs.Add("/ENTRY:mainCRTStartup");
563 compilerArgs.AddRange(linkLibraries);
564 compilerArgs.Add("/out:"+ output);
566 string cl_cmd = String.Format("{0} {1}", compiler, String.Join(" ", compilerArgs.ToArray()));
571 // we are just creating a .lib
572 compilerArgs.Add("/c"); // compile only
573 compilerArgs.Add(temp_c);
574 compilerArgs.Add(String.Format("/Fo" + glue_obj)); // .obj output name
576 string cl_cmd = String.Format("{0} {1}", compiler, String.Join(" ", compilerArgs.ToArray()));
579 string librarian = GetEnv ("LIB", "lib.exe");
580 var librarianArgs = new List<string> ();
581 librarianArgs.Add (String.Format ("/out:{0}.lib" + output));
582 librarianArgs.Add (temp_o);
583 librarianArgs.Add (glue_obj);
584 librarianArgs.Add (monoLib);
585 string lib_cmd = String.Format("{0} {1}", librarian, String.Join(" ", librarianArgs.ToArray()));
591 string zlib = (compress ? "-lz" : "");
592 string debugging = "-g";
593 string cc = GetEnv("CC", "cc");
596 if (style == "linux")
602 smonolib = "`pkg-config --variable=libdir mono-2`/libmono-2.0.a ";
604 smonolib = "-Wl,-Bstatic -lmono-2.0 -Wl,-Bdynamic ";
605 cmd = String.Format("{4} -o '{2}' -Wall `pkg-config --cflags mono-2` {0} {3} " +
606 "`pkg-config --libs-only-L mono-2` " + smonolib +
607 "`pkg-config --libs-only-l mono-2 | sed -e \"s/\\-lmono-2.0 //\"` {1}",
608 temp_c, temp_o, output, zlib, cc);
613 cmd = String.Format("{4} " + debugging + " -o '{2}' -Wall {0} `pkg-config --cflags --libs mono-2` {3} {1}",
614 temp_c, temp_o, output, zlib, cc);
620 Console.WriteLine ("Done");
625 if (object_out == null){
626 File.Delete (temp_o);
629 File.Delete (temp_c);
631 File.Delete (temp_s);
636 static List<string> LoadAssemblies (List<string> sources)
638 List<string> assemblies = new List<string> ();
641 foreach (string name in sources){
643 Assembly a = LoadAssembly (name);
650 assemblies.Add (a.CodeBase);
651 } catch (Exception) {
654 Console.WriteLine ("File will not be scanned: {0}", name);
655 assemblies.Add (new Uri (new FileInfo (name).FullName).ToString ());
663 Environment.Exit (1);
668 static readonly Universe universe = new Universe ();
669 static readonly Dictionary<string, string> loaded_assemblies = new Dictionary<string, string> ();
671 static bool QueueAssembly (List<string> files, string codebase)
673 // Console.WriteLine ("CODE BASE IS {0}", codebase);
674 if (files.Contains (codebase))
677 var path = new Uri(codebase).LocalPath;
678 var name = Path.GetFileName (path);
680 if (loaded_assemblies.TryGetValue (name, out found)) {
681 Error (string.Format ("Duplicate assembly name `{0}'. Both `{1}' and `{2}' use same assembly name.", name, path, found));
685 loaded_assemblies.Add (name, path);
687 files.Add (codebase);
691 Assembly a = universe.LoadFile (path);
693 foreach (AssemblyName an in a.GetReferencedAssemblies ()) {
694 a = universe.Load (an.FullName);
695 if (!QueueAssembly (files, a.CodeBase))
698 } catch (Exception) {
706 static Assembly LoadAssembly (string assembly)
711 char[] path_chars = { '/', '\\' };
713 if (assembly.IndexOfAny (path_chars) != -1) {
714 a = universe.LoadFile (assembly);
716 string ass = assembly;
717 if (ass.EndsWith (".dll"))
718 ass = assembly.Substring (0, assembly.Length - 4);
719 a = universe.Load (ass);
722 } catch (FileNotFoundException){
723 string total_log = "";
725 foreach (string dir in link_paths){
726 string full_path = Path.Combine (dir, assembly);
727 if (!assembly.EndsWith (".dll") && !assembly.EndsWith (".exe"))
731 a = universe.LoadFile (full_path);
733 } catch (FileNotFoundException ff) {
734 total_log += ff.FusionLog;
738 Error ("Cannot find assembly `" + assembly + "'" );
740 Console.WriteLine ("Log: \n" + total_log);
741 } catch (IKVM.Reflection.BadImageFormatException f) {
744 Error ("Cannot load assembly (bad file format) " + f.Message);
745 } catch (FileLoadException f){
746 Error ("Cannot load assembly " + f.Message);
747 } catch (ArgumentNullException){
748 Error("Cannot load assembly (null argument)");
753 static void Error (string msg)
755 Console.Error.WriteLine ("ERROR: " + msg);
756 Environment.Exit (1);
761 Console.WriteLine ("Usage is: mkbundle [options] assembly1 [assembly2...]\n\n" +
763 " -c Produce stub only, do not compile\n" +
764 " -o out Specifies output filename\n" +
765 " -oo obj Specifies output filename for helper object file\n" +
766 " -L path Adds `path' to the search path for assemblies\n" +
767 " --nodeps Turns off automatic dependency embedding (default)\n" +
768 " --deps Turns on automatic dependency embedding\n" +
769 " --dos2unix[=true|false]\n" +
770 " When no value provided, or when `true` specified\n" +
771 " `dos2unix` will be invoked to convert paths on Windows.\n" +
772 " When `--dos2unix=false` used, dos2unix is NEVER used.\n" +
773 " --keeptemp Keeps the temporary files\n" +
774 " --config F Bundle system config file `F'\n" +
775 " --config-dir D Set MONO_CFG_DIR to `D'\n" +
776 " --machine-config F Use the given file as the machine.config for the application.\n" +
777 " --static Statically link to mono libs\n" +
778 " --nomain Don't include a main() function, for libraries\n" +
779 " --custom-main C Link the specified compilation unit (.c or .obj) with entry point/init code\n" +
780 " -z Compress the assemblies before embedding.\n" +
781 " --skip-scan Skip scanning assemblies that could not be loaded (but still embed them).\n" +
782 " --static-ctor ctor Add a constructor call to the supplied function.\n" +
783 " You need zlib development headers and libraries.\n");
787 static extern int system (string s);
789 static extern int uname (IntPtr buf);
791 static void DetectOS ()
794 os_message = "OS is: Windows";
799 IntPtr buf = Marshal.AllocHGlobal (8192);
800 if (uname (buf) != 0){
801 os_message = "Warning: Unable to detect OS";
802 Marshal.FreeHGlobal (buf);
805 string os = Marshal.PtrToStringAnsi (buf);
806 os_message = "OS is: " + os;
810 Marshal.FreeHGlobal (buf);
815 int p = (int) Environment.OSVersion.Platform;
816 return ((p == 4) || (p == 128) || (p == 6));
820 static void Execute (string cmdLine)
824 Console.WriteLine ("[execute cmd]: " + cmdLine);
825 int ret = system (cmdLine);
828 Error(String.Format("[Fail] {0}", ret));
833 // on Windows, we have to pipe the output of a
834 // `cmd` interpolation to dos2unix, because the shell does not
835 // strip the CRLFs generated by the native pkg-config distributed
838 // But if it's *not* on cygwin, just skip it.
840 // check if dos2unix is applicable.
841 if (use_dos2unix == true)
843 var info = new ProcessStartInfo ("dos2unix");
844 info.CreateNoWindow = true;
845 info.RedirectStandardInput = true;
846 info.UseShellExecute = false;
847 var dos2unix = Process.Start (info);
848 dos2unix.StandardInput.WriteLine ("aaa");
849 dos2unix.StandardInput.WriteLine ("\u0004");
850 dos2unix.StandardInput.Close ();
851 dos2unix.WaitForExit ();
852 if (dos2unix.ExitCode == 0)
855 Console.WriteLine("Warning: dos2unix not found");
856 use_dos2unix = false;
859 if (use_dos2unix == null)
860 use_dos2unix = false;
862 ProcessStartInfo psi = new ProcessStartInfo();
863 psi.UseShellExecute = false;
865 // if there is no dos2unix, just run cmd /c.
866 if (use_dos2unix == false)
868 psi.FileName = "cmd";
869 psi.Arguments = String.Format("/c \"{0}\"", cmdLine);
874 StringBuilder b = new StringBuilder();
876 for (int i = 0; i < cmdLine.Length; i++)
878 if (cmdLine[i] == '`')
882 b.Append("|dos2unix");
886 b.Append(cmdLine[i]);
888 cmdLine = b.ToString();
889 psi.Arguments = String.Format("-c \"{0}\"", cmdLine);
893 Console.WriteLine(cmdLine);
894 using (Process p = Process.Start (psi)) {
896 int ret = p.ExitCode;
898 Error (String.Format("[Fail] {0}", ret));
903 static string GetEnv(string name, string defaultValue)
905 string val = Environment.GetEnvironmentVariable(name);
909 Console.WriteLine("{0} = {1}", name, val);
915 Console.WriteLine("{0} = {1} (default)", name, val);
920 static string LocateFile(string default_path)
922 var override_path = Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(default_path));
923 if (File.Exists(override_path))
924 return override_path;
925 else if (File.Exists(default_path))
928 throw new FileNotFoundException(default_path);