未验证 提交 f7594c50 编写于 作者: A Ashley Hauck

Reduce the use of the preprocessor

Prefer to use runtime checks instead.
上级 9ff9ac1c
......@@ -36,6 +36,9 @@
<Compile Include="..\..\Shared\CoreClrAnalyzerAssemblyLoader.cs">
<Link>CoreClrAnalyzerAssemblyLoader.cs</Link>
</Compile>
<Compile Include="..\..\Shared\CoreClrShim.cs">
<Link>CoreClrShim.cs</Link>
</Compile>
<Compile Include="..\..\Shared\DesktopBuildClient.cs">
<Link>DesktopBuildClient.cs</Link>
</Compile>
......
......@@ -36,11 +36,8 @@ internal static ICompilerServerHost CreateCompilerServerHost()
// VBCSCompiler is installed in the same directory as csc.exe and vbc.exe which is also the
// location of the response files.
var clientDirectory = AppDomain.CurrentDomain.BaseDirectory;
#if NET46
var sdkDirectory = RuntimeEnvironment.GetRuntimeDirectory();
#else
var sdkDirectory = (string)null;
#endif
var sdkDirectory = BuildClient.GetSystemSdkDirectory();
return new DesktopCompilerServerHost(clientDirectory, sdkDirectory);
}
......
......@@ -23,7 +23,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'" />
<ItemGroup>
<Reference Include="System.Configuration" Condition="'$(TargetFramework)' != 'netcoreapp2.0'" />
<Reference Include="System.Configuration" Condition="'$(TargetFramework)' != 'netcoreapp2.0'" />
<PackageReference Include="Microsoft.NETCore.App" Version="$(MicrosoftNETCoreAppVersion)" Condition="'$(TargetFramework)' == 'netcoreapp2.0'" />
<PackageReference Include="System.IO.Pipes.AccessControl" Version="$(SystemIOPipesAccessControlVersion)" Condition="'$(TargetFramework)' == 'netcoreapp2.0'" />
</ItemGroup>
......@@ -37,6 +37,9 @@
<Compile Include="..\..\Shared\CoreClrAnalyzerAssemblyLoader.cs">
<Link>CoreClrAnalyzerAssemblyLoader.cs</Link>
</Compile>
<Compile Include="..\..\Shared\CoreClrShim.cs">
<Link>CoreClrShim.cs</Link>
</Compile>
<Compile Include="..\..\Shared\DesktopAnalyzerAssemblyLoader.cs">
<Link>DesktopAnalyzerAssemblyLoader.cs</Link>
</Compile>
......
......@@ -198,11 +198,7 @@ private static (T Result, string Output) UseTextWriter<T>(Encoding encoding, Fun
var client = ServerUtil.CreateBuildClient(language);
client.TimeoutOverride = Timeout.Infinite;
#if NET461
var sdkDir = RuntimeEnvironment.GetRuntimeDirectory();
#else
string sdkDir = null;
#endif
var sdkDir = ServerUtil.DefaultSdkDirectory;
var buildPaths = new BuildPaths(
clientDir: Path.GetDirectoryName(typeof(CommonCompiler).Assembly.Location),
......@@ -222,10 +218,11 @@ private static DisposableFile GetResultFile(TempDirectory directory, string resu
private static void RunCompilerOutput(TempFile file, string expectedOutput)
{
#if NET461
var result = ProcessUtilities.Run(file.Path, "", Path.GetDirectoryName(file.Path));
Assert.Equal(expectedOutput.Trim(), result.Output.Trim());
#endif
if (!CoreClrShim.IsRunningOnCoreClr)
{
var result = ProcessUtilities.Run(file.Path, "", Path.GetDirectoryName(file.Path));
Assert.Equal(expectedOutput.Trim(), result.Output.Trim());
}
}
private static void VerifyResult((int ExitCode, string Output) result)
......
......@@ -69,11 +69,7 @@ public void Dispose()
internal static class ServerUtil
{
internal static string DefaultClientDirectory { get; } = Path.GetDirectoryName(typeof(DesktopBuildClientTests).Assembly.Location);
#if NET461
internal static string DefaultSdkDirectory { get; } = RuntimeEnvironment.GetRuntimeDirectory();
#else
internal static string DefaultSdkDirectory { get; } = null;
#endif
internal static string DefaultSdkDirectory { get; } = BuildClient.GetSystemSdkDirectory();
internal static BuildPaths CreateBuildPaths(string workingDir, string tempDir)
{
......
......@@ -40,6 +40,18 @@ internal abstract class BuildClient
{
protected static bool IsRunningOnWindows => Path.DirectorySeparatorChar == '\\';
public static string GetSystemSdkDirectory()
{
if (CoreClrShim.IsRunningOnCoreClr)
{
return null;
}
else
{
return RuntimeEnvironment.GetRuntimeDirectory();
}
}
/// <summary>
/// Run a compilation through the compiler server and print the output
/// to the console. If the compiler server fails, run the fallback
......@@ -191,15 +203,16 @@ private static bool UseNativeArguments()
return false;
}
#if NET46
if (CoreClrShim.IsRunningOnCoreClr)
{
// The native invoke ends up giving us both CoreRun and the exe file.
// We've decided to ignore backcompat for CoreCLR,
// and use the Main()-provided arguments
// https://github.com/dotnet/roslyn/issues/6677
return false;
}
return true;
#else
// (Not NET46 -> on CoreCLR)
// The native invoke ends up giving us both CoreRun and the exe file.
// Need to find a good way to remove the host as well as the EXE argument.
// https://github.com/dotnet/roslyn/issues/6677
return false;
#endif
}
/// <summary>
......
......@@ -341,13 +341,7 @@ internal sealed class BuildServerConnection
internal static bool TryCreateServerCore(string clientDir, string pipeName)
{
#if NETSTANDARD1_3
bool isRunningOnCoreClr = CoreClrShim.IsRunningOnCoreClr;
#elif NET46
bool isRunningOnCoreClr = false;
#elif NETCOREAPP2_0
bool isRunningOnCoreClr = true;
#endif
string expectedPath;
string processArguments;
if (isRunningOnCoreClr)
......
......@@ -3,11 +3,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Microsoft.CodeAnalysis.CommandLine
{
......@@ -31,15 +28,14 @@ internal DesktopBuildClient(RequestLanguage language, CompileFunc compileFunc, I
internal static int Run(IEnumerable<string> arguments, RequestLanguage language, CompileFunc compileFunc, IAnalyzerAssemblyLoader analyzerAssemblyLoader)
{
#if NET46
var sdkDir = RuntimeEnvironment.GetRuntimeDirectory();
#else
string sdkDir = null;
var sdkDir = GetSystemSdkDirectory();
if (CoreClrShim.IsRunningOnCoreClr)
{
// Register encodings for console
// https://github.com/dotnet/roslyn/issues/10785
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
}
// Register encodings for console
// https://github.com/dotnet/roslyn/issues/10785
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
#endif
var client = new DesktopBuildClient(language, compileFunc, analyzerAssemblyLoader);
var clientDir = AppContext.BaseDirectory;
var workingDir = Directory.GetCurrentDirectory();
......
......@@ -35,6 +35,9 @@
<Compile Include="..\..\Shared\CoreClrAnalyzerAssemblyLoader.cs">
<Link>CoreClrAnalyzerAssemblyLoader.cs</Link>
</Compile>
<Compile Include="..\..\Shared\CoreClrShim.cs">
<Link>CoreClrShim.cs</Link>
</Compile>
<Compile Include="..\..\Shared\DesktopBuildClient.cs">
<Link>DesktopBuildClient.cs</Link>
</Compile>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册