Merge pull request #122 from brthor/compilenative

Compile Native Option, first version
This commit is contained in:
Bryan Thornbury 2015-10-30 15:57:08 -07:00
commit d5412f767e
6 changed files with 130 additions and 35 deletions

View file

@ -9,7 +9,7 @@
"homepage": "https://dotnet.github.io/core",
"release":{
"package_version":"0.3",
"package_version":"0.201",
"package_revision":"1",
"urgency" : "low",
"changelog_message" : "Bootstrap loop package"

View file

@ -42,6 +42,11 @@ add_system_dir_placement(){
abs_installation_dir=$2
dir_files=( $(_get_files_in_dir_tree $PACKAGE_SOURCE_DIR/$in_package_dir) )
# If in_package_dir isn't empty include a slash
if [ ! -z "$in_package_dir" ]; then
in_package_dir="${in_package_dir}/"
fi
for rel_filepath in ${dir_files[@]}
do
@ -49,9 +54,9 @@ add_system_dir_placement(){
# If there is no parent, parent_path = "."
if [[ "$parent_path" == "." ]]; then
add_system_file_placement "${in_package_dir}/${rel_filepath}" "${abs_installation_dir}"
add_system_file_placement "${in_package_dir}${rel_filepath}" "${abs_installation_dir}"
else
add_system_file_placement "${in_package_dir}/${rel_filepath}" "${abs_installation_dir}/${parent_path}"
add_system_file_placement "${in_package_dir}${rel_filepath}" "${abs_installation_dir}/${parent_path}"
fi
done

View file

@ -1,6 +1,5 @@
#!/usr/bin/env bash
set -e
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink

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

@ -13,5 +13,10 @@ 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 DynamicLibSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".dll" :
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ".dylib" : ".so";
public static readonly string StaticLibSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".lib" : ".a" ;
}
}

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.Option("-n|--native", "Compiles source to native machine code.", CommandOptionType.NoValue);
app.OnExecute(() =>
{
@ -40,6 +41,7 @@ namespace Microsoft.DotNet.Tools.Compiler
}
var buildProjectReferences = !noProjectDependencies.HasValue();
var isNative = native.HasValue();
// Load project contexts for each framework and compile them
bool success = true;
@ -48,6 +50,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);
}
}
}
else
@ -55,6 +62,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);
}
}
}
return success ? 0 : 1;
@ -75,10 +87,33 @@ 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, bool buildProjectReferences)
{
string outputPath = Path.Combine(GetOutputPath(context, configuration, outputOptionValue), "native");
var compilationOptions = context.ProjectFile.GetCompilerOptions(context.TargetFramework, configuration);
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();
return result.ExitCode == 0;
}
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 +162,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 outputExtension = ".dll";
@ -248,7 +254,83 @@ 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 outputOptionValue)
{
string rootOutputPath = String.Empty;
if (string.IsNullOrEmpty(outputOptionValue))
{
rootOutputPath = context.ProjectFile.ProjectDirectory;
}
return rootOutputPath;
}
private static void CleanOrCreateDirectory(string path)
{
if (Directory.Exists(path))
{
try
{
Directory.Delete(path, recursive: true);
}
catch(Exception e)
{
Console.WriteLine("Unable to remove directory: " + path);
Console.WriteLine(e.Message);
}
}
Directory.CreateDirectory(path);
}
private static void EmitHost(string outputPath, string projectName, LibraryExporter exporter)
{
// Write the Host information file (basically a simplified form of the lock file)