working on build scripts

This commit is contained in:
Andrew Stanton-Nurse 2016-02-02 10:04:50 -08:00
parent f273ea4f7d
commit d524732bbb
111 changed files with 3052 additions and 1989 deletions

View file

@ -0,0 +1,401 @@
using Microsoft.DotNet.Cli.Build.Framework;
using Microsoft.Extensions.PlatformAbstractions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using static Microsoft.DotNet.Cli.Build.FS;
using static Microsoft.DotNet.Cli.Build.Framework.BuildHelpers;
namespace Microsoft.DotNet.Cli.Build
{
public class CompileTargets
{
public static readonly string CoreCLRVersion = "1.0.1-rc2-23811";
public static readonly string AppDepSdkVersion = "1.0.5-prerelease-00001";
public static readonly List<string> AssembliesToCrossGen = GetAssembliesToCrossGen();
public static readonly string[] BinariesForCoreHost = new[]
{
"csi",
"csc",
"vbc"
};
public static readonly string[] ProjectsToPublish = new[]
{
"dotnet"
};
public static readonly string[] FilesToClean = new[]
{
"README.md",
"Microsoft.DotNet.Runtime.exe",
"Microsoft.DotNet.Runtime.dll",
"Microsoft.DotNet.Runtime.deps",
"Microsoft.DotNet.Runtime.pdb"
};
public static readonly string[] ProjectsToPack = new[]
{
"Microsoft.DotNet.Cli.Utils",
"Microsoft.DotNet.ProjectModel",
"Microsoft.DotNet.ProjectModel.Loader",
"Microsoft.DotNet.ProjectModel.Workspaces",
"Microsoft.Extensions.DependencyModel",
"Microsoft.Extensions.Testing.Abstractions"
};
[Target(nameof(PrepareTargets.Init), nameof(CompileCoreHost), nameof(CompileStage1), nameof(CompileStage2))]
public static BuildTargetResult Compile(BuildTargetContext c)
{
return c.Success();
}
[Target]
public static BuildTargetResult CompileCoreHost(BuildTargetContext c)
{
// Generate build files
var cmakeOut = Path.Combine(Dirs.Corehost, "cmake");
Rmdir(cmakeOut);
Mkdirp(cmakeOut);
var configuration = (string)c.BuildContext["Configuration"];
// Run the build
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Why does Windows directly call cmake but Linux/Mac calls "build.sh" in the corehost dir?
// See the comment in "src/corehost/build.sh" for details. It doesn't work for some reason.
ExecIn(cmakeOut, "cmake",
Path.Combine(c.BuildContext.BuildDirectory, "src", "corehost"),
"-G",
"Visual Studio 14 2015 Win64");
var pf32 = RuntimeInformation.OSArchitecture == Architecture.X64 ?
Environment.GetEnvironmentVariable("ProgramFiles(x86)") :
Environment.GetEnvironmentVariable("ProgramFiles");
if (configuration.Equals("Release"))
{
// Cmake calls it "RelWithDebInfo" in the generated MSBuild
configuration = "RelWithDebInfo";
}
Exec(Path.Combine(pf32, "MSBuild", "14.0", "Bin", "MSBuild.exe"),
Path.Combine(cmakeOut, "ALL_BUILD.vcxproj"),
$"/p:Configuration={configuration}");
// Copy the output out
File.Copy(Path.Combine(cmakeOut, "cli", "Debug", "corehost.exe"), Path.Combine(Dirs.Corehost, "corehost.exe"), overwrite: true);
File.Copy(Path.Combine(cmakeOut, "cli", "Debug", "corehost.pdb"), Path.Combine(Dirs.Corehost, "corehost.pdb"), overwrite: true);
File.Copy(Path.Combine(cmakeOut, "cli", "dll", "Debug", "hostpolicy.dll"), Path.Combine(Dirs.Corehost, "hostpolicy.dll"), overwrite: true);
File.Copy(Path.Combine(cmakeOut, "cli", "dll", "Debug", "hostpolicy.pdb"), Path.Combine(Dirs.Corehost, "hostpolicy.pdb"), overwrite: true);
}
else
{
ExecIn(cmakeOut, Path.Combine(c.BuildContext.BuildDirectory, "src", "corehost", "build.sh"));
// Copy the output out
File.Copy(Path.Combine(cmakeOut, "cli", "corehost"), Path.Combine(Dirs.Corehost, "corehost"), overwrite: true);
File.Copy(Path.Combine(cmakeOut, "cli", "dll", $"{Constants.DynamicLibPrefix}hostpolicy{Constants.DynamicLibSuffix}"), Path.Combine(Dirs.Corehost, $"{Constants.DynamicLibPrefix}hostpolicy{Constants.DynamicLibSuffix}"), overwrite: true);
}
return c.Success();
}
[Target]
public static BuildTargetResult CompileStage1(BuildTargetContext c)
{
CleanBinObj(c, Path.Combine(c.BuildContext.BuildDirectory, "src"));
CleanBinObj(c, Path.Combine(c.BuildContext.BuildDirectory, "test"));
return CompileStage(c,
dotnet: DotNetCli.Stage0,
outputDir: Dirs.Stage1);
}
[Target]
public static BuildTargetResult CompileStage2(BuildTargetContext c)
{
var configuration = (string)c.BuildContext["Configuration"];
CleanBinObj(c, Path.Combine(c.BuildContext.BuildDirectory, "src"));
CleanBinObj(c, Path.Combine(c.BuildContext.BuildDirectory, "test"));
var result = CompileStage(c,
dotnet: DotNetCli.Stage1,
outputDir: Dirs.Stage2);
if (!result.Success)
{
return result;
}
// Build projects that are packed in NuGet packages, but only on Windows
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var packagingOutputDir = Path.Combine(Dirs.Stage2Compilation, "forPackaging");
Mkdirp(packagingOutputDir);
foreach(var project in ProjectsToPack)
{
// Just build them, we'll pack later
DotNetCli.Stage1.Build(
"--build-base-path",
packagingOutputDir,
"--configuration",
configuration,
Path.Combine(c.BuildContext.BuildDirectory, "src", project))
.Execute()
.EnsureSuccessful();
}
}
return c.Success();
}
private static BuildTargetResult CompileStage(BuildTargetContext c, DotNetCli dotnet, string outputDir)
{
Rmdir(outputDir);
dotnet.SetDotNetHome();
var configuration = (string)c.BuildContext["Configuration"];
var binDir = Path.Combine(outputDir, "bin");
var runtimeOutputDir = Path.Combine(outputDir, "runtime", "coreclr");
Mkdirp(binDir);
Mkdirp(runtimeOutputDir);
foreach (var project in ProjectsToPublish)
{
dotnet.Publish(
"--native-subdirectory",
"--output",
binDir,
"--configuration",
configuration,
Path.Combine(c.BuildContext.BuildDirectory, "src", project))
.Execute()
.EnsureSuccessful();
}
// Publish the runtime
dotnet.Publish(
"--output",
runtimeOutputDir,
"--configuration",
configuration,
Path.Combine(c.BuildContext.BuildDirectory, "src", "Microsoft.DotNet.Runtime"))
.Execute()
.EnsureSuccessful();
// Clean bogus files
foreach (var fileToClean in FilesToClean)
{
var pathToClean = Path.Combine(runtimeOutputDir, fileToClean);
if (File.Exists(pathToClean))
{
File.Delete(pathToClean);
}
}
FixModeFlags(outputDir);
// Copy the whole runtime local to the tools
CopyRecursive(runtimeOutputDir, binDir);
// Copy corehost
File.Copy(Path.Combine(Dirs.Corehost, $"corehost{Constants.ExeSuffix}"), Path.Combine(binDir, $"corehost{Constants.ExeSuffix}"), overwrite: true);
File.Copy(Path.Combine(Dirs.Corehost, $"{Constants.DynamicLibPrefix}hostpolicy{Constants.DynamicLibSuffix}"), Path.Combine(binDir, $"{Constants.DynamicLibPrefix}hostpolicy{Constants.DynamicLibSuffix}"), overwrite: true);
// Corehostify binaries
foreach(var binaryToCorehostify in BinariesForCoreHost)
{
try
{
// Yes, it is .exe even on Linux. This is the managed exe we're working with
File.Copy(Path.Combine(binDir, $"{binaryToCorehostify}.exe"), Path.Combine(binDir, $"{binaryToCorehostify}.dll"));
File.Delete(Path.Combine(binDir, $"{binaryToCorehostify}.exe"));
File.Copy(Path.Combine(binDir, $"corehost{Constants.ExeSuffix}"), Path.Combine(binDir, binaryToCorehostify + Constants.ExeSuffix));
}
catch(Exception ex)
{
return c.Failed($"Failed to corehostify '{binaryToCorehostify}': {ex.ToString()}");
}
}
// Crossgen Roslyn
var result = Crossgen(c, binDir);
if (!result.Success)
{
return result;
}
// Copy AppDeps
result = CopyAppDeps(c, binDir);
if(!result.Success)
{
return result;
}
// Generate .version file
var version = ((BuildVersion)c.BuildContext["BuildVersion"]).SimpleVersion;
var content = $@"{version}{Environment.NewLine}{c.BuildContext["CommitHash"]}{Environment.NewLine}";
File.WriteAllText(Path.Combine(outputDir, ".version"), content);
return c.Success();
}
private static BuildTargetResult CopyAppDeps(BuildTargetContext c, string outputDir)
{
var appDepOutputDir = Path.Combine(outputDir, "appdepsdk");
Rmdir(appDepOutputDir);
Mkdirp(appDepOutputDir);
// Find toolchain package
string packageId;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
packageId = "toolchain.win7-x64.Microsoft.DotNet.AppDep";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
var osname = PlatformServices.Default.Runtime.OperatingSystem;
if (string.Equals(osname, "ubuntu", StringComparison.OrdinalIgnoreCase))
{
packageId = "toolchain.ubuntu.14.04-x64.Microsoft.DotNet.AppDep";
}
else if (string.Equals(osname, "centos", StringComparison.OrdinalIgnoreCase))
{
c.Warn("Native compilation is not yet working on CentOS");
return c.Success();
}
else
{
return c.Failed($"Unknown Linux Distro: {osname}");
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
packageId = "toolchain.osx.10.10-x64.Microsoft.DotNet.AppDep";
}
else
{
return c.Failed("Unsupported OS Platform");
}
var appDepPath = Path.Combine(
Dirs.NuGetPackages,
packageId,
AppDepSdkVersion);
CopyRecursive(appDepPath, appDepOutputDir, overwrite: true);
return c.Success();
}
private static BuildTargetResult Crossgen(BuildTargetContext c, string outputDir)
{
// Check if we need to skip crossgen
if (string.Equals(Environment.GetEnvironmentVariable("DOTNET_BUILD_SKIP_CROSSGEN"), "1"))
{
c.Warn("Skipping crossgen because DOTNET_BUILD_SKIP_CROSSGEN is set");
return c.Success();
}
// Find crossgen
string packageId;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
packageId = "runtime.win7-x64.Microsoft.NETCore.Runtime.CoreCLR";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
var osname = PlatformServices.Default.Runtime.OperatingSystem;
if (string.Equals(osname, "ubuntu", StringComparison.OrdinalIgnoreCase))
{
packageId = "runtime.ubuntu.14.04-x64.Microsoft.NETCore.Runtime.CoreCLR";
}
else if (string.Equals(osname, "centos", StringComparison.OrdinalIgnoreCase))
{
// CentOS runtime is in the runtime.rhel.7-x64... package.
packageId = "runtime.rhel.7-x64.Microsoft.NETCore.Runtime.CoreCLR";
}
else
{
return c.Failed($"Unknown Linux Distro: {osname}");
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
packageId = "runtime.osx.10.10-x64.Microsoft.NETCore.Runtime.CoreCLR";
}
else
{
return c.Failed("Unsupported OS Platform");
}
var crossGenExePath = Path.Combine(
Dirs.NuGetPackages,
packageId,
CoreCLRVersion,
"tools",
$"crossgen{Constants.ExeSuffix}");
// We have to copy crossgen next to mscorlib
var crossgen = Path.Combine(outputDir, $"crossgen{Constants.ExeSuffix}");
File.Copy(crossGenExePath, crossgen, overwrite: true);
Chmod(crossgen, "a+x");
// And if we have mscorlib.ni.dll, we need to rename it to mscorlib.dll
if (File.Exists(Path.Combine(outputDir, "mscorlib.ni.dll")))
{
File.Copy(Path.Combine(outputDir, "mscorlib.ni.dll"), Path.Combine(outputDir, "mscorlib.dll"), overwrite: true);
}
foreach (var assemblyToCrossgen in AssembliesToCrossGen)
{
c.Info($"Crossgenning {assemblyToCrossgen}");
ExecIn(outputDir, crossgen, "-nologo", "-platform_assemblies_paths", outputDir, assemblyToCrossgen);
}
c.Info("Crossgen complete");
// Check if csc/vbc.ni.exe exists, and overwrite the dll with it just in case
if (File.Exists(Path.Combine(outputDir, "csc.ni.exe")) && !File.Exists(Path.Combine(outputDir, "csc.ni.dll")))
{
File.Move(Path.Combine(outputDir, "csc.ni.exe"), Path.Combine(outputDir, "csc.ni.dll"));
}
if (File.Exists(Path.Combine(outputDir, "vbc.ni.exe")) && !File.Exists(Path.Combine(outputDir, "vbc.ni.dll")))
{
File.Move(Path.Combine(outputDir, "vbc.ni.exe"), Path.Combine(outputDir, "vbc.ni.dll"));
}
return c.Success();
}
private static List<string> GetAssembliesToCrossGen()
{
var list = new List<string>
{
"System.Collections.Immutable.dll",
"System.Reflection.Metadata.dll",
"Microsoft.CodeAnalysis.dll",
"Microsoft.CodeAnalysis.CSharp.dll",
"Microsoft.CodeAnalysis.VisualBasic.dll",
"csc.dll",
"vbc.dll"
};
// mscorlib is already crossgenned on Windows
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// mscorlib has to be crossgenned first
list.Insert(0, "mscorlib.dll");
}
return list;
}
}
}

