Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: get zig build test working #18207

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 81 additions & 11 deletions .buildkite/ci.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function getTargetLabel(target) {
* @property {string} release
* @property {Tier} [tier]
* @property {string[]} [features]
* @property {boolean} [zigTests] This platform wants Zig unit tests.
*/

/**
Expand All @@ -105,7 +106,7 @@ const buildPlatforms = [
{ os: "darwin", arch: "aarch64", release: "14" },
{ os: "darwin", arch: "x64", release: "14" },
{ os: "linux", arch: "aarch64", distro: "amazonlinux", release: "2023", features: ["docker"] },
{ os: "linux", arch: "x64", distro: "amazonlinux", release: "2023", features: ["docker"] },
{ os: "linux", arch: "x64", distro: "amazonlinux", release: "2023", features: ["docker"], zigTests: true },
{ os: "linux", arch: "x64", baseline: true, distro: "amazonlinux", release: "2023", features: ["docker"] },
{ os: "linux", arch: "aarch64", abi: "musl", distro: "alpine", release: "3.21" },
{ os: "linux", arch: "x64", abi: "musl", distro: "alpine", release: "3.21" },
Expand Down Expand Up @@ -476,18 +477,42 @@ function getBuildZigStep(platform, options) {
}

/**
* Similar to {@link getBuildZigStep}, but builds Zig unit tests instead of the
* normal `bun-zig` object file.
* @param {Platform} platform
* @param {PipelineOptions} options
* @returns {Step}
*/
function getLinkBunStep(platform, options) {
function getBuildZigTestsStep(platform, options) {
const toolchain = getBuildToolchain(platform);
return {
key: `${getTargetKey(platform)}-build-bun`,
label: `${getTargetLabel(platform)} - build-bun`,
key: `${getTargetKey(platform)}-build-zig-tests`,
label: `${getTargetLabel(platform)} - build-zig-tests`,
agents: getZigAgent(platform, options),
retry: getRetry(),
cancel_on_build_failing: isMergeQueue(),
env: getBuildEnv(platform, options),
// note: uses same target name. See `BuildBun.cmake` for details.
command: `bun run build:ci:test --target bun-zig --toolchain ${toolchain}`,
timeout_in_minutes: 35,
};
}

/**
* @param {Platform} platform
* @param {PipelineOptions} options
* @param {boolean} zigTests link Zig unit tests instead of the normal `bun-zig` object file. Default: `false`
* @returns {Step}
*/
function getLinkBunStep(platform, options, zigTests = false) {
const suffix = zigTests ? "build-bug-zig-tests" : "build-bun";
return {
key: `${getTargetKey(platform)}-${suffix}`,
label: `${getTargetLabel(platform)} - ${suffix}`,
depends_on: [
`${getTargetKey(platform)}-build-vendor`,
`${getTargetKey(platform)}-build-cpp`,
`${getTargetKey(platform)}-build-zig`,
`${getTargetKey(platform)}-build-zig${zigTests ? "-tests" : ""}`,
],
agents: getCppAgent(platform, options),
retry: getRetry(),
Expand All @@ -496,7 +521,7 @@ function getLinkBunStep(platform, options) {
BUN_LINK_ONLY: "ON",
...getBuildEnv(platform, options),
},
command: "bun run build:ci --target bun",
command: `bun run ${zigTests ? "build:ci:test" : "build:ci"} --target bun`,
};
}

Expand Down Expand Up @@ -563,6 +588,33 @@ function getTestBunStep(platform, options, testOptions = {}) {
};
}

/**
* @param {Platform} platform
* @param {PipelineOptions} options
* @param {TestOptions} [testOptions]
* @returns {Step}
*/
function getZigTestBunStep(platform, options, testOptions = {}) {
const { buildId } = testOptions;

const depends = [];
if (!buildId) {
depends.push(`${getTargetKey(platform)}-build-bun-zig-tests`);
}
const profile = platform.profile?.toLowerCase() ?? "release";
return {
key: `${getPlatformKey(platform)}-test-zig-bun`,
label: `${getPlatformLabel(platform)} - test-zig-bun`,
depends_on: depends,
agents: getTestAgent(platform, options),
retry: getRetry(),
cancel_on_build_failing: isMergeQueue(),
// TODO: run tests in parallel
// parallelism: unifiedTests ? undefined : os === "darwin" ? 2 : 10,
command: `./build/${profile}/bun-test`,
};
}

/**
* @param {Platform} platform
* @param {PipelineOptions} options
Expand Down Expand Up @@ -1028,6 +1080,7 @@ async function getPipelineOptions() {
*/
async function getPipeline(options = {}) {
const priority = getPriority();
const isMain = isMainBranch();

if (isBuildManual() && !Object.keys(options).length) {
return {
Expand Down Expand Up @@ -1076,6 +1129,13 @@ async function getPipeline(options = {}) {
}
}

/**
* @param {Platform} target
* @returns {boolean}
*/
const targetWantsZigTest = target =>
Boolean(target.zigTests && (!(isMain || options.skipTests) || options.forceTests));

if (!buildId) {
steps.push(
...buildPlatforms
Expand All @@ -1094,6 +1154,11 @@ async function getPipeline(options = {}) {
getBuildCppStep(target, options),
getBuildZigStep(target, options),
getLinkBunStep(target, options),
// TODO: blocked by https://github.com/ziglang/zig/issues/23281
// un-comment once fixed.
// ...(targetWantsZigTest(target)
// ? [getBuildZigTestsStep(target, options), getLinkBunStep(target, options, true)]
// : []),
],
},
imagePlatforms.has(imageKey) ? `${imageKey}-build-image` : undefined,
Expand All @@ -1102,7 +1167,7 @@ async function getPipeline(options = {}) {
);
}

if (!isMainBranch()) {
if (!isMain) {
const { skipTests, forceTests, unifiedTests, testFiles } = options;
if (!skipTests || forceTests) {
steps.push(
Expand All @@ -1111,13 +1176,18 @@ async function getPipeline(options = {}) {
.map(target => ({
key: getTargetKey(target),
group: getTargetLabel(target),
steps: [getTestBunStep(target, options, { unifiedTests, testFiles, buildId })],
steps: [
getTestBunStep(target, options, { unifiedTests, testFiles, buildId }),
// TODO: blocked by https://github.com/ziglang/zig/issues/23281
// un-comment once fixed.
// ...(targetWantsZigTest(target)
// ? [getZigTestBunStep(target, options, { unifiedTests, testFiles, buildId })]
// : []),
],
})),
);
}
}

if (isMainBranch()) {
} else {
steps.push(getReleaseStep(buildPlatforms, options));
}

Expand Down
38 changes: 36 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,37 @@ pub fn build(b: *Build) !void {
step.dependOn(addInstallObjectFile(b, bun_obj, "bun-zig", obj_format));
}

// zig build test
{
var step = b.step("test", "Build Bun's unit test suite");
var o = build_options;
var unit_tests = b.addTest(.{
.name = "bun-test",
.optimize = build_options.optimize,
.root_source_file = b.path("src/unit_test.zig"),
.test_runner = .{ .path = b.path("src/main_test.zig"), .mode = .simple },
.target = build_options.target,
.use_llvm = !build_options.no_llvm,
.use_lld = if (build_options.os == .mac) false else !build_options.no_llvm,
.omit_frame_pointer = false,
.strip = false,
});
configureObj(b, &o, unit_tests);
unit_tests.linker_allow_shlib_undefined = true;
unit_tests.link_function_sections = true;
unit_tests.link_data_sections = true;
unit_tests.bundle_ubsan_rt = false;

const bin = unit_tests.getEmittedBin();
const obj = Build.LazyPath{ .generated = .{
.file = bin.generated.file,
.up = 1,
.sub_path = "bun-test.o",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you file an issue with Zig asking for a better way to do this and link it here as a TODO?

} };
const cpy_obj = b.addInstallFile(obj, "bun-test.o");
step.dependOn(&cpy_obj.step);
}

// zig build windows-shim
{
var step = b.step("windows-shim", "Build the Windows shim (bun_shim_impl.exe + bun_shim_debug.exe)");
Expand Down Expand Up @@ -456,6 +487,11 @@ pub fn addBunObject(b: *Build, opts: *BunBuildOptions) *Compile {
.omit_frame_pointer = false,
.strip = false, // stripped at the end
});
configureObj(b, opts, obj);
return obj;
}

fn configureObj(b: *Build, opts: *BunBuildOptions, obj: *Compile) void {
if (opts.enable_asan) {
if (@hasField(Build.Module, "sanitize_address")) {
obj.root_module.sanitize_address = true;
Expand Down Expand Up @@ -494,8 +530,6 @@ pub fn addBunObject(b: *Build, opts: *BunBuildOptions) *Compile {

const translate_c = getTranslateC(b, opts.target, opts.optimize);
obj.root_module.addImport("translated-c-headers", translate_c.createModule());

return obj;
}

const ObjectFormat = enum {
Expand Down
9 changes: 9 additions & 0 deletions cmake/Options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ else()
setx(DEBUG OFF)
endif()

optionx(BUN_TEST BOOL "Build Bun's unit test suite instead of the normal build" DEFAULT OFF)

if (BUN_TEST)
setx(TEST ON)
else()
setx(TEST OFF)
endif()


if(CMAKE_BUILD_TYPE MATCHES "MinSizeRel")
setx(ENABLE_SMOL ON)
endif()
Expand Down
3 changes: 3 additions & 0 deletions cmake/scripts/DownloadZig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ else()
endif()

set(ZIG_NAME bootstrap-${ZIG_ARCH}-${ZIG_OS_ABI})
if(ZIG_COMPILER_SAFE)
set(ZIG_NAME ${ZIG_NAME}-ReleaseSafe)
endif()
set(ZIG_FILENAME ${ZIG_NAME}.zip)

if(CMAKE_HOST_WIN32)
Expand Down
20 changes: 17 additions & 3 deletions cmake/targets/BuildBun.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ else()
set(bunStrip bun)
endif()

if(TEST)
set(bun ${bun}-test)
endif()

set(bunExe ${bun}${CMAKE_EXECUTABLE_SUFFIX})

if(bunStrip)
Expand Down Expand Up @@ -528,7 +532,6 @@ file(GLOB_RECURSE BUN_ZIG_SOURCES ${CONFIGURE_DEPENDS}

list(APPEND BUN_ZIG_SOURCES
${CWD}/build.zig
${CWD}/src/main.zig
${BUN_BINDGEN_ZIG_OUTPUTS}
)

Expand All @@ -550,7 +553,11 @@ else()
list(APPEND BUN_ZIG_GENERATED_SOURCES ${BUN_BAKE_RUNTIME_OUTPUTS})
endif()

set(BUN_ZIG_OUTPUT ${BUILD_PATH}/bun-zig.o)
if (TEST)
set(BUN_ZIG_OUTPUT ${BUILD_PATH}/bun-test.o)
else()
set(BUN_ZIG_OUTPUT ${BUILD_PATH}/bun-zig.o)
endif()

if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm|ARM|arm64|ARM64|aarch64|AARCH64")
if(APPLE)
Expand All @@ -573,6 +580,12 @@ if(NOT "${REVISION}" STREQUAL "")
set(ZIG_FLAGS_BUN ${ZIG_FLAGS_BUN} -Dsha=${REVISION})
endif()

if (TEST)
set(ZIG_STEPS test)
else()
set(ZIG_STEPS obj)
endif()

register_command(
TARGET
bun-zig
Expand All @@ -582,7 +595,7 @@ register_command(
"Building src/*.zig for ${ZIG_TARGET}"
COMMAND
${ZIG_EXECUTABLE}
build obj
build ${ZIG_STEPS}
${CMAKE_ZIG_FLAGS}
--prefix ${BUILD_PATH}
-Dobj_format=${ZIG_OBJECT_FORMAT}
Expand All @@ -596,6 +609,7 @@ register_command(
-Dcodegen_path=${CODEGEN_PATH}
-Dcodegen_embed=$<IF:$<BOOL:${CODEGEN_EMBED}>,true,false>
--prominent-compile-errors
--summary all
${ZIG_FLAGS_BUN}
ARTIFACTS
${BUN_ZIG_OUTPUT}
Expand Down
2 changes: 2 additions & 0 deletions cmake/tools/SetupZig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ optionx(ZIG_OBJECT_FORMAT "obj|bc" "Output file format for Zig object files" DEF

optionx(ZIG_LOCAL_CACHE_DIR FILEPATH "The path to local the zig cache directory" DEFAULT ${CACHE_PATH}/zig/local)
optionx(ZIG_GLOBAL_CACHE_DIR FILEPATH "The path to the global zig cache directory" DEFAULT ${CACHE_PATH}/zig/global)
optionx(ZIG_COMPILER_SAFE BOOL "Download a ReleaseSafe build of the Zig compiler. Only availble on macos aarch64." DEFAULT OFF)

setenv(ZIG_LOCAL_CACHE_DIR ${ZIG_LOCAL_CACHE_DIR})
setenv(ZIG_GLOBAL_CACHE_DIR ${ZIG_GLOBAL_CACHE_DIR})
Expand Down Expand Up @@ -78,6 +79,7 @@ register_command(
-DZIG_PATH=${ZIG_PATH}
-DZIG_COMMIT=${ZIG_COMMIT}
-DENABLE_ASAN=${ENABLE_ASAN}
-DZIG_COMPILER_SAFE=${ZIG_COMPILER_SAFE}
-P ${CWD}/cmake/scripts/DownloadZig.cmake
SOURCES
${CWD}/cmake/scripts/DownloadZig.cmake
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
},
"scripts": {
"build": "bun run build:debug",
"build:test": "bun run zig:test",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete, since it doesn't just build, it also runs the tests

"build:debug": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Debug -B build/debug",
"build:valgrind": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Debug -DENABLE_BASELINE=ON -ENABLE_VALGRIND=ON -B build/debug-valgrind",
"build:release": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Release -B build/release",
"build:ci": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=ON -DCI=true -B build/release-ci --verbose --fresh",
"build:ci:test": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Release -DBUN_TEST=ON -DZIG_OPTIMIZE=ReleaseSafe -DCMAKE_VERBOSE_MAKEFILE=ON -DCI=true -B build/release-ci --verbose --fresh",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename zig:test:ci in line with the other zig:test scripts and the deletion of build:test

"build:assert": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DENABLE_ASSERTIONS=ON -DENABLE_LOGS=ON -B build/release-assert",
"build:logs": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Release -DENABLE_LOGS=ON -B build/release-logs",
"build:safe": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Release -DZIG_OPTIMIZE=ReleaseSafe -B build/release-safe",
Expand All @@ -55,6 +57,8 @@
"test:release": "node scripts/runner.node.mjs --exec-path ./build/release/bun",
"banned": "bun test test/internal/ban-words.test.ts",
"zig": "vendor/zig/zig.exe",
"zig:test": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Debug -DBUN_TEST=ON -B build/debug",
"zig:test:release": "bun ./scripts/build.mjs -GNinja -DCMAKE_BUILD_TYPE=Release -DBUNTEST=ON -B build/release",
"zig:fmt": "bun run zig-format",
"zig:check": "bun run zig build check --summary new",
"zig:check-all": "bun run zig build check-all --summary new",
Expand All @@ -74,6 +78,7 @@
"prettier:check": "bun run analysis:no-llvm --target prettier-check",
"prettier:extra": "bun run analysis:no-llvm --target prettier-extra",
"prettier:diff": "bun run analysis:no-llvm --target prettier-diff",
"node:test": "node ./scripts/runner.node.mjs --quiet --exec-path=$npm_execpath --node-tests "
"node:test": "node ./scripts/runner.node.mjs --quiet --exec-path=$npm_execpath --node-tests ",
"clean:zig": "rm -rf build/debug/cache/zig build/debug/CMakeCache.txt 'build/debug/*.o' .zig-cache zig-out || true"
}
}
4 changes: 0 additions & 4 deletions src/bun.js/base.zig
Original file line number Diff line number Diff line change
Expand Up @@ -779,10 +779,6 @@ pub const RefString = struct {
}
};

comptime {
std.testing.refAllDecls(RefString);
}

pub export fn MarkedArrayBuffer_deallocator(bytes_: *anyopaque, _: *anyopaque) void {
const mimalloc = @import("../allocators/mimalloc.zig");
// zig's memory allocator interface won't work here
Expand Down
4 changes: 0 additions & 4 deletions src/bun.js/node/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2180,10 +2180,6 @@ pub const PathOrBlob = union(enum) {
}
};

comptime {
std.testing.refAllDecls(Process);
}

/// StatFS and BigIntStatFS classes from node:fs
pub fn StatFSType(comptime big: bool) type {
const Int = if (big) i64 else i32;
Expand Down
Loading