From 5699348faa910c5decad62ea5aff41d7948d4798 Mon Sep 17 00:00:00 2001 From: Gregor Jasny Date: Thu, 4 Feb 2021 09:38:13 +0100 Subject: [PATCH] feat(cmake): Generate pkg-config files Fixes: #447 --- .github/workflows/cmake-ci.yml | 44 ++++++++++++++----- CMakeLists.txt | 11 +++-- .../CMakeLists.txt | 0 .../sample_client.cc | 0 .../sample_server.cc | 0 cmake/project-import-pkgconfig/CMakeLists.txt | 21 +++++++++ .../project-import-pkgconfig/sample_client.cc | 1 + .../project-import-pkgconfig/sample_server.cc | 1 + cmake/prometheus-cpp-core.pc.in | 14 ++++++ cmake/prometheus-cpp-pull.pc.in | 14 ++++++ cmake/prometheus-cpp-push.pc.in | 14 ++++++ core/CMakeLists.txt | 20 +++++++++ pull/CMakeLists.txt | 24 ++++++++++ push/CMakeLists.txt | 18 ++++++++ 14 files changed, 167 insertions(+), 15 deletions(-) rename cmake/{project-import => project-import-cmake}/CMakeLists.txt (100%) rename cmake/{project-import => project-import-cmake}/sample_client.cc (100%) rename cmake/{project-import => project-import-cmake}/sample_server.cc (100%) create mode 100644 cmake/project-import-pkgconfig/CMakeLists.txt create mode 120000 cmake/project-import-pkgconfig/sample_client.cc create mode 120000 cmake/project-import-pkgconfig/sample_server.cc create mode 100644 cmake/prometheus-cpp-core.pc.in create mode 100644 cmake/prometheus-cpp-pull.pc.in create mode 100644 cmake/prometheus-cpp-push.pc.in diff --git a/.github/workflows/cmake-ci.yml b/.github/workflows/cmake-ci.yml index 2b8c0b8f..310c3ee1 100644 --- a/.github/workflows/cmake-ci.yml +++ b/.github/workflows/cmake-ci.yml @@ -86,24 +86,44 @@ jobs: - name: "Install Release" run: cmake --install ${{ github.workspace }}/_build --config Release - - name: "Configure import for Unix with internal dependencies" + - name: "Configure CMake import for Unix with internal dependencies" if: matrix.dependencies == 'submodule' && runner.os != 'Windows' - run: cmake -Dprometheus-cpp_DIR=${{ github.workspace }}/_install/lib/cmake/prometheus-cpp -DCMAKE_CONFIGURATION_TYPES='Release;Debug' -G"Ninja Multi-Config" -S ${{ github.workspace }}/cmake/project-import -B ${{ github.workspace }}/_import + run: cmake -Dprometheus-cpp_DIR=${{ github.workspace }}/_install/lib/cmake/prometheus-cpp -DCMAKE_CONFIGURATION_TYPES='Release;Debug' -G"Ninja Multi-Config" -S ${{ github.workspace }}/cmake/project-import-cmake -B ${{ github.workspace }}/_import_cmake - - name: "Configure import for Windows with internal dependencies" + - name: "Configure CMake import for Windows with internal dependencies" if: matrix.dependencies == 'submodule' && runner.os == 'Windows' - run: cmake -Dprometheus-cpp_DIR=${{ github.workspace }}/_install/lib/cmake/prometheus-cpp -S ${{ github.workspace }}/cmake/project-import -B ${{ github.workspace }}/_import + run: cmake -Dprometheus-cpp_DIR=${{ github.workspace }}/_install/lib/cmake/prometheus-cpp -S ${{ github.workspace }}/cmake/project-import-cmake -B ${{ github.workspace }}/_import_cmake - - name: "Configure import for Unix with vcpkg dependencies" + - name: "Configure CMake import for Unix with vcpkg dependencies" if: matrix.dependencies == 'vcpkg' && runner.os != 'Windows' - run: cmake -Dprometheus-cpp_DIR=${{ github.workspace }}/_install/lib/cmake/prometheus-cpp "-DCMAKE_TOOLCHAIN_FILE=${VCPKG_INSTALLATION_ROOT}/scripts/buildsystems/vcpkg.cmake" -DCMAKE_CONFIGURATION_TYPES='Release;Debug' -G"Ninja Multi-Config" -S ${{ github.workspace }}/cmake/project-import -B ${{ github.workspace }}/_import + run: cmake -Dprometheus-cpp_DIR=${{ github.workspace }}/_install/lib/cmake/prometheus-cpp "-DCMAKE_TOOLCHAIN_FILE=${VCPKG_INSTALLATION_ROOT}/scripts/buildsystems/vcpkg.cmake" -DCMAKE_CONFIGURATION_TYPES='Release;Debug' -G"Ninja Multi-Config" -S ${{ github.workspace }}/cmake/project-import-cmake -B ${{ github.workspace }}/_import_cmake - - name: "Configure import for Windows with vcpkg dependencies" + - name: "Configure CMake import for Windows with vcpkg dependencies" if: matrix.dependencies == 'vcpkg' && runner.os == 'Windows' - run: cmake -Dprometheus-cpp_DIR=${{ github.workspace }}/_install/lib/cmake/prometheus-cpp "-DCMAKE_TOOLCHAIN_FILE=${Env:VCPKG_INSTALLATION_ROOT}\scripts\buildsystems\vcpkg.cmake" -S ${{ github.workspace }}/cmake/project-import -B ${{ github.workspace }}/_import + run: cmake -Dprometheus-cpp_DIR=${{ github.workspace }}/_install/lib/cmake/prometheus-cpp "-DCMAKE_TOOLCHAIN_FILE=${Env:VCPKG_INSTALLATION_ROOT}\scripts\buildsystems\vcpkg.cmake" -S ${{ github.workspace }}/cmake/project-import-cmake -B ${{ github.workspace }}/_import_cmake - - name: "Build import Debug" - run: cmake --build ${{ github.workspace }}/_import --config Debug + - name: "Build CMake import Debug" + run: cmake --build ${{ github.workspace }}/_import_cmake --config Debug - - name: "Build import Release" - run: cmake --build ${{ github.workspace }}/_import --config Release + - name: "Build CMake import Release" + run: cmake --build ${{ github.workspace }}/_import_cmake --config Release + + - name: "Configure for Unix Shared Libs with internal dependencies" + if: matrix.dependencies == 'submodule' && runner.os != 'Windows' + run: cmake -DUSE_THIRDPARTY_LIBRARIES=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/_install_shared -DENABLE_WARNINGS_AS_ERRORS=ON -DENABLE_COMPRESSION=OFF -DENABLE_PUSH=OFF -DCMAKE_DEBUG_POSTFIX=_d -GNinja -S ${{ github.workspace }} -B ${{ github.workspace }}/_build_shared + + - name: "Build for Unix Shared Libs" + if: matrix.dependencies == 'submodule' && runner.os != 'Windows' + run: cmake --build ${{ github.workspace }}/_build_shared + + - name: "Install for Unix Shared Libs" + if: matrix.dependencies == 'submodule' && runner.os != 'Windows' + run: cmake --install ${{ github.workspace }}/_build_shared + + - name: "Configure pkg-config import for Unix" + if: matrix.dependencies == 'submodule' && runner.os != 'Windows' + run: cmake -DCMAKE_PREFIX_PATH=${{ github.workspace }}/_install_shared -GNinja -S ${{ github.workspace }}/cmake/project-import-pkgconfig -B ${{ github.workspace }}/_import_pkgconfig + + - name: "Build pkg-config import for Unix" + if: matrix.dependencies == 'submodule' && runner.os != 'Windows' + run: cmake --build ${{ github.workspace }}/_import_pkgconfig diff --git a/CMakeLists.txt b/CMakeLists.txt index 372e9434..7912dcb9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,11 @@ if(POLICY CMP0091) cmake_policy(SET CMP0091 NEW) # recognize CMAKE_MSVC_RUNTIME_LIBRARY endif() -project(prometheus-cpp VERSION 0.12.1) +project(prometheus-cpp + VERSION 0.12.1 + DESCRIPTION "Prometheus Client Library for Modern C++" + HOMEPAGE_URL "https://github.com/jupp0r/prometheus-cpp" +) include(GenerateExportHeader) include(GNUInstallDirs) @@ -20,6 +24,7 @@ option(ENABLE_TESTING "Build tests" ON) option(USE_THIRDPARTY_LIBRARIES "Use 3rdParty submodules" ON) option(THIRDPARTY_CIVETWEB_WITH_SSL "Enable SSL support for embedded civetweb source code") option(OVERRIDE_CXX_STANDARD_FLAGS "Force building with -std=c++11 even if the CXXLFAGS are configured differently" ON) +option(GENERATE_PKGCONFIG "Generate and install pkg-config files" ${UNIX}) option(RUN_IWYU "Run include-what-you-use" OFF) if(OVERRIDE_CXX_STANDARD_FLAGS) @@ -140,8 +145,7 @@ install( if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) set(CPACK_PACKAGE_CONTACT "prometheus-cpp@@noreply.github.com") - set(CPACK_PACKAGE_DESCRIPTION "C++ library for Prometheus exporters") - set(CPACK_PACKAGE_HOMEPAGE_URL "https://github.com/jupp0r/prometheus-cpp") + set(CPACK_PACKAGE_DESCRIPTION "${PROJECT_DESCRIPTION}") set(CPACK_PACKAGE_RELOCATABLE OFF) set(CPACK_PACKAGE_VENDOR "The prometheus-cpp authors") @@ -167,5 +171,6 @@ include(FeatureSummary) add_feature_info("Pull" "${ENABLE_PULL}" "support for pulling metrics") add_feature_info("Push" "${ENABLE_PUSH}" "support for pushing metrics to a push-gateway") add_feature_info("Compression" "${ENABLE_COMPRESSION}" "support for zlib compression of metrics") +add_feature_info("pkg-config" "${GENERATE_PKGCONFIG}" "generate pkg-config files") add_feature_info("IYWU" "${RUN_IWYU}" "include-what-you-use") feature_summary(WHAT ALL) diff --git a/cmake/project-import/CMakeLists.txt b/cmake/project-import-cmake/CMakeLists.txt similarity index 100% rename from cmake/project-import/CMakeLists.txt rename to cmake/project-import-cmake/CMakeLists.txt diff --git a/cmake/project-import/sample_client.cc b/cmake/project-import-cmake/sample_client.cc similarity index 100% rename from cmake/project-import/sample_client.cc rename to cmake/project-import-cmake/sample_client.cc diff --git a/cmake/project-import/sample_server.cc b/cmake/project-import-cmake/sample_server.cc similarity index 100% rename from cmake/project-import/sample_server.cc rename to cmake/project-import-cmake/sample_server.cc diff --git a/cmake/project-import-pkgconfig/CMakeLists.txt b/cmake/project-import-pkgconfig/CMakeLists.txt new file mode 100644 index 00000000..bcd1d8e1 --- /dev/null +++ b/cmake/project-import-pkgconfig/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.12 FATAL_ERROR) + +project(prometheus-cpp-import) + +set(CMAKE_CXX_STANDARD 11) + +find_package(PkgConfig REQUIRED) + +pkg_check_modules(PROMETHEUS_CPP_CORE REQUIRED prometheus-cpp-core) +pkg_check_modules(PROMETHEUS_CPP_PUSH IMPORTED_TARGET prometheus-cpp-push) +pkg_check_modules(PROMETHEUS_CPP_PULL IMPORTED_TARGET prometheus-cpp-pull) + +if(PROMETHEUS_CPP_PUSH_FOUND) + add_executable(sample-client sample_client.cc) + target_link_libraries(sample-client PRIVATE PkgConfig::PROMETHEUS_CPP_PUSH) +endif() + +if(PROMETHEUS_CPP_PULL_FOUND) + add_executable(sample-server sample_server.cc) + target_link_libraries(sample-server PRIVATE PkgConfig::PROMETHEUS_CPP_PULL) +endif() diff --git a/cmake/project-import-pkgconfig/sample_client.cc b/cmake/project-import-pkgconfig/sample_client.cc new file mode 120000 index 00000000..4c67af80 --- /dev/null +++ b/cmake/project-import-pkgconfig/sample_client.cc @@ -0,0 +1 @@ +../../push/tests/integration/sample_client.cc \ No newline at end of file diff --git a/cmake/project-import-pkgconfig/sample_server.cc b/cmake/project-import-pkgconfig/sample_server.cc new file mode 120000 index 00000000..89f9e5c0 --- /dev/null +++ b/cmake/project-import-pkgconfig/sample_server.cc @@ -0,0 +1 @@ +../../pull/tests/integration/sample_server.cc \ No newline at end of file diff --git a/cmake/prometheus-cpp-core.pc.in b/cmake/prometheus-cpp-core.pc.in new file mode 100644 index 00000000..8a24ced8 --- /dev/null +++ b/cmake/prometheus-cpp-core.pc.in @@ -0,0 +1,14 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ +libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@ + +Name: @PROJECT_NAME@-core +Description: @PROJECT_DESCRIPTION@ +URL: @PROJECT_HOMEPAGE_URL@ +Version: @PROJECT_VERSION@ +Requires: +Requires.private: @PKGCONFIG_REQUIRES@ +Cflags: -I${includedir} +Libs: -L${libdir} -l@PROJECT_NAME@-core +Libs.private: @CMAKE_THREAD_LIBS_INIT@ @PKGCONFIG_LIBS@ \ No newline at end of file diff --git a/cmake/prometheus-cpp-pull.pc.in b/cmake/prometheus-cpp-pull.pc.in new file mode 100644 index 00000000..652848f6 --- /dev/null +++ b/cmake/prometheus-cpp-pull.pc.in @@ -0,0 +1,14 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ +libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@ + +Name: @PROJECT_NAME@-pull +Description: @PROJECT_DESCRIPTION@ +URL: @PROJECT_HOMEPAGE_URL@ +Version: @PROJECT_VERSION@ +Requires: @PROJECT_NAME@-core +Requires.private: @PKGCONFIG_REQUIRES@ +Cflags: -I${includedir} +Libs: -L${libdir} -l@PROJECT_NAME@-pull +Libs.private: @CMAKE_THREAD_LIBS_INIT@ @PKGCONFIG_LIBS@ diff --git a/cmake/prometheus-cpp-push.pc.in b/cmake/prometheus-cpp-push.pc.in new file mode 100644 index 00000000..a94484d1 --- /dev/null +++ b/cmake/prometheus-cpp-push.pc.in @@ -0,0 +1,14 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ +libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@ + +Name: @PROJECT_NAME@-push +Description: @PROJECT_DESCRIPTION@ +URL: @PROJECT_HOMEPAGE_URL@ +Version: @PROJECT_VERSION@ +Requires: @PROJECT_NAME@-core +Requires.private: @PKGCONFIG_REQUIRES@ +Cflags: -I${includedir} +Libs: -L${libdir} -l@PROJECT_NAME@-push +Libs.private: @CMAKE_THREAD_LIBS_INIT@ @PKGCONFIG_LIBS@ diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 11096a0b..13b27624 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -62,6 +62,26 @@ install( DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) +if(GENERATE_PKGCONFIG) + set(PKGCONFIG_LIBS) + set(PKGCONFIG_REQUIRES) + + if(HAVE_CXX_LIBATOMIC) + string(APPEND PKGCONFIG_LIBS " -latomic") + endif() + + configure_file( + ${PROJECT_SOURCE_DIR}/cmake/prometheus-cpp-core.pc.in + ${CMAKE_CURRENT_BINARY_DIR}/prometheus-cpp-core.pc + @ONLY + ) + + install( + FILES ${CMAKE_CURRENT_BINARY_DIR}/prometheus-cpp-core.pc + DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig + ) +endif() + if(ENABLE_TESTING) add_subdirectory(tests) endif() diff --git a/pull/CMakeLists.txt b/pull/CMakeLists.txt index 6dfa6a2f..db1452e7 100644 --- a/pull/CMakeLists.txt +++ b/pull/CMakeLists.txt @@ -85,6 +85,30 @@ install( DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) +if(GENERATE_PKGCONFIG) + set(PKGCONFIG_LIBS) + set(PKGCONFIG_REQUIRES) + + if(NOT USE_THIRDPARTY_LIBRARIES) + string(APPEND PKGCONFIG_LIBS " -lcivetweb-cpp -lcivetweb") + endif() + + if(ENABLE_COMPRESSION) + string(APPEND PKGCONFIG_REQUIRES " zlib") + endif() + + configure_file( + ${PROJECT_SOURCE_DIR}/cmake/prometheus-cpp-pull.pc.in + ${CMAKE_CURRENT_BINARY_DIR}/prometheus-cpp-pull.pc + @ONLY + ) + + install( + FILES ${CMAKE_CURRENT_BINARY_DIR}/prometheus-cpp-pull.pc + DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig + ) +endif() + if(ENABLE_TESTING) add_library(pull_internal_headers INTERFACE) add_library(${PROJECT_NAME}::pull_internal_headers ALIAS pull_internal_headers) diff --git a/push/CMakeLists.txt b/push/CMakeLists.txt index 81e606f1..d8690c38 100644 --- a/push/CMakeLists.txt +++ b/push/CMakeLists.txt @@ -49,6 +49,24 @@ install( DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) +if(GENERATE_PKGCONFIG) + set(PKGCONFIG_LIBS) + set(PKGCONFIG_REQUIRES) + + string(APPEND PKGCONFIG_REQUIRES " libcurl") + + configure_file( + ${PROJECT_SOURCE_DIR}/cmake/prometheus-cpp-push.pc.in + ${CMAKE_CURRENT_BINARY_DIR}/prometheus-cpp-push.pc + @ONLY + ) + + install( + FILES ${CMAKE_CURRENT_BINARY_DIR}/prometheus-cpp-push.pc + DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig + ) +endif() + if(ENABLE_TESTING) add_subdirectory(tests) endif()