Skip to content

Commit 296e419

Browse files
committed
Add settings of UniversalCompiler
1 parent 20a00b8 commit 296e419

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed

extra/UniversalCompiler/Program.cs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,24 @@ private static int Main(string[] args)
1515
int exitCode;
1616
Logger logger = null;
1717

18+
Settings settings;
19+
try
20+
{
21+
settings = Settings.Load() ?? Settings.Default;
22+
}
23+
catch (Exception e)
24+
{
25+
Console.Error.Write("Failed in loading settings: " + e);
26+
return 1;
27+
}
28+
1829
#if LOGGING_ENABLED
1930
using (logger = new Logger())
2031
#endif
2132
{
2233
try
2334
{
24-
exitCode = Compile(args, logger);
35+
exitCode = Compile(args, logger, settings);
2536
}
2637
catch (Exception e)
2738
{
@@ -33,7 +44,7 @@ private static int Main(string[] args)
3344
return exitCode;
3445
}
3546

36-
private static int Compile(string[] args, Logger logger)
47+
private static int Compile(string[] args, Logger logger, Settings settings)
3748
{
3849
logger?.AppendHeader();
3950

@@ -56,7 +67,7 @@ private static int Compile(string[] args, Logger logger)
5667
return -1;
5768
}
5869

59-
var compiler = FindSuitableCompiler(logger, CurrentPlatform, projectDir, compilationOptions, unityEditorDataDir);
70+
var compiler = CreateCompiler(settings.Compiler, logger, CurrentPlatform, projectDir, compilationOptions, unityEditorDataDir);
6071

6172
logger?.Append($"Compiler: {compiler.Name}");
6273
logger?.Append("");
@@ -94,6 +105,43 @@ private static int Compile(string[] args, Logger logger)
94105
return 0;
95106
}
96107

108+
private static Compiler CreateCompiler(CompilerType compilerType, Logger logger, Platform platform, string projectDir, string[] compilationOptions, string unityEditorDataDir)
109+
{
110+
var compilerDirectory = Path.Combine(projectDir, LANGUAGE_SUPPORT_DIR);
111+
112+
switch (compilerType)
113+
{
114+
case CompilerType.Auto:
115+
return FindSuitableCompiler(logger, platform, projectDir, compilationOptions, unityEditorDataDir);
116+
117+
case CompilerType.Mono3:
118+
var stockCompilerPath = Path.Combine(unityEditorDataDir, @"Mono/lib/mono/2.0/gmcs.exe");
119+
return new Mono30Compiler(logger, stockCompilerPath);
120+
121+
case CompilerType.Mono5:
122+
var bleedingEdgeCompilerPath = Path.Combine(unityEditorDataDir, @"MonoBleedingEdge/lib/mono/4.5/mcs.exe");
123+
return new Mono50Compiler(logger, bleedingEdgeCompilerPath);
124+
125+
case CompilerType.Mono6:
126+
if (Mono60Compiler.IsAvailable(compilerDirectory))
127+
return new Mono60Compiler(logger, compilerDirectory);
128+
break;
129+
130+
case CompilerType.Microsoft6:
131+
var roslynDirectory = Path.Combine(compilerDirectory, "Roslyn");
132+
if (Microsoft60Compiler.IsAvailable(roslynDirectory))
133+
return new Microsoft60Compiler(logger, roslynDirectory);
134+
break;
135+
136+
case CompilerType.Incremental6:
137+
if (Incremental60Compiler.IsAvailable(compilerDirectory))
138+
return new Incremental60Compiler(logger, compilerDirectory);
139+
break;
140+
}
141+
142+
return null;
143+
}
144+
97145
private static Compiler FindSuitableCompiler(Logger logger, Platform platform, string projectDir, string[] compilationOptions, string unityEditorDataDir)
98146
{
99147
Compiler compiler = null;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
using System.Xml.Linq;
5+
6+
public enum CompilerType
7+
{
8+
Auto,
9+
Mono3,
10+
Mono5,
11+
Mono6,
12+
Microsoft6,
13+
Incremental6,
14+
}
15+
16+
public class Settings
17+
{
18+
public CompilerType Compiler;
19+
20+
public static Settings Default = new Settings
21+
{
22+
Compiler = CompilerType.Auto,
23+
};
24+
25+
public static Settings Load()
26+
{
27+
var fileName = Path.ChangeExtension(Assembly.GetEntryAssembly().Location, ".xml");
28+
if (File.Exists(fileName) == false)
29+
return null;
30+
31+
using (var reader = File.OpenText(fileName))
32+
{
33+
return Load(reader);
34+
}
35+
}
36+
37+
public static Settings Load(TextReader reader)
38+
{
39+
// To reduce start-up time, do manual parsing instead of using XmlSerializer
40+
var xdoc = XDocument.Load(reader).Element("Settings");
41+
return new Settings
42+
{
43+
Compiler = (CompilerType)Enum.Parse(typeof(CompilerType), xdoc.Element("Compiler").Value),
44+
};
45+
}
46+
}

extra/UniversalCompiler/UniversalCompiler.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050
<Compile Include="Platform.cs" />
5151
<Compile Include="Program.cs" />
5252
<Compile Include="Properties\AssemblyInfo.cs" />
53+
<Compile Include="Settings.cs" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<Content Include="UniversalCompiler.xml">
57+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
58+
</Content>
5359
</ItemGroup>
5460
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5561
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

extra/UniversalCompiler/UniversalCompiler.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0" encoding="UTF-8"?><Settings> <!-- Compiler: Type of compiler will be used. - Auto : Automatically choose compiler. Order: Incremental6, Microsoft6, Mono6, Mono5, Mono3 - Mono3 : Mono C# 3 Compiler (Unity Default) - Mono5 : Mono C# 5 Compiler - Mono6 : Mono C# 6 Compiler - Microsoft6 : Microsoft C# 6 Compiler - Incremental6 : Incremental C# 6 Compiler --> <Compiler>Auto</Compiler> </Settings>

0 commit comments

Comments
 (0)