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
10 // (C) 2016 Xamarin Inc
13 // * Add support for packaging native libraries, extracting at runtime and setting the library path.
14 // * Implement --list-targets lists all the available remote targets
17 using System.Diagnostics;
19 using System.Collections.Generic;
21 using System.IO.Compression;
22 using System.Runtime.InteropServices;
24 using IKVM.Reflection;
26 using System.Diagnostics;
28 using System.Threading.Tasks;
31 static string output = "a.out";
32 static string object_out = null;
33 static List<string> link_paths = new List<string> ();
34 static bool autodeps = false;
35 static bool keeptemp = false;
36 static bool compile_only = false;
37 static bool static_link = false;
38 static string config_file = null;
39 static string machine_config_file = null;
40 static string config_dir = null;
41 static string style = "linux";
42 static string os_message = "";
45 static string custom_main = null;
46 static bool? use_dos2unix = null;
47 static bool skip_scan;
48 static string ctor_func;
50 static string cross_target = null;
51 static string fetch_target = null;
52 static bool custom_mode = true;
53 static string embedded_options = null;
54 static string runtime = null;
55 static string target_server = "https://download.mono-project.com/runtimes/raw/";
57 static int Main (string [] args)
59 List<string> sources = new List<string> ();
60 int top = args.Length;
65 for (int i = 0; i < top; i++){
67 case "--help": case "-h": case "-?":
84 case "--local-targets":
85 CommandLocalTargets ();
95 cross_target = args [++i];
98 case "--fetch-target":
103 fetch_target = args [++i];
106 case "--list-targets":
107 var wc = new WebClient ();
108 var s = wc.DownloadString (new Uri (target_server + "target-list.txt"));
109 Console.WriteLine ("Cross-compilation targets available:\n" + s);
113 case "--target-server":
118 target_server = args [++i];
134 embedded_options = args [++i];
141 runtime = args [++i];
148 object_out = args [++i];
156 link_paths.Add (args [++i]);
180 config_file = args [++i];
182 case "--machine-config":
188 machine_config_file = args [++i];
191 Console.WriteLine ("WARNING:\n Check that the machine.config file you are bundling\n doesn't contain sensitive information specific to this machine.");
199 config_dir = args [++i];
207 case "--custom-main":
212 custom_main = args [++i];
226 Console.Error.WriteLine ("Invalid style '{0}' - only 'windows', 'mac' and 'linux' are supported for --style argument", style);
234 case "--static-ctor":
239 ctor_func = args [++i];
242 case "--dos2unix=true":
245 case "--dos2unix=false":
246 use_dos2unix = false;
253 sources.Add (args [i]);
259 if (fetch_target != null){
260 var truntime = Path.Combine (targets_dir, fetch_target, "mono");
261 Directory.CreateDirectory (Path.GetDirectoryName (truntime));
262 var wc = new WebClient ();
263 var uri = new Uri ($"{target_server}{fetch_target}");
266 Console.WriteLine ($"Downloading runtime {uri} to {truntime}");
269 wc.DownloadFile (uri, truntime);
271 Console.Error.WriteLine ($"Failure to download the specified runtime from {uri}");
272 File.Delete (truntime);
279 Console.WriteLine (os_message);
280 Console.WriteLine ("Sources: {0} Auto-dependencies: {1}", sources.Count, autodeps);
283 if (sources.Count == 0 || output == null) {
285 Environment.Exit (1);
288 List<string> assemblies = LoadAssemblies (sources);
289 List<string> files = new List<string> ();
290 foreach (string file in assemblies)
291 if (!QueueAssembly (files, file))
295 GenerateBundles (files);
297 if (cross_target == "default")
300 if (runtime == null){
301 if (cross_target == null){
302 Console.Error.WriteLine ("you should specify either a --runtime or a --cross compilation target");
303 Environment.Exit (1);
305 runtime = Path.Combine (targets_dir, cross_target, "mono");
306 if (!File.Exists (runtime)){
307 Console.Error.WriteLine ($"The runtime for the {cross_target} does not exist, use --fetch-target {cross_target} to download first");
311 if (!File.Exists (runtime)){
312 Console.Error.WriteLine ($"The Mono runtime specified with --runtime does not exist");
317 Console.WriteLine ("Using runtime {0}", runtime);
319 GeneratePackage (files);
325 static string targets_dir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), ".mono", "targets");
327 static void CommandLocalTargets ()
331 Console.WriteLine ("Available targets:");
332 Console.WriteLine ("\tdefault\t- Current System Mono");
334 targets = Directory.GetDirectories (targets_dir);
338 foreach (var target in targets){
339 var p = Path.Combine (target, "mono");
341 Console.WriteLine ("\t{0}", Path.GetFileName (target));
345 static void WriteSymbol (StreamWriter sw, string name, long size)
351 "\t.section .rodata\n" +
353 "\t.type {0}, \"object\"\n" +
354 "\t.size {0}, {1}\n" +
360 "\t.section __TEXT,__text,regular,pure_instructions\n" +
370 "\t.section .rdata,\"dr\"\n" +
378 static string [] chars = new string [256];
380 static void WriteBuffer (StreamWriter ts, Stream stream, byte[] buffer)
384 // Preallocate the strings we need.
385 if (chars [0] == null) {
386 for (int i = 0; i < chars.Length; i++)
387 chars [i] = string.Format ("{0}", i.ToString ());
390 while ((n = stream.Read (buffer, 0, buffer.Length)) != 0) {
392 for (int i = 0; i < n; i++) {
393 if (count % 32 == 0) {
394 ts.Write ("\n\t.byte ");
398 ts.Write (chars [buffer [i]]);
407 Dictionary<string, Tuple<long,int>> locations = new Dictionary<string, Tuple<long,int>> ();
408 const int align = 4096;
411 public PackageMaker (string output)
413 package = File.Create (output, 128*1024);
415 File.SetAttributes (output, unchecked ((FileAttributes) 0x80000000));
419 public int AddFile (string fname)
421 using (Stream fileStream = File.OpenRead (fname)){
422 var ret = fileStream.Length;
424 Console.WriteLine ("At {0:x} with input {1}", package.Position, fileStream.Length);
425 fileStream.CopyTo (package);
426 package.Position = package.Position + (align - (package.Position % align));
432 public void Add (string entry, string fname)
434 var p = package.Position;
435 var size = AddFile (fname);
437 locations [entry] = Tuple.Create(p, size);
440 public void AddString (string entry, string text)
442 var bytes = Encoding.UTF8.GetBytes (text);
443 locations [entry] = Tuple.Create (package.Position, bytes.Length);
444 package.Write (bytes, 0, bytes.Length);
445 package.Position = package.Position + (align - (package.Position % align));
450 foreach (var floc in locations.Keys){
451 Console.WriteLine ($"{floc} at {locations[floc]:x}");
455 public void WriteIndex ()
457 var indexStart = package.Position;
458 var binary = new BinaryWriter (package);
460 binary.Write (locations.Count);
461 foreach (var entry in from entry in locations orderby entry.Value.Item1 ascending select entry){
462 var bytes = Encoding.UTF8.GetBytes (entry.Key);
463 binary.Write (bytes.Length+1);
464 binary.Write (bytes);
465 binary.Write ((byte) 0);
466 binary.Write (entry.Value.Item1);
467 binary.Write (entry.Value.Item2);
469 binary.Write (indexStart);
470 binary.Write (Encoding.UTF8.GetBytes ("xmonkeysloveplay"));
482 static bool MaybeAddFile (PackageMaker maker, string code, string file)
487 if (!File.Exists (file)){
488 Console.Error.WriteLine ("The file {0} does not exist", file);
491 maker.Add (code, file);
495 static bool GeneratePackage (List<string> files)
497 if (runtime == null){
499 runtime = Process.GetCurrentProcess().MainModule.FileName;
501 Console.Error.WriteLine ("You must specify at least one runtime with --runtime or --cross");
502 Environment.Exit (1);
505 if (!File.Exists (runtime)){
506 Console.Error.WriteLine ($"The specified runtime at {runtime} does not exist");
507 Environment.Exit (1);
510 if (ctor_func != null){
511 Console.Error.WriteLine ("--static-ctor not supported with package bundling, you must use native compilation for this");
515 var maker = new PackageMaker (output);
516 maker.AddFile (runtime);
518 foreach (var url in files){
519 string fname = LocateFile (new Uri (url).LocalPath);
520 string aname = Path.GetFileName (fname);
522 maker.Add ("assembly:" + aname, fname);
523 if (File.Exists (fname + ".config"))
524 maker.Add ("config:" + aname, fname + ".config");
526 if (!MaybeAddFile (maker, "systemconfig:", config_file) || !MaybeAddFile (maker, "machineconfig:", machine_config_file))
529 if (config_dir != null)
530 maker.Add ("config_dir:", config_dir);
531 if (embedded_options != null)
532 maker.AddString ("options:", embedded_options);
538 static void GenerateBundles (List<string> files)
540 string temp_s = "temp.s"; // Path.GetTempFileName ();
541 string temp_c = "temp.c";
542 string temp_o = "temp.o";
546 if (object_out != null)
550 List<string> c_bundle_names = new List<string> ();
551 List<string[]> config_names = new List<string[]> ();
553 using (StreamWriter ts = new StreamWriter (File.Create (temp_s))) {
554 using (StreamWriter tc = new StreamWriter (File.Create (temp_c))) {
558 tc.WriteLine ("/* This source code was produced by mkbundle, do not edit */");
559 tc.WriteLine ("\n#ifndef NULL\n#define NULL (void *)0\n#endif");
563 const unsigned char *data;
564 const unsigned int size;
565 } MonoBundledAssembly;
566 void mono_register_bundled_assemblies (const MonoBundledAssembly **assemblies);
567 void mono_register_config_for_assembly (const char* assembly_name, const char* config_xml);
570 tc.WriteLine ("#include <mono/metadata/mono-config.h>");
571 tc.WriteLine ("#include <mono/metadata/assembly.h>\n");
575 tc.WriteLine ("typedef struct _compressed_data {");
576 tc.WriteLine ("\tMonoBundledAssembly assembly;");
577 tc.WriteLine ("\tint compressed_size;");
578 tc.WriteLine ("} CompressedAssembly;\n");
581 object monitor = new object ();
583 var streams = new Dictionary<string, Stream> ();
584 var sizes = new Dictionary<string, long> ();
586 // Do the file reading and compression in parallel
587 Action<string> body = delegate (string url) {
588 string fname = LocateFile (new Uri (url).LocalPath);
589 Stream stream = File.OpenRead (fname);
591 long real_size = stream.Length;
594 byte[] cbuffer = new byte [8192];
595 MemoryStream ms = new MemoryStream ();
596 GZipStream deflate = new GZipStream (ms, CompressionMode.Compress, leaveOpen:true);
597 while ((n = stream.Read (cbuffer, 0, cbuffer.Length)) != 0){
598 deflate.Write (cbuffer, 0, n);
602 byte [] bytes = ms.GetBuffer ();
603 stream = new MemoryStream (bytes, 0, (int) ms.Length, false, false);
607 streams [url] = stream;
608 sizes [url] = real_size;
614 Parallel.ForEach (files, body);
616 foreach (var url in files)
620 // The non-parallel part
621 byte [] buffer = new byte [8192];
622 // everything other than a-zA-Z0-9_ needs to be escaped in asm symbols.
623 var symbolEscapeRE = new System.Text.RegularExpressions.Regex ("[^\\w_]");
624 foreach (var url in files) {
625 string fname = LocateFile (new Uri (url).LocalPath);
626 string aname = Path.GetFileName (fname);
627 string encoded = symbolEscapeRE.Replace (aname, "_");
632 var stream = streams [url];
633 var real_size = sizes [url];
636 Console.WriteLine (" embedding: " + fname);
638 WriteSymbol (ts, "assembly_data_" + encoded, stream.Length);
640 WriteBuffer (ts, stream, buffer);
643 tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
644 tc.WriteLine ("static CompressedAssembly assembly_bundle_{0} = {{{{\"{1}\"," +
645 " assembly_data_{0}, {2}}}, {3}}};",
646 encoded, aname, real_size, stream.Length);
648 double ratio = ((double) stream.Length * 100) / real_size;
649 Console.WriteLine (" compression ratio: {0:.00}%", ratio);
652 tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
653 tc.WriteLine ("static const MonoBundledAssembly assembly_bundle_{0} = {{\"{1}\", assembly_data_{0}, {2}}};",
654 encoded, aname, real_size);
658 c_bundle_names.Add ("assembly_bundle_" + encoded);
661 FileStream cf = File.OpenRead (fname + ".config");
663 Console.WriteLine (" config from: " + fname + ".config");
664 tc.WriteLine ("extern const unsigned char assembly_config_{0} [];", encoded);
665 WriteSymbol (ts, "assembly_config_" + encoded, cf.Length);
666 WriteBuffer (ts, cf, buffer);
668 config_names.Add (new string[] {aname, encoded});
669 } catch (FileNotFoundException) {
670 /* we ignore if the config file doesn't exist */
674 if (config_file != null){
677 conf = File.OpenRead (config_file);
679 Error (String.Format ("Failure to open {0}", config_file));
683 Console.WriteLine ("System config from: " + config_file);
684 tc.WriteLine ("extern const char system_config;");
685 WriteSymbol (ts, "system_config", config_file.Length);
687 WriteBuffer (ts, conf, buffer);
689 ts.Write ("\t.byte 0\n");
693 if (machine_config_file != null){
696 conf = File.OpenRead (machine_config_file);
698 Error (String.Format ("Failure to open {0}", machine_config_file));
702 Console.WriteLine ("Machine config from: " + machine_config_file);
703 tc.WriteLine ("extern const char machine_config;");
704 WriteSymbol (ts, "machine_config", machine_config_file.Length);
706 WriteBuffer (ts, conf, buffer);
707 ts.Write ("\t.byte 0\n");
713 tc.WriteLine ("\nstatic const CompressedAssembly *compressed [] = {");
715 tc.WriteLine ("\nstatic const MonoBundledAssembly *bundled [] = {");
717 foreach (string c in c_bundle_names){
718 tc.WriteLine ("\t&{0},", c);
720 tc.WriteLine ("\tNULL\n};\n");
721 tc.WriteLine ("static char *image_name = \"{0}\";", prog);
723 if (ctor_func != null) {
724 tc.WriteLine ("\nextern void {0} (void);", ctor_func);
725 tc.WriteLine ("\n__attribute__ ((constructor)) static void mono_mkbundle_ctor (void)");
726 tc.WriteLine ("{{\n\t{0} ();\n}}", ctor_func);
729 tc.WriteLine ("\nstatic void install_dll_config_files (void) {\n");
730 foreach (string[] ass in config_names){
731 tc.WriteLine ("\tmono_register_config_for_assembly (\"{0}\", assembly_config_{1});\n", ass [0], ass [1]);
733 if (config_file != null)
734 tc.WriteLine ("\tmono_config_parse_memory (&system_config);\n");
735 if (machine_config_file != null)
736 tc.WriteLine ("\tmono_register_machine_config (&machine_config);\n");
737 tc.WriteLine ("}\n");
739 if (config_dir != null)
740 tc.WriteLine ("static const char *config_dir = \"{0}\";", config_dir);
742 tc.WriteLine ("static const char *config_dir = NULL;");
744 Stream template_stream;
746 template_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_z.c");
748 template_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template.c");
751 StreamReader s = new StreamReader (template_stream);
752 string template = s.ReadToEnd ();
755 if (!nomain && custom_main == null) {
756 Stream template_main_stream = System.Reflection.Assembly.GetAssembly (typeof(MakeBundle)).GetManifestResourceStream ("template_main.c");
757 StreamReader st = new StreamReader (template_main_stream);
758 string maintemplate = st.ReadToEnd ();
759 tc.Write (maintemplate);
764 string assembler = GetEnv("AS", "as");
765 string as_cmd = String.Format("{0} -o {1} {2} ", assembler, temp_o, temp_s);
772 Console.WriteLine("Compiling:");
774 if (style == "windows")
777 Func<string, string> quote = (pp) => { return "\"" + pp + "\""; };
779 string compiler = GetEnv("CC", "cl.exe");
780 string winsdkPath = GetEnv("WINSDK", @"C:\Program Files (x86)\Windows Kits\8.1");
781 string vsPath = GetEnv("VSINCLUDE", @"C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC");
782 string monoPath = GetEnv("MONOPREFIX", @"C:\Program Files (x86)\Mono");
784 string[] includes = new string[] {winsdkPath + @"\Include\um", winsdkPath + @"\Include\shared", vsPath + @"\include", monoPath + @"\include\mono-2.0", "." };
785 string[] libs = new string[] { winsdkPath + @"\Lib\winv6.3\um\x86" , vsPath + @"\lib" };
786 var linkLibraries = new string[] { "kernel32.lib",
801 string glue_obj = "mkbundle_glue.obj";
805 monoLib = LocateFile (monoPath + @"\lib\monosgen-2.0-static.lib");
808 Console.WriteLine ("WARNING: Dynamically linking the Mono runtime on Windows is not a tested option.");
809 monoLib = LocateFile (monoPath + @"\lib\monosgen-2.0.lib");
810 LocateFile (monoPath + @"\lib\monosgen-2.0.dll"); // in this case, the .lib is just the import library, and the .dll is also needed
813 var compilerArgs = new List<string>();
814 compilerArgs.Add("/MT");
816 foreach (string include in includes)
817 compilerArgs.Add(String.Format ("/I {0}", quote (include)));
819 if (!nomain || custom_main != null) {
820 compilerArgs.Add(quote(temp_c));
821 compilerArgs.Add(quote(temp_o));
822 if (custom_main != null)
823 compilerArgs.Add(quote(custom_main));
824 compilerArgs.Add(quote(monoLib));
825 compilerArgs.Add("/link");
826 compilerArgs.Add("/NODEFAULTLIB");
827 compilerArgs.Add("/SUBSYSTEM:windows");
828 compilerArgs.Add("/ENTRY:mainCRTStartup");
829 compilerArgs.AddRange(linkLibraries);
830 compilerArgs.Add("/out:"+ output);
832 string cl_cmd = String.Format("{0} {1}", compiler, String.Join(" ", compilerArgs.ToArray()));
837 // we are just creating a .lib
838 compilerArgs.Add("/c"); // compile only
839 compilerArgs.Add(temp_c);
840 compilerArgs.Add(String.Format("/Fo" + glue_obj)); // .obj output name
842 string cl_cmd = String.Format("{0} {1}", compiler, String.Join(" ", compilerArgs.ToArray()));
845 string librarian = GetEnv ("LIB", "lib.exe");
846 var librarianArgs = new List<string> ();
847 librarianArgs.Add (String.Format ("/out:{0}.lib" + output));
848 librarianArgs.Add (temp_o);
849 librarianArgs.Add (glue_obj);
850 librarianArgs.Add (monoLib);
851 string lib_cmd = String.Format("{0} {1}", librarian, String.Join(" ", librarianArgs.ToArray()));
857 string zlib = (compress ? "-lz" : "");
858 string debugging = "-g";
859 string cc = GetEnv("CC", "cc");
862 if (style == "linux")
868 smonolib = "`pkg-config --variable=libdir mono-2`/libmono-2.0.a ";
870 smonolib = "-Wl,-Bstatic -lmono-2.0 -Wl,-Bdynamic ";
871 cmd = String.Format("{4} -o '{2}' -Wall `pkg-config --cflags mono-2` {0} {3} " +
872 "`pkg-config --libs-only-L mono-2` " + smonolib +
873 "`pkg-config --libs-only-l mono-2 | sed -e \"s/\\-lmono-2.0 //\"` {1}",
874 temp_c, temp_o, output, zlib, cc);
879 cmd = String.Format("{4} " + debugging + " -o '{2}' -Wall {0} `pkg-config --cflags --libs mono-2` {3} {1}",
880 temp_c, temp_o, output, zlib, cc);
886 Console.WriteLine ("Done");
891 if (object_out == null){
892 File.Delete (temp_o);
895 File.Delete (temp_c);
897 File.Delete (temp_s);
902 static List<string> LoadAssemblies (List<string> sources)
904 List<string> assemblies = new List<string> ();
907 foreach (string name in sources){
909 Assembly a = LoadAssembly (name);
916 assemblies.Add (a.CodeBase);
917 } catch (Exception) {
920 Console.WriteLine ("File will not be scanned: {0}", name);
921 assemblies.Add (new Uri (new FileInfo (name).FullName).ToString ());
929 Environment.Exit (1);
934 static readonly Universe universe = new Universe ();
935 static readonly Dictionary<string, string> loaded_assemblies = new Dictionary<string, string> ();
937 static bool QueueAssembly (List<string> files, string codebase)
939 // Console.WriteLine ("CODE BASE IS {0}", codebase);
940 if (files.Contains (codebase))
943 var path = new Uri(codebase).LocalPath;
944 var name = Path.GetFileName (path);
946 if (loaded_assemblies.TryGetValue (name, out found)) {
947 Error (string.Format ("Duplicate assembly name `{0}'. Both `{1}' and `{2}' use same assembly name.", name, path, found));
951 loaded_assemblies.Add (name, path);
953 files.Add (codebase);
957 Assembly a = universe.LoadFile (path);
959 foreach (AssemblyName an in a.GetReferencedAssemblies ()) {
960 a = universe.Load (an.FullName);
961 if (!QueueAssembly (files, a.CodeBase))
964 } catch (Exception) {
972 static Assembly LoadAssembly (string assembly)
977 char[] path_chars = { '/', '\\' };
979 if (assembly.IndexOfAny (path_chars) != -1) {
980 a = universe.LoadFile (assembly);
982 string ass = assembly;
983 if (ass.EndsWith (".dll"))
984 ass = assembly.Substring (0, assembly.Length - 4);
985 a = universe.Load (ass);
988 } catch (FileNotFoundException){
989 string total_log = "";
991 foreach (string dir in link_paths){
992 string full_path = Path.Combine (dir, assembly);
993 if (!assembly.EndsWith (".dll") && !assembly.EndsWith (".exe"))
997 a = universe.LoadFile (full_path);
999 } catch (FileNotFoundException ff) {
1000 total_log += ff.FusionLog;
1004 Error ("Cannot find assembly `" + assembly + "'" );
1006 Console.WriteLine ("Log: \n" + total_log);
1007 } catch (IKVM.Reflection.BadImageFormatException f) {
1010 Error ("Cannot load assembly (bad file format) " + f.Message);
1011 } catch (FileLoadException f){
1012 Error ("Cannot load assembly " + f.Message);
1013 } catch (ArgumentNullException){
1014 Error("Cannot load assembly (null argument)");
1019 static void Error (string msg)
1021 Console.Error.WriteLine ("ERROR: " + msg);
1022 Environment.Exit (1);
1027 Console.WriteLine ("Usage is: mkbundle [options] assembly1 [assembly2...]\n\n" +
1029 " --config F Bundle system config file `F'\n" +
1030 " --config-dir D Set MONO_CFG_DIR to `D'\n" +
1031 " --deps Turns on automatic dependency embedding (default on simple)\n" +
1032 " -L path Adds `path' to the search path for assemblies\n" +
1033 " --machine-config F Use the given file as the machine.config for the application.\n" +
1034 " -o out Specifies output filename\n" +
1035 " --nodeps Turns off automatic dependency embedding (default on custom)\n" +
1036 " --skip-scan Skip scanning assemblies that could not be loaded (but still embed them).\n" +
1038 "--simple Simple mode does not require a C toolchain and can cross compile\n" +
1039 " --cross TARGET Generates a binary for the given TARGET\n"+
1040 " --local-targets Lists locally available targets\n" +
1041 " --list-targets Lists available targets on the remote server\n" +
1042 " --options OPTIONS Embed the specified Mono command line options on target\n" +
1043 " --runtime RUNTIME Manually specifies the Mono runtime to use\n" +
1044 " --target-server URL Specified a server to download targets from, default is " + target_server + "\n" +
1046 "--custom Builds a custom launcher, options for --custom\n" +
1047 " -c Produce stub only, do not compile\n" +
1048 " -oo obj Specifies output filename for helper object file\n" +
1049 " --dos2unix[=true|false]\n" +
1050 " When no value provided, or when `true` specified\n" +
1051 " `dos2unix` will be invoked to convert paths on Windows.\n" +
1052 " When `--dos2unix=false` used, dos2unix is NEVER used.\n" +
1053 " --keeptemp Keeps the temporary files\n" +
1054 " --static Statically link to mono libs\n" +
1055 " --nomain Don't include a main() function, for libraries\n" +
1056 " --custom-main C Link the specified compilation unit (.c or .obj) with entry point/init code\n" +
1057 " -z Compress the assemblies before embedding.\n" +
1058 " --static-ctor ctor Add a constructor call to the supplied function.\n" +
1059 " You need zlib development headers and libraries.\n");
1062 [DllImport ("libc")]
1063 static extern int system (string s);
1064 [DllImport ("libc")]
1065 static extern int uname (IntPtr buf);
1067 static void DetectOS ()
1070 os_message = "OS is: Windows";
1075 IntPtr buf = Marshal.AllocHGlobal (8192);
1076 if (uname (buf) != 0){
1077 os_message = "Warning: Unable to detect OS";
1078 Marshal.FreeHGlobal (buf);
1081 string os = Marshal.PtrToStringAnsi (buf);
1082 os_message = "OS is: " + os;
1086 Marshal.FreeHGlobal (buf);
1089 static bool IsUnix {
1091 int p = (int) Environment.OSVersion.Platform;
1092 return ((p == 4) || (p == 128) || (p == 6));
1096 static void Execute (string cmdLine)
1100 Console.WriteLine ("[execute cmd]: " + cmdLine);
1101 int ret = system (cmdLine);
1104 Error(String.Format("[Fail] {0}", ret));
1109 // on Windows, we have to pipe the output of a
1110 // `cmd` interpolation to dos2unix, because the shell does not
1111 // strip the CRLFs generated by the native pkg-config distributed
1114 // But if it's *not* on cygwin, just skip it.
1116 // check if dos2unix is applicable.
1117 if (use_dos2unix == true)
1119 var info = new ProcessStartInfo ("dos2unix");
1120 info.CreateNoWindow = true;
1121 info.RedirectStandardInput = true;
1122 info.UseShellExecute = false;
1123 var dos2unix = Process.Start (info);
1124 dos2unix.StandardInput.WriteLine ("aaa");
1125 dos2unix.StandardInput.WriteLine ("\u0004");
1126 dos2unix.StandardInput.Close ();
1127 dos2unix.WaitForExit ();
1128 if (dos2unix.ExitCode == 0)
1129 use_dos2unix = true;
1131 Console.WriteLine("Warning: dos2unix not found");
1132 use_dos2unix = false;
1135 if (use_dos2unix == null)
1136 use_dos2unix = false;
1138 ProcessStartInfo psi = new ProcessStartInfo();
1139 psi.UseShellExecute = false;
1141 // if there is no dos2unix, just run cmd /c.
1142 if (use_dos2unix == false)
1144 psi.FileName = "cmd";
1145 psi.Arguments = String.Format("/c \"{0}\"", cmdLine);
1149 psi.FileName = "sh";
1150 StringBuilder b = new StringBuilder();
1152 for (int i = 0; i < cmdLine.Length; i++)
1154 if (cmdLine[i] == '`')
1158 b.Append("|dos2unix");
1162 b.Append(cmdLine[i]);
1164 cmdLine = b.ToString();
1165 psi.Arguments = String.Format("-c \"{0}\"", cmdLine);
1169 Console.WriteLine(cmdLine);
1170 using (Process p = Process.Start (psi)) {
1172 int ret = p.ExitCode;
1174 Error (String.Format("[Fail] {0}", ret));
1179 static string GetEnv(string name, string defaultValue)
1181 string val = Environment.GetEnvironmentVariable(name);
1185 Console.WriteLine("{0} = {1}", name, val);
1191 Console.WriteLine("{0} = {1} (default)", name, val);
1196 static string LocateFile(string default_path)
1198 var override_path = Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(default_path));
1199 if (File.Exists(override_path))
1200 return override_path;
1201 else if (File.Exists(default_path))
1202 return default_path;
1204 throw new FileNotFoundException(default_path);