Skip to content

Commit b07d208

Browse files
committed
Support .NET 2.0 Subset profile
1 parent 76a4af9 commit b07d208

File tree

9 files changed

+72
-58
lines changed

9 files changed

+72
-58
lines changed

core/IncrementalCompiler/Compiler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private CompileResult BuildFull(CompileOptions options)
7070
_referenceMap.Values,
7171
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
7272
.WithSpecificDiagnosticOptions(specificDiagnosticOptions)
73+
.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default)
7374
.WithAllowUnsafe(options.Options.Contains("-unsafe")));
7475

7576
Emit(result);

extra/CompilerPlugin/CustomCSharpCompiler.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,37 @@ public CustomCSharpCompiler(MonoIsland island, bool runUpdater) : base(island, r
1919
}
2020
#endif
2121

22-
private string GetCompilerPath(List<string> arguments)
22+
private string[] GetAdditionalReferences()
2323
{
24-
var basePath = Path.Combine(Directory.GetCurrentDirectory(), "Compiler");
25-
var compilerPath = Path.Combine(basePath, "UniversalCompiler.exe");
26-
if (File.Exists(compilerPath))
27-
{
28-
return compilerPath;
29-
}
30-
31-
Debug.LogWarning($"Custom C# compiler not found in project directory ({compilerPath}), using the default compiler");
32-
3324
// calling base method via reflection
3425
var bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
35-
var methodInfo = GetType().BaseType.GetMethod(nameof(GetCompilerPath), bindingFlags);
36-
var result = (string)methodInfo.Invoke(this, new object[] {arguments});
26+
var methodInfo = GetType().BaseType.GetMethod(nameof(GetAdditionalReferences), bindingFlags);
27+
var result = (string[])methodInfo.Invoke(this, null);
3728
return result;
3829
}
3930

40-
private string[] GetAdditionalReferences()
31+
private string GetCompilerPath(List<string> arguments)
4132
{
4233
// calling base method via reflection
4334
var bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic;
44-
var methodInfo = GetType().BaseType.GetMethod(nameof(GetAdditionalReferences), bindingFlags);
45-
var result = (string[])methodInfo.Invoke(this, null);
35+
var methodInfo = GetType().BaseType.GetMethod(nameof(GetCompilerPath), bindingFlags);
36+
var result = (string)methodInfo.Invoke(this, new object[] {arguments});
4637
return result;
4738
}
4839

40+
private string GetCompilerPathEx(List<string> arguments)
41+
{
42+
var basePath = Path.Combine(Directory.GetCurrentDirectory(), "Compiler");
43+
var compilerPath = Path.Combine(basePath, "UniversalCompiler.exe");
44+
if (File.Exists(compilerPath))
45+
{
46+
return compilerPath;
47+
}
48+
49+
Debug.LogWarning($"Custom C# compiler not found in project directory ({compilerPath}), using the default compiler");
50+
return GetCompilerPath(arguments);
51+
}
52+
4953
// Copy of MonoCSharpCompiler.StartCompiler()
5054
// The only reason it exists is to call the new implementation
5155
// of GetCompilerPath(...) which is non-virtual unfortunately.
@@ -56,9 +60,10 @@ protected override Program StartCompiler()
5660
"-debug",
5761
"-target:library",
5862
"-nowarn:0169",
59-
"-unsafe",
6063
"-out:" + PrepareFileName(_island._output),
61-
"-define:__UNITY_PROCESSID__" + System.Diagnostics.Process.GetCurrentProcess().Id
64+
"-unsafe",
65+
"-define:__UNITY_PROCESSID__" + System.Diagnostics.Process.GetCurrentProcess().Id,
66+
"-define:__UNITY_PROFILE__" + Path.GetFileName(base.GetProfileDirectory()).Replace(".", "_")
6267
};
6368
foreach (var reference in _island._references)
6469
{
@@ -85,6 +90,6 @@ protected override Program StartCompiler()
8590
}
8691
}
8792

88-
return StartCompiler(_island._target, GetCompilerPath(arguments), arguments);
93+
return StartCompiler(_island._target, GetCompilerPathEx(arguments), arguments);
8994
}
9095
}

extra/UniversalCompiler/Compilers/Compiler.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ protected Compiler(Logger logger, string compilerPath, string pbd2MdbPath = null
2929
this.pbd2MdbPath = pbd2MdbPath;
3030
}
3131

