Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmake/modules/hfc_cmake_targets_cache.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ function(hfc_cmake_targets_cache_isolated)
set(HERMETIC_FETCHCONTENT_CMAKE_ADDITIONAL_EXPORTS "${FN_ARG_CMAKE_ADDITIONAL_EXPORTS}")
set(HERMETIC_FETCHCONTENT_TARGETS_FILE_PATTERN "${FN_ARG_TARGETS_FILE_PATTERN}")

# forward the ENABLED_LANGUAGES information to the dump project
# so that libraries forced to be found on system can take that
# info into account
set(TEMPLATE_ENABLE_LANGUAGES "NONE")
if(FORCE_SYSTEM_${content_name})
get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
set(TEMPLATE_ENABLE_LANGUAGES ${languages})
endif()

string(RANDOM LENGTH 10 rand_str)
set(tmp_proj_dir "${FN_ARG_TEMP_DIR}/tmp_${rand_str}")

Expand Down
1 change: 1 addition & 0 deletions cmake/modules/hfc_make_available_single.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ function(hfc_make_available_single content_name build_at_configure_time)
${content_name}
LOAD_TARGETS_CMAKE "[==[find_package(${content_name} REQUIRED ${__PARAMS_FIND_PACKAGE_ARGS} ) \n]==]"
CACHE_DESTINATION_FILE "${target_cache_file}"
CREATE_TARGET_ALIASES "${__PARAMS_HERMETIC_CREATE_TARGET_ALIASES}"
TEMP_DIR "${HERMETIC_FETCHCONTENT_INSTALL_DIR}/targets_dump_tmp"
TOOLCHAIN_FILE ${proxy_toolchain_path}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function(create_target_aliases target_name OUT_aliases_name)

block(PROPAGATE TARGET_NAME TARGET_ALIASES)
# injected code
@TEMPLATE_CREATE_TARGET_ALIASES@
@CREATE_TARGET_ALIASES@

endblock()

Expand Down
2 changes: 1 addition & 1 deletion cmake/templates/dump_build_targets.CMakeLists.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


cmake_minimum_required(VERSION 3.27.0)
project(dump_build_targets LANGUAGES NONE)
project(dump_build_targets LANGUAGES @TEMPLATE_ENABLE_LANGUAGES@)


list(APPEND CMAKE_MODULE_PATH "@HERMETIC_FETCHCONTENT_ROOT_DIR@")
Expand Down
8 changes: 7 additions & 1 deletion test/targets_cache_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ namespace hfc::test {
targets_cache_test_data_set{ "hfc_targets_cache/autotools_export_declaration", "Iconv.cmake", true },
targets_cache_test_data_set{ "hfc_targets_cache/cmake_no_install", "mathslib.cmake", false },
targets_cache_test_data_set{ "hfc_targets_cache/alternate_exports_naming", "mathslib.cmake", true },
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 */ }
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 */ },

// aliasing tests
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" },
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
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" },
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
};

static auto TEST_DATA_hfc_makeAvailableAt_type = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#
# Base hfc targets cache functionality test
# ensures that packages and properties can be read from package config exports
#

cmake_minimum_required(VERSION 3.27.6)
project(
ModernCMakeExample
VERSION 1.0
LANGUAGES CXX)


set(CMAKE_MODULE_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/cmake"
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
${CMAKE_MODULE_PATH}
)

include(FetchContent)
include(HermeticFetchContent)


if(HFC_TEST_RENAMETARGET)
set(mathlib_alias_fn
[==[
if("${TARGET_NAME}" STREQUAL "MathFunctionscbrt::MathFunctionscbrt")
set(TARGET_ALIASES "RenamedMathFunctionscbrt::MathFunctionscbrt") # rename
endif()
]==]
)
else()
set(mathlib_alias_fn
[==[
if("${TARGET_NAME}" STREQUAL "MathFunctionscbrt::MathFunctionscbrt")
list(APPEND TARGET_ALIASES "MathFunctionscbrt::MathFunctionscbrt" "AliasedMathFunctionscbrt::MathFunctionscbrt") # alias
endif()
]==]
)
endif()

