File: DownloadPackage.cmake

package info (click to toggle)
orthanc-webviewer 2.10%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,932 kB
  • sloc: javascript: 21,840; cpp: 7,296; python: 345; sh: 93; makefile: 60
file content (287 lines) | stat: -rw-r--r-- 9,423 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Orthanc - A Lightweight, RESTful DICOM Store
# Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
# Department, University Hospital of Liege, Belgium
# Copyright (C) 2017-2023 Osimis S.A., Belgium
# Copyright (C) 2024-2025 Orthanc Team SRL, Belgium
# Copyright (C) 2021-2025 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.


macro(GetUrlFilename TargetVariable Url)
  string(REGEX REPLACE "^.*/" "" ${TargetVariable} "${Url}")
endmacro()


macro(GetUrlExtension TargetVariable Url)
  #string(REGEX REPLACE "^.*/[^.]*\\." "" TMP "${Url}")
  string(REGEX REPLACE "^.*\\." "" TMP "${Url}")
  string(TOLOWER "${TMP}" "${TargetVariable}")
endmacro()



##
## Setup the patch command-line tool
##

if (NOT ORTHANC_DISABLE_PATCH)
  if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
    set(PATCH_EXECUTABLE ${CMAKE_CURRENT_LIST_DIR}/../ThirdParty/patch/patch.exe)
    if (NOT EXISTS ${PATCH_EXECUTABLE})
      message(FATAL_ERROR "Unable to find the patch.exe tool that is shipped with Orthanc")
    endif()

  else ()
    find_program(PATCH_EXECUTABLE patch)
    if (${PATCH_EXECUTABLE} MATCHES "PATCH_EXECUTABLE-NOTFOUND")
      message(FATAL_ERROR "Please install the 'patch' standard command-line tool")
    endif()
  endif()
endif()



##
## Check the existence of the required decompression tools
##

if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
  find_program(ZIP_EXECUTABLE 7z 
    PATHS 
    "$ENV{ProgramFiles}/7-Zip"
    "$ENV{ProgramW6432}/7-Zip"
    )

  if (${ZIP_EXECUTABLE} MATCHES "ZIP_EXECUTABLE-NOTFOUND")
    message(FATAL_ERROR "Please install the '7-zip' software (http://www.7-zip.org/)")
  endif()

else()
  find_program(UNZIP_EXECUTABLE unzip)
  if (${UNZIP_EXECUTABLE} MATCHES "UNZIP_EXECUTABLE-NOTFOUND")
    message(FATAL_ERROR "Please install the 'unzip' package")
  endif()

  find_program(TAR_EXECUTABLE tar)
  if (${TAR_EXECUTABLE} MATCHES "TAR_EXECUTABLE-NOTFOUND")
    message(FATAL_ERROR "Please install the 'tar' package")
  endif()

  find_program(GUNZIP_EXECUTABLE gunzip)
  if (${GUNZIP_EXECUTABLE} MATCHES "GUNZIP_EXECUTABLE-NOTFOUND")
    message(FATAL_ERROR "Please install the 'gzip' package")
  endif()
endif()


macro(DownloadFile MD5 Url)
  GetUrlFilename(TMP_FILENAME "${Url}")

  set(TMP_PATH "${CMAKE_SOURCE_DIR}/ThirdPartyDownloads/${TMP_FILENAME}")
  if (NOT EXISTS "${TMP_PATH}")
    message("Downloading ${Url}")

    # This fixes issue 6: "I think cmake shouldn't download the
    # packages which are not in the system, it should stop and let
    # user know."
    # https://code.google.com/p/orthanc/issues/detail?id=6
    if (NOT STATIC_BUILD AND NOT ALLOW_DOWNLOADS)
      message(FATAL_ERROR "CMake is not allowed to download from Internet. Please set the ALLOW_DOWNLOADS option to ON")
    endif()

    foreach (retry RANGE 1 5)   # Retries 5 times
      if ("${MD5}" STREQUAL "no-check")
        message(WARNING "Not checking the MD5 of: ${Url}")
        file(DOWNLOAD "${Url}" "${TMP_PATH}"
          SHOW_PROGRESS TIMEOUT 30 INACTIVITY_TIMEOUT 10
          STATUS Failure)
      else()
        file(DOWNLOAD "${Url}" "${TMP_PATH}"
          SHOW_PROGRESS TIMEOUT 30 INACTIVITY_TIMEOUT 10
          EXPECTED_MD5 "${MD5}" STATUS Failure)
      endif()

      list(GET Failure 0 Status)
      if (Status EQUAL 0)
        break()  # Successful download
      endif()
    endforeach()

    if (NOT Status EQUAL 0)
      file(REMOVE ${TMP_PATH})
      message(FATAL_ERROR "Cannot download file: ${Url}")
    endif()
    
  else()
    message("Using local copy of ${Url}")

    if ("${MD5}" STREQUAL "no-check")
      message(WARNING "Not checking the MD5 of: ${Url}")
    else()
      file(MD5 ${TMP_PATH} ActualMD5)
      if (NOT "${ActualMD5}" STREQUAL "${MD5}")
        message(FATAL_ERROR "The MD5 hash of a previously download file is invalid: ${TMP_PATH}")
      endif()
    endif()
  endif()
endmacro()


