mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
[internal] Make make jsc-bindings-headers more reliable
This commit is contained in:
40
Makefile
40
Makefile
@@ -31,6 +31,7 @@ endif
|
||||
|
||||
AR=
|
||||
|
||||
BUN_OR_NODE = $(shell which bun || which node)
|
||||
|
||||
CXX_VERSION=c++2a
|
||||
TRIPLET = $(OS_NAME)-$(ARCH_NAME)
|
||||
@@ -156,25 +157,28 @@ ENABLE_MIMALLOC ?= 1
|
||||
_MIMALLOC_FILE = libmimalloc.o
|
||||
_MIMALLOC_INPUT_PATH = CMakeFiles/mimalloc-obj.dir/src/static.c.o
|
||||
_MIMALLOC_DEBUG_FILE = libmimalloc-debug.a
|
||||
_MIMALLOC_OBJECT_FILE = 1
|
||||
_MIMALLOC_LINK = $(BUN_DEPS_OUT_DIR)/$(MIMALLOC_FILE)
|
||||
DEFAULT_LINKER_FLAGS =
|
||||
|
||||
JSC_BUILD_STEPS :=
|
||||
ifeq ($(OS_NAME),linux)
|
||||
JSC_BUILD_STEPS += jsc-build-linux
|
||||
_MIMALLOC_LINK = $(BUN_DEPS_OUT_DIR)/$(MIMALLOC_FILE)
|
||||
DEFAULT_LINKER_FLAGS= -pthread -ldl
|
||||
endif
|
||||
ifeq ($(OS_NAME),darwin)
|
||||
_MIMALLOC_OBJECT_FILE = 0
|
||||
JSC_BUILD_STEPS += jsc-build-mac jsc-copy-headers
|
||||
_MIMALLOC_FILE = libmimalloc.a
|
||||
_MIMALLOC_INPUT_PATH = libmimalloc.a
|
||||
_MIMALLOC_LINK = -lmimalloc
|
||||
endif
|
||||
|
||||
MIMALLOC_FILE=
|
||||
MIMALLOC_INPUT_PATH=
|
||||
MIMALLOC_FILE_PATH=
|
||||
ifeq ($(ENABLE_MIMALLOC), 1)
|
||||
MIMALLOC_FILE=$(_MIMALLOC_FILE)
|
||||
MIMALLOC_FILE_PATH=$(BUN_DEPS_OUT_DIR)/$(MIMALLOC_FILE)
|
||||
MIMALLOC_INPUT_PATH=$(_MIMALLOC_INPUT_PATH)
|
||||
endif
|
||||
|
||||
@@ -325,7 +329,7 @@ ifeq ($(OS_NAME), darwin)
|
||||
SHARED_LIB_EXTENSION = .dylib
|
||||
endif
|
||||
|
||||
ARCHIVE_FILES_WITHOUT_LIBCRYPTO = $(MIMALLOC_FILE_PATH) \
|
||||
ARCHIVE_FILES_WITHOUT_LIBCRYPTO = \
|
||||
$(BUN_DEPS_OUT_DIR)/picohttpparser.o \
|
||||
-L$(BUN_DEPS_OUT_DIR) \
|
||||
-llolhtml \
|
||||
@@ -333,7 +337,8 @@ ARCHIVE_FILES_WITHOUT_LIBCRYPTO = $(MIMALLOC_FILE_PATH) \
|
||||
-larchive \
|
||||
-lssl \
|
||||
-lbase64 \
|
||||
-ltcc
|
||||
-ltcc \
|
||||
$(_MIMALLOC_LINK)
|
||||
|
||||
ARCHIVE_FILES = $(ARCHIVE_FILES_WITHOUT_LIBCRYPTO) -lcrypto
|
||||
|
||||
@@ -377,6 +382,7 @@ BUN_LLD_FLAGS_WITHOUT_JSC = $(ARCHIVE_FILES) \
|
||||
|
||||
|
||||
BUN_LLD_FLAGS = $(BUN_LLD_FLAGS_WITHOUT_JSC) $(JSC_FILES) $(BINDINGS_OBJ)
|
||||
BUN_LLD_FLAGS_FAST = $(BUN_LLD_FLAGS_WITHOUT_JSC) $(JSC_FILES_DEBUG) $(BINDINGS_OBJ)
|
||||
|
||||
BUN_LLD_FLAGS_DEBUG = $(BUN_LLD_FLAGS_WITHOUT_JSC) $(JSC_FILES_DEBUG) $(BINDINGS_OBJ)
|
||||
|
||||
@@ -715,18 +721,7 @@ jsc-bindings-headers:
|
||||
$(CXX) $(PLATFORM_LINKER_FLAGS) $(JSC_FILES_DEBUG) ${ICU_FLAGS} $(BUN_LLD_FLAGS_WITHOUT_JSC) -g $(DEBUG_BIN)/headers.o -W -o /tmp/build-jsc-headers -lc;
|
||||
/tmp/build-jsc-headers
|
||||
$(ZIG) translate-c src/bun.js/bindings/headers.h > src/bun.js/bindings/headers.zig
|
||||
$(ZIG) run misctools/headers-cleaner.zig -lc
|
||||
$(SED) -i '/pub const __darwin/d' src/bun.js/bindings/headers.zig || echo "";
|
||||
$(SED) -i '/pub const __builtin/d' src/bun.js/bindings/headers.zig || echo "";
|
||||
$(SED) -i '/pub const int/d' src/bun.js/bindings/headers.zig || echo "";
|
||||
$(SED) -i '/pub const uint/d' src/bun.js/bindings/headers.zig || echo "";
|
||||
$(SED) -i '/pub const intmax/d' src/bun.js/bindings/headers.zig || echo "";
|
||||
$(SED) -i '/pub const uintmax/d' src/bun.js/bindings/headers.zig || echo "";
|
||||
$(SED) -i '/pub const max_align_t/{N;N;N;d;}' src/bun.js/bindings/headers.zig
|
||||
$(SED) -i '/pub const ZigErrorCode/d' src/bun.js/bindings/headers.zig
|
||||
$(SED) -i '/pub const JSClassRef/d' src/bun.js/bindings/headers.zig
|
||||
cat src/bun.js/bindings/headers.zig > /tmp/headers.zig
|
||||
cat src/bun.js/bindings/headers-replacements.zig /tmp/headers.zig > src/bun.js/bindings/headers.zig
|
||||
$(BUN_OR_NODE) misctools/headers-cleaner.js
|
||||
$(ZIG) fmt src/bun.js/bindings/headers.zig
|
||||
|
||||
|
||||
@@ -1186,6 +1181,16 @@ bun-link-lld-release:
|
||||
rm -rf $(BUN_RELEASE_BIN).dSYM
|
||||
cp $(BUN_RELEASE_BIN) $(BUN_RELEASE_BIN)-profile
|
||||
|
||||
bun-link-lld-release-no-lto:
|
||||
$(CXX) $(BUN_LLD_FLAGS_FAST) $(SYMBOLS) \
|
||||
$(BUN_RELEASE_BIN).o \
|
||||
-o $(BUN_RELEASE_BIN) \
|
||||
-W \
|
||||
$(OPTIMIZATION_LEVEL) $(RELEASE_FLAGS)
|
||||
rm -rf $(BUN_RELEASE_BIN).dSYM
|
||||
cp $(BUN_RELEASE_BIN) $(BUN_RELEASE_BIN)-profile
|
||||
|
||||
|
||||
ifeq ($(OS_NAME),darwin)
|
||||
bun-link-lld-release-dsym:
|
||||
$(DSYMUTIL) -o $(BUN_RELEASE_BIN).dSYM $(BUN_RELEASE_BIN)
|
||||
@@ -1205,6 +1210,7 @@ endif
|
||||
|
||||
|
||||
bun-relink: bun-relink-copy bun-link-lld-release bun-link-lld-release-dsym
|
||||
bun-relink-fast: bun-relink-copy bun-link-lld-release-no-lto
|
||||
|
||||
wasm-return1:
|
||||
zig build-lib -OReleaseSmall test/bun.js/wasm-return-1-test.zig -femit-bin=test/bun.js/wasm-return-1-test.wasm -target wasm32-freestanding
|
||||
@@ -1218,7 +1224,7 @@ generate-sink:
|
||||
|
||||
EMIT_LLVM_FOR_RELEASE=-emit-llvm -flto="full"
|
||||
EMIT_LLVM_FOR_DEBUG=
|
||||
EMIT_LLVM=$(EMIT_LLVM_FOR_RELEASE)
|
||||
EMIT_LLVM=$(EMIT_LLVM_FOR_DEBUG)
|
||||
|
||||
# We do this outside of build.zig for performance reasons
|
||||
# The C compilation stuff with build.zig is really slow and we don't need to run this as often as the rest
|
||||
|
||||
102
misctools/headers-cleaner.js
Normal file
102
misctools/headers-cleaner.js
Normal file
@@ -0,0 +1,102 @@
|
||||
// this file is intended to be runnable both from node and bun
|
||||
var { readFileSync, writeFileSync } = require("fs");
|
||||
var { join } = require("path");
|
||||
|
||||
const destination = join(__dirname, "../src/bun.js/bindings/headers.zig");
|
||||
const replacements = join(
|
||||
__dirname,
|
||||
"../src/bun.js/bindings/headers-replacements.zig"
|
||||
);
|
||||
|
||||
console.log("Writing to", destination);
|
||||
var output = "// GENERATED CODE - DO NOT MODIFY BY HAND\n\n";
|
||||
var input = readFileSync(destination, "utf8");
|
||||
|
||||
const first_extern = input.indexOf("extern fn");
|
||||
const first_extern_line = input.indexOf("\n", first_extern - 128);
|
||||
const last_extern_fn = input.lastIndexOf("extern");
|
||||
const last_extern_fn_line = input.indexOf("\n", last_extern_fn);
|
||||
const keep = (
|
||||
input.substring(0, first_extern_line) + input.substring(last_extern_fn_line)
|
||||
)
|
||||
.split("\n")
|
||||
.filter(
|
||||
(a) =>
|
||||
/const (JSC|WTF|Web)_/gi.test(a) &&
|
||||
!a.includes("JSValue") &&
|
||||
!a.includes("CatchScope")
|
||||
)
|
||||
.join("\n")
|
||||
.trim();
|
||||
|
||||
input = keep + input.slice(first_extern_line, last_extern_fn_line);
|
||||
input = input.replaceAll("*WebCore__", "*bindings.");
|
||||
input = input.replaceAll("*JSC__", "*bindings.");
|
||||
input = input.replaceAll("[*c] JSC__", "[*c]bindings.");
|
||||
input = input.replaceAll("[*c] const JSC__", "[*c]const bindings.");
|
||||
input = input.replaceAll(
|
||||
"[*c]Inspector__ScriptArguments",
|
||||
"[*c]bindings.ScriptArguments"
|
||||
);
|
||||
|
||||
input = input
|
||||
.replaceAll("VirtualMachine", "bindings.VirtualMachine")
|
||||
.replaceAll("bindings.bindings.VirtualMachine", "bindings.VirtualMachine");
|
||||
|
||||
const hardcode = {
|
||||
"[*c][*c]JSC__Exception": "*?*JSC__Exception ",
|
||||
"[*c]?*anyopaque": "[*c]*anyopaque",
|
||||
"[*c]JSC__JSGlobalObject": "?*JSC__JSGlobalObject",
|
||||
};
|
||||
|
||||
for (let key in hardcode) {
|
||||
const value = hardcode[key];
|
||||
input = input.replaceAll(key, value);
|
||||
}
|
||||
|
||||
const remove = [
|
||||
"pub const __darwin",
|
||||
"pub const _",
|
||||
"pub const __builtin",
|
||||
"pub const int",
|
||||
"pub const INT",
|
||||
"pub const uint",
|
||||
"pub const UINT",
|
||||
"pub const WCHAR",
|
||||
"pub const wchar",
|
||||
"pub const intmax",
|
||||
"pub const INTMAX",
|
||||
"pub const uintmax",
|
||||
"pub const UINTMAX",
|
||||
"pub const max_align_t",
|
||||
"pub const ZigErrorCode",
|
||||
"pub const JSClassRef",
|
||||
"pub const __",
|
||||
];
|
||||
var lines = input.split("\n");
|
||||
for (let prefix of remove) {
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
if (line.startsWith(prefix)) {
|
||||
lines[i] = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
if (line.includes("struct_")) {
|
||||
lines[i] = "";
|
||||
continue;
|
||||
}
|
||||
}
|
||||
input = lines.filter((a) => a.length > 0).join("\n");
|
||||
|
||||
writeFileSync(
|
||||
destination,
|
||||
output +
|
||||
"\n" +
|
||||
readFileSync(replacements, "utf8").trim() +
|
||||
"\n" +
|
||||
input.trim() +
|
||||
"\n"
|
||||
);
|
||||
@@ -1,58 +0,0 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
const headers_zig_file_src: std.builtin.SourceLocation = @src();
|
||||
var paths = [_][]const u8{ std.mem.span(headers_zig_file_src.file), "../../src/bun.js/bindings/headers.zig" };
|
||||
const headers_zig_file = try std.fs.path.resolve(std.heap.c_allocator, &paths);
|
||||
std.debug.print("Writing to {s}", .{headers_zig_file});
|
||||
var headers_zig: std.fs.File = try std.fs.openFileAbsolute(headers_zig_file, .{ .mode = .read_write });
|
||||
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 = "// GENERATED CODE - DO NOT MODIFY BY HAND\n\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);
|
||||
var begin = remainder[i..];
|
||||
|
||||
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);
|
||||
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]?*anyopaque",
|
||||
"[*c]*anyopaque",
|
||||
};
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user