forked from intel/opencl-clang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
349 lines (296 loc) · 11 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
cmake_minimum_required(VERSION 3.4.3)
if(NOT DEFINED BASE_LLVM_VERSION)
set(BASE_LLVM_VERSION 16.0.0)
endif(NOT DEFINED BASE_LLVM_VERSION)
set(OPENCL_CLANG_VERSION ${BASE_LLVM_VERSION}.0)
if(NOT DEFINED OPENCL_CLANG_BUILD_EXTERNAL)
# check if we build inside llvm or not
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(OPENCL_CLANG_BUILD_EXTERNAL YES)
endif(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
endif(NOT DEFINED OPENCL_CLANG_BUILD_EXTERNAL)
if(OPENCL_CLANG_BUILD_EXTERNAL)
project(OPENCL_CLANG
VERSION
${OPENCL_CLANG_VERSION}
LANGUAGES
CXX
C
)
endif()
# Do not omit TARGET_OBJECTS expression from the SOURCES target
# property
# `cmake --help-policy CMP0051` for details.
cmake_policy(SET CMP0051 NEW)
cmake_policy(SET CMP0057 NEW)
cmake_policy(SET CMP0022 NEW)
set(CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules")
include(CMakeFunctions)
if(LLVM_USE_HOST_TOOLS AND OPENCL_CLANG_BUILD_EXTERNAL)
include(CrossCompile)
llvm_create_cross_target(${PROJECT_NAME} NATIVE "" Release)
endif()
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(USE_PREBUILT_LLVM ON)
add_definitions(-DUSE_PREBUILT_LLVM)
if(NOT PREFERRED_LLVM_VERSION)
set(PREFERRED_LLVM_VERSION "16.0.0")
endif(NOT PREFERRED_LLVM_VERSION)
message(STATUS "[OPENCL-CLANG] Looking for LLVM version ${PREFERRED_LLVM_VERSION}")
find_package(LLVM ${PREFERRED_LLVM_VERSION} REQUIRED)
message(STATUS "[OPENCL-CLANG] Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "[OPENCL-CLANG] Found LLVM ${LLVM_PACKAGE_VERSION}")
set(CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
${LLVM_CMAKE_DIR})
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(LLVMSPIRV_INCLUDED_IN_LLVM
"Set to ON if libLLVMSPIRVLib is linked into libLLVM" ON)
if(LLVMSPIRV_INCLUDED_IN_LLVM)
message(STATUS "[OPENCL-CLANG] Assuming that libLLVMSPIRVLib is linked into libLLVM")
else(LLVMSPIRV_INCLUDED_IN_LLVM)
message(STATUS
"[OPENCL-CLANG] Assuming that libLLVMSPIRVLib is NOT linked into libLLVM")
if(NOT SPIRV_TRANSLATOR_DIR)
message(FATAL_ERROR "[OPENCL-CLANG] SPIRV_TRANSLATOR_DIR is required")
endif(NOT SPIRV_TRANSLATOR_DIR)
endif(LLVMSPIRV_INCLUDED_IN_LLVM)
else(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
set(USE_PREBUILT_LLVM OFF)
find_package(Git REQUIRED)
endif(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
include(AddLLVM)
include(TableGen)
if (NOT WIN32)
add_subdirectory( linux_linker bin)
endif()
if (CMAKE_SIZEOF_VOID_P EQUAL 4)
set(ADDR 32)
else ()
set(ADDR 64)
endif (CMAKE_SIZEOF_VOID_P EQUAL 4)
# set windows binary suffix
if (WIN32)
set (BUILD_PLATFORM ${ADDR})
else (WIN32)
set (BUILD_PLATFORM "")
endif (WIN32)
# set that name of the main output file as a target name
if (NOT DEFINED COMMON_CLANG_LIBRARY_NAME)
set(COMMON_CLANG_LIBRARY_NAME "opencl-clang")
endif()
set(TARGET_NAME ${COMMON_CLANG_LIBRARY_NAME}${BUILD_PLATFORM} )
if(NOT USE_PREBUILT_LLVM)
if(NOT LLVM_EXTERNAL_CLANG_SOURCE_DIR)
set(CLANG_SOURCE_DIR ${LLVM_SOURCE_DIR}/tools/clang)
elseif(EXISTS "${LLVM_EXTERNAL_CLANG_SOURCE_DIR}/CMakeLists.txt")
set(CLANG_SOURCE_DIR "${LLVM_EXTERNAL_CLANG_SOURCE_DIR}")
endif()
if(EXISTS ${CLANG_SOURCE_DIR})
message(STATUS "[OPENCL-CLANG] Using Clang source code direcotry: ${CLANG_SOURCE_DIR}")
else()
message(FATAL_ERROR
"[OPENCL-CLANG] Can't find Clang source code directory!\n"
"If you are using LLVM monorepo:\n"
" 1. Clean CMake cache: `rm CMakeCache.txt`\n"
" 2. Enable Clang project with `-DLLVM_ENABLE_PROJECTS=\"clang\"` option\n"
"If Clang is used as a separate repository (not monorepo), it should "
"be checked out at `llvm/tools/clang`."
)
endif()
if(NOT LLVM_EXTERNAL_LLVM_SPIRV_SOURCE_DIR)
set(SPIRV_SOURCE_DIR ${LLVM_SOURCE_DIR}/projects/llvm-spirv)
elseif(EXISTS "${LLVM_EXTERNAL_LLVM_SPIRV_SOURCE_DIR}/CMakeLists.txt")
set(SPIRV_SOURCE_DIR ${LLVM_EXTERNAL_LLVM_SPIRV_SOURCE_DIR})
endif()
if(EXISTS ${SPIRV_SOURCE_DIR})
message(STATUS "[OPENCL-CLANG] Using SPIRV-LLVM-Translator source code directory: ${SPIRV_SOURCE_DIR}")
else()
message(FATAL_ERROR
"[OPENCL-CLANG] Can't find SPIRV-LLVM-Translator source code dir!\n"
"If you are using LLVM monorepo, SPIRV-LLVM-Translator should be checked out "
"at '<monorepo_root_dir>/llvm-spirv' and it should be enabled as an external LLVM "
"project using the following 2 options:\n"
" -DLLVM_EXTERNAL_PROJECTS=\"opencl-clang;llvm-spirv\"\n"
" -DLLVM_EXTERNAL_LLVM_SPIRV_SOURCE_DIR=\"<monorepo_root_dir>/llvm-spirv\"\n"
"If you are not using LLVM monorepo, SPIRV-LLVM-Translator should be checked "
"out at `llvm/projects/llvm-spirv`"
)
endif()
set(CLANG_BASE_REVISION master)
set(SPIRV_BASE_REVISION master)
set(TARGET_BRANCH "ocl-open-110")
apply_patches(${CLANG_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/patches/clang
${CLANG_BASE_REVISION}
${TARGET_BRANCH})
apply_patches(${SPIRV_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/patches/spirv
${SPIRV_BASE_REVISION}
${TARGET_BRANCH})
endif(NOT USE_PREBUILT_LLVM)
#
# TblGen the options include file
#
set (COMPILE_OPTIONS_TD opencl_clang_options.td)
set (COMPILE_OPTIONS_INC opencl_clang_options.inc)
find_program(LLVM_TABLEGEN_EXE "llvm-tblgen" ${LLVM_TOOLS_BINARY_DIR})
set(LLVM_TARGET_DEFINITIONS ${COMPILE_OPTIONS_TD})
if(USE_PREBUILT_LLVM)
set(TABLEGEN_ADDITIONAL -I ${LLVM_INCLUDE_DIRS})
else(USE_PREBUILT_LLVM)
set(TABLEGEN_ADDITIONAL "")
endif(USE_PREBUILT_LLVM)
tablegen(LLVM ${COMPILE_OPTIONS_INC} -gen-opt-parser-defs ${TABLEGEN_ADDITIONAL})
add_public_tablegen_target(CClangCompileOptions)
#
# Source code
#
set(TARGET_INCLUDE_FILES
common_clang.h
options.h
binary_result.h
pch_mgr.h
${COMPILE_OPTIONS_TD}
${COMPILE_OPTIONS_INC}
)
set(TARGET_SOURCE_FILES
common_clang.cpp
options.cpp
pch_mgr.cpp
options_compile.cpp
)
#
# Resources
#
set( PRODUCT_VER_MAJOR 2 )
set( PRODUCT_VER_MINOR 0 )
set (LLVM_VER_MAJOR ${LLVM_VERSION_MAJOR} )
set (LLVM_VER_MINOR ${LLVM_VERSION_MINOR} )
add_definitions( -D__STDC_LIMIT_MACROS )
add_definitions( -D__STDC_CONSTANT_MACROS )
add_definitions( -DCOMMON_CLANG_EXPORTS )
#
# Include directories
#
if(NOT USE_PREBUILT_LLVM)
set(CLANG_BINARY_DIR ${LLVM_BINARY_DIR}/tools/clang/)
include_directories(
${CLANG_BINARY_DIR}/include # for tablegened includes
${CLANG_SOURCE_DIR}/include # for basic headers
${SPIRV_SOURCE_DIR}/include) # for SPIRV headers
endif(NOT USE_PREBUILT_LLVM)
if(USE_PREBUILT_LLVM AND NOT LLVMSPIRV_INCLUDED_IN_LLVM)
include_directories(${SPIRV_TRANSLATOR_DIR}/include)
link_directories(${SPIRV_TRANSLATOR_DIR}/lib${LLVM_LIBDIR_SUFFIX})
endif(USE_PREBUILT_LLVM AND NOT LLVMSPIRV_INCLUDED_IN_LLVM)
include_directories( AFTER
${LLVM_INCLUDE_DIRS}
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}
)
link_directories(
${LLVM_LIBRARY_DIRS}
)
set(OPENCL_CLANG_LINK_LIBS ${CMAKE_DL_LIBS})
if(NOT LLVMSPIRVLib IN_LIST LLVM_AVAILABLE_LIBS OR (USE_PREBUILT_LLVM AND LLVM_LINK_LLVM_DYLIB))
# SPIRV-LLVM-Translator is not included into LLVM as a component.
# So, we need to list it here explicitly as an external library
list(APPEND OPENCL_CLANG_LINK_LIBS LLVMSPIRVLib)
endif()
add_subdirectory(cl_headers)
set(LLVM_REQUIRES_EH ON)
if(USE_PREBUILT_LLVM OR CLANG_LINK_CLANG_DYLIB)
list(APPEND OPENCL_CLANG_LINK_LIBS clang-cpp)
else()
list(APPEND OPENCL_CLANG_LINK_LIBS
# The list of clang libraries is taken from clang makefile
# (build/tools/clang/tools/driver/CMakeFiles/clang.dir/link.txt)
# All duplicate libraries are there on purpose
clangBasic
clangCodeGen
clangDriver
clangFrontend
clangFrontendTool
clangCodeGen
clangRewriteFrontend
clangARCMigrate
clangStaticAnalyzerFrontend
clangStaticAnalyzerCheckers
clangStaticAnalyzerCore
clangCrossTU
clangIndex
clangFrontend
clangDriver
clangParse
clangSerialization
clangSema
clangAnalysis
clangEdit
clangFormat
clangToolingInclusions
clangToolingCore
clangRewrite
clangASTMatchers
clangAST
clangLex
clangBasic
)
endif()
add_llvm_library(${TARGET_NAME} SHARED
${TARGET_INCLUDE_FILES}
${TARGET_SOURCE_FILES}
$<TARGET_OBJECTS:cl_headers>
DEPENDS CClangCompileOptions
LINK_COMPONENTS
all
LINK_LIBS
${OPENCL_CLANG_LINK_LIBS}
)
# Configure resource file on Windows
if (WIN32)
# windows_resource_file should be defined by llvm_add_library and should
# contain full patch to a .rc file
# It also might not be defined if this library is built out-of-tree:
# let's use our copy of .rc file from LLVM source tree in that case
if (NOT DEFINED windows_resource_file)
set(windows_resource_file windows_resource_file.rc)
endif(NOT DEFINED windows_resource_file)
set(RC_CHAR_TM "\\231")
set(RC_CHAR_C "\\251")
set(RC_CHAR_R "\\256")
set(RC_FILE_VERSION "${PRODUCT_VER_MAJOR}.${PRODUCT_VER_MINOR}.${LLVM_VER_MAJOR}.${LLVM_VER_MINOR}")
set(RC_PRODUCT_NAME "Intel${RC_CHAR_R} Front-end Library for OpenCL${RC_CHAR_TM} software")
# Adjust content of the resource file by specifying compile definitions
set_property(SOURCE ${windows_resource_file}
PROPERTY COMPILE_DEFINITIONS
"RC_VERSION_FIELD_1=${PRODUCT_VER_MAJOR}"
"RC_VERSION_FIELD_2=${PRODUCT_VER_MINOR}"
"RC_VERSION_FIELD_3=${LLVM_VER_MAJOR}"
"RC_VERSION_FIELD_4=${LLVM_VER_MINOR}"
"RC_COMPANY_NAME=\"Intel Corporation\""
"RC_FILE_DESCRIPTION=\"${RC_PRODUCT_NAME}\""
"RC_FILE_VERSION=\"${RC_FILE_VERSION}\""
"RC_INTERNAL_NAME=\"${TARGET_NAME}\""
"RC_ORIGINAL_FILENAME=\"${TARGET_NAME}.dll\""
"RC_PRODUCT_NAME=\"${RC_PRODUCT_NAME}\""
"RC_PRODUCT_VERSION=\"${RC_FILE_VERSION}\""
"RC_COPYRIGHT=\"Copyright ${RC_CHAR_C} 2018 Intel Corporation. All rights reserved.\"")
# Enable compiler generation of Control Flow Guard security checks.
target_compile_options(${TARGET_NAME} PUBLIC "/guard:cf")
set_property(TARGET ${TARGET_NAME} APPEND_STRING PROPERTY
LINK_FLAGS "/DYNAMICBASE /GUARD:CF")
elseif(UNIX)
set_property(TARGET ${TARGET_NAME} APPEND_STRING PROPERTY
COMPILE_DEFINITIONS LIBCOMMON_CLANG_NAME="$<TARGET_SONAME_FILE_NAME:${TARGET_NAME}>")
set_property(TARGET ${TARGET_NAME} APPEND_STRING PROPERTY
LINK_FLAGS " -Wl,--no-undefined")
endif(WIN32)
install(FILES common_clang.h
DESTINATION include/cclang
COMPONENT ${TARGET_NAME})
SET_LINUX_EXPORTS_FILE( ${TARGET_NAME} common_clang.map )
add_custom_target(deploy DEPENDS install-${TARGET_NAME})