--native first attempt

This commit is contained in:
Bryan Thornbury 2015-10-29 12:03:01 -07:00
parent a29ebe05ff
commit 7fe32a93b1
3 changed files with 116 additions and 31 deletions

View file

@ -68,7 +68,11 @@ create_or_start_vm(){
if [[ $(docker-machine ls | grep $VM_NAME) == "" ]]; then
docker-machine create -d virtualbox $VM_NAME
else
docker-machine start $VM_NAME
# This fails sometimes
if ! docker-machine start $VM_NAME; then
docker-machine rm -f $VM_NAME
docker-machine create -d virtualbox $VM_NAME
fi
fi
}

View file

@ -15,6 +15,11 @@ namespace Microsoft.DotNet.Cli.Utils
public static readonly string DefaultConfiguration = "Debug";
public static readonly string BinDirectoryName = "bin";
public static readonly string ObjDirectoryName = "obj";
public static readonly string DynlibSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".dll" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ".dylib" : ".so";
public static readonly string StaticlibSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".lib" : ".a" ;
public static readonly string ClrPathEnvironmentVariable = "DOTNET_CLR_PATH";
}

View file

@ -29,6 +29,7 @@ namespace Microsoft.DotNet.Tools.Compiler
var configuration = app.Option("-c|--configuration <CONFIGURATION>", "Configuration under which to build", CommandOptionType.SingleValue);
var noProjectDependencies = app.Option("--no-project-dependencies", "Skips building project references.", CommandOptionType.NoValue);
var project = app.Argument("<PROJECT>", "The project to compile, defaults to the current directory. Can be a path to a project.json or a project directory");
var native = app.Argument("-n|--native <NATIVE_TYPE>", "Compiles source to native machine code.", CommandOptionType.SingleValue);
app.OnExecute(() =>
{
@ -40,6 +41,9 @@ namespace Microsoft.DotNet.Tools.Compiler
}
var buildProjectReferences = !noProjectDependencies.HasValue();
var isNative = native.HasValue();
var outputPath = GetOutputPath(output.value());
// Load project contexts for each framework and compile them
bool success = true;
@ -48,6 +52,11 @@ namespace Microsoft.DotNet.Tools.Compiler
foreach (var context in framework.Values.Select(f => ProjectContext.Create(path, NuGetFramework.Parse(f))))
{
success &= Compile(context, configuration.Value() ?? Constants.DefaultConfiguration, output.Value(), buildProjectReferences);
if (isNative)
{
success &= CompileNative(context, configuration.Value() ?? Constants.DefaultConfiguration, output.Value(), buildProjectReferences, native.value());
}
}
}
else
@ -55,6 +64,11 @@ namespace Microsoft.DotNet.Tools.Compiler
foreach (var context in ProjectContext.CreateContextForEachFramework(path))
{
success &= Compile(context, configuration.Value() ?? Constants.DefaultConfiguration, output.Value(), buildProjectReferences);
if (isNative)
{
success &= CompileNative(context, configuration.Value() ?? Constants.DefaultConfiguration, output.Value(), buildProjectReferences, native.value());
}
}
}
return success ? 0 : 1;
@ -75,10 +89,30 @@ namespace Microsoft.DotNet.Tools.Compiler
}
}
private static bool Compile(ProjectContext context, string configuration, string outputPath, bool buildProjectReferences)
private static bool CompileNative(ProjectContext context, string configuration, string outputOptionValue, string nativeOptionValue, bool buildProjectReferences){
string outputPath = GetOutputPath(context, configuration, outputOptionValue);
var managedBinaryPath = Path.Combine(outputPath, context.ProjectFile.Name + (compilationOptions.EmitEntryPoint.GetValueOrDefault() ? ".exe" : ".dll"));
// Do Native Compilation
var result = Command.Create($"dotnet-compile-native", $"\"{managedBinaryPath}\"", $"\"{outputPath}\"")
.ForwardStdErr()
.ForwardStdOut()
.Execute();
}
private static bool Compile(ProjectContext context, string configuration, string outputOptionValue, bool buildProjectReferences)
{
Reporter.Output.WriteLine($"Compiling {context.RootProject.Identity.Name.Yellow()} for {context.TargetFramework.DotNetFrameworkName.Yellow()}");
//Set up Output Paths
string outputPath = GetOutputPath(context, configuration, outputOptionValue);
string intermediateOutputPath = GetIntermediateOutputPath(context, configuration, outputOptionValue);
CleanOrCreateDirectory(outputPath);
CleanOrCreateDirectory(intermediateOutputPath);
// Create the library exporter
var exporter = context.CreateExporter(configuration);
@ -127,35 +161,6 @@ namespace Microsoft.DotNet.Tools.Compiler
// file anyways
// ShowDependencyInfo(dependencies);
// Hackily generate the output path
if (string.IsNullOrEmpty(outputPath))
{
outputPath = Path.Combine(
context.ProjectFile.ProjectDirectory,
Constants.BinDirectoryName,
configuration,
context.TargetFramework.GetTwoDigitShortFolderName());
}
string intermediateOutputPath = Path.Combine(
context.ProjectFile.ProjectDirectory,
Constants.ObjDirectoryName,
configuration,
context.TargetFramework.GetTwoDigitShortFolderName());
if (Directory.Exists(outputPath))
{
Directory.Delete(outputPath, recursive: true);
}
if (Directory.Exists(intermediateOutputPath))
{
Directory.Delete(intermediateOutputPath, recursive: true);
}
Directory.CreateDirectory(outputPath);
Directory.CreateDirectory(intermediateOutputPath);
// Get compilation options
var compilationOptions = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
var outputName = Path.Combine(outputPath, context.ProjectFile.Name + (compilationOptions.EmitEntryPoint.GetValueOrDefault() ? ".exe" : ".dll"));
@ -237,7 +242,78 @@ namespace Microsoft.DotNet.Tools.Compiler
return success;
}
private static string GetOutputPath(ProjectContext context, string configuration, string outputOptionValue)
{
var outputPath = String.Empty;
if (string.IsNullOrEmpty(outputOptionValue))
{
outputPath = Path.Combine(
GetDefaultRootOutputPath(context, outputOptionValue),
Constants.BinDirectoryName,
configuration,
context.TargetFramework.GetTwoDigitShortFolderName());
}
else
{
outputPath = outputOptionValue;
}
return outputPath;
string intermediateOutputPath = Path.Combine(
context.ProjectFile.ProjectDirectory,
Constants.ObjDirectoryName,
configuration,
context.TargetFramework.GetTwoDigitShortFolderName());
}
private static string GetIntermediateOutputPath(ProjectContext context, string configuration, string outputOptionValue)
{
var intermediateOutputPath = String.Empty;
if (string.IsNullOrEmpty(outputOptionValue))
{
intermediateOutputPath = Path.Combine(
GetDefaultRootOutputPath(context, outputOptionValue),
Constants.ObjDirectoryName,
configuration,
context.TargetFramework.GetTwoDigitShortFolderName());
}
else
{
intermediateOutputPath = outputOptionValue;
}
return intermediateOutputPath;
}
private static string GetDefaultRootOutputPath(ProjectContext context)
{
string rootOutputPath = String.Empty;
if (string.IsNullOrEmpty(outputOptionValue))
{
rootOutputPath = context.ProjectFile.ProjectDirectory;
}
return rootOutputPath;
}
private static void CleanOrCreateDirectory(string path)
{
if (Directory.Exists(path))
{
Directory.Delete(path, recursive: true);
}
Directory.Create(path)
}
private static void PrintSummary(bool success, List<DiagnosticMessage> diagnostics)
{
Reporter.Output.Writer.WriteLine();