-
Notifications
You must be signed in to change notification settings - Fork 786
/
CompilerLocation.fs
363 lines (304 loc) · 15.1 KB
/
CompilerLocation.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Internal.Utilities
open System
open System.Diagnostics
open System.IO
open System.Reflection
open System.Runtime.InteropServices
open Microsoft.FSharp.Core
#nowarn "44" // ConfigurationSettings is obsolete but the new stuff is horribly complicated.
module internal FSharpEnvironment =
type private TypeInThisAssembly =
class
end
/// The F# version reported in the banner
let FSharpBannerVersion =
UtilsStrings.SR.fSharpBannerVersion (FSharp.BuildProperties.fsProductVersion, FSharp.BuildProperties.fsLanguageVersion)
let FSharpProductName = UtilsStrings.SR.buildProductName (FSharpBannerVersion)
let versionOf<'t> = typeof<'t>.Assembly.GetName().Version.ToString()
let FSharpCoreLibRunningVersion =
try
match versionOf<Unit> with
| null -> None
| "" -> None
| s -> Some(s)
with _ ->
None
// The F# binary format revision number. The first three digits of this form the significant part of the
// format revision number for F# binary signature and optimization metadata. The last digit is not significant.
//
// WARNING: Do not change this revision number unless you absolutely know what you're doing.
let FSharpBinaryMetadataFormatRevision = "2.0.0.0"
let isRunningOnCoreClr =
typeof<obj>.Assembly.FullName.StartsWith ("System.Private.CoreLib", StringComparison.InvariantCultureIgnoreCase)
module Option =
/// Convert string into Option string where null and String.Empty result in None
let ofString s =
if String.IsNullOrEmpty(s) then None else Some(s)
let internal tryCurrentDomain () =
let pathFromCurrentDomain = AppDomain.CurrentDomain.BaseDirectory
if not (String.IsNullOrEmpty(pathFromCurrentDomain)) then
Some pathFromCurrentDomain
else
None
// The default location of FSharp.Core.dll and fsc.exe based on the version of fsc.exe that is running
// Used for
// - location of design-time copies of FSharp.Core.dll and FSharp.Compiler.Interactive.Settings.dll for the default assumed environment for scripts
// - default ToolPath in tasks in FSharp.Build.dll (for Fsc tasks, but note a probe location is given)
// - default F# binaries directory in service.fs (REVIEW: check this)
// - default location of fsi.exe in FSharp.VS.FSI.dll (REVIEW: check this)
// - default F# binaries directory in (project system) Project.fs
let BinFolderOfDefaultFSharpCompiler (probePoint: string option) =
// Check for an app.config setting to redirect the default compiler location
// Like fsharp-compiler-location
try
// We let you set FSHARP_COMPILER_BIN. I've rarely seen this used and its not documented in the install instructions.
match Environment.GetEnvironmentVariable("FSHARP_COMPILER_BIN") with
| result when not (String.IsNullOrWhiteSpace result) -> Some result
| _ ->
let safeExists f =
(try
File.Exists(f)
with _ ->
false)
// Look in the probePoint if given, e.g. look for a compiler alongside of FSharp.Build.dll
match probePoint with
| Some p when safeExists (Path.Combine(p, "FSharp.Core.dll")) -> Some p
| _ ->
let fallback () =
let d = Assembly.GetExecutingAssembly()
Some(Path.GetDirectoryName d.Location)
match tryCurrentDomain () with
| None -> fallback ()
| Some path -> Some path
with e ->
None
// Specify the tooling-compatible fragments of a path such as:
// typeproviders/fsharp41/net461/MyProvider.DesignTime.dll
// tools/fsharp41/net461/MyProvider.DesignTime.dll
// See https://github.com/dotnet/fsharp/issues/3736
// Represents the F#-compiler <-> type provider protocol.
// When the API or protocol updates, add a new version moniker to the front of the list here.
let toolingCompatibleTypeProviderProtocolMonikers () = [ "fsharp41" ]
// Detect the host tooling context
let toolingCompatibleVersions =
if typeof<obj>.Assembly.GetName().Name = "mscorlib" then
[|
"net481"
"net48"
"net472"
"net471"
"net47"
"net462"
"net461"
"net452"
"net451"
"net45"
"netstandard2.0"
|]
elif typeof<obj>.Assembly.GetName().Name = "System.Private.CoreLib" then
[|
"net8.0"
"net7.0"
"net6.0"
"net5.0"
"netcoreapp3.1"
"netcoreapp3.0"
"netstandard2.1"
"netcoreapp2.2"
"netcoreapp2.1"
"netcoreapp2.0"
"netstandard2.0"
|]
else
Debug.Assert(false, "Couldn't determine runtime tooling context, assuming it supports at least .NET Standard 2.0")
[| "netstandard2.0" |]
let toolPaths = [| "tools"; "typeproviders" |]
let toolingCompatiblePaths () =
[
for toolPath in toolPaths do
for protocol in toolingCompatibleTypeProviderProtocolMonikers () do
for netRuntime in toolingCompatibleVersions do
yield Path.Combine(toolPath, protocol, netRuntime)
]
let searchToolPath compilerToolPath =
seq {
yield compilerToolPath
for toolPath in toolingCompatiblePaths () do
yield Path.Combine(compilerToolPath, toolPath)
}
let rec searchToolPaths path compilerToolPaths =
seq {
for toolPath in compilerToolPaths do
yield! searchToolPath toolPath
match path with
| None -> ()
| Some path -> yield! searchToolPath path
}
let getTypeProviderAssembly
(
runTimeAssemblyFileName: string,
designTimeAssemblyName: string,
compilerToolPaths: string list,
raiseError
) =
// Find and load the designer assembly for the type provider component.
// We look in the directories stepping up from the location of the runtime assembly.
let loadFromLocation designTimeAssemblyPath =
try
Some(Assembly.UnsafeLoadFrom designTimeAssemblyPath)
with e ->
raiseError (Some designTimeAssemblyPath) e
let rec searchParentDirChain path assemblyName =
seq {
match path with
| None -> ()
| Some (p: string) ->
match Path.GetDirectoryName(p) with
| s when s = "" || s = null || Path.GetFileName(p) = "packages" || s = p -> ()
| parentDir -> yield! searchParentDirChain (Some parentDir) assemblyName
for p in searchToolPaths path compilerToolPaths do
let fileName = Path.Combine(p, assemblyName)
if File.Exists fileName then
yield fileName
}
let loadFromParentDirRelativeToRuntimeAssemblyLocation designTimeAssemblyName =
let runTimeAssemblyPath = Path.GetDirectoryName runTimeAssemblyFileName
let paths = searchParentDirChain (Some runTimeAssemblyPath) designTimeAssemblyName
paths
|> Seq.tryHead
|> function
| Some res -> loadFromLocation res
| None ->
// The search failed, just load from the first location and report an error
let runTimeAssemblyPath = Path.GetDirectoryName runTimeAssemblyFileName
loadFromLocation (Path.Combine(runTimeAssemblyPath, designTimeAssemblyName))
if designTimeAssemblyName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) then
loadFromParentDirRelativeToRuntimeAssemblyLocation designTimeAssemblyName
else
// Cover the case where the ".dll" extension has been left off and no version etc. has been used in the assembly
// string specification. The Name=FullName comparison is particularly strange, and was there to support
// design-time DLLs specified using "x.DesignTIme, Version= ..." long assembly names and GAC loads.
// These kind of design-time assembly specifications are no longer used to our knowledge so that comparison is basically legacy
// and will always succeed.
let name = AssemblyName(Path.GetFileNameWithoutExtension designTimeAssemblyName)
if name.Name.Equals(name.FullName, StringComparison.OrdinalIgnoreCase) then
let designTimeFileName = designTimeAssemblyName + ".dll"
loadFromParentDirRelativeToRuntimeAssemblyLocation designTimeFileName
else
// Load from the GAC using Assembly.Load. This is legacy since type provider design-time components are
// never in the GAC these days and "x.DesignTIme, Version= ..." specifications are never used.
try
let name = AssemblyName designTimeAssemblyName
Some(Assembly.Load name)
with e ->
raiseError None e
let getCompilerToolsDesignTimeAssemblyPaths compilerToolPaths = searchToolPaths None compilerToolPaths
let getFSharpCoreLibraryName = "FSharp.Core"
let fsiLibraryName = "FSharp.Compiler.Interactive.Settings"
let getFSharpCompilerLocationWithDefaultFromType (defaultLocation: Type) =
let location =
try
Some(Path.GetDirectoryName(defaultLocation.Assembly.Location))
with _ ->
None
match BinFolderOfDefaultFSharpCompiler(location) with
| Some path -> path
| None ->
let path = location |> Option.defaultValue "<null>"
#if DEBUG
Debug.Print(
sprintf
"""FSharpEnvironment.BinFolderOfDefaultFSharpCompiler (Some '%s') returned None Location
customized incorrectly: algorithm here: https://github.com/dotnet/fsharp/blob/03f3f1c35f82af26593d025dabca57a6ef3ea9a1/src/utils/CompilerLocationUtils.fs#L171"""
path
)
#endif
// Use the location of this dll
path
// Fallback to ambient FSharp.CompilerService.dll
let getFSharpCompilerLocation () =
Path.Combine(getFSharpCompilerLocationWithDefaultFromType (typeof<TypeInThisAssembly>))
// Fallback to ambient FSharp.Core.dll
let getDefaultFSharpCoreLocation () =
Path.Combine(getFSharpCompilerLocationWithDefaultFromType (typeof<Unit>), getFSharpCoreLibraryName + ".dll")
// Must be alongside the location of FSharp.CompilerService.dll
let getDefaultFsiLibraryLocation () =
Path.Combine(Path.GetDirectoryName(getFSharpCompilerLocation ()), fsiLibraryName + ".dll")
let isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
let dotnet = if isWindows then "dotnet.exe" else "dotnet"
let fileExists pathToFile =
try
File.Exists(pathToFile)
with _ ->
false
// Look for global install of dotnet sdk
let getDotnetGlobalHostPath () =
let pf = Environment.GetEnvironmentVariable("ProgramW6432")
let pf =
if String.IsNullOrEmpty(pf) then
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
else
pf
let candidate = Path.Combine(pf, "dotnet", dotnet)
if fileExists candidate then
Some candidate
else
// Can't find it --- give up
None
let getDotnetHostPath () =
// How to find dotnet.exe --- woe is me; probing rules make me sad.
// Algorithm:
// 1. Look for DOTNET_HOST_PATH environment variable
// this is the main user programable override .. provided by user to find a specific dotnet.exe
// 2. Probe for are we part of an .NetSDK install
// In an sdk install we are always installed in: sdk\3.0.100-rc2-014234\FSharp
// dotnet or dotnet.exe will be found in the directory that contains the sdk directory
// 3. We are loaded in-process to some other application ... Eg. try .net
// See if the host is dotnet.exe ... from net5.0 on this is fairly unlikely
// 4. If it's none of the above we are going to have to rely on the path containing the way to find dotnet.exe
// Use the path to search for dotnet.exe
let probePathForDotnetHost () =
let paths =
let p = Environment.GetEnvironmentVariable("PATH")
if not (isNull p) then p.Split(Path.PathSeparator) else [||]
paths |> Array.tryFind (fun f -> fileExists (Path.Combine(f, dotnet)))
match (Environment.GetEnvironmentVariable("DOTNET_HOST_PATH")) with
// Value set externally
| value when not (String.IsNullOrEmpty(value)) && fileExists value -> Some value
| _ ->
// Probe for netsdk install, dotnet. and dotnet.exe is a constant offset from the location of System.Int32
let candidate =
let assemblyLocation =
Path.GetDirectoryName(typeof<Int32>.GetTypeInfo().Assembly.Location)
Path.GetFullPath(Path.Combine(assemblyLocation, "..", "..", "..", dotnet))
if fileExists candidate then
Some candidate
else
match probePathForDotnetHost () with
| Some f -> Some(Path.Combine(f, dotnet))
| None -> getDotnetGlobalHostPath ()
let getDotnetHostDirectories () =
let isDotnetMultilevelLookup =
(Int32.TryParse(Environment.GetEnvironmentVariable("DOTNET_MULTILEVEL_LOOKUP"))
|> snd)
<> 0
[|
match getDotnetHostPath (), getDotnetGlobalHostPath () with
| Some hostPath, Some globalHostPath ->
yield Path.GetDirectoryName(hostPath)
if isDotnetMultilevelLookup && hostPath <> globalHostPath then
yield Path.GetDirectoryName(globalHostPath)
| Some hostPath, None -> yield Path.GetDirectoryName(hostPath)
| None, Some globalHostPath -> yield Path.GetDirectoryName(globalHostPath)
| None, None -> ()
|]
let getDotnetHostDirectory () =
getDotnetHostDirectories () |> Array.tryHead
let getDotnetHostSubDirectories (path: string) =
[|
for directory in getDotnetHostDirectories () do
let subdirectory = Path.Combine(directory, path)
if Directory.Exists(subdirectory) then
yield! DirectoryInfo(subdirectory).GetDirectories()
|]