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"));
            parameters.ReferencedAssemblies.Add(MainAssembly);

            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);
        }
Example #2
0
        static void ResolveAssemblies(ProjectTargetElement target, string mainAssembly)
        {
            var resolveAssemblies = target.AddTask("ResolveAssemblies");

            //NOTE: [Export] requires Mono.Android.Export.dll
            resolveAssemblies.SetParameter("Assemblies", mainAssembly + ";" + XamarinAndroid.FindAssembly("Mono.Android.Export.dll"));
            resolveAssemblies.SetParameter("LinkMode", LinkMode);
            resolveAssemblies.SetParameter("ReferenceAssembliesDirectory", "$(TargetFrameworkDirectory)");
            resolveAssemblies.AddOutputItem("ResolvedAssemblies", "ResolvedAssemblies");
            resolveAssemblies.AddOutputItem("ResolvedUserAssemblies", "ResolvedUserAssemblies");
            resolveAssemblies.AddOutputItem("ResolvedFrameworkAssemblies", "ResolvedFrameworkAssemblies");
        }
Example #3
0
        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");
        }
        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.MacOS)
            {
                //Copy native libs
                var platformDir = Path.Combine(classesDir, "darwin");
                if (!Directory.Exists(platformDir))
                {
                    Directory.CreateDirectory(platformDir);
                }

                var libName   = $"lib{name}.dylib";
                var outputDir = Path.Combine(Options.OutputDir, libName);
                File.Copy(outputDir, Path.Combine(platformDir, libName), 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);
                        }
                    }
                }

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

            //Embed JNA into our jar file
            using (var stream = File.OpenRead(Path.Combine(FindDirectory("external"), "jna", "jna-4.4.0.jar")))
                using (var zip = new ZipArchive(stream))
                {
                    foreach (var entry in zip.Entries)
                    {
                        //Skip META-INF
                        if (entry.FullName.StartsWith("META-INF", StringComparison.Ordinal))
                        {
                            continue;
                        }

                        var entryPath = Path.Combine(classesDir, entry.FullName);

                        if (string.IsNullOrEmpty(entry.Name))
                        {
                            if (!Directory.Exists(entryPath))
                            {
                                Directory.CreateDirectory(entryPath);
                            }
                        }
                        else
                        {
                            //NOTE: *.so files on Android will be packaged in a different way
                            if (Options.Compilation.Platform == TargetPlatform.Android && entry.Name.EndsWith(".so", StringComparison.Ordinal))
                            {
                                continue;
                            }

                            using (var zipEntryStream = entry.Open())
                                using (var fileStream = File.Create(entryPath))
                                {
                                    zipEntryStream.CopyTo(fileStream);
                                }
                        }
                    }
                }

            //Embed mono.android.jar into our jar file
            if (Options.Compilation.Platform == TargetPlatform.Android)
            {
                using (var stream = File.OpenRead(XamarinAndroid.FindAssembly("mono.android.jar")))
                    using (var zip = new ZipArchive(stream))
                    {
                        foreach (var entry in zip.Entries)
                        {
                            //Skip META-INF
                            if (entry.FullName.StartsWith("META-INF", StringComparison.Ordinal))
                            {
                                continue;
                            }

                            var entryPath = Path.Combine(classesDir, entry.FullName);

                            if (string.IsNullOrEmpty(entry.Name))
                            {
                                if (!Directory.Exists(entryPath))
                                {
                                    Directory.CreateDirectory(entryPath);
                                }
                            }
                            else
                            {
                                using (var zipEntryStream = entry.Open())
                                    using (var fileStream = File.Create(entryPath))
                                    {
                                        zipEntryStream.CopyTo(fileStream);
                                    }
                            }
                        }
                    }
            }

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

            return(output.ExitCode == 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),
                $"-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);
                    }
                }

                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);
        }