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 Feb 26, 2021
1 parent b54cd07 commit 8c62476
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 17 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
10 changes: 7 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" ON)

if(OVERRIDE_CXX_STANDARD_FLAGS)
set(CMAKE_CXX_STANDARD 11)
Expand Down Expand Up @@ -121,8 +126,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 Down
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
12 changes: 12 additions & 0 deletions cmake/prometheus-cpp-core.pc.in
@@ -0,0 +1,12 @@
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@
Cflags: -I${includedir}
Libs: -L${libdir} -l@PROJECT_NAME@-core
Libs.private: @CMAKE_THREAD_LIBS_INIT@ @PKGCONFIG_LIBS@ @PKGCONFIG_LIBATOMIC@
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@
18 changes: 17 additions & 1 deletion core/CMakeLists.txt
@@ -1,4 +1,3 @@

add_library(core
src/check_names.cc
src/counter.cc
Expand Down Expand Up @@ -62,6 +61,23 @@ install(
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

if(GENERATE_PKGCONFIG)
if(HAVE_CXX_LIBATOMIC)
string(PKGCONFIG_LIBATOMIC -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
15 changes: 15 additions & 0 deletions pull/CMakeLists.txt
Expand Up @@ -7,6 +7,7 @@ if(USE_THIRDPARTY_LIBRARIES)
)
else()
find_package(civetweb CONFIG REQUIRED)
string(APPEND PKGCONFIG_LIBS " -lcivetweb-cpp -lcivetweb")

# work-around https://github.com/civetweb/civetweb/pull/918
if(WIN32 AND NOT TARGET WINSOCK::WINSOCK)
Expand All @@ -17,6 +18,7 @@ endif()

if(ENABLE_COMPRESSION)
find_package(ZLIB REQUIRED)
string(APPEND PKGCONFIG_REQUIRES " zlib")
endif()

add_library(pull
Expand Down Expand Up @@ -85,6 +87,19 @@ install(
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

if(GENERATE_PKGCONFIG)
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
16 changes: 15 additions & 1 deletion push/CMakeLists.txt
@@ -1,4 +1,3 @@

find_package(CURL REQUIRED)

add_library(push
Expand Down Expand Up @@ -49,6 +48,21 @@ install(
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

if(GENERATE_PKGCONFIG)
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 8c62476

Please sign in to comment.