forked from intel/opencl-clang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
306 lines (254 loc) · 11.1 KB
/
CMakeLists.txt
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
295
296
297
298
299
300
301
302
303
304
305
#
# Copyright (c) 2017, Intel Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
#
# Common clang
#
cmake_minimum_required(VERSION 3.4.3)
set(CCLANG_LIB_NAME cclang)
set(CCLANG_LIB_NAME ${CCLANG_LIB_NAME} PARENT_SCOPE)
if(NOT DEFINED COMMON_CLANG_LIBRARY_NAME)
set(COMMON_CLANG_LIBRARY_NAME common_clang)
endif()
if(NOT PATCH)
find_program(PATCH NAMES patch)
endif()
# function version_from_exe - Decode the output of "${exefile} --version".
# The expected version number format is three or fewer decimal values
# separated by periods.
#
# The caller passes in variable names to be set by this function:
# - _ver_full - Full version string, like "3" or "3.8" or "3.8.5"
# - _vermajor - major version - first field
# - _verminor - minor version - second field
# - _vermicro - "micro" version - third field
#
# Fields not present are returned as blank.
#
# Only the first line from "exefile --version" is examined.
#
# Processing:
# - All text in parentheses is discard.
# - Discarded: The first word and, if present, "version".
# - The first remaining word is considered the version number.
#
# Example version output (first line only):
#
# Fedora example:
## $ clang --version
## clang version 5.0.1 (tags/RELEASE_501/final)
#
## $ gcc --version
## gcc (GCC) 7.2.1 20170915 (Red Hat 7.2.1-2)
#
# Ubuntu example:
## $ clang --version
## clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
#
## $ gcc --version
## gcc (Ubuntu 5.4.0-6ubuntu1~16.04.6) 5.4.0 20160609
function (version_from_exe exefile _ver_full _vermajor _verminor _vermicro)
execute_process(
COMMAND "${exefile}" "--version"
RESULT_VARIABLE var_result
OUTPUT_VARIABLE var_stdout
ERROR_VARIABLE var_stderr
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_STRIP_TRAILING_WHITESPACE
)
if(NOT var_result EQUAL 0)
message("version number request failed: ${var_result}: \"${exefile}\"")
if(NOT "${var_stderr}" STREQUAL "")
message("version request: \"${var_stderr}\"")
endif()
set(${_ver_full} "")
set(${_vermajor} "")
set(${_verminor} "")
set(${_vermicro} "")
return()
endif()
# Discard all but first line
string(FIND "${var_stdout}" "\n" nl)
string(SUBSTRING "${var_stdout}" 0 ${nl} var_stdout)
# Remove all parenthesized items.
string(REGEX REPLACE "\\([^)]+\\)" "" var_stdout "${var_stdout}")
# Remove first word and optional following "version"
string(REGEX REPLACE "^[A-Za-z0-9_]+( +version +)? +" "" var_stdout "${var_stdout}")
string(REGEX MATCH "^([0-9]+)((\\.([0-9]+))(\\.([0-9]+))?)?" var_stdout "${var_stdout}")
# ( )(( ( ))( ( )) )
# 1 23 4 5 6
set("${_ver_full}" "${CMAKE_MATCH_0}" PARENT_SCOPE)
set("${_vermajor}" "${CMAKE_MATCH_1}" PARENT_SCOPE)
set("${_verminor}" "${CMAKE_MATCH_4}" PARENT_SCOPE)
set("${_vermicro}" "${CMAKE_MATCH_6}" PARENT_SCOPE)
endfunction()
if(NOT DEFINED CLANG)
find_program(CLANG NAMES clang)
if("${CLANG}" STREQUAL "CLANG-NOTFOUND" )
unset(CLANG CACHE)
else()
version_from_exe("${CLANG}" "clang_ver_full" "clang_ver_major" "clang_ver_minor" "clang_ver_micro")
if("${clang_ver_major}" STREQUAL "")
unset(CLANG CACHE)
elseif("${clang_ver_major}" LESS 4)
unset(CLANG CACHE)
endif()
endif()
endif()
if(NOT DEFINED CLANGXX)
find_program(CLANGXX NAMES clang++)
if("${CLANGXX}" STREQUAL "CLANGXX-NOTFOUND" )
unset(CLANGXX CACHE)
else()
version_from_exe("${CLANGXX}" "clangxx_ver_full" "clangxx_ver_major" "clangxx_ver_minor" "clangxx_ver_micro")
if("${clangxx_ver_major}" STREQUAL "")
unset(CLANGXX CACHE)
elseif("${clangxx_ver_major}" LESS 4)
unset(CLANGXX CACHE)
endif()
endif()
endif()
if((NOT DEFINED CLANG ) OR (NOT DEFINED CLANGXX ))
if((DEFINED CLANG ) OR (DEFINED CLANGXX ))
message(SEND_ERROR "${CMAKE_CURRENT_LIST_FILE}: CLANG = \"${CLANG}\"")
message(SEND_ERROR "${CMAKE_CURRENT_LIST_FILE}: CLANGXX = \"${CLANGXX}\"")
message(FATAL_ERROR "${CMAKE_CURRENT_LIST_FILE}: inconsistent CLANG vs CLANGXX")
endif()
endif()
if(NOT DEFINED CLANG)
find_program(CLANG NAMES clang-4.0)
if("${CLANG}" STREQUAL "CLANG-NOTFOUND" )
message(FATAL_ERROR "${CMAKE_CURRENT_LIST_FILE}: clang-4.0 not found")
endif()
endif()
if(NOT DEFINED CLANGXX)
find_program(CLANGXX NAMES clang++-4.0)
if("${CLANGXX}" STREQUAL "CLANGXX-NOTFOUND" )
message(FATAL_ERROR "${CMAKE_CURRENT_LIST_FILE}: clang++-4.0 not found")
endif()
endif()
set(CCLANG_OPTIONS
"-DBUILD_X64:BOOL=ON"
"-DLLVM_USE_INTEL_JITEVENTS:BOOL=ON"
"-DLLVM_USE_LINKER:STRING=gold"
"-DCMAKE_CXX_COMPILER:FILEPATH=${CLANGXX}"
"-DCMAKE_C_COMPILER:FILEPATH=${CLANG}"
"-DLLVM_OPTIMIZED_TABLEGEN=ON"
"-DLLVM_TARGETS_TO_BUILD='X86'"
"-DCOMMON_CLANG_LIBRARY_NAME:STRING=${COMMON_CLANG_LIBRARY_NAME}"
)
if(NOT LLVM_STOCK_SOURCE_DIR)
set(LLVM_STOCK_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../llvm_source")
endif()
if(NOT CLANG_SOURCE_DIR)
set(CLANG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../clang_source")
endif()
if(NOT CCLANG_OCL_HEADERS_DIR)
set(CCLANG_OCL_HEADERS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../opencl_headers")
endif()
set (CCLANG_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cclang")
if(NOT CCLANG_INSTALL_DIR)
set(CCLANG_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/install")
endif()
# Use LLVM sources stored at LLVM_STOCK_SOURCE_DIR.
set(LLVM_STOCK_SOURCE_URL ${LLVM_STOCK_SOURCE_DIR})
# Also tar archive with stock/pure LLVM sources can be used. Just specify the path ...
# set(LLVM_STOCK_SOURCE_URL "${CMAKE_CURRENT_SOURCE_DIR}/llvm-4.0.0.src.tar.xz")
# Use CLANG sources stored at CLANG_SOURCE_DIR.
set(CLANG_SOURCE_URL ${CLANG_SOURCE_DIR})
# Use CCLANG sources stored at CCLANG_SOURCE_DIR
set(CCLANG_SOURCE_URL ${CCLANG_SOURCE_DIR})
message(STATUS "LLVM_SOURCE_URL = ${LLVM_STOCK_SOURCE_URL}")
message(STATUS "CLANG_SOURCE_URL = ${CLANG_SOURCE_URL}")
message(STATUS "CCLANG_LIB_NAME = ${CCLANG_LIB_NAME}")
message(STATUS "CCLANG_SOURCE_URL = ${CCLANG_SOURCE_URL}")
message(STATUS "CCLANG_OCL_HEADERS_DIR = ${CCLANG_OCL_HEADERS_DIR}")
message(STATUS "CCLANG_BUILD_TYPE = ${CMAKE_BUILD_TYPE}")
message(STATUS "CCLANG_INSTALL_DIR = ${CCLANG_INSTALL_DIR}")
message(STATUS "CCLANG_OPTIONS = ${CCLANG_OPTIONS}")
# Customization patches will be applied to LLVM if present.
file(GLOB LLVM_PATCH_FILES "${CMAKE_CURRENT_SOURCE_DIR}/llvm_patches/releases/4.0.0/patches/*.patch")
# Sort list of patch files.
list(SORT LLVM_PATCH_FILES)
# Customization patches will be applied to CLANG if present.
file(GLOB CLANG_PATCH_FILES "${CMAKE_CURRENT_SOURCE_DIR}/clang_patches/releases/4.0.1/patches/*.patch")
# Sort list of patch files.
list(SORT CLANG_PATCH_FILES)
include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)
ExternalProject_Add(${CCLANG_LIB_NAME}
URL ${LLVM_STOCK_SOURCE_URL}
PREFIX src
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/src
CMAKE_ARGS -G ${CMAKE_GENERATOR}
-DCMAKE_BUILD_TYPE=$<$<CONFIG:Debug>:Debug>$<$<CONFIG:Release>:Release>$<$<CONFIG:ReleaseInternal>:Release>
-DCMAKE_INSTALL_PREFIX=${CCLANG_INSTALL_DIR}
${CCLANG_OPTIONS}
)
ExternalProject_Add_Step(${CCLANG_LIB_NAME} "add_clang"
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CLANG_SOURCE_URL} ${CMAKE_CURRENT_BINARY_DIR}/src/src/${CCLANG_LIB_NAME}/tools/clang
DEPENDEES download
COMMENT "Copying ${CLANG_SOURCE_URL} to ${CMAKE_CURRENT_BINARY_DIR}/src/src/${CCLANG_LIB_NAME}/tools/clang"
)
ExternalProject_Add_Step(${CCLANG_LIB_NAME} "add_cclang"
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CCLANG_SOURCE_URL} ${CMAKE_CURRENT_BINARY_DIR}/src/src/${CCLANG_LIB_NAME}/projects/cclang
DEPENDEES add_clang
COMMENT "Copying ${CCLANG_SOURCE_URL} to ${CMAKE_CURRENT_BINARY_DIR}/src/src/${CCLANG_LIB_NAME}/projects/cclang"
)
ExternalProject_Add_Step(${CCLANG_LIB_NAME} "add_opencl_headers"
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CCLANG_OCL_HEADERS_DIR} ${CMAKE_CURRENT_BINARY_DIR}/src/src/${CCLANG_LIB_NAME}/projects/cclang
DEPENDEES add_cclang
DEPENDERS patch
COMMENT "Copying ${CCLANG_OCL_HEADERS_DIR} to ${CMAKE_CURRENT_BINARY_DIR}/src/src/${CCLANG_LIB_NAME}/projects/cclang"
)
# Apply customization patches to LLVM if any.
set(i 1)
set(patch_dependee "patch")
foreach(patch_file ${LLVM_PATCH_FILES})
ExternalProject_Add_Step(${CCLANG_LIB_NAME} "patch${i}"
COMMAND ${PATCH} -d "${CMAKE_CURRENT_BINARY_DIR}/src/src/${CCLANG_LIB_NAME}" -p1 < ${patch_file}
DEPENDEES ${patch_dependee}
DEPENDERS configure
COMMENT "Apply ${patch_file} file"
)
set(patch_dependee "patch${i}")
math(EXPR i "${i} + 1")
endforeach()
# Apply customization patches to CLANG if any.
foreach(patch_file ${CLANG_PATCH_FILES})
ExternalProject_Add_Step(${CCLANG_LIB_NAME} "patch${i}"
COMMAND ${PATCH} -d "${CMAKE_CURRENT_BINARY_DIR}/src/src/${CCLANG_LIB_NAME}/tools/clang" -p1 < ${patch_file}
DEPENDEES ${patch_dependee}
DEPENDERS configure
COMMENT "Apply ${patch_file} file"
)
set(patch_dependee "patch${i}")
math(EXPR i "${i} + 1")
endforeach()
if(CCLANG_INSTALL_ARTIFACTS_DIR)
ExternalProject_Add_Step(${CCLANG_LIB_NAME} "copy_artifacts"
COMMAND "${CMAKE_COMMAND}" -E make_directory "${CCLANG_INSTALL_ARTIFACTS_DIR}"
COMMAND ${CMAKE_COMMAND} -E copy "${CCLANG_INSTALL_DIR}/bin/clang-4.0" ${CCLANG_INSTALL_ARTIFACTS_DIR}/clang
COMMAND ${CMAKE_COMMAND} -E copy "${CCLANG_INSTALL_DIR}/lib/lib${COMMON_CLANG_LIBRARY_NAME}.so" ${CCLANG_INSTALL_ARTIFACTS_DIR}
COMMAND ${CMAKE_COMMAND} -E copy_directory "${CCLANG_INSTALL_DIR}/include/cclang/" ${CCLANG_INSTALL_ARTIFACTS_DIR}
DEPENDEES install
COMMENT "Copying common clang artifacts from ${CCLANG_INSTALL_DIR} to ${CCLANG_INSTALL_ARTIFACTS_DIR}"
)
endif()