1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
- using System ;
4
+ using System ;
5
5
using System . CodeDom ;
6
6
using System . CodeDom . Compiler ;
7
7
using System . Collections . Generic ;
12
12
using System . Security . Permissions ;
13
13
using System . Security . Principal ;
14
14
using System . Text ;
15
- using static Microsoft . CodeDom . Providers . DotNetCompilerPlatform . Constants . CustomCompilerParameters ;
16
15
17
16
namespace Microsoft . CodeDom . Providers . DotNetCompilerPlatform {
18
17
internal abstract class Compiler : ICodeCompiler {
19
18
private readonly CodeDomProvider _codeDomProvider ;
20
19
private readonly ICompilerSettings _compilerSettings ;
20
+ private string _compilerFullPath = null ;
21
21
private const string CLR_PROFILING_SETTING = "COR_ENABLE_PROFILING" ;
22
22
private const string DISABLE_PROFILING = "0" ;
23
23
24
- // Needs to be initialized using InitializeCompilerFullPath where the CompilerParameters are available.
25
- private string _compilerFullPath = null ;
26
-
27
24
public Compiler ( CodeDomProvider codeDomProvider , ICompilerSettings compilerSettings ) {
28
- _codeDomProvider = codeDomProvider ;
29
- _compilerSettings = compilerSettings ;
25
+ this . _codeDomProvider = codeDomProvider ;
26
+ this . _compilerSettings = compilerSettings ;
30
27
}
31
28
32
29
public CompilerResults CompileAssemblyFromDom ( CompilerParameters options , CodeCompileUnit compilationUnit ) {
@@ -50,8 +47,6 @@ public CompilerResults CompileAssemblyFromDomBatch(CompilerParameters options, C
50
47
throw new ArgumentNullException ( "compilationUnits" ) ;
51
48
}
52
49
53
- InitializeCompilerFullPath ( options ) ;
54
-
55
50
try {
56
51
var sources = compilationUnits . Select ( c => {
57
52
var writer = new StringWriter ( ) ;
@@ -87,8 +82,6 @@ public CompilerResults CompileAssemblyFromFileBatch(CompilerParameters options,
87
82
throw new ArgumentNullException ( "fileNames" ) ;
88
83
}
89
84
90
- InitializeCompilerFullPath ( options ) ;
91
-
92
85
try {
93
86
// Try opening the files to make sure they exists. This will throw an exception
94
87
// if it doesn't
@@ -124,8 +117,6 @@ public CompilerResults CompileAssemblyFromSourceBatch(CompilerParameters options
124
117
throw new ArgumentNullException ( "sources" ) ;
125
118
}
126
119
127
- InitializeCompilerFullPath ( options ) ;
128
-
129
120
try {
130
121
return FromSourceBatch ( options , sources ) ;
131
122
}
@@ -138,41 +129,17 @@ protected abstract string FileExtension {
138
129
get ;
139
130
}
140
131
141
- protected void InitializeCompilerFullPath ( CompilerParameters options = null ) {
142
- if ( string . IsNullOrEmpty ( _compilerFullPath ) ) {
143
- if ( options != null ) {
144
- // Determining whether the custom compiler path parameter is provided.
145
- var customCompilerPathParameter = options . CompilerOptions . Split ( '/' ) . FirstOrDefault ( p => p . StartsWith ( CustomCompilerPath ) ) ;
146
- if ( ! string . IsNullOrEmpty ( customCompilerPathParameter ) ) {
147
- if ( ! customCompilerPathParameter . Contains ( ":" ) ) {
148
- throw new ArgumentException ( $ "There's no value defined for the \" { CustomCompilerPath } \" compiler parameter!") ;
149
- }
132
+ protected virtual string CompilerName {
133
+ get {
134
+ if ( null == _compilerFullPath ) {
135
+ _compilerFullPath = _compilerSettings . CompilerFullPath ;
150
136
151
- // Removing trailing space (when this is not the last parameter) and extracting value.
152
- var customCompilerPath = customCompilerPathParameter . TrimEnd ( ' ' ) . Split ( ':' ) [ 1 ] ;
153
-
154
- if ( string . IsNullOrEmpty ( customCompilerPath ) ) {
155
- throw new ArgumentException ( $ "The value of the \" { CustomCompilerPath } \" compiler parameter can't be empty!") ;
156
- }
157
-
158
- // Extracting the name of the compiler executable from the default path.
159
- var compilerExecutable = _compilerSettings . CompilerFullPath . Substring ( _compilerSettings . CompilerFullPath . LastIndexOf ( '\\ ' ) ) ;
160
-
161
- // And finally, we're able to construct the complete custom path to the compiler executable.
162
- // If the custom path contains spaces, then it has to be surrounded by quotes, which we don't need now.
163
- _compilerFullPath = CompilationSettingsHelper . CompilerFullPath ( $ "{ customCompilerPath . Trim ( '"' ) } \\ { compilerExecutable } ") ;
164
-
165
- // Removing the custom parameter, as the compiler can't process it.
166
- options . CompilerOptions = options . CompilerOptions . Replace ( $ "/{ CustomCompilerPath } :{ customCompilerPath } ", "" ) ;
167
- }
168
- // Falling back to the default behavior.
169
- else _compilerFullPath = _compilerSettings . CompilerFullPath ;
137
+ // Try opening the file to make sure the compiler exist. This will throw an exception
138
+ // if it doesn't
139
+ using ( var str = File . OpenRead ( _compilerFullPath ) ) { }
170
140
}
171
- else _compilerFullPath = _compilerSettings . CompilerFullPath ;
172
141
173
- // Try opening the file to make sure that the compiler exists.
174
- // This will throw an exception if it doesn't.
175
- using ( var str = File . OpenRead ( _compilerFullPath ) ) { }
142
+ return _compilerFullPath ;
176
143
}
177
144
}
178
145
@@ -315,7 +282,7 @@ private CompilerResults FromFileBatch(CompilerParameters options, string[] fileN
315
282
}
316
283
317
284
Compile ( options ,
318
- _compilerFullPath ,
285
+ CompilerName ,
319
286
args ,
320
287
ref outputFile ,
321
288
ref retValue ) ;
@@ -333,7 +300,7 @@ private CompilerResults FromFileBatch(CompilerParameters options, string[] fileN
333
300
replacedArgs = true ;
334
301
var outputLine = string . Format ( "{0}>{1} {2}" ,
335
302
Environment . CurrentDirectory ,
336
- _compilerFullPath ,
303
+ CompilerName ,
337
304
trueArgs ) ;
338
305
results . Output . Add ( outputLine ) ;
339
306
}
0 commit comments