Beispiel #1
0
        public bool WriteAssembly()
        {
            var parameters = new CompilerParameters
            {
                //NOTE: we place this assembly in the output directory, the linker will move it to the final folder
                OutputAssembly = Path.Combine(OutputDirectory, "Resource.designer.dll"),
            };

            parameters.ReferencedAssemblies.Add(XamarinAndroid.FindAssembly("System.dll"));
            parameters.ReferencedAssemblies.Add(XamarinAndroid.FindAssembly("System.Runtime.dll"));
            parameters.ReferencedAssemblies.Add(XamarinAndroid.FindAssembly("Java.Interop.dll"));
            parameters.ReferencedAssemblies.Add(XamarinAndroid.FindAssembly("Mono.Android.dll"));
            foreach (var assembly in Assemblies)
            {
                parameters.ReferencedAssemblies.Add(assembly.Location);
            }

            var results = csc.CompileAssemblyFromDom(parameters, unit);

            if (results.Errors.HasErrors)
            {
                foreach (var error in results.Errors)
                {
                    Diagnostics.Error("Error: {0}", error);
                }
                return(false);
            }

            return(true);
        }
        static void ResolveAssemblies(ProjectTargetElement target, List<IKVM.Reflection.Assembly> assemblies)
        {
            var resolveAssemblies = target.AddTask("ResolveAssemblies");
            var assemblyPaths = assemblies.Select(a => a.Location).ToList();
            //NOTE: [Export] requires Mono.Android.Export.dll
            assemblyPaths.Add(XamarinAndroid.FindAssembly("Mono.Android.Export.dll"));

            resolveAssemblies.SetParameter("Assemblies", string.Join(";", assemblyPaths));
            resolveAssemblies.SetParameter("LinkMode", LinkMode);
            resolveAssemblies.SetParameter("ReferenceAssembliesDirectory", "$(TargetFrameworkDirectory)");
            resolveAssemblies.AddOutputItem("ResolvedAssemblies", "ResolvedAssemblies");
            resolveAssemblies.AddOutputItem("ResolvedUserAssemblies", "ResolvedUserAssemblies");
            resolveAssemblies.AddOutputItem("ResolvedFrameworkAssemblies", "ResolvedFrameworkAssemblies");
        }
Beispiel #3
0
        bool CompileJava(IEnumerable <string> files)
        {
            var executableSuffix = Platform.IsWindows ? ".exe" : string.Empty;
            var javaSdk          = XamarinAndroid.JavaSdkPath;
            var javac            = Path.Combine(javaSdk, "bin", "javac" + executableSuffix);
            var classesDir       = Path.Combine(Options.OutputDir, "classes");
            var bootClassPath    = Path.Combine(javaSdk, "jre", "lib", "rt.jar");

            if (Options.Compilation.Platform == TargetPlatform.Android)
            {
                bootClassPath = Path.Combine(XamarinAndroid.PlatformDirectory, "android.jar");
            }

            var javaFiles = files.Select(file => Path.GetFullPath(file)).ToList();

            var supportFiles = Directory.GetFiles(FindDirectory("support"), "*.java", SearchOption.AllDirectories)
                               .Where(f => Options.Compilation.Platform == TargetPlatform.Android || Path.GetFileName(Path.GetDirectoryName(f)) != "android");

            javaFiles.AddRange(supportFiles);

            //NOTE: GenerateJavaStubs puts them in /src/
            if (Options.Compilation.Platform == TargetPlatform.Android)
            {
                var stubsPath = Path.Combine(Options.OutputDir, "src");
                if (Directory.Exists(stubsPath))
                {
                    var stubFiles = Directory.GetFiles(stubsPath, "*.java", SearchOption.AllDirectories);
                    javaFiles.AddRange(stubFiles);
                }
            }

            var args = new List <string> {
                string.Join(" ", javaFiles),
                "-encoding UTF-8",
                $"-source {XamarinAndroid.JavaVersion} -target {XamarinAndroid.JavaVersion}",
                $"-bootclasspath \"{bootClassPath}\"",
                $"-d {classesDir}",
            };

            if (Options.Compilation.DebugMode)
            {
                args.Add("-g");
            }

            //Jar files needed: JNA, android.jar, mono.android.jar
            args.Add("-cp");

            var jnaJar = Path.Combine(FindDirectory("external"), "jna", "jna-4.4.0.jar");

            if (Options.Compilation.Platform == TargetPlatform.Android)
            {
                var androidJar     = Path.Combine(XamarinAndroid.PlatformDirectory, "android.jar");
                var monoAndroidJar = XamarinAndroid.FindAssembly("mono.android.jar");
                var delimiter      = Platform.IsWindows ? ";" : ":";
                var classpath      = new List <string> {
                    jnaJar, androidJar, monoAndroidJar
                };

                //Now we need to add any additional jars from binding projects to the classpath
                var intermediateDir = Path.Combine(Options.OutputDir, "obj");
                if (Directory.Exists(intermediateDir))
                {
                    foreach (var jar in Directory.GetFiles(intermediateDir, "*.jar", SearchOption.AllDirectories))
                    {
                        classpath.Add(jar);
                    }
                }

                //There are yet, another set of jar files
                string resourcePaths = Path.Combine(Options.OutputDir, XamarinAndroidBuild.IntermediateDir, XamarinAndroidBuild.ResourcePaths);
                if (File.Exists(resourcePaths))
                {
                    var document = new XmlDocument();
                    document.Load(resourcePaths);
                    foreach (XmlNode node in document.SelectNodes("/Paths/AdditionalJavaLibraryReferences/AdditionalJavaLibraryReference"))
                    {
                        classpath.Add(node.InnerText);
                    }
                }

                args.Add("\"" + string.Join(delimiter, classpath) + "\"");
            }
            else
            {
                args.Add(jnaJar);
            }

            //If "classes" directory doesn't exists, javac fails
            if (!Directory.Exists(classesDir))
            {
                Directory.CreateDirectory(classesDir);
            }

            var invocation = string.Join(" ", args);
            var output     = Invoke(javac, invocation);

            return(output.ExitCode == 0);
        }
