From 3652d5fe0d06c967bf3dbfc07ee5d6597cba663e Mon Sep 17 00:00:00 2001
From: patrick96
Date: Sun, 11 Jul 2021 14:11:58 +0200
Subject: [PATCH] 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.
---
CHANGELOG.md | 1 +
cmake/05-summary.cmake | 5 +--
cmake/cxx.cmake | 90 ++++++++++++++++++++++--------------------
common/ci/configure.sh | 2 +-
src/CMakeLists.txt | 8 +++-
src/settings.cpp.cmake | 4 +-
6 files changed, 59 insertions(+), 51 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7740fb6f..d2c6eb74 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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
whatever folder you invoked `cmake` from instead of in the root folder of the
repository.
+- The `POLYBAR_FLAGS` cmake variable can be used to pass extra C++ compiler flags.
### Added
- Option to always show urgent windows in i3 module when `pin-workspace` is active
diff --git a/cmake/05-summary.cmake b/cmake/05-summary.cmake
index 9c3504eb..c992d285 100644
--- a/cmake/05-summary.cmake
+++ b/cmake/05-summary.cmake
@@ -1,13 +1,12 @@
#
# Output build summary
#
-
message(STATUS " Build:")
message_colored(STATUS " Version: ${APP_VERSION}" "32;1")
message_colored(STATUS " Type: ${CMAKE_BUILD_TYPE}" "37;2")
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 " LD: ${CMAKE_LINKER} ${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_EXE_LINKER_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}} ${cxx_linker_flags_str}" "37;2")
endif()
if (BUILD_DOC)
message_colored(STATUS " sphinx-build: ${BIN_SPHINX} ${SPHINX_FLAGS}" "37;2")
diff --git a/cmake/cxx.cmake b/cmake/cxx.cmake
index b271e4b8..5ca83887 100644
--- a/cmake/cxx.cmake
+++ b/cmake/cxx.cmake
@@ -21,47 +21,45 @@ set(CMAKE_CXX_EXTENSIONS OFF)
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
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)
if (HAS_SUGGEST_OVERRIDE)
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wsuggest-override")
+ list(APPEND cxx_base -Wsuggest-override)
endif()
unset(HAS_SUGGEST_OVERRIDE)
if (CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
# 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
- 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
- set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -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")
+ list(APPEND cxx_linker_base -L/usr/local/lib)
endif()
# 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")
message_colored(FATAL_ERROR "Compiler not supported (Requires clang-3.4+ or gcc-5.1+)" 31)
else()
message_colored(STATUS "Using supported compiler ${CMAKE_CXX_COMPILER_ID}-${CMAKE_CXX_COMPILER_VERSION}" 32)
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")
message_colored(FATAL_ERROR "Compiler not supported (Requires clang-3.4+ or gcc-5.1+)" 31)
else()
@@ -74,29 +72,35 @@ endif()
# Set compiler and linker flags for preferred C++ library
if(CXXLIB_CLANG)
message_colored(STATUS "Linking against libc++" 32)
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
- set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lc++ -lc++abi")
+ list(APPEND cxx_base -stdlib=libc++)
+ list(APPEND cxx_linker_base -lc++ -lc++abi)
elseif(CXXLIB_GCC)
message_colored(STATUS "Linking against libstdc++" 32)
- set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lstdc++")
+ list(APPEND cxx_linker_base -lstdc++)
endif()
-# Custom build type ; SANITIZE
-SET(CMAKE_CXX_FLAGS_SANITIZE "-O0 -g -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls"
- CACHE STRING "Flags used by the C++ compiler during sanitize builds." FORCE)
-SET(CMAKE_EXE_LINKER_FLAGS_SANITIZE ""
- CACHE STRING "Flags used for linking binaries during sanitize builds." FORCE)
-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', inherits the debug flags
+list(APPEND cxx_coverage ${cxx_debug} ${cxx_coverage})
+SET(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS_COVERAGE}")
+SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${CMAKE_EXE_LINKER_FLAGS_COVERAGE}")
+SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${CMAKE_SHARED_LINKER_FLAGS_COVERAGE}")
-# Custom build type ; Coverage
-SET(CMAKE_CXX_FLAGS_COVERAGE
- "${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS_COVERAGE} --coverage")
-SET(CMAKE_EXE_LINKER_FLAGS_COVERAGE
- "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${CMAKE_EXE_LINKER_FLAGS_COVERAGE}")
-SET(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
- "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${CMAKE_SHARED_LINKER_FLAGS_COVERAGE}")
+list(APPEND cxx_flags ${cxx_base})
+list(APPEND cxx_linker_flags ${cxx_linker_base})
+
+if (CMAKE_BUILD_TYPE_UPPER STREQUAL "DEBUG")
+ list(APPEND cxx_flags ${cxx_debug})
+elseif (CMAKE_BUILD_TYPE_UPPER STREQUAL "MINSIZEREL")
+ 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}")
diff --git a/common/ci/configure.sh b/common/ci/configure.sh
index 0c6d3374..9700ac10 100755
--- a/common/ci/configure.sh
+++ b/common/ci/configure.sh
@@ -28,7 +28,7 @@ fi
cmake \
-DCMAKE_CXX_COMPILER="${CXX}" \
- -DCMAKE_CXX_FLAGS="${CXXFLAGS} -Werror" \
+ -DPOLYBAR_FLAGS="${CXXFLAGS} -Werror" \
-DCMAKE_BUILD_TYPE="${BUILD_TYPE}" \
-DBUILD_TESTS:BOOL="${BUILD_TESTS:-OFF}" \
-DBUILD_DOC:BOOL=OFF \
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 411b3ed2..d09d8ba7 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -205,7 +205,8 @@ if(BUILD_LIBPOLY)
target_link_libraries(poly PUBLIC LibInotify::LibInotify)
endif()
- target_compile_options(poly PUBLIC $<$:$<$:-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)
# }}}
@@ -213,6 +214,8 @@ if(BUILD_LIBPOLY)
if(BUILD_POLYBAR)
add_executable(polybar main.cpp)
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)
install(TARGETS polybar
@@ -230,7 +233,8 @@ if(BUILD_POLYBAR_MSG)
utils/file.cpp
utils/string.cpp)
target_include_directories(polybar-msg PRIVATE ${includes_dir})
- target_compile_options(polybar-msg PUBLIC $<$:$<$:-flto>>)
+ target_compile_options(polybar-msg PUBLIC ${cxx_flags})
+ target_link_options(polybar-msg PUBLIC ${cxx_linker_flags})
install(TARGETS polybar-msg
DESTINATION ${CMAKE_INSTALL_BINDIR}
diff --git a/src/settings.cpp.cmake b/src/settings.cpp.cmake
index eadf3bb0..5b65578c 100644
--- a/src/settings.cpp.cmake
+++ b/src/settings.cpp.cmake
@@ -53,8 +53,8 @@ void print_build_info(bool extended) {
printf("\n");
printf("Build type: @CMAKE_BUILD_TYPE@\n");
printf("Compiler: @CMAKE_CXX_COMPILER@\n");
- printf("Compiler flags: @CMAKE_CXX_FLAGS@ ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}\n");
- printf("Linker flags: @CMAKE_EXE_LINKER_FLAGS@ ${CMAKE_EXE_LINKER_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}} @cxx_linker_flags_str@\n");
}
}
// clang-format on