Remove native compilation, add multiple project files and globbing

This commit is contained in:
Pavel Krymets 2016-04-19 19:14:59 -07:00
parent 3e5b68dc43
commit 3472aee5c9
28 changed files with 493 additions and 490 deletions

View file

@ -14,8 +14,6 @@ namespace Microsoft.DotNet.ProjectModel.Compilation
private IList<LibraryAsset> _compilationAssemblies;
private IList<LibraryAsset> _compilationAssets;
private IList<LibraryAsset> _sourceReferences;
private IList<LibraryAssetGroup> _nativeLibraryGroups;
@ -34,8 +32,6 @@ namespace Microsoft.DotNet.ProjectModel.Compilation
public IEnumerable<LibraryAsset> CompilationAssemblies => _compilationAssemblies;
public IEnumerable<LibraryAsset> CompilationAssets => _compilationAssets;
public IEnumerable<LibraryAsset> SourceReferences => _sourceReferences;
public IEnumerable<LibraryAssetGroup> NativeLibraryGroups => _nativeLibraryGroups;

View file

@ -489,6 +489,11 @@ namespace Microsoft.DotNet.ProjectModel
if (Project == null && ProjectDirectory != null)
{
Project = ProjectResolver(ProjectDirectory);
if (Project == null)
{
throw new InvalidOperationException($"Could not resolve project at: {ProjectDirectory}. " +
$"This could happen when project.lock.json was moved after restore.");
}
}
}

View file

@ -1,3 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Extensions.Internal;
using NuGet.Frameworks;

View file

@ -0,0 +1,65 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Extensions.FileSystemGlobbing;
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
using Microsoft.DotNet.ProjectModel;
namespace Microsoft.DotNet.Tools.Compiler
{
internal class ProjectGlobbingResolver
{
internal IEnumerable<string> Resolve(IEnumerable<string> values)
{
var currentDirectory = Directory.GetCurrentDirectory();
if (!values.Any())
{
var fileName = Path.Combine(currentDirectory, Project.FileName);
if (!File.Exists(fileName))
{
throw new InvalidOperationException($"Couldn't find '{Project.FileName}' in current directory");
}
yield return fileName;
yield break;
}
foreach (var value in values)
{
var fileName = Path.Combine(currentDirectory, value);
if (File.Exists(fileName))
{
yield return value;
continue;
}
fileName = Path.Combine(currentDirectory, value, Project.FileName);
if (File.Exists(fileName))
{
yield return fileName;
continue;
}
var matcher = new Matcher();
matcher.AddInclude(value);
var result = matcher.Execute(new DirectoryInfoWrapper(new DirectoryInfo(currentDirectory)));
if (result.Files.Any())
{
foreach (var filePatternMatch in result.Files)
{
yield return filePatternMatch.Path;
}
}
else
{
throw new InvalidOperationException($"Could not resolve project path from '{value}':" +
"1. It's not project file" +
"2. It's not directory containing project.json file" +
"3. Globbing returned no mathces");
}
}
}
}
}

View file

