Skip to content

Commit

Permalink
Merge pull request #907 from DustinCampbell/use-compile-target
Browse files Browse the repository at this point in the history
MSBuild Update
  • Loading branch information
DustinCampbell authored Jul 6, 2017
2 parents 16028f2 + 07e7817 commit a881405
Show file tree
Hide file tree
Showing 33 changed files with 342 additions and 146 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ scripts/Omnisharp*
# Build folder
.dotnet/
.dotnet-legacy/
.dotnet-future/
tools/*
!tools/packages.config

Expand Down
29 changes: 20 additions & 9 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,28 @@ Task("SetupMSBuild")

CopyDirectory(msbuildNetCoreAppInstallFolder, msbuildNetCoreAppFolder);

// Finally, copy Microsoft.CSharp.Core.targets from Microsoft.Net.Compilers
var csharpTargetsName = "Microsoft.CSharp.Core.targets";
var csharpTargetsPath = CombinePaths(env.Folders.Tools, "Microsoft.Net.Compilers", "tools", csharpTargetsName);
// Finally, copy Microsoft.Net.Compilers
var roslynFolder = CombinePaths(env.Folders.Tools, "Microsoft.Net.Compilers", "tools");
var roslynNet46Folder = CombinePaths(msbuildNet46Folder, "Roslyn");
var roslynNetCoreAppFolder = CombinePaths(msbuildNetCoreAppFolder, "Roslyn");

var csharpTargetsNet46Folder = CombinePaths(msbuildNet46Folder, "Roslyn");
var csharpTargetsNetCoreAppFolder = CombinePaths(msbuildNetCoreAppFolder, "Roslyn");
CreateDirectory(roslynNet46Folder);
CreateDirectory(roslynNetCoreAppFolder);

CreateDirectory(csharpTargetsNet46Folder);
CreateDirectory(csharpTargetsNetCoreAppFolder);
CopyDirectory(roslynFolder, roslynNet46Folder);
CopyDirectory(roslynFolder, roslynNetCoreAppFolder);

CopyFile(csharpTargetsPath, CombinePaths(csharpTargetsNet46Folder, csharpTargetsName));
CopyFile(csharpTargetsPath, CombinePaths(csharpTargetsNetCoreAppFolder,csharpTargetsName));
// Delete unnecessary files
foreach (var folder in new[] { roslynNet46Folder, roslynNetCoreAppFolder })
{
DeleteFile(CombinePaths(folder, "Microsoft.CodeAnalysis.VisualBasic.dll"));
DeleteFile(CombinePaths(folder, "Microsoft.VisualBasic.Core.targets"));
DeleteFile(CombinePaths(folder, "VBCSCompiler.exe"));
DeleteFile(CombinePaths(folder, "VBCSCompiler.exe.config"));
DeleteFile(CombinePaths(folder, "vbc.exe"));
DeleteFile(CombinePaths(folder, "vbc.exe.config"));
DeleteFile(CombinePaths(folder, "vbc.rsp"));
}
});

/// <summary>
Expand Down Expand Up @@ -318,6 +328,7 @@ Task("BuildEnvironment")
version: buildPlan.LegacyDotNetVersion,
installFolder: env.Folders.LegacyDotNetSdk);


// Capture 'dotnet --info' output and parse out RID.
var lines = new List<string>();

Expand Down
5 changes: 3 additions & 2 deletions build.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"DotNetInstallScriptURL": "https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0/scripts/obtain",
"DotNetChannel": "preview",
"DotNetVersion": "1.0.1",
"DotNetVersion": "1.0.4",
"LegacyDotNetVersion": "1.0.0-preview2-1-003177",
"DownloadURL": "https://omnisharpdownload.blob.core.windows.net/ext",
"MSBuildRuntimeForMono": "Microsoft.Build.Runtime.Mono-alpha4.zip",
Expand All @@ -25,7 +25,8 @@
"MSTestProject",
"ProjectAndSolution",
"ProjectAndSolutionWithProjectSection",
"TwoProjectsWithSolution"
"TwoProjectsWithSolution",
"ProjectWithGeneratedFile"
],
"LegacyTestAssets": [
"BasicTestProjectSample01",
Expand Down
2 changes: 1 addition & 1 deletion msbuild.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

SDK_DIR="$(cd "$(dirname "$0")"/.dotnet/sdk/1.0.1/ && pwd -P)"
SDK_DIR="$(cd "$(dirname "$0")"/.dotnet/sdk/1.0.4/ && pwd -P)"

echo $SDK_DIR

Expand Down
2 changes: 1 addition & 1 deletion src/OmniSharp.MSBuild/MSBuildEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void Initialize(ILogger logger)

// If MSBuild can locate VS 2017 and set up a build environment, we don't need to do anything.
// MSBuild will take care of itself.
if (MSBuildHelpers.TryGetVisualStudioBuildEnvironment())
if (MSBuildHelpers.CanInitializeVisualStudioBuildEnvironment())
{
logger.LogInformation("MSBuild will use local Visual Studio installation.");
s_usingVisualStudio = true;
Expand Down
29 changes: 21 additions & 8 deletions src/OmniSharp.MSBuild/MSBuildHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ public static class MSBuildHelpers

private static Type s_BuildEnvironmentHelperType;
private static Type s_BuildEnvironmentType;
private static Type s_VisualStudioLocationHelperType;

static MSBuildHelpers()
{
s_MicrosoftBuildAssembly = Assembly.Load(new AssemblyName("Microsoft.Build"));

s_BuildEnvironmentHelperType = s_MicrosoftBuildAssembly.GetType("Microsoft.Build.Shared.BuildEnvironmentHelper");
s_BuildEnvironmentType = s_MicrosoftBuildAssembly.GetType("Microsoft.Build.Shared.BuildEnvironment");
s_VisualStudioLocationHelperType = s_MicrosoftBuildAssembly.GetType("Microsoft.Build.Shared.VisualStudioLocationHelper");
}

public static string GetBuildEnvironmentInfo()
Expand Down Expand Up @@ -57,24 +59,35 @@ private static string DumpBuildEnvironment(object buildEnvironment)

private static void AppendPropertyValue(StringBuilder builder, string name, object instance, Type type, BindingFlags bindingFlags)
{
var propInfo = type.GetProperty(name, bindingFlags);
var propValue = propInfo.GetMethod.Invoke(instance, null);
var propValue = GetPropertyValue(name, instance, type, bindingFlags);
builder.AppendLine($"{name}: {propValue}");
}

public static bool TryGetVisualStudioBuildEnvironment()
private static object GetPropertyValue(string name, object instance, Type type, BindingFlags bindingFlags)
{
var propInfo = type.GetProperty(name, bindingFlags);
return propInfo.GetMethod.Invoke(instance, null);
}

public static bool CanInitializeVisualStudioBuildEnvironment()
{
if (!PlatformHelper.IsWindows)
{
return false;
}

// Call Microsoft.Build.Shared.BuildEnvironmentHelper.TryFromSetupApi(...), which attempts
// to compute a build environment by looking for VS 2017.
var tryFromSetupApiMethod = s_BuildEnvironmentHelperType.GetMethod("TryFromSetupApi", BindingFlags.NonPublic | BindingFlags.Static);
var buildEnvironment = tryFromSetupApiMethod.Invoke(null, null);
// Call Microsoft.Build.Shared.BuildEnvironmentHelper.Initialze(...), which attempts to compute a build environment..
var initializeMethod = s_BuildEnvironmentHelperType.GetMethod("Initialize", BindingFlags.NonPublic | BindingFlags.Static);
var buildEnvironment = initializeMethod.Invoke(null, null);

if (buildEnvironment == null)
{
return false;
}

var mode = GetPropertyValue("Mode", buildEnvironment, s_BuildEnvironmentType, BindingFlags.NonPublic | BindingFlags.Instance);

return buildEnvironment != null;
return mode?.ToString() == "VisualStudio";
}
}
}
8 changes: 4 additions & 4 deletions src/OmniSharp.MSBuild/OmniSharp.MSBuild.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options" Version="1.1.0" />
<PackageReference Include="Microsoft.Build" Version="15.3.0-preview-000117-01" />
<PackageReference Include="Microsoft.Build.Framework" Version="15.3.0-preview-000117-01" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="15.3.0-preview-000117-01" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.3.0-preview-000117-01" />
<PackageReference Include="Microsoft.Build" Version="15.3.0-preview-000388-01" />
<PackageReference Include="Microsoft.Build.Framework" Version="15.3.0-preview-000388-01" />
<PackageReference Include="Microsoft.Build.Tasks.Core" Version="15.3.0-preview-000388-01" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="15.3.0-preview-000388-01" />
<PackageReference Include="NuGet.Packaging.Core" Version="4.0.0" />
<PackageReference Include="NuGet.ProjectModel" Version="4.0.0" />
<PackageReference Include="NuGet.Versioning" Version="4.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ private static class PropertyNames
public const string OutputPath = nameof(OutputPath);
public const string Platform = nameof(Platform);
public const string ProjectAssetsFile = nameof(ProjectAssetsFile);
public const string ProvideCommandLineInvocation = nameof(ProvideCommandLineInvocation);
public const string ProjectGuid = nameof(ProjectGuid);
public const string ProjectName = nameof(ProjectName);
public const string _ResolveReferenceDependencies = nameof(_ResolveReferenceDependencies);
public const string SignAssembly = nameof(SignAssembly);
public const string SkipCompilerExecution = nameof(SkipCompilerExecution);
public const string SolutionDir = nameof(SolutionDir);
public const string TargetFramework = nameof(TargetFramework);
public const string TargetFrameworkMoniker = nameof(TargetFrameworkMoniker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public partial class ProjectFileInfo
{
private static class TargetNames
{
public const string Compile = nameof(Compile);
public const string ResolveReferences = nameof(ResolveReferences);
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private static ProjectInstance LoadProject(
}

var projectInstance = project.CreateProjectInstance();
var buildResult = projectInstance.Build(TargetNames.ResolveReferences,
var buildResult = projectInstance.Build(TargetNames.Compile,
new[] { new MSBuildLogForwarder(logger, diagnostics) });

return buildResult
Expand Down Expand Up @@ -214,7 +214,12 @@ private static Dictionary<string, string> GetGlobalProperties(MSBuildOptions opt
{ PropertyNames.DesignTimeBuild, "true" },
{ PropertyNames.BuildProjectReferences, "false" },
{ PropertyNames._ResolveReferenceDependencies, "true" },
{ PropertyNames.SolutionDir, solutionDirectory + Path.DirectorySeparatorChar }
{ PropertyNames.SolutionDir, solutionDirectory + Path.DirectorySeparatorChar },

// This properties allow the design-time build to handle the Compile target without actually invoking the compiler.
// See https://github.com/dotnet/roslyn/pull/4604 for details.
{ PropertyNames.ProvideCommandLineInvocation, "true" },
{ PropertyNames.SkipCompilerExecution, "true" }
};

globalProperties.AddPropertyIfNeeded(
Expand Down
58 changes: 38 additions & 20 deletions src/OmniSharp/app.config
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.3.0.0" newVersion="2.3.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.CSharp" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.3.0.0" newVersion="2.3.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.3.0.0" newVersion="2.3.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.3.0.0" newVersion="2.3.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.CSharp" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.3.0.0" newVersion="2.3.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.CodeAnalysis.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-2.3.0.0" newVersion="2.3.0.0"/>
</dependentAssembly>

<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="2.0.0.0-99.0.0.0" newVersion="15.1.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="2.0.0.0-99.0.0.0" newVersion="15.1.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Tasks.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="2.0.0.0-99.0.0.0" newVersion="15.1.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Utilities.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="2.0.0.0-99.0.0.0" newVersion="15.1.0.0"/>
</dependentAssembly>

</assemblyBinding>
</runtime>
</configuration>
1 change: 1 addition & 0 deletions test-assets/test-projects/HelloWorld/HelloWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<OutputType>exe</OutputType>
<DefineConstants>NETCOREAPP;$(DefineConstants)</DefineConstants>
<PackageTargetFallback>dnxcore50</PackageTargetFallback>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
Expand Down
13 changes: 13 additions & 0 deletions test-assets/test-projects/ProjectWithGeneratedFile/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
var t = new TestNs.TestClass();
Console.WriteLine("Hello World!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<XsdFile Include="**\*.xsd">
<Generator>MSBuild:CompileGeneratedFiles</Generator>
</XsdFile>
</ItemGroup>

<Target Name="CompileGeneratedFiles"
BeforeTargets="CoreCompile"
DependsOnTargets="UpdateGeneratedFiles"
Condition="'@(XsdFile)' != ''" >
<ItemGroup>
<Compile Include="$(IntermediateOutputPath)%(XsdFile.Filename).g.cs" />
<FileWrites Include="$(IntermediateOutputPath)%(XsdFile.Filename).g.cs" />
</ItemGroup>
</Target>

<Target Name="UpdateGeneratedFiles"
Inputs="$(MSBuildProjectFile);@(XsdFile)"
Outputs="$(IntermediateOutputPath)%(XsdFile.Filename).g.cs">
<Message Text="Generating code from @(XsdFile)..."/>
<Copy
SourceFiles="@(XsdFile)"
DestinationFiles="$(IntermediateOutputPath)%(XsdFile.Filename).g.cs"/>
</Target>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
namespace TestNs { public class TestClass { } }
12 changes: 12 additions & 0 deletions test-assets/test-projects/ProjectWithSdkProperty/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace ProjectWithSdkProperty
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>
<Sdk Name="Microsoft.NET.Sdk" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ internal GetTestStartInfoService GetRequestHandler(OmniSharpTestHost host)
}


public abstract bool UseLegacyDotNetCli { get; }
public abstract DotNetCliVersion DotNetCliVersion { get; }

protected async Task GetDotNetTestStartInfoAsync(string projectName, string methodName, string testFramework)
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync(projectName))
using (var host = CreateOmniSharpHost(testProject.Directory, useLegacyDotNetCli: UseLegacyDotNetCli))
using (var host = CreateOmniSharpHost(testProject.Directory, dotNetCliVersion: DotNetCliVersion))
{
var service = GetRequestHandler(host);

Expand Down
4 changes: 2 additions & 2 deletions tests/OmniSharp.DotNetTest.Tests/AbstractRunTestFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ internal RunTestService GetRequestHandler(OmniSharpTestHost host)
return host.GetRequestHandler<RunTestService>(OmniSharpEndpoints.V2.RunTest);
}

public abstract bool UseLegacyDotNetCli { get; }
public abstract DotNetCliVersion DotNetCliVersion { get; }

protected async Task<RunTestResponse> RunDotNetTestAsync(string projectName, string methodName, string testFramework, bool shouldPass, bool expectResults = true)
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync(projectName))
using (var host = CreateOmniSharpHost(testProject.Directory, useLegacyDotNetCli: UseLegacyDotNetCli))
using (var host = CreateOmniSharpHost(testProject.Directory, dotNetCliVersion: DotNetCliVersion))
{
var service = GetRequestHandler(host);

Expand Down
Loading

0 comments on commit a881405

Please sign in to comment.