From ef00f0331e3794817e1a17e76b70a9d463e9b198 Mon Sep 17 00:00:00 2001 From: Senthil Date: Wed, 3 Feb 2016 16:34:03 -0800 Subject: [PATCH] Include project.json dependencies for native --- scripts/dotnet-cli-build/CompileTargets.cs | 2 +- .../commands/dotnet-build/CompileContext.cs | 6 +-- .../dotnet-compile-native/ArgValues.cs | 4 +- .../dotnet-compile-native/ArgumentsParser.cs | 21 +++++++-- .../ILCompilerInvoker.cs | 46 ++++++++++--------- .../Linux/LinuxCppCompileStep.cs | 4 +- .../Linux/LinuxRyuJitCompileStep.cs | 2 +- .../Mac/MacCppCompileStep.cs | 2 +- .../Mac/MacRyuJitCompileStep.cs | 2 +- .../NativeCompileSettings.cs | 22 ++++++--- .../dotnet-compile-native/appdep/project.json | 2 +- .../dotnet-compile/CompilerCommandApp.cs | 8 ++-- .../commands/dotnet-compile/NativeCompiler.cs | 30 ++++++++++-- src/dotnet/project.json | 2 +- test/EndToEnd/EndToEndTest.cs | 1 - 15 files changed, 98 insertions(+), 56 deletions(-) diff --git a/scripts/dotnet-cli-build/CompileTargets.cs b/scripts/dotnet-cli-build/CompileTargets.cs index c3628372b..2c40bca7f 100644 --- a/scripts/dotnet-cli-build/CompileTargets.cs +++ b/scripts/dotnet-cli-build/CompileTargets.cs @@ -13,7 +13,7 @@ namespace Microsoft.DotNet.Cli.Build public class CompileTargets { public static readonly string CoreCLRVersion = "1.0.1-rc2-23811"; - public static readonly string AppDepSdkVersion = "1.0.5-prerelease-00001"; + public static readonly string AppDepSdkVersion = "1.0.6-prerelease-00001"; public static readonly List AssembliesToCrossGen = GetAssembliesToCrossGen(); diff --git a/src/dotnet/commands/dotnet-build/CompileContext.cs b/src/dotnet/commands/dotnet-build/CompileContext.cs index 55352ce0a..1d29664d8 100644 --- a/src/dotnet/commands/dotnet-build/CompileContext.cs +++ b/src/dotnet/commands/dotnet-build/CompileContext.cs @@ -339,10 +339,10 @@ namespace Microsoft.DotNet.Tools.Build args.Add(_args.ArchValue); } - if (!string.IsNullOrWhiteSpace(_args.IlcArgsValue)) + foreach (var ilcArg in _args.IlcArgsValue) { - args.Add("--ilcargs"); - args.Add(_args.IlcArgsValue); + args.Add("--ilcarg"); + args.Add(ilcArg); } if (!string.IsNullOrWhiteSpace(_args.IlcPathValue)) diff --git a/src/dotnet/commands/dotnet-compile-native/ArgValues.cs b/src/dotnet/commands/dotnet-compile-native/ArgValues.cs index c05ccd7ee..625ec65e9 100644 --- a/src/dotnet/commands/dotnet-compile-native/ArgValues.cs +++ b/src/dotnet/commands/dotnet-compile-native/ArgValues.cs @@ -13,7 +13,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native public ArchitectureMode Architecture { get; set; } public NativeIntermediateMode? NativeMode { get; set; } public IEnumerable ReferencePaths { get; set; } - public string IlcArgs { get; set; } + public IEnumerable IlcArgs { get; set; } public IEnumerable LinkLibPaths { get; set; } public string AppDepSDKPath { get; set; } public string IlcPath { get; set; } @@ -88,7 +88,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native config.LogPath = LogPath; } - if (!string.IsNullOrEmpty(IlcArgs)) + if (IlcArgs != null) { config.IlcArgs = IlcArgs; } diff --git a/src/dotnet/commands/dotnet-compile-native/ArgumentsParser.cs b/src/dotnet/commands/dotnet-compile-native/ArgumentsParser.cs index e471b0137..885cf0cd0 100644 --- a/src/dotnet/commands/dotnet-compile-native/ArgumentsParser.cs +++ b/src/dotnet/commands/dotnet-compile-native/ArgumentsParser.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.CommandLine; +using System.Linq; namespace Microsoft.DotNet.Tools.Compiler.Native { @@ -15,7 +16,8 @@ namespace Microsoft.DotNet.Tools.Compiler.Native BuildConfiguration? buildConfiguration = null; string mode = null; NativeIntermediateMode? nativeMode = null; - string ilcArgs = null; + IReadOnlyList ilcArgs = Array.Empty(); + IEnumerable unquotIlcArgs = Array.Empty(); string ilcPath = null; string ilcSdkPath = null; string appDepSdk = null; @@ -26,7 +28,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native string cppCompilerFlags = null; IReadOnlyList references = Array.Empty(); - IReadOnlyList linklib = Array.Empty(); + IReadOnlyList linklib = Array.Empty(); try { @@ -46,7 +48,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native "Use to specify Managed DLL references of the app."); // Custom Extensibility Points to support CoreRT workflow TODO better descriptions - syntax.DefineOption("ilcargs", ref ilcArgs, "Use to specify custom arguments for the IL Compiler."); + syntax.DefineOptionList("ilcarg", ref ilcArgs, "Use to specify custom arguments for the IL Compiler."); syntax.DefineOption("ilcpath", ref ilcPath, "Use to specify a custom build of IL Compiler."); syntax.DefineOption("ilcsdkpath", ref ilcSdkPath, "Use to specify a custom build of IL Compiler SDK"); @@ -67,7 +69,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native "The managed input assembly to compile to native."); helpText = syntax.GetHelpText(); - + if (string.IsNullOrWhiteSpace(inputAssembly)) { syntax.ReportError("Input Assembly is a required parameter."); @@ -99,6 +101,15 @@ namespace Microsoft.DotNet.Tools.Compiler.Native help = true; } } + + unquotIlcArgs = ilcArgs.Select(s => + { + if (!s.StartsWith("\"") || !s.EndsWith("\"")) + { + throw new ArgumentSyntaxException("--ilcarg must be specified in double quotes"); + } + return s.Substring(1, s.Length - 2); + }); }); } catch (ArgumentSyntaxException exception) @@ -130,7 +141,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native BuildConfiguration = buildConfiguration, NativeMode = nativeMode, ReferencePaths = references, - IlcArgs = ilcArgs, + IlcArgs = unquotIlcArgs, IlcPath = ilcPath, IlcSdkPath = ilcSdkPath, LinkLibPaths = linklib, diff --git a/src/dotnet/commands/dotnet-compile-native/ILCompilerInvoker.cs b/src/dotnet/commands/dotnet-compile-native/ILCompilerInvoker.cs index c81281539..b4183c321 100644 --- a/src/dotnet/commands/dotnet-compile-native/ILCompilerInvoker.cs +++ b/src/dotnet/commands/dotnet-compile-native/ILCompilerInvoker.cs @@ -1,13 +1,15 @@ using System.Collections.Generic; using System.IO; +using System.Text; using Microsoft.DotNet.Cli.Utils; namespace Microsoft.DotNet.Tools.Compiler.Native { public class ILCompilerInvoker { - private readonly string ExecutableName = "corerun" + Constants.ExeSuffix; - private readonly string ILCompiler = "ilc.exe"; + private static readonly string HostExeName = "corerun" + Constants.ExeSuffix; + private static readonly string ILCompiler = "ilc.exe"; + private IEnumerable Args; private NativeCompileSettings config; @@ -27,14 +29,6 @@ namespace Microsoft.DotNet.Tools.Compiler.Native { var argsList = new List(); - var managedPath = Path.Combine(config.IlcPath, ILCompiler); - if (!File.Exists(managedPath)) - { - throw new FileNotFoundException("Unable to find ILCompiler at " + managedPath); - } - - argsList.Add($"{managedPath}"); - // Input File var inputFilePath = config.InputManagedAssemblyPath; argsList.Add($"{inputFilePath}"); @@ -43,32 +37,29 @@ namespace Microsoft.DotNet.Tools.Compiler.Native var coreLibsPath = Path.Combine(config.IlcSdkPath, "sdk"); foreach (var reference in Directory.EnumerateFiles(coreLibsPath, "*.dll")) { - argsList.Add($"-r"); - argsList.Add($"{reference}"); + argsList.Add($"-r:{reference}"); } // AppDep References foreach (var reference in config.ReferencePaths) { - argsList.Add($"-r"); - argsList.Add($"{reference}"); + argsList.Add($"-r:{reference}"); } // Set Output DetermineOutFile var outFile = DetermineOutputFile(config); - argsList.Add($"-out"); - argsList.Add($"{outFile}"); + argsList.Add($"-o:{outFile}"); // Add Mode Flag TODO if (config.NativeMode == NativeIntermediateMode.cpp) { - argsList.Add("-cpp"); + argsList.Add("--cpp"); } // Custom Ilc Args support - if (! string.IsNullOrEmpty(config.IlcArgs)) + foreach (var ilcArg in config.IlcArgs) { - argsList.Add(config.IlcArgs); + argsList.Add(ilcArg); } Args = argsList; @@ -76,9 +67,20 @@ namespace Microsoft.DotNet.Tools.Compiler.Native public int Invoke() { - var executablePath = Path.Combine(config.IlcPath, ExecutableName); - - var result = Command.Create(executablePath, Args) + // Check if ILCompiler is present + var ilcExePath = Path.Combine(config.IlcPath, ILCompiler); + if (!File.Exists(ilcExePath)) + { + throw new FileNotFoundException("Unable to find ILCompiler at " + ilcExePath); + } + + // Write the response file + var intermediateDirectory = config.IntermediateDirectory; + var rsp = Path.Combine(intermediateDirectory, "dotnet-compile-native-ilc.rsp"); + File.WriteAllLines(rsp, Args, Encoding.UTF8); + + var hostPath = Path.Combine(config.IlcPath, HostExeName); + var result = Command.Create(hostPath, new string[] { ilcExePath, "@" + $"{rsp}" }) .ForwardStdErr() .ForwardStdOut() .Execute(); diff --git a/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Linux/LinuxCppCompileStep.cs b/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Linux/LinuxCppCompileStep.cs index e92379ad3..28e543de4 100644 --- a/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Linux/LinuxCppCompileStep.cs +++ b/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Linux/LinuxCppCompileStep.cs @@ -25,7 +25,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native private readonly string[] _appdeplibs = { - "libSystem.Native.a" + "System.Native.a" }; public LinuxCppCompileStep(NativeCompileSettings config) @@ -121,4 +121,4 @@ namespace Microsoft.DotNet.Tools.Compiler.Native return outfile; } } -} \ No newline at end of file +} diff --git a/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Linux/LinuxRyuJitCompileStep.cs b/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Linux/LinuxRyuJitCompileStep.cs index 9140a4f2f..65602dd04 100644 --- a/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Linux/LinuxRyuJitCompileStep.cs +++ b/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Linux/LinuxRyuJitCompileStep.cs @@ -24,7 +24,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native private readonly string[] _appdeplibs = { - "libSystem.Native.a" + "System.Native.a" }; public LinuxRyuJitCompileStep(NativeCompileSettings config) diff --git a/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Mac/MacCppCompileStep.cs b/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Mac/MacCppCompileStep.cs index b2f49b408..64fec18ef 100644 --- a/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Mac/MacCppCompileStep.cs +++ b/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Mac/MacCppCompileStep.cs @@ -30,7 +30,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native private readonly string[] _appdeplibs = { - "libSystem.Native.a" + "System.Native.a" }; diff --git a/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Mac/MacRyuJitCompileStep.cs b/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Mac/MacRyuJitCompileStep.cs index 1bd94abb1..6a7ecb13c 100644 --- a/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Mac/MacRyuJitCompileStep.cs +++ b/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Mac/MacRyuJitCompileStep.cs @@ -26,7 +26,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native private readonly string[] appdeplibs = { - "libSystem.Native.a" + "System.Native.a" }; public MacRyuJitCompileStep(NativeCompileSettings config) diff --git a/src/dotnet/commands/dotnet-compile-native/NativeCompileSettings.cs b/src/dotnet/commands/dotnet-compile-native/NativeCompileSettings.cs index 16ac95456..ee260ab23 100644 --- a/src/dotnet/commands/dotnet-compile-native/NativeCompileSettings.cs +++ b/src/dotnet/commands/dotnet-compile-native/NativeCompileSettings.cs @@ -19,8 +19,8 @@ namespace Microsoft.DotNet.Tools.Compiler.Native private string _outputDirectory; private string _intermediateDirectory; private string _logPath; - private string _ilcArgs; - private readonly List _referencePaths; + private IEnumerable _ilcArgs; + private readonly Dictionary _referencePaths; private readonly List _linkLibPaths; private string _cppCompilerFlags; @@ -81,12 +81,12 @@ namespace Microsoft.DotNet.Tools.Compiler.Native { get { - return _referencePaths; + return _referencePaths.Values; } } // Optional Customization Points (Can be null) - public string IlcArgs + public IEnumerable IlcArgs { get { return _ilcArgs; } set { _ilcArgs = value; } @@ -168,8 +168,11 @@ namespace Microsoft.DotNet.Tools.Compiler.Native BuildType = DefaultBuiltType; NativeMode = DefaultNativeModel; AppDepSDKPath = Path.Combine(AppContext.BaseDirectory, "appdepsdk"); - - _referencePaths = new List(Directory.EnumerateFiles(AppDepSDKPath, "*.dll")); + _referencePaths = new Dictionary(); + foreach (var file in Directory.EnumerateFiles(AppDepSDKPath, "*.dll")) + { + _referencePaths.Add(Path.GetFileName(file), file); + } } public static NativeCompileSettings Default @@ -198,7 +201,12 @@ namespace Microsoft.DotNet.Tools.Compiler.Native public void AddReference(string reference) { - _referencePaths.Add(Path.GetFullPath(reference)); + var path = Path.GetFullPath(reference); + var simpleName = Path.GetFileName(path); + if (!_referencePaths.ContainsKey(simpleName)) + { + _referencePaths.Add(simpleName, path); + } } public void AddLinkLibPath(string linkLibPath) diff --git a/src/dotnet/commands/dotnet-compile-native/appdep/project.json b/src/dotnet/commands/dotnet-compile-native/appdep/project.json index 48ef2c271..09ab884be 100644 --- a/src/dotnet/commands/dotnet-compile-native/appdep/project.json +++ b/src/dotnet/commands/dotnet-compile-native/appdep/project.json @@ -5,7 +5,7 @@ }, "dependencies": { "NETStandard.Library": "1.0.0-rc2-23811", - "Microsoft.DotNet.AppDep":"1.0.5-prerelease-00001" + "Microsoft.DotNet.AppDep":"1.0.6-prerelease-00001" }, "frameworks": { "dnxcore50": { } diff --git a/src/dotnet/commands/dotnet-compile/CompilerCommandApp.cs b/src/dotnet/commands/dotnet-compile/CompilerCommandApp.cs index 9f65e9881..667002dd9 100644 --- a/src/dotnet/commands/dotnet-compile/CompilerCommandApp.cs +++ b/src/dotnet/commands/dotnet-compile/CompilerCommandApp.cs @@ -45,7 +45,7 @@ namespace Microsoft.DotNet.Tools.Compiler public string ConfigValue { get; set; } public bool IsNativeValue { get; set; } public string ArchValue { get; set; } - public string IlcArgsValue { get; set; } + public IEnumerable IlcArgsValue { get; set; } public string IlcPathValue { get; set; } public string IlcSdkPathValue { get; set; } public bool IsCppModeValue { get; set; } @@ -83,7 +83,7 @@ namespace Microsoft.DotNet.Tools.Compiler // Native Args _nativeOption = _app.Option("-n|--native", "Compiles source to native machine code.", CommandOptionType.NoValue); _archOption = _app.Option("-a|--arch ", "The architecture for which to compile. x64 only currently supported.", CommandOptionType.SingleValue); - _ilcArgsOption = _app.Option("--ilcargs ", "Command line arguments to be passed directly to ILCompiler.", CommandOptionType.SingleValue); + _ilcArgsOption = _app.Option("--ilcarg ", "Command line option to be passed directly to ILCompiler.", CommandOptionType.MultipleValue); _ilcPathOption = _app.Option("--ilcpath ", "Path to the folder containing custom built ILCompiler.", CommandOptionType.SingleValue); _ilcSdkPathOption = _app.Option("--ilcsdkpath ", "Path to the folder containing ILCompiler application dependencies.", CommandOptionType.SingleValue); _appDepSdkPathOption = _app.Option("--appdepsdkpath ", "Path to the folder containing ILCompiler application dependencies.", CommandOptionType.SingleValue); @@ -109,7 +109,7 @@ namespace Microsoft.DotNet.Tools.Compiler IsNativeValue = _nativeOption.HasValue(); ArchValue = _archOption.Value(); - IlcArgsValue = _ilcArgsOption.Value(); + IlcArgsValue = _ilcArgsOption.HasValue() ? _ilcArgsOption.Values : Enumerable.Empty(); IlcPathValue = _ilcPathOption.Value(); IlcSdkPathValue = _ilcSdkPathOption.Value(); AppDepSdkPathValue = _appDepSdkPathOption.Value(); @@ -170,4 +170,4 @@ namespace Microsoft.DotNet.Tools.Compiler return baseClassOptions.TryGetValue(optionTemplate, out option) && option.HasValue(); } } -} \ No newline at end of file +} diff --git a/src/dotnet/commands/dotnet-compile/NativeCompiler.cs b/src/dotnet/commands/dotnet-compile/NativeCompiler.cs index e3a5efbda..cc5c46acf 100644 --- a/src/dotnet/commands/dotnet-compile/NativeCompiler.cs +++ b/src/dotnet/commands/dotnet-compile/NativeCompiler.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; +using System.Linq; using Microsoft.DotNet.ProjectModel; namespace Microsoft.DotNet.Tools.Compiler @@ -22,16 +23,37 @@ namespace Microsoft.DotNet.Tools.Compiler var managedOutput = outputPaths.CompilationFiles.Assembly; + // Create the library exporter + var exporter = context.CreateExporter(args.ConfigValue); + + // Gather exports for the project + var exports = exporter.GetAllExports(); + + // Runtime assemblies. + // TODO: native assets/resources. + var references = exports + .SelectMany(export => export.RuntimeAssemblies) + .Select(r => r.ResolvedPath) + .ToList(); + + // Setup native args. var nativeArgs = new List(); // Input Assembly nativeArgs.Add($"{managedOutput}"); - // ILC Args - if (!string.IsNullOrWhiteSpace(args.IlcArgsValue)) + // Add Resolved Assembly References + foreach (var reference in references) { - nativeArgs.Add("--ilcargs"); - nativeArgs.Add($"{args.IlcArgsValue}"); + nativeArgs.Add("--reference"); + nativeArgs.Add(reference); + } + + // ILC Args + foreach (var ilcArg in args.IlcArgsValue) + { + nativeArgs.Add("--ilcarg"); + nativeArgs.Add($"\"{ilcArg}\""); } // ILC Path diff --git a/src/dotnet/project.json b/src/dotnet/project.json index 6ffe6144f..5d09d249f 100644 --- a/src/dotnet/project.json +++ b/src/dotnet/project.json @@ -30,7 +30,7 @@ "Microsoft.DotNet.ProjectModel": "1.0.0-*", "Microsoft.DotNet.Compiler.Common": "1.0.0-*", "Microsoft.DotNet.Cli.Utils": "1.0.0-*", - "Microsoft.DotNet.ILCompiler.SDK": "1.0.5-prerelease-00002", + "Microsoft.DotNet.ILCompiler.SDK": "1.0.6-prerelease-00003", "Microsoft.Extensions.Logging": "1.0.0-rc2-16040", "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-16040", diff --git a/test/EndToEnd/EndToEndTest.cs b/test/EndToEnd/EndToEndTest.cs index bd93d730c..5dba2ca02 100644 --- a/test/EndToEnd/EndToEndTest.cs +++ b/test/EndToEnd/EndToEndTest.cs @@ -81,7 +81,6 @@ namespace Microsoft.DotNet.Tests.EndToEnd } [Fact] - [ActiveIssue(712, PlatformID.Windows | PlatformID.OSX | PlatformID.Linux)] public void TestDotnetBuildNativeRyuJit() { if(IsCentOS())