3
3
using System . Linq . Expressions ;
4
4
using System . Reflection ;
5
5
using System . Runtime ;
6
+ using System . Runtime . Loader ;
6
7
using System . Text . RegularExpressions ;
7
8
using Microsoft . CodeAnalysis ;
8
9
using Microsoft . CodeAnalysis . CSharp ;
@@ -17,35 +18,40 @@ internal record CompiledAssemblyResult(CollectibleAssemblyLoadContext AssemblyCo
17
18
internal class Compiler : ICompiler
18
19
{
19
20
private readonly ILogger < Compiler > _logger ;
21
+ private readonly bool _useDebug ;
20
22
private readonly ISyntaxTreeResolver _syntaxResolver ;
21
23
22
24
public Compiler (
23
25
ISyntaxTreeResolver syntaxResolver ,
24
- ILogger < Compiler > logger
25
- )
26
+ ILogger < Compiler > logger ,
27
+ IOptions < CompileSettings > compileSettings )
26
28
{
27
29
_syntaxResolver = syntaxResolver ;
28
30
_logger = logger ;
31
+ _useDebug = compileSettings . Value . UseDebug ;
29
32
}
30
33
31
34
public CompiledAssemblyResult Compile ( )
32
35
{
33
36
CollectibleAssemblyLoadContext context = new ( ) ;
37
+
34
38
var compilation = GetSharpCompilation ( ) ;
35
39
36
40
using var peStream = new MemoryStream ( ) ;
37
- var emitResult = compilation . Emit ( peStream ) ;
41
+ using MemoryStream ? symStream = _useDebug ? new MemoryStream ( ) : null ;
42
+
43
+ var emitResult = compilation . Emit ( peStream , symStream ) ;
38
44
39
45
if ( emitResult . Success )
40
46
{
41
47
peStream . Seek ( 0 , SeekOrigin . Begin ) ;
42
- var assembly = context . LoadFromStream ( peStream ) ;
48
+ symStream ? . Seek ( 0 , SeekOrigin . Begin ) ;
49
+ var assembly = context . LoadFromStream ( peStream , symStream ) ;
43
50
return new CompiledAssemblyResult ( context , assembly ) ;
44
51
}
45
52
46
53
var error = PrettyPrintCompileError ( emitResult ) ;
47
-
48
- _logger . LogError ( "Failed to compile applications\n {error}" , error ) ;
54
+ _logger . LogError ( "Failed to compile applications\n {Error}" , error ) ;
49
55
50
56
context . Unload ( ) ;
51
57
// Finally do cleanup and release memory
@@ -69,8 +75,9 @@ private CSharpCompilation GetSharpCompilation()
69
75
metaDataReference . ToArray ( ) ,
70
76
new CSharpCompilationOptions (
71
77
OutputKind . DynamicallyLinkedLibrary ,
72
- optimizationLevel : OptimizationLevel . Release ,
73
- assemblyIdentityComparer : DesktopAssemblyIdentityComparer . Default
78
+ optimizationLevel : _useDebug ? OptimizationLevel . Debug : OptimizationLevel . Release ,
79
+ assemblyIdentityComparer : DesktopAssemblyIdentityComparer . Default ,
80
+ platform : Platform . AnyCpu
74
81
)
75
82
) ;
76
83
}
@@ -112,4 +119,4 @@ private static string PrettyPrintCompileError(EmitResult emitResult)
112
119
113
120
return msg . ToString ( ) ;
114
121
}
115
- }
122
+ }
0 commit comments