View file

@ -0,0 +1,219 @@
using Microsoft.DotNet.Cli.Build.Framework;
using Microsoft.Extensions.PlatformAbstractions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using static Microsoft.DotNet.Cli.Build.FS;
using static Microsoft.DotNet.Cli.Build.Utils;
using static Microsoft.DotNet.Cli.Build.Framework.BuildHelpers;
namespace Microsoft.DotNet.Cli.Build
{
public class PrepareTargets
{
[Target(nameof(Init), nameof(RestorePackages))]
public static BuildTargetResult Prepare(BuildTargetContext c) => c.Success();
// All major targets will depend on this in order to ensure variables are set up right if they are run independently
[Target(nameof(GenerateVersions), nameof(CheckPrereqs), nameof(LocateStage0))]
public static BuildTargetResult Init(BuildTargetContext c)
{
var runtimeInfo = PlatformServices.Default.Runtime;
var configEnv = Environment.GetEnvironmentVariable("CONFIGURATION");
if(string.IsNullOrEmpty(configEnv))
{
configEnv = "Debug";
}
c.BuildContext["Configuration"] = configEnv;
c.Info($"Building {c.BuildContext["Configuration"]} to: {Dirs.Output}");
c.Info("Build Environment:");
c.Info($" Operating System: {runtimeInfo.OperatingSystem} {runtimeInfo.OperatingSystemVersion}");
c.Info($" Platform: {runtimeInfo.OperatingSystemPlatform}");
return c.Success();
}
[Target]
public static BuildTargetResult GenerateVersions(BuildTargetContext c)
{
var gitResult = Cmd("git", "rev-list", "--count", "HEAD")
.CaptureStdOut()
.Execute();
gitResult.EnsureSuccessful();
var commitCount = int.Parse(gitResult.StdOut);
gitResult = Cmd("git", "rev-parse", "HEAD")
.CaptureStdOut()
.Execute();
gitResult.EnsureSuccessful();
var commitHash = gitResult.StdOut.Trim();
var branchInfo = ReadBranchInfo(c, Path.Combine(c.BuildContext.BuildDirectory, "branchinfo.txt"));
var buildVersion = new BuildVersion()
{
Major = int.Parse(branchInfo["MAJOR_VERSION"]),
Minor = int.Parse(branchInfo["MINOR_VERSION"]),
Patch = int.Parse(branchInfo["PATCH_VERSION"]),
ReleaseSuffix = branchInfo["RELEASE_SUFFIX"],
CommitCount = commitCount
};
c.BuildContext["BuildVersion"] = buildVersion;
c.BuildContext["CommitHash"] = commitHash;
c.Info($"Building Version: {buildVersion.SimpleVersion} (NuGet Packages: {buildVersion.NuGetVersion})");
c.Info($"From Commit: {commitHash}");
return c.Success();
}
[Target]
public static BuildTargetResult LocateStage0(BuildTargetContext c)
{
// We should have been run in the repo root, so locate the stage 0 relative to current directory
var stage0 = DotNetCli.Stage0.BinPath;
if (!Directory.Exists(stage0))
{
return c.Failed($"Stage 0 directory does not exist: {stage0}");
}
// Identify the version
var version = File.ReadAllLines(Path.Combine(stage0, "..", ".version"));
c.Info($"Using Stage 0 Version: {version[1]}");
return c.Success();
}
[Target]
public static BuildTargetResult CheckPrereqs(BuildTargetContext c)
{
try
{
Command.Create("cmake", "--version")
.CaptureStdOut()
.CaptureStdErr()
.Execute();
}
catch (Exception ex)
{
string message = $@"Error running cmake: {ex.Message}
cmake is required to build the native host 'corehost'";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
message += Environment.NewLine + "Download it from https://www.cmake.org";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
message += Environment.NewLine + "Ubuntu: 'sudo apt-get install cmake'";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
message += Environment.NewLine + "OS X w/Homebrew: 'brew install cmake'";
}
return c.Failed(message);
}
return c.Success();
}
[Target]
public static BuildTargetResult CheckPackageCache(BuildTargetContext c)
{
var ciBuild = string.Equals(Environment.GetEnvironmentVariable("CI_BUILD"), "1", StringComparison.Ordinal);
if (ciBuild)
{
// On CI, HOME is redirected under the repo, which gets deleted after every build.
// So make NUGET_PACKAGES outside of the repo.
var nugetPackages = Path.GetFullPath(Path.Combine(c.BuildContext.BuildDirectory, "..", ".nuget", "packages"));
Environment.SetEnvironmentVariable("NUGET_PACKAGES", nugetPackages);
Dirs.NuGetPackages = nugetPackages;
}
// Set the package cache location in NUGET_PACKAGES just to be safe
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("NUGET_PACKAGES")))
{
Environment.SetEnvironmentVariable("NUGET_PACKAGES", Dirs.NuGetPackages);
}
CleanNuGetTempCache();
// Determine cache expiration time
var cacheExpiration = 7 * 24; // cache expiration in hours
var cacheExpirationStr = Environment.GetEnvironmentVariable("NUGET_PACKAGES_CACHE_TIME_LIMIT");
if (!string.IsNullOrEmpty(cacheExpirationStr))
{
cacheExpiration = int.Parse(cacheExpirationStr);
}
if (ciBuild)
{
var cacheTimeFile = Path.Combine(Dirs.NuGetPackages, "packageCacheTime.txt");
DateTime? cacheTime = null;
try
{
// Read the cache file
if(File.Exists(cacheTimeFile))
{
var content = File.ReadAllText(cacheTimeFile);
if(!string.IsNullOrEmpty(content))
{
cacheTime = DateTime.ParseExact("O", content, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
}
}
}
catch(Exception ex)
{
c.Warn($"Error reading NuGet cache time file, leaving the cache alone");
c.Warn($"Error Detail: {ex.ToString()}");
}
if(cacheTime == null || (cacheTime.Value.AddHours(cacheExpiration) < DateTime.UtcNow))
{
// Cache has expired or the status is unknown, clear it and write the file
c.Info("Clearing NuGet cache");
Rmdir(Dirs.NuGetPackages);
Mkdirp(Dirs.NuGetPackages);
File.WriteAllText(cacheTimeFile, DateTime.UtcNow.ToString("O"));
}
}
return c.Success();
}
[Target(nameof(CheckPackageCache))]
public static BuildTargetResult RestorePackages(BuildTargetContext c)
{
var dotnet = DotNetCli.Stage0;
dotnet.Restore().WorkingDirectory(Path.Combine(c.BuildContext.BuildDirectory, "src")).Execute().EnsureSuccessful();
dotnet.Restore().WorkingDirectory(Path.Combine(c.BuildContext.BuildDirectory, "tools")).Execute().EnsureSuccessful();
return c.Success();
}
private static IDictionary<string, string> ReadBranchInfo(BuildTargetContext c, string path)
{
var lines = File.ReadAllLines(path);
var dict = new Dictionary<string, string>();
c.Verbose("Branch Info:");
foreach(var line in lines)
{
if(!line.Trim().StartsWith("#") && !string.IsNullOrWhiteSpace(line))
{
var splat = line.Split(new[] { '=' }, 2);
dict[splat[0]] = splat[1];
c.Verbose($" {splat[0]} = {splat[1]}");
}
}
return dict;
}
}
}

