Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMartin Hořeňovský2025-07-10 16:39:42 +0200
committerMartin Hořeňovský2025-07-10 16:39:46 +0200
commit3839e27f056cd975e5be2faa9adb5a8cf1f5dcf4 (patch)
tree95b769187cbdf6b1f8b3c0b546f99334ae57e1f0
parent39c32b966257ccbc77f6827f359821ee453b1876 (diff)
downloadcatch3-3839e27f056cd975e5be2faa9adb5a8cf1f5dcf4.tar.gz
Generate pkg-config files at install time
This enables us to generate pkg-config with proper paths if the installation prefix is decided at install time, e.g. with ``` cmake --install build-dir --prefix /path/to/somewhere ``` It also means that we can use CMake's generator expression to get the _real_ final name of the libraries, e.g. including debug postfix. Closes #2979
-rw-r--r--CMake/catch2-with-main.pc.in10
-rw-r--r--CMake/catch2.pc.in6
-rw-r--r--CMakeLists.txt50
3 files changed, 42 insertions, 24 deletions
diff --git a/CMake/catch2-with-main.pc.in b/CMake/catch2-with-main.pc.in
index 69a790bb..eeac3e35 100644
--- a/CMake/catch2-with-main.pc.in
+++ b/CMake/catch2-with-main.pc.in
@@ -1,10 +1,10 @@
-includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
-libdir=@CMAKE_INSTALL_FULL_LIBDIR@
+prefix=@CMAKE_INSTALL_PREFIX@
+libdir=${prefix}/@lib_dir@
pkg_version=@Catch2_VERSION@
-Name: Catch2-With-Main
+Name: Catch2 with main function
Description: A modern, C++-native test framework for C++14 and above (links in default main)
+URL: https://github.com/catchorg/Catch2
Version: ${pkg_version}
Requires: catch2 = ${pkg_version}
-Cflags: -I${includedir}
-Libs: -L${libdir} -lCatch2Main
+Libs: -L${libdir} -l@lib_name@
diff --git a/CMake/catch2.pc.in b/CMake/catch2.pc.in
index bd1c95a1..74e22c15 100644
--- a/CMake/catch2.pc.in
+++ b/CMake/catch2.pc.in
@@ -1,11 +1,11 @@
prefix=@CMAKE_INSTALL_PREFIX@
exec_prefix=${prefix}
-includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
-libdir=@CMAKE_INSTALL_FULL_LIBDIR@
+includedir=${prefix}/@include_dir@
+libdir=${prefix}/@lib_dir@
Name: Catch2
Description: A modern, C++-native, test framework for C++14 and above
URL: https://github.com/catchorg/Catch2
Version: @Catch2_VERSION@
Cflags: -I${includedir}
-Libs: -L${libdir} -lCatch2
+Libs: -L${libdir} -l@lib_name@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 885d68a5..bc1022af 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -170,23 +170,41 @@ if(NOT_SUBPROJECT)
"${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig"
CACHE PATH "Path where catch2.pc is installed"
)
- configure_file(
- ${CMAKE_CURRENT_SOURCE_DIR}/CMake/catch2.pc.in
- ${CMAKE_CURRENT_BINARY_DIR}/catch2.pc
- @ONLY
- )
- configure_file(
- ${CMAKE_CURRENT_SOURCE_DIR}/CMake/catch2-with-main.pc.in
- ${CMAKE_CURRENT_BINARY_DIR}/catch2-with-main.pc
- @ONLY
- )
- install(
- FILES
- "${CMAKE_CURRENT_BINARY_DIR}/catch2.pc"
- "${CMAKE_CURRENT_BINARY_DIR}/catch2-with-main.pc"
- DESTINATION
- ${PKGCONFIG_INSTALL_DIR}
+
+ # Generate the pkg-config files
+ # To understand the script below, you have to understand that it works in two steps.
+ # 1) A CMake script is generated at configuration time
+ # 2) It is executed at install time.
+ # And both of these have access to different parts of the information we need.
+ #
+ # Further, the variables before "[[" are expanded at configuration time,
+ # while the ones inside the [[]] block are expanded at script execution (install) time.
+ string(
+ JOIN "\n"
+ install_script
+ "set(install_pkgconfdir \"${PKGCONFIG_INSTALL_DIR}\")"
+ "set(impl_pc_file \"${CMAKE_CURRENT_SOURCE_DIR}/CMake/catch2.pc.in\")"
+ "set(main_pc_file \"${CMAKE_CURRENT_SOURCE_DIR}/CMake/catch2-with-main.pc.in\")"
+ "set(Catch2_VERSION ${Catch2_VERSION})"
+ "set(include_dir \"${CMAKE_INSTALL_INCLUDEDIR}\")"
+ "set(lib_dir \"${CMAKE_INSTALL_LIBDIR}\")"
+ [[
+ set(lib_name "$<TARGET_FILE_BASE_NAME:Catch2>")
+ configure_file(
+ "${impl_pc_file}"
+ "${CMAKE_INSTALL_PREFIX}/${install_pkgconfdir}/catch2.pc"
+ @ONLY
+ )
+
+ set(lib_name "$<TARGET_FILE_BASE_NAME:Catch2WithMain>")
+ configure_file(
+ "${main_pc_file}"
+ "${CMAKE_INSTALL_PREFIX}/${install_pkgconfdir}/catch2-with-main.pc"
+ @ONLY
+ )
+ ]]
)
+ install(CODE "${install_script}")
set(CPACK_PACKAGE_CONTACT "https://github.com/catchorg/Catch2/")