This repository was archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathSimulatedMSBuildReferenceResolver.fs
More file actions
243 lines (210 loc) · 11.7 KB
/
SimulatedMSBuildReferenceResolver.fs
File metadata and controls
243 lines (210 loc) · 11.7 KB
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
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
#if INTERACTIVE
#load "../utils/ResizeArray.fs" "../absil/illib.fs" "../fsharp/ReferenceResolver.fs"
#else
module internal Microsoft.FSharp.Compiler.SimulatedMSBuildReferenceResolver
#endif
open System
open System.IO
open System.Reflection
open Microsoft.Win32
open Microsoft.FSharp.Compiler
open Microsoft.FSharp.Compiler.ReferenceResolver
open Microsoft.FSharp.Compiler.AbstractIL.Internal.Library
let internal SimulatedMSBuildResolver =
let supportedFrameworks = [|
"v4.7.2";
"v4.7.1";
"v4.7";
"v4.6.2";
"v4.6.1";
"v4.6";
"v4.5.1";
"v4.5";
"v4.0"
|]
{ new Resolver with
member x.HighestInstalledNetFrameworkVersion() =
let root = x.DotNetFrameworkReferenceAssembliesRootDirectory
let fwOpt = supportedFrameworks |> Seq.tryFind(fun fw -> Directory.Exists(Path.Combine(root, fw) ))
match fwOpt with
| Some fw -> fw
| None -> "v4.5"
member __.DotNetFrameworkReferenceAssembliesRootDirectory =
#if !FX_RESHAPED_MSBUILD
if System.Environment.OSVersion.Platform = System.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
#endif
""
member __.Resolve(resolutionEnvironment, references, targetFrameworkVersion, targetFrameworkDirectories, targetProcessorArchitecture,
fsharpCoreDir, explicitIncludeDirs, implicitIncludeDir, logMessage, logWarningOrError) =
#if !FX_NO_WIN_REGISTRY
let registrySearchPaths() =
[ let registryKey = @"Software\Microsoft\.NetFramework";
use key = Registry.LocalMachine.OpenSubKey(registryKey)
match key with
| null -> ()
| _ ->
for subKeyName in key.GetSubKeyNames() do
use subKey = key.OpenSubKey(subKeyName)
use subSubKey = subKey.OpenSubKey("AssemblyFoldersEx")
match subSubKey with
| null -> ()
| _ ->
for subSubSubKeyName in subSubKey.GetSubKeyNames() do
use subSubSubKey = subSubKey.OpenSubKey(subSubSubKeyName)
match subSubSubKey.GetValue(null) with
| :? string as s -> yield s
| _ -> ()
use subSubKey = key.OpenSubKey("AssemblyFolders")
match subSubKey with
| null -> ()
| _ ->
for subSubSubKeyName in subSubKey.GetSubKeyNames() do
let subSubSubKey = subSubKey.OpenSubKey(subSubSubKeyName)
match subSubSubKey.GetValue(null) with
| :? string as s -> yield s
| _ -> () ]
#endif
let results = ResizeArray()
let searchPaths =
[ yield! targetFrameworkDirectories
yield! explicitIncludeDirs
yield fsharpCoreDir
yield implicitIncludeDir
#if !FX_NO_WIN_REGISTRY
if System.Environment.OSVersion.Platform = System.PlatformID.Win32NT then
yield! registrySearchPaths()
#endif
]
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 && Path.IsPathRooted(r) then
if FileSystem.SafeExists(r) then
success r
with e -> logWarningOrError false "SR001" (e.ToString())
#if !FX_RESHAPED_MSBUILD
// 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.SafeExists(trialPath) then
success trialPath
with e -> logWarningOrError false "SR001" (e.ToString())
#endif
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.SafeExists(trialPath) then
success trialPath
with e -> logWarningOrError false "SR001" (e.ToString())
#if !FX_RESHAPED_MSBUILD
try
// Seach 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 =
[ for gacdir in Directory.EnumerateDirectories(gac) do
let assdir = Path.Combine(gacdir,n.Name)
if Directory.Exists(assdir) then
for tdir in Directory.EnumerateDirectories(assdir) do
let trialPath = Path.Combine(tdir,qual)
if FileSystem.SafeExists(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 ->
for gacdir in Directory.EnumerateDirectories(gac) do
//printfn "searching GAC directory: %s" gacdir
let assdir = Path.Combine(gacdir,n.Name)
if Directory.Exists(assdir) then
//printfn "searching GAC directory: %s" assdir
let tokText = String.concat "" [| for b in tok -> sprintf "%02x" b |]
let verdir = Path.Combine(assdir,"v4.0_"+v.ToString()+"__"+tokText)
//printfn "searching GAC directory: %s" verdir
if Directory.Exists(verdir) then
let trialPath = Path.Combine(verdir,qual)
//printfn "searching GAC: %s" trialPath
if FileSystem.SafeExists(trialPath) then
success trialPath
with e -> logWarningOrError false "SR001" (e.ToString())
#endif
results.ToArray() }
let internal GetBestAvailableResolver() =
#if !FX_RESHAPED_MSBUILD
let tryMSBuild v =
// Detect if MSBuild is on the machine, if so use the resolver from there
let mb = try Assembly.Load(sprintf "Microsoft.Build.Framework, Version=%s.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" v) |> Option.ofObj with _ -> None
let ass = mb |> Option.bind (fun _ -> try Assembly.Load(sprintf "FSharp.Compiler.Service.MSBuild.v%s" v) |> Option.ofObj with _ -> None)
let ty = ass |> Option.bind (fun ass -> ass.GetType("Microsoft.FSharp.Compiler.MSBuildReferenceResolver") |> Option.ofObj)
let obj = ty |> Option.bind (fun ty -> ty.InvokeMember("get_Resolver",BindingFlags.Static ||| BindingFlags.Public ||| BindingFlags.InvokeMethod ||| BindingFlags.NonPublic, null, null, [| |]) |> Option.ofObj)
let resolver = obj |> Option.bind (fun obj -> match obj with :? Resolver as r -> Some r | _ -> None)
resolver
match tryMSBuild "12" with
| Some r -> r
| None ->
#endif
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