Skip to content

Commit 2a39a3c

Browse files
committed
Enabling FORCE_SYSTEM_<content-name> to use CREATE_TARGET_ALIASES facility
Added tests for the target aliasing functionality. Forwarding the ENABLE_LANGUAGES information from the project through to the evaluation contexts where the information is required. CHANGELOG - HFC now executes the specified CREATE_TARGET_ALIASES function when FORCE_SYSTEM_<content-name>=ON Change-Id: If8054665bc5786f4eb12891d16c975d3f4da1cdc
1 parent 2229c6f commit 2a39a3c

File tree

7 files changed

+171
-3
lines changed

7 files changed

+171
-3
lines changed

cmake/modules/hfc_cmake_targets_cache.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ function(hfc_cmake_targets_cache_isolated)
106106
set(HERMETIC_FETCHCONTENT_CMAKE_ADDITIONAL_EXPORTS "${FN_ARG_CMAKE_ADDITIONAL_EXPORTS}")
107107
set(HERMETIC_FETCHCONTENT_TARGETS_FILE_PATTERN "${FN_ARG_TARGETS_FILE_PATTERN}")
108108

109+
# forward the ENABLED_LANGUAGES information to the dump project
110+
# so that libraries forced to be found on system can take that
111+
# info into account
112+
set(TEMPLATE_ENABLE_LANGUAGES "NONE")
113+
if(FORCE_SYSTEM_${content_name})
114+
get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
115+
set(TEMPLATE_ENABLE_LANGUAGES ${languages})
116+
endif()
117+
109118
string(RANDOM LENGTH 10 rand_str)
110119
set(tmp_proj_dir "${FN_ARG_TEMP_DIR}/tmp_${rand_str}")
111120

cmake/modules/hfc_make_available_single.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ function(hfc_make_available_single content_name build_at_configure_time)
196196
${content_name}
197197
LOAD_TARGETS_CMAKE "[==[find_package(${content_name} REQUIRED ${__PARAMS_FIND_PACKAGE_ARGS} ) \n]==]"
198198
CACHE_DESTINATION_FILE "${target_cache_file}"
199+
CREATE_TARGET_ALIASES "${__PARAMS_HERMETIC_CREATE_TARGET_ALIASES}"
199200
TEMP_DIR "${HERMETIC_FETCHCONTENT_INSTALL_DIR}/targets_dump_tmp"
200201
TOOLCHAIN_FILE ${proxy_toolchain_path}
201202
)

cmake/templates/create_imported_cmake_targets_cache.CMakeLists.txt.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function(create_target_aliases target_name OUT_aliases_name)
1414

1515
block(PROPAGATE TARGET_NAME TARGET_ALIASES)
1616
# injected code
17-
@TEMPLATE_CREATE_TARGET_ALIASES@
17+
@CREATE_TARGET_ALIASES@
1818

1919
endblock()
2020

cmake/templates/dump_build_targets.CMakeLists.txt.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
cmake_minimum_required(VERSION 3.27.0)
16-
project(dump_build_targets LANGUAGES NONE)
16+
project(dump_build_targets LANGUAGES @TEMPLATE_ENABLE_LANGUAGES@)
1717

1818

1919
list(APPEND CMAKE_MODULE_PATH "@HERMETIC_FETCHCONTENT_ROOT_DIR@")

