-
Notifications
You must be signed in to change notification settings - Fork 789
/
SimulatedMSBuildReferenceResolver.fs
325 lines (262 loc) · 12 KB
/
SimulatedMSBuildReferenceResolver.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
module internal FSharp.Compiler.CodeAnalysis.SimulatedMSBuildReferenceResolver
open System
open System.IO
open System.Reflection
open Internal.Utilities.Library
open FSharp.Compiler.IO
// ATTENTION!: the following code needs to be updated every time we are switching to the new MSBuild version because new .NET framework version was released
// 1. List of frameworks
// 2. DeriveTargetFrameworkDirectoriesFor45Plus
// 3. HighestInstalledRefAssembliesOrDotNETFramework
// 4. GetPathToDotNetFrameworkImplementationAssemblies
[<Literal>]
let private Net45 = "v4.5"
[<Literal>]
let private Net451 = "v4.5.1"
[<Literal>]
let private Net452 = "v4.5.2" // not available in Dev15 MSBuild version
[<Literal>]
let private Net46 = "v4.6"
[<Literal>]
let private Net461 = "v4.6.1"
[<Literal>]
let private Net462 = "v4.6.2"
[<Literal>]
let private Net47 = "v4.7"
[<Literal>]
let private Net471 = "v4.7.1"
[<Literal>]
let private Net472 = "v4.7.2"
[<Literal>]
let private Net48 = "v4.8"
let SupportedDesktopFrameworkVersions =
[ Net48; Net472; Net471; Net47; Net462; Net461; Net46; Net452; Net451; Net45 ]
let private SimulatedMSBuildResolver =
/// Get the path to the .NET Framework implementation assemblies by using ToolLocationHelper.GetPathToDotNetFramework
/// This is only used to specify the "last resort" path for assembly resolution.
let GetPathToDotNetFrameworkImplementationAssemblies _ =
let isDesktop = typeof<int>.Assembly.GetName().Name = "mscorlib"
if isDesktop then
match (System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory(): string MaybeNull) with
| null -> []
| x -> [ x ]
else
[]
let GetPathToDotNetFrameworkReferenceAssemblies version =
ignore version
let r: string list = []
r
{ new ILegacyReferenceResolver with
member x.HighestInstalledNetFrameworkVersion() =
let root = x.DotNetFrameworkReferenceAssembliesRootDirectory
let fwOpt =
SupportedDesktopFrameworkVersions
|> Seq.tryFind (fun fw -> FileSystem.DirectoryExistsShim(Path.Combine(root, fw)))
match fwOpt with
| Some fw -> fw
| None -> Net45
member _.DotNetFrameworkReferenceAssembliesRootDirectory =
if Environment.OSVersion.Platform = PlatformID.Win32NT then
let PF =
match Environment.GetEnvironmentVariable("ProgramFiles(x86)") with
| null -> !! Environment.GetEnvironmentVariable("ProgramFiles") // if PFx86 is null, then we are 32-bit and just get PF
| s -> s
PF + @"\Reference Assemblies\Microsoft\Framework\.NETFramework"
else
""
member _.Resolve
(
resolutionEnvironment,
references,
targetFrameworkVersion,
targetFrameworkDirectories,
targetProcessorArchitecture,
fsharpCoreDir,
explicitIncludeDirs,
implicitIncludeDir,
logMessage,
logWarningOrError
) =
let results = ResizeArray()
let searchPaths =
[
yield! targetFrameworkDirectories
yield! explicitIncludeDirs
yield fsharpCoreDir
yield implicitIncludeDir
yield! GetPathToDotNetFrameworkReferenceAssemblies targetFrameworkVersion
yield! GetPathToDotNetFrameworkImplementationAssemblies targetFrameworkVersion
]
for r, baggage in references do
//printfn "resolving %s" r
let mutable found = false
let success path =
if not found then
//printfn "resolved %s --> %s" r path
found <- true
results.Add
{
itemSpec = path
prepareToolTip = snd
baggage = baggage
}
try
if not found && FileSystem.IsPathRootedShim r then
if FileSystem.FileExistsShim r then
success r
with e ->
logWarningOrError false "SR001" (e.ToString())
// For this one we need to get the version search exactly right, without doing a load
try
if
not found
&& r.StartsWithOrdinal("FSharp.Core, Version=")
&& Environment.OSVersion.Platform = PlatformID.Win32NT
then
let n = AssemblyName r
let fscoreDir0 =
let PF =
match Environment.GetEnvironmentVariable("ProgramFiles(x86)") with
| null -> !! Environment.GetEnvironmentVariable("ProgramFiles")
| s -> s
PF
+ @"\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\"
+ (!!n.Version).ToString()
let trialPath = Path.Combine(fscoreDir0, !!n.Name + ".dll")
if FileSystem.FileExistsShim trialPath then
success trialPath
with e ->
logWarningOrError false "SR001" (e.ToString())
let isFileName =
r.EndsWith("dll", StringComparison.OrdinalIgnoreCase)
|| r.EndsWith("exe", StringComparison.OrdinalIgnoreCase)
let qual =
if isFileName then
r
else
try
!!AssemblyName(r).Name + ".dll"
with _ ->
r + ".dll"
for searchPath in searchPaths do
try
if not found then
let trialPath = Path.Combine(searchPath, qual)
if FileSystem.FileExistsShim trialPath then
success trialPath
with e ->
logWarningOrError false "SR001" (e.ToString())
try
// Search the GAC on Windows
if
not found
&& not isFileName
&& Environment.OSVersion.Platform = PlatformID.Win32NT
then
let n = AssemblyName r
let netFx = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
let gac =
Path.Combine(!! Path.GetDirectoryName(Path.GetDirectoryName(netFx.TrimEnd('\\'))), "assembly")
match n.Version, n.GetPublicKeyToken() with
| null, _
| _, null ->
let options =
[
if FileSystem.DirectoryExistsShim gac then
for gacDir in FileSystem.EnumerateDirectoriesShim gac do
let assemblyDir = Path.Combine(gacDir, !!n.Name)
if FileSystem.DirectoryExistsShim assemblyDir then
for tdir in FileSystem.EnumerateDirectoriesShim assemblyDir do
let trialPath = Path.Combine(tdir, qual)
if FileSystem.FileExistsShim trialPath then
yield trialPath
]
//printfn "sorting GAC paths: %A" options
options
|> List.sort // puts latest version last
|> List.tryLast
|> function
| None -> ()
| Some p -> success p
| v, tok ->
if FileSystem.DirectoryExistsShim gac then
for gacDir in Directory.EnumerateDirectories gac do
//printfn "searching GAC directory: %s" gacDir
let assemblyDir = Path.Combine(gacDir, !!n.Name)
if FileSystem.DirectoryExistsShim assemblyDir then
//printfn "searching GAC directory: %s" assemblyDir
let tokText = String.concat "" [| for b in tok -> sprintf "%02x" b |]
let verDir = Path.Combine(assemblyDir, "v4.0_" + v.ToString() + "__" + tokText)
//printfn "searching GAC directory: %s" verDir
if FileSystem.DirectoryExistsShim verDir then
let trialPath = Path.Combine(verDir, qual)
//printfn "searching GAC: %s" trialPath
if FileSystem.FileExistsShim trialPath then
success trialPath
with e ->
logWarningOrError false "SR001" (e.ToString())
results.ToArray()
}
|> LegacyReferenceResolver
let internal getResolver () = SimulatedMSBuildResolver
#if INTERACTIVE
// Some manual testing
SimulatedMSBuildResolver.DotNetFrameworkReferenceAssembliesRootDirectory
SimulatedMSBuildResolver.HighestInstalledNetFrameworkVersion()
let fscoreDir =
if System.Environment.OSVersion.Platform = System.PlatformID.Win32NT then // file references only valid on Windows
let PF =
match Environment.GetEnvironmentVariable("ProgramFiles(x86)") with
| null -> Environment.GetEnvironmentVariable("ProgramFiles") // if PFx86 is null, then we are 32-bit and just get PF
| s -> s
PF + @"\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.4.0.0"
else
System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
let resolve s =
SimulatedMSBuildResolver.Resolve(
ResolutionEnvironment.EditingOrCompilation,
[| for a in s -> (a, "") |],
"v4.5.1",
[
SimulatedMSBuildResolver.DotNetFrameworkReferenceAssembliesRootDirectory
+ @"\v4.5.1"
],
"",
"",
fscoreDir,
[],
__SOURCE_DIRECTORY__,
ignore,
(fun _ _ -> ()),
(fun _ _ -> ())
)
// Resolve partial name to something on search path
resolve [ "FSharp.Core" ]
// Resolve DLL name to something on search path
resolve [ "FSharp.Core.dll" ]
// Resolve from reference assemblies
resolve [ "System"; "mscorlib"; "mscorlib.dll" ]
// Resolve from Registry AssemblyFolders
resolve [ "Microsoft.SqlServer.Dmf.dll"; "Microsoft.SqlServer.Dmf" ]
// Resolve exact version of FSharp.Core
resolve
[
"FSharp.Core, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
]
// Resolve from GAC:
resolve
[
"EventViewer, Version=6.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
]
// Resolve from GAC:
resolve [ "EventViewer" ]
resolve
[
"Microsoft.SharePoint.Client, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
]
resolve
[
"Microsoft.SharePoint.Client, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
]
#endif