From 6f9f6f54e1a259a67fd4a2cee434d27b7c58fb8d Mon Sep 17 00:00:00 2001 From: Gaurav Khanna Date: Fri, 4 Dec 2015 17:41:08 -0800 Subject: [PATCH] Add Mac RyuJIT support --- .../IntermediateCompiler.cs | 2 +- .../Linux/LinuxRyuJitCompileStep.cs | 12 -- .../Mac/MacLinkStep.cs | 31 ---- .../Mac/MacRyuJitCompileStep.cs | 132 ++++++++++++++++++ 4 files changed, 133 insertions(+), 44 deletions(-) delete mode 100644 src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/Mac/MacLinkStep.cs create mode 100644 src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/Mac/MacRyuJitCompileStep.cs diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/IntermediateCompiler.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/IntermediateCompiler.cs index fdbe3515d..0c0c9efef 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/IntermediateCompiler.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/IntermediateCompiler.cs @@ -74,7 +74,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native } else if (config.OS == OSMode.Mac) { - throw new NotImplementedException("Mac RyuJit not supported"); + stepList.Add(new MacRyuJitCompileStep(config)); } else { diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/Linux/LinuxRyuJitCompileStep.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/Linux/LinuxRyuJitCompileStep.cs index 96a51c0e9..73253aea0 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/Linux/LinuxRyuJitCompileStep.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/Linux/LinuxRyuJitCompileStep.cs @@ -102,18 +102,6 @@ namespace Microsoft.DotNet.Tools.Compiler.Native .ForwardStdOut() .Execute(); - // Needs System.Native.so in output - var sharedLibPath = Path.Combine(config.IlcPath, "System.Native.so"); - var outputSharedLibPath = Path.Combine(config.OutputDirectory, "System.Native.so"); - try - { - File.Copy(sharedLibPath, outputSharedLibPath); - } - catch(Exception e) - { - Reporter.Error.WriteLine("Unable to copy System.Native.so to output"); - } - return result.ExitCode; } diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/Mac/MacLinkStep.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/Mac/MacLinkStep.cs deleted file mode 100644 index e58838428..000000000 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/Mac/MacLinkStep.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; - -using Microsoft.Dnx.Runtime.Common.CommandLine; -using Microsoft.DotNet.Cli.Utils; -using Microsoft.DotNet.Tools.Common; - -namespace Microsoft.DotNet.Tools.Compiler.Native -{ - public class MacLinkStep : IPlatformNativeStep - { - public int Invoke() - { - throw new NotImplementedException("Mac linker Not supported yet."); - } - - public bool CheckPreReqs() - { - throw new NotImplementedException("Mac linker Not supported yet."); - } - - public string DetermineOutputFile(NativeCompileSettings config) - { - throw new NotImplementedException("Mac linker Not supported yet."); - } - - } -} \ No newline at end of file diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/Mac/MacRyuJitCompileStep.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/Mac/MacRyuJitCompileStep.cs new file mode 100644 index 000000000..6b70ac06f --- /dev/null +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/IntermediateCompilation/Mac/MacRyuJitCompileStep.cs @@ -0,0 +1,132 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; + +using Microsoft.Dnx.Runtime.Common.CommandLine; +using Microsoft.DotNet.Cli.Utils; +using Microsoft.DotNet.Tools.Common; + +namespace Microsoft.DotNet.Tools.Compiler.Native +{ + public class MacRyuJitCompileStep : IPlatformNativeStep + { + private readonly string CompilerName = "clang"; + private readonly string InputExtension = ".obj"; + + private readonly string CompilerOutputExtension = ""; + + // TODO: debug/release support + private readonly string cflags = "-g -lstdc++ -Wno-invalid-offsetof -pthread -ldl -lm -liconv"; + + private readonly string[] libs = new string[] + { + "libbootstrapper.a", + "libRuntime.a", + "libSystem.Private.CoreLib.Native.a" + }; + + private readonly string[] appdeplibs = new string[] + { + "libSystem.Native.a" + }; + + + private string CompilerArgStr { get; set; } + private NativeCompileSettings config; + + public MacRyuJitCompileStep(NativeCompileSettings config) + { + this.config = config; + InitializeArgs(config); + } + + public int Invoke() + { + var result = InvokeCompiler(); + if (result != 0) + { + Reporter.Error.WriteLine("Compilation of intermediate files failed."); + } + + return result; + } + + public bool CheckPreReqs() + { + // TODO check for clang + return true; + } + + private void InitializeArgs(NativeCompileSettings config) + { + var argsList = new List(); + + // Flags + argsList.Add(cflags); + + // Add Stubs + argsList.Add("-I "+Path.Combine(config.AppDepSDKPath, "CPPSdk/osx.10.10")); + argsList.Add("-I "+Path.Combine(config.AppDepSDKPath, "CPPSdk")); + argsList.Add(Path.Combine(config.AppDepSDKPath, "CPPSdk/osx.10.10/osxstubs.cpp")); + + // Input File + var inLibFile = DetermineInFile(config); + argsList.Add("-Xlinker "+inLibFile); + + // Libs + foreach (var lib in libs) + { + var libPath = Path.Combine(config.IlcPath, lib); + argsList.Add("-Xlinker "+libPath); + } + + // AppDep Libs + var baseAppDepLibPath = Path.Combine(config.AppDepSDKPath, "CPPSdk/osx.10.10", config.Architecture.ToString()); + foreach (var lib in appdeplibs) + { + var appDepLibPath = Path.Combine(baseAppDepLibPath, lib); + argsList.Add("-Xlinker "+appDepLibPath); + } + + // Output + var libOut = DetermineOutputFile(config); + argsList.Add($"-o \"{libOut}\""); + + this.CompilerArgStr = string.Join(" ", argsList); + } + + private int InvokeCompiler() + { + var result = Command.Create(CompilerName, CompilerArgStr) + .ForwardStdErr() + .ForwardStdOut() + .Execute(); + + return result.ExitCode; + } + + private string DetermineInFile(NativeCompileSettings config) + { + var intermediateDirectory = config.IntermediateDirectory; + + var filename = Path.GetFileNameWithoutExtension(config.InputManagedAssemblyPath); + + var infile = Path.Combine(intermediateDirectory, filename + InputExtension); + + return infile; + } + + public string DetermineOutputFile(NativeCompileSettings config) + { + var intermediateDirectory = config.OutputDirectory; + + var filename = Path.GetFileNameWithoutExtension(config.InputManagedAssemblyPath); + + var outfile = Path.Combine(intermediateDirectory, filename + CompilerOutputExtension); + + return outfile; + } + } +}