@@ -11,8 +11,45 @@ endif()
11
11
enable_language (C)
12
12
enable_language (CXX)
13
13
14
- file (SHA1 "${CMAKE_CXX_COMPILER} " CXX_HASH)
15
- file (SHA1 "${CMAKE_C_COMPILER} " C_HASH)
14
+ if (VCPKG_COMPILER_CACHE_FILE)
15
+ if (EXISTS "${VCPKG_COMPILER_CACHE_FILE} " )
16
+ file (READ "${VCPKG_COMPILER_CACHE_FILE} " JSON_CONTENT)
17
+ else ()
18
+ set (JSON_CONTENT "{}" )
19
+ endif ()
20
+
21
+ function (get_hash compiler_path out_var)
22
+ file (TO_CMAKE_PATH "${compiler_path} " "compiler_path" )
23
+ file (SIZE "${compiler_path} " SIZE)
24
+ file (TIMESTAMP "${compiler_path} " TIMESTAMP "%s" UTC)
25
+
26
+ string (JSON COMPILER_EXISTS ERROR_VARIABLE JSON_ERROR GET "${JSON_CONTENT} " "${compiler_path} " )
27
+ if (NOT JSON_ERROR)
28
+ # Get compiler attributes using JSON API
29
+ string (JSON SIZE_JSON GET "${JSON_CONTENT} " "${compiler_path} " "size" )
30
+ string (JSON TIMESTAMP_JSON GET "${JSON_CONTENT} " "${compiler_path} " "timestamp" )
31
+ string (JSON HASH_JSON GET "${JSON_CONTENT} " "${compiler_path} " "hash" )
32
+ if ((SIZE_JSON EQUAL SIZE) AND (TIMESTAMP_JSON EQUAL TIMESTAMP))
33
+ set ("${out_var} " "${HASH_JSON} " PARENT_SCOPE)
34
+ return ()
35
+ endif ()
36
+ endif ()
37
+ file (SHA1 "${compiler_path} " HASH)
38
+ # Add new entry to JSON
39
+ string (JSON JSON_CONTENT SET "${JSON_CONTENT} " "${compiler_path} " "{\" size\" : ${SIZE} , \" timestamp\" : ${TIMESTAMP} , \" hash\" : \" ${HASH} \" }" )
40
+ set ("${out_var} " "${HASH} " PARENT_SCOPE)
41
+ set (JSON_CONTENT "${JSON_CONTENT} " PARENT_SCOPE)
42
+ endfunction ()
43
+
44
+ get_hash("${CMAKE_C_COMPILER} " C_HASH)
45
+ get_hash("${CMAKE_CXX_COMPILER} " CXX_HASH)
46
+
47
+ # Write updated JSON back to file
48
+ file (WRITE "${VCPKG_COMPILER_CACHE_FILE} " "${JSON_CONTENT} " )
49
+ else ()
50
+ file (SHA1 "${CMAKE_CXX_COMPILER} " CXX_HASH)
51
+ file (SHA1 "${CMAKE_C_COMPILER} " C_HASH)
52
+ endif ()
16
53
string (SHA1 COMPILER_HASH "${C_HASH}${CXX_HASH} " )
17
54
18
55
message ("#COMPILER_HASH#${COMPILER_HASH} " )
0 commit comments