cmake: Stop using CMAKE_* variables for flags

It is not recommended anyway and the -Werror flag set in CI was getting
set when compiling gtest, breaking our build.
This commit is contained in:
patrick96 2021-07-11 14:11:58 +02:00 committed by Patrick Ziegler
parent 55509efa1d
commit 3652d5fe0d
6 changed files with 59 additions and 51 deletions

View File

@ -49,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The sample config file is now placed in the `generated-sources` folder inside - The sample config file is now placed in the `generated-sources` folder inside
whatever folder you invoked `cmake` from instead of in the root folder of the whatever folder you invoked `cmake` from instead of in the root folder of the
repository. repository.
- The `POLYBAR_FLAGS` cmake variable can be used to pass extra C++ compiler flags.
### Added ### Added
- Option to always show urgent windows in i3 module when `pin-workspace` is active - Option to always show urgent windows in i3 module when `pin-workspace` is active

View File

@ -1,13 +1,12 @@
# #
# Output build summary # Output build summary
# #
message(STATUS " Build:") message(STATUS " Build:")
message_colored(STATUS " Version: ${APP_VERSION}" "32;1") message_colored(STATUS " Version: ${APP_VERSION}" "32;1")
message_colored(STATUS " Type: ${CMAKE_BUILD_TYPE}" "37;2") message_colored(STATUS " Type: ${CMAKE_BUILD_TYPE}" "37;2")
if (HAS_CXX_COMPILATION) if (HAS_CXX_COMPILATION)
message_colored(STATUS " CXX: ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}" "37;2") message_colored(STATUS " CXX: ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPER}} ${cxx_flags_str}" "37;2")
message_colored(STATUS " LD: ${CMAKE_LINKER} ${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}" "37;2") message_colored(STATUS " LD: ${CMAKE_LINKER} ${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}} ${cxx_linker_flags_str}" "37;2")
endif() endif()
if (BUILD_DOC) if (BUILD_DOC)
message_colored(STATUS " sphinx-build: ${BIN_SPHINX} ${SPHINX_FLAGS}" "37;2") message_colored(STATUS " sphinx-build: ${BIN_SPHINX} ${SPHINX_FLAGS}" "37;2")

View File

