Skip to content

Commit

Permalink
cmake: Add a custom build type with the Fedora rpm compilation options.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
linuxdude42 committed Dec 30, 2024
1 parent 09b0acb commit f2b1aa4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.CMake.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
| Release | -O3 -DNDEBUG |
| RelWithDebInfo | -O2 -g -DNDEBUG |
| MinSizeRel | -Os -DNDEBUG |
| Fedora | <All Fedora build system options> |

If no `CMAKE_BUILD_TYPE` value is supplied, the value of `Debug`
will be used.
Expand Down
5 changes: 5 additions & 0 deletions cmake/SetCompilerOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions cmake/SetCompilerOptionsFedora.cmake
Original file line number Diff line number Diff line change
@@ -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}")

0 comments on commit f2b1aa4

Please sign in to comment.