From 44f39cf1a7f96c52e4acd0010b81b2b941dc9782 Mon Sep 17 00:00:00 2001 From: Fabio Valentini Date: Mar 22 2024 16:58:22 +0000 Subject: Only build in release mode / with optimizations when shipping binaries This required to no longer set RUSTFLAGS explicitly in a few places to ensure they are not getting set *three times* in some cases: 1. The "%__cargo" macro no longer sets the RUSTFLAGS environment variable for cargo invocations. 2. The "%cargo_build", "%cargo_test", and "%cargo_install" macros explicitly override RUSTFLAGS from the build environment with just "--cap-lints=warn". The "rpm" profile used by these three macros already contains compiler settings that are equivalent to the default RUSTFLAGS (except for "--cap-lints=warn"). No packages that ship binaries should be impacted by this change, whether they build with cargo directly, with meson, or use the RPM macros. Additionally, RUSTDOCFLAGS are now also set to "--cap-lints=warn" in the "%cargo_test" macro for the same reason that flag is in RUSTFLAGS, i.e. to avoid breaking builds (in this case, builds of doctests) due to new compiler or deprecation warnings and use of "#![deny(warnings)]" for code snippets in documentation. --- diff --git a/macros.d/macros.cargo b/macros.d/macros.cargo index 3d51751..da66e2d 100644 --- a/macros.d/macros.cargo +++ b/macros.d/macros.cargo @@ -6,7 +6,7 @@ # features that have not been stabilized yet, i.e. the # "-Z avoid-dev-deps" flag which is passed to cargo by the cargo_build, # cargo_install, and cargo_test macros. -%__cargo /usr/bin/env CARGO_HOME=.cargo RUSTC_BOOTSTRAP=1 RUSTFLAGS='%{build_rustflags}' /usr/bin/cargo +%__cargo /usr/bin/env CARGO_HOME=.cargo RUSTC_BOOTSTRAP=1 /usr/bin/cargo # __cargo_common_opts: common command line flags for cargo # @@ -152,13 +152,17 @@ EOF}}\ # cargo_build: builds the crate with cargo with the specified feature flags %cargo_build(naf:)\ -%{shrink: \ - %{__cargo} build \ - %{__cargo_common_opts} \ - --profile rpm \ - %{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ - %* \ -} +(\ +set -euo pipefail \ +export RUSTFLAGS="--cap-lints=warn" \ +%{shrink: \ + %{__cargo} build \ + %{__cargo_common_opts} \ + %{expr:"%{debug_package}" != "%{nil}" ? "--profile rpm" : ""} \ + %{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ + %* \ +}\ +) # cargo_test: runs the test suite with cargo with the specified feature flags # @@ -168,14 +172,19 @@ EOF}}\ # i.e. "%%cargo_test -- -- --skip foo" for skipping all tests with names that # match "foo". %cargo_test(naf:)\ -%{shrink: \ - %{__cargo} test \ - %{__cargo_common_opts} \ - --profile rpm \ - --no-fail-fast \ - %{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ - %* \ -} +(\ +set -euo pipefail \ +export RUSTFLAGS="--cap-lints=warn" \ +export RUSTDOCFLAGS="--cap-lints=warn" \ +%{shrink: \ + %{__cargo} test \ + %{__cargo_common_opts} \ + %{expr:"%{debug_package}" != "%{nil}" ? "--profile rpm" : ""} \ + --no-fail-fast \ + %{__cargo_parse_opts %{-n} %{-a} %{-f:-f%{-f*}}} \ + %* \ +}\ +) # cargo_install: install files into the buildroot # @@ -193,6 +202,7 @@ EOF}}\ %cargo_install(t:naf:)\ (\ set -euo pipefail \ +export RUSTFLAGS="--cap-lints=warn" \ if %{__cargo_is_lib} && [ %{cargo_install_lib} -eq 1 ] ; then \ CRATE_NAME=$(%{__cargo_to_rpm} --path Cargo.toml name) \ CRATE_VERSION=$(%{__cargo_to_rpm} --path Cargo.toml version) \ diff --git a/tests/test_macros_cargo.py b/tests/test_macros_cargo.py index 5aa93d3..7dc2fdc 100644 --- a/tests/test_macros_cargo.py +++ b/tests/test_macros_cargo.py @@ -2,8 +2,7 @@ import pytest def test_cargo(evaluater): - build_rustflags = evaluater("%build_rustflags")[0] - assert evaluater("%__cargo")[0] == f"/usr/bin/env CARGO_HOME=.cargo RUSTC_BOOTSTRAP=1 RUSTFLAGS='{build_rustflags}' /usr/bin/cargo" + assert evaluater("%__cargo")[0] == "/usr/bin/env CARGO_HOME=.cargo RUSTC_BOOTSTRAP=1 /usr/bin/cargo" def test_cargo_to_rpm(evaluater): @@ -263,24 +262,41 @@ 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} --profile rpm" + assert [line.rstrip() for line in evaluater("%cargo_build")[0].splitlines()] == [ + "(", + "set -euo pipefail", + "export RUSTFLAGS=\"--cap-lints=warn\"", + " ".join([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} --profile rpm --no-fail-fast" + assert [line.rstrip() for line in evaluater("%cargo_test")[0].splitlines()] == [ + "(", + "set -euo pipefail", + "export RUSTFLAGS=\"--cap-lints=warn\"", + "export RUSTDOCFLAGS=\"--cap-lints=warn\"", + " ".join([f"{cargo} test", cargo_common_opts, "--profile rpm", "--no-fail-fast"]), + ")", + ] def test_cargo_test_with_args(evaluater): cargo = evaluater("%__cargo")[0] cargo_common_opts = evaluater("%__cargo_common_opts")[0] - assert ( - evaluater("%cargo_test -- -- --exact --skip foo")[0] - == f"{cargo} test {cargo_common_opts} --profile rpm --no-fail-fast -- --exact --skip foo" - ) + assert [line.rstrip() for line in evaluater("%cargo_test -- -- --exact --skip foo")[0].splitlines()] == [ + "(", + "set -euo pipefail", + "export RUSTFLAGS=\"--cap-lints=warn\"", + "export RUSTDOCFLAGS=\"--cap-lints=warn\"", + " ".join([f"{cargo} test", cargo_common_opts, "--profile rpm", "--no-fail-fast", "--", "--exact", "--skip", "foo"]), + ")", + ] def test_cargo_install(evaluater): @@ -297,6 +313,7 @@ def test_cargo_install(evaluater): assert [line.rstrip() for line in evaluater("%cargo_install")[0].splitlines()] == [ "(", "set -euo pipefail", + "export RUSTFLAGS=\"--cap-lints=warn\"", f"if {cargo_is_lib} && [ 1 -eq 1 ] ; then", f" CRATE_NAME=$({cargo_to_rpm} --path Cargo.toml name)", f" CRATE_VERSION=$({cargo_to_rpm} --path Cargo.toml version)",