View file

@ -0,0 +1,12 @@
using Microsoft.DotNet.Cli.Build.Framework;
namespace Microsoft.DotNet.Cli.Build
{
public class Program
{
public static int Main(string[] args) => BuildSetup.Create(".NET Core CLI")
.UseStandardGoals()
.UseAllTargetsFromAssembly<Program>()
.Run(args);
}
}

View file

@ -0,0 +1,241 @@
using Microsoft.DotNet.Cli.Build.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using static Microsoft.DotNet.Cli.Build.FS;
using static Microsoft.DotNet.Cli.Build.Utils;
using static Microsoft.DotNet.Cli.Build.Framework.BuildHelpers;
namespace Microsoft.DotNet.Cli.Build
{
public class TestTargets
{
public static readonly string[] TestPackageProjects = new[]
{
"dotnet-hello/v1/dotnet-hello",
"dotnet-hello/v2/dotnet-hello"
};
public static readonly string[] TestProjects = new[]
{
"EndToEnd",
"dotnet-publish.Tests",
"dotnet-compile.Tests",
"dotnet-compile.UnitTests",
"dotnet-build.Tests",
"Microsoft.DotNet.Cli.Utils.Tests",
"Microsoft.DotNet.Compiler.Common.Tests",
"ArgumentForwardingTests"
};
[Target(nameof(PrepareTargets.Init), nameof(SetupTests), nameof(RestoreTests), nameof(BuildTests), nameof(RunTests), nameof(ValidateDependencies))]
public static BuildTargetResult Test(BuildTargetContext c) => c.Success();
[Target(nameof(RestoreTestPrerequisites), nameof(BuildTestPrerequisites))]
public static BuildTargetResult SetupTests(BuildTargetContext c) => c.Success();
[Target]
public static BuildTargetResult RestoreTestPrerequisites(BuildTargetContext c)
{
CleanBinObj(c, Path.Combine(c.BuildContext.BuildDirectory, "src"));
CleanBinObj(c, Path.Combine(c.BuildContext.BuildDirectory, "test"));
CleanNuGetTempCache();
var dotnet = DotNetCli.Stage2;
dotnet.Restore().WorkingDirectory(Path.Combine(c.BuildContext.BuildDirectory, "TestAssets")).Execute().EnsureSuccessful();
// The 'testapp' directory contains intentionally-unresolved dependencies, so don't check for success. Also, suppress the output
dotnet.Restore().WorkingDirectory(Path.Combine(c.BuildContext.BuildDirectory, "testapp")).CaptureStdErr().CaptureStdOut().Execute();
return c.Success();
}
[Target]
public static BuildTargetResult BuildTestPrerequisites(BuildTargetContext c)
{
var dotnet = DotNetCli.Stage2;
Rmdir(Dirs.TestPackages);
Mkdirp(Dirs.TestPackages);
foreach (var relativePath in TestPackageProjects)
{
var fullPath = Path.Combine(c.BuildContext.BuildDirectory, "TestAssets", "TestPackages", relativePath.Replace('/', Path.DirectorySeparatorChar));
c.Info("Packing: {fullPath}");
dotnet.Pack("--output", Dirs.TestPackages)
.WorkingDirectory(fullPath)
.Execute()
.EnsureSuccessful();
}
return c.Success();
}
[Target]
public static BuildTargetResult RestoreTests(BuildTargetContext c)
{
CleanBinObj(c, Path.Combine(c.BuildContext.BuildDirectory, "src"));
CleanBinObj(c, Path.Combine(c.BuildContext.BuildDirectory, "test"));
CleanNuGetTempCache();
DotNetCli.Stage2.Restore("--fallbacksource", Dirs.TestPackages)
.WorkingDirectory(Path.Combine(c.BuildContext.BuildDirectory, "test"))
.Execute()
.EnsureSuccessful();
return c.Success();
}
[Target]
public static BuildTargetResult BuildTests(BuildTargetContext c)
{
var dotnet = DotNetCli.Stage2;
foreach (var testProject in TestProjects)
{
c.Info("Building tests: {project}");
dotnet.Build()
.WorkingDirectory(Path.Combine(c.BuildContext.BuildDirectory, "test", testProject))
.Execute()
.EnsureSuccessful();
}
return c.Success();
}
[Target(nameof(RunXUnitTests), nameof(RunPackageCommandTests))]
public static BuildTargetResult RunTests(BuildTargetContext c) => c.Success();
[Target]
public static BuildTargetResult RunXUnitTests(BuildTargetContext c)
{
// Need to load up the VS Vars
var dotnet = DotNetCli.Stage2;
var vsvars = LoadVsVars();
// Copy the test projects
var testProjectsDir = Path.Combine(Dirs.TestOutput, "TestProjects");
Rmdir(testProjectsDir);
Mkdirp(testProjectsDir);
CopyRecursive(Path.Combine(c.BuildContext.BuildDirectory, "TestAssets", "TestProjects"), testProjectsDir);
// Run the tests and set the VS vars in the environment when running them
var failingTests = new List<string>();
foreach (var project in TestProjects)
{
c.Info("Running tests in: {project}");
var result = dotnet.Test("-xml", $"{project}-testResults.xml", "-notrait", "category=failing")
.WorkingDirectory(Path.Combine(c.BuildContext.BuildDirectory, "test", project))
.Environment(vsvars)
.EnvironmentVariable("PATH", $"{DotNetCli.Stage2.BinPath}{Path.PathSeparator}{Environment.GetEnvironmentVariable("PATH")}")
.Execute();
if (result.ExitCode != 0)
{
failingTests.Add(project);
}
}
if (failingTests.Any())
{
foreach (var project in failingTests)
{
c.Error($"{project} failed");
}
return c.Failed("Tests failed!");
}
return c.Success();
}
[Target]
public static BuildTargetResult RunPackageCommandTests(BuildTargetContext c)
{
var dotnet = DotNetCli.Stage2;
var consumers = Path.Combine(c.BuildContext.BuildDirectory, "test", "PackagedCommands", "Consumers");
// Compile the consumer apps
foreach(var dir in Directory.EnumerateDirectories(consumers))
{
dotnet.Build().WorkingDirectory(dir).Execute().EnsureSuccessful();
}
// Test the apps
foreach(var dir in Directory.EnumerateDirectories(consumers))
{
var result = dotnet.Exec("hello").WorkingDirectory(dir).CaptureStdOut().CaptureStdErr().Execute();
result.EnsureSuccessful();
if(!string.Equals("Hello", result.StdOut.Trim(), StringComparison.Ordinal))
{
var testName = Path.GetFileName(dir);
c.Error($"Packaged Commands Test '{testName}' failed");
c.Error($" Expected 'Hello', but got: '{result.StdOut.Trim()}'");
return c.Failed($"Packaged Commands Test failed '{testName}'");
}
}
return c.Success();
}
[Target]
public static BuildTargetResult ValidateDependencies(BuildTargetContext c)
{
var configuration = (string)c.BuildContext["Configuration"];
var dotnet = DotNetCli.Stage2;
c.Info("Publishing MultiProjectValidator");
dotnet.Publish("--output", Path.Combine(Dirs.Output, "tools"), "--configuration", configuration)
.WorkingDirectory(Path.Combine(c.BuildContext.BuildDirectory, "tools", "MultiProjectValidator"))
.Execute()
.EnsureSuccessful();
var validator = Path.Combine(Dirs.Output, "tools", $"pjvalidate{Constants.ExeSuffix}");
Cmd(validator, Path.Combine(c.BuildContext.BuildDirectory, "src"))
.Execute();
return c.Success();
}
private static Dictionary<string, string> LoadVsVars()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return new Dictionary<string, string>();
}
var vsvarsPath = Path.GetFullPath(Path.Combine(Environment.GetEnvironmentVariable("VS140COMNTOOLS"), "..", "..", "VC"));
// Write a temp batch file because that seems to be the easiest way to do this (argument parsing is hard)
var temp = Path.Combine(Path.GetTempPath(), $"{Path.GetRandomFileName()}.cmd");
File.WriteAllText(temp, $@"@echo off
cd {vsvarsPath}
call vcvarsall.bat x64
set");
CommandResult result;
try
{
result = Cmd(Environment.GetEnvironmentVariable("COMSPEC"), "/c", temp)
.WorkingDirectory(vsvarsPath)
.CaptureStdOut()
.Execute();
}
finally
{
if (File.Exists(temp))
{
File.Delete(temp);
}
}
result.EnsureSuccessful();
var vars = new Dictionary<string, string>();
foreach (var line in result.StdOut.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
{
var splat = line.Split(new[] { '=' }, 2);
vars[splat[0]] = splat[1];
}
return vars;
}
}
}

