Skip to content

Commit

Permalink
Compiler detection: Cache compiler hash (#42994)
Browse files Browse the repository at this point in the history
  • Loading branch information
autoantwort authored Jan 16, 2025
1 parent 7894bc9 commit 96dbba1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
41 changes: 39 additions & 2 deletions scripts/detect_compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
3 changes: 3 additions & 0 deletions scripts/detect_compiler/portfile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 96dbba1

Please sign in to comment.