forked from aobolensk/pp_2020_spring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTBBGet.cmake
294 lines (243 loc) · 10.8 KB
/
TBBGet.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# Copyright (c) 2017-2020 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
include(CMakeParseArguments)
# Save the location of Intel TBB CMake modules here, as it will not be possible to do inside functions,
# see for details: https://cmake.org/cmake/help/latest/variable/CMAKE_CURRENT_LIST_DIR.html
set(_tbb_cmake_module_path ${CMAKE_CURRENT_LIST_DIR})
##
# Downloads file.
#
# Parameters:
# URL <url> - URL to download data from;
# SAVE_AS <filename> - filename there to save downloaded data;
# INFO <string> - text description of content to be downloaded;
# will be printed as message in format is "Downloading <INFO>: <URL>;
# FORCE - option to delete local file from SAVE_AS if it exists;
#
function(_tbb_download_file)
set(options FORCE)
set(oneValueArgs URL RELEASE SAVE_AS INFO)
cmake_parse_arguments(tbb_df "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if (tbb_df_FORCE AND EXISTS "${tbb_df_SAVE_AS}")
file(REMOVE ${tbb_df_SAVE_AS})
endif()
if (NOT EXISTS "${tbb_df_SAVE_AS}")
set(_show_progress)
if (TBB_DOWNLOADING_PROGRESS)
set(_show_progress SHOW_PROGRESS)
endif()
message(STATUS "Downloading ${tbb_df_INFO}: ${tbb_df_URL}")
file(DOWNLOAD ${tbb_df_URL} ${tbb_df_SAVE_AS} ${_show_progress} STATUS download_status)
list(GET download_status 0 download_status_num)
if (NOT download_status_num EQUAL 0)
message(STATUS "Unsuccessful downloading: ${download_status}")
file(REMOVE ${tbb_df_SAVE_AS})
return()
endif()
else()
message(STATUS "Needed file was found locally ${tbb_df_SAVE_AS}. Remove it if you still want to download a new one")
endif()
endfunction()
##
# Checks if specified Intel TBB release is available on GitHub.
#
# tbb_check_git_release(<release> <result>)
# Parameters:
# <release_tag> - release to be checked;
# <result> - store result (TRUE/FALSE).
#
function(_tbb_check_git_release_tag _tbb_release_tag _tbb_release_tag_avail)
if (_tbb_release_tag STREQUAL LATEST)
set(${_tbb_release_tag_avail} TRUE PARENT_SCOPE)
return()
endif()
set(tbb_releases_file "${CMAKE_CURRENT_BINARY_DIR}/tbb_releases.json")
_tbb_download_file(URL "${tbb_github_api}/releases"
SAVE_AS ${tbb_releases_file}
INFO "information from GitHub about Intel TBB releases"
FORCE)
if (NOT EXISTS "${tbb_releases_file}")
set(${_tbb_release_tag_avail} FALSE PARENT_SCOPE)
return()
endif()
file(READ ${tbb_releases_file} tbb_releases)
string(REPLACE "\"" "" tbb_releases ${tbb_releases})
string(REGEX MATCHALL "tag_name: *([A-Za-z0-9_\\.]+)" tbb_releases ${tbb_releases})
set(_release_available FALSE)
foreach(tbb_rel ${tbb_releases})
string(REGEX REPLACE "tag_name: *" "" tbb_rel_cut ${tbb_rel})
list(REMOVE_ITEM tbb_releases ${tbb_rel})
list(APPEND tbb_releases ${tbb_rel_cut})
if (_tbb_release_tag STREQUAL tbb_rel_cut)
set(_release_available TRUE)
break()
endif()
endforeach()
if (NOT _release_available)
string(REPLACE ";" ", " tbb_releases_str "${tbb_releases}")
message(STATUS "Requested release tag ${_tbb_release_tag} is not available. Available Intel TBB release tags: ${tbb_releases_str}")
endif()
set(${_tbb_release_tag_avail} ${_release_available} PARENT_SCOPE)
endfunction()
##
# Compares two Intel TBB releases and provides result
# TRUE if the first release is less than the second, FALSE otherwise.
#
# tbb_is_release_less(<rel1> <rel2> <result>)
#
function(_tbb_is_release_less rel1 rel2 result)
# Convert release to numeric representation to compare it using "if" with VERSION_LESS.
string(REGEX REPLACE "[A-Za-z]" "" rel1 "${rel1}")
string(REPLACE "_" "." rel1 "${rel1}")
string(REGEX REPLACE "[A-Za-z]" "" rel2 "${rel2}")
string(REPLACE "_" "." rel2 "${rel2}")
if (${rel1} VERSION_LESS ${rel2})
set(${result} TRUE PARENT_SCOPE)
return()
endif()
set(${result} FALSE PARENT_SCOPE)
endfunction()
##
# Finds exact URL to download Intel TBB basing on provided parameters.
#
# Usage:
# _tbb_get_url(URL <var_to_save_url> RELEASE_TAG <release_tag|LATEST> OS <os> [SOURCE_CODE])
#
function(_tbb_get_url)
set(oneValueArgs URL RELEASE_TAG OS)
set(options SOURCE_CODE)
cmake_parse_arguments(tbb_get_url "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
set(tbb_github_api "https://api.github.com/repos/01org/tbb")
_tbb_check_git_release_tag(${tbb_get_url_RELEASE_TAG} tbb_release_available)
if (NOT tbb_release_available)
set(${tbb_download_FULL_PATH} ${tbb_download_FULL_PATH}-NOTFOUND PARENT_SCOPE)
return()
endif()
if (tbb_get_url_RELEASE_TAG STREQUAL LATEST)
set(tbb_rel_info_api_url "${tbb_github_api}/releases/latest")
else()
set(tbb_rel_info_api_url "${tbb_github_api}/releases/tags/${tbb_get_url_RELEASE_TAG}")
endif()
set(tbb_release_info_file "${CMAKE_CURRENT_BINARY_DIR}/tbb_${tbb_get_url_RELEASE_TAG}_info.json")
_tbb_download_file(URL ${tbb_rel_info_api_url}
SAVE_AS ${tbb_release_info_file}
INFO "information from GitHub about packages for Intel TBB ${tbb_get_url_RELEASE_TAG}"
FORCE)
if (NOT EXISTS "${tbb_release_info_file}")
set(${tbb_get_url_URL} ${tbb_get_url_URL}-NOTFOUND PARENT_SCOPE)
return()
endif()
file(STRINGS ${tbb_release_info_file} tbb_release_info)
if (tbb_get_url_SOURCE_CODE)
# Find name of the latest release to get link to source archive.
if (tbb_get_url_RELEASE_TAG STREQUAL LATEST)
string(REPLACE "\"" "" tbb_release_info ${tbb_release_info})
string(REGEX REPLACE ".*tag_name: *([A-Za-z0-9_\\.]+).*" "\\1" tbb_get_url_RELEASE_TAG "${tbb_release_info}")
endif()
set(${tbb_get_url_URL} "https://github.com/01org/tbb/archive/${tbb_get_url_RELEASE_TAG}.tar.gz" PARENT_SCOPE)
else()
if (tbb_get_url_OS MATCHES "Linux")
set(tbb_lib_archive_suffix lin.tgz)
elseif (tbb_get_url_OS MATCHES "Windows")
set(tbb_lib_archive_suffix win.zip)
elseif (tbb_get_url_OS MATCHES "Darwin")
set(tbb_lib_archive_suffix mac.tgz)
# Since 2017_U4 release archive for Apple has suffix "mac.tgz" instead of "osx.tgz".
if (NOT tbb_get_url_RELEASE_TAG STREQUAL "LATEST")
_tbb_is_release_less(${tbb_get_url_RELEASE_TAG} 2017_U4 release_less)
if (release_less)
set(tbb_lib_archive_suffix osx.tgz)
endif()
endif()
elseif (tbb_get_url_OS MATCHES "Android")
set(tbb_lib_archive_suffix and.tgz)
else()
message(STATUS "Currently prebuilt Intel TBB is not available for your OS (${tbb_get_url_OS})")
set(${tbb_get_url_URL} ${tbb_get_url_URL}-NOTFOUND PARENT_SCOPE)
return()
endif()
string(REGEX REPLACE ".*(https.*${tbb_lib_archive_suffix}).*" "\\1" tbb_bin_url "${tbb_release_info}")
set(${tbb_get_url_URL} ${tbb_bin_url} PARENT_SCOPE)
endif()
endfunction()
function(tbb_get)
set(oneValueArgs RELEASE_TAG SYSTEM_NAME SAVE_TO TBB_ROOT CONFIG_DIR)
set(options SOURCE_CODE)
cmake_parse_arguments(tbb_get "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
set(tbb_os ${CMAKE_SYSTEM_NAME})
if (tbb_get_SYSTEM_NAME)
set(tbb_os ${tbb_get_SYSTEM_NAME})
endif()
set(tbb_release_tag LATEST)
if (tbb_get_RELEASE_TAG)
set(tbb_release_tag ${tbb_get_RELEASE_TAG})
endif()
set(tbb_save_to ${CMAKE_CURRENT_BINARY_DIR}/tbb_downloaded)
if (tbb_get_SAVE_TO)
set(tbb_save_to ${tbb_get_SAVE_TO})
endif()
if (tbb_get_SOURCE_CODE)
_tbb_get_url(URL tbb_url RELEASE_TAG ${tbb_release_tag} OS ${tbb_os} SOURCE_CODE)
else()
_tbb_get_url(URL tbb_url RELEASE_TAG ${tbb_release_tag} OS ${tbb_os})
endif()
if (NOT tbb_url)
message(STATUS "URL to download Intel TBB has not been found")
set(${tbb_get_TBB_ROOT} ${tbb_get_TBB_ROOT}-NOTFOUND PARENT_SCOPE)
return()
endif()
get_filename_component(filename ${tbb_url} NAME)
set(local_file "${CMAKE_CURRENT_BINARY_DIR}/${filename}")
_tbb_download_file(URL ${tbb_url}
SAVE_AS ${local_file}
INFO "Intel TBB library")
if (NOT EXISTS "${local_file}")
set(${tbb_get_TBB_ROOT} ${tbb_get_TBB_ROOT}-NOTFOUND PARENT_SCOPE)
return()
endif()
get_filename_component(subdir_name ${filename} NAME_WE)
file(MAKE_DIRECTORY ${tbb_save_to}/${subdir_name})
if (NOT EXISTS "${tbb_save_to}/${subdir_name}")
message(STATUS "${tbb_save_to}/${subdir_name} can not be created")
set(${tbb_get_TBB_ROOT} ${tbb_get_TBB_ROOT}-NOTFOUND PARENT_SCOPE)
return()
endif()
message(STATUS "Unpacking ${local_file} to ${tbb_save_to}/${subdir_name}")
execute_process(COMMAND ${CMAKE_COMMAND} -E tar xzf ${local_file}
WORKING_DIRECTORY ${tbb_save_to}/${subdir_name}
RESULT_VARIABLE unpacking_result)
if (NOT unpacking_result EQUAL 0)
message(STATUS "Unsuccessful unpacking: ${unpacking_result}")
set(${tbb_get_TBB_ROOT} ${tbb_get_TBB_ROOT}-NOTFOUND PARENT_SCOPE)
return()
endif()
file(GLOB_RECURSE tbb_h ${tbb_save_to}/${subdir_name}/*/include/tbb/tbb.h)
list(GET tbb_h 0 tbb_h)
if (NOT EXISTS "${tbb_h}")
message(STATUS "tbb/tbb.h has not been found in the downloaded package")
set(${tbb_get_TBB_ROOT} ${tbb_get_TBB_ROOT}-NOTFOUND PARENT_SCOPE)
return()
endif()
get_filename_component(tbb_root "${tbb_h}" PATH)
get_filename_component(tbb_root "${tbb_root}" PATH)
get_filename_component(tbb_root "${tbb_root}" PATH)
if (NOT tbb_get_SOURCE_CODE)
set(tbb_config_dir ${tbb_root}/cmake)
if (NOT EXISTS "${tbb_config_dir}")
tbb_make_config(TBB_ROOT ${tbb_root} CONFIG_DIR tbb_config_dir)
endif()
set(${tbb_get_CONFIG_DIR} ${tbb_config_dir} PARENT_SCOPE)
endif()
set(${tbb_get_TBB_ROOT} ${tbb_root} PARENT_SCOPE)
endfunction()