View file

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.DotNet.Cli.Build
{
public class BuildVersion
{
public int Major { get; set; }
public int Minor { get; set; }
public int Patch { get; set; }
public int CommitCount { get; set; }
public string CommitCountString => CommitCount.ToString("000000");
public string ReleaseSuffix { get; set; }
public string SimpleVersion => $"{Major}.{Minor}.{Patch}.{CommitCount}";
public string VersionSuffix => $"{ReleaseSuffix}-{CommitCount}";
public string NuGetVersion => $"{Major}.{Minor}.{Patch}-{VersionSuffix}";
public string GenerateMsiVersion()
{
// MSI versioning
// Encode the CLI version to fit into the MSI versioning scheme - https://msdn.microsoft.com/en-us/library/windows/desktop/aa370859(v=vs.85).aspx
// MSI versions are 3 part
// major.minor.build
// Size(bits) of each part 8 8 16
// So we have 32 bits to encode the CLI version
// Starting with most significant bit this how the CLI version is going to be encoded as MSI Version
// CLI major -> 6 bits
// CLI minor -> 6 bits
// CLI patch -> 6 bits
// CLI commitcount -> 14 bits
var major = Major << 26;
var minor = Minor << 20;
var patch = Patch << 14;
var msiVersionNumber = major | minor | patch | CommitCount;
var msiMajor = (msiVersionNumber >> 24) & 0xFF;
var msiMinor = (msiVersionNumber >> 16) & 0xFF;
var msiBuild = msiVersionNumber & 0xFFFF;
return $"{msiMajor}.{msiMinor}.{msiBuild}";
}
}
}

