Add obj build step

Former-commit-id: cd9c4f4dccfce2a5782d285e3ade78c22f1b2a9b
This commit is contained in:
Jarred Sumner
2021-08-20 16:42:34 -07:00
parent 5db15b6ec7
commit 9f43f92cfa

147
build.zig
View File

@@ -1,14 +1,18 @@
const std = @import("std");
const resolve_path = @import("./src/resolver/resolve_path.zig");
pub fn addPicoHTTP(step: *std.build.LibExeObjStep) void {
pub fn addPicoHTTP(step: *std.build.LibExeObjStep, comptime with_obj: bool) void {
const picohttp = step.addPackage(.{
.name = "picohttp",
.path = .{ .path = "src/deps/picohttp.zig" },
});
step.addObjectFile("src/deps/picohttpparser.o");
step.addIncludeDir("src/deps");
if (with_obj) {
step.addObjectFile("src/deps/picohttpparser.o");
}
// step.add("/Users/jarred/Code/WebKit/WebKitBuild/Release/lib/libWTF.a");
// ./Tools/Scripts/build-jsc --jsc-only --cmakeargs="-DENABLE_STATIC_JSC=ON"
@@ -16,64 +20,6 @@ pub fn addPicoHTTP(step: *std.build.LibExeObjStep) void {
// homebrew-provided icu4c
}
pub var original_make_fn: ?fn (step: *std.build.Step) anyerror!void = null;
pub var headers_zig_file: ?[]const u8 = null;
const HeadersMaker = struct {
pub fn make(self: *std.build.Step) anyerror!void {
try original_make_fn.?(self);
var headers_zig: std.fs.File = try std.fs.openFileAbsolute(headers_zig_file.?, .{ .write = true });
var contents = try headers_zig.readToEndAlloc(std.heap.page_allocator, headers_zig.getEndPos() catch unreachable);
const last_extern_i = std.mem.lastIndexOf(u8, contents, "pub extern fn") orelse @panic("Expected contents");
const last_newline = std.mem.indexOf(u8, contents[last_extern_i..], "\n") orelse @panic("Expected newline");
const to_splice = "usingnamespace @import(\"./headers-replacements.zig\");\n";
var new_contents = try std.heap.page_allocator.alloc(u8, contents.len + to_splice.len);
std.mem.copy(u8, new_contents, to_splice);
std.mem.copy(u8, new_contents[to_splice.len..], contents);
var i: usize = to_splice.len;
var remainder = new_contents[i..];
while (remainder.len > 0) {
i = std.mem.indexOf(u8, remainder, "\npub const struct_b") orelse break + "\npub const struct_b".len;
var begin = remainder[i..];
const end_line = std.mem.indexOf(u8, begin, "extern struct {") orelse break;
const end_struct = std.mem.indexOf(u8, begin, "\n};\n") orelse break + "\n};\n".len;
std.mem.set(u8, begin[1 .. end_struct + 3], ' ');
remainder = begin[end_struct..];
}
i = to_splice.len;
remainder = new_contents[i..];
while (remainder.len > 0) {
i = std.mem.indexOf(u8, remainder, "\npub const struct_") orelse break + "\npub const struct_".len;
var begin = remainder[i..];
var end_struct = std.mem.indexOf(u8, begin, "opaque {};") orelse break;
end_struct += std.mem.indexOf(u8, begin[end_struct..], "\n") orelse break;
i = 0;
std.mem.set(u8, begin[1..end_struct], ' ');
remainder = begin[end_struct..];
}
const HARDCODE = [_][]const u8{
"[*c][*c]JSC__Exception",
"*?*JSC__Exception ",
"[*c]?*c_void",
"[*c]*c_void",
};
i = 0;
while (i < HARDCODE.len) : (i += 2) {
_ = std.mem.replace(u8, new_contents, HARDCODE[i], HARDCODE[i + 1], new_contents);
}
const js_value_start = std.mem.indexOf(u8, new_contents, "pub const JSC__JSValue") orelse unreachable;
const js_value_end = std.mem.indexOf(u8, new_contents[js_value_start..], "\n") orelse unreachable;
std.mem.set(u8, new_contents[js_value_start..][0..js_value_end], ' ');
try headers_zig.seekTo(0);
try headers_zig.writeAll(new_contents);
try headers_zig.setEndPos(last_newline + last_extern_i + to_splice.len);
}
};
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
@@ -241,19 +187,19 @@ pub fn build(b: *std.build.Builder) void {
original_make_fn = headers_step.makeFn;
headers_step.makeFn = HeadersMaker.make;
b.default_step.dependOn(&exe.step);
var steps = [_]*std.build.LibExeObjStep{ exe, javascript, typings_exe, headers_exec };
for (steps) |step| {
for (steps) |step, i| {
step.linkLibC();
step.linkLibCpp();
addPicoHTTP(
step,
true,
);
step.addObjectFile("src/JavaScript/jsc/WebKit/WebKitBuild/Release/lib/libJavaScriptCore.a");
step.addObjectFile("src/JavaScript/jsc/WebKit/WebKitBuild/Release/lib/libWTF.a");
step.addObjectFile("src/JavaScript/jsc/WebKit/WebKitBuild/Release/lib/libbmalloc.a");
step.addObjectFile("src/deps/libJavaScriptCore.a");
step.addObjectFile("src/deps/libWTF.a");
step.addObjectFile("src/deps/libbmalloc.a");
// We must link ICU statically
step.addObjectFile("/usr/local/opt/icu4c/lib/libicudata.a");
@@ -273,6 +219,19 @@ pub fn build(b: *std.build.Builder) void {
);
}
}
var obj_step = b.step("obj", "Build Bun as a .o file");
var obj = b.addObject("bun", exe.root_src.?.path);
obj.bundle_compiler_rt = true;
addPicoHTTP(obj, false);
obj.addPackage(.{
.name = "clap",
.path = .{ .path = "src/deps/zig-clap/clap.zig" },
});
obj_step.dependOn(&obj.step);
obj.setOutputDir(output_dir);
obj.setBuildMode(mode);
} else {
b.default_step.dependOn(&exe.step);
}
@@ -311,3 +270,61 @@ pub fn build(b: *std.build.Builder) void {
var javascript_cmd = b.step("spjs", "Build standalone JavaScript runtime. Must run \"make jsc\" first.");
javascript_cmd.dependOn(&javascript.step);
}
pub var original_make_fn: ?fn (step: *std.build.Step) anyerror!void = null;
pub var headers_zig_file: ?[]const u8 = null;
const HeadersMaker = struct {
pub fn make(self: *std.build.Step) anyerror!void {
try original_make_fn.?(self);
var headers_zig: std.fs.File = try std.fs.openFileAbsolute(headers_zig_file.?, .{ .write = true });
var contents = try headers_zig.readToEndAlloc(std.heap.page_allocator, headers_zig.getEndPos() catch unreachable);
const last_extern_i = std.mem.lastIndexOf(u8, contents, "pub extern fn") orelse @panic("Expected contents");
const last_newline = std.mem.indexOf(u8, contents[last_extern_i..], "\n") orelse @panic("Expected newline");
const to_splice = "usingnamespace @import(\"./headers-replacements.zig\");\n";
var new_contents = try std.heap.page_allocator.alloc(u8, contents.len + to_splice.len);
std.mem.copy(u8, new_contents, to_splice);
std.mem.copy(u8, new_contents[to_splice.len..], contents);
var i: usize = to_splice.len;
var remainder = new_contents[i..];
while (remainder.len > 0) {
i = std.mem.indexOf(u8, remainder, "\npub const struct_b") orelse break + "\npub const struct_b".len;
var begin = remainder[i..];
const end_line = std.mem.indexOf(u8, begin, "extern struct {") orelse break;
const end_struct = std.mem.indexOf(u8, begin, "\n};\n") orelse break + "\n};\n".len;
std.mem.set(u8, begin[1 .. end_struct + 3], ' ');
remainder = begin[end_struct..];
}
i = to_splice.len;
remainder = new_contents[i..];
while (remainder.len > 0) {
i = std.mem.indexOf(u8, remainder, "\npub const struct_") orelse break + "\npub const struct_".len;
var begin = remainder[i..];
var end_struct = std.mem.indexOf(u8, begin, "opaque {};") orelse break;
end_struct += std.mem.indexOf(u8, begin[end_struct..], "\n") orelse break;
i = 0;
std.mem.set(u8, begin[1..end_struct], ' ');
remainder = begin[end_struct..];
}
const HARDCODE = [_][]const u8{
"[*c][*c]JSC__Exception",
"*?*JSC__Exception ",
"[*c]?*c_void",
"[*c]*c_void",
};
i = 0;
while (i < HARDCODE.len) : (i += 2) {
_ = std.mem.replace(u8, new_contents, HARDCODE[i], HARDCODE[i + 1], new_contents);
}
const js_value_start = std.mem.indexOf(u8, new_contents, "pub const JSC__JSValue") orelse unreachable;
const js_value_end = std.mem.indexOf(u8, new_contents[js_value_start..], "\n") orelse unreachable;
std.mem.set(u8, new_contents[js_value_start..][0..js_value_end], ' ');
try headers_zig.seekTo(0);
try headers_zig.writeAll(new_contents);
try headers_zig.setEndPos(last_newline + last_extern_i + to_splice.len);
}
};