Skip to content

Commit

Permalink
feat(cmake): Generate pkg-config files
Browse files Browse the repository at this point in the history
Fixes: #447
  • Loading branch information
gjasny committed Mar 4, 2021
1 parent fe4f437 commit 5699348
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 15 deletions.
44 changes: 32 additions & 12 deletions .github/workflows/cmake-ci.yml
Expand Up @@ -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
11 changes: 8 additions & 3 deletions CMakeLists.txt
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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")

Expand All @@ -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)
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions 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()
1 change: 1 addition & 0 deletions cmake/project-import-pkgconfig/sample_client.cc
1 change: 1 addition & 0 deletions cmake/project-import-pkgconfig/sample_server.cc
14 changes: 14 additions & 0 deletions 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@
14 changes: 14 additions & 0 deletions 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@
14 changes: 14 additions & 0 deletions 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@
20 changes: 20 additions & 0 deletions core/CMakeLists.txt
Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions pull/CMakeLists.txt
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions push/CMakeLists.txt
Expand Up @@ -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()

0 comments on commit 5699348

Please sign in to comment.