comparison Resources/Orthanc/CMake/DownloadPackage.cmake @ 0:39585ba26f20

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 15 Jun 2023 09:48:46 +0200
parents
children 78f21b9263b1
comparison
equal deleted inserted replaced
-1:000000000000 0:39585ba26f20
1 # Orthanc - A Lightweight, RESTful DICOM Store
2 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
3 # Department, University Hospital of Liege, Belgium
4 # Copyright (C) 2017-2023 Osimis S.A., Belgium
5 # Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
6 #
7 # This program is free software: you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public License
9 # as published by the Free Software Foundation, either version 3 of
10 # the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this program. If not, see
19 # <http://www.gnu.org/licenses/>.
20
21
22 macro(GetUrlFilename TargetVariable Url)
23 string(REGEX REPLACE "^.*/" "" ${TargetVariable} "${Url}")
24 endmacro()
25
26
27 macro(GetUrlExtension TargetVariable Url)
28 #string(REGEX REPLACE "^.*/[^.]*\\." "" TMP "${Url}")
29 string(REGEX REPLACE "^.*\\." "" TMP "${Url}")
30 string(TOLOWER "${TMP}" "${TargetVariable}")
31 endmacro()
32
33
34
35 ##
36 ## Setup the patch command-line tool
37 ##
38
39 if (NOT ORTHANC_DISABLE_PATCH)
40 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
41 set(PATCH_EXECUTABLE ${CMAKE_CURRENT_LIST_DIR}/../ThirdParty/patch/patch.exe)
42 if (NOT EXISTS ${PATCH_EXECUTABLE})
43 message(FATAL_ERROR "Unable to find the patch.exe tool that is shipped with Orthanc")
44 endif()
45
46 else ()
47 find_program(PATCH_EXECUTABLE patch)
48 if (${PATCH_EXECUTABLE} MATCHES "PATCH_EXECUTABLE-NOTFOUND")
49 message(FATAL_ERROR "Please install the 'patch' standard command-line tool")
50 endif()
51 endif()
52 endif()
53
54
55
56 ##
57 ## Check the existence of the required decompression tools
58 ##
59
60 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
61 find_program(ZIP_EXECUTABLE 7z
62 PATHS
63 "$ENV{ProgramFiles}/7-Zip"
64 "$ENV{ProgramW6432}/7-Zip"
65 )
66
67 if (${ZIP_EXECUTABLE} MATCHES "ZIP_EXECUTABLE-NOTFOUND")
68 message(FATAL_ERROR "Please install the '7-zip' software (http://www.7-zip.org/)")
69 endif()
70
71 else()
72 find_program(UNZIP_EXECUTABLE unzip)
73 if (${UNZIP_EXECUTABLE} MATCHES "UNZIP_EXECUTABLE-NOTFOUND")
74 message(FATAL_ERROR "Please install the 'unzip' package")
75 endif()
76
77 find_program(TAR_EXECUTABLE tar)
78 if (${TAR_EXECUTABLE} MATCHES "TAR_EXECUTABLE-NOTFOUND")
79 message(FATAL_ERROR "Please install the 'tar' package")
80 endif()
81
82 find_program(GUNZIP_EXECUTABLE gunzip)
83 if (${GUNZIP_EXECUTABLE} MATCHES "GUNZIP_EXECUTABLE-NOTFOUND")
84 message(FATAL_ERROR "Please install the 'gzip' package")
85 endif()
86 endif()
87
88
89 macro(DownloadFile MD5 Url)
90 GetUrlFilename(TMP_FILENAME "${Url}")
91
92 set(TMP_PATH "${CMAKE_SOURCE_DIR}/ThirdPartyDownloads/${TMP_FILENAME}")
93 if (NOT EXISTS "${TMP_PATH}")
94 message("Downloading ${Url}")
95
96 # This fixes issue 6: "I think cmake shouldn't download the
97 # packages which are not in the system, it should stop and let
98 # user know."
99 # https://code.google.com/p/orthanc/issues/detail?id=6
100 if (NOT STATIC_BUILD AND NOT ALLOW_DOWNLOADS)
101 message(FATAL_ERROR "CMake is not allowed to download from Internet. Please set the ALLOW_DOWNLOADS option to ON")
102 endif()
103
104 foreach (retry RANGE 1 5) # Retries 5 times
105 if ("${MD5}" STREQUAL "no-check")
106 message(WARNING "Not checking the MD5 of: ${Url}")
107 file(DOWNLOAD "${Url}" "${TMP_PATH}"
108 SHOW_PROGRESS TIMEOUT 30 INACTIVITY_TIMEOUT 10
109 STATUS Failure)
110 else()
111 file(DOWNLOAD "${Url}" "${TMP_PATH}"
112 SHOW_PROGRESS TIMEOUT 30 INACTIVITY_TIMEOUT 10
113 EXPECTED_MD5 "${MD5}" STATUS Failure)
114 endif()
115
116 list(GET Failure 0 Status)
117 if (Status EQUAL 0)
118 break() # Successful download
119 endif()
120 endforeach()
121
122 if (NOT Status EQUAL 0)
123 file(REMOVE ${TMP_PATH})
124 message(FATAL_ERROR "Cannot download file: ${Url}")
125 endif()
126
127 else()
128 message("Using local copy of ${Url}")
129
130 if ("${MD5}" STREQUAL "no-check")
131 message(WARNING "Not checking the MD5 of: ${Url}")
132 else()
133 file(MD5 ${TMP_PATH} ActualMD5)
134 if (NOT "${ActualMD5}" STREQUAL "${MD5}")
135 message(FATAL_ERROR "The MD5 hash of a previously download file is invalid: ${TMP_PATH}")
136 endif()
137 endif()
138 endif()
139 endmacro()
140
141
142 macro(DownloadPackage MD5 Url TargetDirectory)
143 if (NOT IS_DIRECTORY "${TargetDirectory}")
144 DownloadFile("${MD5}" "${Url}")
145
146 GetUrlExtension(TMP_EXTENSION "${Url}")
147 #message(${TMP_EXTENSION})
148 message("Uncompressing ${TMP_FILENAME}")
149
150 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
151 # How to silently extract files using 7-zip
152 # http://superuser.com/questions/331148/7zip-command-line-extract-silently-quietly
153
154 if (("${TMP_EXTENSION}" STREQUAL "gz") OR
155 ("${TMP_EXTENSION}" STREQUAL "tgz") OR
156 ("${TMP_EXTENSION}" STREQUAL "xz"))
157 execute_process(
158 COMMAND ${ZIP_EXECUTABLE} e -y ${TMP_PATH}
159 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
160 RESULT_VARIABLE Failure
161 OUTPUT_QUIET
162 )
163
164 if (Failure)
165 message(FATAL_ERROR "Error while running the uncompression tool")
166 endif()
167
168 if ("${TMP_EXTENSION}" STREQUAL "tgz")
169 string(REGEX REPLACE ".tgz$" ".tar" TMP_FILENAME2 "${TMP_FILENAME}")
170 elseif ("${TMP_EXTENSION}" STREQUAL "gz")
171 string(REGEX REPLACE ".gz$" "" TMP_FILENAME2 "${TMP_FILENAME}")
172 elseif ("${TMP_EXTENSION}" STREQUAL "xz")
173 string(REGEX REPLACE ".xz" "" TMP_FILENAME2 "${TMP_FILENAME}")
174 endif()
175
176 execute_process(
177 COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_FILENAME2}
178 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
179 RESULT_VARIABLE Failure
180 OUTPUT_QUIET
181 )
182 elseif ("${TMP_EXTENSION}" STREQUAL "zip")
183 execute_process(
184 COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_PATH}
185 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
186 RESULT_VARIABLE Failure
187 OUTPUT_QUIET
188 )
189 else()
190 message(FATAL_ERROR "Unsupported package extension: ${TMP_EXTENSION}")
191 endif()
192
193 else()
194 if ("${TMP_EXTENSION}" STREQUAL "zip")
195 execute_process(
196 COMMAND sh -c "${UNZIP_EXECUTABLE} -q ${TMP_PATH}"
197 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
198 RESULT_VARIABLE Failure
199 )
200 elseif (("${TMP_EXTENSION}" STREQUAL "gz") OR ("${TMP_EXTENSION}" STREQUAL "tgz"))
201 #message("tar xvfz ${TMP_PATH}")
202 execute_process(
203 COMMAND sh -c "${TAR_EXECUTABLE} xfz ${TMP_PATH}"
204 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
205 RESULT_VARIABLE Failure
206 )
207 elseif ("${TMP_EXTENSION}" STREQUAL "bz2")
208 execute_process(
209 COMMAND sh -c "${TAR_EXECUTABLE} xfj ${TMP_PATH}"
210 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
211 RESULT_VARIABLE Failure
212 )
213 elseif ("${TMP_EXTENSION}" STREQUAL "xz")
214 execute_process(
215 COMMAND sh -c "${TAR_EXECUTABLE} xf ${TMP_PATH}"
216 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
217 RESULT_VARIABLE Failure
218 )
219 else()
220 message(FATAL_ERROR "Unsupported package extension: ${TMP_EXTENSION}")
221 endif()
222 endif()
223
224 if (Failure)
225 message(FATAL_ERROR "Error while running the uncompression tool")
226 endif()
227
228 if (NOT IS_DIRECTORY "${TargetDirectory}")
229 message(FATAL_ERROR "The package was not uncompressed at the proper location. Check the CMake instructions.")
230 endif()
231 endif()
232 endmacro()
233
234
235
236 macro(DownloadCompressedFile MD5 Url TargetFile)
237 if (NOT EXISTS "${TargetFile}")
238 DownloadFile("${MD5}" "${Url}")
239
240 GetUrlExtension(TMP_EXTENSION "${Url}")
241 #message(${TMP_EXTENSION})
242 message("Uncompressing ${TMP_FILENAME}")
243
244 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
245 # How to silently extract files using 7-zip
246 # http://superuser.com/questions/331148/7zip-command-line-extract-silently-quietly
247
248 if ("${TMP_EXTENSION}" STREQUAL "gz")
249 execute_process(
250 # "-so" writes uncompressed file to stdout
251 COMMAND ${ZIP_EXECUTABLE} e -so -y ${TMP_PATH}
252 OUTPUT_FILE "${TargetFile}"
253 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
254 RESULT_VARIABLE Failure
255 OUTPUT_QUIET
256 )
257
258 if (Failure)
259 message(FATAL_ERROR "Error while running the uncompression tool")
260 endif()
261
262 else()
263 message(FATAL_ERROR "Unsupported file extension: ${TMP_EXTENSION}")
264 endif()
265
266 else()
267 if ("${TMP_EXTENSION}" STREQUAL "gz")
268 execute_process(
269 COMMAND sh -c "${GUNZIP_EXECUTABLE} -c ${TMP_PATH}"
270 OUTPUT_FILE "${TargetFile}"
271 RESULT_VARIABLE Failure
272 )
273 else()
274 message(FATAL_ERROR "Unsupported file extension: ${TMP_EXTENSION}")
275 endif()
276 endif()
277
278 if (Failure)
279 message(FATAL_ERROR "Error while running the uncompression tool")
280 endif()
281
282 if (NOT EXISTS "${TargetFile}")
283 message(FATAL_ERROR "The file was not uncompressed at the proper location. Check the CMake instructions.")
284 endif()
285 endif()
286 endmacro()