View file

@ -0,0 +1,38 @@
using Microsoft.Extensions.PlatformAbstractions;
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Microsoft.DotNet.Cli.Build
{
public static class Dirs
{
public static readonly string Output = Path.Combine(
Directory.GetCurrentDirectory(),
"artifacts",
PlatformServices.Default.Runtime.GetRuntimeIdentifier());
public static readonly string Packages = Path.Combine(Output, "packages");
public static readonly string Stage1 = Path.Combine(Output, "stage1");
public static readonly string Stage1Compilation = Path.Combine(Output, "stage1compilation");
public static readonly string Stage2 = Path.Combine(Output, "stage2");
public static readonly string Stage2Compilation = Path.Combine(Output, "stage2compilation");
public static readonly string Corehost = Path.Combine(Output, "corehost");
public static readonly string TestOutput = Path.Combine(Output, "tests");
public static readonly string TestPackages = Path.Combine(TestOutput, "packages");
public static readonly string OSXReferenceAssembliesPath = "/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/xbuild-frameworks";
public static readonly string UsrLocalReferenceAssembliesPath = "/usr/local/lib/mono/xbuild-frameworks";
public static readonly string UsrReferenceAssembliesPath = "/usr/lib/mono/xbuild-frameworks";
public static string NuGetPackages = Environment.GetEnvironmentVariable("NUGET_PACKAGES") ?? GetNuGetPackagesDir();
private static string GetNuGetPackagesDir()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), ".nuget", "packages");
}
return Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".nuget", "packages");
}
}
}

