Appearance
| 1 | namespace Driver is | |
| 2 | use IO.Std | |
| 3 | ||
| 4 | use Collections.Iterable | |
| 5 | use Collections.LIST | |
| 6 | ||
| 7 | use Ghul.Pipes | |
| 8 | ||
| 9 | class PATH_CONFIG is | |
| 10 | _default_library_prefix: string | |
| 11 | _library_prefix: string? | |
| 12 | ||
| 13 | install_folder: string | |
| 14 | ||
| 15 | // The .NET reference-assembly directory. Either supplied by the build | |
| 16 | // via `--sdk-ref-path`, or filled in by auto-discovery when the | |
| 17 | // compiler is invoked with source files alone. Only consulted when no | |
| 18 | // assemblies are passed with `-a`; MSBuild builds pass the full | |
| 19 | // resolved reference set and never reach either route. The | |
| 20 | // auto-discovered value is written back here so the target-framework | |
| 21 | // resolver can pick it up when generating `.runtimeconfig.json`. | |
| 22 | sdk_reference_path: string? public | |
| 23 | ||
| 24 | // Explicit override from the `--target-framework` flag, used when | |
| 25 | // deriving the .runtimeconfig.json moniker beside the compiled binary. | |
| 26 | // Wins over inference from `sdk_reference_path` and the host TFM. | |
| 27 | target_framework: string? public | |
| 28 | ||
| 29 | _host_target_framework: string? | |
| 30 | _host_target_framework_resolved: bool | |
| 31 | ||
| 32 | // The TFM the compiler was itself built for, read from its own | |
| 33 | // .runtimeconfig.json (sibling of the executing assembly). Cached. | |
| 34 | // Returns null when the file is missing or doesn't contain a "tfm" | |
| 35 | // field — the resolver chain falls back to a hardcoded default. | |
| 36 | @IF.dotnet() | |
| 37 | host_target_framework: string? public is | |
| 38 | if _host_target_framework_resolved then | |
| 39 | return _host_target_framework | |
| 40 | fi | |
| 41 | ||
| 42 | _host_target_framework_resolved = true | |
| 43 | ||
| 44 | let assembly_path = _executing_assembly_path() | |
| 45 | let directory = IO.Path.get_directory_name(assembly_path) | |
| 46 | let name = IO.Path.get_file_name_without_extension(assembly_path) | |
| 47 | let config_path = IO.Path.join(directory, "{name}.runtimeconfig.json") | |
| 48 | ||
| 49 | if !IO.File.exists(config_path) then | |
| 50 | return null | |
| 51 | fi | |
| 52 | ||
| 53 | try | |
| 54 | _host_target_framework = TARGET_FRAMEWORK_RESOLVER().extract_tfm(IO.File.read_all_text(config_path)) | |
| 55 | catch ex: System.Exception | |
| 56 | _host_target_framework = null | |
| 57 | yrt | |
| 58 | ||
| 59 | return _host_target_framework | |
| 60 | si | |
| 61 | ||
| 62 | library_prefix: string public => | |
| 63 | if _library_prefix? then | |
| 64 | _library_prefix | |
| 65 | else | |
| 66 | _default_library_prefix | |
| 67 | fi, | |
| 68 | = value is | |
| 69 | _library_prefix = value | |
| 70 | ||
| 71 | if !library_prefix.ends_with('/') then | |
| 72 | library_prefix = "{library_prefix}/" | |
| 73 | fi | |
| 74 | si | |
| 75 | ||
| 76 | init() is | |
| 77 | super.init() | |
| 78 | ||
| 79 | let assembly_path = _executing_assembly_path() | |
| 80 | ||
| 81 | install_folder = IO.Path.get_directory_name(assembly_path) ?? "." | |
| 82 | ||
| 83 | if !install_folder.ends_with('/') then | |
| 84 | install_folder = "{install_folder}/" | |
| 85 | fi | |
| 86 | ||
| 87 | _default_library_prefix = "{install_folder}lib/" | |
| 88 | ||
| 89 | si | |
| 90 | ||
| 91 | ||
| 92 | // A native or single-file build has no assembly file, and its | |
| 93 | // location is empty: the files that would sit beside it sit beside | |
| 94 | // the executable instead. | |
| 95 | @IF.dotnet() | |
| 96 | _executing_assembly_path() -> string is | |
| 97 | let executing_assembly = System.Reflection.Assembly.get_executing_assembly() | |
| 98 | let location = executing_assembly.location | |
| 99 | ||
| 100 | if location.length > 0 then | |
| 101 | return IO.Path.get_full_path(location) | |
| 102 | fi | |
| 103 | ||
| 104 | return IO.Path.join(System.AppContext.base_directory, "{executing_assembly.get_name().name}.dll") | |
| 105 | si | |
| 106 | ||
| 107 | get_library_location(directory: string mut) -> string is | |
| 108 | if !directory.starts_with('/') then | |
| 109 | directory = "{library_prefix}{directory}" | |
| 110 | fi | |
| 111 | ||
| 112 | if !directory.ends_with('/') then | |
| 113 | directory = "{directory}/" | |
| 114 | fi | |
| 115 | ||
| 116 | return directory | |
| 117 | si | |
| 118 | ||
| 119 | ||
| 120 | // Auto-discover the .NET reference-assembly directory. Called when the | |
| 121 | // compiler is invoked with source files but no -a or --sdk-ref-path, so | |
| 122 | // a user can run `ghul-compiler hello.ghul` without a .ghulproj. | |
| 123 | // | |
| 124 | // The moniker is chosen in the same order the target-framework resolver | |
| 125 | // uses when it writes the .runtimeconfig.json beside the binary, so the | |
| 126 | // reference assemblies compiled against are the ones the program will | |
| 127 | // then ask to run on. A requested framework with no matching ref pack | |
| 128 | // resolves to nothing, and surfaces as the no-reference-assemblies | |
| 129 | // error rather than a silent mismatch. | |
| 130 | @IF.dotnet() | |
| 131 | discover_sdk_reference_path() -> string? => | |
| 132 | SDK_REFERENCE_PATH_RESOLVER().resolve( | |
| 133 | target_framework ?? host_target_framework ?? "net10.0") | |
| 134 | ||
| 135 | @IF.not.dotnet() | |
| 136 | host_target_framework: string? => null | |
| 137 | ||
| 138 | // The compiler is the executable itself, so the files that sit | |
| 139 | // beside it are found from its path. | |
| 140 | @IF.not.dotnet() | |
| 141 | _executing_assembly_path() -> string => Driver.COMPILER_ENVIRONMENT.process_path ?? "." | |
| 142 | ||
| 143 | @IF.not.dotnet() | |
| 144 | discover_sdk_reference_path() -> string? => null | |
| 145 | si | |
| 146 | si |