From e5a408386b792de68639e8cb6051c4d2f2bf96ff Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Fri, 26 Aug 2022 22:20:51 +0200 Subject: [PATCH] build: add pkg-config support (#1767) * build: add pkg-config support The CMake build script now generates a simple pkg-config files that can be easily used by non-CMake users. The file is generated from a template file that gets filled in at configure time. As CMake doesn't have anything similar to Meson's pkg-config generator the file is quite static, i.e. new simdjson public defines/dependencies won't be picked up automatically. This approach also suffers from one minor issue, mentioned in [jtojnar/cmake-snips][]; in short, it doesn't work well when users specify CMAKE_INSTALL_INCLUDEDIR and similar as absolute paths. It's not a big deal, and it will easily fixable once you'll require CMake >=3.20. Fixes #1763 [jtojnar/cmake-snips]: https://github.com/jtojnar/cmake-snips#concatenating-paths-when-building-pkg-config-files * build: handle absolute paths in .pc generation As mentioned in the previous commit message, correct concatenation of paths is only available in CMake >=3.20, so handling absolute paths in pkg-config file generation requires using jtojnar's JoinPaths module. * ci: add debian job This new jobs compiles simdjson on Debian Testing, a semi-rolling release, so that new compilers are always tested. This job also tests the pkg-config file introduced in commit 1096c3b299fb32ec326ed823c9310b23f761de3c --- .github/workflows/debian.yaml | 33 +++++++++++++++++++++++++++++++++ CMakeLists.txt | 18 ++++++++++++++++++ cmake/JoinPaths.cmake | 23 +++++++++++++++++++++++ simdjson.pc.in | 11 +++++++++++ 4 files changed, 85 insertions(+) create mode 100644 .github/workflows/debian.yaml create mode 100644 cmake/JoinPaths.cmake create mode 100644 simdjson.pc.in diff --git a/.github/workflows/debian.yaml b/.github/workflows/debian.yaml new file mode 100644 index 000000000..fdeb022c1 --- /dev/null +++ b/.github/workflows/debian.yaml @@ -0,0 +1,33 @@ +name: Debian + +on: [push, pull_request] + +defaults: + run: + shell: sh + +permissions: + contents: read + +jobs: + pkg-config: + runs-on: ubuntu-latest + container: + image: debian:testing + + steps: + - uses: actions/checkout@v3 + + - name: Install dependencies + run: | + apt -y update + apt -y --no-install-recommends install g++ cmake make pkg-config + + - name: Build and install + run: | + cmake -B build + cmake --build build + cmake --install build + + - name: Test pkg-config + run: g++ examples/quickstart/quickstart.cpp $(pkg-config --cflags --libs simdjson) diff --git a/CMakeLists.txt b/CMakeLists.txt index 83d360c91..dc236cf52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,6 +152,24 @@ install( COMPONENT example_Development ) +# pkg-config +include(cmake/JoinPaths.cmake) +join_paths(PKGCONFIG_INCLUDEDIR "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") +join_paths(PKGCONFIG_LIBDIR "\${prefix}" "${CMAKE_INSTALL_LIBDIR}") + +if(SIMDJSON_ENABLE_THREADS) + set(PKGCONFIG_CFLAGS "-DSIMDJSON_THREADS_ENABLED=1") + if(CMAKE_THREAD_LIBS_INIT) + set(PKGCONFIG_LIBS_PRIVATE "Libs.private: ${CMAKE_THREAD_LIBS_INIT}") + endif() +endif() + +configure_file("simdjson.pc.in" "simdjson.pc" @ONLY) +install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/simdjson.pc" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig" +) + # # CPack # diff --git a/cmake/JoinPaths.cmake b/cmake/JoinPaths.cmake new file mode 100644 index 000000000..c68d91b84 --- /dev/null +++ b/cmake/JoinPaths.cmake @@ -0,0 +1,23 @@ +# This module provides function for joining paths +# known from most languages +# +# SPDX-License-Identifier: (MIT OR CC0-1.0) +# Copyright 2020 Jan Tojnar +# https://github.com/jtojnar/cmake-snips +# +# Modelled after Python’s os.path.join +# https://docs.python.org/3.7/library/os.path.html#os.path.join +# Windows not supported +function(join_paths joined_path first_path_segment) + set(temp_path "${first_path_segment}") + foreach(current_segment IN LISTS ARGN) + if(NOT ("${current_segment}" STREQUAL "")) + if(IS_ABSOLUTE "${current_segment}") + set(temp_path "${current_segment}") + else() + set(temp_path "${temp_path}/${current_segment}") + endif() + endif() + endforeach() + set(${joined_path} "${temp_path}" PARENT_SCOPE) +endfunction() diff --git a/simdjson.pc.in b/simdjson.pc.in new file mode 100644 index 000000000..7312a0107 --- /dev/null +++ b/simdjson.pc.in @@ -0,0 +1,11 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +includedir=@PKGCONFIG_INCLUDEDIR@ +libdir=@PKGCONFIG_LIBDIR@ + +Name: @PROJECT_NAME@ +Description: @PROJECT_DESCRIPTION@ +URL: @PROJECT_HOMEPAGE_URL@ +Version: @PROJECT_VERSION@ +Cflags: -I${includedir} @PKGCONFIG_CFLAGS@ +Libs: -L${libdir} -l@PROJECT_NAME@ +@PKGCONFIG_LIBS_PRIVATE@