Appearance
| 1 | @@IF.dotnet() | |
| 2 | ||
| 3 | namespace Driver is | |
| 4 | // Locates the .NET reference-assembly directory to compile against when the | |
| 5 | // compiler is invoked with source files but neither `-a` nor | |
| 6 | // `--sdk-ref-path`. The install root comes from DOTNET_ROOT, or is derived | |
| 7 | // from the runtime directory the compiler is itself running on, or failing | |
| 8 | // that from the `dotnet` host on PATH, which is where a natively compiled | |
| 9 | // compiler, running on no shared runtime, finds it; under it, | |
| 10 | // the highest Microsoft.NETCore.App.Ref pack matching the target | |
| 11 | // framework's version supplies `ref/<tfm>/`. Every step answers null rather | |
| 12 | // than throwing, so a layout this doesn't recognise surfaces as the | |
| 13 | // caller's "no reference assemblies supplied" error. | |
| 14 | class SDK_REFERENCE_PATH_RESOLVER is | |
| 15 | init() is si | |
| 16 | ||
| 17 | resolve(target_framework: string) -> string? is | |
| 18 | let dotnet_root = discover_dotnet_root() | |
| 19 | ||
| 20 | if !dotnet_root? then | |
| 21 | return null | |
| 22 | fi | |
| 23 | ||
| 24 | return resolve_under(dotnet_root, target_framework) | |
| 25 | si | |
| 26 | ||
| 27 | resolve_under(dotnet_root: string, target_framework: string) -> string? is | |
| 28 | let version = framework_version(target_framework) | |
| 29 | ||
| 30 | if !version? then | |
| 31 | return null | |
| 32 | fi | |
| 33 | ||
| 34 | let packs_root = IO.Path.join([dotnet_root, "packs", "Microsoft.NETCore.App.Ref"]) | |
| 35 | ||
| 36 | if !IO.Directory.exists(packs_root) then | |
| 37 | return null | |
| 38 | fi | |
| 39 | ||
| 40 | let pack = latest_ref_pack(packs_root, version) | |
| 41 | ||
| 42 | if !pack? then | |
| 43 | return null | |
| 44 | fi | |
| 45 | ||
| 46 | let reference_path = IO.Path.join([pack, "ref", target_framework]) | |
| 47 | ||
| 48 | if !IO.Directory.exists(reference_path) then | |
| 49 | return null | |
| 50 | fi | |
| 51 | ||
| 52 | return reference_path | |
| 53 | si | |
| 54 | ||
| 55 | // "net10.0" -> "10.0", matching ref-pack directory names like "10.0.10". | |
| 56 | // A moniker that isn't `net` followed by a version has no pack of this | |
| 57 | // shape to find, so it answers null rather than globbing on a prefix | |
| 58 | // that would match nothing (or, for a short value, indexing off the end). | |
| 59 | framework_version(target_framework: string) -> string? is | |
| 60 | if target_framework.length <= 3 \/ !target_framework.starts_with("net") then | |
| 61 | return null | |
| 62 | fi | |
| 63 | ||
| 64 | let first = target_framework[3] | |
| 65 | ||
| 66 | if first < '0' \/ first > '9' then | |
| 67 | return null | |
| 68 | fi | |
| 69 | ||
| 70 | return target_framework.substring(3, target_framework.length - 3) | |
| 71 | si | |
| 72 | ||
| 73 | // The runtime directory is `shared/Microsoft.NETCore.App/<version>/` | |
| 74 | // under the install root, so the root is three levels up. Trailing | |
| 75 | // separators have to go first, or get_directory_name consumes one of | |
| 76 | // them instead of a path segment and the walk stops a level short. | |
| 77 | install_root_from_runtime_directory(runtime_directory: string) -> string? is | |
| 78 | let trimmed = strip_trailing_separators(runtime_directory) | |
| 79 | ||
| 80 | if trimmed.length == 0 then | |
| 81 | return null | |
| 82 | fi | |
| 83 | ||
| 84 | let current mut = trimmed | |
| 85 | ||
| 86 | for i in 0..3 do | |
| 87 | let parent = IO.Path.get_directory_name(current) | |
| 88 | ||
| 89 | if !parent? then | |
| 90 | return null | |
| 91 | fi | |
| 92 | ||
| 93 | if parent.length == 0 then | |
| 94 | return null | |
| 95 | fi | |
| 96 | ||
| 97 | current = parent | |
| 98 | od | |
| 99 | ||
| 100 | return current | |
| 101 | si | |
| 102 | ||
| 103 | strip_trailing_separators(path: string) -> string is | |
| 104 | let result mut = path | |
| 105 | ||
| 106 | while result.length > 0 /\ is_separator(result[result.length - 1]) do | |
| 107 | result = result.substring(0, result.length - 1) | |
| 108 | od | |
| 109 | ||
| 110 | return result | |
| 111 | si | |
| 112 | ||
| 113 | // Both of the running platform's separators: on Windows that covers the | |
| 114 | // native `\` as well as `/`, and on Unix `\` is a legal filename | |
| 115 | // character and deliberately isn't stripped. | |
| 116 | is_separator(c: char) -> bool => | |
| 117 | c == IO.Path.directory_separator_char \/ c == IO.Path.alt_directory_separator_char | |
| 118 | ||
| 119 | latest_ref_pack(packs_root: string, framework_version: string) -> string? is | |
| 120 | let best: string? mut = null | |
| 121 | let best_version: System.Version? mut = null | |
| 122 | ||
| 123 | for directory in IO.Directory.get_directories(packs_root, "{framework_version}.*") do | |
| 124 | let version_text = IO.Path.get_file_name(directory) ?? "" | |
| 125 | ||
| 126 | try | |
| 127 | let version = System.Version.parse(version_text) | |
| 128 | ||
| 129 | if !best_version? \/ version.compare_to(best_version) > 0 then | |
| 130 | best_version = version | |
| 131 | best = directory | |
| 132 | fi | |
| 133 | catch ex: System.Exception | |
| 134 | // not a version directory; skip | |
| 135 | yrt | |
| 136 | od | |
| 137 | ||
| 138 | return best | |
| 139 | si | |
| 140 | ||
| 141 | discover_dotnet_root() -> string? is | |
| 142 | let env_root = Driver.COMPILER_ENVIRONMENT.dotnet_root | |
| 143 | ||
| 144 | if env_root? /\ env_root.length > 0 /\ IO.Directory.exists(env_root) then | |
| 145 | return env_root | |
| 146 | fi | |
| 147 | ||
| 148 | let derived = | |
| 149 | install_root_from_runtime_directory( | |
| 150 | System.Runtime.InteropServices.RuntimeEnvironment.get_runtime_directory()) | |
| 151 | ||
| 152 | if derived? /\ is_install_root(derived) then | |
| 153 | return derived | |
| 154 | fi | |
| 155 | ||
| 156 | return install_root_from_search_path(Driver.COMPILER_ENVIRONMENT.path) | |
| 157 | si | |
| 158 | ||
| 159 | // A natively compiled compiler's runtime directory is its own, so the | |
| 160 | // walk up from it lands somewhere that is not an install root at all. | |
| 161 | is_install_root(directory: string) -> bool => | |
| 162 | IO.Directory.exists(IO.Path.join([directory, "shared", "Microsoft.NETCore.App"])) | |
| 163 | ||
| 164 | // The directory of the first `dotnet` host on `search_path` that sits | |
| 165 | // in an install root. A package manager's host is usually a symbolic | |
| 166 | // link into the install root, so the link is followed first. | |
| 167 | install_root_from_search_path(search_path: string?) -> string? is | |
| 168 | if !search_path? then | |
| 169 | return null | |
| 170 | fi | |
| 171 | ||
| 172 | let host_name = | |
| 173 | if System.OperatingSystem.is_windows() then "dotnet.exe" else "dotnet" fi | |
| 174 | ||
| 175 | for directory in search_path.split([IO.Path.path_separator], System.StringSplitOptions.REMOVE_EMPTY_ENTRIES) do | |
| 176 | let candidate = IO.Path.join(directory, host_name) | |
| 177 | ||
| 178 | if !IO.File.exists(candidate) then | |
| 179 | continue | |
| 180 | fi | |
| 181 | ||
| 182 | let host_path mut = candidate | |
| 183 | ||
| 184 | try | |
| 185 | if let target = IO.File.resolve_link_target(candidate, true) then | |
| 186 | host_path = target.full_name | |
| 187 | fi | |
| 188 | catch ex: System.Exception | |
| 189 | // an unresolvable link is tried as it stands | |
| 190 | yrt | |
| 191 | ||
| 192 | if let root = IO.Path.get_directory_name(IO.Path.get_full_path(host_path)) then | |
| 193 | if is_install_root(root) then | |
| 194 | return root | |
| 195 | fi | |
| 196 | fi | |
| 197 | od | |
| 198 | ||
| 199 | return null | |
| 200 | si | |
| 201 | si | |
| 202 | si |