diff --git a/CMakeLists.txt b/CMakeLists.txt index f3a37d3..b3fdc05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,34 +1,53 @@ -cmake_minimum_required (VERSION 3.2) +# Support version 3.6 and above, but use policy settings up to 3.14. +# 3.6 is needed because of IMPORTED_TARGET in pkg_check_modules(). +cmake_minimum_required(VERSION 3.6...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() + project(identiconpp VERSION 0.6.1 LANGUAGES CXX) +# DESCRIPTION was introduced in version 3.9. +if(NOT (${CMAKE_VERSION} VERSION_LESS 3.9)) + set(PROJECT_DESCRIPTION + "Library to generate identicons for C++.") +endif() option(WITH_TESTS "Enable Tests" NO) +option(BUILD_SHARED_LIBS "Build shared libraries." YES) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) -set(CMAKE_CXX_FLAGS_DEBUG - "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra -Wpedantic -ftrapv \ --fsanitize=undefined -g -Og -fno-omit-frame-pointer") +set(DEBUG_CXXFLAGS + "-Wall" + "-Wextra" + "-Wpedantic" + "-ftrapv" + "-fsanitize=undefined" + "-g" + "-Og" + "-fno-omit-frame-pointer") +set(DEBUG_LDFLAGS + "-fsanitize=undefined") +add_compile_options("$<$:${DEBUG_CXXFLAGS}>") +# 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() if(CMAKE_BUILD_TYPE STREQUAL "Debug") add_definitions(-DDEBUG=1) endif() -include(GNUInstallDirs) - -install(FILES README.md DESTINATION ${CMAKE_INSTALL_DOCDIR}) - add_subdirectory(src) - -configure_file("${PROJECT_SOURCE_DIR}/${PROJECT_NAME}.pc.in" - "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" @ONLY) - -install(FILES - "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" - DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") +add_subdirectory(cmake) +add_subdirectory(pkg-config) if(WITH_TESTS) add_subdirectory(tests) diff --git a/README.md b/README.md index 84aef8d..e679cce 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ modifications. "6x4 identicon, ltr_symmetric, 10px padding") The example images above are generated using [example.cpp] -(https://schlomp.space/tastytea/identiconpp/src/branch/master/example.cpp). +(https://schlomp.space/tastytea/identiconpp/src/branch/master/examples/example.cpp). ## Features @@ -77,7 +77,7 @@ key](https://tastytea.de/tastytea_autosign.asc). * C++ compiler (tested: [gcc](https://gcc.gnu.org/) 6/8/9, [clang](https://llvm.org/) 6) -* [cmake](https://cmake.org/) (at least 3.2) +* [cmake](https://cmake.org/) (at least 3.6) * [imagemagick](https://www.imagemagick.org/) (tested: 7.0 / 6.7) * Optional: * Tests: [Catch](https://github.com/catchorg/Catch2) (tested: 2.3 / 1.2) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt new file mode 100644 index 0000000..0a62f8c --- /dev/null +++ b/cmake/CMakeLists.txt @@ -0,0 +1,19 @@ +include(CMakePackageConfigHelpers) +include(GNUInstallDirs) + +write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake" + VERSION ${PACKAGE_VERSION} + COMPATIBILITY ExactVersion) # NOTE: Set to SameMajorVersion when stable. + +install(EXPORT ${PROJECT_NAME}Targets + FILE "${PROJECT_NAME}Targets.cmake" + NAMESPACE "${PROJECT_NAME}::" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") + +configure_file("${PROJECT_NAME}Config.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" @ONLY) + +install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") diff --git a/cmake/identiconppConfig.cmake.in b/cmake/identiconppConfig.cmake.in new file mode 100644 index 0000000..b7e7780 --- /dev/null +++ b/cmake/identiconppConfig.cmake.in @@ -0,0 +1,7 @@ +include(CMakeFindDependencyMacro) +include(GNUInstallDirs) + +find_dependency(PkgConfig REQUIRED) +pkg_check_modules(Magick++ REQUIRED IMPORTED_TARGET Magick++) + +include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..0e31bf4 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,4 @@ +# You need to install identiconpp first so that CMake can find it. +find_package(identiconpp REQUIRED CONFIG) +add_executable(example example.cpp) +target_link_libraries(example PRIVATE identiconpp::identiconpp) diff --git a/example.cpp b/examples/example.cpp similarity index 100% rename from example.cpp rename to examples/example.cpp diff --git a/pkg-config/CMakeLists.txt b/pkg-config/CMakeLists.txt new file mode 100644 index 0000000..4640f17 --- /dev/null +++ b/pkg-config/CMakeLists.txt @@ -0,0 +1,7 @@ +include(GNUInstallDirs) + +configure_file("${PROJECT_NAME}.pc.in" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" @ONLY) + +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") diff --git a/identiconpp.pc.in b/pkg-config/identiconpp.pc.in similarity index 83% rename from identiconpp.pc.in rename to pkg-config/identiconpp.pc.in index 87e9d1f..577b198 100644 --- a/identiconpp.pc.in +++ b/pkg-config/identiconpp.pc.in @@ -5,7 +5,7 @@ libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@ includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ Name: ${name} -Description: Library to generate identicons for C++. +Description: @PROJECT_DESCRIPTION@ Version: @PROJECT_VERSION@ Libs: -L${libdir} -l${name} Cflags: -I${includedir} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index df6bd74..44280a2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,18 +1,18 @@ +include(GNUInstallDirs) + find_package(PkgConfig REQUIRED) -pkg_check_modules(MAGICPP REQUIRED Magick++) - -include_directories(${MAGICPP_INCLUDE_DIRS}) - -link_directories(${MAGICPP_LIBRARY_DIRS}) +pkg_check_modules(Magick++ REQUIRED IMPORTED_TARGET Magick++) file(GLOB sources *.cpp) -add_library(${PROJECT_NAME} SHARED ${sources}) -target_link_libraries(${PROJECT_NAME} ${MAGICPP_LIBRARIES}) -target_compile_options(${PROJECT_NAME} PUBLIC ${MAGICPP_CFLAGS}) +add_library(${PROJECT_NAME} ${sources} ${PROJECT_NAME}.hpp) +target_link_libraries(${PROJECT_NAME} PkgConfig::Magick++) set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${${PROJECT_NAME}_VERSION_MAJOR}) -install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR}) -install(FILES ${CMAKE_PROJECT_NAME}.hpp +install(TARGETS ${PROJECT_NAME} + EXPORT "${PROJECT_NAME}Targets" + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) +install(FILES ${PROJECT_NAME}.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})