Ejemplo 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")
                {
                    Console.Error.WriteLine("The -serialver option is deprecated and will be removed in the future. Use -japi instead.");
                    includeSerialVersionUID = true;
                }
                else if (s == "-japi")
                {
                    includeSerialVersionUID    = true;
                    includeNonPublicInterfaces = true;
                    includeNonPublicMembers    = 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 if (s == "-parameters")
                {
                    includeParameterNames = 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("    -japi                      Generate jar suitable for comparison with japitools");
            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");
            Console.Error.WriteLine("    -parameters                Emit Java 8 classes with parameter names");
            return(1);
        }
        if (File.Exists(assemblyNameOrPath) && nostdlib)
        {
            // Add the target assembly to the references list, to allow it to be considered as "System.Runtime".
            // 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)

        {
            Console.Error.WriteLine($"Located file and loading assembly at {assemblyNameOrPath}");
            assembly = StaticCompiler.LoadFile(assemblyNameOrPath);
        }
        else
        {
            Console.Error.WriteLine($"Trying to locate and load {assemblyNameOrPath}");
            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);
    }
Ejemplo n.º 2
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;

        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
                {
                    // 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 [-serialver] [-skiperror] [-reference:<assembly>] [-lib:<dir>] <assemblyNameOrPath>");
            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;
            int        rc1   = StaticCompiler.Resolver.ResolveReference(cache, ref dummy, reference);
            if (rc1 != 0)
            {
                return(rc1);
            }
        }
        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.");
            }
            try
            {
                using (zipFile = new ZipOutputStream(new FileStream(assembly.GetName().Name + ".jar", 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 (ProcessAssembly(asm, continueOnError) != 0)
                            {
                                rc = 1;
                                if (!continueOnError)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    catch (TypeLoadException x)
                    {
                        Console.WriteLine(x);
                    }
                    catch (System.Exception x)
                    {
                        Console.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);
    }