From a0b09d0ff735d34fd99029bb59ffff2874565b42 Mon Sep 17 00:00:00 2001 From: Andrea Pappacoda Date: Sun, 28 Aug 2022 13:01:20 +0200 Subject: [PATCH] build(cmake): improve pkg-config generation With this patch the pkg-config generation when using the CMake build system is improved in the following ways: - Libs.private is now filled when needed - The JoinPaths module is now used to join paths, leading to simpler code - The .pc file is always generated, regardless of the platform, as it can also be consumed on Windows Here's how the .pc file is affected by these changes, in comparison to the one generated with the official Makefiles: $ diff -s lib/libzstd.pc build/cmake/build-old/lib/libzstd.pc 15c15 < Libs.private: -pthread --- > Libs.private: $ diff -s lib/libzstd.pc build/cmake/build-new/lib/libzstd.pc Files lib/libzstd.pc and build/cmake/build-new/lib/libzstd.pc are identical --- build/cmake/CMakeModules/JoinPaths.cmake | 23 ++++++++++++++++ build/cmake/lib/CMakeLists.txt | 35 ++++++++---------------- 2 files changed, 34 insertions(+), 24 deletions(-) create mode 100644 build/cmake/CMakeModules/JoinPaths.cmake diff --git a/build/cmake/CMakeModules/JoinPaths.cmake b/build/cmake/CMakeModules/JoinPaths.cmake new file mode 100644 index 00000000000..c68d91b84db --- /dev/null +++ b/build/cmake/CMakeModules/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/build/cmake/lib/CMakeLists.txt b/build/cmake/lib/CMakeLists.txt index 4905bd91338..cf7af0f8cd6 100644 --- a/build/cmake/lib/CMakeLists.txt +++ b/build/cmake/lib/CMakeLists.txt @@ -135,30 +135,17 @@ if (ZSTD_BUILD_STATIC) OUTPUT_NAME ${STATIC_LIBRARY_BASE_NAME}) endif () -if (UNIX OR MINGW) - # pkg-config - set(PREFIX "${CMAKE_INSTALL_PREFIX}") - set(EXEC_PREFIX "\${prefix}") - set(LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}") - set(INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}") - set(VERSION "${zstd_VERSION}") - - string(LENGTH "${PREFIX}" PREFIX_LENGTH) - string(SUBSTRING "${LIBDIR}" 0 ${PREFIX_LENGTH} LIBDIR_PREFIX) - string(SUBSTRING "${LIBDIR}" ${PREFIX_LENGTH} -1 LIBDIR_SUFFIX) - string(SUBSTRING "${INCLUDEDIR}" 0 ${PREFIX_LENGTH} INCLUDEDIR_PREFIX) - string(SUBSTRING "${INCLUDEDIR}" ${PREFIX_LENGTH} -1 INCLUDEDIR_SUFFIX) - - if ("${INCLUDEDIR_PREFIX}" STREQUAL "${PREFIX}") - set(INCLUDEDIR "\${prefix}${INCLUDEDIR_SUFFIX}") - endif() - if ("${LIBDIR_PREFIX}" STREQUAL "${PREFIX}") - set(LIBDIR "\${exec_prefix}${LIBDIR_SUFFIX}") - endif() - - configure_file("${LIBRARY_DIR}/libzstd.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libzstd.pc" @ONLY) - install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libzstd.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") -endif () +# pkg-config +include(JoinPaths) # can be replaced by cmake_path(APPEND) in CMake 3.20 +set(PREFIX "${CMAKE_INSTALL_PREFIX}") +set(EXEC_PREFIX "\${prefix}") +join_paths(LIBDIR "\${exec_prefix}" "${CMAKE_INSTALL_LIBDIR}") +join_paths(INCLUDEDIR "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") +set(LIBS_PRIVATE "${THREADS_LIBS}") +set(VERSION "${zstd_VERSION}") + +configure_file("${LIBRARY_DIR}/libzstd.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libzstd.pc" @ONLY) +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libzstd.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") # install target install(FILES