diff --git a/buildflags.md b/buildflags.md index 0332c9f..121694e 100644 --- a/buildflags.md +++ b/buildflags.md @@ -5,6 +5,20 @@ and how to use them. # Using RPM build flags +The %set_build_flags macro sets the environment variables `CFLAGS`, +`CXXFLAGS`, `FFLAGS`, `FCFLAGS`, `LDFLAGS` and `LT_SYS_LIBRARY_PATH` to +the value of their corresponding rpm macros. %set_build_flags is automatically +called prior to the %build, %check, and %install phases so these flags can be +used by makefiles and other build tools. + +You can opt out of this behavior by doing: + + %undefine _auto_set_build_flags + +If you do opt out of this behavior, you can still manually use %set_build_flags +by adding it to the %build section of your spec file or by using one of the +build system helper macros like %configure, %cmake, and %meson + For packages which use autoconf to set up the build environment, use the `%configure` macro to obtain the full complement of flags, like this: @@ -16,20 +30,6 @@ This will invoke `./configure` with arguments (such as to that, some common problems in autotools scripts are automatically patched across the source tree. -As a side effect, this will set the environment variables `CFLAGS`, -`CXXFLAGS`, `FFLAGS`, `FCFLAGS`, `LDFLAGS` and `LT_SYS_LIBRARY_PATH`, -so they can be used by makefiles and other build tools. (However, -existing values for these variables are not overwritten.) - -If your package does not use autoconf, you can still set the same -environment variables using - - %set_build_flags - -early in the `%build` section. (Again, existing environment variables -are not overwritten.) `%set_build_flags` does not perform autotools -script rewriting, unlike `%configure`. - Individual build flags are also available through RPM macros: * `%{build_cc}` for the command name of the C compiler. diff --git a/macros b/macros index 427fa03..3e3c8e4 100644 --- a/macros +++ b/macros @@ -89,6 +89,13 @@ CC="${CC:-%{__cc}}" ; export CC ; \ CXX="${CXX:-%{__cxx}}" ; export CXX +# Automatically use set_build_flags macro for build, check, and +# install phases. +# Use "%undefine _auto_set_build_flags" to disable" +%_auto_set_build_flags 1 +%__spec_build_pre %{___build_pre} %{?_auto_set_build_flags:%{set_build_flags}} +%__spec_check_pre %{___build_pre} %{?_auto_set_build_flags:%{set_build_flags}} + # Internal-only. Do not use. Expand a variable and strip the flags # not suitable to extension builders. %__extension_strip_flags() %{lua: @@ -211,6 +218,7 @@ print(result) [ "$RPM_BUILD_ROOT" != "/" ] && rm -rf "${RPM_BUILD_ROOT}"\ mkdir -p "`dirname "$RPM_BUILD_ROOT"`"\ mkdir "$RPM_BUILD_ROOT"\ + %{?_auto_set_build_flags:%{set_build_flags}}\ %{nil} #--------------------------------------------------------------------- diff --git a/redhat-rpm-config.spec b/redhat-rpm-config.spec index a42b2a6..e6d2ea0 100644 --- a/redhat-rpm-config.spec +++ b/redhat-rpm-config.spec @@ -6,7 +6,7 @@ Summary: Red Hat specific rpm configuration files Name: redhat-rpm-config -Version: 207 +Version: 208 Release: 1%{?dist} # No version specified. License: GPL+ @@ -190,6 +190,9 @@ install -p -m 644 -t %{buildroot}%{_rpmluadir}/fedora/srpm forge.lua %doc buildflags.md %changelog +* Tue Jan 04 2022 Tom Stellard - 208-1 +- Call %%set_build_flags before %%build, %%check, and %%install stages + * Tue Dec 14 2021 Tom Stellard - 207-1 - Add -Wl,--build-id=sha1 to the default LDFLAGS diff --git a/tests/auto-set-build-flags/Makefile b/tests/auto-set-build-flags/Makefile new file mode 100644 index 0000000..ddf3d76 --- /dev/null +++ b/tests/auto-set-build-flags/Makefile @@ -0,0 +1,11 @@ +.phony: all + +all: hello-c hello-cpp + +clean: + rm -Rf *.o hello-c hello-cpp + +hello-c: main-c.o hello-c.o + +hello-cpp: main-cpp.o hello-cpp.o + $(CXX) $(LDFLAGS) -o $@ $^ diff --git a/tests/auto-set-build-flags/hello-c.c b/tests/auto-set-build-flags/hello-c.c new file mode 100644 index 0000000..fbc4d72 --- /dev/null +++ b/tests/auto-set-build-flags/hello-c.c @@ -0,0 +1,5 @@ +#include + +void hello(void) { + printf("Hello, World!"); +} diff --git a/tests/auto-set-build-flags/hello-cpp.cpp b/tests/auto-set-build-flags/hello-cpp.cpp new file mode 100644 index 0000000..e925107 --- /dev/null +++ b/tests/auto-set-build-flags/hello-cpp.cpp @@ -0,0 +1,5 @@ +#include + +void hello(void) { + std::cout << "Hello, World!\n"; +} diff --git a/tests/auto-set-build-flags/main-c.c b/tests/auto-set-build-flags/main-c.c new file mode 100644 index 0000000..543bba3 --- /dev/null +++ b/tests/auto-set-build-flags/main-c.c @@ -0,0 +1,5 @@ +void hello(void); + +int main(int argc, char **argv) { + hello(); +} diff --git a/tests/auto-set-build-flags/main-cpp.cpp b/tests/auto-set-build-flags/main-cpp.cpp new file mode 100644 index 0000000..543bba3 --- /dev/null +++ b/tests/auto-set-build-flags/main-cpp.cpp @@ -0,0 +1,5 @@ +void hello(void); + +int main(int argc, char **argv) { + hello(); +} diff --git a/tests/auto-set-build-flags/runtest.sh b/tests/auto-set-build-flags/runtest.sh new file mode 100755 index 0000000..8d4a8ed --- /dev/null +++ b/tests/auto-set-build-flags/runtest.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -ex + +dnf -y build-dep test.spec +rpmbuild --define '_sourcedir .' --define '_builddir .' -bi test.spec +rpmbuild --without auto_set_build_flags --define '_sourcedir .' --define '_builddir .' -bi test.spec diff --git a/tests/auto-set-build-flags/test.spec b/tests/auto-set-build-flags/test.spec new file mode 100644 index 0000000..1172eeb --- /dev/null +++ b/tests/auto-set-build-flags/test.spec @@ -0,0 +1,39 @@ +%bcond_without auto_set_build_flags + +%if %{without auto_set_build_flags} +%undefine _auto_set_build_flags +%endif + +Name: test +Version: 1 +Release: 1 +Summary: Test package for checking %%set_build_flag usage +License: MIT + +BuildRequires: gcc gcc-c++ make +BuildRequires: annobin-annocheck + +Source0: Makefile +Source1: main-c.c +Source2: hello-c.c +Source3: main-cpp.cpp +Source4: hello-cpp.cpp + +%global build_and_check \ + make \ + %{!?with_auto_set_build_flags:!} annocheck hello-c hello-cpp \ + make clean + +%description +Test package for checking %%set_build_flag usage + +%prep + +%build +%build_and_check + +%check +%build_and_check + +%install +%build_and_check diff --git a/tests/tests.yml b/tests/tests.yml index ad49de7..52d9cec 100644 --- a/tests/tests.yml +++ b/tests/tests.yml @@ -31,6 +31,11 @@ - gcc - gcc-c++ - annobin-annocheck + #auto-set-build-flags + - gcc + - gcc-c++ + - annobin-annocheck + - make # no-new-dependency - diffutils - grep @@ -44,3 +49,4 @@ - clang/fedora-flags - gcc-fedora-flags - no-new-dependency + - auto-set-build-flags