From 4dcd3edf0175cabd82a5251c1847d628f3335da8 Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Sep 26 2023 13:11:48 +0000 Subject: [PATCH 1/6] set custom profile for cargo to override incompatible upstream settings --- diff --git a/macros.d/macros.cargo b/macros.d/macros.cargo index 4bfc519..8fb798a 100644 --- a/macros.d/macros.cargo +++ b/macros.d/macros.cargo @@ -65,6 +65,13 @@ cat > .cargo/config << EOF\ rustc = "%{__rustc}"\ rustdoc = "%{__rustdoc}"\ \ +[profile.rpm]\ +inherits = "release"\ +opt-level = %{rustflags_opt_level}\ +codegen-units = %{rustflags_codegen_units}\ +debug = %{rustflags_debuginfo}\ +strip = "none"\ +\ [env]\ CFLAGS = "%{build_cflags}"\ CXXFLAGS = "%{build_cxxflags}"\ @@ -124,7 +131,7 @@ EOF}}\ %{shrink: \ %{__cargo} build \ %{__cargo_common_opts} \ - --release \ + --profile rpm \ %{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ %* \ } @@ -140,7 +147,7 @@ EOF}}\ %{shrink: \ %{__cargo} test \ %{__cargo_common_opts} \ - --release \ + --profile rpm \ --no-fail-fast \ %{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ %* \ @@ -178,6 +185,7 @@ if %{__cargo_is_bin} ; then \ %{shrink: \ %{__cargo} install \ %{__cargo_common_opts} \ + --profile rpm \ --no-track \ --path . \ %{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ diff --git a/tests/test_macros_cargo.py b/tests/test_macros_cargo.py index a7916f6..db98ee5 100644 --- a/tests/test_macros_cargo.py +++ b/tests/test_macros_cargo.py @@ -51,6 +51,9 @@ def test_cargo_prep(evaluater): buildroot = evaluater("%buildroot")[0] prefix = evaluater("%_prefix")[0] cargo_registry = evaluater("%cargo_registry")[0] + rustflags_opt_level = evaluater("%rustflags_opt_level")[0] + rustflags_codegen_units = evaluater("%rustflags_codegen_units")[0] + rustflags_debuginfo = evaluater("%rustflags_debuginfo")[0] assert evaluater("%cargo_prep")[0].splitlines() == [ "(", @@ -61,6 +64,13 @@ def test_cargo_prep(evaluater): 'rustc = "/usr/bin/rustc"', 'rustdoc = "/usr/bin/rustdoc"', "", + "[profile.rpm]", + 'inherits = "release"', + f"opt-level = {rustflags_opt_level}", + f"codegen-units = {rustflags_codegen_units}", + f"debug = {rustflags_debuginfo}", + 'strip = "none"', + "", "[env]", f'CFLAGS = "{build_cflags} "', f'CXXFLAGS = "{build_cxxflags} "', @@ -96,6 +106,9 @@ def test_cargo_prep_vendor(evaluater): build_ldflags = evaluater("%build_ldflags")[0] buildroot = evaluater("%buildroot")[0] prefix = evaluater("%_prefix")[0] + rustflags_opt_level = evaluater("%rustflags_opt_level")[0] + rustflags_codegen_units = evaluater("%rustflags_codegen_units")[0] + rustflags_debuginfo = evaluater("%rustflags_debuginfo")[0] assert evaluater("%cargo_prep -v vendor")[0].splitlines() == [ "(", @@ -106,6 +119,13 @@ def test_cargo_prep_vendor(evaluater): 'rustc = "/usr/bin/rustc"', 'rustdoc = "/usr/bin/rustdoc"', "", + "[profile.rpm]", + 'inherits = "release"', + f"opt-level = {rustflags_opt_level}", + f"codegen-units = {rustflags_codegen_units}", + f"debug = {rustflags_debuginfo}", + 'strip = "none"', + "", "[env]", f'CFLAGS = "{build_cflags} "', f'CXXFLAGS = "{build_cxxflags} "', @@ -139,6 +159,9 @@ def test_cargo_prep_no_replacement(evaluater): build_ldflags = evaluater("%build_ldflags")[0] buildroot = evaluater("%buildroot")[0] prefix = evaluater("%_prefix")[0] + rustflags_opt_level = evaluater("%rustflags_opt_level")[0] + rustflags_codegen_units = evaluater("%rustflags_codegen_units")[0] + rustflags_debuginfo = evaluater("%rustflags_debuginfo")[0] assert evaluater("%cargo_prep -N")[0].splitlines() == [ "(", @@ -149,6 +172,13 @@ def test_cargo_prep_no_replacement(evaluater): 'rustc = "/usr/bin/rustc"', 'rustdoc = "/usr/bin/rustdoc"', "", + "[profile.rpm]", + 'inherits = "release"', + f"opt-level = {rustflags_opt_level}", + f"codegen-units = {rustflags_codegen_units}", + f"debug = {rustflags_debuginfo}", + 'strip = "none"', + "", "[env]", f'CFLAGS = "{build_cflags} "', f'CXXFLAGS = "{build_cxxflags} "', @@ -214,14 +244,14 @@ def test_cargo_build(evaluater): cargo = evaluater("%__cargo")[0] cargo_common_opts = evaluater("%__cargo_common_opts")[0] - assert evaluater("%cargo_build")[0] == f"{cargo} build {cargo_common_opts} --release" + assert evaluater("%cargo_build")[0] == f"{cargo} build {cargo_common_opts} --profile rpm" def test_cargo_test(evaluater): cargo = evaluater("%__cargo")[0] cargo_common_opts = evaluater("%__cargo_common_opts")[0] - assert evaluater("%cargo_test")[0] == f"{cargo} test {cargo_common_opts} --release --no-fail-fast" + assert evaluater("%cargo_test")[0] == f"{cargo} test {cargo_common_opts} --profile rpm --no-fail-fast" def test_cargo_test_with_args(evaluater): @@ -230,7 +260,7 @@ def test_cargo_test_with_args(evaluater): assert ( evaluater("%cargo_test -- -- --exact --skip foo")[0] - == f"{cargo} test {cargo_common_opts} --release --no-fail-fast -- --exact --skip foo" + == f"{cargo} test {cargo_common_opts} --profile rpm --no-fail-fast -- --exact --skip foo" ) @@ -261,7 +291,7 @@ def test_cargo_install(evaluater): f' echo \'{{"files":{{}},"package":""}}\' > $REG_DIR/.cargo-checksum.json', f"fi", f"if {cargo_is_bin} ; then", - f" {cargo} install {cargo_common_opts} --no-track --path .", + f" {cargo} install {cargo_common_opts} --profile rpm --no-track --path .", f"fi", f")", ] From 13299af7dc6ad881adb568682a68d26fc97d4f70 Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Sep 26 2023 13:21:12 +0000 Subject: [PATCH 2/6] unconditionally remove pre-existing .cargo directories in cargo_prep --- diff --git a/macros.d/macros.cargo b/macros.d/macros.cargo index 8fb798a..318698f 100644 --- a/macros.d/macros.cargo +++ b/macros.d/macros.cargo @@ -59,6 +59,7 @@ %{-v:%{-N:%{error:-v and -N are mutually exclusive!}}}\ (\ set -euo pipefail\ +%{__rm} -rf .cargo/\ %{__mkdir} -p .cargo\ cat > .cargo/config << EOF\ [build]\ diff --git a/tests/test_macros_cargo.py b/tests/test_macros_cargo.py index db98ee5..fa84918 100644 --- a/tests/test_macros_cargo.py +++ b/tests/test_macros_cargo.py @@ -58,6 +58,7 @@ def test_cargo_prep(evaluater): assert evaluater("%cargo_prep")[0].splitlines() == [ "(", "set -euo pipefail", + "/usr/bin/rm -rf .cargo/", "/usr/bin/mkdir -p .cargo", "cat > .cargo/config << EOF", "[build]", @@ -113,6 +114,7 @@ def test_cargo_prep_vendor(evaluater): assert evaluater("%cargo_prep -v vendor")[0].splitlines() == [ "(", "set -euo pipefail", + "/usr/bin/rm -rf .cargo/", "/usr/bin/mkdir -p .cargo", "cat > .cargo/config << EOF", "[build]", @@ -166,6 +168,7 @@ def test_cargo_prep_no_replacement(evaluater): assert evaluater("%cargo_prep -N")[0].splitlines() == [ "(", "set -euo pipefail", + "/usr/bin/rm -rf .cargo/", "/usr/bin/mkdir -p .cargo", "cat > .cargo/config << EOF", "[build]", From fc319c9bec5afed7546bae7cc4171a767710061c Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Sep 26 2023 14:20:04 +0000 Subject: [PATCH 3/6] add symlink from target/rpm to target/release for compatibility --- diff --git a/macros.d/macros.cargo b/macros.d/macros.cargo index 318698f..ca1eea3 100644 --- a/macros.d/macros.cargo +++ b/macros.d/macros.cargo @@ -129,13 +129,17 @@ EOF}}\ # cargo_build: builds the crate with cargo with the specified feature flags %cargo_build(naf:)\ +(\ +set -euo pipefail\ %{shrink: \ %{__cargo} build \ %{__cargo_common_opts} \ --profile rpm \ %{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ %* \ -} +}\ +%{__ln} -s rpm target/release\ +) # cargo_test: runs the test suite with cargo with the specified feature flags # diff --git a/tests/test_macros_cargo.py b/tests/test_macros_cargo.py index fa84918..b18f296 100644 --- a/tests/test_macros_cargo.py +++ b/tests/test_macros_cargo.py @@ -246,8 +246,15 @@ def test_cargo_generate_buildrequires(evaluater, flags: str, expected: str): def test_cargo_build(evaluater): cargo = evaluater("%__cargo")[0] cargo_common_opts = evaluater("%__cargo_common_opts")[0] + ln = evaluater("%__ln")[0] - assert evaluater("%cargo_build")[0] == f"{cargo} build {cargo_common_opts} --profile rpm" + assert [line.rstrip() for line in evaluater("%cargo_build")[0].splitlines()] == [ + f"(", + f"set -euo pipefail", + f"{cargo} build {cargo_common_opts} --profile rpm", + f"{ln} -s rpm target/release", + f")", + ] def test_cargo_test(evaluater): From 3b69c9c2421d38ed6c39be005e5b15fe9e70bff3 Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Sep 26 2023 16:20:54 +0000 Subject: [PATCH 4/6] simplify cargo_build macro (no need for subshell here) --- diff --git a/macros.d/macros.cargo b/macros.d/macros.cargo index ca1eea3..ea9a720 100644 --- a/macros.d/macros.cargo +++ b/macros.d/macros.cargo @@ -129,8 +129,6 @@ EOF}}\ # cargo_build: builds the crate with cargo with the specified feature flags %cargo_build(naf:)\ -(\ -set -euo pipefail\ %{shrink: \ %{__cargo} build \ %{__cargo_common_opts} \ @@ -138,8 +136,7 @@ set -euo pipefail\ %{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ %* \ }\ -%{__ln} -s rpm target/release\ -) +%{__ln} -s rpm target/release # cargo_test: runs the test suite with cargo with the specified feature flags # diff --git a/tests/test_macros_cargo.py b/tests/test_macros_cargo.py index b18f296..92792e9 100644 --- a/tests/test_macros_cargo.py +++ b/tests/test_macros_cargo.py @@ -249,11 +249,8 @@ def test_cargo_build(evaluater): ln = evaluater("%__ln")[0] assert [line.rstrip() for line in evaluater("%cargo_build")[0].splitlines()] == [ - f"(", - f"set -euo pipefail", f"{cargo} build {cargo_common_opts} --profile rpm", f"{ln} -s rpm target/release", - f")", ] From aae524ce14a00cba3d219e36eee7063dfa0eb84a Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Sep 27 2023 10:28:44 +0000 Subject: [PATCH 5/6] move creation of target/release compat symlink to %cargo_prep Not all projects that use %cargo_prep use %cargo_build (like Python extensions built with setuptools_rust or maturin). To ensure the symlink for the different profile name is always present, move its creation into the same macro that also sets up the profile. --- diff --git a/macros.d/macros.cargo b/macros.d/macros.cargo index ea9a720..fd482b0 100644 --- a/macros.d/macros.cargo +++ b/macros.d/macros.cargo @@ -59,6 +59,8 @@ %{-v:%{-N:%{error:-v and -N are mutually exclusive!}}}\ (\ set -euo pipefail\ +%{__mkdir} -p target/rpm\ +%{__ln} -s rpm target/release\ %{__rm} -rf .cargo/\ %{__mkdir} -p .cargo\ cat > .cargo/config << EOF\ @@ -135,8 +137,7 @@ EOF}}\ --profile rpm \ %{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ %* \ -}\ -%{__ln} -s rpm target/release +} # cargo_test: runs the test suite with cargo with the specified feature flags # diff --git a/tests/test_macros_cargo.py b/tests/test_macros_cargo.py index 92792e9..ed46305 100644 --- a/tests/test_macros_cargo.py +++ b/tests/test_macros_cargo.py @@ -58,6 +58,8 @@ def test_cargo_prep(evaluater): assert evaluater("%cargo_prep")[0].splitlines() == [ "(", "set -euo pipefail", + "/usr/bin/mkdir -p target/rpm", + "/usr/bin/ln -s rpm target/release", "/usr/bin/rm -rf .cargo/", "/usr/bin/mkdir -p .cargo", "cat > .cargo/config << EOF", @@ -114,6 +116,8 @@ def test_cargo_prep_vendor(evaluater): assert evaluater("%cargo_prep -v vendor")[0].splitlines() == [ "(", "set -euo pipefail", + "/usr/bin/mkdir -p target/rpm", + "/usr/bin/ln -s rpm target/release", "/usr/bin/rm -rf .cargo/", "/usr/bin/mkdir -p .cargo", "cat > .cargo/config << EOF", @@ -168,6 +172,8 @@ def test_cargo_prep_no_replacement(evaluater): assert evaluater("%cargo_prep -N")[0].splitlines() == [ "(", "set -euo pipefail", + "/usr/bin/mkdir -p target/rpm", + "/usr/bin/ln -s rpm target/release", "/usr/bin/rm -rf .cargo/", "/usr/bin/mkdir -p .cargo", "cat > .cargo/config << EOF", @@ -246,12 +252,8 @@ def test_cargo_generate_buildrequires(evaluater, flags: str, expected: str): def test_cargo_build(evaluater): cargo = evaluater("%__cargo")[0] cargo_common_opts = evaluater("%__cargo_common_opts")[0] - ln = evaluater("%__ln")[0] - assert [line.rstrip() for line in evaluater("%cargo_build")[0].splitlines()] == [ - f"{cargo} build {cargo_common_opts} --profile rpm", - f"{ln} -s rpm target/release", - ] + assert evaluater("%cargo_build")[0] == f"{cargo} build {cargo_common_opts} --profile rpm" def test_cargo_test(evaluater): From 8f17f183234ab2a9aadd183c0758815ef6b75b33 Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Sep 27 2023 20:22:24 +0000 Subject: [PATCH 6/6] also include -Cstrip=none in $RUSTFLAGS / %build_rustflags --- diff --git a/macros.d/macros.rust b/macros.d/macros.rust index 9967804..f5fd0a1 100644 --- a/macros.d/macros.rust +++ b/macros.d/macros.rust @@ -41,6 +41,7 @@ -Copt-level=%rustflags_opt_level -Cdebuginfo=%rustflags_debuginfo -Ccodegen-units=%rustflags_codegen_units + -Cstrip=none %{expr:0%{?_include_frame_pointers} && ("%{_arch}" != "ppc64le" && "%{_arch}" != "s390x" && "%{_arch}" != "i386") ? "-Cforce-frame-pointers=yes" : ""} -Clink-arg=-Wl,-z,relro -Clink-arg=-Wl,-z,now diff --git a/tests/test_macros_rust.py b/tests/test_macros_rust.py index dc2660a..7e7544e 100644 --- a/tests/test_macros_rust.py +++ b/tests/test_macros_rust.py @@ -16,3 +16,27 @@ def test_rustflags_debuginfo(evaluater): def test_rustflags_codegen_units(evaluater): assert evaluater("%rustflags_codegen_units")[0] == "1" + + +def test_build_rustflags(evaluater): + rustflags_opt_level = evaluater("%rustflags_opt_level")[0] + rustflags_debuginfo = evaluater("%rustflags_debuginfo")[0] + rustflags_codegen_units = evaluater("%rustflags_codegen_units")[0] + rustflags_force_frame_pointers = evaluater( + '%{expr:0%{?_include_frame_pointers} && ("%{_arch}" != "ppc64le" && "%{_arch}" != "s390x" && "%{_arch}" != "i386") ? "-Cforce-frame-pointers=yes" : ""}' + )[0] + + expected = [ + f"-Copt-level={rustflags_opt_level}", + f"-Cdebuginfo={rustflags_debuginfo}", + f"-Ccodegen-units={rustflags_codegen_units}", + "-Cstrip=none", + "-Clink-arg=-Wl,-z,relro", + "-Clink-arg=-Wl,-z,now", + "--cap-lints=warn", + ] + + if rustflags_force_frame_pointers: + expected.insert(4, rustflags_force_frame_pointers) + + assert evaluater("%build_rustflags")[0] == " ".join(expected)