32-
public int Compile(Platform platform, string unityEditorDataDir, string responseFile)
32+
public int Compile(Platform platform, string monoProfile, string unityEditorDataDir, string responseFile)
3333
{
34-
var process = CreateCompilerProcess(platform, unityEditorDataDir, responseFile);
34+
var process = CreateCompilerProcess(platform, monoProfile, unityEditorDataDir, responseFile);
3535
process.OutputDataReceived += (sender, e) => outputLines.Add(e.Data);
3636
process.ErrorDataReceived += (sender, e) => errorLines.Add(e.Data);
3737

@@ -77,7 +77,12 @@ where string.IsNullOrEmpty(trimmedLine) == false
7777
}
7878
}
7979

80-
protected abstract Process CreateCompilerProcess(Platform platform, string unityEditorDataDir, string responseFile);
80+
public static string GetMonoDllPath(string unityEditorDataDir, string monoProfile, string fileName)
81+
{
82+
return Path.Combine(unityEditorDataDir, @"Mono/lib/mono/" + monoProfile + "/" + fileName);
83+
}
84+
85+
protected abstract Process CreateCompilerProcess(Platform platform, string monoProfile, string unityEditorDataDir, string responseFile);
8186

8287
public virtual void ConvertDebugSymbols(Platform platform, string libraryPath, string unityEditorDataDir) { }
8388

@@ -150,4 +155,4 @@ public virtual void PrintPdb2MdbOutputAndErrors()
150155
{
151156
throw new NotSupportedException();
152157
}
153-
}
158+
}

extra/UniversalCompiler/Compilers/Incremental60Compiler.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public Incremental60Compiler(Logger logger, string directory)
1313

1414
public static bool IsAvailable(string directory) => File.Exists(Path.Combine(directory, "IncrementalCompiler.exe"));
1515

16-
protected override Process CreateCompilerProcess(Platform platform, string unityEditorDataDir, string responseFile)
16+
protected override Process CreateCompilerProcess(Platform platform, string monoProfile, string unityEditorDataDir, string responseFile)
1717
{
18-
var systemDllPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/System.dll");
19-
var systemCoreDllPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/System.Core.dll");
20-
var systemXmlDllPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/System.Xml.dll");
21-
var mscorlibDllPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/mscorlib.dll");
18+
var systemDllPath = GetMonoDllPath(unityEditorDataDir, monoProfile, "System.dll");
19+
var systemCoreDllPath = GetMonoDllPath(unityEditorDataDir, monoProfile, "System.Core.dll");
20+
var systemXmlDllPath = GetMonoDllPath(unityEditorDataDir, monoProfile, "System.Xml.dll");
21+
var mscorlibDllPath = GetMonoDllPath(unityEditorDataDir, monoProfile, "mscorlib.dll");
2222

2323
string processArguments = "-nostdlib+ -noconfig "
2424
+ $"-r:\"{mscorlibDllPath}\" "
@@ -66,4 +66,4 @@ where string.IsNullOrEmpty(trimmedLine) == false
6666
logger?.Append($"{i}: {lines[i]}");
6767
}
6868
}
69-
}
69+
}

extra/UniversalCompiler/Compilers/Microsoft60Compiler.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ public Microsoft60Compiler(Logger logger, string directory)
1414
public static bool IsAvailable(string directory) => File.Exists(Path.Combine(directory, "csc.exe")) &&
1515
File.Exists(Path.Combine(directory, "pdb2mdb.exe"));
1616

