Skip to content

Commit 5c797cf

Browse files
authored
Merge pull request #6850 from guptapratykshh/fix-ninja-multiconfig-support
Fix Ninja Multi-Config generator support for pkg-config files
2 parents 0f19300 + 7505812 commit 5c797cf

4 files changed

Lines changed: 225 additions & 28 deletions

File tree

cmake/HPX_GeneratePackage.cmake

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,39 +63,40 @@ install(
6363
# Install dir
6464
set(HPX_CONFIG_IS_INSTALL ON)
6565
configure_file(
66-
cmake/templates/${HPX_PACKAGE_NAME}Config.cmake.in
66+
${CMAKE_CURRENT_LIST_DIR}/templates/${HPX_PACKAGE_NAME}Config.cmake.in
6767
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${HPX_PACKAGE_NAME}Config.cmake"
6868
ESCAPE_QUOTES
6969
@ONLY
7070
)
7171
set(HPX_CONF_PREFIX ${CMAKE_INSTALL_PREFIX})
7272
if(HPX_WITH_PKGCONFIG)
7373
configure_file(
74-
cmake/templates/hpxcxx.in
74+
${CMAKE_CURRENT_LIST_DIR}/templates/hpxcxx.in
7575
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/hpxcxx" @ONLY
7676
)
7777
endif()
7878

7979
# Build dir
8080
set(HPX_CONFIG_IS_INSTALL OFF)
8181
configure_file(
82-
cmake/templates/${HPX_PACKAGE_NAME}Config.cmake.in
82+
${CMAKE_CURRENT_LIST_DIR}/templates/${HPX_PACKAGE_NAME}Config.cmake.in
8383
"${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/${HPX_PACKAGE_NAME}/${HPX_PACKAGE_NAME}Config.cmake"
8484
ESCAPE_QUOTES
8585
@ONLY
8686
)
8787
set(HPX_CONF_PREFIX ${PROJECT_BINARY_DIR})
8888
if(HPX_WITH_PKGCONFIG)
8989
configure_file(
90-
cmake/templates/hpxcxx.in "${CMAKE_CURRENT_BINARY_DIR}/bin/hpxcxx" @ONLY
90+
${CMAKE_CURRENT_LIST_DIR}/templates/hpxcxx.in
91+
"${CMAKE_CURRENT_BINARY_DIR}/bin/hpxcxx" @ONLY
9192
)
9293
endif()
9394
unset(HPX_CONFIG_IS_INSTALL)
9495

9596
# Configure macros for the install dir ...
9697
set(HPX_CMAKE_MODULE_PATH "\${CMAKE_CURRENT_LIST_DIR}")
9798
configure_file(
98-
cmake/templates/HPXMacros.cmake.in
99+
${CMAKE_CURRENT_LIST_DIR}/templates/HPXMacros.cmake.in
99100
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/HPXMacros.cmake" ESCAPE_QUOTES
100101
@ONLY
101102
)
@@ -104,7 +105,7 @@ set(HPX_CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake"
104105
"${HPX_CMAKE_ADDITIONAL_MODULE_PATH_BUILD}"
105106
)
106107
configure_file(
107-
cmake/templates/HPXMacros.cmake.in
108+
${CMAKE_CURRENT_LIST_DIR}/templates/HPXMacros.cmake.in
108109
"${CMAKE_CURRENT_BINARY_DIR}/lib/cmake/${HPX_PACKAGE_NAME}/HPXMacros.cmake"
109110
ESCAPE_QUOTES @ONLY
110111
)
@@ -170,13 +171,31 @@ if(HPX_WITH_PKGCONFIG)
170171
hpx_pkgconfig_component hpx_component FALSE EXCLUDE ${exclude_targets}
171172
)
172173

