comparison Orthanc/Resources/CMake/DownloadPackage.cmake @ 25:15acbf5e7545

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 02 Jun 2015 11:16:30 +0200
parents
children
comparison
equal deleted inserted replaced
24:ed9acb0f938e 25:15acbf5e7545
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 ## Check the existence of the required decompression tools
15 ##
16
17 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
18 find_program(ZIP_EXECUTABLE 7z
19 PATHS
20 "$ENV{ProgramFiles}/7-Zip"
21 "$ENV{ProgramW6432}/7-Zip"
22 )
23
24 if (${ZIP_EXECUTABLE} MATCHES "ZIP_EXECUTABLE-NOTFOUND")
25 message(FATAL_ERROR "Please install the '7-zip' software (http://www.7-zip.org/)")
26 endif()
27
28 else()
29 find_program(UNZIP_EXECUTABLE unzip)
30 if (${UNZIP_EXECUTABLE} MATCHES "UNZIP_EXECUTABLE-NOTFOUND")
31 message(FATAL_ERROR "Please install the 'unzip' package")
32 endif()
33
34 find_program(TAR_EXECUTABLE tar)
35 if (${TAR_EXECUTABLE} MATCHES "TAR_EXECUTABLE-NOTFOUND")
36 message(FATAL_ERROR "Please install the 'tar' package")
37 endif()
38 endif()
39
40
41 macro(DownloadPackage MD5 Url TargetDirectory)
42 if (NOT IS_DIRECTORY "${TargetDirectory}")
43 GetUrlFilename(TMP_FILENAME "${Url}")
44
45 set(TMP_PATH "${CMAKE_SOURCE_DIR}/ThirdPartyDownloads/${TMP_FILENAME}")
46 if (NOT EXISTS "${TMP_PATH}")
47 message("Downloading ${Url}")
48
49 # This fixes issue 6: "I think cmake shouldn't download the
50 # packages which are not in the system, it should stop and let
51 # user know."
52 # https://code.google.com/p/orthanc/issues/detail?id=6
53 if (NOT STATIC_BUILD AND NOT ALLOW_DOWNLOADS)
54 message(FATAL_ERROR "CMake is not allowed to download from Internet. Please set the ALLOW_DOWNLOADS option to ON")
55 endif()
56
57 file(DOWNLOAD "${Url}" "${TMP_PATH}" SHOW_PROGRESS EXPECTED_MD5 "${MD5}")
58 else()
59 message("Using local copy of ${Url}")
60 endif()
61
62 GetUrlExtension(TMP_EXTENSION "${Url}")
63 #message(${TMP_EXTENSION})
64 message("Uncompressing ${TMP_FILENAME}")
65
66 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
67 # How to silently extract files using 7-zip
68 # http://superuser.com/questions/331148/7zip-command-line-extract-silently-quietly
69
70 if (("${TMP_EXTENSION}" STREQUAL "gz") OR ("${TMP_EXTENSION}" STREQUAL "tgz"))
71 execute_process(
72 COMMAND ${ZIP_EXECUTABLE} e -y ${TMP_PATH}
73 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
74 RESULT_VARIABLE Failure
75 OUTPUT_QUIET
76 )
77
78 if (Failure)
79 message(FATAL_ERROR "Error while running the uncompression tool")
80 endif()
81
82 if ("${TMP_EXTENSION}" STREQUAL "tgz")
83 string(REGEX REPLACE ".tgz$" ".tar" TMP_FILENAME2 "${TMP_FILENAME}")
84 else()
85 string(REGEX REPLACE ".gz$" "" TMP_FILENAME2 "${TMP_FILENAME}")
86 endif()
87
88 execute_process(
89 COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_FILENAME2}
90 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
91 RESULT_VARIABLE Failure
92 OUTPUT_QUIET
93 )
94 elseif ("${TMP_EXTENSION}" STREQUAL "zip")
95 execute_process(
96 COMMAND ${ZIP_EXECUTABLE} x -y ${TMP_PATH}
97 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
98 RESULT_VARIABLE Failure
99 OUTPUT_QUIET
100 )
101 else()
102 message(FATAL_ERROR "Support your platform here")
103 endif()
104
105 else()
106 if ("${TMP_EXTENSION}" STREQUAL "zip")
107 execute_process(
108 COMMAND sh -c "${UNZIP_EXECUTABLE} -q ${TMP_PATH}"
109 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
110 RESULT_VARIABLE Failure
111 )
112 elseif (("${TMP_EXTENSION}" STREQUAL "gz") OR ("${TMP_EXTENSION}" STREQUAL "tgz"))
113 #message("tar xvfz ${TMP_PATH}")
114 execute_process(
115 COMMAND sh -c "${TAR_EXECUTABLE} xfz ${TMP_PATH}"
116 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
117 RESULT_VARIABLE Failure
118 )
119 elseif ("${TMP_EXTENSION}" STREQUAL "bz2")
120 execute_process(
121 COMMAND sh -c "${TAR_EXECUTABLE} xfj ${TMP_PATH}"
122 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
123 RESULT_VARIABLE Failure
124 )
125 else()
126 message(FATAL_ERROR "Unknown package format.")
127 endif()
128 endif()
129
130 if (Failure)
131 message(FATAL_ERROR "Error while running the uncompression tool")
132 endif()
133
134 if (NOT IS_DIRECTORY "${TargetDirectory}")
135 message(FATAL_ERROR "The package was not uncompressed at the proper location. Check the CMake instructions.")
136 endif()
137 endif()
138 endmacro()