[mkbundle] Add support for --quiet.
[mono.git] / mcs / tools / mkbundle / mkbundle.cs
index 4611bf698423f9fbcbee43e1d4e881336f755735..fa39892bb03574f3d7a8a0f42533cef69a606f99 100755 (executable)
@@ -33,11 +33,13 @@ class MakeBundle {
        static string machine_config_file = null;
        static string config_dir = null;
        static string style = "linux";
+       static string os_message = "";
        static bool compress;
        static bool nomain;
        static bool? use_dos2unix = null;
        static bool skip_scan;
        static string ctor_func;
+       static bool quiet;
        
        static int Main (string [] args)
        {
@@ -94,8 +96,10 @@ class MakeBundle {
                                break;
                        case "--static":
                                static_link = true;
-                               Console.WriteLine ("Note that statically linking the LGPL Mono runtime has more licensing restrictions than dynamically linking.");
-                               Console.WriteLine ("See http://www.mono-project.com/Licensing for details on licensing.");
+                               if (!quiet) {
+                                       Console.WriteLine ("Note that statically linking the LGPL Mono runtime has more licensing restrictions than dynamically linking.");
+                                       Console.WriteLine ("See http://www.mono-project.com/Licensing for details on licensing.");
+                               }
                                break;
                        case "--config":
                                if (i+1 == top) {
@@ -113,7 +117,8 @@ class MakeBundle {
 
                                machine_config_file = args [++i];
 
-                               Console.WriteLine ("WARNING:\n  Check that the machine.config file you are bundling\n  doesn't contain sensitive information specific to this machine.");
+                               if (!quiet)
+                                       Console.WriteLine ("WARNING:\n  Check that the machine.config file you are bundling\n  doesn't contain sensitive information specific to this machine.");
                                break;
                        case "--config-dir":
                                if (i+1 == top) {
@@ -163,6 +168,10 @@ class MakeBundle {
                        case "--dos2unix=false":
                                use_dos2unix = false;
                                break;
+                       case "-q":
+                       case "--quiet":
+                               quiet = true;
+                               break;
                        default:
                                sources.Add (args [i]);
                                break;
@@ -170,7 +179,10 @@ class MakeBundle {
 
                }
 
-               Console.WriteLine ("Sources: {0} Auto-dependencies: {1}", sources.Count, autodeps);
+               if (!quiet) {
+                       Console.WriteLine (os_message);
+                       Console.WriteLine ("Sources: {0} Auto-dependencies: {1}", sources.Count, autodeps);
+               }
                if (sources.Count == 0 || output == null) {
                        Help ();
                        Environment.Exit (1);
@@ -299,7 +311,7 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
 
                        // Do the file reading and compression in parallel
                        Action<string> body = delegate (string url) {
-                               string fname = new Uri (url).LocalPath;
+                               string fname = LocateFile (new Uri (url).LocalPath);
                                Stream stream = File.OpenRead (fname);
 
                                long real_size = stream.Length;
@@ -334,7 +346,7 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                        // The non-parallel part
                        byte [] buffer = new byte [8192];
                        foreach (var url in files) {
-                               string fname = new Uri (url).LocalPath;
+                               string fname = LocateFile (new Uri (url).LocalPath);
                                string aname = Path.GetFileName (fname);
                                string encoded = aname.Replace ("-", "_").Replace (".", "_");
 
@@ -344,7 +356,8 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                                var stream = streams [url];
                                var real_size = sizes [url];
 
-                               Console.WriteLine ("   embedding: " + fname);
+                               if (!quiet)
+                                       Console.WriteLine ("   embedding: " + fname);
 
                                WriteSymbol (ts, "assembly_data_" + encoded, stream.Length);
                        
@@ -355,8 +368,10 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                                        tc.WriteLine ("static CompressedAssembly assembly_bundle_{0} = {{{{\"{1}\"," +
                                                                  " assembly_data_{0}, {2}}}, {3}}};",
                                                                  encoded, aname, real_size, stream.Length);
-                                       double ratio = ((double) stream.Length * 100) / real_size;
-                                       Console.WriteLine ("   compression ratio: {0:.00}%", ratio);
+                                       if (!quiet) {
+                                               double ratio = ((double) stream.Length * 100) / real_size;
+                                               Console.WriteLine ("   compression ratio: {0:.00}%", ratio);
+                                       }
                                } else {
                                        tc.WriteLine ("extern const unsigned char assembly_data_{0} [];", encoded);
                                        tc.WriteLine ("static const MonoBundledAssembly assembly_bundle_{0} = {{\"{1}\", assembly_data_{0}, {2}}};",
@@ -368,7 +383,8 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
 
                                try {
                                        FileStream cf = File.OpenRead (fname + ".config");
-                                       Console.WriteLine (" config from: " + fname + ".config");
+                                       if (!quiet)
+                                               Console.WriteLine (" config from: " + fname + ".config");
                                        tc.WriteLine ("extern const unsigned char assembly_config_{0} [];", encoded);
                                        WriteSymbol (ts, "assembly_config_" + encoded, cf.Length);
                                        WriteBuffer (ts, cf, buffer);
@@ -387,7 +403,8 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                                        Error (String.Format ("Failure to open {0}", config_file));
                                        return;
                                }
-                               Console.WriteLine ("System config from: " + config_file);
+                               if (!quiet)
+                                       Console.WriteLine ("System config from: " + config_file);
                                tc.WriteLine ("extern const char system_config;");
                                WriteSymbol (ts, "system_config", config_file.Length);
 
@@ -405,7 +422,8 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                                        Error (String.Format ("Failure to open {0}", machine_config_file));
                                        return;
                                }
-                               Console.WriteLine ("Machine config from: " + machine_config_file);
+                               if (!quiet)
+                                       Console.WriteLine ("Machine config from: " + machine_config_file);
                                tc.WriteLine ("extern const char machine_config;");
                                WriteSymbol (ts, "machine_config", machine_config_file.Length);
 
@@ -467,15 +485,18 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
 
                        tc.Close ();
 
+                       string assembler = GetEnv("AS", "as");
+                       string as_cmd = String.Format("{0} -o {1} {2} ", assembler, temp_o, temp_s);
+                       Execute(as_cmd);
+
                        if (compile_only)
                                return;
-                       Console.WriteLine("Compiling:");
+
+                       if (!quiet)
+                               Console.WriteLine("Compiling:");
 
                        if (style == "windows")
                        {
-                               string assembler = GetEnv("AS", "as");
-                               string as_cmd = String.Format("{0} -o {1} {2} ", assembler, temp_o, temp_s);
-                               Execute(as_cmd);
 
                                Func<string, string> quote = (pp) => { return "\"" + pp + "\""; };
 
@@ -492,19 +513,20 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                                foreach (string include in includes)
                                        compilerArgs.Add(String.Format ("/I {0}", quote (include)));
 
-                               if (static_link) {
-                                       compilerArgs.Add("/MT");
-                                       monoFile = monoPath + @"\lib\mono-2.0.lib";
-                               }
-                               else {
-                                       compilerArgs.Add("/MD");
-                                       monoFile = monoPath + @"\lib\mono-2.0.dll";
-                               }
+                               if (static_link)
+                                       monoFile = LocateFile (monoPath + @"\lib\monosgen-2.0.lib");
+                               else
+                                       monoFile = LocateFile (monoPath + @"\lib\monosgen-2.0.dll");
 
+                               compilerArgs.Add("/MD");
                                compilerArgs.Add(temp_c);
                                compilerArgs.Add(temp_o);
                                compilerArgs.Add("/link");
 
+                               if (nomain)
+                                       compilerArgs.Add("/NOENTRY");
+                                       compilerArgs.Add("/DLL");
+
                                foreach (string lib in libs)
                                        compilerArgs.Add(String.Format ("/LIBPATH:{0}", quote(lib)));
                                compilerArgs.Add (quote(monoFile));
@@ -514,13 +536,10 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                        }
                        else
                        {
-                               string assembler = GetEnv("AS", "as");
-                               string cmd = String.Format("{0} -o {1} {2} ", assembler, temp_o, temp_s);
-                               Execute(cmd);
-
                                string zlib = (compress ? "-lz" : "");
                                string debugging = "-g";
                                string cc = GetEnv("CC", "cc");
+                               string cmd = null;
 
                                if (style == "linux")
                                        debugging = "-ggdb";
@@ -545,7 +564,8 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                                Execute (cmd);
                        }
 
-                       Console.WriteLine ("Done");
+                       if (!quiet)
+                               Console.WriteLine ("Done");
                }
        }
                } finally {
@@ -576,9 +596,10 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                                }
                        
                                assemblies.Add (a.CodeBase);
-                       } catch (Exception e) {
+                       } catch (Exception) {
                                if (skip_scan) {
-                                       Console.WriteLine ("File will not be scanned: {0}", name);
+                                       if (!quiet)
+                                               Console.WriteLine ("File will not be scanned: {0}", name);
                                        assemblies.Add (new Uri (new FileInfo (name).FullName).ToString ());
                                } else {
                                        throw;
@@ -622,7 +643,7 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                                if (!QueueAssembly (files, a.CodeBase))
                                        return false;
                        }
-               } catch (Exception e) {
+               } catch (Exception) {
                        if (!skip_scan)
                                throw;
                }
@@ -663,7 +684,8 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                                }
                        }
                        Error ("Cannot find assembly `" + assembly + "'" );
-                       Console.WriteLine ("Log: \n" + total_log);
+                       if (!quiet)
+                               Console.WriteLine ("Log: \n" + total_log);
                } catch (IKVM.Reflection.BadImageFormatException f) {
                        if (skip_scan)
                                throw;
@@ -716,19 +738,19 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
        static void DetectOS ()
        {
                if (!IsUnix) {
-                       Console.WriteLine ("OS is: Windows");
+                       os_message = "OS is: Windows";
                        style = "windows";
                        return;
                }
 
                IntPtr buf = Marshal.AllocHGlobal (8192);
                if (uname (buf) != 0){
-                       Console.WriteLine ("Warning: Unable to detect OS");
+                       os_message = "Warning: Unable to detect OS";
                        Marshal.FreeHGlobal (buf);
                        return;
                }
                string os = Marshal.PtrToStringAnsi (buf);
-               Console.WriteLine ("OS is: " + os);
+               os_message = "OS is: " + os;
                if (os == "Darwin")
                        style = "osx";
                
@@ -745,12 +767,14 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
        static void Execute (string cmdLine)
        {
                if (IsUnix) {
-                       Console.WriteLine (cmdLine);
+                       if (!quiet)
+                               Console.WriteLine ("[execute cmd]: " + cmdLine);
                        int ret = system (cmdLine);
                        if (ret != 0)
                        {
                                Error(String.Format("[Fail] {0}", ret));
                        }
+                       return;
                }
 
                // on Windows, we have to pipe the output of a
@@ -812,7 +836,8 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                        psi.Arguments = String.Format("-c \"{0}\"", cmdLine);
                }
 
-               Console.WriteLine(cmdLine);
+               if (!quiet)
+                       Console.WriteLine(cmdLine);
                using (Process p = Process.Start (psi)) {
                        p.WaitForExit ();
                        int ret = p.ExitCode;
@@ -827,13 +852,26 @@ void          mono_register_config_for_assembly (const char* assembly_name, cons
                string val = Environment.GetEnvironmentVariable(name);
                if (val != null)
                {
-                       Console.WriteLine("{0} = {1}", name, val);
+                       if (!quiet)
+                               Console.WriteLine("{0} = {1}", name, val);
                }
                else
                {
                        val = defaultValue;
-                       Console.WriteLine("{0} = {1} (default)", name, val);
+                       if (!quiet)
+                               Console.WriteLine("{0} = {1} (default)", name, val);
                }
                return val;
        }
+
+       static string LocateFile(string default_path)
+       {
+               var override_path = Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(default_path));
+               if (File.Exists(override_path))
+                       return override_path;
+               else if (File.Exists(default_path))
+                       return default_path;
+               else
+                       throw new FileNotFoundException(default_path);
+       }
 }