Beispiel #1
0
        public static int CompileDirectory(DirectoryInfo toolDirectory, DirectoryInfo inputDirectory, DirectoryInfo outputDirectory, bool crossgen, bool cpaot, DirectoryInfo[] referencePath)
        {
            if (toolDirectory == null)
            {
                Console.WriteLine("--tool-directory is a required argument.");
                return(1);
            }

            if (inputDirectory == null)
            {
                Console.WriteLine("--input-directory is a required argument.");
                return(1);
            }

            if (outputDirectory == null)
            {
                Console.WriteLine("--output-directory is a required argument.");
                return(1);
            }

            CompilerRunner runner;

            if (cpaot)
            {
                runner = new CpaotRunner(toolDirectory.ToString(), inputDirectory.ToString(), outputDirectory.ToString(), referencePath?.Select(x => x.ToString())?.ToList());
            }
            else
            {
                runner = new CrossgenRunner(toolDirectory.ToString(), inputDirectory.ToString(), outputDirectory.ToString(), referencePath?.Select(x => x.ToString())?.ToList());
            }

            bool success = true;

            foreach (var assemblyToOptimize in ComputeManagedAssemblies.GetManagedAssembliesInFolder(inputDirectory.ToString()))
            {
                // Compile all assemblies in the input folder
                if (!runner.CompileAssembly(assemblyToOptimize))
                {
                    success = false;
                }
            }

            return(success ? 0 : 1);
        }
Beispiel #2
0
        public static int CompileDirectory(DirectoryInfo toolDirectory, DirectoryInfo inputDirectory, DirectoryInfo outputDirectory, bool crossgen, bool cpaot, DirectoryInfo[] referencePath)
        {
            if (toolDirectory == null)
            {
                Console.WriteLine("--tool-directory is a required argument.");
                return(1);
            }

            if (inputDirectory == null)
            {
                Console.WriteLine("--input-directory is a required argument.");
                return(1);
            }

            if (outputDirectory == null)
            {
                Console.WriteLine("--output-directory is a required argument.");
                return(1);
            }

            if (OutputPathIsParentOfInputPath(inputDirectory, outputDirectory))
            {
                Console.WriteLine("Error: Input and output folders must be distinct, and the output directory (which gets deleted) better not be a parent of the input directory.");
                return(1);
            }

            CompilerRunner runner;

            if (cpaot)
            {
                runner = new CpaotRunner(toolDirectory.ToString(), inputDirectory.ToString(), outputDirectory.ToString(), referencePath?.Select(x => x.ToString())?.ToList());
            }
            else
            {
                runner = new CrossgenRunner(toolDirectory.ToString(), inputDirectory.ToString(), outputDirectory.ToString(), referencePath?.Select(x => x.ToString())?.ToList());
            }

            if (outputDirectory.Exists)
            {
                try
                {
                    outputDirectory.Delete(recursive: true);
                }
                catch (Exception ex) when(
                    ex is UnauthorizedAccessException ||
                    ex is DirectoryNotFoundException ||
                    ex is IOException
                    )
                {
                    Console.WriteLine($"Error: Could not delete output folder {outputDirectory.FullName}. {ex.Message}");
                    return(1);
                }
            }

            outputDirectory.Create();

            bool success = true;

            // Copy unmanaged files (runtime, native dependencies, resources, etc)
            foreach (string file in Directory.EnumerateFiles(inputDirectory.FullName))
            {
                if (ComputeManagedAssemblies.IsManaged(file))
                {
                    // Compile managed code
                    if (!runner.CompileAssembly(file))
                    {
                        success = false;

                        // On compile failure, pass through the input IL assembly so the output is still usable
                        File.Copy(file, Path.Combine(outputDirectory.FullName, Path.GetFileName(file)));
                    }
                }
                else
                {
                    // Copy through all other files
                    File.Copy(file, Path.Combine(outputDirectory.FullName, Path.GetFileName(file)));
                }
            }

            return(success ? 0 : 1);
        }
Beispiel #3
0
        public static int CompileDirectory(DirectoryInfo toolDirectory, DirectoryInfo inputDirectory, DirectoryInfo outputDirectory, bool crossgen, bool cpaot, DirectoryInfo[] referencePath)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            if (toolDirectory == null)
            {
                Console.WriteLine("--tool-directory is a required argument.");
                return(1);
            }

            if (inputDirectory == null)
            {
                Console.WriteLine("--input-directory is a required argument.");
                return(1);
            }

            if (outputDirectory == null)
            {
                outputDirectory = inputDirectory;
            }

            if (OutputPathIsParentOfInputPath(inputDirectory, outputDirectory))
            {
                Console.WriteLine("Error: Input and output folders must be distinct, and the output directory (which gets deleted) better not be a parent of the input directory.");
                return(1);
            }

            CompilerRunner runner;

            if (cpaot)
            {
                runner = new CpaotRunner(toolDirectory.ToString(), inputDirectory.ToString(), outputDirectory.ToString(), referencePath?.Select(x => x.ToString())?.ToList());
            }
            else
            {
                runner = new CrossgenRunner(toolDirectory.ToString(), inputDirectory.ToString(), outputDirectory.ToString(), referencePath?.Select(x => x.ToString())?.ToList());
            }

            string runnerOutputPath = runner.GetOutputPath();

            if (Directory.Exists(runnerOutputPath))
            {
                try
                {
                    Directory.Delete(runnerOutputPath, recursive: true);
                }
                catch (Exception ex) when(
                    ex is UnauthorizedAccessException ||
                    ex is DirectoryNotFoundException ||
                    ex is IOException
                    )
                {
                    Console.WriteLine($"Error: Could not delete output folder {runnerOutputPath}. {ex.Message}");
                    return(1);
                }
            }

            Directory.CreateDirectory(runnerOutputPath);

            List <ProcessInfo> compilationsToRun = new List <ProcessInfo>();

            // Copy unmanaged files (runtime, native dependencies, resources, etc)
            foreach (string file in Directory.EnumerateFiles(inputDirectory.FullName))
            {
                if (ComputeManagedAssemblies.IsManaged(file))
                {
                    ProcessInfo compilationToRun = runner.CompilationProcess(file);
                    compilationToRun.InputFileName = file;
                    compilationsToRun.Add(compilationToRun);
                }
                else
                {
                    // Copy through all other files
                    File.Copy(file, Path.Combine(runnerOutputPath, Path.GetFileName(file)));
                }
            }

            ParallelRunner.Run(compilationsToRun);

            bool          success = true;
            List <string> failedCompilationAssemblies = new List <string>();
            int           successfulCompileCount      = 0;

            foreach (ProcessInfo processInfo in compilationsToRun)
            {
                if (processInfo.Succeeded)
                {
                    successfulCompileCount++;
                }
                else
                {
                    File.Copy(processInfo.InputFileName, Path.Combine(runnerOutputPath, Path.GetFileName(processInfo.InputFileName)));
                    failedCompilationAssemblies.Add(processInfo.InputFileName);
                }
            }

            Console.WriteLine($"Compiled {successfulCompileCount}/{successfulCompileCount + failedCompilationAssemblies.Count} assemblies in {stopwatch.ElapsedMilliseconds} msecs.");

            if (failedCompilationAssemblies.Count > 0)
            {
                Console.WriteLine($"Failed to compile {failedCompilationAssemblies.Count} assemblies:");
                foreach (var assembly in failedCompilationAssemblies)
                {
                    Console.WriteLine(assembly);
                }
            }

            return(success ? 0 : 1);
        }