Skip to content

Commit

Permalink
Don't require csc.dll when using in-process compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
mhutch committed Jan 26, 2024
1 parent 121ec3d commit 590808c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static bool IsLangVersionArg (string arg) =>
// If we were unable to determine the supported language version for the runtime,
// its MaxSupportedLangVersion will default to "Latest" so its language features
// are available before we add a language version mapping for that runtime version.
return $"-langversion:{ToString (runtime.MaxSupportedLangVersion)}";
return $"-langversion:{ToString (runtime.CscMaxLangVersion)}";
}

//https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-version-history
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class CscCodeCompiler : CodeCompiler
public CscCodeCompiler (RuntimeInfo runtime)
{
this.runtime = runtime;
if (runtime.CscPath is null) {
throw new TemplatingEngineException ("Cannot find C# compiler");
}
}

static StreamWriter CreateTempTextFile (string extension, out string path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ class RuntimeInfo
public RuntimeKind Kind { get; private set; }
public string Error { get; private set; }
public string RuntimeDir { get; private set; }

// may be null, as this is not a problem when the optional in-process compiler is used
public string CscPath { get; private set; }

/// <summary>
/// Maximum C# language version supported by C# compiler in <see cref="CscPath"/>.
/// </summary>
public CSharpLangVersion CscMaxLangVersion { get; private set; }

public bool IsValid => Error == null;
public Version Version { get; private set; }
public CSharpLangVersion MaxSupportedLangVersion { get; private set; }

public string RefAssembliesDir { get; private set; }
public string RuntimeFacadesDir { get; internal set; }
Expand Down Expand Up @@ -85,7 +92,7 @@ static RuntimeInfo GetMonoRuntime ()
// we don't really care about the version if it's not .net core
Version = new Version ("4.7.2"),
//if mono has csc at all, we know it at least supports 6.0
MaxSupportedLangVersion = CSharpLangVersion.v6_0
CscMaxLangVersion = CSharpLangVersion.v6_0
};
}

Expand All @@ -102,7 +109,7 @@ static RuntimeInfo GetNetFrameworkRuntime ()
RuntimeFacadesDir = runtimeDir,
// we don't really care about the version if it's not .net core
Version = new Version ("4.7.2"),
MaxSupportedLangVersion = CSharpLangVersion.v5_0
CscMaxLangVersion = CSharpLangVersion.v5_0
};
}

Expand Down Expand Up @@ -140,11 +147,12 @@ static RuntimeInfo GetDotNetCoreSdk ()
}
}

// find the highest available C# compiler. we don't load it in process, so its runtime doesn't matter.
static string MakeCscPath (string d) => Path.Combine (d, "Roslyn", "bincore", "csc.dll");
var sdkDir = FindHighestVersionedDirectory (Path.Combine (dotnetRoot, "sdk"), d => File.Exists (MakeCscPath (d)), out var sdkVersion);
if (sdkDir == null) {
return FromError (RuntimeKind.NetCore, "Could not find csc.dll in any .NET Core SDK");
}

// it's okay if cscPath is null as we may be using the in-process compiler
string cscPath = sdkDir == null ? null : MakeCscPath (sdkDir);
var maxCSharpVersion = CSharpLangVersionHelper.FromNetCoreSdkVersion (sdkVersion);

// it's ok if this is null, we may be running on an older SDK that didn't support packs
Expand All @@ -155,7 +163,7 @@ static RuntimeInfo GetDotNetCoreSdk ()
out _
);

return new RuntimeInfo (RuntimeKind.NetCore) { RuntimeDir = runtimeDir, RefAssembliesDir = refAssembliesDir, CscPath = MakeCscPath (sdkDir), MaxSupportedLangVersion = maxCSharpVersion, Version = hostVersion };
return new RuntimeInfo (RuntimeKind.NetCore) { RuntimeDir = runtimeDir, RefAssembliesDir = refAssembliesDir, CscPath = MakeCscPath (sdkDir), CscMaxLangVersion = maxCSharpVersion, Version = hostVersion };
}

static string FindHighestVersionedDirectory (string parentFolder, Func<string, bool> validate, out SemVersion bestVersion)
Expand Down

0 comments on commit 590808c

Please sign in to comment.