Skip to content

Commit

Permalink
Improved FORCE_SYSTEM_<content-name> behavior
Browse files Browse the repository at this point in the history
Improve the behavior of hfc_make_available_single when FORCE_SYSTEM_<content-name> is set for a given dependency by forcing find_xxx to use the system root for its search. Additionally the system was made more reliable by ensuring that a proxy toolchain is generate to forward necessary project information in addition to enabling the same CMAKE_LANGUAGEs as the invoking project

Co-authored-by: Damien Buhl <damien@tipi.build>

Change-Id: Ia55263361f376fb40a7c4b66dc559c7bbc931feb
  • Loading branch information
pysco68 committed Jan 30, 2025
1 parent d96218a commit 06f8bb6
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmake/modules/hfc_autotools_restore_or_configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function(hfc_autotools_restore_or_configure content_name)
cmake_language(CALL ${FN_ARG_CMAKE_ADAPTER_GENERATOR_FN}_get_install_target install_target_name)

# Toolchain to forward arguments
set(proxy_toolchain_path "${HERMETIC_FETCHCONTENT_INSTALL_DIR}/${content_name}-toolchain/hfc_hermetic_proxy_toolchain.cmake")
hfc_get_content_proxy_toolchain_path(${content_name} proxy_toolchain_path)
hfc_generate_cmake_proxy_toolchain(${content_name}
# PROJECT_DEPENDENCIES not passed : Autotools cannot have dependencies, it doesn't "cmake-find_package"
PROJECT_TOOLCHAIN_EXTENSION "${FN_ARG_PROJECT_TOOLCHAIN_EXTENSION}"
Expand Down
5 changes: 2 additions & 3 deletions cmake/modules/hfc_cmake_restore_or_configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ function(hfc_cmake_restore_or_configure content_name)
)
endif()

set(proxy_toolchain_dir "${HERMETIC_FETCHCONTENT_INSTALL_DIR}/${content_name}-toolchain")
set(proxy_toolchain_path "${HERMETIC_FETCHCONTENT_INSTALL_DIR}/${content_name}-toolchain/hfc_hermetic_proxy_toolchain.cmake")

hfc_get_content_proxy_toolchain_dir(${content_name} proxy_toolchain_dir)
hfc_get_content_proxy_toolchain_path(${content_name} proxy_toolchain_path)
file(LOCK ${proxy_toolchain_dir} DIRECTORY GUARD FUNCTION)

hfc_generate_cmake_proxy_toolchain(${content_name}
Expand Down
10 changes: 10 additions & 0 deletions cmake/modules/hfc_generate_cmake_proxy_toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ include_guard()
include(hfc_log)
include(hfc_targets_cache_common)

function(hfc_get_content_proxy_toolchain_dir content_name OUT_result)
set(${OUT_result} "${FETCHCONTENT_BASE_DIR}/${content_name}-toolchain" PARENT_SCOPE)
endfunction()

function(hfc_get_content_proxy_toolchain_path content_name OUT_result)
hfc_get_content_proxy_toolchain_dir("${content_name}" proxy_toolchain_dir)
set(${OUT_result} "${proxy_toolchain_dir}/hfc_hermetic_proxy_toolchain.cmake" PARENT_SCOPE)
endfunction()


#[=======================================================================[.rst:
hfc_generate_cmake_proxy_toolchain
------------------------------------------------------------------------------------------
Expand Down
22 changes: 22 additions & 0 deletions cmake/modules/hfc_make_available_single.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ include(hfc_cmake_restore_or_configure)
include(hfc_saved_details)
include(hfc_determine_cache_id)
include(hfc_populate_project)
include(hfc_generate_cmake_proxy_toolchain)
include(hfc_targets_cache_create)

# This creates prefixes where HermeticFetchContent will reuse buildLocation or installed location
function(hfc_create_restore_prefixes content_name buildLocation installedLocation)
Expand Down Expand Up @@ -148,13 +150,33 @@ function(hfc_make_available_single content_name build_at_configure_time)

if (DEFINED FORCE_SYSTEM_${content_name})
if (${FORCE_SYSTEM_${content_name}})

hfc_log(WARNING "FORCE_SYSTEM_${content_name} is ON, HermeticFetchContent will only find_package the library on the system.")

hfc_get_content_proxy_toolchain_path(${content_name} proxy_toolchain_path)

string(APPEND __PARAMS_HERMETIC_TOOLCHAIN_EXTENSION
[=[
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE NEVER)
]=]
)