test/targets_cache_test.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ namespace hfc::test {
5656
targets_cache_test_data_set{ "hfc_targets_cache/autotools_export_declaration", "Iconv.cmake", true },
5757
targets_cache_test_data_set{ "hfc_targets_cache/cmake_no_install", "mathslib.cmake", false },
5858
targets_cache_test_data_set{ "hfc_targets_cache/alternate_exports_naming", "mathslib.cmake", true },
59-
targets_cache_test_data_set{ "hfc_targets_cache/alternate_exports_naming", "mathslib.cmake", false, false /* expect configure failure */, false, "-DHFCTEST_NEGATIVE_CASE=ON" /* enable the negative test in the project */ }
59+
targets_cache_test_data_set{ "hfc_targets_cache/alternate_exports_naming", "mathslib.cmake", false, false /* expect configure failure */, false, "-DHFCTEST_NEGATIVE_CASE=ON" /* enable the negative test in the project */ },
60+
61+
// aliasing tests
62+
targets_cache_test_data_set{ "hfc_targets_cache/target_aliasing", "mathslib.cmake", true, true , true /* expect build and configure success */, "-D=HFC_TEST_RENAMETARGET=OFF" },
63+
targets_cache_test_data_set{ "hfc_targets_cache/target_aliasing", "mathslib.cmake", true, true , true /* expect build and configure success */, "-D=HFC_TEST_RENAMETARGET=ON" }, // rename instead of just adding an alias
64+
targets_cache_test_data_set{ "hfc_targets_cache/target_aliasing", "mathslib.cmake", true, true , true /* expect build and configure success */, "-D=HFC_TEST_FORCE_SYSTEM_RENAME_TARGET=OFF" },
65+
targets_cache_test_data_set{ "hfc_targets_cache/target_aliasing", "mathslib.cmake", true, true , true /* expect build and configure success */, "-D=HFC_TEST_FORCE_SYSTEM_RENAME_TARGET=ON" }, // rename instead of just adding an alias
6066
};
6167

