2
|
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 macro(DownloadPackage Url TargetDirectory PreloadedVariable UncompressArguments)
|
|
14 if (NOT IS_DIRECTORY "${TargetDirectory}")
|
|
15 GetUrlFilename(TMP_FILENAME "${Url}")
|
|
16 if ("${PreloadedVariable}" STREQUAL "")
|
|
17 set(TMP_PATH "${CMAKE_SOURCE_DIR}/ThirdPartyDownloads/${TMP_FILENAME}")
|
|
18 if (NOT EXISTS "${TMP_PATH}")
|
|
19 message("Downloading ${Url}")
|
|
20 file(DOWNLOAD "${Url}" "${TMP_PATH}" SHOW_PROGRESS)
|
|
21 else()
|
|
22 message("Already downloaded ${Url}")
|
|
23 endif()
|
|
24 else()
|
|
25 message("Using preloaded archive ${PreloadedVariable} for ${Url}")
|
|
26 set(TMP_PATH "${PreloadedVariable}")
|
|
27 endif()
|
|
28
|
|
29 GetUrlExtension(TMP_EXTENSION "${Url}")
|
|
30 #message(${TMP_EXTENSION})
|
|
31
|
|
32 if ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Linux")
|
|
33 if ("${TMP_EXTENSION}" STREQUAL "zip")
|
|
34 execute_process(
|
|
35 COMMAND sh -c "unzip ${TMP_PATH} ${UncompressArguments}"
|
|
36 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
|
37 )
|
|
38 elseif (("${TMP_EXTENSION}" STREQUAL "gz") OR ("${TMP_EXTENSION}" STREQUAL "tgz"))
|
|
39 #message("tar xvfz ${TMP_PATH} ${UncompressArguments}")
|
|
40 execute_process(
|
|
41 COMMAND sh -c "tar xvfz ${TMP_PATH} ${UncompressArguments}"
|
|
42 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
|
43 )
|
|
44 elseif ("${TMP_EXTENSION}" STREQUAL "bz2")
|
|
45 execute_process(
|
|
46 COMMAND sh -c "tar xvfj ${TMP_PATH} ${UncompressArguments}"
|
|
47 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
|
48 )
|
|
49 else()
|
|
50 message(FATAL_ERROR "Unknown package format.")
|
|
51 endif()
|
|
52
|
|
53 elseif ("${CMAKE_HOST_SYSTEM_NAME}" STREQUAL "Windows")
|
|
54 FIND_PROGRAM(ZIP_EXECUTABLE 7z PATHS "$ENV{ProgramFiles}/7-Zip")
|
|
55
|
|
56 if (("${TMP_EXTENSION}" STREQUAL "gz") OR ("${TMP_EXTENSION}" STREQUAL "tgz"))
|
|
57 execute_process(
|
|
58 COMMAND ${ZIP_EXECUTABLE} e ${TMP_PATH}
|
|
59 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
|
60 )
|
|
61 set(ARGS ${UncompressArguments})
|
|
62 SEPARATE_ARGUMENTS(ARGS)
|
|
63 list(LENGTH ARGS TMP_LENGTH)
|
|
64
|
|
65 if ("${TMP_EXTENSION}" STREQUAL "tgz")
|
|
66 string(REGEX REPLACE ".tgz$" ".tar" TMP_FILENAME2 "${TMP_FILENAME}")
|
|
67 else()
|
|
68 string(REGEX REPLACE ".gz$" "" TMP_FILENAME2 "${TMP_FILENAME}")
|
|
69 endif()
|
|
70
|
|
71 if (TMP_LENGTH EQUAL 0)
|
|
72 execute_process(
|
|
73 COMMAND ${ZIP_EXECUTABLE} x ${TMP_FILENAME2}
|
|
74 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
|
75 )
|
|
76 else()
|
|
77 foreach(SUBDIR ${ARGS})
|
|
78 execute_process(
|
|
79 COMMAND ${ZIP_EXECUTABLE} x "-i!${SUBDIR}" "${TMP_FILENAME2}"
|
|
80 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
|
81 )
|
|
82 endforeach()
|
|
83 endif()
|
|
84 elseif ("${TMP_EXTENSION}" STREQUAL "zip")
|
|
85 execute_process(
|
|
86 COMMAND ${ZIP_EXECUTABLE} x ${TMP_PATH}
|
|
87 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
|
88 )
|
|
89 else()
|
|
90 message(FATAL_ERROR "Support your platform here")
|
|
91 endif()
|
|
92 else()
|
|
93 message(FATAL_ERROR "Support your platform here")
|
|
94 endif()
|
|
95
|
|
96 if (NOT IS_DIRECTORY "${TargetDirectory}")
|
|
97 message(FATAL_ERROR "The package was not uncompressed at the proper location. Check the CMake instructions.")
|
|
98 endif()
|
|
99 endif()
|
|
100 endmacro()
|
|
101
|