hfc_generate_cmake_proxy_toolchain(${content_name}
HERMETIC_FIND_PACKAGES "${__PARAMS_HERMETIC_FIND_PACKAGES}"
PROJECT_TOOLCHAIN_EXTENSION "${__PARAMS_HERMETIC_TOOLCHAIN_EXTENSION}"
DESTINATION_TOOLCHAIN_PATH "${proxy_toolchain_path}"
)

get_hermetic_target_cache_file_path(${content_name} target_cache_file)
hfc_targets_cache_create_isolated(
${content_name}
LOAD_TARGETS_CMAKE "[==[find_package(${content_name} REQUIRED ${__PARAMS_FIND_PACKAGE_ARGS} ) \n]==]"
CACHE_DESTINATION_FILE "${target_cache_file}"
TEMP_DIR "${HERMETIC_FETCHCONTENT_INSTALL_DIR}/targets_dump_tmp"
TOOLCHAIN_FILE ${proxy_toolchain_path}
)

# Now load the targets in our context
Expand Down
11 changes: 10 additions & 1 deletion cmake/modules/hfc_targets_cache_create.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ endfunction()
# LOAD_TARGETS_CMAKE <cmake code to eval for loading targets>
# CACHE_DESTINATION_FILE <path> # path to the target info cache to write. Parent directory will be created if not already present. Destination file will be overwritten if present
# )
function(hfc_targets_cache_create_isolated)
function(hfc_targets_cache_create_isolated content_name)

# arguments parsing
set(options "")
Expand All @@ -252,11 +252,19 @@ function(hfc_targets_cache_create_isolated)

file(MAKE_DIRECTORY "${tmp_proj_dir}")

# make sure we have the same enabled languages if the FORCE_SYSTEM_<content-name>
set(TEMPLATE_ENABLE_LANGUAGES "NONE")
if(FORCE_SYSTEM_${content_name})
get_property(languages GLOBAL PROPERTY ENABLED_LANGUAGES)
set(TEMPLATE_ENABLE_LANGUAGES ${languages})
endif()

block(SCOPE_FOR VARIABLES PROPAGATE
HERMETIC_FETCHCONTENT_ROOT_DIR
FN_ARG_LOAD_TARGETS_CMAKE
FN_ARG_CACHE_DESTINATION_FILE
FN_ARG_CREATE_TARGET_ALIASES
TEMPLATE_ENABLE_LANGUAGES
)
set(HERMETIC_FETCHCONTENT_ROOT_DIR "${HERMETIC_FETCHCONTENT_ROOT_DIR}")
set(LOAD_TARGETS_CMAKE "${FN_ARG_LOAD_TARGETS_CMAKE}")
Expand Down Expand Up @@ -317,6 +325,7 @@ function(hfc_targets_cache_create_from_export_declaration content_name)
hfc_log_debug("Generating targets cache for ${content_name} from export declaration at ${${FN_ARG_OUT_TARGETS_CACHE_FILE}}")

hfc_targets_cache_create_isolated(
${content_name}
LOAD_TARGETS_CMAKE "[==[${FN_ARG_CMAKE_EXPORT_LIBRARY_DECLARATION}]==]"
TOOLCHAIN_FILE ${FN_ARG_TOOLCHAIN_FILE}
CACHE_DESTINATION_FILE "${${FN_ARG_OUT_TARGETS_CACHE_FILE}}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.27.0)
project(create_imported_cmake_targets_cache LANGUAGES NONE)
project(create_imported_cmake_targets_cache LANGUAGES @TEMPLATE_ENABLE_LANGUAGES@)

list(APPEND CMAKE_MODULE_PATH "@HERMETIC_FETCHCONTENT_ROOT_DIR@")
list(APPEND CMAKE_MODULE_PATH "@HERMETIC_FETCHCONTENT_ROOT_DIR@/modules")
Expand Down
1 change: 1 addition & 0 deletions test/check_openssl_build_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace hfc::test {
BOOST_REQUIRE(!fs::exists(test_project_path / "thirdparty" / "cache" / "openssl-src"));
BOOST_REQUIRE(!fs::exists(test_project_path / "build" / "_deps" / "OpenSSL-install" / "lib" / "libssl.a" ));
BOOST_REQUIRE(!fs::exists(test_project_path / "build" / "_deps" / "OpenSSL-install" / "lib" / "libcrypto.a" ));
BOOST_REQUIRE(fs::exists(test_project_path / "build" / "_deps" / "hermetic_targetcaches" / "OpenSSL.cmake"));

std::string ninja_output = run_command(cmake_build_command, test_project_path);
BOOST_REQUIRE(boost::contains(ninja_output, "ninja: no work to do"));
Expand Down

0 comments on commit 06f8bb6

Please sign in to comment.