6268
static auto TEST_DATA_hfc_makeAvailableAt_type = {
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#
2+
# Base hfc targets cache functionality test
3+
# ensures that packages and properties can be read from package config exports
4+
#
5+
6+
cmake_minimum_required(VERSION 3.27.6)
7+
project(
8+
ModernCMakeExample
9+
VERSION 1.0
10+
LANGUAGES CXX)
11+
12+
13+
set(CMAKE_MODULE_PATH
14+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake"
15+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
16+
${CMAKE_MODULE_PATH}
17+
)
18+
19+
include(FetchContent)
20+
include(HermeticFetchContent)
21+
22+
23+
if(HFC_TEST_RENAMETARGET)
24+
set(mathlib_alias_fn
25+
[==[
26+
if("${TARGET_NAME}" STREQUAL "MathFunctionscbrt::MathFunctionscbrt")
27+
set(TARGET_ALIASES "RenamedMathFunctionscbrt::MathFunctionscbrt") # rename
28+
endif()
29+
]==]
30+
)
31+
else()
32+
set(mathlib_alias_fn
33+
[==[
34+
if("${TARGET_NAME}" STREQUAL "MathFunctionscbrt::MathFunctionscbrt")
35+
list(APPEND TARGET_ALIASES "MathFunctionscbrt::MathFunctionscbrt" "AliasedMathFunctionscbrt::MathFunctionscbrt") # alias
36+
endif()
37+
]==]
38+
)
39+
endif()
40+
41+
FetchContent_Declare(
42+
mathslib
43+
GIT_REPOSITORY "https://github.com/tipi-build/unit-test-cmake-template-2libs.git"
44+
GIT_TAG "ecc756a4c3f1811cdfd637bd6d8f4e3feb6aff92"
45+
)
46+
47+
FetchContent_MakeHermetic(mathslib
48+
HERMETIC_CREATE_TARGET_ALIASES "${mathlib_alias_fn}"
49+
)
50+
51+
52+
if(HFC_TEST_FORCE_SYSTEM_RENAME_TARGET)
53+
set(iconv_alias_fn
54+
[==[
55+
if("${TARGET_NAME}" STREQUAL "Iconv::Iconv")
56+
set(TARGET_ALIASES "RenamedIconv::Iconv") # rename
57+
endif()
58+
]==]
59+
)
60+
else()
61+
set(iconv_alias_fn
62+
[==[
63+
if("${TARGET_NAME}" STREQUAL "Iconv::Iconv")
64+
list(APPEND TARGET_ALIASES "Iconv::Iconv" "AliasedIconv::Iconv") # alias
65+
endif()
66+
]==]
67+
)
68+
endif()
69+
70+
set(FORCE_SYSTEM_Iconv ON)
71+
72+
FetchContent_Declare(
73+
Iconv
74+
GIT_REPOSITORY https://github.com/tipi-build/unittest-autotools-sample.git
75+
GIT_TAG ad80b024eeda8f4c0a96eedf669dc453ed33a094
76+
)
77+
78+
FetchContent_MakeHermetic(
79+
Iconv
80+
HERMETIC_CMAKE_EXPORT_LIBRARY_DECLARATION
81+
[=[
82+
message(FATAL_ERROR "This bit of code should not have been executed because we FORCE_SYSTEM_Iconv")
83+
]=]
84+
HERMETIC_CREATE_TARGET_ALIASES "${iconv_alias_fn}"
85+
HERMETIC_BUILD_SYSTEM autotools
86+
)
87+
88+
if(HFCTEST_CONFIGURETIME_DEPENDENCY AND HFCTEST_BUILDTIME_DEPENDENCY)
89+
set(param_error ON)
90+
else()
91+
if(HFCTEST_CONFIGURETIME_DEPENDENCY)
92+
HermeticFetchContent_MakeAvailableAtConfigureTime(mathslib)
93+
HermeticFetchContent_MakeAvailableAtConfigureTime(Iconv)
94+
elseif(HFCTEST_BUILDTIME_DEPENDENCY)
95+
HermeticFetchContent_MakeAvailableAtBuildTime(mathslib)
96+
HermeticFetchContent_MakeAvailableAtBuildTime(Iconv)
97+
else()
98+
set(param_error ON)
99+
endif()
100+
endif()
101+
102+
if(param_error)
103+
message(FATAL_ERROR "This project needs either HFCTEST_CONFIGURETIME_DEPENDENCY or HFCTEST_BUILDTIME_DEPENDENCY defined")
104+
endif()
105+
106+
107+
set(exampleTargetLinkLibs "MathFunctions::MathFunctions") # this one should always exist as we don't alias/rename it (we do that only for MathFunctionscbrt::MathFunctionscbrt)
108+
109+
110+
# check for expected targets of our mathlib content
111+
if(HFC_TEST_RENAMETARGET)
112+
list(APPEND exampleTargetLinkLibs "RenamedMathFunctionscbrt::MathFunctionscbrt")
113+
114+
if(TARGET MathFunctionscbrt::MathFunctionscbrt)
115+
message(FATAL_ERROR "Found target MathFunctionscbrt::MathFunctionscbrt which should not exist")
116+
endif()
117+
else()
118+
list(APPEND exampleTargetLinkLibs "AliasedMathFunctionscbrt::MathFunctionscbrt")
119+
120+
if(NOT TARGET MathFunctionscbrt::MathFunctionscbrt)
121+
message(FATAL_ERROR "Didn't find target MathFunctionscbrt::MathFunctionscbrt which should still exist despite aliasing")
122+
endif()
123+
endif()
124+
125+
# check for expected targets of our Iconv content
126+
if(HFC_TEST_FORCE_SYSTEM_RENAME_TARGET)
127+
list(APPEND exampleTargetLinkLibs "RenamedIconv::Iconv")
128+
129+
if(TARGET Iconv::Iconv)
130+
message(FATAL_ERROR "Found target Iconv::Iconv which should not exist")
131+
endif()
132+
133+
else()
134+
135+
list(APPEND exampleTargetLinkLibs "AliasedIconv::Iconv")
136+
137+
if(NOT TARGET Iconv::Iconv)
138+
message(FATAL_ERROR "Didn't find target Iconv::Iconv which should still exist despite aliasing")
139+
endif()
140+
141+
endif()
142+
143+
144+
add_executable(MyExample simple_example.cpp)
145+
target_link_libraries(MyExample PRIVATE ${exampleTargetLinkLibs})
146+
147+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <MathFunctions.h>
2+
3+
int main() {
4+
return MathFunctions::sqrt(4.0) == 2.0;
5+
}

0 commit comments

Comments
 (0)