2015-12-17 13:28:11 +00:00
|
|
|
|
// 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.CommandLine;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
using Microsoft.DotNet.Cli.Compiler.Common;
|
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
|
using Microsoft.DotNet.ProjectModel;
|
2016-02-08 10:03:27 +00:00
|
|
|
|
using NuGet.Frameworks;
|
2015-12-17 13:28:11 +00:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Compiler.Fsc
|
|
|
|
|
{
|
2016-01-31 05:47:50 +00:00
|
|
|
|
public class CompileFscCommand
|
2015-12-17 13:28:11 +00:00
|
|
|
|
{
|
|
|
|
|
private const int ExitFailed = 1;
|
|
|
|
|
|
2016-01-31 05:47:50 +00:00
|
|
|
|
public static int Run(string[] args)
|
2015-12-17 13:28:11 +00:00
|
|
|
|
{
|
|
|
|
|
DebugHelper.HandleDebugSwitch(ref args);
|
|
|
|
|
|
|
|
|
|
CommonCompilerOptions commonOptions = null;
|
|
|
|
|
AssemblyInfoOptions assemblyInfoOptions = null;
|
|
|
|
|
string tempOutDir = null;
|
|
|
|
|
IReadOnlyList<string> references = Array.Empty<string>();
|
|
|
|
|
IReadOnlyList<string> resources = Array.Empty<string>();
|
|
|
|
|
IReadOnlyList<string> sources = Array.Empty<string>();
|
|
|
|
|
string outputName = null;
|
|
|
|
|
var help = false;
|
|
|
|
|
var returnCode = 0;
|
|
|
|
|
string helpText = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ArgumentSyntax.Parse(args, syntax =>
|
|
|
|
|
{
|
|
|
|
|
syntax.HandleHelp = false;
|
|
|
|
|
syntax.HandleErrors = false;
|
|
|
|
|
|
|
|
|
|
commonOptions = CommonCompilerOptionsExtensions.Parse(syntax);
|
|
|
|
|
|
|
|
|
|
assemblyInfoOptions = AssemblyInfoOptions.Parse(syntax);
|
|
|
|
|
|
|
|
|
|
syntax.DefineOption("temp-output", ref tempOutDir, "Compilation temporary directory");
|
|
|
|
|
|
|
|
|
|
syntax.DefineOption("out", ref outputName, "Name of the output assembly");
|
|
|
|
|
|
|
|
|
|
syntax.DefineOptionList("reference", ref references, "Path to a compiler metadata reference");
|
|
|
|
|
|
|
|
|
|
syntax.DefineOptionList("resource", ref resources, "Resources to embed");
|
|
|
|
|
|
|
|
|
|
syntax.DefineOption("h|help", ref help, "Help for compile native.");
|
|
|
|
|
|
|
|
|
|
syntax.DefineParameterList("source-files", ref sources, "Compilation sources");
|
|
|
|
|
|
|
|
|
|
helpText = syntax.GetHelpText();
|
|
|
|
|
|
|
|
|
|
if (tempOutDir == null)
|
|
|
|
|
{
|
|
|
|
|
syntax.ReportError("Option '--temp-output' is required");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentSyntaxException exception)
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine(exception.Message);
|
|
|
|
|
help = true;
|
|
|
|
|
returnCode = ExitFailed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (help)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(helpText);
|
|
|
|
|
|
|
|
|
|
return returnCode;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-08 19:03:14 +00:00
|
|
|
|
var translated = TranslateCommonOptions(commonOptions, outputName);
|
2015-12-17 13:28:11 +00:00
|
|
|
|
|
|
|
|
|
var allArgs = new List<string>(translated);
|
|
|
|
|
allArgs.AddRange(GetDefaultOptions());
|
|
|
|
|
|
|
|
|
|
// Generate assembly info
|
|
|
|
|
var assemblyInfo = Path.Combine(tempOutDir, $"dotnet-compile.assemblyinfo.fs");
|
|
|
|
|
File.WriteAllText(assemblyInfo, AssemblyInfoFileGenerator.GenerateFSharp(assemblyInfoOptions));
|
2016-01-22 22:04:04 +00:00
|
|
|
|
allArgs.Add($"{assemblyInfo}");
|
2015-12-17 13:28:11 +00:00
|
|
|
|
|
2016-03-16 01:28:47 +00:00
|
|
|
|
bool targetNetCore =
|
|
|
|
|
commonOptions.Defines.Contains("DNXCORE50") ||
|
|
|
|
|
commonOptions.Defines.Where(d => d.StartsWith("NETSTANDARDAPP1_")).Any() ||
|
|
|
|
|
commonOptions.Defines.Where(d => d.StartsWith("NETSTANDARD1_")).Any();
|
2016-02-11 14:42:20 +00:00
|
|
|
|
|
2015-12-17 13:28:11 +00:00
|
|
|
|
//HACK fsc raise error FS0208 if target exe doesnt have extension .exe
|
2016-02-11 14:42:20 +00:00
|
|
|
|
bool hackFS0208 = targetNetCore && commonOptions.EmitEntryPoint == true;
|
2015-12-17 13:28:11 +00:00
|
|
|
|
string originalOutputName = outputName;
|
|
|
|
|
|
|
|
|
|
if (outputName != null)
|
|
|
|
|
{
|
|
|
|
|
if (hackFS0208)
|
|
|
|
|
{
|
|
|
|
|
outputName = Path.ChangeExtension(outputName, ".exe");
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 10:04:53 +00:00
|
|
|
|
allArgs.Add($"--out:{outputName}");
|
2015-12-17 13:28:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 10:03:27 +00:00
|
|
|
|
//set target framework
|
2016-02-11 14:42:20 +00:00
|
|
|
|
if (targetNetCore)
|
2016-02-08 10:03:27 +00:00
|
|
|
|
{
|
|
|
|
|
allArgs.Add("--targetprofile:netcore");
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-18 00:32:33 +00:00
|
|
|
|
allArgs.AddRange(references.Select(r => $"-r:{r}"));
|
|
|
|
|
allArgs.AddRange(resources.Select(resource => $"--resource:{resource}"));
|
|
|
|
|
allArgs.AddRange(sources.Select(s => $"{s}"));
|
2015-12-17 13:28:11 +00:00
|
|
|
|
|
|
|
|
|
var rsp = Path.Combine(tempOutDir, "dotnet-compile-fsc.rsp");
|
|
|
|
|
File.WriteAllLines(rsp, allArgs, Encoding.UTF8);
|
|
|
|
|
|
|
|
|
|
// Execute FSC!
|
2016-01-22 22:04:04 +00:00
|
|
|
|
var result = RunFsc(allArgs)
|
2015-12-17 13:28:11 +00:00
|
|
|
|
.ForwardStdErr()
|
|
|
|
|
.ForwardStdOut()
|
|
|
|
|
.Execute();
|
|
|
|
|
|
2016-02-11 14:42:20 +00:00
|
|
|
|
bool successFsc = result.ExitCode == 0;
|
|
|
|
|
|
2015-12-17 13:28:11 +00:00
|
|
|
|
if (hackFS0208 && File.Exists(outputName))
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(originalOutputName))
|
|
|
|
|
File.Delete(originalOutputName);
|
|
|
|
|
File.Move(outputName, originalOutputName);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 10:12:30 +00:00
|
|
|
|
//HACK dotnet build require a pdb (crash without), fsc atm cant generate a portable pdb, so an empty pdb is created
|
|
|
|
|
string pdbPath = Path.ChangeExtension(outputName, ".pdb");
|
2016-02-11 14:42:20 +00:00
|
|
|
|
if (successFsc && !File.Exists(pdbPath))
|
2016-02-08 10:12:30 +00:00
|
|
|
|
{
|
|
|
|
|
File.WriteAllBytes(pdbPath, Array.Empty<byte>());
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-17 13:28:11 +00:00
|
|
|
|
return result.ExitCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Review if this is the place for default options
|
|
|
|
|
private static IEnumerable<string> GetDefaultOptions()
|
|
|
|
|
{
|
|
|
|
|
var args = new List<string>()
|
|
|
|
|
{
|
|
|
|
|
"--noframework",
|
|
|
|
|
"--nologo",
|
|
|
|
|
"--simpleresolution"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
|
args.Add("--debug:full");
|
|
|
|
|
else
|
|
|
|
|
args.Add("--debug-");
|
|
|
|
|
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-08 19:03:14 +00:00
|
|
|
|
private static IEnumerable<string> TranslateCommonOptions(CommonCompilerOptions options, string outputName)
|
2015-12-17 13:28:11 +00:00
|
|
|
|
{
|
|
|
|
|
List<string> commonArgs = new List<string>();
|
|
|
|
|
|
|
|
|
|
if (options.Defines != null)
|
|
|
|
|
{
|
|
|
|
|
commonArgs.AddRange(options.Defines.Select(def => $"-d:{def}"));
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 10:04:53 +00:00
|
|
|
|
if (options.SuppressWarnings != null)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 14:34:04 +00:00
|
|
|
|
// Additional arguments are added verbatim
|
|
|
|
|
if (options.AdditionalArguments != null)
|
|
|
|
|
{
|
|
|
|
|
commonArgs.AddRange(options.AdditionalArguments);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 10:04:53 +00:00
|
|
|
|
if (options.LanguageVersion != null)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-17 13:28:11 +00:00
|
|
|
|
if (options.Platform != null)
|
|
|
|
|
{
|
|
|
|
|
commonArgs.Add($"--platform:{options.Platform}");
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 10:04:53 +00:00
|
|
|
|
if (options.AllowUnsafe == true)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-17 13:28:11 +00:00
|
|
|
|
if (options.WarningsAsErrors == true)
|
|
|
|
|
{
|
|
|
|
|
commonArgs.Add("--warnaserror");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.Optimize == true)
|
|
|
|
|
{
|
|
|
|
|
commonArgs.Add("--optimize");
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 10:04:53 +00:00
|
|
|
|
if (options.KeyFile != null)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.DelaySign == true)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.PublicSign == true)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.GenerateXmlDocumentation == true)
|
2016-01-08 19:03:14 +00:00
|
|
|
|
{
|
|
|
|
|
commonArgs.Add($"--doc:{Path.ChangeExtension(outputName, "xml")}");
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-17 13:28:11 +00:00
|
|
|
|
if (options.EmitEntryPoint != true)
|
|
|
|
|
{
|
|
|
|
|
commonArgs.Add("--target:library");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
commonArgs.Add("--target:exe");
|
|
|
|
|
|
|
|
|
|
//HACK we need default.win32manifest for exe
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
|
{
|
|
|
|
|
var win32manifestPath = Path.Combine(AppContext.BaseDirectory, "default.win32manifest");
|
2016-02-08 10:04:53 +00:00
|
|
|
|
commonArgs.Add($"--win32manifest:{win32manifestPath}");
|
2015-12-17 13:28:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-08 19:03:14 +00:00
|
|
|
|
|
2015-12-17 13:28:11 +00:00
|
|
|
|
return commonArgs;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 22:04:04 +00:00
|
|
|
|
private static Command RunFsc(List<string> fscArgs)
|
2015-12-17 13:28:11 +00:00
|
|
|
|
{
|
|
|
|
|
var corerun = Path.Combine(AppContext.BaseDirectory, Constants.HostExecutableName);
|
|
|
|
|
var fscExe = Path.Combine(AppContext.BaseDirectory, "fsc.exe");
|
2016-01-22 22:04:04 +00:00
|
|
|
|
|
|
|
|
|
List<string> args = new List<string>();
|
|
|
|
|
args.Add(fscExe);
|
|
|
|
|
args.AddRange(fscArgs);
|
|
|
|
|
|
|
|
|
|
return Command.Create(corerun, args.ToArray());
|
2015-12-17 13:28:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|