17-
protected override Process CreateCompilerProcess(Platform platform, string unityEditorDataDir, string responseFile)
17+
protected override Process CreateCompilerProcess(Platform platform, string monoProfile, string unityEditorDataDir, string responseFile)
1818
{
19-
var systemDllPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/System.dll");
20-
var systemCoreDllPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/System.Core.dll");
21-
var systemXmlDllPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/System.Xml.dll");
22-
var mscorlibDllPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/mscorlib.dll");
19+
var systemDllPath = GetMonoDllPath(unityEditorDataDir, monoProfile, "System.dll");
20+
var systemCoreDllPath = GetMonoDllPath(unityEditorDataDir, monoProfile, "System.Core.dll");
21+
var systemXmlDllPath = GetMonoDllPath(unityEditorDataDir, monoProfile, "System.Xml.dll");
22+
var mscorlibDllPath = GetMonoDllPath(unityEditorDataDir, monoProfile, "mscorlib.dll");
2323

24-
string processArguments = "-nostdlib+ -noconfig "
24+
string processArguments = "-nostdlib+ -noconfig -nologo "
2525
+ $"-r:\"{mscorlibDllPath}\" "
2626
+ $"-r:\"{systemDllPath}\" "
2727
+ $"-r:\"{systemCoreDllPath}\" "
@@ -55,14 +55,10 @@ public override void ConvertDebugSymbols(Platform platform, string libraryPath,
5555
public override void PrintCompilerOutputAndErrors()
5656
{
5757
// Microsoft's compiler writes all warnings and errors to the standard output channel,
58-
// so move them to the error channel skipping first 3 lines that are just part of the header.
58+
// so move them to the error channel
5959

60-
while (outputLines.Count > 3)
61-
{
62-
var line = outputLines[3];
63-
outputLines.RemoveAt(3);
64-
errorLines.Add(line);
65-
}
60+
errorLines.AddRange(outputLines);
61+
outputLines.Clear();
6662

6763
base.PrintCompilerOutputAndErrors();
6864
}
@@ -82,4 +78,4 @@ where string.IsNullOrEmpty(trimmedLine) == false
8278
logger?.Append($"{i}: {lines[i]}");
8379
}
8480
}
85-
}
81+
}

extra/UniversalCompiler/Compilers/Mono30Compiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ internal class Mono30Compiler : Compiler
55
public Mono30Compiler(Logger logger, string compilerPath) : base(logger, compilerPath, null) { }
66
public override string Name => "Mono C# 3.0";
77

8-
protected override Process CreateCompilerProcess(Platform platform, string unityEditorDataDir, string responseFile)
8+
protected override Process CreateCompilerProcess(Platform platform, string monoProfile, string unityEditorDataDir, string responseFile)
99
{
1010
var process = new Process();
1111
process.StartInfo = CreateOSDependentStartInfo(platform, ProcessRuntime.CLR20, compilerPath, responseFile, unityEditorDataDir);
1212
return process;
1313
}
14-
}
14+
}

extra/UniversalCompiler/Compilers/Mono50Compiler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ internal class Mono50Compiler : Compiler
66
public Mono50Compiler(Logger logger, string compilerPath) : base(logger, compilerPath, null) { }
77
public override string Name => "Mono C# 5.0";
88

9-
protected override Process CreateCompilerProcess(Platform platform, string unityEditorDataDir, string responseFile)
9+
protected override Process CreateCompilerProcess(Platform platform, string monoProfile, string unityEditorDataDir, string responseFile)
1010
{
11-
var systemCoreDllPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/System.Core.dll");
11+
var systemCoreDllPath = GetMonoDllPath(unityEditorDataDir, monoProfile, "System.Core.dll");
1212

1313
string processArguments;
1414
if (platform == Platform.Windows)
@@ -24,4 +24,4 @@ protected override Process CreateCompilerProcess(Platform platform, string unity
2424
process.StartInfo = CreateOSDependentStartInfo(platform, ProcessRuntime.CLR40, compilerPath, processArguments, unityEditorDataDir);
2525
return process;
2626
}
27-
}
27+
}

extra/UniversalCompiler/Compilers/Mono60Compiler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ public Mono60Compiler(Logger logger, string directory)
88

99
public override string Name => "Mono C# 6.0";
1010

11-
protected override Process CreateCompilerProcess(Platform platform, string unityEditorDataDir, string responseFile)
11+
protected override Process CreateCompilerProcess(Platform platform, string monoProfile, string unityEditorDataDir, string responseFile)
1212
{
13-
var systemCoreDllPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/System.Core.dll");
13+
var systemCoreDllPath = GetMonoDllPath(unityEditorDataDir, monoProfile, "System.Core.dll");
1414

1515
string processArguments;
1616
if (platform == Platform.Windows)
@@ -28,4 +28,4 @@ protected override Process CreateCompilerProcess(Platform platform, string unity
2828
}
2929

3030
public static bool IsAvailable(string directory) => File.Exists(Path.Combine(directory, "mcs.exe"));
31-
}
31+
}

extra/UniversalCompiler/Program.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ private static int Compile(string[] args, Logger logger, Settings settings)
5353
var unityEditorDataDir = GetUnityEditorDataDir();
5454
var projectDir = Directory.GetCurrentDirectory();
5555
var targetAssembly = compilationOptions.First(line => line.StartsWith("-out:")).Substring(10).Trim('\'');
56+
var monoProfile = compilationOptions.First(line => line.StartsWith("-define:__UNITY_PROFILE__")).Substring(25);
57+
if (monoProfile == "2_0") monoProfile = "2.0";
5658

