commit:     e4f91d2c2e48b6d6e068a0f1c1adffcece28a324
Author:     sin-ack <sin-ack <AT> protonmail <DOT> com>
AuthorDate: Sun Jan 19 10:05:05 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Mon Mar  3 19:26:28 2025 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=e4f91d2c

zig.eclass: always use absolute path for `--prefix` argument

Needed for correctness when `build.zig` uses them to manipulate default
pathes in compiled artifact. Without `DESTDIR` it would try to install
to root directly, so to fix this we set `DESTDIR` where appliable.

Co-authored-by: Eric Joldasov <bratishkaerik <AT> landless-city.net>
Signed-off-by: Eric Joldasov <bratishkaerik <AT> landless-city.net>
Closes: https://github.com/gentoo/gentoo/pull/40197
Signed-off-by: Sam James <sam <AT> gentoo.org>

 dev-lang/zig/zig-0.13.0-r2.ebuild | 13 +++++++------
 dev-lang/zig/zig-0.13.0-r3.ebuild |  9 +++++----
 dev-lang/zig/zig-9999.ebuild      |  9 +++++----
 eclass/zig.eclass                 | 27 +++++++++++++++++++++------
 4 files changed, 38 insertions(+), 20 deletions(-)

diff --git a/dev-lang/zig/zig-0.13.0-r2.ebuild 
b/dev-lang/zig/zig-0.13.0-r2.ebuild
index a17a501b392e..1eca3d4bd335 100644
--- a/dev-lang/zig/zig-0.13.0-r2.ebuild
+++ b/dev-lang/zig/zig-0.13.0-r2.ebuild
@@ -92,7 +92,7 @@ pkg_setup() {
        # Skip detecting zig executable.
        ZIG_EXE="not-applicable" ZIG_VER="${PV}" zig_pkg_setup
 
-       export ZIG_SYS_INSTALL_DEST="${EPREFIX}/usr/$(get_libdir)/zig/${PV}"
+       declare -r -g 
ZIG_SYS_INSTALL_DEST="${EPREFIX}/usr/$(get_libdir)/zig/${PV}"
 
        if use llvm; then
                tc-is-cross-compiler && die "USE=llvm is not yet supported when 
cross-compiling"
@@ -142,7 +142,8 @@ src_configure() {
        # are used only after compiling zig2.
        local my_zbs_args=(
                --zig-lib-dir "${S}/lib/"
-               # Will be a subdir under ZIG_SYS_INSTALL_DEST.
+
+               --prefix "${ZIG_SYS_INSTALL_DEST}/"
                --prefix-lib-dir lib/
 
                # These are built separately
@@ -199,12 +200,12 @@ src_compile() {
        fi
 
        cd "${BUILD_DIR}" || die
-       ZIG_EXE="./zig2" zig_src_compile --prefix "${BUILD_DIR}/stage3/"
+       ZIG_EXE="./zig2" zig_src_compile --prefix stage3/
 
        ./stage3/bin/zig env || die "Zig compilation failed"
 
        if use doc; then
-               ZIG_EXE="./stage3/bin/zig" zig_src_compile langref --prefix 
"${S}/docgen/"
+               ZIG_EXE="./stage3/bin/zig" zig_src_compile langref --prefix 
docgen/
        fi
 }
 
@@ -241,9 +242,9 @@ src_test() {
 }
 
 src_install() {
-       use doc && local HTML_DOCS=( "docgen/doc/langref.html" )
+       use doc && local HTML_DOCS=( "${BUILD_DIR}/docgen/doc/langref.html" )
 
-       ZIG_EXE="./zig2" zig_src_install --prefix "${ZIG_SYS_INSTALL_DEST}"
+       ZIG_EXE="./zig2" zig_src_install
 
        cd "${D}/${ZIG_SYS_INSTALL_DEST}" || die
        mv lib/zig/ lib2/ || die

diff --git a/dev-lang/zig/zig-0.13.0-r3.ebuild 
b/dev-lang/zig/zig-0.13.0-r3.ebuild
index d893e2e33ea2..474339a4e1a3 100644
--- a/dev-lang/zig/zig-0.13.0-r3.ebuild
+++ b/dev-lang/zig/zig-0.13.0-r3.ebuild
@@ -143,7 +143,8 @@ src_configure() {
        # are used only after compiling zig2.
        local my_zbs_args=(
                --zig-lib-dir "${S}/lib/"
-               # Will be a subdir under ZIG_SYS_INSTALL_DEST.
+
+               --prefix "${ZIG_SYS_INSTALL_DEST}/"
                --prefix-lib-dir lib/
 
                # These are built separately
@@ -283,7 +284,7 @@ src_compile() {
                ./stage3/bin/zig env || die "Zig compilation failed"
 
                if use doc; then
-                       ZIG_EXE="./stage3/bin/zig" zig_src_compile langref 
--prefix "${S}/docgen/"
+                       ZIG_EXE="./stage3/bin/zig" zig_src_compile langref 
--prefix docgen/
                fi
        fi
 }
@@ -321,9 +322,9 @@ src_test() {
 }
 
 src_install() {
-       use doc && local HTML_DOCS=( "docgen/doc/langref.html" )
+       use doc && local HTML_DOCS=( "${BUILD_DIR}/docgen/doc/langref.html" )
 
-       ZIG_EXE="./zig2" zig_src_install --prefix "${ZIG_SYS_INSTALL_DEST}"
+       ZIG_EXE="./zig2" zig_src_install
 
        cd "${D}/${ZIG_SYS_INSTALL_DEST}" || die
        mv lib/zig/ lib2/ || die

diff --git a/dev-lang/zig/zig-9999.ebuild b/dev-lang/zig/zig-9999.ebuild
index 8fb29ab10b9e..fb120976ef65 100644
--- a/dev-lang/zig/zig-9999.ebuild
+++ b/dev-lang/zig/zig-9999.ebuild
@@ -136,7 +136,8 @@ src_configure() {
        # are used only after compiling zig2.
        local my_zbs_args=(
                --zig-lib-dir "${S}/lib/"
-               # Will be a subdir under ZIG_SYS_INSTALL_DEST.
+
+               --prefix "${ZIG_SYS_INSTALL_DEST}/"
                --prefix-lib-dir lib/
 
                # These are built separately
@@ -276,7 +277,7 @@ src_compile() {
                ./stage3/bin/zig env || die "Zig compilation failed"
 
                if use doc; then
-                       ZIG_EXE="./stage3/bin/zig" zig_src_compile langref 
--prefix "${S}/docgen/"
+                       ZIG_EXE="./stage3/bin/zig" zig_src_compile langref 
--prefix docgen/
                fi
        fi
 }
@@ -314,9 +315,9 @@ src_test() {
 }
 
 src_install() {
-       use doc && local HTML_DOCS=( "docgen/doc/langref.html" )
+       use doc && local HTML_DOCS=( "${BUILD_DIR}/docgen/doc/langref.html" )
 
-       ZIG_EXE="./zig2" zig_src_install --prefix "${ZIG_SYS_INSTALL_DEST}"
+       ZIG_EXE="./zig2" zig_src_install
 
        cd "${D}/${ZIG_SYS_INSTALL_DEST}" || die
        mv lib/zig/ lib2/ || die

diff --git a/eclass/zig.eclass b/eclass/zig.eclass
index 26b55bf05d7f..0de7ee9eb47a 100644
--- a/eclass/zig.eclass
+++ b/eclass/zig.eclass
@@ -160,6 +160,8 @@ _zig_set_zbs_uris
 # need to override default optimize mode of this eclass (ReleaseSafe)
 # with your default, please use "--release=small" etc. syntax so that
 # user can still override it in ZBS_ARGS_EXTRA.
+# Note: `--prefix` and other relative arguments will
+# process here as relative to BUILD_DIR.
 #
 # Example:
 # @CODE
@@ -292,14 +294,18 @@ zig_init_base_args() {
                -Dcpu="${ZIG_CPU}"
                --release=safe
 
+               # We want absolute path here so that it would appear correctly
+               # when embedded to binaries with build.zig, but without DESTDIR
+               # it would try to escape sandbox and install directly to root.
+               #
+               # Therefore, we set DESTDIR each time to be:
+               # 1) BUILD_DIR in phases before `src_install`,
+               # 2) D during `src_install`.
+               --prefix "${EPREFIX}/usr/"
                --prefix-exe-dir bin/
                --prefix-lib-dir "$(get_libdir)/"
                --prefix-include-dir include/
 
-               # Should be relative path to make other calls easier,
-               # so remove leading slash here.
-               --prefix "${EPREFIX:+${EPREFIX#/}/}usr/"
-
                --libc "${T}/zig_libc.txt"
        )
        if [[ "${ZBS_VERBOSE}" != OFF ]]; then
@@ -486,12 +492,17 @@ zig_src_configure() {
 # @DESCRIPTION:
 # Calls "ezig build" with previously set ZBS_ARGS.
 # Args passed to this function will be passed after ZBS_ARGS.
+# They can be used to call custom steps or override some
+# options temporarily like `--prefix`.
+# Note that `--prefix` and other relative arguments will
+# process here as relative to BUILD_DIR.
 zig_src_compile() {
        pushd "${BUILD_DIR}" > /dev/null || die
 
        local args=( "${ZBS_ARGS[@]}" "${@}" )
        einfo "ZBS: compiling with: ${args[@]}"
-       nonfatal ezig build "${args[@]}" || die "ZBS: compilation failed"
+       DESTDIR="${BUILD_DIR}" nonfatal ezig build "${args[@]}" ||
+               die "ZBS: compilation failed"
 
        popd > /dev/null || die
 }
@@ -504,6 +515,8 @@ zig_src_compile() {
 # Args passed to this function will be passed after ZBS_ARGS.
 # Note: currently step detection might give false positives in
 # very rare cases, it will be improved in the future.
+# Note that `--prefix` and other relative arguments will
+# process here as relative to BUILD_DIR.
 zig_src_test() {
        pushd "${BUILD_DIR}" > /dev/null || die
 
@@ -525,7 +538,7 @@ zig_src_test() {
                nonfatal ezig build --list-steps "${args[@]}"
        ); then
                einfo "ZBS: testing with: ${args[@]}"
-               nonfatal ezig build test "${args[@]}" ||
+               DESTDIR="${BUILD_DIR}" nonfatal ezig build test "${args[@]}" ||
                        die "ZBS: tests failed"
        else
                einfo "Test step not found, skipping."
@@ -540,6 +553,8 @@ zig_src_test() {
 # Calls "ezig build" with DESTDIR and previously set ZBS_ARGS.
 # Args passed to this function will be passed after ZBS_ARGS.
 # Also installs documentation via "einstalldocs".
+# Note that `--prefix` and other relative arguments will
+# process here as relative to D.
 zig_src_install() {
        pushd "${BUILD_DIR}" > /dev/null || die
        local args=( "${ZBS_ARGS[@]}" "${@}" )

Reply via email to