From 96dbba1e03d1ecc7c7096e97d5a09ff3c58e21df Mon Sep 17 00:00:00 2001 From: autoantwort <41973254+autoantwort@users.noreply.github.com> Date: Thu, 16 Jan 2025 09:26:51 +0100 Subject: [PATCH] Compiler detection: Cache compiler hash (#42994) --- scripts/detect_compiler/CMakeLists.txt | 41 ++++++++++++++++++++++++-- scripts/detect_compiler/portfile.cmake | 3 ++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/scripts/detect_compiler/CMakeLists.txt b/scripts/detect_compiler/CMakeLists.txt index db7973e7b73d30..d695a9e1210834 100644 --- a/scripts/detect_compiler/CMakeLists.txt +++ b/scripts/detect_compiler/CMakeLists.txt @@ -11,8 +11,45 @@ endif() enable_language(C) enable_language(CXX) -file(SHA1 "${CMAKE_CXX_COMPILER}" CXX_HASH) -file(SHA1 "${CMAKE_C_COMPILER}" C_HASH) +if(VCPKG_COMPILER_CACHE_FILE) + if(EXISTS "${VCPKG_COMPILER_CACHE_FILE}") + file(READ "${VCPKG_COMPILER_CACHE_FILE}" JSON_CONTENT) + else() + set(JSON_CONTENT "{}") + endif() + + function(get_hash compiler_path out_var) + file(TO_CMAKE_PATH "${compiler_path}" "compiler_path") + file(SIZE "${compiler_path}" SIZE) + file(TIMESTAMP "${compiler_path}" TIMESTAMP "%s" UTC) + + string(JSON COMPILER_EXISTS ERROR_VARIABLE JSON_ERROR GET "${JSON_CONTENT}" "${compiler_path}") + if(NOT JSON_ERROR) + # Get compiler attributes using JSON API + string(JSON SIZE_JSON GET "${JSON_CONTENT}" "${compiler_path}" "size") + string(JSON TIMESTAMP_JSON GET "${JSON_CONTENT}" "${compiler_path}" "timestamp") + string(JSON HASH_JSON GET "${JSON_CONTENT}" "${compiler_path}" "hash") + if ((SIZE_JSON EQUAL SIZE) AND (TIMESTAMP_JSON EQUAL TIMESTAMP)) + set("${out_var}" "${HASH_JSON}" PARENT_SCOPE) + return() + endif() + endif() + file(SHA1 "${compiler_path}" HASH) + # Add new entry to JSON + string(JSON JSON_CONTENT SET "${JSON_CONTENT}" "${compiler_path}" "{\"size\": ${SIZE}, \"timestamp\": ${TIMESTAMP}, \"hash\": \"${HASH}\"}") + set("${out_var}" "${HASH}" PARENT_SCOPE) + set(JSON_CONTENT "${JSON_CONTENT}" PARENT_SCOPE) + endfunction() + + get_hash("${CMAKE_C_COMPILER}" C_HASH) + get_hash("${CMAKE_CXX_COMPILER}" CXX_HASH) + + # Write updated JSON back to file + file(WRITE "${VCPKG_COMPILER_CACHE_FILE}" "${JSON_CONTENT}") +else() + file(SHA1 "${CMAKE_CXX_COMPILER}" CXX_HASH) + file(SHA1 "${CMAKE_C_COMPILER}" C_HASH) +endif() string(SHA1 COMPILER_HASH "${C_HASH}${CXX_HASH}") message("#COMPILER_HASH#${COMPILER_HASH}") diff --git a/scripts/detect_compiler/portfile.cmake b/scripts/detect_compiler/portfile.cmake index 4f68faea46936b..f4d6d9c3da4bbf 100644 --- a/scripts/detect_compiler/portfile.cmake +++ b/scripts/detect_compiler/portfile.cmake @@ -18,6 +18,9 @@ set(VCPKG_BUILD_TYPE release) vcpkg_configure_cmake( SOURCE_PATH "${CMAKE_CURRENT_LIST_DIR}" PREFER_NINJA + OPTIONS + "-DVCPKG_COMPILER_CACHE_FILE=${VCPKG_COMPILER_CACHE_FILE}" + ) foreach(LOG IN LISTS LOGS)