From eac65cdabbc4e15a405a23909edeec923653dacf Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 25 Oct 2016 20:14:44 +0200 Subject: [PATCH] fix(cmake): Make specific c++lib linking optional - Do not enforce linking against libc++ - Fix various linter warnings --- CMakeLists.txt | 52 ++++++++++++++++++++--------- include/components/builder.hpp | 2 +- include/components/command_line.hpp | 8 ++--- include/components/controller.hpp | 1 - include/components/logger.hpp | 8 +++-- include/components/types.hpp | 5 ++- include/components/x11/color.hpp | 13 +++----- include/components/x11/draw.hpp | 2 +- include/utils/inotify.hpp | 2 +- include/utils/scope.hpp | 2 +- include/utils/string.hpp | 2 +- 11 files changed, 55 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c77c8653..57985c0a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,47 +4,60 @@ cmake_minimum_required(VERSION 3.1 FATAL_ERROR) project(lemonbuddy CXX) +option(CXXLIB_CLANG "Link against libc++" OFF) +option(CXXLIB_GCC "Link against stdlibc++" OFF) + # Include local cmake modules set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake/modules) +# Export compile commands used for custom targets +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + # Load cmake utility functions include(cmake/utils.cmake) -# Warn if using an unsupported compiler -if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL Clang) - message_colored(WARNING "Unsupported compiler!" 31) -endif() - # Set default build type if not specified if(NOT CMAKE_BUILD_TYPE) message_colored(STATUS "No build type specified; using Release" 33) set(CMAKE_BUILD_TYPE "Release") endif() -# Export compile commands used for custom targets -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - # Generic compiler flags set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic") -#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic-errors") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror") # Debug specific compiler flags set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG") -set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pedantic-errors") -set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Werror") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g2") +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pedantic") +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -pedantic-errors") # Release specific compiler flags -set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2") +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3") -# Generic linker flags -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lc++ -lc++abi") +# Compiler specific flags +if("${CMAKE_CXX_COMPILER_ID}" STREQUAL Clang) + message_colored(STATUS "Using supported compiler ${CMAKE_CXX_COMPILER_ID}" 32) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL GNU) + message_colored(STATUS "Using supported compiler ${CMAKE_CXX_COMPILER_ID}" 32) +else() + message_colored(WARNING "Using unsupported compiler ${CMAKE_CXX_COMPILER_ID} !" 31) +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") +elseif(CXXLIB_GCC) + message_colored(STATUS "Linking against libstdc++" 32) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lstdc++") +else() + message_colored(STATUS "No preferred c++lib specified... linking against system default" 33) +endif() # Project settings {{{ @@ -123,6 +136,13 @@ elseif(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel") elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo") message(STATUS " + relwithdebinfo flags:: ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}") endif() +if(CXXLIB_CLANG) + message(STATUS "Linking C++ library: libc++") +elseif(CXXLIB_GCC) + message(STATUS "Linking C++ library: libstdc++") +else() + message(STATUS "Linking C++ library: system default") +endif() message(STATUS "---------------------------") message(STATUS " Build testsuite ${BUILD_TESTS}") message(STATUS " Enable ccache support ${ENABLE_CCACHE}") diff --git a/include/components/builder.hpp b/include/components/builder.hpp index 5cd8c89c..f1cb92b1 100644 --- a/include/components/builder.hpp +++ b/include/components/builder.hpp @@ -460,4 +460,4 @@ class builder { int m_fontindex = 1; }; -LEMONBUDDY_NS_END; +LEMONBUDDY_NS_END diff --git a/include/components/command_line.hpp b/include/components/command_line.hpp index 8c68d815..10d99ed0 100644 --- a/include/components/command_line.hpp +++ b/include/components/command_line.hpp @@ -33,11 +33,7 @@ namespace command_line { */ explicit option( string flag, string flag_long, string desc, string token = "", const choices c = {}) - : flag(flag) - , flag_long(flag_long) - , desc(desc) - , token(token) - , values(c) {} + : flag(flag), flag_long(flag_long), desc(desc), token(token), values(c) {} }; // }}} @@ -101,7 +97,7 @@ namespace command_line { } for (auto& opt : m_opts) { - int pad = maxlen - opt.flag_long.length() - opt.token.length(); + size_t pad = maxlen - opt.flag_long.length() - opt.token.length(); std::cout << " " << opt.flag << ", " << opt.flag_long; diff --git a/include/components/controller.hpp b/include/components/controller.hpp index fa0467bd..c28902ee 100644 --- a/include/components/controller.hpp +++ b/include/components/controller.hpp @@ -563,7 +563,6 @@ namespace { di::injector configure_controller(inotify_watch_t& confwatch) { // clang-format off return di::make_injector( - di::bind().to(), di::bind<>().to(confwatch), configure_connection(), configure_logger(), diff --git a/include/components/logger.hpp b/include/components/logger.hpp index b9daf0f8..5714fe35 100644 --- a/include/components/logger.hpp +++ b/include/components/logger.hpp @@ -128,14 +128,14 @@ class logger { template decltype(auto) convert(T&& arg) const { return forward(arg); - }; + } /** * Convert string to const char* */ const char* convert(string arg) const { return arg.c_str(); - }; + } /** * Write the log message to the output channel @@ -150,10 +150,14 @@ class logger { auto suffix = m_suffixes.find(level)->second; // silence the compiler +#if defined(__clang__) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-security" +#endif dprintf(m_fd, (prefix + format + suffix + "\n").c_str(), convert(values)...); +#if defined(__clang__) #pragma clang diagnostic pop +#endif } private: diff --git a/include/components/types.hpp b/include/components/types.hpp index 4b2f44dc..69a8eed4 100644 --- a/include/components/types.hpp +++ b/include/components/types.hpp @@ -56,7 +56,7 @@ struct bar_settings { }; snprintf(buffer, sizeof(buffer), "%dx%d+%d+%d", width, height, x, y); return string{*buffer}; - }; + } }; struct tray_settings { @@ -105,7 +105,6 @@ struct action_block { #endif }; -struct wmsettings_bspwm { -}; +struct wmsettings_bspwm {}; LEMONBUDDY_NS_END diff --git a/include/components/x11/color.hpp b/include/components/x11/color.hpp index e88d788f..6ec1f842 100644 --- a/include/components/x11/color.hpp +++ b/include/components/x11/color.hpp @@ -7,21 +7,16 @@ LEMONBUDDY_NS -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu-anonymous-struct" - union rgba { struct { uint8_t r; uint8_t g; uint8_t b; uint8_t a; - }; + } _; uint32_t v; }; -#pragma clang diagnostic pop - static map g_colorstore; class color { @@ -29,9 +24,9 @@ class color { explicit color(string hex) : m_hex(string_util::upper(hex)) { m_rgba.v = static_cast(strtoul(&hex[1], nullptr, 16)); // premultiply alpha - m_rgba.r = m_rgba.r * m_rgba.a / 255; - m_rgba.g = m_rgba.g * m_rgba.a / 255; - m_rgba.b = m_rgba.b * m_rgba.a / 255; + m_rgba._.r = m_rgba._.r * m_rgba._.a / 255; + m_rgba._.g = m_rgba._.g * m_rgba._.a / 255; + m_rgba._.b = m_rgba._.b * m_rgba._.a / 255; } explicit color(uint32_t v) { diff --git a/include/components/x11/draw.hpp b/include/components/x11/draw.hpp index 01f3de17..106d169c 100644 --- a/include/components/x11/draw.hpp +++ b/include/components/x11/draw.hpp @@ -25,7 +25,7 @@ namespace draw_util { * Code: http://wmdia.sourceforge.net/ */ auto xcb_poly_text_16_patched(xcb_connection_t* conn, xcb_drawable_t d, xcb_gcontext_t gc, - int16_t x, int16_t y, uint32_t len, uint16_t* str) { + int16_t x, int16_t y, uint8_t len, uint16_t* str) { static const xcb_protocol_request_t xcb_req = { 5, // count 0, // ext diff --git a/include/utils/inotify.hpp b/include/utils/inotify.hpp index e4b1fb74..73d6f487 100644 --- a/include/utils/inotify.hpp +++ b/include/utils/inotify.hpp @@ -120,7 +120,7 @@ namespace inotify_util { inline auto make_watch(string path) { di::injector injector = di::make_injector(di::bind<>().to(path)); return injector.create(); - }; + } } LEMONBUDDY_NS_END diff --git a/include/utils/scope.hpp b/include/utils/scope.hpp index bccb4cc6..62b16e7b 100644 --- a/include/utils/scope.hpp +++ b/include/utils/scope.hpp @@ -35,7 +35,7 @@ namespace scope_util { template , typename... Args> decltype(auto) make_exit_handler(Fn&& fn, Args&&... args) { return make_unique>(forward(fn), forward(args)...); - }; + } } LEMONBUDDY_NS_END diff --git a/include/utils/string.hpp b/include/utils/string.hpp index 49409b95..05b0b85b 100644 --- a/include/utils/string.hpp +++ b/include/utils/string.hpp @@ -59,7 +59,7 @@ namespace string_util { for (size_t i = 0; i < haystack.length(); i++) { if (haystack.compare(i, needle.length(), needle) == 0) { replaced += replacement; - i += needle.length()-1; + i += needle.length() - 1; } else { replaced += haystack[i]; }