comparison Framework/Orthanc/Resources/CMake/DownloadPackage.cmake @ 1:2dbe613f6c93

add orthanc core
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 14 Oct 2016 15:39:01 +0200
parents
children 9220cf4a63d5
comparison
equal deleted inserted replaced
0:351ab0da0150 1:2dbe613f6c93
1 macro(GetUrlFilename TargetVariable Url)
2 string(REGEX REPLACE "^.*/" "" ${TargetVariable} "${Url}")
3 endmacro()
4
5
6 macro(GetUrlExtension TargetVariable Url)
7 #string(REGEX REPLACE "^.*/[^.]*\\." "" TMP "${Url}")
8 string(REGEX REPLACE "^.*\\." "" TMP "${Url}")
9 string(TOLOWER "${TMP}" "${TargetVariable}")
10 endmacro()
11
12
13
14 ##
15 ## Setup the patch command-line tool
16 ##
17
18 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
19 set(PATCH_EXECUTABLE ${CMAKE_SOURCE_DIR}/Resources/ThirdParty/patch/patch.exe)
20 else ()
21 find_program(PATCH_EXECUTABLE patch)
22 if (${PATCH_EXECUTABLE} MATCHES "PATCH_EXECUTABLE-NOTFOUND")
23 message(FATAL_ERROR "Please install the 'patch' standard command-line tool")
24 endif()
25 endif()
26
27
28
29 ##
30 ## Check the existence of the required decompression tools
31 ##
32
33 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
34 find_program(ZIP_EXECUTABLE 7z
35 PATHS
36 "$ENV{ProgramFiles}/7-Zip"
37 "$ENV{ProgramW6432}/7-Zip"
38 )
39
40 if (${ZIP_EXECUTABLE} MATCHES "ZIP_EXECUTABLE-NOTFOUND")
41 message(FATAL_ERROR "Please install the '7-zip' software (http://www.7-zip.org/)")
42 endif()
43
44 else()
45 find_program(UNZIP_EXECUTABLE unzip)
46 if (${UNZIP_EXECUTABLE} MATCHES "UNZIP_EXECUTABLE-NOTFOUND")
47 message(FATAL_ERROR "Please install the 'unzip' package")
48 endif()
49
50 find_program(TAR_EXECUTABLE tar)
51 if (${TAR_EXECUTABLE} MATCHES "TAR_EXECUTABLE-NOTFOUND")
52 message(FATAL_ERROR "Please install the 'tar' package")
53 endif()
54 endif()
55
56
57 macro(DownloadPackage MD5 Url TargetDirectory)
58 if (NOT IS_DIRECTORY "${TargetDirectory}")
59 GetUrlFilename(TMP_FILENAME "${Url}")
60
61 set(TMP_PATH "${CMAKE_SOURCE_DIR}/ThirdPartyDownloads/${TMP_FILENAME}")
62 if (NOT EXISTS "${TMP_PATH}")
63 message("Downloading ${Url}")
64
65 # This fixes issue 6: "I think cmake shouldn't download the
66 # packages which are not in the system, it should stop and let
67 # user know."
68 # https://code.google.com/p/orthanc/issues/detail?id=6
69 if (NOT STATIC_BUILD AND NOT ALLOW_DOWNLOADS)
70 message(FATAL_ERROR "CMake is not allowed to download from Internet. Please set the ALLOW_DOWNLOADS option to ON")
71 endif()
72
73 file(DOWNLOAD "${Url}" "${TMP_PATH}" SHOW_PROGRESS EXPECTED_MD5 "${MD5}")
74 else()
75 message("Using local copy of ${Url}")
76 endif()
77
78 GetUrlExtension(TMP_EXTENSION "${Url}")
79 #message(${TMP_EXTENSION})
80 message("Uncompressing ${TMP_FILENAME}")
81
82 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
83 # How to silently extract files using 7-zip
84 # http://superuser.com/questions/331148/7zip-command-line-extract-silently-quietly
85
86 if (("${TMP_EXTENSION}" STREQUAL "gz") OR
87 ("${TMP_EXTENSION}" STREQUAL "tgz") OR
88 ("${TMP_EXTENSION}" STREQUAL "xz"))
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 elseif ("${TMP_EXTENSION}" STREQUAL "gz")
103 string(REGEX REPLACE ".gz$" "" TMP_FILENAME2 "${TMP_FILENAME}")
104 elseif ("${TMP_EXTENSION}" STREQUAL "xz")
105 string(REGEX REPLACE ".xz" "" TMP_FILENAME2 "${TMP_FILENAME}")
106 endif()
107
108 execute_process(
109 COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_FILENAME2}
110 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
111 RESULT_VARIABLE Failure
112 OUTPUT_QUIET
113 )
114 elseif ("${TMP_EXTENSION}" STREQUAL "zip")
115 execute_process(
116 COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_PATH}
117 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
118 RESULT_VARIABLE Failure
119 OUTPUT_QUIET
120 )
121 else()
122 message(FATAL_ERROR "Support your platform here")
123 endif()
124
125 else()
126 if ("${TMP_EXTENSION}" STREQUAL "zip")
127 execute_process(
128 COMMAND sh -c "${UNZIP_EXECUTABLE} -q ${TMP_PATH}"
129 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
130 RESULT_VARIABLE Failure
131 )
132 elseif (("${TMP_EXTENSION}" STREQUAL "gz") OR ("${TMP_EXTENSION}" STREQUAL "tgz"))
133 #message("tar xvfz ${TMP_PATH}")
134 execute_process(
135 COMMAND sh -c "${TAR_EXECUTABLE} xfz ${TMP_PATH}"
136 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
137 RESULT_VARIABLE Failure
138 )
139 elseif ("${TMP_EXTENSION}" STREQUAL "bz2")
140 execute_process(
141 COMMAND sh -c "${TAR_EXECUTABLE} xfj ${TMP_PATH}"
142 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
143 RESULT_VARIABLE Failure
144 )
145 elseif ("${TMP_EXTENSION}" STREQUAL "xz")
146 execute_process(
147 COMMAND sh -c "${TAR_EXECUTABLE} xf ${TMP_PATH}"
148 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
149 RESULT_VARIABLE Failure
150 )
151 else()
152 message(FATAL_ERROR "Unknown package format.")
153 endif()
154 endif()
155
156 if (Failure)
157 message(FATAL_ERROR "Error while running the uncompression tool")
158 endif()
159
160 if (NOT IS_DIRECTORY "${TargetDirectory}")
161 message(FATAL_ERROR "The package was not uncompressed at the proper location. Check the CMake instructions.")
162 endif()
163 endif()
164 endmacro()