aboutsummaryrefslogtreecommitdiff
path: root/build.zig
diff options
context:
space:
mode:
Diffstat (limited to 'build.zig')
-rw-r--r--build.zig114
1 files changed, 56 insertions, 58 deletions
diff --git a/build.zig b/build.zig
index 3049a092b..195570c1c 100644
--- a/build.zig
+++ b/build.zig
@@ -1,18 +1,21 @@
const std = @import("std");
-const resolve_path = @import("./src/resolver/resolve_path.zig");
fn pkgPath(comptime out: []const u8) std.build.FileSource {
- const outpath = comptime std.fs.path.dirname(@src().file).? ++ std.fs.path.sep_str ++ out;
- return .{ .path = outpath };
+ if (comptime std.fs.path.dirname(@src().file)) |base| {
+ const outpath = comptime base ++ std.fs.path.sep_str ++ out;
+ return .{ .path = outpath };
+ } else {
+ return .{ .path = out };
+ }
}
pub fn addPicoHTTP(step: *std.build.LibExeObjStep, comptime with_obj: bool) void {
- step.addIncludeDir("src/deps");
+ step.addIncludePath("src/deps");
if (with_obj) {
step.addObjectFile("src/deps/picohttpparser.o");
}
- step.addIncludeDir("src/deps");
+ step.addIncludePath("src/deps");
if (with_obj) {
step.addObjectFile(panicIfNotFound("src/deps/picohttpparser.o"));
@@ -80,10 +83,8 @@ fn addInternalPackages(step: *std.build.LibExeObjStep, _: std.mem.Allocator, tar
javascript_core_stub
else
javascript_core_real;
- io.dependencies = &[_]std.build.Pkg{bun};
- javascript_core.dependencies = &[_]std.build.Pkg{bun};
+ javascript_core.dependencies = &[_]std.build.Pkg{};
step.addPackage(io);
- step.addPackage(javascript_core);
step.addPackage(bun);
}
@@ -93,6 +94,7 @@ const BunBuildOptions = struct {
baseline: bool = false,
bindgen: bool = false,
sizegen: bool = false,
+ base_path: [:0]const u8 = "",
pub fn step(this: BunBuildOptions, b: anytype) *std.build.OptionsStep {
var opts = b.addOptions();
@@ -101,6 +103,7 @@ const BunBuildOptions = struct {
opts.addOption(@TypeOf(this.baseline), "baseline", this.baseline);
opts.addOption(@TypeOf(this.bindgen), "bindgen", this.bindgen);
opts.addOption(@TypeOf(this.sizegen), "sizegen", this.sizegen);
+ opts.addOption(@TypeOf(this.base_path), "base_path", this.base_path);
return opts;
}
};
@@ -115,6 +118,18 @@ fn panicIfNotFound(comptime filepath: []const u8) []const u8 {
return filepath;
}
+const fmt = struct {
+ pub usingnamespace @import("std").fmt;
+
+ pub fn hexInt(value: anytype) @TypeOf(std.fmt.fmtSliceHexLower("")) {
+ return std.fmt.fmtSliceHexLower(std.mem.asBytes(&value));
+ }
+
+ pub fn hexIntUp(value: anytype) @TypeOf(std.fmt.fmtSliceHexUpper("")) {
+ return std.fmt.fmtSliceHexUpper(std.mem.asBytes(&value));
+ }
+};
+
fn updateRuntime() anyerror!void {
var runtime_out_file = try std.fs.cwd().openFile("src/runtime.out.js", .{ .mode = .read_only });
const runtime_hash = std.hash.Wyhash.hash(
@@ -123,7 +138,7 @@ fn updateRuntime() anyerror!void {
);
const runtime_version_file = std.fs.cwd().createFile("src/runtime.version", .{ .truncate = true }) catch std.debug.panic("Failed to create src/runtime.version", .{});
defer runtime_version_file.close();
- runtime_version_file.writer().print("{x}", .{runtime_hash}) catch unreachable;
+ runtime_version_file.writer().print("{any}", .{fmt.hexInt(runtime_hash)}) catch unreachable;
var fallback_out_file = try std.fs.cwd().openFile("src/fallback.out.js", .{ .mode = .read_only });
const fallback_hash = std.hash.Wyhash.hash(
0,
@@ -132,7 +147,7 @@ fn updateRuntime() anyerror!void {
const fallback_version_file = std.fs.cwd().createFile("src/fallback.version", .{ .truncate = true }) catch std.debug.panic("Failed to create src/fallback.version", .{});
- fallback_version_file.writer().print("{x}", .{fallback_hash}) catch unreachable;
+ fallback_version_file.writer().print("{any}", .{fmt.hexInt(fallback_hash)}) catch unreachable;
fallback_version_file.close();
}
@@ -155,7 +170,6 @@ pub fn build(b: *std.build.Builder) !void {
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
mode = b.standardReleaseOptions();
- var exe: *std.build.LibExeObjStep = undefined;
var output_dir_buf = std.mem.zeroes([4096]u8);
var bin_label = if (mode == std.builtin.Mode.Debug) "packages/debug-bun-" else "packages/bun-";
@@ -192,8 +206,8 @@ pub fn build(b: *std.build.Builder) !void {
var triplet = triplet_buf[0 .. osname.len + cpuArchName.len + 1];
- if (std.os.getenv("OUTPUT_DIR")) |output_dir_| {
- output_dir = output_dir_;
+ if (b.option([]const u8, "output-dir", "target to install to") orelse std.os.getenv("OUTPUT_DIR")) |output_dir_| {
+ output_dir = b.pathFromRoot(output_dir_);
} else {
const output_dir_base = try std.fmt.bufPrint(&output_dir_buf, "{s}{s}", .{ bin_label, triplet });
output_dir = b.pathFromRoot(output_dir_base);
@@ -201,21 +215,13 @@ pub fn build(b: *std.build.Builder) !void {
std.fs.cwd().makePath(output_dir) catch {};
const bun_executable_name = if (mode == std.builtin.Mode.Debug) "bun-debug" else "bun";
- exe = b.addExecutable(bun_executable_name, if (target.getOsTag() == std.Target.Os.Tag.freestanding)
+ const root_src = if (target.getOsTag() == std.Target.Os.Tag.freestanding)
"src/main_wasm.zig"
else
- "src/main.zig");
- // exe.setLibCFile("libc.txt");
- exe.linkLibC();
- // exe.linkLibCpp();
+ "root.zig";
- exe.setOutputDir(output_dir);
updateRuntime() catch {};
- exe.setTarget(target);
- exe.setBuildMode(mode);
- b.install_path = output_dir;
-
const min_version: std.builtin.Version = if (target.getOsTag() != .freestanding)
target.getOsVersionMin().semver
else .{ .major = 0, .minor = 0, .patch = 0 };
@@ -224,20 +230,9 @@ pub fn build(b: *std.build.Builder) !void {
target.getOsVersionMax().semver
else .{ .major = 0, .minor = 0, .patch = 0 };
- // exe.want_lto = true;
- defer b.default_step.dependOn(&b.addLog("Output: {s}/{s}\n", .{ output_dir, bun_executable_name }).step);
- defer b.default_step.dependOn(&b.addLog(
- "Build {s} v{} - v{}\n",
- .{
- triplet,
- min_version,
- max_version,
- },
- ).step);
-
var obj_step = b.step("obj", "Build bun as a .o file");
- var obj = b.addObject(bun_executable_name, exe.root_src.?.path);
-
+ obj_step.dependOn(&b.addLog("Output: {s}/{s}\n", .{ output_dir, bun_executable_name }).step);
+ var obj = b.addObject(bun_executable_name, root_src);
var default_build_options: BunBuildOptions = brk: {
const is_baseline = arch.isX86() and (target.cpu_model == .baseline or
!std.Target.x86.featureSetHas(target.getCpuFeatures(), .avx2));
@@ -272,6 +267,7 @@ pub fn build(b: *std.build.Builder) !void {
.sha = git_sha,
.baseline = is_baseline,
.bindgen = false,
+ .base_path = try b.allocator.dupeZ(u8, b.pathFromRoot(".")),
};
};
@@ -308,9 +304,8 @@ pub fn build(b: *std.build.Builder) !void {
).step);
}
- obj_step.dependOn(&obj.step);
+ defer obj_step.dependOn(&obj.step);
- obj.setOutputDir(output_dir);
obj.setBuildMode(mode);
var actual_build_options = default_build_options;
@@ -327,8 +322,6 @@ pub fn build(b: *std.build.Builder) !void {
obj.bundle_compiler_rt = true;
obj.omit_frame_pointer = mode != .Debug;
- b.default_step.dependOn(&obj.step);
-
if (target.getOsTag() == .linux) {
// obj.want_lto = tar;
obj.link_emit_relocs = true;
@@ -344,7 +337,7 @@ pub fn build(b: *std.build.Builder) !void {
const headers_step = b.step("headers-obj", "Build JavaScriptCore headers");
var headers_obj: *std.build.LibExeObjStep = b.addObject("headers", "src/bindgen.zig");
defer headers_step.dependOn(&headers_obj.step);
- try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
+ try configureObjectStep(b, headers_obj, @TypeOf(target), target, obj.main_pkg_path.?);
var headers_build_options = default_build_options;
headers_build_options.bindgen = true;
headers_obj.addOptions("build_options", default_build_options.step(b));
@@ -359,14 +352,14 @@ pub fn build(b: *std.build.Builder) !void {
// wasm_step.link_function_sections = true;
// wasm_step.link_emit_relocs = true;
// wasm_step.single_threaded = true;
- try configureObjectStep(b, wasm_step, target, obj.main_pkg_path.?);
+ try configureObjectStep(b, wasm_step, @TypeOf(target), target, obj.main_pkg_path.?);
}
{
const headers_step = b.step("httpbench-obj", "Build HTTPBench tool (object files)");
var headers_obj: *std.build.LibExeObjStep = b.addObject("httpbench", "misctools/http_bench.zig");
defer headers_step.dependOn(&headers_obj.step);
- try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
+ try configureObjectStep(b, headers_obj, @TypeOf(target), target, obj.main_pkg_path.?);
headers_obj.addOptions("build_options", default_build_options.step(b));
}
@@ -374,7 +367,7 @@ pub fn build(b: *std.build.Builder) !void {
const headers_step = b.step("machbench-obj", "Build Machbench tool (object files)");
var headers_obj: *std.build.LibExeObjStep = b.addObject("machbench", "misctools/machbench.zig");
defer headers_step.dependOn(&headers_obj.step);
- try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
+ try configureObjectStep(b, headers_obj, @TypeOf(target), target, obj.main_pkg_path.?);
headers_obj.addOptions("build_options", default_build_options.step(b));
}
@@ -382,7 +375,7 @@ pub fn build(b: *std.build.Builder) !void {
const headers_step = b.step("fetch-obj", "Build fetch (object files)");
var headers_obj: *std.build.LibExeObjStep = b.addObject("fetch", "misctools/fetch.zig");
defer headers_step.dependOn(&headers_obj.step);
- try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
+ try configureObjectStep(b, headers_obj, @TypeOf(target), target, obj.main_pkg_path.?);
headers_obj.addOptions("build_options", default_build_options.step(b));
}
@@ -390,7 +383,7 @@ pub fn build(b: *std.build.Builder) !void {
const headers_step = b.step("string-bench", "Build string bench");
var headers_obj: *std.build.LibExeObjStep = b.addExecutable("string-bench", "src/bench/string-handling.zig");
defer headers_step.dependOn(&headers_obj.step);
- try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
+ try configureObjectStep(b, headers_obj, @TypeOf(target), target, obj.main_pkg_path.?);
headers_obj.addOptions("build_options", default_build_options.step(b));
}
@@ -398,7 +391,7 @@ pub fn build(b: *std.build.Builder) !void {
const headers_step = b.step("sha-bench-obj", "Build sha bench");
var headers_obj: *std.build.LibExeObjStep = b.addObject("sha", "src/sha.zig");
defer headers_step.dependOn(&headers_obj.step);
- try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
+ try configureObjectStep(b, headers_obj, @TypeOf(target), target, obj.main_pkg_path.?);
headers_obj.addOptions("build_options", default_build_options.step(b));
}
@@ -406,7 +399,7 @@ pub fn build(b: *std.build.Builder) !void {
const headers_step = b.step("vlq-bench", "Build vlq bench");
var headers_obj: *std.build.LibExeObjStep = b.addExecutable("vlq-bench", "src/sourcemap/vlq_bench.zig");
defer headers_step.dependOn(&headers_obj.step);
- try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
+ try configureObjectStep(b, headers_obj, @TypeOf(target), target, obj.main_pkg_path.?);
headers_obj.addOptions("build_options", default_build_options.step(b));
}
@@ -414,7 +407,7 @@ pub fn build(b: *std.build.Builder) !void {
const headers_step = b.step("tgz-obj", "Build tgz (object files)");
var headers_obj: *std.build.LibExeObjStep = b.addObject("tgz", "misctools/tgz.zig");
defer headers_step.dependOn(&headers_obj.step);
- try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
+ try configureObjectStep(b, headers_obj, @TypeOf(target), target, obj.main_pkg_path.?);
headers_obj.addOptions("build_options", default_build_options.step(b));
}
@@ -432,7 +425,7 @@ pub fn build(b: *std.build.Builder) !void {
if (std.fs.path.dirname(test_bin)) |dir| headers_obj.setOutputDir(dir);
}
- try configureObjectStep(b, headers_obj, target, obj.main_pkg_path.?);
+ try configureObjectStep(b, headers_obj, @TypeOf(target), target, obj.main_pkg_path.?);
try linkObjectFiles(b, headers_obj, target);
{
@@ -451,7 +444,7 @@ pub fn build(b: *std.build.Builder) !void {
test_.setMainPkgPath(obj.main_pkg_path.?);
test_.setTarget(target);
- try configureObjectStep(b, test_, target, obj.main_pkg_path.?);
+ try configureObjectStep(b, test_, @TypeOf(target), target, obj.main_pkg_path.?);
try linkObjectFiles(b, test_, target);
test_.addOptions("build_options", default_build_options.step(b));
@@ -467,9 +460,11 @@ pub fn build(b: *std.build.Builder) !void {
headers_step.dependOn(&after.step);
}
}
+ obj.setOutputDir(output_dir);
+ b.default_step.dependOn(obj_step);
}
-pub var original_make_fn: ?fn (step: *std.build.Step) anyerror!void = null;
+pub var original_make_fn: ?*const fn (step: *std.build.Step) anyerror!void = null;
// Due to limitations in std.build.Builder
// we cannot use this with debugging
@@ -514,13 +509,13 @@ pub fn linkObjectFiles(b: *std.build.Builder, obj: *std.build.LibExeObjStep, tar
});
for (dirs_to_search.slice()) |deps_path| {
- var deps_dir = std.fs.cwd().openDir(deps_path, .{ .iterate = true }) catch continue;
+ var deps_dir = std.fs.cwd().openIterableDir(deps_path, .{}) catch continue;
var iterator = deps_dir.iterate();
- obj.addIncludeDir(deps_path);
- obj.addLibPath(deps_path);
+ obj.addIncludePath(deps_path);
+ obj.addLibraryPath(deps_path);
while (iterator.next() catch null) |entr| {
- const entry: std.fs.Dir.Entry = entr;
+ const entry: std.fs.IterableDir.Entry = entr;
if (files_we_care_about.get(entry.name)) |obj_name| {
var has_added = try added.getOrPut(std.hash.Wyhash.hash(0, obj_name));
if (!has_added.found_existing) {
@@ -532,7 +527,7 @@ pub fn linkObjectFiles(b: *std.build.Builder, obj: *std.build.LibExeObjStep, tar
}
}
-pub fn configureObjectStep(_: *std.build.Builder, obj: *std.build.LibExeObjStep, target: anytype, main_pkg_path: []const u8) !void {
+pub fn configureObjectStep(_: *std.build.Builder, obj: *std.build.LibExeObjStep, comptime Target: type, target: Target, main_pkg_path: []const u8) !void {
obj.setMainPkgPath(main_pkg_path);
obj.setTarget(target);
@@ -541,10 +536,13 @@ pub fn configureObjectStep(_: *std.build.Builder, obj: *std.build.LibExeObjStep,
addPicoHTTP(obj, false);
obj.strip = false;
+
obj.setOutputDir(output_dir);
obj.setBuildMode(mode);
+ obj.bundle_compiler_rt = true;
+
if (target.getOsTag() != .freestanding) obj.linkLibC();
- if (target.getOsTag() != .freestanding) obj.bundle_compiler_rt = true;
+ if (target.getOsTag() != .freestanding) obj.bundle_compiler_rt = false;
if (target.getOsTag() == .linux) {
// obj.want_lto = tar;