@ -0,0 +1,134 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
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.ProjectModel;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
// This class is responsible with defining the arguments for the Compile verb.
// It knows how to interpret them and set default values
namespace Microsoft.DotNet.Tools.Compiler
{
public delegate bool OnExecute(IEnumerable<string> files, IEnumerable<NuGetFramework> frameworks, BuildCommandApp buildCommand);
public class BuildCommandApp
{
public static readonly string NoIncrementalFlag = "--no-incremental";
public static readonly string BuildProfileFlag = "--build-profile";
private readonly CommandLineApplication _app;
// options and arguments for compilation
private CommandOption _outputOption;
private CommandOption _buildBasePath;
private CommandOption _frameworkOption;
private CommandOption _runtimeOption;
private CommandOption _versionSuffixOption;
private CommandOption _configurationOption;
private CommandArgument _projectArgument;
private CommandOption _shouldPrintIncrementalPreconditionsArgument;
private CommandOption _shouldNotUseIncrementalityArgument;
private CommandOption _shouldSkipDependenciesArgument;
public string BuildBasePathValue { get; set; }
public string RuntimeValue { get; set; }
public string OutputValue { get; set; }
public string VersionSuffixValue { get; set; }
public string ConfigValue { get; set; }
public bool IsNativeValue { get; set; }
public bool ShouldPrintIncrementalPreconditions { get; set; }
public bool ShouldNotUseIncrementality { get; set; }
public bool ShouldSkipDependencies { get; set; }
// workaround: CommandLineApplication is internal therefore I cannot make _app protected so baseclasses can add their own params
private readonly Dictionary<string, CommandOption> baseClassOptions;
public BuildCommandApp(string name, string fullName, string description)
{
_app = new CommandLineApplication
{
Name = name,
FullName = fullName,
Description = description
};
baseClassOptions = new Dictionary<string, CommandOption>();
AddCompileParameters();
}
private void AddCompileParameters()
{
_app.HelpOption("-h|--help");
_outputOption = _app.Option("-o|--output <OUTPUT_DIR>", "Directory in which to place outputs", CommandOptionType.SingleValue);
_buildBasePath = _app.Option("-b|--build-base-path <OUTPUT_DIR>", "Directory in which to place temporary outputs", CommandOptionType.SingleValue);
_frameworkOption = _app.Option("-f|--framework <FRAMEWORK>", "Compile a specific framework", CommandOptionType.SingleValue);
_runtimeOption = _app.Option("-r|--runtime <RUNTIME_IDENTIFIER>", "Produce runtime-specific assets for the specified runtime", CommandOptionType.SingleValue);
_configurationOption = _app.Option("-c|--configuration <CONFIGURATION>", "Configuration under which to build", CommandOptionType.SingleValue);
_versionSuffixOption = _app.Option("--version-suffix <VERSION_SUFFIX>", "Defines what `*` should be replaced with in version field in project.json", CommandOptionType.SingleValue);
_projectArgument = _app.Argument("<PROJECT>", "The project to compile, defaults to the current directory. " +
"Can be one or multiple paths to project.json, project directory " +
"or globbing patter that matches project.json files", multipleValues: true);
_shouldPrintIncrementalPreconditionsArgument = _app.Option(BuildProfileFlag, "Set this flag to print the incremental safety checks that prevent incremental compilation", CommandOptionType.NoValue);
_shouldNotUseIncrementalityArgument = _app.Option(NoIncrementalFlag, "Set this flag to turn off incremental build", CommandOptionType.NoValue);
_shouldSkipDependenciesArgument = _app.Option("--no-dependencies", "Set this flag to ignore project to project references and only build the root project", CommandOptionType.NoValue);
}
public int Execute(OnExecute execute, string[] args)
{
_app.OnExecute(() =>
{
if (_outputOption.HasValue() && !_frameworkOption.HasValue())
{
Reporter.Error.WriteLine("When the '--output' option is provided, the '--framework' option must also be provided.");
return 1;
}
OutputValue = _outputOption.Value();
BuildBasePathValue = _buildBasePath.Value();
ConfigValue = _configurationOption.Value() ?? Constants.DefaultConfiguration;
RuntimeValue = _runtimeOption.Value();
VersionSuffixValue = _versionSuffixOption.Value();
ShouldPrintIncrementalPreconditions = _shouldPrintIncrementalPreconditionsArgument.HasValue();
ShouldNotUseIncrementality = _shouldNotUseIncrementalityArgument.HasValue();
ShouldSkipDependencies = _shouldSkipDependenciesArgument.HasValue();
var files = new ProjectGlobbingResolver().Resolve(_projectArgument.Values);
IEnumerable<NuGetFramework> frameworks = null;
if (_frameworkOption.HasValue())
{
frameworks = new [] { NuGetFramework.Parse(_frameworkOption.Value()) };
}
var success = execute(files, frameworks, this);
return success ? 0 : 1;
});
return _app.Execute(args);
}
public IEnumerable<string> GetRuntimes()
{
var rids = new List<string>();
if (string.IsNullOrEmpty(RuntimeValue))
{
return PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers();
}
else
{
return new[] { RuntimeValue };
}
}
}
}

View file

@ -1,25 +0,0 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.Tools.Compiler;
namespace Microsoft.DotNet.Tools.Build
{
internal class BuilderCommandApp : CompilerCommandApp
{
public const string BuildProfileFlag = "--build-profile";
public const string NoIncrementalFlag = "--no-incremental";
public const string NoDependenciesFlag = "--no-dependencies";
public bool ShouldPrintIncrementalPreconditions => OptionHasValue(BuildProfileFlag);
public bool ShouldNotUseIncrementality => OptionHasValue(NoIncrementalFlag);
public bool ShouldSkipDependencies => OptionHasValue(NoDependenciesFlag);
public BuilderCommandApp(string name, string fullName, string description) : base(name, fullName, description)
{
AddNoValueOption(BuildProfileFlag, "Set this flag to print the incremental safety checks that prevent incremental compilation");
AddNoValueOption(NoIncrementalFlag, "Set this flag to turn off incremental build");
AddNoValueOption(NoDependenciesFlag, "Set this flag to ignore project to project references and only build the root project");
}
}
}

View file

@ -0,0 +1,10 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace Microsoft.DotNet.Tools.Build
{
internal enum CompilationResult
{
IncrementalSkip, Success, Failure
}
}

View file

