Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler detection: Cache compiler hash #42994

Merged
merged 4 commits into from
Jan 16, 2025
Merged
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
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 @@ -15,9 +15,12 @@

set(VCPKG_BUILD_TYPE release)

vcpkg_configure_cmake(

Check warning on line 18 in scripts/detect_compiler/portfile.cmake

View workflow job for this annotation

GitHub Actions / Check

The function vcpkg_configure_cmake is deprecated. Please use vcpkg_cmake_configure (Please remove the option PREFER_NINJA) (from port vcpkg-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
Loading