dotnet-installer/src/dotnet/commands/dotnet-compile-native/IntermediateCompilation/Linux/LinuxCppCompileStep.cs

125 lines
3.7 KiB
C#
Raw Normal View History

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
{
private const string CompilerName = "clang-3.5";
private const string InputExtension = ".cpp";
// TODO: debug/release support
private readonly string [] _cLibsFlags = { "-lm", "-ldl"};
private readonly string [] _cflags = { "-g", "-lstdc++", "-lrt", "-Wno-invalid-offsetof", "-pthread"};
public IEnumerable<string> CompilerArgs { get; set; }
2015-11-17 19:50:19 -08:00
private readonly string[] _ilcSdkLibs =
{
"libbootstrappercpp.a",
"libPortableRuntime.a",
"libSystem.Private.CoreLib.Native.a"
};
private readonly string[] _appdeplibs =
{
"System.Native.a"
};
2015-11-17 17:02:16 -08:00
public LinuxCppCompileStep(NativeCompileSettings config)
{
InitializeArgs(config);
}
2015-11-17 19:50:19 -08:00
public int Invoke()
{
2015-11-17 19:50:19 -08:00
var result = InvokeCompiler();
if (result != 0)
{
Reporter.Error.WriteLine("Compilation of intermediate files failed.");
}
return result;
}
public bool CheckPreReqs()
{
// TODO check for clang
return true;
}
private void InitializeArgs(NativeCompileSettings config)
{
var argsList = new List<string>();
// Flags
argsList.AddRange(_cflags);
var ilcSdkIncPath = Path.Combine(config.IlcSdkPath, "inc");
argsList.Add("-I");
argsList.Add($"{ilcSdkIncPath}");
// Input File
var inCppFile = DetermineInFile(config);
argsList.Add(inCppFile);
// Pass the optional native compiler flags if specified
if (!string.IsNullOrWhiteSpace(config.CppCompilerFlags))
{
argsList.Add(config.CppCompilerFlags);
}
// ILC SDK Libs
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
var baseAppDeplibPath = Path.Combine(config.AppDepSDKPath, "CPPSdk/ubuntu.14.04/x64");
argsList.AddRange(_appdeplibs.Select(lib => Path.Combine(baseAppDeplibPath, lib)));
2015-11-17 19:50:19 -08:00
argsList.AddRange(_cLibsFlags);
// Output
var libOut = DetermineOutputFile(config);
argsList.Add($"-o");
argsList.Add($"{libOut}");
CompilerArgs = argsList;
}
2015-11-17 19:50:19 -08:00
private int InvokeCompiler()
{
var result = Command.Create(CompilerName, CompilerArgs)
.ForwardStdErr()
.ForwardStdOut()
.Execute();
return result.ExitCode;
}
private string DetermineInFile(NativeCompileSettings config)
{
var intermediateDirectory = config.IntermediateDirectory;
var filename = Path.GetFileNameWithoutExtension(config.InputManagedAssemblyPath);
var infile = Path.Combine(intermediateDirectory, filename + InputExtension);
return infile;
}
public string DetermineOutputFile(NativeCompileSettings config)
{
2015-11-17 17:02:16 -08:00
var intermediateDirectory = config.OutputDirectory;
2015-11-17 17:02:16 -08:00
var filename = Path.GetFileNameWithoutExtension(config.InputManagedAssemblyPath);
2015-11-17 17:02:16 -08:00
var outfile = Path.Combine(intermediateDirectory, filename);
2015-11-17 17:02:16 -08:00
return outfile;
}
}
}