Skip to content

Commit

Permalink
Merge pull request #50 from tranzystorekk/build-pie
Browse files Browse the repository at this point in the history
Allow building PIE binaries
  • Loading branch information
Arnau478 authored Jul 23, 2024
2 parents 24d9585 + 9ed1def commit e7ed174
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ fn getVersion(b: *std.Build) SemanticVersion {
}
}

fn addExe(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, release: bool, build_options: *std.Build.Step.Options, hevi_mod: *std.Build.Module) *std.Build.Step.Compile {
fn addExe(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, pie: bool, release: bool, build_options: *std.Build.Step.Options, hevi_mod: *std.Build.Module) *std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = if (release) b.fmt("hevi-{s}-{s}", .{ @tagName(target.result.cpu.arch), @tagName(target.result.os.tag) }) else "hevi",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

exe.pie = pie;
exe.root_module.addImport("hevi", hevi_mod);
exe.root_module.addOptions("build_options", build_options);
exe.root_module.addImport("ziggy", b.dependency("ziggy", .{}).module("ziggy"));
Expand All @@ -88,14 +89,16 @@ pub fn build(b: *std.Build) !void {

const optimize = b.standardOptimizeOption(.{});

const pie = b.option(bool, "pie", "Build a Position Independent Executable") orelse false;

var build_options = b.addOptions();
build_options.addOption(SemanticVersion, "version", getVersion(b));

const mod = b.addModule("hevi", .{
.root_source_file = b.path("src/hevi.zig"),
});

const exe = addExe(b, target, optimize, false, build_options, mod);
const exe = addExe(b, target, optimize, pie, false, build_options, mod);
b.installArtifact(exe);

const docs_step = b.step("docs", "Build the documentation");
Expand Down Expand Up @@ -128,7 +131,7 @@ pub fn build(b: *std.Build) !void {

const release_step = b.step("release", "Create release builds for all targets");
for (release_targets) |rt| {
const rexe = addExe(b, b.resolveTargetQuery(rt), .ReleaseSmall, true, build_options, mod);
const rexe = addExe(b, b.resolveTargetQuery(rt), .ReleaseSmall, false, true, build_options, mod);
release_step.dependOn(&b.addInstallArtifact(rexe, .{ .dest_sub_path = try std.fs.path.join(b.allocator, &.{ "release", rexe.out_filename }) }).step);
}

Expand Down

0 comments on commit e7ed174

Please sign in to comment.