View file

@ -0,0 +1,51 @@
using System.IO;
using System.Linq;
using System;
using System.Runtime.InteropServices;
using Microsoft.DotNet.Cli.Build.Framework;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.DotNet.Cli.Build
{
internal class DotNetCli
{
public static readonly DotNetCli Stage0 = new DotNetCli(GetStage0Path());
public static readonly DotNetCli Stage1 = new DotNetCli(Path.Combine(Dirs.Stage1, "bin"));
public static readonly DotNetCli Stage2 = new DotNetCli(Path.Combine(Dirs.Stage2, "bin"));
public string BinPath { get; }
public DotNetCli(string binPath)
{
BinPath = binPath;
}
public void SetDotNetHome()
{
Environment.SetEnvironmentVariable("DOTNET_HOME", Path.GetDirectoryName(BinPath));
}
public Command Exec(string command, params string[] args)
{
return Command.Create(Path.Combine(BinPath, $"dotnet{Constants.ExeSuffix}"), Enumerable.Concat(new[] { command }, args));
}
public Command Restore(params string[] args) => Exec("restore", args);
public Command Build(params string[] args) => Exec("build", args);
public Command Pack(params string[] args) => Exec("pack", args);
public Command Test(params string[] args) => Exec("test", args);
public Command Publish(params string[] args) => Exec("publish", args);
private static string GetStage0Path()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Path.Combine(Directory.GetCurrentDirectory(), ".dotnet_stage0", PlatformServices.Default.Runtime.OperatingSystemPlatform.ToString(), "cli", "bin");
}
else
{
return Path.Combine(Directory.GetCurrentDirectory(), ".dotnet_stage0", PlatformServices.Default.Runtime.OperatingSystemPlatform.ToString(), "share", "dotnet", "cli", "bin");
}
}
}
}

