2 // mkbundle: tool to create bundles.
4 // Based on the `make-bundle' Perl script written by Paolo Molaro (lupus@debian.org)
7 // [x] Rename the paths for the zip file that is downloaded
8 // [x] Update documentation with new flag
9 // [x] Load internationalized assemblies
10 // [x] Dependencies - if System.dll -> include Mono.Security.* (not needed, automatic)
11 // [x] --list-targets should download from a different url
12 // [x] --fetch-target should unpack zip file
13 // [x] Update --cross to use not a runtime, but an SDK
14 // [x] Update --local-targets to show the downloaded SDKs
19 // (C) Novell, Inc 2004
20 // (C) 2016 Xamarin Inc
23 // * Add support for packaging native libraries, extracting at runtime and setting the library path.
24 // * Implement --list-targets lists all the available remote targets
27 using System.Diagnostics;
29 using System.Collections.Generic;
31 using System.IO.Compression;
32 using System.Runtime.InteropServices;
34 using IKVM.Reflection;
37 using System.Threading.Tasks;
40 static string output = "a.out";
41 static string object_out = null;
42 static List<string> link_paths = new List<string> ();
43 static Dictionary<string,string> libraries = new Dictionary<string,string> ();
44 static bool autodeps = false;
45 static bool keeptemp = false;
46 static bool compile_only = false;
47 static bool static_link = false;
48 static string config_file = null;
49 static string machine_config_file = null;
50 static string config_dir = null;
51 static string style = "linux";
52 static bool bundled_header = false;
53 static string os_message = "";
56 static string custom_main = null;
57 static bool? use_dos2unix = null;
58 static bool skip_scan;
59 static string ctor_func;
60 static bool quiet = true;
61 static string cross_target = null;
62 static string fetch_target = null;
63 static bool custom_mode = true;
64 static string embedded_options = null;
65 static string runtime = null;
66 static bool aot_compile = false;
67 static string aot_args = "static";
68 static string sdk_path = null;
69 static string lib_path = null;
70 static Dictionary<string,string> environment = new Dictionary<string,string>();
71 static string [] i18n = new string [] {
75 static string [] i18n_all = new string [] {
83 static string target_server = "https://download.mono-project.com/runtimes/raw/";
85 static int Main (string [] args)
87 List<string> sources = new List<string> ();
88 int top = args.Length;
93 for (int i = 0; i < top; i++){
95 case "--help": case "-h": case "-?":
113 var iarg = args [++i];
116 else if (iarg == "none")
117 i18n = new string [0];
119 i18n = iarg.Split (',');
130 case "--local-targets":
131 CommandLocalTargets ();
139 if (sdk_path != null || runtime != null)
140 Error ("You can only specify one of --runtime, --sdk or --cross");
143 cross_target = args [++i];
152 Console.Error.WriteLine ("--library can only be used with --simple/--runtime/--cross mode");
156 var lspec = args [++i];
157 var p = lspec.IndexOf (",");
160 alias = Path.GetFileName (lspec);
163 alias = lspec.Substring (0, p);
164 path = lspec.Substring (p+1);
166 if (!File.Exists (path))
167 Error ($"The specified library file {path} does not exist");
168 libraries [alias] = path;
171 case "--fetch-target":
176 fetch_target = args [++i];
179 case "--list-targets":
180 CommandLocalTargets ();
181 var wc = new WebClient ();
182 var s = wc.DownloadString (new Uri (target_server + "target-sdks.txt"));
183 Console.WriteLine ("Targets available for download with --fetch-target:\n" + s);
186 case "--target-server":
191 target_server = args [++i];
207 embedded_options = args [++i];
216 sdk_path = args [++i];
217 if (cross_target != null || runtime != null)
218 Error ("You can only specify one of --runtime, --sdk or --cross");
225 if (sdk_path != null || cross_target != null)
226 Error ("You can only specify one of --runtime, --sdk or --cross");
229 runtime = args [++i];
236 object_out = args [++i];
244 link_paths.Add (args [++i]);
268 config_file = args [++i];
270 case "--machine-config":
276 machine_config_file = args [++i];
279 Console.WriteLine ("WARNING:\n Check that the machine.config file you are bundling\n doesn't contain sensitive information specific to this machine.");
287 config_dir = args [++i];
295 case "--custom-main":
300 custom_main = args [++i];
314 Error ("Invalid style '{0}' - only 'windows', 'mac' and 'linux' are supported for --style argument", style);
322 case "--static-ctor":
327 ctor_func = args [++i];
330 case "--dos2unix=true":
333 case "--dos2unix=false":
334 use_dos2unix = false;
346 var env = args [++i];
347 p = env.IndexOf ('=');
349 environment.Add (env, "");
351 environment.Add (env.Substring (0, p), env.Substring (p+1));
353 case "--bundled-header":
354 bundled_header = true;
356 case "--aot-compile":
361 Console.WriteLine ("AOT arguments are passed as a comma-delimited list");
364 aot_args = String.Format("static,{0}", args [++i]);
367 sources.Add (args [i]);
371 // Modern bundling starts here
373 if (runtime != null){
374 // Nothing to do here, the user has chosen to manually specify --runtime nad libraries
375 } else if (sdk_path != null) {
376 VerifySdk (sdk_path);
377 } else if (cross_target == "default" || cross_target == null){
378 sdk_path = Path.GetFullPath (Path.Combine (Process.GetCurrentProcess().MainModule.FileName, "..", ".."));
379 VerifySdk (sdk_path);
381 sdk_path = Path.Combine (targets_dir, cross_target);
382 Console.WriteLine ("From: " + sdk_path);
383 VerifySdk (sdk_path);
387 if (fetch_target != null){
388 var directory = Path.Combine (targets_dir, fetch_target);
389 var zip_download = Path.Combine (directory, "sdk.zip");
390 Directory.CreateDirectory (directory);
391 var wc = new WebClient ();
392 var uri = new Uri ($"{target_server}{fetch_target}");
395 Console.WriteLine ($"Downloading runtime {uri} to {zip_download}");
398 wc.DownloadFile (uri, zip_download);
399 ZipFile.ExtractToDirectory(zip_download, directory);
400 File.Delete (zip_download);
402 Console.Error.WriteLine ($"Failure to download the specified runtime from {uri}");
403 File.Delete (zip_download);
410 Console.WriteLine (os_message);
411 Console.WriteLine ("Sources: {0} Auto-dependencies: {1}", sources.Count, autodeps);
414 if (sources.Count == 0 || output == null) {
416 Environment.Exit (1);
419 List<string> assemblies = LoadAssemblies (sources);
420 LoadLocalizedAssemblies (assemblies);
421 List<string> files = new List<string> ();
422 foreach (string file in assemblies)
423 if (!QueueAssembly (files, file))
428 GenerateBundles (files);
430 GeneratePackage (files);
432 Console.WriteLine ("Generated {0}", output);
437 static void VerifySdk (string path)
439 if (!Directory.Exists (path))
440 Error ($"The specified SDK path does not exist: {path}");
441 runtime = Path.Combine (sdk_path, "bin", "mono");
442 if (!File.Exists (runtime))
443 Error ($"The SDK location does not contain a {path}/bin/mono runtime");
444 lib_path = Path.Combine (path, "lib", "mono", "4.5");
445 if (!Directory.Exists (lib_path))
446 Error ($"The SDK location does not contain a {path}/lib/mono/4.5 directory");
447 link_paths.Add (lib_path);
450 static string targets_dir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), ".mono", "targets");
452 static void CommandLocalTargets ()
456 Console.WriteLine ("Available targets locally:");
457 Console.WriteLine ("\tdefault\t- Current System Mono");
459 targets = Directory.GetDirectories (targets_dir);
463 foreach (var target in targets){
464 var p = Path.Combine (target, "bin", "mono");
466 Console.WriteLine ("\t{0}", Path.GetFileName (target));
470 static void WriteSymbol (StreamWriter sw, string name, long size)
476 "\t.section .rodata\n" +
478 "\t.type {0}, \"object\"\n" +
479 "\t.size {0}, {1}\n" +
485 "\t.section __TEXT,__text,regular,pure_instructions\n" +
495 "\t.section .rdata,\"dr\"\n" +
503 static string [] chars = new string [256];
505 static void WriteBuffer (StreamWriter ts, Stream stream, byte[] buffer)
509 // Preallocate the strings we need.
510 if (chars [0] == null) {
511 for (int i = 0; i < chars.Length; i++)
512 chars [i] = string.Format ("{0}", i.ToString ());
515 while ((n = stream.Read (buffer, 0, buffer.Length)) != 0) {
517 for (int i = 0; i < n; i++) {
518 if (count % 32 == 0) {
519 ts.Write ("\n\t.byte ");
523 ts.Write (chars [buffer [i]]);
532 Dictionary<string, Tuple<long,int>> locations = new Dictionary<string, Tuple<long,int>> ();
533 const int align = 4096;
536 public PackageMaker (string output)
538 package = File.Create (output, 128*1024);
540 File.SetAttributes (output, unchecked ((FileAttributes) 0x80000000));
544 public int AddFile (string fname)
546 using (Stream fileStream = File.OpenRead (fname)){
547 var ret = fileStream.Length;
550 Console.WriteLine ("At {0:x} with input {1}", package.Position, fileStream.Length);
551 fileStream.CopyTo (package);
552 package.Position = package.Position + (align - (package.Position % align));
557 public void Add (string entry, string fname)
559 var p = package.Position;
560 var size = AddFile (fname);
562 locations [entry] = Tuple.Create(p, size);
565 public void AddString (string entry, string text)
567 var bytes = Encoding.UTF8.GetBytes (text);
568 locations [entry] = Tuple.Create (package.Position, bytes.Length);
569 package.Write (bytes, 0, bytes.Length);
570 package.Position = package.Position + (align - (package.Position % align));
573 public void AddStringPair (string entry, string key, string value)
575 var kbytes = Encoding.UTF8.GetBytes (key);
576 var vbytes = Encoding.UTF8.GetBytes (value);
578 Console.WriteLine ("ADDING {0} to {1}", key, value);
579 if (kbytes.Length > 255){
580 Console.WriteLine ("The key value can not exceed 255 characters: " + key);
581 Environment.Exit (1);
584 locations [entry] = Tuple.Create (package.Position, kbytes.Length+vbytes.Length+3);
585 package.WriteByte ((byte)kbytes.Length);
586 package.Write (kbytes, 0, kbytes.Length);
587 package.WriteByte (0);
588 package.Write (vbytes, 0, vbytes.Length);
589 package.WriteByte (0);
590 package.Position = package.Position + (align - (package.Position % align));
597 foreach (var floc in locations.Keys){
598 Console.WriteLine ($"{floc} at {locations[floc]:x}");
602 public void WriteIndex ()
604 var indexStart = package.Position;
605 var binary = new BinaryWriter (package);
607 binary.Write (locations.Count);
608 foreach (var entry in from entry in locations orderby entry.Value.Item1 ascending select entry){
609 var bytes = Encoding.UTF8.GetBytes (entry.Key);
610 binary.Write (bytes.Length+1);
611 binary.Write (bytes);
612 binary.Write ((byte) 0);
613 binary.Write (entry.Value.Item1);
614 binary.Write (entry.Value.Item2);
616 binary.Write (indexStart);
617 binary.Write (Encoding.UTF8.GetBytes ("xmonkeysloveplay"));
629 static bool MaybeAddFile (PackageMaker maker, string code, string file)
634 if (!File.Exists (file)){
635 Error ("The file {0} does not exist", file);
638 maker.Add (code, file);
642 static bool GeneratePackage (List<string> files)
644 if (runtime == null){
646 runtime = Process.GetCurrentProcess().MainModule.FileName;
648 Error ("You must specify at least one runtime with --runtime or --cross");
649 Environment.Exit (1);
652 if (!File.Exists (runtime)){
653 Error ($"The specified runtime at {runtime} does not exist");
654 Environment.Exit (1);
657 if (ctor_func != null){
658 Error ("--static-ctor not supported with package bundling, you must use native compilation for this");
662 var maker = new PackageMaker (output);
663 Console.WriteLine ("Using runtime: " + runtime);
664 maker.AddFile (runtime);
666 foreach (var url in files){
667 string fname = LocateFile (new Uri (url).LocalPath);
668 string aname = MakeBundle.GetAssemblyName (fname);
670 maker.Add ("assembly:" + aname, fname);
671 Console.WriteLine (" Assembly: " + fname);
672 if (File.Exists (fname + ".config")){
673 maker.Add ("config:" + aname, fname + ".config");
674 Console.WriteLine (" Config: " + runtime);
678 if (!MaybeAddFile (maker, "systemconfig:", config_file) || !MaybeAddFile (maker, "machineconfig:", machine_config_file))
681 if (config_dir != null)
682 maker.Add ("config_dir:", config_dir);
683 if (embedded_options != null)
684 maker.AddString ("options:", embedded_options);
685 if (environment.Count > 0){
686 foreach (var key in environment.Keys)
687 maker.AddStringPair ("env:" + key, key, environment [key]);
689 if (libraries.Count > 0){
690 foreach (var alias_and_path in libraries){
691 Console.WriteLine (" Library: " + alias_and_path.Value);
692 maker.Add ("library:" + alias_and_path.Key, alias_and_path.Value);
700 static void GenerateBundles (List<string> files)
702 string temp_s = "temp.s"; // Path.GetTempFileName ();
703 string temp_c = "temp.c";
704 string temp_o = "temp.o";
708 if (object_out != null)
712 List<string> c_bundle_names = new List<string> ();
713 List<string[]> config_names = new List<string[]> ();
715 using (StreamWriter ts = new StreamWriter (File.Create (temp_s))) {
716 using (StreamWriter tc = new StreamWriter (File.Create (temp_c))) {
719 if (bundled_header) {
720 tc.WriteLine ("/* This source code was produced by mkbundle, do not edit */");
721 tc.WriteLine ("\n#ifndef NULL\n#define NULL (void *)0\n#endif");
725 const unsigned char *data;
726 const unsigned int size;
727 } MonoBundledAssembly;
728 void mono_register_bundled_assemblies (const MonoBundledAssembly **assemblies);
729 void mono_register_config_for_assembly (const char* assembly_name, const char* config_xml);
732 tc.WriteLine ("#include <mono/metadata/mono-config.h>");
733 tc.WriteLine ("#include <mono/metadata/assembly.h>\n");
737 tc.WriteLine ("typedef struct _compressed_data {");
738 tc.WriteLine ("\tMonoBundledAssembly assembly;");
739 tc.WriteLine ("\tint compressed_size;");
740 tc.WriteLine ("} CompressedAssembly;\n");
743 object monitor = new object ();
745 var streams = new Dictionary<string, Stream> ();
746 var sizes = new Dictionary<string, long> ();
748 // Do the file reading and compression in parallel
749 Action<string> body = delegate (string url) {
750 string fname = LocateFile (new Uri (url).LocalPath);
751 Stream stream = File.OpenRead (fname);
753 long real_size = stream.Length;
756 byte[] cbuffer = new byte [8192];
757 MemoryStream ms = new MemoryStream ();
758 GZipStream deflate = new GZipStream (ms, CompressionMode.Compress, leaveOpen:true);
759 while ((n = stream.Read (cbuffer, 0, cbuffer.Length)) != 0){
760 deflate.Write (cbuffer, 0, n);
764 byte [] bytes = ms.GetBuffer ();
765 stream = new MemoryStream (bytes, 0, (int) ms.Length, false, false);
769 streams [url] = stream;
770 sizes [url] = real_size;
776 Parallel.ForEach (files, body);
778 foreach (var url in files)
782 // The non-parallel part
783 byte [] buffer = new byte [8192];
784 // everything other than a-zA-Z0-9_ needs to be escaped in asm symbols.
785 var symbolEscapeRE = new System.Text.RegularExpressions.Regex ("[^\\w_]");
786 foreach (var url in files) {
787 string fname = LocateFile (new Uri (url).LocalPath);
788 string aname = MakeBundle.GetAssemblyName (fname);
789 string encoded = symbolEscapeRE.Replace (aname, "_");
794 var stream = streams [url];
795 var real_size = sizes [url];
798 Console.WriteLine (" embedding: " + fname);
800 WriteSymbol (ts, "assembly_data_" + encoded, stream.Length);
802 WriteBuffer (ts, stream, buffer);
805 tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
806 tc.WriteLine ("static CompressedAssembly assembly_bundle_{0} = {{{{\"{1}\"," +
807 " assembly_data_{0}, {2}}}, {3}}};",
808 encoded, aname, real_size, stream.Length);
810 double ratio = ((double) stream.Length * 100) / real_size;
811 Console.WriteLine (" compression ratio: {0:.00}%", ratio);
814 tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
815 tc.WriteLine ("static const MonoBundledAssembly assembly_bundle_{0} = {{\"{1}\", assembly_data_{0}, {2}}};",
816 encoded, aname, real_size);
820 c_bundle_names.Add ("assembly_bundle_" + encoded);
823 FileStream cf = File.OpenRead (fname + ".config");
825 Console.WriteLine (" config from: " + fname + ".config");
826 tc.WriteLine ("extern const unsigned char assembly_config_{0} [];", encoded);
827 WriteSymbol (ts, "assembly_config_" + encoded, cf.Length);
828 WriteBuffer (ts, cf, buffer);
830 config_names.Add (new string[] {aname, encoded});
831 } catch (FileNotFoundException) {
832 /* we ignore if the config file doesn't exist */
837 // Read in static AOT file
840 if (config_file != null){
843 conf = File.OpenRead (config_file);
845 Error ("Failure to open {0}", config_file);
849 Console.WriteLine ("System config from: " + config_file);
850 tc.WriteLine ("extern const char system_config;");
851 WriteSymbol (ts, "system_config", config_file.Length);
853 WriteBuffer (ts, conf, buffer);
855 ts.Write ("\t.byte 0\n");
859 if (machine_config_file != null){
862 conf = File.OpenRead (machine_config_file);
864 Error ("Failure to open {0}", machine_config_file);
868 Console.WriteLine ("Machine config from: " + machine_config_file);
869 tc.WriteLine ("extern const char machine_config;");
870 WriteSymbol (ts, "machine_config", machine_config_file.Length);
872 WriteBuffer (ts, conf, buffer);
873 ts.Write ("\t.byte 0\n");
879 tc.WriteLine ("\nstatic const CompressedAssembly *compressed [] = {");
881 tc.WriteLine ("\nstatic const MonoBundledAssembly *bundled [] = {");
883 foreach (string c in c_bundle_names){
884 tc.WriteLine ("\t&{0},", c);
886 tc.WriteLine ("\tNULL\n};\n");
887 tc.WriteLine ("static char *image_name = \"{0}\";", prog);
889 if (ctor_func != null) {
890 tc.WriteLine ("\nextern void {0} (void);", ctor_func);
891 tc.WriteLine ("\n__attribute__ ((constructor)) static void mono_mkbundle_ctor (void)");
892 tc.WriteLine ("{{\n\t{0} ();\n}}", ctor_func);
895 tc.WriteLine ("\nstatic void install_dll_config_files (void) {\n");
896 foreach (string[] ass in config_names){
897 tc.WriteLine ("\tmono_register_config_for_assembly (\"{0}\", assembly_config_{1});\n", ass [0], ass [1]);
899 if (config_file != null)
900 tc.WriteLine ("\tmono_config_parse_memory (&system_config);\n");
901 if (machine_config_file != null)
902 tc.WriteLine ("\tmono_register_machine_config (&machine_config);\n");
903 tc.WriteLine ("}\n");
905 if (config_dir != null)
906 tc.WriteLine ("static const char *config_dir = \"{0}\";", config_dir);
908 tc.WriteLine ("static const char *config_dir = NULL;");
910 Stream template_stream;
912 template_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_z.c");
914 template_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template.c");
917 StreamReader s = new StreamReader (template_stream);
918 string template = s.ReadToEnd ();
921 if (!nomain && custom_main == null) {
922 Stream template_main_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_main.c");
923 StreamReader st = new StreamReader (template_main_stream);
924 string maintemplate = st.ReadToEnd ();
925 tc.Write (maintemplate);
930 string assembler = GetEnv("AS", "as");
931 string as_cmd = String.Format("{0} -o {1} {2} ", assembler, temp_o, temp_s);
938 Console.WriteLine("Compiling:");
940 if (style == "windows")
943 Func<string, string> quote = (pp) => { return "\"" + pp + "\""; };
945 string compiler = GetEnv("CC", "cl.exe");
946 string winsdkPath = GetEnv("WINSDK", @"C:\Program Files (x86)\Windows Kits\8.1");
947 string vsPath = GetEnv("VSINCLUDE", @"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC");
948 string monoPath = GetEnv("MONOPREFIX", @"C:\Program Files (x86)\Mono");
950 string[] includes = new string[] {winsdkPath + @"\Include\um", winsdkPath + @"\Include\shared", vsPath + @"\include", monoPath + @"\include\mono-2.0", "." };
951 // string[] libs = new string[] { winsdkPath + @"\Lib\winv6.3\um\x86" , vsPath + @"\lib" };
952 var linkLibraries = new string[] { "kernel32.lib",
967 string glue_obj = "mkbundle_glue.obj";
971 monoLib = LocateFile (monoPath + @"\lib\monosgen-2.0-static.lib");
974 Console.WriteLine ("WARNING: Dynamically linking the Mono runtime on Windows is not a tested option.");
975 monoLib = LocateFile (monoPath + @"\lib\monosgen-2.0.lib");
976 LocateFile (monoPath + @"\lib\monosgen-2.0.dll"); // in this case, the .lib is just the import library, and the .dll is also needed
979 var compilerArgs = new List<string>();
980 compilerArgs.Add("/MT");
982 foreach (string include in includes)
983 compilerArgs.Add(String.Format ("/I {0}", quote (include)));
985 if (!nomain || custom_main != null) {
986 compilerArgs.Add(quote(temp_c));
987 compilerArgs.Add(quote(temp_o));
988 if (custom_main != null)
989 compilerArgs.Add(quote(custom_main));
990 compilerArgs.Add(quote(monoLib));
991 compilerArgs.Add("/link");
992 compilerArgs.Add("/NODEFAULTLIB");
993 compilerArgs.Add("/SUBSYSTEM:windows");
994 compilerArgs.Add("/ENTRY:mainCRTStartup");
995 compilerArgs.AddRange(linkLibraries);
996 compilerArgs.Add("/out:"+ output);
998 string cl_cmd = String.Format("{0} {1}", compiler, String.Join(" ", compilerArgs.ToArray()));
1003 // we are just creating a .lib
1004 compilerArgs.Add("/c"); // compile only
1005 compilerArgs.Add(temp_c);
1006 compilerArgs.Add(String.Format("/Fo" + glue_obj)); // .obj output name
1008 string cl_cmd = String.Format("{0} {1}", compiler, String.Join(" ", compilerArgs.ToArray()));
1011 string librarian = GetEnv ("LIB", "lib.exe");
1012 var librarianArgs = new List<string> ();
1013 librarianArgs.Add (String.Format ("/out:{0}.lib" + output));
1014 librarianArgs.Add (temp_o);
1015 librarianArgs.Add (glue_obj);
1016 librarianArgs.Add (monoLib);
1017 string lib_cmd = String.Format("{0} {1}", librarian, String.Join(" ", librarianArgs.ToArray()));
1023 string zlib = (compress ? "-lz" : "");
1024 string debugging = "-g";
1025 string cc = GetEnv("CC", "cc");
1028 if (style == "linux")
1029 debugging = "-ggdb";
1034 smonolib = "`pkg-config --variable=libdir mono-2`/libmono-2.0.a ";
1036 smonolib = "-Wl,-Bstatic -lmono-2.0 -Wl,-Bdynamic ";
1037 cmd = String.Format("{4} -o '{2}' -Wall `pkg-config --cflags mono-2` {0} {3} " +
1038 "`pkg-config --libs-only-L mono-2` " + smonolib +
1039 "`pkg-config --libs-only-l mono-2 | sed -e \"s/\\-lmono-2.0 //\"` {1}",
1040 temp_c, temp_o, output, zlib, cc);
1045 cmd = String.Format("{4} " + debugging + " -o '{2}' -Wall {0} `pkg-config --cflags --libs mono-2` {3} {1}",
1046 temp_c, temp_o, output, zlib, cc);
1052 Console.WriteLine ("Done");
1057 if (object_out == null){
1058 File.Delete (temp_o);
1061 File.Delete (temp_c);
1063 File.Delete (temp_s);
1068 static List<string> LoadAssemblies (List<string> sources)
1070 List<string> assemblies = new List<string> ();
1073 foreach (string name in sources){
1075 Assembly a = LoadAssemblyFile (name);
1082 assemblies.Add (a.CodeBase);
1083 } catch (Exception) {
1086 Console.WriteLine ("File will not be scanned: {0}", name);
1087 assemblies.Add (new Uri (new FileInfo (name).FullName).ToString ());
1095 Error ("Couldn't load one or more of the assemblies.");
1096 Environment.Exit (1);
1102 static void LoadLocalizedAssemblies (List<string> assemblies)
1104 var other = i18n.Select (x => "I18N." + x + (x.Length > 0 ? "." : "") + "dll");
1105 string error = null;
1107 foreach (string name in other) {
1109 Assembly a = LoadAssembly (name);
1112 error = "Failed to load " + name;
1116 assemblies.Add (a.CodeBase);
1117 } catch (Exception) {
1120 Console.WriteLine ("File will not be scanned: {0}", name);
1121 assemblies.Add (new Uri (new FileInfo (name).FullName).ToString ());
1128 if (error != null) {
1129 Console.Error.WriteLine ("Failure to load i18n assemblies, the following directories were searched for the assemblies:");
1130 foreach (var path in link_paths){
1131 Console.Error.WriteLine (" Path: " + path);
1134 Console.WriteLine ("In Custom mode, you need to provide the directory to lookup assemblies from using -L");
1137 Error ("Couldn't load one or more of the i18n assemblies: " + error);
1138 Environment.Exit (1);
1143 static readonly Universe universe = new Universe ();
1144 static readonly Dictionary<string, string> loaded_assemblies = new Dictionary<string, string> ();
1146 public static string GetAssemblyName (string path)
1148 string resourcePathSeparator = style == "windows" ? "\\\\" : "/";
1149 string name = Path.GetFileName (path);
1151 // A bit of a hack to support satellite assemblies. They all share the same name but
1152 // are placed in subdirectories named after the locale they implement. Also, all of
1153 // them end in .resources.dll, therefore we can use that to detect the circumstances.
1154 if (name.EndsWith (".resources.dll", StringComparison.OrdinalIgnoreCase)) {
1155 string dir = Path.GetDirectoryName (path);
1156 int idx = dir.LastIndexOf (Path.DirectorySeparatorChar);
1158 name = dir.Substring (idx + 1) + resourcePathSeparator + name;
1159 Console.WriteLine ($"Storing satellite assembly '{path}' with name '{name}'");
1161 Console.WriteLine ($"Warning: satellite assembly {path} doesn't have locale path prefix, name conflicts possible");
1167 static bool QueueAssembly (List<string> files, string codebase)
1169 //Console.WriteLine ("CODE BASE IS {0}", codebase);
1170 if (files.Contains (codebase))
1173 var path = new Uri(codebase).LocalPath;
1174 var name = GetAssemblyName (path);
1176 if (loaded_assemblies.TryGetValue (name, out found)) {
1177 Error ("Duplicate assembly name `{0}'. Both `{1}' and `{2}' use same assembly name.", name, path, found);
1181 loaded_assemblies.Add (name, path);
1183 files.Add (codebase);
1187 Assembly a = universe.LoadFile (path);
1189 Error ("Unable to to load assembly `{0}'", path);
1193 foreach (AssemblyName an in a.GetReferencedAssemblies ()) {
1194 a = LoadAssembly (an.Name);
1196 Error ("Unable to load assembly `{0}' referenced by `{1}'", an.Name, path);
1200 if (!QueueAssembly (files, a.CodeBase))
1203 } catch (Exception) {
1212 // Loads an assembly from a specific path
1214 static Assembly LoadAssemblyFile (string assembly)
1220 Console.WriteLine ("Attempting to load assembly: {0}", assembly);
1221 a = universe.LoadFile (assembly);
1223 Console.WriteLine ("Assembly {0} loaded successfully.", assembly);
1225 } catch (FileNotFoundException){
1226 Error ($"Cannot find assembly `{assembly}'");
1227 } catch (IKVM.Reflection.BadImageFormatException f) {
1230 Error ($"Cannot load assembly (bad file format) " + f.Message);
1231 } catch (FileLoadException f){
1232 Error ($"Cannot load assembly " + f.Message);
1233 } catch (ArgumentNullException){
1234 Error( $"Cannot load assembly (null argument)");
1240 // Loads an assembly from any of the link directories provided
1242 static Assembly LoadAssembly (string assembly)
1244 string total_log = "";
1245 foreach (string dir in link_paths){
1246 string full_path = Path.Combine (dir, assembly);
1248 Console.WriteLine ("Attempting to load assembly from: " + full_path);
1249 if (!assembly.EndsWith (".dll") && !assembly.EndsWith (".exe"))
1250 full_path += ".dll";
1253 var a = universe.LoadFile (full_path);
1255 } catch (FileNotFoundException ff) {
1256 total_log += ff.FusionLog;
1261 Console.WriteLine ("Log: \n" + total_log);
1265 static void Error (string msg, params object [] args)
1267 Console.Error.WriteLine ("ERROR: {0}", string.Format (msg, args));
1268 Environment.Exit (1);
1273 Console.WriteLine ("Usage is: mkbundle [options] assembly1 [assembly2...]\n\n" +
1275 " --config F Bundle system config file `F'\n" +
1276 " --config-dir D Set MONO_CFG_DIR to `D'\n" +
1277 " --deps Turns on automatic dependency embedding (default on simple)\n" +
1278 " -L path Adds `path' to the search path for assemblies\n" +
1279 " --machine-config F Use the given file as the machine.config for the application.\n" +
1280 " -o out Specifies output filename\n" +
1281 " --nodeps Turns off automatic dependency embedding (default on custom)\n" +
1282 " --skip-scan Skip scanning assemblies that could not be loaded (but still embed them).\n" +
1283 " --i18n ENCODING none, all or comma separated list of CJK, MidWest, Other, Rare, West.\n" +
1284 " -v Verbose output\n" +
1285 " --bundled-header Do not attempt to include 'mono-config.h'. Define the entry points directly in the generated code\n" +
1287 "--simple Simple mode does not require a C toolchain and can cross compile\n" +
1288 " --cross TARGET Generates a binary for the given TARGET\n"+
1289 " --env KEY=VALUE Hardcodes an environment variable for the target\n" +
1290 " --fetch-target NAME Downloads the target SDK from the remote server\n" +
1291 " --library [LIB,]PATH Bundles the specified dynamic library to be used at runtime\n" +
1292 " LIB is optional shortname for file located at PATH\n" +
1293 " --list-targets Lists available targets on the remote server\n" +
1294 " --local-targets Lists locally available targets\n" +
1295 " --options OPTIONS Embed the specified Mono command line options on target\n" +
1296 " --runtime RUNTIME Manually specifies the Mono runtime to use\n" +
1297 " --sdk PATH Use a Mono SDK root location instead of a target\n" +
1298 " --target-server URL Specified a server to download targets from, default is " + target_server + "\n" +
1300 "--custom Builds a custom launcher, options for --custom\n" +
1301 " -c Produce stub only, do not compile\n" +
1302 " -oo obj Specifies output filename for helper object file\n" +
1303 " --dos2unix[=true|false]\n" +
1304 " When no value provided, or when `true` specified\n" +
1305 " `dos2unix` will be invoked to convert paths on Windows.\n" +
1306 " When `--dos2unix=false` used, dos2unix is NEVER used.\n" +
1307 " --keeptemp Keeps the temporary files\n" +
1308 " --static Statically link to mono libs\n" +
1309 " --nomain Don't include a main() function, for libraries\n" +
1310 " --custom-main C Link the specified compilation unit (.c or .obj) with entry point/init code\n" +
1311 " -z Compress the assemblies before embedding.\n" +
1312 " --static-ctor ctor Add a constructor call to the supplied function.\n" +
1313 " You need zlib development headers and libraries.\n");
1316 [DllImport ("libc")]
1317 static extern int system (string s);
1318 [DllImport ("libc")]
1319 static extern int uname (IntPtr buf);
1321 static void DetectOS ()
1324 os_message = "OS is: Windows";
1329 IntPtr buf = Marshal.AllocHGlobal (8192);
1330 if (uname (buf) != 0){
1331 os_message = "Warning: Unable to detect OS";
1332 Marshal.FreeHGlobal (buf);
1335 string os = Marshal.PtrToStringAnsi (buf);
1336 os_message = "OS is: " + os;
1340 Marshal.FreeHGlobal (buf);
1343 static bool IsUnix {
1345 int p = (int) Environment.OSVersion.Platform;
1346 return ((p == 4) || (p == 128) || (p == 6));
1350 static void AotCompile (List<string> files)
1352 foreach (var fileName in files) {
1353 string path = LocateFile (new Uri (fileName).LocalPath);
1354 Execute (String.Format ("{0} --aot={1} {2}", runtime, aot_args, path));
1358 static void Execute (string cmdLine)
1362 Console.WriteLine ("[execute cmd]: " + cmdLine);
1363 int ret = system (cmdLine);
1366 Error(String.Format("[Fail] {0}", ret));
1371 // on Windows, we have to pipe the output of a
1372 // `cmd` interpolation to dos2unix, because the shell does not
1373 // strip the CRLFs generated by the native pkg-config distributed
1376 // But if it's *not* on cygwin, just skip it.
1378 // check if dos2unix is applicable.
1379 if (use_dos2unix == true)
1381 var info = new ProcessStartInfo ("dos2unix");
1382 info.CreateNoWindow = true;
1383 info.RedirectStandardInput = true;
1384 info.UseShellExecute = false;
1385 var dos2unix = Process.Start (info);
1386 dos2unix.StandardInput.WriteLine ("aaa");
1387 dos2unix.StandardInput.WriteLine ("\u0004");
1388 dos2unix.StandardInput.Close ();
1389 dos2unix.WaitForExit ();
1390 if (dos2unix.ExitCode == 0)
1391 use_dos2unix = true;
1393 Console.WriteLine("Warning: dos2unix not found");
1394 use_dos2unix = false;
1397 if (use_dos2unix == null)
1398 use_dos2unix = false;
1400 ProcessStartInfo psi = new ProcessStartInfo();
1401 psi.UseShellExecute = false;
1403 // if there is no dos2unix, just run cmd /c.
1404 if (use_dos2unix == false)
1406 psi.FileName = "cmd";
1407 psi.Arguments = String.Format("/c \"{0}\"", cmdLine);
1411 psi.FileName = "sh";
1412 StringBuilder b = new StringBuilder();
1414 for (int i = 0; i < cmdLine.Length; i++)
1416 if (cmdLine[i] == '`')
1420 b.Append("|dos2unix");
1424 b.Append(cmdLine[i]);
1426 cmdLine = b.ToString();
1427 psi.Arguments = String.Format("-c \"{0}\"", cmdLine);
1431 Console.WriteLine(cmdLine);
1432 using (Process p = Process.Start (psi)) {
1434 int ret = p.ExitCode;
1436 Error ("[Fail] {0}", ret);
1441 static string GetEnv(string name, string defaultValue)
1443 string val = Environment.GetEnvironmentVariable(name);
1447 Console.WriteLine("{0} = {1}", name, val);
1453 Console.WriteLine("{0} = {1} (default)", name, val);
1458 static string LocateFile(string default_path)
1460 var override_path = Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(default_path));
1461 if (File.Exists(override_path))
1462 return override_path;
1463 else if (File.Exists(default_path))
1464 return default_path;
1466 throw new FileNotFoundException(default_path);