Find the path to the WASI SDK.
(context)
| 218 | |
| 219 | |
| 220 | def wasi_sdk(context): |
| 221 | """Find the path to the WASI SDK.""" |
| 222 | if wasi_sdk_path := context.wasi_sdk_path: |
| 223 | if not wasi_sdk_path.exists(): |
| 224 | raise ValueError( |
| 225 | "WASI SDK not found; " |
| 226 | "download from " |
| 227 | "https://github.com/WebAssembly/wasi-sdk and/or " |
| 228 | "specify via $WASI_SDK_PATH or --wasi-sdk" |
| 229 | ) |
| 230 | return wasi_sdk_path |
| 231 | |
| 232 | with (HERE / "config.toml").open("rb") as file: |
| 233 | config = tomllib.load(file) |
| 234 | wasi_sdk_version = config["targets"]["wasi-sdk"] |
| 235 | |
| 236 | if wasi_sdk_path_env_var := os.environ.get("WASI_SDK_PATH"): |
| 237 | wasi_sdk_path = pathlib.Path(wasi_sdk_path_env_var) |
| 238 | if not wasi_sdk_path.exists(): |
| 239 | raise ValueError( |
| 240 | f"WASI SDK not found at $WASI_SDK_PATH ({wasi_sdk_path})" |
| 241 | ) |
| 242 | else: |
| 243 | opt_path = pathlib.Path("/opt") |
| 244 | # WASI SDK versions have a ``.0`` suffix, but it's a constant; the WASI SDK team |
| 245 | # has said they don't plan to ever do a point release and all of their Git tags |
| 246 | # lack the ``.0`` suffix. |
| 247 | # Starting with WASI SDK 23, the tarballs went from containing a directory named |
| 248 | # ``wasi-sdk-{WASI_SDK_VERSION}.0`` to e.g. |
| 249 | # ``wasi-sdk-{WASI_SDK_VERSION}.0-x86_64-linux``. |
| 250 | potential_sdks = [ |
| 251 | path |
| 252 | for path in opt_path.glob(f"wasi-sdk-{wasi_sdk_version}.0*") |
| 253 | if path.is_dir() |
| 254 | ] |
| 255 | if len(potential_sdks) == 1: |
| 256 | wasi_sdk_path = potential_sdks[0] |
| 257 | elif (default_path := opt_path / "wasi-sdk").is_dir(): |
| 258 | wasi_sdk_path = default_path |
| 259 | |
| 260 | # Starting with WASI SDK 25, a VERSION file is included in the root |
| 261 | # of the SDK directory that we can read to warn folks when they are using |
| 262 | # an unsupported version. |
| 263 | if wasi_sdk_path and (version_file := wasi_sdk_path / "VERSION").is_file(): |
| 264 | version_details = version_file.read_text(encoding="utf-8") |
| 265 | found_version = version_details.splitlines()[0] |
| 266 | # Make sure there's a trailing dot to avoid false positives if somehow the |
| 267 | # supported version is a prefix of the found version (e.g. `25` and `2567`). |
| 268 | if not found_version.startswith(f"{wasi_sdk_version}."): |
| 269 | major_version = found_version.partition(".")[0] |
| 270 | log( |
| 271 | "⚠️", |
| 272 | f" Found WASI SDK {major_version}, " |
| 273 | f"but WASI SDK {wasi_sdk_version} is the supported version", |
| 274 | ) |
| 275 | |
| 276 | # Cache the result. |
| 277 | context.wasi_sdk_path = wasi_sdk_path |
no test coverage detected
searching dependent graphs…