@ -1,8 +1,11 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
namespace Microsoft.DotNet.Tools.Build
{
public struct CompilerIO
internal struct CompilerIO
{
public readonly List<string> Inputs;
public readonly List<string> Outputs;

View file

@ -1,3 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;

View file

@ -1,3 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
@ -11,17 +14,17 @@ using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.DotNet.Tools.Build
{
class DotNetProjectBuilder : ProjectBuilder
internal class DotNetProjectBuilder : ProjectBuilder
{
private readonly BuilderCommandApp _args;
private readonly BuildCommandApp _args;
private readonly IncrementalPreconditionManager _preconditionManager;
private readonly CompilerIOManager _compilerIOManager;
private readonly ScriptRunner _scriptRunner;
private readonly DotNetCommandFactory _commandFactory;
public DotNetProjectBuilder(BuilderCommandApp args) : base(args.ShouldSkipDependencies)
public DotNetProjectBuilder(BuildCommandApp args)
{
_args = (BuilderCommandApp)args.ShallowCopy();
_args = args;
_preconditionManager = new IncrementalPreconditionManager(
args.ShouldPrintIncrementalPreconditions,
args.ShouldNotUseIncrementality,

View file

@ -1,3 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
@ -9,7 +12,7 @@ using Microsoft.DotNet.ProjectModel.Utilities;
namespace Microsoft.DotNet.Tools.Build
{
class IncrementalPreconditionManager
internal class IncrementalPreconditionManager
{
private readonly bool _printPreconditions;
private readonly bool _forceNonIncremental;
@ -41,34 +44,14 @@ namespace Microsoft.DotNet.Tools.Build
preconditions.AddForceUnsafePrecondition();
}
var projectsToCheck = GetProjectsToCheck(projectNode);
foreach (var project in projectsToCheck)
{
CollectScriptPreconditions(project, preconditions);
CollectCompilerNamePreconditions(project, preconditions);
CollectCheckPathProbingPreconditions(project, preconditions);
}
var project = projectNode.ProjectContext;
CollectScriptPreconditions(project, preconditions);
CollectCompilerNamePreconditions(project, preconditions);
CollectCheckPathProbingPreconditions(project, preconditions);
_preconditions[projectNode.ProjectContext.Identity] = preconditions;
return preconditions;
}
private List<ProjectContext> GetProjectsToCheck(ProjectGraphNode projectNode)
{
if (_skipDependencies)
{
return new List<ProjectContext>(1) { projectNode.ProjectContext };
}
// include initial root project
var contextsToCheck = new List<ProjectContext>(1 + projectNode.Dependencies.Count) { projectNode.ProjectContext };
// TODO: not traversing deeper than 1 level of dependencies
contextsToCheck.AddRange(projectNode.Dependencies.Select(n => n.ProjectContext));
return contextsToCheck;
}
private void CollectCheckPathProbingPreconditions(ProjectContext project, IncrementalPreconditions preconditions)
{
var pathCommands = CompilerUtil.GetCommandsInvokedByCompile(project)

View file

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Compiler;
namespace Microsoft.DotNet.Tools.Build
{
@ -36,7 +37,7 @@ namespace Microsoft.DotNet.Tools.Build
public void AddForceUnsafePrecondition()
{
_preconditions.Add($"[Forced Unsafe] The build was marked as unsafe. Remove the {BuilderCommandApp.NoIncrementalFlag} flag to enable incremental compilation");
_preconditions.Add($"[Forced Unsafe] The build was marked as unsafe. Remove the {BuildCommandApp.NoIncrementalFlag} flag to enable incremental compilation");
}
public bool PreconditionsDetected()
@ -75,7 +76,7 @@ namespace Microsoft.DotNet.Tools.Build
{
if (PreconditionsDetected())
{
return _isProfile ? PreconditionsMessage().Yellow() : $"(The compilation time can be improved. Run \"dotnet build {BuilderCommandApp.BuildProfileFlag}\" for more information)";
return _isProfile ? PreconditionsMessage().Yellow() : $"(The compilation time can be improved. Run \"dotnet build {BuildCommandApp.BuildProfileFlag}\" for more information)";
}
return "";

View file

@ -9,6 +9,7 @@ using System.Threading.Tasks;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.Tools.Compiler;
using Microsoft.DotNet.Cli.Utils;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Tools.Build
{
@ -20,7 +21,7 @@ namespace Microsoft.DotNet.Tools.Build
try
{
var app = new BuilderCommandApp("dotnet build", ".NET Builder", "Builder for the .NET Platform. It performs incremental compilation if it's safe to do so. Otherwise it delegates to dotnet-compile which performs non-incremental compilation");
var app = new BuildCommandApp("dotnet build", ".NET Builder", "Builder for the .NET Platform. It performs incremental compilation if it's safe to do so. Otherwise it delegates to dotnet-compile which performs non-incremental compilation");
return app.Execute(OnExecute, args);
}
catch (Exception ex)
@ -34,12 +35,68 @@ namespace Microsoft.DotNet.Tools.Build
}
}
private static bool OnExecute(List<ProjectContext> contexts, CompilerCommandApp args)
private static bool OnExecute(IEnumerable<string> files, IEnumerable<NuGetFramework> frameworks, BuildCommandApp args)
{
var graphCollector = new ProjectGraphCollector((project, target) => ProjectContext.Create(project, target));
var builderCommandApp = args;
var graphCollector = new ProjectGraphCollector(
!builderCommandApp.ShouldSkipDependencies,
(project, target) => ProjectContext.Create(project, target));
var contexts = ResolveRootContexts(files, frameworks, args);
var graph = graphCollector.Collect(contexts).ToArray();
var builder = new DotNetProjectBuilder((BuilderCommandApp) args);
return builder.Build(graph).All(r => r != CompilationResult.Failure);
var builder = new DotNetProjectBuilder(builderCommandApp);
return builder.Build(graph).ToArray().All(r => r != CompilationResult.Failure);
}
private static IEnumerable<ProjectContext> ResolveRootContexts(
IEnumerable<string> files,
IEnumerable<NuGetFramework> frameworks,
BuildCommandApp args)
{
// Set defaults based on the environment
var settings = ProjectReaderSettings.ReadFromEnvironment();
if (!string.IsNullOrEmpty(args.VersionSuffixValue))
{
settings.VersionSuffix = args.VersionSuffixValue;
}
foreach (var file in files)
{
var project = ProjectReader.GetProject(file);
var projectFrameworks = project.GetTargetFrameworks().Select(f => f.FrameworkName);
if (!projectFrameworks.Any())
{
throw new InvalidOperationException(
$"Project '{file}' does not have any frameworks listed in the 'frameworks' section.");
}
IEnumerable<NuGetFramework> selectedFrameworks;
if (frameworks != null)
{
var unsupportedByProject = frameworks.Where(f => !projectFrameworks.Contains(f));
if (unsupportedByProject.Any())
{
throw new InvalidOperationException(
$"Project \'{file}\' does not support framework: {string.Join(", ", unsupportedByProject.Select(fx => fx.DotNetFrameworkName))}.");
}
selectedFrameworks = frameworks;
}
else
{
selectedFrameworks = projectFrameworks;
}
foreach (var framework in selectedFrameworks)
{
yield return new ProjectContextBuilder()
.WithProjectDirectory(Path.GetDirectoryName(file))
.WithTargetFramework(framework)
.WithReaderSettings(settings)
.Build();
}
}
}
}
}

View file

@ -1,3 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
@ -6,27 +9,14 @@ using NuGet.Frameworks;
namespace Microsoft.DotNet.Tools.Build
{
internal enum CompilationResult
{
IncrementalSkip, Success, Failure
}
internal abstract class ProjectBuilder
{
private readonly bool _skipDependencies;
public ProjectBuilder(bool skipDependencies)
{
_skipDependencies = skipDependencies;
}
private Dictionary<ProjectContextIdentity, CompilationResult> _compilationResults = new Dictionary<ProjectContextIdentity, CompilationResult>();
public IEnumerable<CompilationResult> Build(IEnumerable<ProjectGraphNode> roots)
{
foreach (var projectNode in roots)
{
Console.WriteLine(projectNode.ProjectContext.Identity.TargetFramework);
yield return Build(projectNode);
}
}
@ -67,20 +57,18 @@ namespace Microsoft.DotNet.Tools.Build
private CompilationResult CompileWithDependencies(ProjectGraphNode projectNode)
{
if (!_skipDependencies)
foreach (var dependency in projectNode.Dependencies)
{
foreach (var dependency in projectNode.Dependencies)
var context = dependency.ProjectContext;
if (!context.ProjectFile.Files.SourceFiles.Any())
{
var context = dependency.ProjectContext;
if (!context.ProjectFile.Files.SourceFiles.Any())
{
continue;
}
var result = Build(dependency);
if (result == CompilationResult.Failure)
{
return CompilationResult.Failure;
}
continue;
}
var result = Build(dependency);
if (result == CompilationResult.Failure)
{
return CompilationResult.Failure;
}
}
if (NeedsRebuilding(projectNode))

View file

@ -1,3 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using NuGet.Frameworks;
@ -8,12 +11,15 @@ using System.Threading.Tasks;
namespace Microsoft.DotNet.Tools.Build
{
public class ProjectGraphCollector
internal class ProjectGraphCollector
{
private readonly bool _collectDependencies;
private readonly Func<string, NuGetFramework, ProjectContext> _projectContextFactory;
public ProjectGraphCollector(Func<string, NuGetFramework, ProjectContext> projectContextFactory)
public ProjectGraphCollector(bool collectDependencies,
Func<string, NuGetFramework, ProjectContext> projectContextFactory)
{
_collectDependencies = collectDependencies;
_projectContextFactory = projectContextFactory;
}
@ -24,24 +30,27 @@ namespace Microsoft.DotNet.Tools.Build
var libraries = context.LibraryManager.GetLibraries();
var lookup = libraries.ToDictionary(l => l.Identity.Name);
var root = lookup[context.ProjectFile.Name];
yield return TraverseProject((ProjectDescription)root, lookup, context);
yield return TraverseProject((ProjectDescription) root, lookup, context);
}
}
private ProjectGraphNode TraverseProject(ProjectDescription project, IDictionary<string, LibraryDescription> lookup, ProjectContext context = null)
{
var deps = new List<ProjectGraphNode>();
foreach (var dependency in project.Dependencies)
if (_collectDependencies)
{
var libraryDescription = lookup[dependency.Name];
foreach (var dependency in project.Dependencies)
{
var libraryDescription = lookup[dependency.Name];
if (libraryDescription.Identity.Type.Equals(LibraryType.Project))
{
deps.Add(TraverseProject((ProjectDescription)libraryDescription, lookup));
}
else
{
deps.AddRange(TraverseNonProject(libraryDescription, lookup));
if (libraryDescription.Identity.Type.Equals(LibraryType.Project))
{
deps.Add(TraverseProject((ProjectDescription)libraryDescription, lookup));
}
else
{
deps.AddRange(TraverseNonProject(libraryDescription, lookup));
}
}
}
var task = context != null ? Task.FromResult(context) : Task.Run(() => _projectContextFactory(project.Path, project.Framework));

View file

@ -1,3 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.ProjectModel;
using System;
using System.Collections.Generic;
@ -6,7 +9,7 @@ using System.Threading.Tasks;
namespace Microsoft.DotNet.Tools.Build
{
public class ProjectGraphNode
internal class ProjectGraphNode
{
private readonly Task<ProjectContext> _projectContextCreator;

View file

@ -17,7 +17,7 @@ namespace Microsoft.DotNet.Tools.Compiler
_nativeCompiler = nativeCompiler;
}
public bool Compile(IEnumerable<ProjectContext> contexts, CompilerCommandApp args)
public bool Compile(IEnumerable<ProjectContext> contexts, BuildCommandApp args)
{
var success = true;

View file

@ -15,7 +15,7 @@ namespace Microsoft.DotNet.Tools.Compiler
{
public abstract class Compiler : ICompiler
{
public abstract bool Compile(ProjectContext context, CompilerCommandApp args);
public abstract bool Compile(ProjectContext context, BuildCommandApp args);
protected static bool PrintSummary(List<DiagnosticMessage> diagnostics, Stopwatch sw, bool success = true)
{

View file

@ -1,205 +0,0 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
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.ProjectModel;
using Microsoft.Extensions.PlatformAbstractions;
using NuGet.Frameworks;
// This class is responsible with defining the arguments for the Compile verb.
// It knows how to interpret them and set default values
namespace Microsoft.DotNet.Tools.Compiler
{
public delegate bool OnExecute(List<ProjectContext> contexts, CompilerCommandApp compilerCommand);
public class CompilerCommandApp
{
private readonly CommandLineApplication _app;
// options and arguments for compilation
private CommandOption _outputOption;
private CommandOption _buildBasePath;
private CommandOption _frameworkOption;
private CommandOption _runtimeOption;
private CommandOption _versionSuffixOption;
private CommandOption _configurationOption;
private CommandArgument _projectArgument;
private CommandOption _nativeOption;
private CommandOption _archOption;
private CommandOption _ilcArgsOption;
private CommandOption _ilcPathOption;
private CommandOption _ilcSdkPathOption;
private CommandOption _appDepSdkPathOption;
private CommandOption _cppModeOption;
private CommandOption _cppCompilerFlagsOption;
// resolved values for the options and arguments
public string ProjectPathValue { get; set; }
public string BuildBasePathValue { get; set; }
public string RuntimeValue { get; set; }
public string OutputValue { get; set; }
public string VersionSuffixValue { get; set; }
public string ConfigValue { get; set; }
public bool IsNativeValue { get; set; }
public string ArchValue { get; set; }
public IEnumerable<string> IlcArgsValue { get; set; }
public string IlcPathValue { get; set; }
public string IlcSdkPathValue { get; set; }
public bool IsCppModeValue { get; set; }
public string AppDepSdkPathValue { get; set; }
public string CppCompilerFlagsValue { get; set; }
// workaround: CommandLineApplication is internal therefore I cannot make _app protected so baseclasses can add their own params
private readonly Dictionary<string, CommandOption> baseClassOptions;
public CompilerCommandApp(string name, string fullName, string description)
{
_app = new CommandLineApplication
{
Name = name,
FullName = fullName,
Description = description
};
baseClassOptions = new Dictionary<string, CommandOption>();
AddCompileParameters();
}
private void AddCompileParameters()
{
_app.HelpOption("-h|--help");
_outputOption = _app.Option("-o|--output <OUTPUT_DIR>", "Directory in which to place outputs", CommandOptionType.SingleValue);
_buildBasePath = _app.Option("-b|--build-base-path <OUTPUT_DIR>", "Directory in which to place temporary outputs", CommandOptionType.SingleValue);
_frameworkOption = _app.Option("-f|--framework <FRAMEWORK>", "Compile a specific framework", CommandOptionType.SingleValue);
_runtimeOption = _app.Option("-r|--runtime <RUNTIME_IDENTIFIER>", "Produce runtime-specific assets for the specified runtime", CommandOptionType.SingleValue);
_configurationOption = _app.Option("-c|--configuration <CONFIGURATION>", "Configuration under which to build", CommandOptionType.SingleValue);
_versionSuffixOption = _app.Option("--version-suffix <VERSION_SUFFIX>", "Defines what `*` should be replaced with in version field in project.json", CommandOptionType.SingleValue);
_projectArgument = _app.Argument("<PROJECT>", "The project to compile, defaults to the current directory. Can be a path to a project.json or a project directory");
// Native Args
_nativeOption = _app.Option("-n|--native", "Compiles source to native machine code.", CommandOptionType.NoValue);
_archOption = _app.Option("-a|--arch <ARCH>", "The architecture for which to compile. x64 only currently supported.", CommandOptionType.SingleValue);
_ilcArgsOption = _app.Option("--ilcarg <ARG>", "Command line option to be passed directly to ILCompiler.", CommandOptionType.MultipleValue);
_ilcPathOption = _app.Option("--ilcpath <PATH>", "Path to the folder containing custom built ILCompiler.", CommandOptionType.SingleValue);
_ilcSdkPathOption = _app.Option("--ilcsdkpath <PATH>", "Path to the folder containing ILCompiler application dependencies.", CommandOptionType.SingleValue);
_appDepSdkPathOption = _app.Option("--appdepsdkpath <PATH>", "Path to the folder containing ILCompiler application dependencies.", CommandOptionType.SingleValue);
_cppModeOption = _app.Option("--cpp", "Flag to do native compilation with C++ code generator.", CommandOptionType.NoValue);
_cppCompilerFlagsOption = _app.Option("--cppcompilerflags <flags>", "Additional flags to be passed to the native compiler.", CommandOptionType.SingleValue);
}
public int Execute(OnExecute execute, string[] args)
{
_app.OnExecute(() =>
{
if (_outputOption.HasValue() && !_frameworkOption.HasValue())
{
Reporter.Error.WriteLine("When the '--output' option is provided, the '--framework' option must also be provided.");
return 1;
}
// Locate the project and get the name and full path
ProjectPathValue = _projectArgument.Value;
if (string.IsNullOrEmpty(ProjectPathValue))
{
ProjectPathValue = Directory.GetCurrentDirectory();
}
OutputValue = _outputOption.Value();
BuildBasePathValue = _buildBasePath.Value();
ConfigValue = _configurationOption.Value() ?? Constants.DefaultConfiguration;
RuntimeValue = _runtimeOption.Value();
VersionSuffixValue = _versionSuffixOption.Value();
IsNativeValue = _nativeOption.HasValue();
ArchValue = _archOption.Value();
IlcArgsValue = _ilcArgsOption.HasValue() ? _ilcArgsOption.Values : Enumerable.Empty<string>();
IlcPathValue = _ilcPathOption.Value();
IlcSdkPathValue = _ilcSdkPathOption.Value();
AppDepSdkPathValue = _appDepSdkPathOption.Value();
IsCppModeValue = _cppModeOption.HasValue();
CppCompilerFlagsValue = _cppCompilerFlagsOption.Value();
// Set defaults based on the environment
var settings = ProjectReaderSettings.ReadFromEnvironment();
if (!string.IsNullOrEmpty(VersionSuffixValue))
{
settings.VersionSuffix = VersionSuffixValue;
}
// Load the project file and construct all the targets
var targets = ProjectContext.CreateContextForEachFramework(ProjectPathValue, settings).ToList();
if (targets.Count == 0)
{
// Project is missing 'frameworks' section
Reporter.Error.WriteLine("Project does not have any frameworks listed in the 'frameworks' section.");
return 1;
}
// Filter the targets down based on the inputs
if (_frameworkOption.HasValue())
{
var fx = NuGetFramework.Parse(_frameworkOption.Value());
targets = targets.Where(t => fx.Equals(t.TargetFramework)).ToList();
if (targets.Count == 0)
{
// We filtered everything out
Reporter.Error.WriteLine($"Project does not support framework: {fx.DotNetFrameworkName}.");
return 1;
}
Debug.Assert(targets.Count == 1);
}
Debug.Assert(targets.All(t => string.IsNullOrEmpty(t.RuntimeIdentifier)));
var success = execute(targets, this);
return success ? 0 : 1;
});
return _app.Execute(args);
}
public CompilerCommandApp ShallowCopy()
{
return (CompilerCommandApp)MemberwiseClone();
}
// CommandOptionType is internal. Cannot pass it as argument. Therefore the method name encodes the option type.
protected void AddNoValueOption(string optionTemplate, string descriptino)
{
baseClassOptions[optionTemplate] = _app.Option(optionTemplate, descriptino, CommandOptionType.NoValue);
}
protected bool OptionHasValue(string optionTemplate)
{
CommandOption option;
return baseClassOptions.TryGetValue(optionTemplate, out option) && option.HasValue();
}
public IEnumerable<string> GetRuntimes()
{
var rids = new List<string>();
if (string.IsNullOrEmpty(RuntimeValue))
{
return PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers();
}
else
{
return new[] { RuntimeValue };
}
}
}
}

View file

@ -7,6 +7,6 @@ namespace Microsoft.DotNet.Tools.Compiler
{
public interface ICompiler
{
bool Compile(ProjectContext context, CompilerCommandApp args);
bool Compile(ProjectContext context, BuildCommandApp args);
}
}

View file

@ -29,7 +29,7 @@ namespace Microsoft.DotNet.Tools.Compiler
_commandFactory = commandFactory;
}
public override bool Compile(ProjectContext context, CompilerCommandApp args)
public override bool Compile(ProjectContext context, BuildCommandApp args)
{
// Set up Output Paths
var outputPaths = context.GetOutputPaths(args.ConfigValue, args.BuildBasePathValue);

View file

@ -1,126 +0,0 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.DotNet.ProjectModel;
namespace Microsoft.DotNet.Tools.Compiler
{
public class NativeCompiler : Compiler
{
public override bool Compile(ProjectContext context, CompilerCommandApp args)
{
var outputPaths = context.GetOutputPaths(args.ConfigValue, args.BuildBasePathValue, args.OutputValue);
var outputPath = outputPaths.RuntimeOutputPath;
var nativeOutputPath = Path.Combine(outputPath, "native");
var intermediateOutputPath =
outputPaths.IntermediateOutputDirectoryPath;
var nativeTempOutput = Path.Combine(intermediateOutputPath, "native");
Directory.CreateDirectory(nativeOutputPath);
Directory.CreateDirectory(nativeTempOutput);
var managedOutput = outputPaths.CompilationFiles.Assembly;
// Create the library exporter
var exporter = context.CreateExporter(args.ConfigValue);
var exports = exporter.GetDependencies();
// Runtime assemblies.
// TODO: native assets/resources.
var references = exports
.SelectMany(export => export.RuntimeAssemblyGroups.GetDefaultAssets())
.Select(r => r.ResolvedPath)
.ToList();
// Setup native args.
var nativeArgs = new List<string>();
// Input Assembly
nativeArgs.Add($"{managedOutput}");
// Add Resolved Assembly References
foreach (var reference in references)
{
nativeArgs.Add("--reference");
nativeArgs.Add(reference);
}
// ILC Args
foreach (var ilcArg in args.IlcArgsValue)
{
nativeArgs.Add("--ilcarg");
nativeArgs.Add($"\"{ilcArg}\"");
}
// ILC Path
if (!string.IsNullOrWhiteSpace(args.IlcPathValue))
{
nativeArgs.Add("--ilcpath");
nativeArgs.Add(args.IlcPathValue);
}
// ILC SDK Path
if (!string.IsNullOrWhiteSpace(args.IlcSdkPathValue))
{
nativeArgs.Add("--ilcsdkpath");
nativeArgs.Add(args.IlcSdkPathValue);
}
// AppDep SDK Path
if (!string.IsNullOrWhiteSpace(args.AppDepSdkPathValue))
{
nativeArgs.Add("--appdepsdk");
nativeArgs.Add(args.AppDepSdkPathValue);
}
// CodeGen Mode
if (args.IsCppModeValue)
{
nativeArgs.Add("--mode");
nativeArgs.Add("cpp");
}
if (!string.IsNullOrWhiteSpace(args.CppCompilerFlagsValue))
{
nativeArgs.Add("--cppcompilerflags");
nativeArgs.Add(args.CppCompilerFlagsValue);
}
// Configuration
if (args.ConfigValue != null)
{
nativeArgs.Add("--configuration");
nativeArgs.Add(args.ConfigValue);
}
// Architecture
if (args.ArchValue != null)
{
nativeArgs.Add("--arch");
nativeArgs.Add(args.ArchValue);
}
// Intermediate Path
nativeArgs.Add("--temp-output");
nativeArgs.Add($"{nativeTempOutput}");
// Output Path
nativeArgs.Add("--output");
nativeArgs.Add($"{nativeOutputPath}");
// Write Response File
var rsp = Path.Combine(nativeTempOutput, $"dotnet-compile-native.{context.ProjectFile.Name}.rsp");
File.WriteAllLines(rsp, nativeArgs);
// TODO Add -r assembly.dll for all Nuget References
// Need CoreRT Framework published to nuget
// Do Native Compilation
var result = Native.CompileNativeCommand.Run(new string[] { "--rsp", $"{rsp}" });
return result == 0;
}
}
}

View file

@ -1,40 +0,0 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.Tools.Compiler
{
public class CompileCommand
{
public static int Run(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);
try
{
var commandFactory = new DotNetCommandFactory();
var scriptRunner = new ScriptRunner();
var managedCompiler = new ManagedCompiler(scriptRunner, commandFactory);
var nativeCompiler = new NativeCompiler();
var compilationDriver = new CompilationDriver(managedCompiler, nativeCompiler);
var compilerCommandArgs = new CompilerCommandApp("dotnet compile", ".NET Compiler", "Compiler for the .NET Platform");
return compilerCommandArgs.Execute(compilationDriver.Compile, args);
}
catch (Exception ex)
{
#if DEBUG
Console.Error.WriteLine(ex);
#else
Console.Error.WriteLine(ex.Message);
#endif
return 1;
}
}
}
}

View file

@ -18,12 +18,14 @@ namespace Microsoft.DotNet.Cli.Utils.Tests
.CreateTestInstance("DesktopAppWhichCallsDotnet")
.WithLockFiles()
.WithBuildArtifacts();
// project was moved to another location and needs it's relative path to Utils project restored
new RestoreCommand().Execute(testInstance.TestRoot).Should().Pass();
var testProjectAssetManager = GetTestGroupTestAssetsManager("TestProjects");
var testInstanceToBuild = testProjectAssetManager
.CreateTestInstance("TestAppSimple")
.WithLockFiles();
var testProject = Path.Combine(testInstance.TestRoot, "project.json");
var testProjectToBuild = Path.Combine(testInstanceToBuild.TestRoot, "project.json");

View file

@ -34,7 +34,7 @@ namespace Microsoft.DotNet.Tools.Builder.Tests
.ExecuteWithCapturedOutput();
result.Should().Fail();
result.Should().HaveStdErrContaining("Project does not support framework: Silverlight,Version=v4.0.");
result.Should().HaveStdErrMatching("Project '.*?' does not support framework: Silverlight,Version=v4\\.0\\.");
}
[Fact]

View file

@ -0,0 +1,134 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.DotNet.TestFramework;
using Microsoft.DotNet.Tools.Test.Utilities;
using Xunit;
using FluentAssertions;
using NuGet.Frameworks;
namespace Microsoft.DotNet.Tools.Builder.Tests
{
public class ProjectNameArgumentTests : TestBase
{
private TestInstance _testInstance;
[Fact]
public void TestProjectDirectoryPath()
{
Test(new[] { Path.Combine("src", "L21") }, new[] { "L21" });
}
[Fact]
public void TestProjectFile()
{
Test(new[] { Path.Combine("src", "L21", "project.json") }, new[] { "L21" });
}
[Fact]
public void TestMultipleProjectDirectories()
{
Test(new[]
{
Path.Combine("src", "L21"),
Path.Combine("src", "L11")
},
new[] { "L21", "L11" });
}
[Fact]
public void TestMultipleProjectFiles()
{
Test(new[]
{
Path.Combine("src", "L21", "project.json"),
Path.Combine("src", "L11", "project.json"),
},
new[] { "L21", "L11" });
}
[Fact]
public void TestGlobbing()
{
Test(new[]
{
Path.Combine("src", "**", "project.json")
},
new[] { "L21", "L11", "L12" });
}
[Fact]
public void TestMultipleGlobbing()
{
Test(new[]
{
Path.Combine("src", "L1*", "project.json"),
Path.Combine("src", "L2*", "project.json")
},
new[] { "L11", "L12", "L21", "L22" });
}
[Fact]
public void TestFailsWhenNoGlobbingNoMatch()
{
Test(new[]
{
Path.Combine("src", "L33*", "project.json")
},
null);
}
[Fact]
public void TestFailsFileDoedNotExist()
{
Test(new[]
{
Path.Combine("src", "L33", "project.json")
},
null);
}
[Fact]
public void TestFindsProjectJsonInCurrentDirectoryWithNoArguments()
{
Test(new string[] { }, new[] { "L21" }, workingDirectory: Path.Combine("src", "L21"));
}
[Fact]
public void TestFailsIfNoProjectJsonInCurrentDirectoryWithNoArguments()
{
Test(new string[] { }, null, workingDirectory: "src");
}
private void Test(IEnumerable<string> inputs, IEnumerable<string> expectedProjects, string workingDirectory = null, [CallerMemberName] string testName = null)
{
var instance = TestAssetsManager.CreateTestInstance("TestProjectToProjectDependencies", testName)
.WithLockFiles()
.WithBuildArtifacts();
string args = string.Join(" ", inputs);
workingDirectory = workingDirectory != null
? Path.Combine(instance.TestRoot, workingDirectory)
: instance.TestRoot;
var result = new TestCommand("dotnet")
{
WorkingDirectory = Path.Combine(workingDirectory)
}.ExecuteWithCapturedOutput("--verbose build --no-dependencies " + args);
if (expectedProjects != null)
{
result.Should().Pass();
foreach (var expectedProject in expectedProjects)
{
result.Should().HaveSkippedProjectCompilation(expectedProject, NuGetFramework.Parse("netstandard1.5").DotNetFrameworkName);
}
}
else
{
result.Should().Fail();
}
}
}
}

View file

@ -19,7 +19,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests
private Mock<ICompiler> _managedCompilerMock;
private Mock<ICompiler> _nativeCompilerMock;
private List<ProjectContext> _contexts;
private CompilerCommandApp _args;
private BuildCommandApp _args;
public GivenACompilationDriverController()
{
@ -27,11 +27,11 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests
Path.Combine(AppContext.BaseDirectory, "TestAssets", "TestProjects", "TestAppWithLibrary", "TestApp", "project.json");
_managedCompilerMock = new Mock<ICompiler>();
_managedCompilerMock.Setup(c => c
.Compile(It.IsAny<ProjectContext>(), It.IsAny<CompilerCommandApp>()))
.Compile(It.IsAny<ProjectContext>(), It.IsAny<BuildCommandApp>()))
.Returns(true);
_nativeCompilerMock = new Mock<ICompiler>();
_nativeCompilerMock.Setup(c => c
.Compile(It.IsAny<ProjectContext>(), It.IsAny<CompilerCommandApp>()))
.Compile(It.IsAny<ProjectContext>(), It.IsAny<BuildCommandApp>()))
.Returns(true);
_contexts = new List<ProjectContext>
@ -39,7 +39,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests
ProjectContext.Create(_projectJson, NuGetFramework.Parse("netcoreapp1.0"))
};
_args = new CompilerCommandApp("dotnet compile", ".NET Compiler", "Compiler for the .NET Platform");
_args = new BuildCommandApp("dotnet compile", ".NET Compiler", "Compiler for the .NET Platform");
}
[Fact]
@ -47,8 +47,8 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests
{
var compiledProjectContexts = new List<ProjectContext>();
_managedCompilerMock.Setup(c => c
.Compile(It.IsAny<ProjectContext>(), It.IsAny<CompilerCommandApp>()))
.Callback<ProjectContext, CompilerCommandApp>((p, c) => compiledProjectContexts.Add(p))
.Compile(It.IsAny<ProjectContext>(), It.IsAny<BuildCommandApp>()))
.Callback<ProjectContext, BuildCommandApp>((p, c) => compiledProjectContexts.Add(p))
.Returns(true);
var compilerController = new CompilationDriver(_managedCompilerMock.Object, _nativeCompilerMock.Object);
@ -65,7 +65,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests
compilerController.Compile(_contexts, _args);
_nativeCompilerMock.Verify(c => c.Compile(It.IsAny<ProjectContext>(), It.IsAny<CompilerCommandApp>()), Times.Never);
_nativeCompilerMock.Verify(c => c.Compile(It.IsAny<ProjectContext>(), It.IsAny<BuildCommandApp>()), Times.Never);
}
[Fact]
@ -77,7 +77,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests
compilerController.Compile(_contexts, _args);
_nativeCompilerMock.Verify(c => c.Compile(It.IsAny<ProjectContext>(), It.IsAny<CompilerCommandApp>()), Times.Once);
_nativeCompilerMock.Verify(c => c.Compile(It.IsAny<ProjectContext>(), It.IsAny<BuildCommandApp>()), Times.Once);
}
}
}

View file

@ -188,7 +188,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Tests
It.IsAny<string>()))
.Returns(command.Object);
var _args = new CompilerCommandApp("dotnet compile", ".NET Compiler", "Compiler for the .NET Platform");
var _args = new BuildCommandApp("dotnet compile", ".NET Compiler", "Compiler for the .NET Platform");
_args.ConfigValue = ConfigValue;
PreCompileScriptVariables = new Dictionary<string, string>();