comparison Resources/Orthanc/CMake/DownloadPackage.cmake @ 0:3f1cf4a8e31f

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 22 Mar 2023 10:16:47 +0100
parents
children 776e650a6386
comparison
equal deleted inserted replaced
-1:000000000000 0:3f1cf4a8e31f
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-2022 Osimis S.A., Belgium
5 # Copyright (C) 2021-2022 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 if ("${MD5}" STREQUAL "no-check")
105 message(WARNING "Not checking the MD5 of: ${Url}")
106 file(DOWNLOAD "${Url}" "${TMP_PATH}"
107 SHOW_PROGRESS TIMEOUT 300 INACTIVITY_TIMEOUT 60
108 STATUS Failure)
109 else()
110 file(DOWNLOAD "${Url}" "${TMP_PATH}"
111 SHOW_PROGRESS TIMEOUT 300 INACTIVITY_TIMEOUT 60
112 EXPECTED_MD5 "${MD5}" STATUS Failure)
113 endif()
114
115 list(GET Failure 0 Status)
116 if (NOT Status EQUAL 0)
117 message(FATAL_ERROR "Cannot download file: ${Url}")
118 endif()
119
120 else()
121 message("Using local copy of ${Url}")
122
123 if ("${MD5}" STREQUAL "no-check")
124 message(WARNING "Not checking the MD5 of: ${Url}")
125 else()
126 file(MD5 ${TMP_PATH} ActualMD5)
127 if (NOT "${ActualMD5}" STREQUAL "${MD5}")
128 message(FATAL_ERROR "The MD5 hash of a previously download file is invalid: ${TMP_PATH}")
129 endif()
130 endif()
131 endif()
132 endmacro()
133
134
135 macro(DownloadPackage MD5 Url TargetDirectory)
136 if (NOT IS_DIRECTORY "${TargetDirectory}")
137 DownloadFile("${MD5}" "${Url}")
138
139 GetUrlExtension(TMP_EXTENSION "${Url}")
140 #message(${TMP_EXTENSION})
141 message("Uncompressing ${TMP_FILENAME}")
142
143 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
144 # How to silently extract files using 7-zip
145 # http://superuser.com/questions/331148/7zip-command-line-extract-silently-quietly
146
147 if (("${TMP_EXTENSION}" STREQUAL "gz") OR
148 ("${TMP_EXTENSION}" STREQUAL "tgz") OR
149 ("${TMP_EXTENSION}" STREQUAL "xz"))
150 execute_process(
151 COMMAND ${ZIP_EXECUTABLE} e -y ${TMP_PATH}
152 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
153 RESULT_VARIABLE Failure
154 OUTPUT_QUIET
155 )
156
157 if (Failure)
158 message(FATAL_ERROR "Error while running the uncompression tool")
159 endif()
160
161 if ("${TMP_EXTENSION}" STREQUAL "tgz")
162 string(REGEX REPLACE ".tgz$" ".tar" TMP_FILENAME2 "${TMP_FILENAME}")
163 elseif ("${TMP_EXTENSION}" STREQUAL "gz")
164 string(REGEX REPLACE ".gz$" "" TMP_FILENAME2 "${TMP_FILENAME}")
165 elseif ("${TMP_EXTENSION}" STREQUAL "xz")
166 string(REGEX REPLACE ".xz" "" TMP_FILENAME2 "${TMP_FILENAME}")
167 endif()
168
169 execute_process(
170 COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_FILENAME2}
171 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
172 RESULT_VARIABLE Failure
173 OUTPUT_QUIET
174 )
175 elseif ("${TMP_EXTENSION}" STREQUAL "zip")
176 execute_process(
177 COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_PATH}
178 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
179 RESULT_VARIABLE Failure
180 OUTPUT_QUIET
181 )
182 else()
183 message(FATAL_ERROR "Unsupported package extension: ${TMP_EXTENSION}")
184 endif()
185
186 else()
187 if ("${TMP_EXTENSION}" STREQUAL "zip")
188 execute_process(
189 COMMAND sh -c "${UNZIP_EXECUTABLE} -q ${TMP_PATH}"
190 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
191 RESULT_VARIABLE Failure
192 )
193 elseif (("${TMP_EXTENSION}" STREQUAL "gz") OR ("${TMP_EXTENSION}" STREQUAL "tgz"))
194 #message("tar xvfz ${TMP_PATH}")
195 execute_process(
196 COMMAND sh -c "${TAR_EXECUTABLE} xfz ${TMP_PATH}"
197 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
198 RESULT_VARIABLE Failure
199 )
200 elseif ("${TMP_EXTENSION}" STREQUAL "bz2")
201 execute_process(
202 COMMAND sh -c "${TAR_EXECUTABLE} xfj ${TMP_PATH}"
203 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
204 RESULT_VARIABLE Failure
205 )
206 elseif ("${TMP_EXTENSION}" STREQUAL "xz")
207 execute_process(
208 COMMAND sh -c "${TAR_EXECUTABLE} xf ${TMP_PATH}"
209 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
210 RESULT_VARIABLE Failure
211 )
212 else()
213 message(FATAL_ERROR "Unsupported package extension: ${TMP_EXTENSION}")
214 endif()
215 endif()
216
217 if (Failure)
218 message(FATAL_ERROR "Error while running the uncompression tool")
219 endif()
220
221 if (NOT IS_DIRECTORY "${TargetDirectory}")
222 message(FATAL_ERROR "The package was not uncompressed at the proper location. Check the CMake instructions.")
223 endif()
224 endif()
225 endmacro()
226
227
228
229 macro(DownloadCompressedFile MD5 Url TargetFile)
230 if (NOT EXISTS "${TargetFile}")
231 DownloadFile("${MD5}" "${Url}")
232
233 GetUrlExtension(TMP_EXTENSION "${Url}")
234 #message(${TMP_EXTENSION})
235 message("Uncompressing ${TMP_FILENAME}")
236
237 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
238 # How to silently extract files using 7-zip
239 # http://superuser.com/questions/331148/7zip-command-line-extract-silently-quietly
240
241 if ("${TMP_EXTENSION}" STREQUAL "gz")
242 execute_process(
243 # "-so" writes uncompressed file to stdout
244 COMMAND ${ZIP_EXECUTABLE} e -so -y ${TMP_PATH}
245 OUTPUT_FILE "${TargetFile}"
246 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
247 RESULT_VARIABLE Failure
248 OUTPUT_QUIET
249 )
250
251 if (Failure)
252 message(FATAL_ERROR "Error while running the uncompression tool")
253 endif()
254
255 else()
256 message(FATAL_ERROR "Unsupported file extension: ${TMP_EXTENSION}")
257 endif()
258
259 else()
260 if ("${TMP_EXTENSION}" STREQUAL "gz")
261 execute_process(
262 COMMAND sh -c "${GUNZIP_EXECUTABLE} -c ${TMP_PATH}"
263 OUTPUT_FILE "${TargetFile}"
264 RESULT_VARIABLE Failure
265 )
266 else()
267 message(FATAL_ERROR "Unsupported file extension: ${TMP_EXTENSION}")
268 endif()
269 endif()
270
271 if (Failure)
272 message(FATAL_ERROR "Error while running the uncompression tool")
273 endif()
274
275 if (NOT EXISTS "${TargetFile}")
276 message(FATAL_ERROR "The file was not uncompressed at the proper location. Check the CMake instructions.")
277 endif()
278 endif()
279 endmacro()