ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.5k stars 2.52k forks source link

CompileStep.addRPath always generates absolute paths #15849

Open karlseguin opened 1 year ago

karlseguin commented 1 year ago

Zig Version

0.11.0-dev.3295+7cb2e653a

Steps to Reproduce and Observed Behavior

  1. Start a new project using zig init-exe
  2. Make a slight modification to build.zig:
pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "xx",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    b.installArtifact(exe);

    // link to any installed library, doesn't matter
    exe.linkSystemLibrary("ssl");

    // specify a relative rpath
    exe.addRPath("lib");

    const run_cmd = b.addRunArtifact(exe);
    const run_step = b.step("run", "Run the app");
    run_step.dependOn(&run_cmd.step);
}
  1. Build the project using zig build
  2. Inspect the run path:
    $ readelf --dynamic  zig-out/bin/test | grep RUNPATH
    0x000000000000001d (RUNPATH)            Library runpath: [/home/user/code/test/lib:/usr/lib/x86_64-linux-gnu:/lib/x86_64-linux-gnu]

build-exe works as expected:

$ zig build-exe src/main.zig --name test -lssl -rpath lib
$ readelf --dynamic  test | grep RUNPATH
 0x000000000000001d (RUNPATH)            Library runpath: [lib:/usr/lib/x86_64-linux-gnu:/lib/x86_64-linux-gnu]

Expected Behavior

The relative path should be preserved. build-exe and build.zig should be consistent with each other.

bfloch commented 6 months ago

Isn't this by design?

We have addRPathSpecial(...) for explicitly adding $ORIGIN (Linux) and $rpath/@executable_path/@loader_path (MacOS): https://github.com/ziglang/zig/blob/master/lib/std/Build/Module.zig#L583

There is a problem with assuming that relative paths should end up being either of the options as they have subtle differences.