2015-11-17 12:10:19 -08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Compiler.Native
|
|
|
|
{
|
2015-11-17 17:02:16 -08:00
|
|
|
public class LinuxCppCompileStep : IPlatformNativeStep
|
2015-11-17 12:10:19 -08:00
|
|
|
{
|
2016-01-22 14:04:04 -08:00
|
|
|
private const string CompilerName = "clang-3.5";
|
|
|
|
private const string InputExtension = ".cpp";
|
2015-11-17 12:10:19 -08:00
|
|
|
|
|
|
|
// TODO: debug/release support
|
2016-01-22 14:04:04 -08:00
|
|
|
private readonly string [] _cLibsFlags = { "-lm", "-ldl"};
|
|
|
|
private readonly string [] _cflags = { "-g", "-lstdc++", "-lrt", "-Wno-invalid-offsetof", "-pthread"};
|
2015-11-17 12:10:19 -08:00
|
|
|
|
2016-01-22 14:04:04 -08:00
|
|
|
public IEnumerable<string> CompilerArgs { get; set; }
|
2015-11-17 19:50:19 -08:00
|
|
|
|
2016-01-22 14:04:04 -08:00
|
|
|
private readonly string[] _ilcSdkLibs =
|
|
|
|
{
|
|
|
|
"libbootstrappercpp.a",
|
|
|
|
"libPortableRuntime.a",
|
|
|
|
"libSystem.Private.CoreLib.Native.a"
|
|
|
|
};
|
2015-11-17 12:10:19 -08:00
|
|
|
|
2016-01-22 14:04:04 -08:00
|
|
|
private readonly string[] _appdeplibs =
|
|
|
|
{
|
2016-02-03 16:34:03 -08:00
|
|
|
"System.Native.a"
|
2016-01-22 14:04:04 -08:00
|
|
|
};
|
2015-11-17 12:10:19 -08:00
|
|
|
|
2015-11-17 17:02:16 -08:00
|
|
|
public LinuxCppCompileStep(NativeCompileSettings config)
|
2015-11-17 12:10:19 -08:00
|
|
|
{
|
|
|
|
InitializeArgs(config);
|
|
|
|
}
|
|
|
|
|
2015-11-17 19:50:19 -08:00
|
|
|
public int Invoke()
|
2015-11-17 12:10:19 -08:00
|
|
|
{
|
2015-11-17 19:50:19 -08:00
|
|
|
var result = InvokeCompiler();
|
2015-11-17 12:10:19 -08:00
|
|
|
if (result != 0)
|
|
|
|
{
|
|
|
|
Reporter.Error.WriteLine("Compilation of intermediate files failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CheckPreReqs()
|
|
|
|
{
|
|
|
|
// TODO check for clang
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-17 13:20:34 -08:00
|
|
|
private void InitializeArgs(NativeCompileSettings config)
|
2015-11-17 12:10:19 -08:00
|
|
|
{
|
|
|
|
var argsList = new List<string>();
|
|
|
|
|
|
|
|
// Flags
|
2016-01-22 14:04:04 -08:00
|
|
|
argsList.AddRange(_cflags);
|
2015-11-17 12:10:19 -08:00
|
|
|
|
2015-12-30 15:23:55 -08:00
|
|
|
var ilcSdkIncPath = Path.Combine(config.IlcSdkPath, "inc");
|
2015-11-17 12:10:19 -08:00
|
|
|
argsList.Add("-I");
|
2016-01-22 14:04:04 -08:00
|
|
|
argsList.Add($"{ilcSdkIncPath}");
|
2015-11-17 12:10:19 -08:00
|
|
|
|
|
|
|
// Input File
|
|
|
|
var inCppFile = DetermineInFile(config);
|
|
|
|
argsList.Add(inCppFile);
|
|
|
|
|
2015-12-20 20:46:28 -08:00
|
|
|
// Pass the optional native compiler flags if specified
|
|
|
|
if (!string.IsNullOrWhiteSpace(config.CppCompilerFlags))
|
|
|
|
{
|
|
|
|
argsList.Add(config.CppCompilerFlags);
|
|
|
|
}
|
|
|
|
|
2015-12-19 12:14:46 -08:00
|
|
|
// ILC SDK Libs
|
2016-01-22 14:04:04 -08:00
|
|
|
var ilcSdkLibPath = Path.Combine(config.IlcSdkPath, "sdk");
|
|
|
|
argsList.AddRange(_ilcSdkLibs.Select(lib => Path.Combine(ilcSdkLibPath, lib)));
|
2015-11-17 19:50:19 -08:00
|
|
|
|
|
|
|
// AppDep Libs
|
2015-11-20 15:32:44 -08:00
|
|
|
var baseAppDeplibPath = Path.Combine(config.AppDepSDKPath, "CPPSdk/ubuntu.14.04/x64");
|
2016-01-22 14:04:04 -08:00
|
|
|
argsList.AddRange(_appdeplibs.Select(lib => Path.Combine(baseAppDeplibPath, lib)));
|
2015-11-17 19:50:19 -08:00
|
|
|
|
2016-01-22 14:04:04 -08:00
|
|
|
argsList.AddRange(_cLibsFlags);
|
2015-11-17 12:10:19 -08:00
|
|
|
|
|
|
|
// Output
|
|
|
|
var libOut = DetermineOutputFile(config);
|
2016-01-22 14:04:04 -08:00
|
|
|
argsList.Add($"-o");
|
|
|
|
argsList.Add($"{libOut}");
|
2015-11-17 12:10:19 -08:00
|
|
|
|
2016-01-22 14:04:04 -08:00
|
|
|
CompilerArgs = argsList;
|
2015-11-17 12:10:19 -08:00
|
|
|
}
|
|
|
|
|
2015-11-17 19:50:19 -08:00
|
|
|
private int InvokeCompiler()
|
2015-11-17 12:10:19 -08:00
|
|
|
{
|
2016-01-22 14:04:04 -08:00
|
|
|
var result = Command.Create(CompilerName, CompilerArgs)
|
2015-11-17 12:10:19 -08:00
|
|
|
.ForwardStdErr()
|
|
|
|
.ForwardStdOut()
|
|
|
|
.Execute();
|
|
|
|
|
|
|
|
return result.ExitCode;
|
|
|
|
}
|
|
|
|
|
2015-11-17 13:20:34 -08:00
|
|
|
private string DetermineInFile(NativeCompileSettings config)
|
2015-11-17 12:10:19 -08:00
|
|
|
{
|
|
|
|
var intermediateDirectory = config.IntermediateDirectory;
|
|
|
|
|
|
|
|
var filename = Path.GetFileNameWithoutExtension(config.InputManagedAssemblyPath);
|
|
|
|
|
|
|
|
var infile = Path.Combine(intermediateDirectory, filename + InputExtension);
|
|
|
|
|
|
|
|
return infile;
|
|
|
|
}
|
|
|
|
|
2015-11-17 13:20:34 -08:00
|
|
|
public string DetermineOutputFile(NativeCompileSettings config)
|
2015-11-17 12:10:19 -08:00
|
|
|
{
|
2015-11-17 17:02:16 -08:00
|
|
|
var intermediateDirectory = config.OutputDirectory;
|
2015-11-17 12:10:19 -08:00
|
|
|
|
2015-11-17 17:02:16 -08:00
|
|
|
var filename = Path.GetFileNameWithoutExtension(config.InputManagedAssemblyPath);
|
2015-11-17 12:10:19 -08:00
|
|
|
|
2015-11-17 17:02:16 -08:00
|
|
|
var outfile = Path.Combine(intermediateDirectory, filename);
|
2015-11-17 12:10:19 -08:00
|
|
|
|
2015-11-17 17:02:16 -08:00
|
|
|
return outfile;
|
|
|
|
}
|
2015-11-17 12:10:19 -08:00
|
|
|
}
|
2016-02-03 16:34:03 -08:00
|
|
|
}
|