From 232180257539f908831856058956dd36d5164fb8 Mon Sep 17 00:00:00 2001 From: tastytea Date: Mon, 16 Dec 2019 15:21:06 +0100 Subject: [PATCH] Rewrite root CMake recipe. --- CMakeLists.txt | 67 ++++++++++++++++++----------------------- cmake/debug_flags.cmake | 58 +++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 37 +++++++++++++++++++++++ 3 files changed, 125 insertions(+), 37 deletions(-) create mode 100644 cmake/debug_flags.cmake create mode 100644 src/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index f5bcebb..7d75ad2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,47 +1,40 @@ -cmake_minimum_required (VERSION 3.6) +# Support version 3.9 and above, but use policy settings up to 3.14. +# 3.9 is needed for project description. +cmake_minimum_required(VERSION 3.9...3.14) +# Ranges are supported from 3.12, set policy to current for < 3.12. +if(${CMAKE_VERSION} VERSION_LESS 3.12) + cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) +endif() + +# Global build options. +set(CMAKE_BUILD_TYPE "Release" CACHE STRING "The type of build.") +option(BUILD_SHARED_LIBS "Build shared libraries." YES) + project (mastorss - VERSION 0.9.0 - LANGUAGES CXX -) + VERSION 0.9.9999 + DESCRIPTION "Another RSS to Mastodon bot." + LANGUAGES CXX) -include(GNUInstallDirs) -find_package(CURL REQUIRED) -find_package(Boost REQUIRED COMPONENTS system filesystem) -find_package(PkgConfig REQUIRED) -pkg_check_modules(CURLPP REQUIRED curlpp) -pkg_check_modules(JSONCPP REQUIRED jsoncpp) +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") -set(CMAKE_CXX_STANDARD 14) +# Project build options. +option(WITH_MAN "Compile and install manpage." YES) +option(WITH_TESTS "Compile tests." NO) + +set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) -include_directories(${PROJECT_SOURCE_DIR}/src) -include_directories(${PROJECT_BINARY_DIR}) +include(debug_flags) -include_directories(${CURL_INCLUDE_DIR}) -include_directories(${CURLPP_INCLUDE_DIR}) -include_directories(${JSONCPP_INCLUDE_DIR}) -include_directories(${Boost_INCLUDE_DIRS}) +add_subdirectory(src) -link_directories(${CURL_LIBRARY_DIRS}) -link_directories(${CURLPP_LIBRARY_DIRS}) -link_directories(${JSONCPP_LIBRARY_DIRS}) -link_directories(${Boost_LIBRARY_DIRS}) +if(WITH_MAN) + add_subdirectory(man) +endif() -add_definitions(${Boost_DEFINITIONS}) +# if(WITH_TESTS) +# add_subdirectory(tests) +# endif() -# Write version in header -configure_file ( - "${PROJECT_SOURCE_DIR}/src/version.hpp.in" - "${PROJECT_BINARY_DIR}/version.hpp" -) - -file(GLOB sources src/*.cpp) -add_executable(mastorss ${sources}) -target_link_libraries(mastorss - mastodon-cpp ${CURLPP_LIBRARIES} - ${JSONCPP_LIBRARIES} ${Boost_LIBRARIES} stdc++fs) -install(TARGETS mastorss DESTINATION ${CMAKE_INSTALL_BINDIR}) - -install(FILES watchwords.json - DESTINATION ${CMAKE_INSTALL_DATADIR}/mastorss) +# include(cmake/packages.cmake) diff --git a/cmake/debug_flags.cmake b/cmake/debug_flags.cmake new file mode 100644 index 0000000..0009039 --- /dev/null +++ b/cmake/debug_flags.cmake @@ -0,0 +1,58 @@ +# Set compiler flags for Debug builds. +# Only has an effect on GCC/Clang >= 5.0. + +set(DEBUG_CXXFLAGS "") +if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang" + AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5") + list(APPEND DEBUG_CXXFLAGS + "-Wall" + "-Wextra" + "-Wpedantic" + "-Wuninitialized" + "-Wshadow" + "-Wnon-virtual-dtor" + "-Wconversion" + "-Wsign-conversion" + "-Wold-style-cast" + "-Wzero-as-null-pointer-constant" + "-Wmissing-declarations" + "-Wcast-align" + "-Wunused" + "-Woverloaded-virtual" + "-Wdouble-promotion" + "-Wformat=2" + "-ftrapv" + "-fsanitize=undefined" + "-g" + "-Og" + "-fno-omit-frame-pointer") + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU") + list(APPEND DEBUG_CXXFLAGS + "-Wlogical-op" + "-Wuseless-cast") + if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6") + list(APPEND DEBUG_CXXFLAGS + "-Wmisleading-indentation" + "-Wduplicated-cond" + "-Wnull-dereference") + if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "7") + list(APPEND DEBUG_CXXFLAGS + "-Wduplicated-branches") + endif() + endif() + endif() + add_compile_options("$<$:${DEBUG_CXXFLAGS}>") + + set(DEBUG_LDFLAGS + "-fsanitize=undefined") + # add_link_options was introduced in version 3.13. + if(${CMAKE_VERSION} VERSION_LESS 3.13) + set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${DEBUG_LDFLAGS}") + else() + add_link_options("$<$:${DEBUG_LDFLAGS}>") + endif() +else() + message(STATUS + "No additional compiler flags were set, " + "because your compiler was not anticipated.") +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..9d605e6 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,37 @@ +include(GNUInstallDirs) +find_package(CURL REQUIRED) +find_package(Boost REQUIRED COMPONENTS system filesystem) +find_package(PkgConfig REQUIRED) +pkg_check_modules(CURLPP REQUIRED curlpp) +pkg_check_modules(JSONCPP REQUIRED jsoncpp) + +include_directories(${PROJECT_SOURCE_DIR}/src) +include_directories(${PROJECT_BINARY_DIR}) + +include_directories(${CURL_INCLUDE_DIR}) +include_directories(${CURLPP_INCLUDE_DIR}) +include_directories(${JSONCPP_INCLUDE_DIR}) +include_directories(${Boost_INCLUDE_DIRS}) + +link_directories(${CURL_LIBRARY_DIRS}) +link_directories(${CURLPP_LIBRARY_DIRS}) +link_directories(${JSONCPP_LIBRARY_DIRS}) +link_directories(${Boost_LIBRARY_DIRS}) + +add_definitions(${Boost_DEFINITIONS}) + +# Write version in header +configure_file ( + "${PROJECT_SOURCE_DIR}/src/version.hpp.in" + "${PROJECT_BINARY_DIR}/version.hpp" +) + +file(GLOB sources *.cpp) +add_executable(mastorss ${sources}) +target_link_libraries(mastorss + mastodon-cpp ${CURLPP_LIBRARIES} + ${JSONCPP_LIBRARIES} ${Boost_LIBRARIES} stdc++fs) +install(TARGETS mastorss DESTINATION ${CMAKE_INSTALL_BINDIR}) + +install(FILES watchwords.json + DESTINATION ${CMAKE_INSTALL_DATADIR}/mastorss)