Skip to content

Commit

Permalink
Reduce the use of the preprocessor
Browse files Browse the repository at this point in the history
Prefer to use runtime checks instead.
  • Loading branch information
khyperia committed Nov 10, 2017
1 parent 9ff9ac1 commit f7594c5
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 45 deletions.
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/csc/csc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 4 additions & 1 deletion src/Compilers/Server/VBCSCompiler/VBCSCompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down
15 changes: 6 additions & 9 deletions src/Compilers/Server/VBCSCompilerTests/CompilerServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,7 @@ internal static (int ExitCode, string Output) RunCommandLineCompiler(
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),
Expand All @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions src/Compilers/Server/VBCSCompilerTests/ServerUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
29 changes: 21 additions & 8 deletions src/Compilers/Shared/BuildClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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>
Expand Down
6 changes: 0 additions & 6 deletions src/Compilers/Shared/BuildServerConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,7 @@ internal static async Task<NamedPipeClientStream> TryConnectToServerAsync(

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)
Expand Down
18 changes: 7 additions & 11 deletions src/Compilers/Shared/DesktopBuildClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/VisualBasic/vbc/vbc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit f7594c5

Please sign in to comment.