|
| 1 | +import { strict as assert } from "node:assert"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | +import { describe, test } from "node:test"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | + |
| 6 | +const BUILD_RS_PATH = fileURLToPath(new URL("../../packages/native/build.rs", import.meta.url)); |
| 7 | +const BUILD_NATIVE_SCRIPT_PATH = fileURLToPath( |
| 8 | + new URL("../../packages/native/scripts/build-native.mjs", import.meta.url), |
| 9 | +); |
| 10 | +const PREBUILD_WORKFLOW_PATH = fileURLToPath( |
| 11 | + new URL("../../.github/workflows/prebuild.yml", import.meta.url), |
| 12 | +); |
| 13 | +const RELEASE_WORKFLOW_PATH = fileURLToPath( |
| 14 | + new URL("../../.github/workflows/release.yml", import.meta.url), |
| 15 | +); |
| 16 | + |
| 17 | +const STATIC_CRT_RUSTFLAGS = "-C target-feature=+crt-static"; |
| 18 | + |
| 19 | +function assertWindowsPrebuildUsesStaticCrt(workflow, label) { |
| 20 | + assert.match( |
| 21 | + workflow, |
| 22 | + new RegExp( |
| 23 | + [ |
| 24 | + "- name: Build prebuild", |
| 25 | + "[\\s\\S]*?env:", |
| 26 | + `[\\s\\S]*?RUSTFLAGS: \\$\\{\\{ runner\\.os == 'Windows' && '${STATIC_CRT_RUSTFLAGS.replace("+", "\\+")}' \\|\\| '' \\}\\}`, |
| 27 | + '[\\s\\S]*?run: npx napi build --platform --release --target "\\$\\{\\{ matrix\\.rustTarget \\}\\}" --js false', |
| 28 | + ].join("\\n?"), |
| 29 | + "u", |
| 30 | + ), |
| 31 | + `${label} Windows prebuilds must pass Rust +crt-static`, |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +describe("native Windows CRT linking", () => { |
| 36 | + test("local Windows native builds use static MSVC CRT", () => { |
| 37 | + const buildRs = readFileSync(BUILD_RS_PATH, "utf8"); |
| 38 | + const buildScript = readFileSync(BUILD_NATIVE_SCRIPT_PATH, "utf8"); |
| 39 | + |
| 40 | + assert.match(buildRs, /CARGO_CFG_TARGET_ENV"\)\.as_deref\(\) == Ok\("msvc"\)/); |
| 41 | + assert.match( |
| 42 | + buildRs, |
| 43 | + /target_features\.split\(','\)\.any\(\|feature\| feature == "crt-static"\)/, |
| 44 | + ); |
| 45 | + assert.match(buildRs, /build\.static_crt\(true\)/); |
| 46 | + assert.match( |
| 47 | + buildScript, |
| 48 | + /const WINDOWS_STATIC_CRT_RUSTFLAGS = "-C target-feature=\+crt-static";/, |
| 49 | + ); |
| 50 | + assert.match(buildScript, /function isWindowsMsvcTarget\(targetTriple\)/); |
| 51 | + assert.match(buildScript, /function withWindowsStaticCrtEnv\(env, hostTargetTriple\)/); |
| 52 | + }); |
| 53 | + |
| 54 | + test("published Windows native prebuilds use static MSVC CRT", () => { |
| 55 | + assertWindowsPrebuildUsesStaticCrt( |
| 56 | + readFileSync(PREBUILD_WORKFLOW_PATH, "utf8"), |
| 57 | + "manual prebuild workflow", |
| 58 | + ); |
| 59 | + assertWindowsPrebuildUsesStaticCrt( |
| 60 | + readFileSync(RELEASE_WORKFLOW_PATH, "utf8"), |
| 61 | + "release workflow", |
| 62 | + ); |
| 63 | + }); |
| 64 | +}); |
0 commit comments