1+
2+ cmake_minimum_required (VERSION 3.19 )
3+
4+
5+
6+ # dlcache("https://github.com/g-truc/glm/archive/refs/heads/master.zip"
7+ # SHA256 97198B71B24AD5087114C1FB64DC3111AEE1C7976CB5AE8A7C4476F3EEAB8D69
8+ # OUT url
9+ # )
10+ # message(STATUS "file loc >>>>> ${url}")
11+
12+ function (gitcache url githash )
13+
14+ if (WIN32 )
15+ file (TO_CMAKE_PATH $ENV{LocalAppData} appdatadir )
16+ set (DLCACHEDIR "${appdatadir} /dlcache" )
17+ else ()
18+ set (DLCACHEDIR "$ENV{HOME} /.cache/dlcache" )
19+ endif ()
20+ string (REGEX REPLACE [^A-Za-z0-9/.-] _ urlstripped "${url} " )
21+ set (dlpath "${DLCACHEDIR} /${urlstripped} " )
22+
23+ set (TMPDEST "${dlpath} .tmp" )
24+ if (EXISTS "${TMPDEST} " )
25+ file (REMOVE_RECURSE "${TMPDEST} " )
26+ endif ()
27+
28+ if (NOT EXISTS "${dlpath} " )
29+ execute_process (
30+ COMMAND git clone --recursive "${url} " "${TMPDEST} "
31+ COMMAND_ERROR_IS_FATAL ANY
32+ )
33+ execute_process (
34+ COMMAND ${CMAKE_COMMAND} -E rename "${TMPDEST} " "${dlpath} "
35+ COMMAND_ERROR_IS_FATAL ANY
36+ )
37+ endif ()
38+
39+ set (FOUND 0)
40+ if (EXISTS "${dlpath} " )
41+ execute_process (
42+ COMMAND git show --format= "%H" --no-patch
43+ WORKING_DIRECTORY "${dlpath} "
44+ OUTPUT_VARIABLE lout
45+ )
46+ if (lout MATCHES "${githash} " )
47+ set (FOUND 1)
48+ endif ()
49+ endif ()
50+
51+ if (NOT FOUND)
52+ execute_process (
53+ COMMAND ${CMAKE_COMMAND} -E rename "${dlpath} " "${TMPDEST} "
54+ COMMAND_ERROR_IS_FATAL ANY
55+ )
56+ execute_process (
57+ COMMAND git clean -fdx
58+ WORKING_DIRECTORY "${TMPDEST} "
59+ COMMAND_ERROR_IS_FATAL ANY
60+ )
61+ execute_process (
62+ COMMAND git checkout --recurse-submodules -f "${githash} "
63+ WORKING_DIRECTORY "${TMPDEST} "
64+ COMMAND_ERROR_IS_FATAL ANY
65+ )
66+ execute_process (
67+ COMMAND ${CMAKE_COMMAND} -E rename "${TMPDEST} " "${dlpath} "
68+ COMMAND_ERROR_IS_FATAL ANY
69+ )
70+ endif ()
71+
72+ set (dl_local_path "${dlpath} " PARENT_SCOPE )
73+ endfunction ()
74+
75+ macro (ghgitcache urlpart githash )
76+ gitcache ("https://github.com/${urlpart} .git" ${githash} )
77+ endmacro ()
78+
79+ # dlcache("https://..." [SHA256 0123...] [OUT localFileLocVar] [UNPACK <subdir rel to current bin dir>] [UNPACK_OUT unpackDirLocVar])
80+ function (dlcache url )
81+ # https://cmake.org/cmake/help/latest/command/cmake_parse_arguments.html
82+ cmake_parse_arguments (DL "" "SHA256;OUT;UNPACK;UNPACK_OUT" "" ${ARGN} )
83+
84+ if (WIN32 )
85+ file (TO_CMAKE_PATH $ENV{LocalAppData} appdatadir )
86+ set (DLCACHEDIR "${appdatadir} /dlcache" )
87+ else ()
88+ set (DLCACHEDIR "$ENV{HOME} /.cache/dlcache" )
89+ endif ()
90+ string (REGEX REPLACE [^A-Za-z0-9/.-] _ urlstripped "${url} " )
91+ set (dlpath "${DLCACHEDIR} /${urlstripped} " )
92+
93+ IF (EXISTS "${dlpath} " )
94+ ELSE ()
95+ IF (DL_SHA256)
96+ file (DOWNLOAD "${url} " "${dlpath} " STATUS dlstatus )
97+ ELSE ()
98+ file (DOWNLOAD "${url} " "${dlpath} " STATUS dlstatus )
99+ ENDIF ()
100+ LIST (GET dlstatus 0 status_code)
101+ IF (status_code)
102+ MESSAGE (FATAL_ERROR "${dlstatus} (${url} )" )
103+ ENDIF ()
104+ ENDIF ()
105+
106+ IF (DL_SHA256)
107+ STRING (TOLOWER ${DL_SHA256} DL_SHA256)
108+ FILE (SHA256 "${dlpath} " cksum )
109+ STRING (TOLOWER ${cksum} cksum)
110+ IF (cksum STREQUAL "${DL_SHA256} " )
111+ ELSE ()
112+ FILE (REMOVE "${dlpath} " )
113+ MESSAGE (FATAL_ERROR "CHECKSUM MISMATCH for: ${url} \n EXPECTED: ${DL_SHA256} \n GOT: ${cksum} " )
114+ ENDIF ()
115+ ENDIF ()
116+
117+ IF (DL_OUT)
118+ set (${DL_OUT} "${dlpath} " PARENT_SCOPE )
119+ ENDIF ()
120+
121+ IF (DL_UNPACK)
122+ cmake_path (SET unpack_path NORMALIZE "${CMAKE_CURRENT_BINARY_DIR} /${DL_UNPACK} " )
123+ message ("unpack path = ${unpack_path} " )
124+ IF (NOT EXISTS ${unpack_path} )
125+ execute_process (
126+ COMMAND ${CMAKE_COMMAND} -E make_directory "${unpack_path} "
127+ )
128+ execute_process (
129+ COMMAND ${CMAKE_COMMAND} -E tar x "${dlpath} "
130+ WORKING_DIRECTORY ${unpack_path}
131+ )
132+ ENDIF ()
133+ IF (DL_UNPACK_OUT)
134+ set (${DL_UNPACK_OUT} "${unpack_path} " PARENT_SCOPE )
135+ ENDIF ()
136+ ENDIF ()
137+ endfunction ()
138+
139+
140+ macro (dlcache2 url sha256 )
141+ dlcache ("${url} " SHA256 "${sha256} " OUT "dl_local_path" )
142+ endmacro ()
143+
144+ # fetch source code zip from github
145+ #
146+ # ${dl_local_path} will contain the file path
147+ #
148+ # project: github-user/project-name
149+ # tag: either 40 chars (commit id) or a ref tag
150+ macro (dlghzip project tag sha256 )
151+ string (LENGTH "${tag} " taglen)
152+ if (taglen EQUAL 40)
153+ dlcache ("https://github.com/${project} /archive/${tag} .zip" SHA256 "${sha256} " OUT "dl_local_path" )
154+ else ()
155+ dlcache ("https://github.com/${project} /archive/refs/tags/${tag} .zip" SHA256 "${sha256} " OUT "dl_local_path" )
156+ endif ()
157+ endmacro ()
158+
159+
160+ # Examples:
161+ #
162+ # dlcache_assets(
163+ # LIST
164+ # https://raw.githubusercontent.com/Overv/VulkanTutorial/master/images/texture.jpg
165+ # 663a43377a9d3b42a1925a17313b12e339b146d219a62c4c07a56c89032858bb
166+ # https://raw.githubusercontent.com/Overv/VulkanTutorial/master/resources/viking_room.png
167+ # facb693858cafcb70b7eed264e34107f06bbf3f41805e7b8084e5b42bd914a66
168+ # OUTDIR textures
169+ # )
170+ # dlcache_assets(
171+ # LIST
172+ # https://raw.githubusercontent.com/Overv/VulkanTutorial/master/resources/viking_room.obj
173+ # 0af27cd99ce43f48c89d9c73cff47cbdfc3d29c3754b29bd0ccfe7e3fe7de869
174+ # OUTDIR models
175+ # )
176+ #
177+ # Writes the given files into textures/modles directories, using the last part of each url as filename.
178+ #
179+ function (dlcache_assets )
180+ cmake_parse_arguments (DL "EXTRACT_FILENAMES" "OUTDIR" "LIST" ${ARGN} )
181+ if (NOT DL_OUTDIR)
182+ set (DL_OUTDIR "assets" )
183+ endif ()
184+ list (LENGTH DL_LIST len1)
185+ math (EXPR len2 "${len1} / 2 - 1" )
186+ foreach (val RANGE ${len2} )
187+ math (EXPR vala "2 * ${val} " )
188+ math (EXPR valb "2 * ${val} + 1" )
189+ list (GET DL_LIST ${vala} val1)
190+ list (GET DL_LIST ${valb} val2)
191+ dlcache2 ("${val1} " "${val2} " )
192+ file (COPY "${dl_local_path} " DESTINATION "${DL_OUTDIR} " )
193+ endforeach ()
194+ endfunction ()
0 commit comments