5759
logger?.Append($"CSharpCompilerWrapper.exe version: {Assembly.GetExecutingAssembly().GetName().Version}");
5860
logger?.Append($"Platform: {CurrentPlatform}");
61+
logger?.Append($"Mono profile: {monoProfile}");
5962
logger?.Append($"Target assembly: {targetAssembly}");
6063
logger?.Append($"Project directory: {projectDir}");
6164
logger?.Append($"Unity 'Data' or 'Frameworks' directory: {unityEditorDataDir}");
@@ -67,15 +70,15 @@ private static int Compile(string[] args, Logger logger, Settings settings)
6770
return -1;
6871
}
6972

70-
var compiler = CreateCompiler(settings.Compiler, logger, CurrentPlatform, projectDir, compilationOptions, unityEditorDataDir);
73+
var compiler = CreateCompiler(settings.Compiler, logger, CurrentPlatform, monoProfile, projectDir, compilationOptions, unityEditorDataDir);
7174

7275
logger?.Append($"Compiler: {compiler.Name}");
7376
logger?.Append("");
7477
logger?.Append("- Compilation -----------------------------------------------");
7578
logger?.Append("");
7679

7780
var stopwatch = Stopwatch.StartNew();
78-
var exitCode = compiler.Compile(CurrentPlatform, unityEditorDataDir, responseFile);
81+
var exitCode = compiler.Compile(CurrentPlatform, monoProfile, unityEditorDataDir, responseFile);
7982
stopwatch.Stop();
8083

8184
logger?.Append($"Elapsed time: {stopwatch.ElapsedMilliseconds / 1000f:F2} sec");
@@ -105,17 +108,19 @@ private static int Compile(string[] args, Logger logger, Settings settings)
105108
return 0;
106109
}
107110

108-
private static Compiler CreateCompiler(CompilerType compilerType, Logger logger, Platform platform, string projectDir, string[] compilationOptions, string unityEditorDataDir)
111+
private static Compiler CreateCompiler(CompilerType compilerType, Logger logger, Platform platform, string monoProfile, string projectDir, string[] compilationOptions, string unityEditorDataDir)
109112
{
110113
var compilerDirectory = Path.Combine(projectDir, LANGUAGE_SUPPORT_DIR);
111114

112115
switch (compilerType)
113116
{
114117
case CompilerType.Auto:
115-
return FindSuitableCompiler(logger, platform, projectDir, compilationOptions, unityEditorDataDir);
118+
return FindSuitableCompiler(logger, platform, monoProfile, projectDir, compilationOptions, unityEditorDataDir);
116119

117120
case CompilerType.Mono3:
118-
var stockCompilerPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/gmcs.exe");
121+
var stockCompilerPath = monoProfile == "2.0"
122+
? Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/gmcs.exe")
123+
: Path.Combine(unityEditorDataDir, @"Mono/lib/mono/" + monoProfile + "/smcs.exe");
119124
return new Mono30Compiler(logger, stockCompilerPath);
120125

121126
case CompilerType.Mono5:
@@ -142,7 +147,7 @@ private static Compiler CreateCompiler(CompilerType compilerType, Logger logger,
142147
return null;
143148
}
144149

145-
private static Compiler FindSuitableCompiler(Logger logger, Platform platform, string projectDir, string[] compilationOptions, string unityEditorDataDir)
150+
private static Compiler FindSuitableCompiler(Logger logger, Platform platform, string monoProfile, string projectDir, string[] compilationOptions, string unityEditorDataDir)
146151
{
147152
Compiler compiler = null;
148153

@@ -185,7 +190,9 @@ private static Compiler FindSuitableCompiler(Logger logger, Platform platform, s
185190
if (compiler == null)
186191
{
187192
// Using stock Mono C# 3.0 compiler
188-
var stockCompilerPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/gmcs.exe");
193+
var stockCompilerPath = monoProfile == "2.0"
194+
? Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/gmcs.exe")
195+
: Path.Combine(unityEditorDataDir, @"Mono/lib/mono/" + monoProfile + "/smcs.exe");
189196
compiler = new Mono30Compiler(logger, stockCompilerPath);
190197
}
191198

@@ -235,4 +242,4 @@ private static string GetUnityEditorDataDir()
235242
var path = monoPath.Substring(0, index);
236243
return path;
237244
}
238-
}
245+
}

0 commit comments

Comments
 (0)