Files
scriptc/packages/runtime-pack-common/scripts/zlib-inputs.mjs
T
Chris Tate 133551d76e Build node:zlib on the zlib Node bundles
The runtime vendored zlib 1.3.1 and archived it at -Os, and host C-lane
builds linked the system libz, so deflate, raw deflate and gzip output
differed from Node's at every level. The vendored copy is now Node
v24.22.0's deps/zlib (Chromium's zlib, 1.3.2.1-motley-285e94b), built at
-O3 with the units and defines Node's zlib.gyp selects per target: the
SSSE3 Adler-32 and SSE2 inflate and slide-hash code on x86-64, the NEON
and ARMv8 CRC-32 code on arm64, and the portable code on wasm32. The SIMD
units check the CPU at run time and give the same output, so compressed
bytes match Node's on every target.

zlibInputs (runtime-pack-common/scripts/zlib-inputs.mjs for the runtime
packs, vendor-inputs.ts for Zig cross builds and the sanitized runtime)
holds the per-target table, and a unit test keeps the two identical.
Every C-lane driver now links the vendored objects instead of -lz.
Chromium's BSD license ships beside zlib's in the runtime packs.

Corpus 8405 compares compressed bytes with Node across levels,
strategies, memLevel, windowBits, dictionaries and flushing streams;
8406 covers chunked inflation with small input pieces and output chunks.
2026-10-26 15:12:51 -05:00

84 lines
2.8 KiB
JavaScript

// The translation units and defines of the vendored zlib (packages/runtime/vendor/zlib,
// the copy Node v24.22.0 bundles in deps/zlib), chosen per target the way Node's
// deps/zlib/zlib.gyp chooses them. The SIMD units carry their own target attributes
// and check the CPU at run time, so every unit builds with the same flags. Compressed
// output does not depend on these choices. packages/compiler/src/backend/vendor-inputs.ts
// keeps the same table for cross builds; a unit test checks that the two agree.
const COMMON_SOURCES = [
"adler32.c",
"compress.c",
"cpu_features.c",
"crc32.c",
"deflate.c",
"infback.c",
"inffast.c",
"inftrees.c",
"trees.c",
"uncompr.c",
"zutil.c",
];
/**
* @param {string} triple an LLVM or Zig target triple, such as
* "x86_64-unknown-linux-gnu", "arm64-apple-ios15.0" or "aarch64-linux-musl".
* @returns {{ sources: string[], defines: string[] }}
*/
export function zlibInputs(triple) {
const [cpu = "", ...rest] = triple.split("-");
const system = rest.join("-");
const architecture =
cpu === "x86_64" || cpu === "amd64"
? "x64"
: cpu === "aarch64" || cpu === "arm64"
? "arm64"
: cpu;
const ios = system.includes("ios");
const windows =
system.includes("windows") || system.includes("win32") || system.includes("mingw");
// Android reads the CPU features through getauxval, as Linux does: Node's
// ARMV8_OS_ANDROID path needs the NDK's cpu-features library.
const linux = system.includes("linux") || system.includes("android");
const mac = !ios && (system.includes("darwin") || system.includes("macos"));
const sources = [...COMMON_SOURCES];
const defines = windows ? [] : ["HAVE_HIDDEN"];
if (architecture === "x64" && !ios) {
sources.push(
"adler32_simd.c",
"contrib/optimizations/inffast_chunk.c",
"contrib/optimizations/inflate.c",
);
defines.push(
"ADLER32_SIMD_SSSE3",
windows ? "X86_WINDOWS" : "X86_NOT_WINDOWS",
"INFLATE_CHUNK_SIMD_SSE2",
"INFLATE_CHUNK_READ_64LE",
"DEFLATE_SLIDE_HASH_SSE2",
);
} else if (architecture === "arm64") {
sources.push("contrib/optimizations/inffast_chunk.c", "contrib/optimizations/inflate.c");
defines.push(
"INFLATE_CHUNK_SIMD_NEON",
"INFLATE_CHUNK_READ_64LE",
"__ARM_NEON__",
"DEFLATE_SLIDE_HASH_NEON",
);
if (windows) {
defines.push("ARMV8_OS_WINDOWS");
} else if (linux || mac) {
// Node builds the Adler-32 and CRC-32 units for every arm64 target but
// iOS and Windows.
sources.push("adler32_simd.c", "crc32_simd.c");
defines.push(
"ADLER32_SIMD_NEON",
"CRC32_ARMV8_CRC32",
linux ? "ARMV8_OS_LINUX" : "ARMV8_OS_MACOS",
);
}
} else {
sources.push("inflate.c");
defines.push("CPU_NO_SIMD");
}
return { sources, defines };
}