From 7692bbaf45de80df5a3b6564dfbcb882f545a456 Mon Sep 17 00:00:00 2001 From: Florian Weimer Date: Wed, 5 Jul 2023 12:46:49 +0200 Subject: [PATCH] Filter out C, C++ build flags from Fortran build flags (#2177253) And pave the way for future front-end-specific compiler flags. --- buildflags.md | 14 +++++++++++--- macros | 29 +++++++++++++++++++++++++---- redhat-rpm-config.spec | 5 ++++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/buildflags.md b/buildflags.md index d20c918..c89f124 100644 --- a/buildflags.md +++ b/buildflags.md @@ -36,9 +36,7 @@ Individual build flags are also available through RPM macros: * `%{build_cxx}` for the command name of the C++ compiler. * `%{build_cpp}` for the command name of the C-compatible preprocessor. * `%{build_cflags}` for the C compiler flags (also known as the - `CFLAGS` variable). Also historically available as `%{optflags}`. - Furthermore, at the start of the `%build` section, the environment - variable `RPM_OPT_FLAGS` is set to this value. + `CFLAGS` variable). * `%{build_cxxflags}` for the C++ compiler flags (usually assigned to the `CXXFLAGS` shell variable). * `%{build_fflags}` for `FFLAGS` (the Fortran compiler flags, also @@ -50,6 +48,13 @@ Individual build flags are also available through RPM macros: driver. At the start of the `%build` section, the environment variable `RPM_LD_FLAGS` is set to this value. +The C and C++ compiler flags are historically available as the +`%{optflags}` macro. These flags may not contain flags that work with +certain languagues or compiler front ends, so the language-specific +`%build_*` are more precise. At the start of the `%build` section, +the environment variable `RPM_OPT_FLAGS` is set to the `%{optflags}` +value; similar limitations apply. + The variable `LT_SYS_LIBRARY_PATH` is defined here to prevent the `libtool` script (v2.4.6+) from hardcoding `%_libdir` into the binaries' `RPATH`. @@ -402,6 +407,9 @@ The general (architecture-independent) build flags are: compilation performance. (This does not affect code generation.) * `-Wall`: Turn on various GCC warnings. See the [GCC manual](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wall). +* `-Wno-complain-wrong-lang`: Do not warn about front end mismatches + (e.g, using `-Werror=format-security` with Fortran). Only included + in `%optflags`, and not the front-end-specific `%build_*` macros. * `-Werror=format-security`: Turn on format string warnings and treat them as errors. See the [GCC manual](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wformat-security). diff --git a/macros b/macros index 23e7d19..39aca1b 100644 --- a/macros +++ b/macros @@ -57,14 +57,14 @@ # C compiler flags. This is traditionally called CFLAGS in makefiles. # Historically also available as %%{optflags}, and %%build sets the # environment variable RPM_OPT_FLAGS to this value. -%build_cflags %{optflags} %{?_distro_extra_cflags} +%build_cflags %{__build_flags_lang_c} %{?_distro_extra_cflags} # C++ compiler flags. This is traditionally called CXXFLAGS in makefiles. -%build_cxxflags %{optflags} %{?_distro_extra_cxxflags} +%build_cxxflags %{__build_flags_lang_cxx} %{?_distro_extra_cxxflags} # Fortran compiler flags. Makefiles use both FFLAGS and FCFLAGS as # the corresponding variable names. -%build_fflags %{optflags} -I%{_fmoddir} %{?_distro_extra_fflags} +%build_fflags %{__build_flags_common} -I%{_fmoddir} %{?_distro_extra_fflags} # Vala compiler flags. This is used to set VALAFLAGS. %build_valaflags -g @@ -308,6 +308,12 @@ print(result) # Use Zstandard compression for binary payloads %_binary_payload w19.zstdio +#============================================================================== +# --- Compiler flags control. +# +# Please consult buildflags.md for parts that can be configured +# from RPM spec files. + %_hardening_gcc_cflags -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 %_hardening_clang_cflags --config /usr/lib/rpm/redhat/redhat-hardened-clang.cfg %_hardening_cflags %{expand:%%{_hardening_%{toolchain}_cflags}} -fstack-protector-strong @@ -383,7 +389,7 @@ print(result) %_build_id_flags -Wl,--build-id=sha1 %_general_options -O2 %{?_lto_cflags} -fexceptions -g -grecord-gcc-switches -pipe -%_warning_options -Wall -Werror=format-security +%_warning_options -Wall%[%__build_for_lang_any ? " -Wno-complain-wrong-lang" : ""]%[%__build_for_lang_c + %__build_for_lang_cxx ? " -Werror=format-security" : ""] %_preprocessor_defines %{_fortify_level_flags} -Wp,-D_GLIBCXX_ASSERTIONS # Common variables are no longer generated by default by gcc and clang @@ -392,6 +398,21 @@ print(result) %__global_compiler_flags %{_general_options} %{_warning_options} %{_preprocessor_defines} %{_hardened_cflags} %{_annotation_cflags} %{_legacy_options} +# Internal macros. Do not use directly. These variables can be rebound +# to suppress certain frontend-specific compiler flags (or in the case +# of __build_for_lang_any, frontend-agnostic flags). Dynamic scoping +# and shadowing redefinitions are used for the __build_for_* variables +# to remain largely compatible with existing spec files that have +# hard-coded assumptions which macros assume which other macros. +# Note: Using short-circuit evaluation with || in __build_for_* conditionals +# may result in unused-macro warnings from RPM. +%__build_for_lang_c 1 +%__build_for_lang_cxx 1 +%__build_for_lang_any 1 +%__build_flags_common() %{expand:%define __build_for_lang_c 0}%{expand:%define __build_for_lang_cxx 0}%{expand:%define __build_for_lang_any 0}%{optflags} +%__build_flags_lang_c() %{expand:%define __build_for_lang_cxx 0}%{expand:%define __build_for_lang_any 0}%{optflags} +%__build_flags_lang_cxx() %{expand:%define __build_for_lang_c 0}%{expand:%define __build_for_lang_any 0}%{optflags} + # Automatically trim changelog entries after 2 years %_changelog_trimage %{expr:2*365*24*60*60} diff --git a/redhat-rpm-config.spec b/redhat-rpm-config.spec index 06b923c..02fe635 100644 --- a/redhat-rpm-config.spec +++ b/redhat-rpm-config.spec @@ -4,7 +4,7 @@ # 2) When making changes, increment the version (in baserelease) by 1. # rpmdev-bumpspec and other tools update the macro below, which is used # in Version: to get the desired effect. -%global baserelease 258 +%global baserelease 259 Summary: Red Hat specific rpm configuration files Name: redhat-rpm-config @@ -254,6 +254,9 @@ install -p -m 644 -t %{buildroot}%{_rpmluadir}/fedora/srpm forge.lua %doc buildflags.md %changelog +* Wed Jul 5 2023 Florian Weimer - 259-1 +- Filter out C, C++ build flags from Fortran build flags (#2177253) + * Wed Jul 5 2023 Florian Weimer - 258-1 - Enable PIC mode for assembler files (#2167430)