173-
string(TOLOWER ${CMAKE_BUILD_TYPE} build_type)
174-
install(
175-
FILES ${OUTPUT_DIR_PC}/hpx_application_${build_type}.pc
176-
${OUTPUT_DIR_PC}/hpx_component_${build_type}.pc
177-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
178-
COMPONENT pkgconfig
179-
)
174+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
175+
if(is_multi_config)
176+
set(config_types ${CMAKE_CONFIGURATION_TYPES})
177+
if(NOT config_types)
178+
set(config_types Debug Release RelWithDebInfo MinSizeRel)
179+
endif()
180+
181+
foreach(config_type ${config_types})
182+
string(TOLOWER ${config_type} config_lower)
183+
install(
184+
FILES ${OUTPUT_DIR_PC}/hpx_application_${config_lower}.pc
185+
${OUTPUT_DIR_PC}/hpx_component_${config_lower}.pc
186+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
187+
COMPONENT pkgconfig
188+
)
189+
endforeach()
190+
else()
191+
string(TOLOWER ${CMAKE_BUILD_TYPE} build_type)
192+
install(
193+
FILES ${OUTPUT_DIR_PC}/hpx_application_${build_type}.pc
194+
${OUTPUT_DIR_PC}/hpx_component_${build_type}.pc
195+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
196+
COMPONENT pkgconfig
197+
)
198+
endif()
180199
# Temporary (to deprecate gradually)
181200
install(
182201
FILES ${OUTPUT_DIR_PC}/hpx_application.pc ${OUTPUT_DIR_PC}/hpx_component.pc

cmake/HPX_GeneratePackageUtils.cmake

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ endmacro(get_target_property)
1414
# https://github.com/boost-cmake/bcm/blob/master/share/bcm/cmake/BCMPkgConfig.cmake
1515
# https://gitlab.kitware.com/cmake/cmake/issues/17984
1616

17+
set(_hpx_generate_package_utils_dir ${CMAKE_CURRENT_LIST_DIR})
18+
1719
# Recursively add the interface_include_dirs of the dependencies and link them
1820
function(
1921
hpx_collect_usage_requirements
@@ -396,26 +398,72 @@ function(hpx_generate_pkgconfig_from_target target template is_build)
396398
hpx_link_libraries hpx_link_options hpx_library_list
397399
)
398400
399-
string(TOLOWER ${CMAKE_BUILD_TYPE} build_type)
401+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
402+
if(is_multi_config)
403+
# For multi-config generators, we generate a single template and use
404+
# configuration-specific file generation.
405+
configure_file(
406+
${_hpx_generate_package_utils_dir}/templates/${template}.pc.in
407+
${OUTPUT_DIR}${template}_configurable.pc.in @ONLY ESCAPE_QUOTES
408+
)
409+
410+
set(config_types ${CMAKE_CONFIGURATION_TYPES})
411+
if(NOT config_types)
412+
set(config_types Debug Release RelWithDebInfo MinSizeRel)
413+
endif()
414+
415+
set(default_config "")
416+
foreach(config_type ${config_types})
417+
string(TOLOWER ${config_type} config_lower)
418+
419+
file(
420+
GENERATE
421+
OUTPUT ${OUTPUT_DIR}/${template}_${config_lower}.pc
422+
INPUT ${OUTPUT_DIR}${template}_configurable.pc.in
423+
CONDITION "$<CONFIG:${config_type}>"
424+
)
425+
426+
# We track a default configuration (preferring Release) to generate the
427+
# legacy .pc file for backward compatibility.
428+
if(NOT default_config OR "${config_lower}" STREQUAL "release")
429+
if(NOT default_config OR NOT "${default_config}" STREQUAL "release")
430+
if("${config_lower}" MATCHES "rel")
431+
set(default_config ${config_type})
432+
endif()
433+
endif()
434+
endif()
435+
endforeach()
436+
437+
if(default_config)
438+
file(
439+
GENERATE
440+
OUTPUT ${OUTPUT_DIR}/${template}.pc
441+
INPUT ${OUTPUT_DIR}${template}_configurable.pc.in
442+
CONDITION "$<CONFIG:${default_config}>"
443+
)
444+
endif()
445+
else()
446+
# Logic for single-config generators
447+
string(TOLOWER ${CMAKE_BUILD_TYPE} build_type)
448+
449+
configure_file(
450+
${_hpx_generate_package_utils_dir}/templates/${template}.pc.in
451+
${OUTPUT_DIR}${template}_${build_type}.pc.in @ONLY ESCAPE_QUOTES
452+
)
400453
401-
configure_file(
402-
cmake/templates/${template}.pc.in
403-
${OUTPUT_DIR}${template}_${build_type}.pc.in @ONLY ESCAPE_QUOTES
404-
)
405-
# Can't use generator expression directly as name of output file (solved in
406-
# CMake 3.20)
407-
file(
408-
GENERATE
409-
OUTPUT ${OUTPUT_DIR}/${template}_${build_type}.pc
410-
INPUT ${OUTPUT_DIR}${template}_${build_type}.pc.in
411-
)
412-
# Temporary (to deprecate gradually)
413-
if("${build_type}" MATCHES "rel")
414454
file(
415455
GENERATE
416-
OUTPUT ${OUTPUT_DIR}/${template}.pc
456+
OUTPUT ${OUTPUT_DIR}/${template}_${build_type}.pc
417457
INPUT ${OUTPUT_DIR}${template}_${build_type}.pc.in
418458
)
459+
460+
if("${build_type}" MATCHES "rel")
461+
file(
462+
GENERATE
463+
OUTPUT ${OUTPUT_DIR}/${template}.pc
464+
INPUT ${OUTPUT_DIR}${template}_${build_type}.pc.in
465+
)
466+
endif()
419467
endif()
420468
421469
endfunction(hpx_generate_pkgconfig_from_target)

tests/unit/build/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,31 @@ if(HPX_WITH_PKGCONFIG AND HPX_WITH_DISTRIBUTED_RUNTIME)
212212
add_hpx_pseudo_dependencies(tests.unit.build tests.unit.build.pkgconfig)
213213
endif()
214214
endif()
215+
216+
function(create_pkgconfig_generation_test name test_dir)
217+
set(build_dir "${CMAKE_CURRENT_BINARY_DIR}/${name}")
218+
add_custom_target(
219+
${name}
220+
COMMAND
221+
"${CMAKE_CTEST_COMMAND}" --build-and-test
222+
"${PROJECT_SOURCE_DIR}/${test_dir}" "${build_dir}" --build-generator
223+
"${CMAKE_GENERATOR}" --build-noclean --build-options
224+
-DHPX_SOURCE_DIR=${PROJECT_SOURCE_DIR} -DCMAKE_BUILD_TYPE=$<CONFIGURATION>
225+
--test-command "${CMAKE_CTEST_COMMAND}" --output-on-failure
226+
VERBATIM
227+
)
228+
229+
if(MSVC)
230+
set_target_properties(${name} PROPERTIES FOLDER "Tests/Unit/Build")
231+
endif()
232+
endfunction()
233+
234+
if(HPX_WITH_PKGCONFIG)
235+
create_pkgconfig_generation_test(
236+
tests.unit.build.pkgconfig_generation
237+
"tests/unit/build/pkgconfig_generation"
238+
)
239+
add_hpx_pseudo_dependencies(
240+
tests.unit.build tests.unit.build.pkgconfig_generation
241+
)
242+
endif()
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Copyright (c) 2025 Pratyksh Gupta
2+
#
3+
# SPDX-License-Identifier: BSL-1.0
4+
# Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
7+
cmake_minimum_required(VERSION 3.20)
8+
project(pkgconfig_generation_test NONE)
9+
10+
set(HPX_COMPONENTS "")
11+
set(CMAKE_INSTALL_LIBDIR "lib")
12+
set(CMAKE_FILES_DIRECTORY "/CMakeFiles")
13+
set(PROJECT_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
14+
set(HPX_SOURCE_DIR "${HPX_SOURCE_DIR}")
15+
16+
macro(hpx_handle_component_dependencies)
17+
18+
endmacro()
19+
20+
include("${HPX_SOURCE_DIR}/cmake/HPX_GeneratePackageUtils.cmake")
21+
22+
add_library(hpx_mock INTERFACE)
23+
set_target_properties(
24+
hpx_mock PROPERTIES INTERFACE_COMPILE_DEFINITIONS "HPX_MOCK_TEST"
25+
INTERFACE_INCLUDE_DIRECTORIES "/usr/include/hpx_mock"
26+
)
27+
28+
hpx_generate_pkgconfig_from_target(hpx_mock hpx_application TRUE)
29+
hpx_generate_pkgconfig_from_target(hpx_mock hpx_application FALSE)
30+
31+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
32+
set(config_types ${CMAKE_CONFIGURATION_TYPES})
33+
if(NOT config_types)
34+
set(config_types Debug Release RelWithDebInfo MinSizeRel)
35+
endif()
36+
37+
string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LC)
38+
39+
set(VERIFY_SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/verify_pc.cmake")
40+
file(
41+
WRITE "${VERIFY_SCRIPT}"
42+
"
43+
set(is_multi_config ${is_multi_config})
44+
set(config_types \"${config_types}\")
45+
set(CMAKE_BUILD_TYPE_LC \"${CMAKE_BUILD_TYPE_LC}\")
46+
47+
set(BUILD_PC_DIR \"${CMAKE_CURRENT_BINARY_DIR}/lib/pkgconfig\")
48+
set(INSTALL_PC_DIR \"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles\")
49+
50+
macro(check_dir dir_path)
51+
if(is_multi_config)
52+
foreach(config \${config_types})
53+
string(TOLOWER \${config} config_lc)
54+
set(pc_file \"\${dir_path}/hpx_application_\${config_lc}.pc\")
55+
if(NOT EXISTS \"\${pc_file}\")
56+
message(FATAL_ERROR \"Multi-config: Failed to generate pkg-config file for \${config}: \${pc_file}\")
57+
endif()
58+
59+
file(READ \"\${pc_file}\" content)
60+
if(NOT content MATCHES \"HPX_MOCK_TEST\")
61+
message(FATAL_ERROR \"Content verification failed for \${pc_file}: Missing HPX_MOCK_TEST\")
62+
endif()
63+
message(STATUS \"Verified: \${pc_file}\")
64+
endforeach()
65+
66+
if(EXISTS \"\${dir_path}/hpx_application.pc\")
67+
file(READ \"\${dir_path}/hpx_application.pc\" content)
68+
if(NOT content MATCHES \"HPX_MOCK_TEST\")
69+
message(FATAL_ERROR \"Content verification failed for legacy \${dir_path}/hpx_application.pc\")
70+
endif()
71+
message(STATUS \"Verified legacy: hpx_application.pc in \${dir_path}\")
72+
endif()
73+
else()
74+
set(pc_file \"\${dir_path}/hpx_application_\${CMAKE_BUILD_TYPE_LC}.pc\")
75+
if(NOT EXISTS \"\${pc_file}\")
76+
message(FATAL_ERROR \"Single-config: Failed to generate pkg-config file for \${CMAKE_BUILD_TYPE_LC}: \${pc_file}\")
77+
endif()
78+
79+
file(READ \"\${pc_file}\" content)
80+
if(NOT content MATCHES \"HPX_MOCK_TEST\")
81+
message(FATAL_ERROR \"Content verification failed for \${pc_file}\")
82+
endif()
83+
message(STATUS \"Verified: \${pc_file}\")
84+
85+
if(\"\${CMAKE_BUILD_TYPE_LC}\" MATCHES \"rel\")
86+
if(EXISTS \"\${dir_path}/hpx_application.pc\")
87+
message(STATUS \"Verified legacy: hpx_application.pc in \${dir_path}\")
88+
else()
89+
message(FATAL_ERROR \"Legacy pkg-config file missing for release-like build in \${dir_path}\")
90+
endif()
91+
endif()
92+
endif()
93+
endmacro()
94+
95+
message(STATUS \"Checking build directory pkg-config files...\")
96+
check_dir(\"\${BUILD_PC_DIR}\")
97+
message(STATUS \"Checking simulated install directory pkg-config files...\")
98+
check_dir(\"\${INSTALL_PC_DIR}\")
99+
"
100+
)
101+
102+
add_custom_target(check_pc ALL COMMAND ${CMAKE_COMMAND} -P "${VERIFY_SCRIPT}")

0 commit comments

Comments
 (0)