forked from intel/opencl-clang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
343 lines (291 loc) · 11.6 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
cmake_minimum_required(VERSION 3.13.4)
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
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(CMAKE_CROSSCOMPILING 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 "18")
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 17)
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()
use_rtti(FALSE)
use_eh(TRUE)
if (CMAKE_SIZEOF_VOID_P EQUAL 4)
set(ADDR 32)
elseif (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(ADDR 64)
else ()
# CMAKE_SIZEOF_VOID_P might not be set on windows x86 in in-tree build
message(WARNING "[OPENCL-CLANG] CMAKE_SIZEOF_VOID_P is not set")
if (WIN32 AND LLVM_BUILD_32_BITS)
set(ADDR 32)
endif()
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 OPENCL_CLANG_LIBRARY_NAME)
set(OPENCL_CLANG_LIBRARY_NAME "opencl-clang")
endif()
set(TARGET_NAME ${OPENCL_CLANG_LIBRARY_NAME}${BUILD_PLATFORM} )
# OPENCL_CLANG_PCH_EXTENSION can override PCH extension map in options_compile.cpp.
# Example: "cl_khr_3d_image_writes,cl_khr_depth_images"
set(OPENCL_CLANG_PCH_EXTENSION "" CACHE STRING "Comma-separated list of OpenCL extensions")
if (NOT "${OPENCL_CLANG_PCH_EXTENSION}" STREQUAL "")
add_definitions(-DPCH_EXTENSION="${OPENCL_CLANG_PCH_EXTENSION}")
endif()
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
opencl_clang.h
options.h
binary_result.h
pch_mgr.h
${COMPILE_OPTIONS_TD}
${COMPILE_OPTIONS_INC}
)
set(TARGET_SOURCE_FILES
opencl_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( -DOPENCL_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
clangBasic
clangFrontend
clangFrontendTool
clangSerialization
)
endif()
if(USE_PREBUILT_LLVM AND UNIX)
# llvm_map_components_to_libnames(... all) returns empty string if llvm is
# pre-built locally in either static or shared type in Ubuntu 22.04 container.
find_program(LLVM_CONFIG_EXE
NAMES llvm-config-${PREFERRED_LLVM_VERSION} llvm-config
PATHS ${LLVM_BINARY_DIR} ${LLVM_BINARY_DIR}/bin)
if(NOT LLVM_CONFIG_EXE)
message(FATAL_ERROR "[OPENCL-CLANG] llvm-config is not found")
endif()
execute_process(COMMAND ${LLVM_CONFIG_EXE} --libs all OUTPUT_VARIABLE ALL_LIBS)
string(REGEX REPLACE "( |\r|\n|-l)+" ";" ALL_LLVM_LIBS ${ALL_LIBS})
set(ALL_LLVM_LIBS "LLVMSPIRVLib${ALL_LLVM_LIBS}")
else()
llvm_map_components_to_libnames(ALL_LLVM_LIBS all)
endif()
set(OPENCL_CLANG_EXCLUDE_LIBS_FROM_ALL "" CACHE STRING "Space-separated list of LLVM libraries to exclude from all")
if (NOT "${OPENCL_CLANG_EXCLUDE_LIBS_FROM_ALL}" STREQUAL "")
list(REMOVE_ITEM ALL_LLVM_LIBS ${OPENCL_CLANG_EXCLUDE_LIBS_FROM_ALL})
endif()
list(APPEND OPENCL_CLANG_LINK_LIBS ${ALL_LLVM_LIBS})
add_llvm_library(${TARGET_NAME} SHARED
${TARGET_INCLUDE_FILES}
${TARGET_SOURCE_FILES}
$<TARGET_OBJECTS:cl_headers>
DEPENDS CClangCompileOptions
LINK_LIBS
${OPENCL_CLANG_LINK_LIBS}
)
if (WIN32)
# 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 LIBOPENCL_CLANG_NAME="$<TARGET_SONAME_FILE_NAME:${TARGET_NAME}>")
# Sanitizers do not support this flag, disable this when under sanitizer build
if(NOT LLVM_USE_SANITIZER)
set_property(TARGET ${TARGET_NAME} APPEND_STRING PROPERTY
LINK_FLAGS " -Wl,--no-undefined")
endif()
endif(WIN32)
install(FILES opencl_clang.h
DESTINATION include/cclang
COMPONENT ${TARGET_NAME})
#
# Stripped PDB files
#
if (WIN32)
get_target_property(RT_OUTPUT_DIRECTORY ${TARGET_NAME} RUNTIME_OUTPUT_DIRECTORY)
file(TO_NATIVE_PATH ${RT_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}/${TARGET_NAME}_stripped.pdb PDB_NAME)
if (${MSVC_VERSION} EQUAL 1500)
# Visual Studio 2008
set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS "${LINK_FLAGS} /PDBSTRIPPED:${PDB_NAME}")
else (${MSVC_VERSION} EQUAL 1500)
# Visual Studio 2010 (assumed if not Visual Studio 2008)
# This is a fix due to a bug in CMake, Does not add the flag /DEBUG to the linker flags in Release mode.
# The /DEBUG flag is required in order to create stripped pdbs.
set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS_DEBUG "${LINK_FLAGS_DEBUG} /PDBSTRIPPED:${PDB_NAME}")
set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS_RELEASE "${LINK_FLAGS_RELEASE} /DEBUG /PDBSTRIPPED:${PDB_NAME}")
endif (${MSVC_VERSION} EQUAL 1500)
if (INSTALL_PDBS)
install(FILES ${RT_OUTPUT_DIRECTORY}/\${BUILD_TYPE}/${TARGET_NAME}.pdb DESTINATION bin)
endif(INSTALL_PDBS)
install(FILES ${RT_OUTPUT_DIRECTORY}/\${BUILD_TYPE}/${TARGET_NAME}_stripped.pdb DESTINATION bin)
else (WIN32)
SET_LINUX_EXPORTS_FILE( ${TARGET_NAME} opencl_clang.map )
endif(WIN32)