macro(DownloadPackage MD5 Url TargetDirectory)
  if (NOT IS_DIRECTORY "${TargetDirectory}")
    DownloadFile("${MD5}" "${Url}")
    
    GetUrlExtension(TMP_EXTENSION "${Url}")
    #message(${TMP_EXTENSION})
    message("Uncompressing ${TMP_FILENAME}")

    if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
      # How to silently extract files using 7-zip
      # http://superuser.com/questions/331148/7zip-command-line-extract-silently-quietly

      if (("${TMP_EXTENSION}" STREQUAL "gz") OR 
          ("${TMP_EXTENSION}" STREQUAL "tgz") OR
          ("${TMP_EXTENSION}" STREQUAL "xz"))
        execute_process(
          COMMAND ${ZIP_EXECUTABLE} e -y ${TMP_PATH}
          WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
          RESULT_VARIABLE Failure
          OUTPUT_QUIET
          )

        if (Failure)
          message(FATAL_ERROR "Error while running the uncompression tool")
        endif()

        if ("${TMP_EXTENSION}" STREQUAL "tgz")
          string(REGEX REPLACE ".tgz$" ".tar" TMP_FILENAME2 "${TMP_FILENAME}")
        elseif ("${TMP_EXTENSION}" STREQUAL "gz")
          string(REGEX REPLACE ".gz$" "" TMP_FILENAME2 "${TMP_FILENAME}")
        elseif ("${TMP_EXTENSION}" STREQUAL "xz")
          string(REGEX REPLACE ".xz" "" TMP_FILENAME2 "${TMP_FILENAME}")
        endif()

        execute_process(
          COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_FILENAME2}
          WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
          RESULT_VARIABLE Failure
          OUTPUT_QUIET
          )
      elseif ("${TMP_EXTENSION}" STREQUAL "zip")
        execute_process(
          COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_PATH}
          WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
          RESULT_VARIABLE Failure
          OUTPUT_QUIET
          )
      else()
        message(FATAL_ERROR "Unsupported package extension: ${TMP_EXTENSION}")
      endif()

    else()
      if ("${TMP_EXTENSION}" STREQUAL "zip")
        execute_process(
          COMMAND sh -c "${UNZIP_EXECUTABLE} -q ${TMP_PATH}"
          WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
          RESULT_VARIABLE Failure
        )
      elseif (("${TMP_EXTENSION}" STREQUAL "gz") OR ("${TMP_EXTENSION}" STREQUAL "tgz"))
        #message("tar xvfz ${TMP_PATH}")
        execute_process(
          COMMAND sh -c "${TAR_EXECUTABLE} xfz ${TMP_PATH}"
          WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
          RESULT_VARIABLE Failure
          )
      elseif ("${TMP_EXTENSION}" STREQUAL "bz2")
        execute_process(
          COMMAND sh -c "${TAR_EXECUTABLE} xfj ${TMP_PATH}"
          WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
          RESULT_VARIABLE Failure
          )
      elseif ("${TMP_EXTENSION}" STREQUAL "xz")
        execute_process(
          COMMAND sh -c "${TAR_EXECUTABLE} xf ${TMP_PATH}"
          WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
          RESULT_VARIABLE Failure
          )
      else()
        message(FATAL_ERROR "Unsupported package extension: ${TMP_EXTENSION}")
      endif()
    endif()
   
    if (Failure)
      message(FATAL_ERROR "Error while running the uncompression tool")
    endif()

    if (NOT IS_DIRECTORY "${TargetDirectory}")
      message(FATAL_ERROR "The package was not uncompressed at the proper location. Check the CMake instructions.")
    endif()
  endif()
endmacro()



macro(DownloadCompressedFile MD5 Url TargetFile)
  if (NOT EXISTS "${TargetFile}")
    DownloadFile("${MD5}" "${Url}")
    
    GetUrlExtension(TMP_EXTENSION "${Url}")
    #message(${TMP_EXTENSION})
    message("Uncompressing ${TMP_FILENAME}")

    if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
      # How to silently extract files using 7-zip
      # http://superuser.com/questions/331148/7zip-command-line-extract-silently-quietly

      if ("${TMP_EXTENSION}" STREQUAL "gz")
        execute_process(
          # "-so" writes uncompressed file to stdout
          COMMAND ${ZIP_EXECUTABLE} e -so -y ${TMP_PATH}
          OUTPUT_FILE "${TargetFile}"
          WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
          RESULT_VARIABLE Failure
          OUTPUT_QUIET
          )

        if (Failure)
          message(FATAL_ERROR "Error while running the uncompression tool")
        endif()

      else()
        message(FATAL_ERROR "Unsupported file extension: ${TMP_EXTENSION}")
      endif()

    else()
      if ("${TMP_EXTENSION}" STREQUAL "gz")
        execute_process(
          COMMAND sh -c "${GUNZIP_EXECUTABLE} -c ${TMP_PATH}"
          OUTPUT_FILE "${TargetFile}"
          RESULT_VARIABLE Failure
          )
      else()
        message(FATAL_ERROR "Unsupported file extension: ${TMP_EXTENSION}")
      endif()
    endif()
   
    if (Failure)
      message(FATAL_ERROR "Error while running the uncompression tool")
    endif()

    if (NOT EXISTS "${TargetFile}")
      message(FATAL_ERROR "The file was not uncompressed at the proper location. Check the CMake instructions.")
    endif()
  endif()
endmacro()