comparison Resources/CMake/DownloadPackage.cmake @ 0:02f7a0400a91

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Feb 2015 13:45:35 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:02f7a0400a91
1 # Orthanc - A Lightweight, RESTful DICOM Store
2 # Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
3 # Department, University Hospital of Liege, Belgium
4 #
5 # This program is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU Affero General Public License
7 # as published by the Free Software Foundation, either version 3 of
8 # the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Affero General Public License for more details.
14 #
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18
19 macro(GetUrlFilename TargetVariable Url)
20 string(REGEX REPLACE "^.*/" "" ${TargetVariable} "${Url}")
21 endmacro()
22
23
24 macro(GetUrlExtension TargetVariable Url)
25 #string(REGEX REPLACE "^.*/[^.]*\\." "" TMP "${Url}")
26 string(REGEX REPLACE "^.*\\." "" TMP "${Url}")
27 string(TOLOWER "${TMP}" "${TargetVariable}")
28 endmacro()
29
30
31 ##
32 ## Check the existence of the required decompression tools
33 ##
34
35 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
36 find_program(ZIP_EXECUTABLE 7z
37 PATHS
38 "$ENV{ProgramFiles}/7-Zip"
39 "$ENV{ProgramW6432}/7-Zip"
40 )
41
42 if (${ZIP_EXECUTABLE} MATCHES "ZIP_EXECUTABLE-NOTFOUND")
43 message(FATAL_ERROR "Please install the '7-zip' software (http://www.7-zip.org/)")
44 endif()
45
46 else()
47 find_program(UNZIP_EXECUTABLE unzip)
48 if (${UNZIP_EXECUTABLE} MATCHES "UNZIP_EXECUTABLE-NOTFOUND")
49 message(FATAL_ERROR "Please install the 'unzip' package")
50 endif()
51
52 find_program(TAR_EXECUTABLE tar)
53 if (${TAR_EXECUTABLE} MATCHES "TAR_EXECUTABLE-NOTFOUND")
54 message(FATAL_ERROR "Please install the 'tar' package")
55 endif()
56 endif()
57
58
59 macro(DownloadPackage MD5 Url TargetDirectory)
60 if (NOT IS_DIRECTORY "${TargetDirectory}")
61 GetUrlFilename(TMP_FILENAME "${Url}")
62
63 set(TMP_PATH "${CMAKE_SOURCE_DIR}/ThirdPartyDownloads/${TMP_FILENAME}")
64 if (NOT EXISTS "${TMP_PATH}")
65 message("Downloading ${Url}")
66
67 # This fixes issue 6: "I think cmake shouldn't download the
68 # packages which are not in the system, it should stop and let
69 # user know."
70 # https://code.google.com/p/orthanc/issues/detail?id=6
71 if (NOT STATIC_BUILD AND NOT ALLOW_DOWNLOADS)
72 message(FATAL_ERROR "CMake is not allowed to download from Internet. Please set the ALLOW_DOWNLOADS option to ON")
73 endif()
74
75 file(DOWNLOAD "${Url}" "${TMP_PATH}" SHOW_PROGRESS EXPECTED_MD5 "${MD5}")
76 else()
77 message("Using local copy of ${Url}")
78 endif()
79
80 GetUrlExtension(TMP_EXTENSION "${Url}")
81 #message(${TMP_EXTENSION})
82 message("Uncompressing ${TMP_FILENAME}")
83
84 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
85 # How to silently extract files using 7-zip
86 # http://superuser.com/questions/331148/7zip-command-line-extract-silently-quietly
87
88 if (("${TMP_EXTENSION}" STREQUAL "gz") OR ("${TMP_EXTENSION}" STREQUAL "tgz"))
89 execute_process(
90 COMMAND ${ZIP_EXECUTABLE} e -y ${TMP_PATH}
91 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
92 RESULT_VARIABLE Failure
93 OUTPUT_QUIET
94 )
95
96 if (Failure)
97 message(FATAL_ERROR "Error while running the uncompression tool")
98 endif()
99
100 if ("${TMP_EXTENSION}" STREQUAL "tgz")
101 string(REGEX REPLACE ".tgz$" ".tar" TMP_FILENAME2 "${TMP_FILENAME}")
102 else()
103 string(REGEX REPLACE ".gz$" "" TMP_FILENAME2 "${TMP_FILENAME}")
104 endif()
105
106 execute_process(
107 COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_FILENAME2}
108 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
109 RESULT_VARIABLE Failure
110 OUTPUT_QUIET
111 )
112 elseif ("${TMP_EXTENSION}" STREQUAL "zip")
113 execute_process(
114 COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_PATH}
115 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
116 RESULT_VARIABLE Failure
117 OUTPUT_QUIET
118 )
119 else()
120 message(FATAL_ERROR "Support your platform here")
121 endif()
122
123 else()
124 if ("${TMP_EXTENSION}" STREQUAL "zip")
125 execute_process(
126 COMMAND sh -c "${UNZIP_EXECUTABLE} -q ${TMP_PATH}"
127 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
128 RESULT_VARIABLE Failure
129 )
130 elseif (("${TMP_EXTENSION}" STREQUAL "gz") OR ("${TMP_EXTENSION}" STREQUAL "tgz"))
131 #message("tar xvfz ${TMP_PATH}")
132 execute_process(
133 COMMAND sh -c "${TAR_EXECUTABLE} xfz ${TMP_PATH}"
134 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
135 RESULT_VARIABLE Failure
136 )
137 elseif ("${TMP_EXTENSION}" STREQUAL "bz2")
138 execute_process(
139 COMMAND sh -c "${TAR_EXECUTABLE} xfj ${TMP_PATH}"
140 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
141 RESULT_VARIABLE Failure
142 )
143 else()
144 message(FATAL_ERROR "Unknown package format.")
145 endif()
146 endif()
147
148 if (Failure)
149 message(FATAL_ERROR "Error while running the uncompression tool")
150 endif()
151
152 if (NOT IS_DIRECTORY "${TargetDirectory}")
153 message(FATAL_ERROR "The package was not uncompressed at the proper location. Check the CMake instructions.")
154 endif()
155 endif()
156 endmacro()