FetchContent_Declare(
mathslib
GIT_REPOSITORY "https://github.com/tipi-build/unit-test-cmake-template-2libs.git"
GIT_TAG "ecc756a4c3f1811cdfd637bd6d8f4e3feb6aff92"
)

FetchContent_MakeHermetic(mathslib
HERMETIC_CREATE_TARGET_ALIASES "${mathlib_alias_fn}"
)


if(HFC_TEST_FORCE_SYSTEM_RENAME_TARGET)
set(iconv_alias_fn
[==[
if("${TARGET_NAME}" STREQUAL "Iconv::Iconv")
set(TARGET_ALIASES "RenamedIconv::Iconv") # rename
endif()
]==]
)
else()
set(iconv_alias_fn
[==[
if("${TARGET_NAME}" STREQUAL "Iconv::Iconv")
list(APPEND TARGET_ALIASES "Iconv::Iconv" "AliasedIconv::Iconv") # alias
endif()
]==]
)
endif()

set(FORCE_SYSTEM_Iconv ON)

FetchContent_Declare(
Iconv
GIT_REPOSITORY https://github.com/tipi-build/unittest-autotools-sample.git
GIT_TAG ad80b024eeda8f4c0a96eedf669dc453ed33a094
)

FetchContent_MakeHermetic(
Iconv
HERMETIC_CMAKE_EXPORT_LIBRARY_DECLARATION
[=[
message(FATAL_ERROR "This bit of code should not have been executed because we FORCE_SYSTEM_Iconv")
]=]
HERMETIC_CREATE_TARGET_ALIASES "${iconv_alias_fn}"
HERMETIC_BUILD_SYSTEM autotools
)

if(HFCTEST_CONFIGURETIME_DEPENDENCY AND HFCTEST_BUILDTIME_DEPENDENCY)
set(param_error ON)
else()
if(HFCTEST_CONFIGURETIME_DEPENDENCY)
HermeticFetchContent_MakeAvailableAtConfigureTime(mathslib)
HermeticFetchContent_MakeAvailableAtConfigureTime(Iconv)
elseif(HFCTEST_BUILDTIME_DEPENDENCY)
HermeticFetchContent_MakeAvailableAtBuildTime(mathslib)
HermeticFetchContent_MakeAvailableAtBuildTime(Iconv)
else()
set(param_error ON)
endif()
endif()

if(param_error)
message(FATAL_ERROR "This project needs either HFCTEST_CONFIGURETIME_DEPENDENCY or HFCTEST_BUILDTIME_DEPENDENCY defined")
endif()


set(exampleTargetLinkLibs "MathFunctions::MathFunctions") # this one should always exist as we don't alias/rename it (we do that only for MathFunctionscbrt::MathFunctionscbrt)


# check for expected targets of our mathlib content
if(HFC_TEST_RENAMETARGET)
list(APPEND exampleTargetLinkLibs "RenamedMathFunctionscbrt::MathFunctionscbrt")

if(TARGET MathFunctionscbrt::MathFunctionscbrt)
message(FATAL_ERROR "Found target MathFunctionscbrt::MathFunctionscbrt which should not exist")
endif()
else()
list(APPEND exampleTargetLinkLibs "AliasedMathFunctionscbrt::MathFunctionscbrt")

if(NOT TARGET MathFunctionscbrt::MathFunctionscbrt)
message(FATAL_ERROR "Didn't find target MathFunctionscbrt::MathFunctionscbrt which should still exist despite aliasing")
endif()
endif()

# check for expected targets of our Iconv content
if(HFC_TEST_FORCE_SYSTEM_RENAME_TARGET)
list(APPEND exampleTargetLinkLibs "RenamedIconv::Iconv")

if(TARGET Iconv::Iconv)
message(FATAL_ERROR "Found target Iconv::Iconv which should not exist")
endif()

else()

list(APPEND exampleTargetLinkLibs "AliasedIconv::Iconv")

if(NOT TARGET Iconv::Iconv)
message(FATAL_ERROR "Didn't find target Iconv::Iconv which should still exist despite aliasing")
endif()

endif()


add_executable(MyExample simple_example.cpp)
target_link_libraries(MyExample PRIVATE ${exampleTargetLinkLibs})


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <MathFunctions.h>

int main() {
return MathFunctions::sqrt(4.0) == 2.0;
}