From f2b1aa4845108d26cfed14b55b1a5dd3948d388b Mon Sep 17 00:00:00 2001 From: David Hampton Date: Sun, 29 Dec 2024 00:57:17 -0500 Subject: [PATCH] cmake: Add a custom build type with the Fedora rpm compilation options. Configuring a cmake build with the -DCMAKE_BUILD_TYPE=Fedora argument will add all the compile/link arguments used by the Fedora build system to the already specified arguments, except for the one that adds rpm build notes to the metadata of a linked executable. This build type should allow for easy testing of whether a compile by the fedora build team will fail. --- README.CMake.md | 1 + cmake/SetCompilerOptions.cmake | 5 +++++ cmake/SetCompilerOptionsFedora.cmake | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 cmake/SetCompilerOptionsFedora.cmake diff --git a/README.CMake.md b/README.CMake.md index e9f44ec74ea..82f34c6de6b 100644 --- a/README.CMake.md +++ b/README.CMake.md @@ -201,6 +201,7 @@ | Release | -O3 -DNDEBUG | | RelWithDebInfo | -O2 -g -DNDEBUG | | MinSizeRel | -Os -DNDEBUG | + | Fedora | | If no `CMAKE_BUILD_TYPE` value is supplied, the value of `Debug` will be used. diff --git a/cmake/SetCompilerOptions.cmake b/cmake/SetCompilerOptions.cmake index 02b425b785c..81723705d7a 100644 --- a/cmake/SetCompilerOptions.cmake +++ b/cmake/SetCompilerOptions.cmake @@ -25,6 +25,11 @@ endif() # Always use position independent code. # set(CMAKE_POSITION_INDEPENDENT_CODE ON) +if(CMAKE_BUILD_TYPE MATCHES "([A-Za-z])(.*)") + string(TOUPPER ${CMAKE_MATCH_1} _INIT) + string(TOLOWER ${CMAKE_MATCH_2} _REST) + include(SetCompilerOptions${_INIT}${_REST} OPTIONAL) +endif() # # Always used flags diff --git a/cmake/SetCompilerOptionsFedora.cmake b/cmake/SetCompilerOptionsFedora.cmake new file mode 100644 index 00000000000..6194cda7465 --- /dev/null +++ b/cmake/SetCompilerOptionsFedora.cmake @@ -0,0 +1,21 @@ +# +# Copyright (C) 2024 David Hampton +# +# See the file LICENSE_FSF for licensing information. +# +if(NOT CMAKE_C_COMPILER_ID STREQUAL GNU) + message( + FATAL_ERROR "The Fedora build type requires the use of the gcc compiler.") +endif() + +set(CMAKE_C_FLAGS_FEDORA + "-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=3 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -march=x86-64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -mtls-dialect=gnu2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer" +) +set(CMAKE_CXX_FLAGS_FEDORA + "-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=3 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -march=x86-64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -mtls-dialect=gnu2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer" +) +set(CMAKE_EXE_LINKER_FLAGS_FEDORA + "-Wl,-z,relro -Wl,--as-needed -Wl,-z,pack-relative-relocs -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,--build-id=sha1" +) +set(CMAKE_MODULE_LINKER_FLAGS_FEDORA "${CMAKE_EXE_LINKER_FLAGS_FEDORA}") +set(CMAKE_SHARED_LINKER_FLAGS_FEDORA "${CMAKE_EXE_LINKER_FLAGS_FEDORA}")