@ -21,47 +21,45 @@ set(CMAKE_CXX_EXTENSIONS OFF)
set(THREADS_PREFER_PTHREAD_FLAG ON) set(THREADS_PREFER_PTHREAD_FLAG ON)
set(POLYBAR_FLAGS "" CACHE STRING "C++ compiler flags used for compiling polybar")
list(APPEND cxx_base -Wall -Wextra -Wpedantic)
list(APPEND cxx_debug -DDEBUG -g2)
list(APPEND cxx_minsizerel "")
list(APPEND cxx_sanitize -O0 -g -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls)
list(APPEND cxx_coverage --coverage)
list(APPEND cxx_linker_base "")
list(APPEND cxx_linker_minsizerel "")
# Compiler flags # Compiler flags
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wpedantic")
check_cxx_compiler_flag("-Wsuggest-override" HAS_SUGGEST_OVERRIDE) check_cxx_compiler_flag("-Wsuggest-override" HAS_SUGGEST_OVERRIDE)
if (HAS_SUGGEST_OVERRIDE) if (HAS_SUGGEST_OVERRIDE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsuggest-override") list(APPEND cxx_base -Wsuggest-override)
endif() endif()
unset(HAS_SUGGEST_OVERRIDE) unset(HAS_SUGGEST_OVERRIDE)
if (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD") if (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
# Need dprintf() for FreeBSD 11.1 and older # Need dprintf() for FreeBSD 11.1 and older
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_WITH_DPRINTF")
# libinotify uses c99 extension, so suppress this error # libinotify uses c99 extension, so suppress this error
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-c99-extensions") list(APPEND cxx_base -D_WITH_DPRINTF -Wno-c99-extensions)
# Ensures that libraries from dependencies in LOCALBASE are used # Ensures that libraries from dependencies in LOCALBASE are used
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib") list(APPEND cxx_linker_base -L/usr/local/lib)
endif()
if(${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=parentheses-equality")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-zero-length-array")
endif()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g2")
if(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} -fdata-sections -ffunction-sections")
set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} -Wl,--gc-sections,--icf=safe")
endif() endif()
# Check compiler # Check compiler
if(${CMAKE_CXX_COMPILER_ID} STREQUAL Clang) if(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
list(APPEND cxx_base -Wno-error=parentheses-equality -Wno-zero-length-array)
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.4.0") if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.4.0")
message_colored(FATAL_ERROR "Compiler not supported (Requires clang-3.4+ or gcc-5.1+)" 31) message_colored(FATAL_ERROR "Compiler not supported (Requires clang-3.4+ or gcc-5.1+)" 31)
else() else()
message_colored(STATUS "Using supported compiler ${CMAKE_CXX_COMPILER_ID}-${CMAKE_CXX_COMPILER_VERSION}" 32) message_colored(STATUS "Using supported compiler ${CMAKE_CXX_COMPILER_ID}-${CMAKE_CXX_COMPILER_VERSION}" 32)
endif() endif()
elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL GNU) elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
list(APPEND cxx_minsizerel -fdata-sections -ffunction-sections -flto)
list(APPEND cxx_linker_minsizerel -Wl,--gc-sections)
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.1.0") if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.1.0")
message_colored(FATAL_ERROR "Compiler not supported (Requires clang-3.4+ or gcc-5.1+)" 31) message_colored(FATAL_ERROR "Compiler not supported (Requires clang-3.4+ or gcc-5.1+)" 31)
else() else()
@ -74,29 +72,35 @@ endif()
# Set compiler and linker flags for preferred C++ library # Set compiler and linker flags for preferred C++ library
if(CXXLIB_CLANG) if(CXXLIB_CLANG)
message_colored(STATUS "Linking against libc++" 32) message_colored(STATUS "Linking against libc++" 32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") list(APPEND cxx_base -stdlib=libc++)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lc++ -lc++abi") list(APPEND cxx_linker_base -lc++ -lc++abi)
elseif(CXXLIB_GCC) elseif(CXXLIB_GCC)
message_colored(STATUS "Linking against libstdc++" 32) message_colored(STATUS "Linking against libstdc++" 32)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lstdc++") list(APPEND cxx_linker_base -lstdc++)
endif() endif()
# Custom build type ; SANITIZE # Custom build type 'Coverage', inherits the debug flags
SET(CMAKE_CXX_FLAGS_SANITIZE "-O0 -g -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls" list(APPEND cxx_coverage ${cxx_debug} ${cxx_coverage})
CACHE STRING "Flags used by the C++ compiler during sanitize builds." FORCE) SET(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS_COVERAGE}")
SET(CMAKE_EXE_LINKER_FLAGS_SANITIZE "" SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${CMAKE_EXE_LINKER_FLAGS_COVERAGE}")
CACHE STRING "Flags used for linking binaries during sanitize builds." FORCE) SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${CMAKE_SHARED_LINKER_FLAGS_COVERAGE}")
SET(CMAKE_SHARED_LINKER_FLAGS_SANITIZE ""
CACHE STRING "Flags used by the shared libraries linker during sanitize builds." FORCE)
MARK_AS_ADVANCED(
CMAKE_CXX_FLAGS_SANITIZE
CMAKE_EXE_LINKER_FLAGS_SANITIZE
CMAKE_SHARED_LINKER_FLAGS_SANITIZE)
# Custom build type ; Coverage list(APPEND cxx_flags ${cxx_base})
SET(CMAKE_CXX_FLAGS_COVERAGE list(APPEND cxx_linker_flags ${cxx_linker_base})
"${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS_COVERAGE} --coverage")
SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE if (CMAKE_BUILD_TYPE_UPPER STREQUAL "DEBUG")
"${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${CMAKE_EXE_LINKER_FLAGS_COVERAGE}") list(APPEND cxx_flags ${cxx_debug})
SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE elseif (CMAKE_BUILD_TYPE_UPPER STREQUAL "MINSIZEREL")
"${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${CMAKE_SHARED_LINKER_FLAGS_COVERAGE}") list(APPEND cxx_flags ${cxx_minsizerel})
list(APPEND cxx_linker_flags ${cxx_linker_minsizerel})
elseif (CMAKE_BUILD_TYPE_UPPER STREQUAL "SANITIZE")
list(APPEND cxx_flags ${cxx_sanitize})
elseif (CMAKE_BUILD_TYPE_UPPER STREQUAL "COVERAGE")
list(APPEND cxx_flags ${cxx_coverage})
endif()
string(REPLACE " " ";" polybar_flags_list "${POLYBAR_FLAGS}")
list(APPEND cxx_flags ${polybar_flags_list})
string(REPLACE ";" " " cxx_flags_str "${cxx_flags}")
string(REPLACE ";" " " cxx_linker_flags_str "${cxx_linker_flags}")

