Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

2.3 KB
Raw Blame
// Pure helpers that map a target CPU architecture to the parameters the Windows
// native-helper build needs. Kept side-effect free so it is unit-testable and
// importable from both the build script and Vitest.

export const SUPPORTED_ARCHES = ["x64", "arm64"];

const ALIASES = new Map([
	["x64", "x64"],
	["amd64", "x64"],
	["x86_64", "x64"],
	["arm64", "arm64"],
	["aarch64", "arm64"],
]);

export function normalizeArch(value) {
	if (value === undefined || value === null || value === "") {
		return undefined;
	}
	return ALIASES.get(String(value).toLowerCase());
}

// `--arch` with nothing after it must not read as "flag absent": that would fall
// through to the host and quietly produce a binary for the wrong architecture.
export function parseArchFlag(argv) {
	const eq = argv.find((a) => a.startsWith("--arch="));
	const idx = argv.indexOf("--arch");
	if (eq === undefined && idx === -1) {
		return undefined;
	}
	const value = eq !== undefined ? eq.slice("--arch=".length) : argv[idx + 1];
	if (!value || value.startsWith("-")) {
		throw new Error(`--arch requires a value (${SUPPORTED_ARCHES.join(" or ")}).`);
	}
	return value;
}

export function resolveTargetArch({ cliArch, envArch, hostArch } = {}) {
	for (const [label, raw] of [
		["--arch", cliArch],
		["OPENSCREEN_WIN_HELPER_ARCH", envArch],
	]) {
		if (raw !== undefined && raw !== null && raw !== "") {
			const normalized = normalizeArch(raw);
			if (!normalized) {
				throw new Error(
					`Invalid ${label} value "${raw}". Expected one of: ${SUPPORTED_ARCHES.join(", ")}.`,
				);
			}
			return normalized;
		}
	}

	const host = normalizeArch(hostArch);
	if (!host) {
		throw new Error(
			`Unsupported host architecture "${hostArch}". Pass --arch with one of: ${SUPPORTED_ARCHES.join(", ")}.`,
		);
	}
	return host;
}

export function resolveVcvarsArch(hostArch, targetArch) {
	const host = normalizeArch(hostArch);
	const target = normalizeArch(targetArch);
	if (!host || !target) {
		throw new Error(`Unsupported architecture pair host="${hostArch}" target="${targetArch}".`);
	}
	return host === target ? target : `${host}_${target}`;
}

export function winBinDirName(targetArch) {
	const target = normalizeArch(targetArch);
	if (!target) {
		throw new Error(`Unsupported target architecture "${targetArch}".`);
	}
	return `win32-${target}`;
}