comparison Orthanc/Resources/CMake/DownloadPackage.cmake @ 78:d6da56f86e5a

sync
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Sep 2015 11:29:17 +0200
parents
children 3251ec958a29
comparison
equal deleted inserted replaced
77:f44ebb25691c 78:d6da56f86e5a
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 ("${TMP_EXTENSION}" STREQUAL "tgz"))
87 execute_process(
88 COMMAND ${ZIP_EXECUTABLE} e -y ${TMP_PATH}
89 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
90 RESULT_VARIABLE Failure
91 OUTPUT_QUIET
92 )
93
94 if (Failure)
95 message(FATAL_ERROR "Error while running the uncompression tool")
96 endif()
97
98 if ("${TMP_EXTENSION}" STREQUAL "tgz")
99 string(REGEX REPLACE ".tgz$" ".tar" TMP_FILENAME2 "${TMP_FILENAME}")
100 else()
101 string(REGEX REPLACE ".gz$" "" TMP_FILENAME2 "${TMP_FILENAME}")
102 endif()
103
104 execute_process(
105 COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_FILENAME2}
106 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
107 RESULT_VARIABLE Failure
108 OUTPUT_QUIET
109 )
110 elseif ("${TMP_EXTENSION}" STREQUAL "zip")
111 execute_process(
112 COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_PATH}
113 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
114 RESULT_VARIABLE Failure
115 OUTPUT_QUIET
116 )
117 else()
118 message(FATAL_ERROR "Support your platform here")
119 endif()
120
121 else()
122 if ("${TMP_EXTENSION}" STREQUAL "zip")
123 execute_process(
124 COMMAND sh -c "${UNZIP_EXECUTABLE} -q ${TMP_PATH}"
125 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
126 RESULT_VARIABLE Failure
127 )
128 elseif (("${TMP_EXTENSION}" STREQUAL "gz") OR ("${TMP_EXTENSION}" STREQUAL "tgz"))
129 #message("tar xvfz ${TMP_PATH}")
130 execute_process(
131 COMMAND sh -c "${TAR_EXECUTABLE} xfz ${TMP_PATH}"
132 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
133 RESULT_VARIABLE Failure
134 )
135 elseif ("${TMP_EXTENSION}" STREQUAL "bz2")
136 execute_process(
137 COMMAND sh -c "${TAR_EXECUTABLE} xfj ${TMP_PATH}"
138 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
139 RESULT_VARIABLE Failure
140 )
141 else()
142 message(FATAL_ERROR "Unknown package format.")
143 endif()
144 endif()
145
146 if (Failure)
147 message(FATAL_ERROR "Error while running the uncompression tool")
148 endif()
149
150 if (NOT IS_DIRECTORY "${TargetDirectory}")
151 message(FATAL_ERROR "The package was not uncompressed at the proper location. Check the CMake instructions.")
152 endif()
153 endif()
154 endmacro()