Esempio n. 1
0
    static int Main(string[] args)
    {
        IKVM.Internal.Tracer.EnableTraceConsoleListener();
        IKVM.Internal.Tracer.EnableTraceForDebug();
        string        assemblyNameOrPath = null;
        bool          continueOnError    = false;
        bool          autoLoadSharedClassLoaderAssemblies = false;
        List <string> references = new List <string>();
        List <string> libpaths   = new List <string>();
        bool          nostdlib   = false;
        bool          bootstrap  = false;
        string        outputFile = null;
        bool          forwarders = false;

        foreach (string s in args)
        {
            if (s.StartsWith("-") || assemblyNameOrPath != null)
            {
                if (s == "-serialver")
                {
                    includeSerialVersionUID = true;
                }
                else if (s == "-skiperror")
                {
                    continueOnError = true;
                }
                else if (s == "-shared")
                {
                    autoLoadSharedClassLoaderAssemblies = true;
                }
                else if (s.StartsWith("-r:") || s.StartsWith("-reference:"))
                {
                    references.Add(s.Substring(s.IndexOf(':') + 1));
                }
                else if (s == "-nostdlib")
                {
                    nostdlib = true;
                }
                else if (s.StartsWith("-lib:"))
                {
                    libpaths.Add(s.Substring(5));
                }
                else if (s == "-bootstrap")
                {
                    bootstrap = true;
                }
                else if (s.StartsWith("-out:"))
                {
                    outputFile = s.Substring(5);
                }
                else if (s.StartsWith("-namespace:"))
                {
                    namespaces.Add(s.Substring(11) + ".");
                }
                else if (s == "-forwarders")
                {
                    forwarders = true;
                }
                else
                {
                    // unrecognized option, or multiple assemblies, print usage message and exit
                    assemblyNameOrPath = null;
                    break;
                }
            }
            else
            {
                assemblyNameOrPath = s;
            }
        }
        if (assemblyNameOrPath == null)
        {
            Console.Error.WriteLine(GetVersionAndCopyrightInfo());
            Console.Error.WriteLine();
            Console.Error.WriteLine("usage: ikvmstub [-options] <assemblyNameOrPath>");
            Console.Error.WriteLine();
            Console.Error.WriteLine("options:");
            Console.Error.WriteLine("    -out:<outputfile>          Specify the output filename");
            Console.Error.WriteLine("    -reference:<filespec>      Reference an assembly (short form -r:<filespec>)");
            Console.Error.WriteLine("    -serialver                 Include serialVersionUID fields");
            Console.Error.WriteLine("    -skiperror                 Continue when errors are encountered");
            Console.Error.WriteLine("    -shared                    Process all assemblies in shared group");
            Console.Error.WriteLine("    -nostdlib                  Do not reference standard libraries");
            Console.Error.WriteLine("    -lib:<dir>                 Additional directories to search for references");
            Console.Error.WriteLine("    -namespace:<ns>            Only include types from specified namespace");
            Console.Error.WriteLine("    -forwarders                Export forwarded types too");
            return(1);
        }
        if (File.Exists(assemblyNameOrPath) && nostdlib)
        {
            // Add the target assembly to the references list, to allow it to be considered as "mscorlib".
            // This allows "ikvmstub -nostdlib \...\mscorlib.dll" to work.
            references.Add(assemblyNameOrPath);
        }
        StaticCompiler.Resolver.Warning += new AssemblyResolver.WarningEvent(Resolver_Warning);
        StaticCompiler.Resolver.Init(StaticCompiler.Universe, nostdlib, references, libpaths);
        Dictionary <string, Assembly> cache = new Dictionary <string, Assembly>();

        foreach (string reference in references)
        {
            Assembly[] dummy = null;
            if (!StaticCompiler.Resolver.ResolveReference(cache, ref dummy, reference))
            {
                Console.Error.WriteLine("Error: reference not found {0}", reference);
                return(1);
            }
        }
        Assembly assembly = null;

        try
        {
            file = new FileInfo(assemblyNameOrPath);
        }
        catch (System.Exception x)
        {
            Console.Error.WriteLine("Error: unable to load \"{0}\"\n  {1}", assemblyNameOrPath, x.Message);
            return(1);
        }
        if (file != null && file.Exists)
        {
            assembly = StaticCompiler.LoadFile(assemblyNameOrPath);
        }
        else
        {
            assembly = StaticCompiler.Resolver.LoadWithPartialName(assemblyNameOrPath);
        }
        int rc = 0;

        if (assembly == null)
        {
            Console.Error.WriteLine("Error: Assembly \"{0}\" not found", assemblyNameOrPath);
        }
        else
        {
            if (bootstrap)
            {
                StaticCompiler.runtimeAssembly = StaticCompiler.LoadFile(typeof(NetExp).Assembly.Location);
                ClassLoaderWrapper.SetBootstrapClassLoader(new BootstrapBootstrapClassLoader());
            }
            else
            {
                StaticCompiler.LoadFile(typeof(NetExp).Assembly.Location);
                StaticCompiler.runtimeAssembly = StaticCompiler.LoadFile(Path.Combine(typeof(NetExp).Assembly.Location, "../IKVM.Runtime.dll"));
                JVM.CoreAssembly = StaticCompiler.LoadFile(Path.Combine(typeof(NetExp).Assembly.Location, "../IKVM.OpenJDK.Core.dll"));
            }
            if (AttributeHelper.IsJavaModule(assembly.ManifestModule))
            {
                Console.Error.WriteLine("Warning: Running ikvmstub on ikvmc compiled assemblies is not supported.");
            }
            if (outputFile == null)
            {
                outputFile = assembly.GetName().Name + ".jar";
            }
            try
            {
                using (zipFile = new ZipOutputStream(new FileStream(outputFile, FileMode.Create)))
                {
                    zipFile.SetComment(GetVersionAndCopyrightInfo());
                    try
                    {
                        List <Assembly> assemblies = new List <Assembly>();
                        assemblies.Add(assembly);
                        if (autoLoadSharedClassLoaderAssemblies)
                        {
                            LoadSharedClassLoaderAssemblies(assembly, assemblies);
                        }
                        foreach (Assembly asm in assemblies)
                        {
                            if (ProcessTypes(asm.GetTypes(), continueOnError) != 0)
                            {
                                rc = 1;
                                if (!continueOnError)
                                {
                                    break;
                                }
                            }
                            if (forwarders && ProcessTypes(asm.ManifestModule.__GetExportedTypes(), continueOnError) != 0)
                            {
                                rc = 1;
                                if (!continueOnError)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    catch (System.Exception x)
                    {
                        Console.Error.WriteLine(x);

                        if (!continueOnError)
                        {
                            Console.Error.WriteLine("Warning: Assembly reflection encountered an error. Resultant JAR may be incomplete.");
                        }

                        rc = 1;
                    }
                }
            }
            catch (ZipException x)
            {
                rc = 1;
                if (zipCount == 0)
                {
                    Console.Error.WriteLine("Error: Assembly contains no public IKVM.NET compatible types");
                }
                else
                {
                    Console.Error.WriteLine("Error: {0}", x.Message);
                }
            }
        }
        return(rc);
    }