View file

@ -0,0 +1,114 @@
using System.IO;
using System.Runtime.InteropServices;
using System;
using Microsoft.DotNet.Cli.Build.Framework;
using static Microsoft.DotNet.Cli.Build.Framework.BuildHelpers;
namespace Microsoft.DotNet.Cli.Build
{
public static class FS
{
public static void Mkdirp(string dir)
{
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
}
public static void Rm(string file)
{
if(File.Exists(file))
{
File.Delete(file);
}
}
public static void Rmdir(string dir)
{
if(Directory.Exists(dir))
{
Directory.Delete(dir, recursive: true);
}
}
public static void Chmod(string file, string mode, bool recursive = false)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (recursive)
{
Command.Create("chmod", "-R", mode, file).Execute().EnsureSuccessful();
}
else
{
Command.Create("chmod", mode, file).Execute().EnsureSuccessful();
}
}
}
public static void ChmodAll(string searchDir, string pattern, string mode)
{
Exec("find", searchDir, "-type", "f", "-name", pattern, "-exec", "chmod", mode, "{}", ";");
}
public static void FixModeFlags(string dir)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Managed code doesn't need 'x'
ChmodAll(dir, "*.dll", "644");
ChmodAll(dir, "*.exe", "644");
// Generally, dylibs and sos have 'x' (no idea if it's required ;))
// (No need to condition this on OS since there shouldn't be any dylibs on Linux,
// but even if they are we may as well set their mode flags :))
ChmodAll(dir, "*.dylib", "755");
ChmodAll(dir, "*.so", "755");
// Executables (those without dots) are executable :)
Exec("find", dir, "-type", "f", "!", "-name", "*.*", "-exec", "chmod", "755", "{}", ";");
}
}
public static void CopyRecursive(string sourceDirectory, string destinationDirectory, bool overwrite = false)
{
Mkdirp(destinationDirectory);
foreach(var dir in Directory.EnumerateDirectories(sourceDirectory))
{
CopyRecursive(dir, Path.Combine(destinationDirectory, Path.GetFileName(dir)), overwrite);
}
foreach(var file in Directory.EnumerateFiles(sourceDirectory))
{
var dest = Path.Combine(destinationDirectory, Path.GetFileName(file));
if (!File.Exists(dest) || overwrite)
{
// We say overwrite true, because we only get here if the file didn't exist (thus it doesn't matter) or we
// wanted to overwrite :)
File.Copy(file, dest, overwrite: true);
}
}
}
public static void CleanBinObj(BuildTargetContext c, string dir)
{
dir = dir ?? c.BuildContext.BuildDirectory;
foreach(var candidate in Directory.EnumerateDirectories(dir))
{
if (string.Equals(Path.GetFileName(candidate), "bin") ||
string.Equals(Path.GetFileName(candidate), "obj"))
{
Directory.Delete(candidate, recursive: true);
}
else
{
CleanBinObj(c, candidate);
}
}
}
}
}

View file

@ -0,0 +1,38 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Microsoft.DotNet.Cli.Build
{
public static class Utils
{
public static void CleanNuGetTempCache()
{
// Clean NuGet Temp Cache on Linux (seeing some issues on Linux)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && Directory.Exists("/tmp/NuGet"))
{
Directory.Delete("/tmp/NuGet", recursive: true);
}
}
public static string GetOSName()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return "win";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return "osx";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
throw new NotImplementedException();
}
else
{
throw new PlatformNotSupportedException();
}
}
}
}

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>d7b9695d-23eb-4ea8-b8ab-707a0092e1d5</ProjectGuid>
<RootNamespace>Microsoft.DotNet.Cli.Build</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View file

@ -0,0 +1,18 @@
{
"version": "1.0.0-*",
"description": "Build scripts for dotnet-cli",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"NETStandard.Library": "1.0.0-rc2-23811",
"System.IO.Compression.ZipFile": "4.0.1-rc2-23811",
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-rc2-16537",
"Microsoft.DotNet.Cli.Build.Framework": "1.0.0-*"
},
"frameworks": {
"dnxcore50": { }
}
}