comparison Common/Resources/Orthanc/CMake/DownloadPackage.cmake @ 59:f3c44d61e1e1

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