-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.cmake
193 lines (160 loc) · 6.66 KB
/
utils.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
cmake_minimum_required(VERSION 3.28)
include(ExternalProject)
include(FetchContent)
include(ProcessorCount)
set(DEPENDENCIES_DIR ${CMAKE_BINARY_DIR}/_deps)
function(download_to target_filename target_url sha_256_hash
destination_path display_destination_path target_name)
if (OFFLINE_MODE EQUAL 1 OR "${target_url}" STREQUAL "")
file(COPY ${CMAKE_CURRENT_LIST_DIR}/dist/${target_filename} DESTINATION ${destination_path})
else ()
if (NOT target_name)
set(target_name "${target_filename}")
endif ()
if (display_destination_path)
message(STATUS "Downloading ${target_name} ${target_url} to ${destination_path}")
else ()
message(STATUS "Downloading ${target_name} ${target_url}")
endif ()
if ("${sha_256_hash}" STREQUAL "")
file(DOWNLOAD ${target_url} ${destination_path}/${target_filename} STATUS status)
else ()
file(
DOWNLOAD ${target_url} ${destination_path}/${target_filename}
EXPECTED_HASH SHA256=${sha_256_hash} STATUS status
)
endif ()
list(GET status 0 status_code)
list(GET status 1 error_message)
if (NOT status_code EQUAL 0)
message(FATAL_ERROR "Error downloading ${target_url}: ${error_message}")
endif ()
endif ()
endfunction()
function(unpack file_to_unpack unpack_to unpack_to_parent_dir)
if (NOT unpack_to_parent_dir)
set(unpack_to ${unpack_to}/..)
endif ()
file(ARCHIVE_EXTRACT INPUT "${file_to_unpack}" DESTINATION "${unpack_to}")
set(EXTERNAL_SOURCE_DIR ${unpack_to} PARENT_SCOPE)
endfunction()
function(patch_sources target_name patches_dir EXTERNAL_SOURCE_DIR)
if (NOT patches_dir)
return()
endif ()
message(STATUS "Patching ${target_name}")
file(GLOB PATCH_FILES "${patches_dir}/*.patch")
foreach (PATCH_FILE ${PATCH_FILES})
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
set(PATCH_EXECUTABLE "${CMAKE_PREFIX_PATH}/../usr/bin/patch.exe")
else ()
set(PATCH_EXECUTABLE "patch")
execute_process(
COMMAND lsdiff ${PATCH_FILE}
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE FILE_TO_PATCH
)
execute_process(
COMMAND dos2unix ${EXTERNAL_SOURCE_DIR}/${FILE_TO_PATCH}
ERROR_QUIET
)
endif ()
execute_process(
COMMAND ${PATCH_EXECUTABLE}
-ul -d ${EXTERNAL_SOURCE_DIR}
-p0 -i ${PATCH_FILE}
RESULT_VARIABLE PATCH_RESULT
OUTPUT_VARIABLE PATCH_OUTPUT
ERROR_VARIABLE PATCH_ERROR
)
if (NOT PATCH_RESULT EQUAL 0)
message(FATAL_ERROR
"Failed to apply patch ${PATCH_FILE}: ${PATCH_OUTPUT}"
"${PATCH_ERROR}"
)
endif ()
endforeach ()
endfunction()
function(unpack_and_patch file_to_unpack target_name unpack_to_parent_dir
target_unpacked_dir patches_dir)
unpack("${file_to_unpack}" "${DEPENDENCIES_DIR}/${target_name}" ${unpack_to_parent_dir})
set(EXTERNAL_SOURCE_DIR "${DEPENDENCIES_DIR}/${target_name}/${target_unpacked_dir}")
patch_sources("${target_name}" "${patches_dir}" "${EXTERNAL_SOURCE_DIR}")
set(EXTERNAL_SOURCE_DIR ${EXTERNAL_SOURCE_DIR} PARENT_SCOPE)
endfunction()
function(download_and_patch target_name target_filename target_url
sha_256_hash unpack_to_parent_dir target_unpacked_dir patches_dir)
if (OFFLINE_MODE EQUAL 1 OR "${target_url}" STREQUAL "")
unpack_and_patch("${CMAKE_CURRENT_LIST_DIR}/dist/${target_filename}" "${target_name}"
"${unpack_to_parent_dir}" "${target_unpacked_dir}" "${patches_dir}")
else ()
download_to("${target_filename}" "${target_url}" "${sha_256_hash}" "${DEPENDENCIES_DIR}/${target_name}"
false "${target_name}")
unpack_and_patch("${DEPENDENCIES_DIR}/${target_name}/${target_filename}" "${target_name}"
"${unpack_to_parent_dir}" "${target_unpacked_dir}" "${patches_dir}")
endif ()
set(EXTERNAL_SOURCE_DIR ${EXTERNAL_SOURCE_DIR} PARENT_SCOPE)
endfunction()
function(download_patch_and_add target_name target_filename target_url
sha_256_hash unpack_to_parent_dir target_unpacked_dir patches_dir)
download_and_patch(
"${target_name}" "${target_filename}" "${target_url}" "${sha_256_hash}" "${unpack_to_parent_dir}"
"${target_unpacked_dir}" "${patches_dir}"
)
ExternalProject_Add(
${target_name}
SOURCE_DIR ${EXTERNAL_SOURCE_DIR}
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)
set(EXTERNAL_SOURCE_DIR ${EXTERNAL_SOURCE_DIR} PARENT_SCOPE)
endfunction()
function(download_patch_and_make target_name target_filename target_url sha_256_hash
unpack_to_parent_dir target_unpacked_dir patches_dir make_args
build_byproducts)
download_and_patch(
"${target_name}" "${target_filename}" "${target_url}" "${sha_256_hash}" "${unpack_to_parent_dir}"
"${target_unpacked_dir}" "${patches_dir}"
)
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
set(make_command
${CMAKE_PREFIX_PATH}/../usr/bin/make.exe
AR=${CMAKE_PREFIX_PATH}/bin/ar.exe
)
else ()
set(make_command make)
endif ()
ProcessorCount(N)
if (N EQUAL 0)
set(N "")
else ()
math(EXPR N "${N}-1")
endif ()
set(make_command ${make_command} -j${N} ${make_args})
ExternalProject_Add(
${target_name}
SOURCE_DIR ${EXTERNAL_SOURCE_DIR}
CONFIGURE_COMMAND ""
BUILD_COMMAND ${make_command}
INSTALL_COMMAND ""
BUILD_IN_SOURCE 1
BUILD_BYPRODUCTS ${EXTERNAL_SOURCE_DIR}/${build_byproducts}
)
set(EXTERNAL_SOURCE_DIR ${EXTERNAL_SOURCE_DIR} PARENT_SCOPE)
endfunction()
function(download_patch_and_cmake target_name target_filename target_url
sha_256_hash unpack_to_parent_dir target_unpacked_dir patches_dir)
download_and_patch(
"${target_name}" "${target_filename}" "${target_url}" "${sha_256_hash}" "${unpack_to_parent_dir}"
"${target_unpacked_dir}" "${patches_dir}"
)
set(FETCHCONTENT_BASE_DIR "${DEPENDENCIES_DIR}/${target_name}")
FetchContent_Declare(
${target_name}
SOURCE_DIR ${EXTERNAL_SOURCE_DIR}
EXCLUDE_FROM_ALL
)
FetchContent_MakeAvailable(${target_name})
set(EXTERNAL_SOURCE_DIR ${EXTERNAL_SOURCE_DIR} PARENT_SCOPE)
endfunction()