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

Enable lto in zig #17139

Draft
wants to merge 3 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
12 changes: 11 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const BunBuildOptions = struct {
optimize: OptimizeMode,
os: OperatingSystem,
arch: Arch,

lto: bool = false,
version: Version,
canary_revision: ?u32,
sha: []const u8,
Expand Down Expand Up @@ -215,6 +215,7 @@ pub fn build(b: *Build) !void {
const obj_format = b.option(ObjectFormat, "obj_format", "Output file for object files") orelse .obj;

const no_llvm = b.option(bool, "no_llvm", "Experiment with Zig self hosted backends. No stability guaranteed") orelse false;
b.verbose = true;

var build_options = BunBuildOptions{
.target = target,
Expand All @@ -238,6 +239,8 @@ pub fn build(b: *Build) !void {
"0.0.0-unset",
),

.lto = b.option(bool, "lto", "Enable LTO") orelse false,

.sha = sha: {
const sha_buildoption = b.option([]const u8, "sha", "Force the git sha");
const sha_github = b.graph.env_map.get("GITHUB_SHA");
Expand Down Expand Up @@ -283,8 +286,10 @@ pub fn build(b: *Build) !void {
{
var step = b.step("obj", "Build Bun's Zig code as a .o file");
var bun_obj = addBunObject(b, &build_options);

step.dependOn(&bun_obj.step);
step.dependOn(addInstallObjectFile(b, bun_obj, "bun-zig", obj_format));
build_options.lto = false;
}

// zig build windows-shim
Expand Down Expand Up @@ -442,6 +447,7 @@ pub fn addBunObject(b: *Build, opts: *BunBuildOptions) *Compile {

.omit_frame_pointer = false,
.strip = false, // stripped at the end

});
if (opts.enable_asan) {
if (@hasField(Build.Module, "sanitize_address")) {
Expand All @@ -452,6 +458,10 @@ pub fn addBunObject(b: *Build, opts: *BunBuildOptions) *Compile {
}
}
obj.bundle_compiler_rt = false;
if (opts.lto) {
obj.want_lto = true;
}

obj.root_module.omit_frame_pointer = false;

// Link libc
Expand Down
5 changes: 5 additions & 0 deletions cmake/targets/BuildBun.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,11 @@ if(NOT "${REVISION}" STREQUAL "")
set(ZIG_FLAGS_BUN ${ZIG_FLAGS_BUN} -Dsha=${REVISION})
endif()

if(ENABLE_LTO)
set(ZIG_FLAGS_BUN ${ZIG_FLAGS_BUN} -Dlto=true)
endif()


register_command(
TARGET
bun-zig
Expand Down