View File

@ -28,7 +28,7 @@ fi
cmake \ cmake \
-DCMAKE_CXX_COMPILER="${CXX}" \ -DCMAKE_CXX_COMPILER="${CXX}" \
-DCMAKE_CXX_FLAGS="${CXXFLAGS} -Werror" \ -DPOLYBAR_FLAGS="${CXXFLAGS} -Werror" \
-DCMAKE_BUILD_TYPE="${BUILD_TYPE}" \ -DCMAKE_BUILD_TYPE="${BUILD_TYPE}" \
-DBUILD_TESTS:BOOL="${BUILD_TESTS:-OFF}" \ -DBUILD_TESTS:BOOL="${BUILD_TESTS:-OFF}" \
-DBUILD_DOC:BOOL=OFF \ -DBUILD_DOC:BOOL=OFF \

View File

@ -205,7 +205,8 @@ if(BUILD_LIBPOLY)
target_link_libraries(poly PUBLIC LibInotify::LibInotify) target_link_libraries(poly PUBLIC LibInotify::LibInotify)
endif() endif()
target_compile_options(poly PUBLIC $<$<CXX_COMPILER_ID:GNU>:$<$<CONFIG:MinSizeRel>:-flto>>) target_compile_options(poly PUBLIC ${cxx_flags})
target_link_options(poly PUBLIC ${cxx_linker_flags})
set_target_properties(poly PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/libs) set_target_properties(poly PROPERTIES ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/libs)
# }}} # }}}
@ -213,6 +214,8 @@ if(BUILD_LIBPOLY)
if(BUILD_POLYBAR) if(BUILD_POLYBAR)
add_executable(polybar main.cpp) add_executable(polybar main.cpp)
target_link_libraries(polybar poly) target_link_libraries(polybar poly)
target_compile_options(polybar PUBLIC ${cxx_flags})
target_link_options(polybar PUBLIC ${cxx_linker_flags})
set_target_properties(poly PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) set_target_properties(poly PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
install(TARGETS polybar install(TARGETS polybar
@ -230,7 +233,8 @@ if(BUILD_POLYBAR_MSG)
utils/file.cpp utils/file.cpp
utils/string.cpp) utils/string.cpp)
target_include_directories(polybar-msg PRIVATE ${includes_dir}) target_include_directories(polybar-msg PRIVATE ${includes_dir})
target_compile_options(polybar-msg PUBLIC $<$<CXX_COMPILER_ID:GNU>:$<$<CONFIG:MinSizeRel>:-flto>>) target_compile_options(polybar-msg PUBLIC ${cxx_flags})
target_link_options(polybar-msg PUBLIC ${cxx_linker_flags})
install(TARGETS polybar-msg install(TARGETS polybar-msg
DESTINATION ${CMAKE_INSTALL_BINDIR} DESTINATION ${CMAKE_INSTALL_BINDIR}

View File

@ -53,8 +53,8 @@ void print_build_info(bool extended) {
printf("\n"); printf("\n");
printf("Build type: @CMAKE_BUILD_TYPE@\n"); printf("Build type: @CMAKE_BUILD_TYPE@\n");
printf("Compiler: @CMAKE_CXX_COMPILER@\n"); printf("Compiler: @CMAKE_CXX_COMPILER@\n");
printf("Compiler flags: @CMAKE_CXX_FLAGS@ ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}\n"); printf("Compiler flags: @CMAKE_CXX_FLAGS@ ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPER}} @cxx_flags_str@\n");
printf("Linker flags: @CMAKE_EXE_LINKER_FLAGS@ ${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}\n"); printf("Linker flags: @CMAKE_EXE_LINKER_FLAGS@ ${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}} @cxx_linker_flags_str@\n");
} }
} }
// clang-format on // clang-format on