Beispiel #4
0
        bool CreateJar()
        {
            var executableSuffix = Platform.IsWindows ? ".exe" : string.Empty;
            var jar        = Path.Combine(XamarinAndroid.JavaSdkPath, "bin", "jar" + executableSuffix);
            var classesDir = Path.Combine(Options.OutputDir, "classes");
            var name       = Path.GetFileNameWithoutExtension(Project.Assemblies[0]).Replace('-', '_');

            var args = new List <string> {
                "cf",
                Path.Combine(Options.OutputDir, name + ".jar"),
                $"-C {classesDir} ."
            };

            // On desktop Java, we need a few more files included
            if (Options.Compilation.Platform != TargetPlatform.Android)
            {
                //Copy native libs
                var platformDir = Path.Combine(classesDir, GetJnaPlatformDir());
                if (!Directory.Exists(platformDir))
                {
                    Directory.CreateDirectory(platformDir);
                }

                var libName = (Options.Compilation.Platform == TargetPlatform.Windows) ?
                              $"{name}.dll" : $"lib{name}.dylib";
                var libFile = Path.Combine(Options.OutputDir, libName);
                if (File.Exists(libFile))
                {
                    var outputFile = Path.Combine(platformDir, libName);
                    File.Copy(libFile, outputFile, true);
                }

                //Copy .NET assemblies
                var assembliesDir = Path.Combine(classesDir, "assemblies");
                if (!Directory.Exists(assembliesDir))
                {
                    Directory.CreateDirectory(assembliesDir);
                }

                foreach (var assembly in Project.Assemblies)
                {
                    File.Copy(assembly, Path.Combine(assembliesDir, Path.GetFileName(assembly)), true);
                }

                //Copy any referenced assemblies such as mscorlib.dll
                List <string> referencedAssemblies = new List <string>();
                foreach (var assembly in Assemblies)
                {
                    foreach (var reference in assembly.GetReferencedAssemblies())
                    {
                        if (!referencedAssemblies.Contains(reference.Name))
                        {
                            referencedAssemblies.Add(reference.Name);
                        }
                    }
                }

                foreach (var reference in referencedAssemblies)
                {
                    var referencePath = Path.Combine(MonoSdkPath, "lib", "mono", "4.5", reference + ".dll");
                    if (File.Exists(referencePath))
                    {
                        File.Copy(referencePath, Path.Combine(assembliesDir, reference + ".dll"), true);
                    }
                }

                // Copy the Mono runtime shared library to the JAR file
                var libDir = (Options.Compilation.Platform == TargetPlatform.Windows) ?
                             "bin" : "lib";

                switch (Options.Compilation.Platform)
                {
                case TargetPlatform.Windows:
                    libName = "mono-2.0-sgen.dll";
                    break;

                case TargetPlatform.MacOS:
                    libName = "libmonosgen-2.0.dylib";
                    break;

                case TargetPlatform.Linux:
                    libName = "libmonosgen-2.0.so";
                    break;

                default:
                    throw new NotImplementedException();
                }

                var monoLib = Path.Combine(MonoSdkPath, libDir, libName);

                if (!File.Exists(monoLib))
                {
                    throw new Exception($"Cannot find Mono runtime shared library: {monoLib}");
                }

                File.Copy(monoLib, Path.Combine(platformDir, libName), true);
            }

            //Embed JNA into our jar file
            //  If on Android, we do not need any native libraries
            var jnaJar = Path.Combine(FindDirectory("external"), "jna", "jna-4.4.0.jar");
            var filter = Options.Compilation.Platform == TargetPlatform.Android ?
                         entry => entry.Name.EndsWith(".class", StringComparison.Ordinal) :
                         default(Func <ZipArchiveEntry, bool>);

            XamarinAndroidBuild.ExtractJar(jnaJar, classesDir, filter);

            //Embed mono.android.jar into our jar file
            if (Options.Compilation.Platform == TargetPlatform.Android)
            {
                var monoAndroidJar = XamarinAndroid.FindAssembly("mono.android.jar");
                XamarinAndroidBuild.ExtractJar(monoAndroidJar, classesDir);

                //Look for other JAR file dependencies from the user's assemblies
                foreach (var assembly in Assemblies)
                {
                    var intermediateDir = Path.Combine(Options.OutputDir, XamarinAndroidBuild.IntermediateDir, Path.GetFileNameWithoutExtension(assembly.Location));
                    if (Directory.Exists(intermediateDir))
                    {
                        foreach (var dependency in Directory.GetFiles(intermediateDir, "*.jar", SearchOption.AllDirectories))
                        {
                            XamarinAndroidBuild.ExtractJar(dependency, classesDir);
                        }
                    }
                }
            }

            var invocation = string.Join(" ", args);
            var output     = Invoke(jar, invocation);

            return(output.ExitCode == 0);
        }