-
Notifications
You must be signed in to change notification settings - Fork 567
/
CMakeLists.txt
401 lines (350 loc) · 15.2 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Copyright (c) 2015-2023 The Khronos Group Inc.
#
# 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.
cmake_minimum_required(VERSION 3.22.1)
project(spirv-tools)
# Avoid a bug in CMake 3.22.1. By default it will set -std=c++11 for
# targets in test/*, when those tests need -std=c++17.
# https://github.com/KhronosGroup/SPIRV-Tools/issues/5340
# The bug is fixed in CMake 3.22.2
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.22.1")
if (${CMAKE_VERSION} VERSION_LESS "3.22.2")
cmake_policy(SET CMP0128 NEW)
endif()
endif()
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
enable_testing()
set(SPIRV_TOOLS "SPIRV-Tools")
include(GNUInstallDirs)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Require at least C++17
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()
if(${CMAKE_CXX_STANDARD} LESS 17)
message(FATAL_ERROR "SPIRV-Tools requires C++17 or later, but is configured for C++${CMAKE_CXX_STANDARD})")
endif()
set(CMAKE_CXX_EXTENSIONS OFF)
option(ENABLE_RTTI "Enables RTTI" OFF)
option(SPIRV_ALLOW_TIMERS "Allow timers via clock_gettime on supported platforms" ON)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
set(SPIRV_TIMER_ENABLED ${SPIRV_ALLOW_TIMERS})
elseif("${CMAKE_SYSTEM_NAME}" MATCHES "Windows")
add_definitions(-DSPIRV_WINDOWS)
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
add_definitions(-DSPIRV_WINDOWS)
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
set(SPIRV_TIMER_ENABLED ${SPIRV_ALLOW_TIMERS})
endif()
if (${SPIRV_TIMER_ENABLED})
add_definitions(-DSPIRV_TIMER_ENABLED)
endif()
if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
message(STATUS "No build type selected, default to Debug")
set(CMAKE_BUILD_TYPE "Debug")
endif()
option(SKIP_SPIRV_TOOLS_INSTALL "Skip installation" ${SKIP_SPIRV_TOOLS_INSTALL})
if(NOT ${SKIP_SPIRV_TOOLS_INSTALL})
set(ENABLE_SPIRV_TOOLS_INSTALL ON)
endif()
option(SPIRV_BUILD_COMPRESSION "Build SPIR-V compressing codec" OFF)
if(SPIRV_BUILD_COMPRESSION)
message(FATAL_ERROR "SPIR-V compression codec has been removed from SPIR-V tools. "
"Please remove SPIRV_BUILD_COMPRESSION from your build options.")
endif(SPIRV_BUILD_COMPRESSION)
option(SPIRV_BUILD_FUZZER "Build spirv-fuzz" OFF)
set(SPIRV_LIB_FUZZING_ENGINE_LINK_OPTIONS "" CACHE STRING "Used by OSS-Fuzz to control, via link options, which fuzzing engine should be used")
option(SPIRV_BUILD_LIBFUZZER_TARGETS "Build libFuzzer targets" OFF)
option(SPIRV_WERROR "Enable error on warning" ON)
if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") AND (NOT CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")))
set(COMPILER_IS_LIKE_GNU TRUE)
endif()
if(${COMPILER_IS_LIKE_GNU})
set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wno-self-assign)
endif()
option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
if(${SPIRV_WARN_EVERYTHING})
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
-Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
else()
message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
"so SPIRV_WARN_EVERYTHING has no effect")
endif()
endif()
if(${SPIRV_WERROR})
set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
endif()
elseif(MSVC)
set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS /wd4800 /wd4819)
if(${SPIRV_WERROR})
set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
endif()
endif()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/)
option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
if(${SPIRV_COLOR_TERMINAL})
add_definitions(-DSPIRV_COLOR_TERMINAL)
endif()
option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
if(${SPIRV_LOG_DEBUG})
add_definitions(-DSPIRV_LOG_DEBUG)
endif()
if (DEFINED SPIRV_TOOLS_EXTRA_DEFINITIONS)
add_definitions(${SPIRV_TOOLS_EXTRA_DEFINITIONS})
endif()
# Library build setting definitions:
#
# * SPIRV_TOOLS_BUILD_STATIC - ON or OFF - Defaults to ON.
# If enabled the following targets will be created:
# ${SPIRV_TOOLS}-static - STATIC library.
# Has full public symbol visibility.
# ${SPIRV_TOOLS}-shared - SHARED library.
# Has default-hidden symbol visibility.
# ${SPIRV_TOOLS} - will alias to one of above, based on BUILD_SHARED_LIBS.
# If disabled the following targets will be created:
# ${SPIRV_TOOLS} - either STATIC or SHARED based on SPIRV_TOOLS_LIBRARY_TYPE.
# Has full public symbol visibility.
# ${SPIRV_TOOLS}-shared - SHARED library.
# Has default-hidden symbol visibility.
#
# * SPIRV_TOOLS_LIBRARY_TYPE - SHARED or STATIC.
# Specifies the library type used for building SPIRV-Tools libraries.
# Defaults to SHARED when BUILD_SHARED_LIBS=1, otherwise STATIC.
#
# * SPIRV_TOOLS_FULL_VISIBILITY - "${SPIRV_TOOLS}-static" or "${SPIRV_TOOLS}"
# Evaluates to the SPIRV_TOOLS target library name that has no hidden symbols.
# This is used by internal targets for accessing symbols that are non-public.
# Note this target provides no API stability guarantees.
#
# Ideally, all of these will go away - see https://github.com/KhronosGroup/SPIRV-Tools/issues/3909.
option(ENABLE_EXCEPTIONS_ON_MSVC "Build SPIRV-TOOLS with c++ exceptions enabled in MSVC" ON)
option(SPIRV_TOOLS_BUILD_STATIC "Build ${SPIRV_TOOLS}-static target. ${SPIRV_TOOLS} will alias to ${SPIRV_TOOLS}-static or ${SPIRV_TOOLS}-shared based on BUILD_SHARED_LIBS" ON)
if(SPIRV_TOOLS_BUILD_STATIC)
set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS}-static)
set(SPIRV_TOOLS_LIBRARY_TYPE "STATIC")
else(SPIRV_TOOLS_BUILD_STATIC)
set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS})
if (NOT DEFINED SPIRV_TOOLS_LIBRARY_TYPE)
if(BUILD_SHARED_LIBS)
set(SPIRV_TOOLS_LIBRARY_TYPE "SHARED")
else()
set(SPIRV_TOOLS_LIBRARY_TYPE "STATIC")
endif()
endif()
endif(SPIRV_TOOLS_BUILD_STATIC)
function(spvtools_default_compile_options TARGET)
target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
if (${COMPILER_IS_LIKE_GNU})
target_compile_options(${TARGET} PRIVATE
-Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
-Wno-sign-conversion -fno-exceptions)
if(NOT ENABLE_RTTI)
add_compile_options(-fno-rtti)
endif()
# For good call stacks in profiles, keep the frame pointers.
if(NOT "${SPIRV_PERF}" STREQUAL "")
target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
set(SPIRV_USE_SANITIZER "" CACHE STRING
"Use the clang sanitizer [address|memory|thread|...]")
if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
target_compile_options(${TARGET} PRIVATE
-fsanitize=${SPIRV_USE_SANITIZER})
set_target_properties(${TARGET} PROPERTIES
LINK_FLAGS -fsanitize=${SPIRV_USE_SANITIZER})
endif()
target_compile_options(${TARGET} PRIVATE
-ftemplate-depth=1024)
else()
target_compile_options(${TARGET} PRIVATE
-Wno-missing-field-initializers)
endif()
endif()
if (MSVC)
# Specify /EHs for exception handling. This makes using SPIRV-Tools as
# dependencies in other projects easier.
if(ENABLE_EXCEPTIONS_ON_MSVC)
target_compile_options(${TARGET} PRIVATE /EHs)
endif()
endif()
# For MinGW cross compile, statically link to the C++ runtime.
# But it still depends on MSVCRT.dll.
if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
if (NOT MSVC)
set_target_properties(${TARGET} PROPERTIES
LINK_FLAGS -static -static-libgcc -static-libstdc++)
endif()
endif()
endfunction()
if(NOT COMMAND find_host_package)
macro(find_host_package)
find_package(${ARGN})
endmacro()
endif()
if(NOT COMMAND find_host_program)
macro(find_host_program)
find_program(${ARGN})
endmacro()
endif()
# Tests require Python3
find_host_package(Python3 REQUIRED)
# Check for symbol exports on Linux.
# At the moment, this check will fail on the OSX build machines for the Android NDK.
# It appears they don't have objdump.
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
macro(spvtools_check_symbol_exports TARGET)
if (NOT "${SPIRV_SKIP_TESTS}")
add_test(NAME spirv-tools-symbol-exports-${TARGET}
COMMAND Python3::Interpreter
${spirv-tools_SOURCE_DIR}/utils/check_symbol_exports.py "$<TARGET_FILE:${TARGET}>")
endif()
endmacro()
else()
macro(spvtools_check_symbol_exports TARGET)
if (NOT "${SPIRV_SKIP_TESTS}")
message("Skipping symbol exports test for ${TARGET}")
endif()
endmacro()
endif()
if(ENABLE_SPIRV_TOOLS_INSTALL)
if(WIN32 AND NOT MINGW)
macro(spvtools_config_package_dir TARGET PATH)
set(${PATH} ${TARGET}/cmake)
endmacro()
else()
macro(spvtools_config_package_dir TARGET PATH)
set(${PATH} ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET})
endmacro()
endif()
macro(spvtools_generate_config_file TARGET)
file(WRITE ${CMAKE_BINARY_DIR}/${TARGET}Config.cmake
"include(CMakeFindDependencyMacro)\n"
"find_dependency(${SPIRV_TOOLS})\n"
"include(\${CMAKE_CURRENT_LIST_DIR}/${TARGET}Targets.cmake)\n"
"set(${TARGET}_LIBRARIES ${TARGET})\n"
"get_target_property(${TARGET}_INCLUDE_DIRS ${TARGET} INTERFACE_INCLUDE_DIRECTORIES)\n")
endmacro()
endif()
# Currently iOS and Android are very similar.
# They both have their own packaging (APP/APK).
# Which makes regular executables/testing problematic.
#
# Currently the only deliverables for these platforms are
# libraries (either STATIC or SHARED).
#
# Furthermore testing is equally problematic.
if (IOS OR ANDROID)
set(SPIRV_SKIP_EXECUTABLES ON)
endif()
option(SPIRV_SKIP_EXECUTABLES "Skip building the executable and tests along with the library")
if (SPIRV_SKIP_EXECUTABLES)
set(SPIRV_SKIP_TESTS ON)
endif()
option(SPIRV_SKIP_TESTS "Skip building tests along with the library")
# Defaults to ON. The checks can be time consuming.
# Turn off if they take too long.
option(SPIRV_CHECK_CONTEXT "In a debug build, check if the IR context is in a valid state." ON)
if (${SPIRV_CHECK_CONTEXT})
add_compile_options($<$<CONFIG:Debug>:-DSPIRV_CHECK_CONTEXT>)
endif()
# Precompiled header macro. Parameters are source file list and filename for pch cpp file.
macro(spvtools_pch SRCS PCHPREFIX)
if(MSVC AND CMAKE_GENERATOR MATCHES "^Visual Studio" AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(PCH_NAME "$(IntDir)\\${PCHPREFIX}.pch")
# make source files use/depend on PCH_NAME
set_source_files_properties(${${SRCS}} PROPERTIES COMPILE_FLAGS "/Yu${PCHPREFIX}.h /FI${PCHPREFIX}.h /Fp${PCH_NAME} /Zm300" OBJECT_DEPENDS "${PCH_NAME}")
# make PCHPREFIX.cpp file compile and generate PCH_NAME
set_source_files_properties("${PCHPREFIX}.cpp" PROPERTIES COMPILE_FLAGS "/Yc${PCHPREFIX}.h /Fp${PCH_NAME} /Zm300" OBJECT_OUTPUTS "${PCH_NAME}")
list(APPEND ${SRCS} "${PCHPREFIX}.cpp")
endif()
endmacro(spvtools_pch)
add_subdirectory(external)
# Warning about extra semi-colons.
#
# This is not supported on all compilers/versions. so enabling only
# for clang, since that works for all versions that our bots run.
#
# This is intentionally done after adding the external subdirectory,
# so we don't enforce this flag on our dependencies, some of which do
# not pass it.
#
# If the minimum version of CMake supported is updated to 3.0 or
# later, then check_cxx_compiler_flag could be used instead.
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
add_compile_options("-Wextra-semi")
endif()
add_subdirectory(source)
add_subdirectory(tools)
add_subdirectory(test)
add_subdirectory(examples)
if(ENABLE_SPIRV_TOOLS_INSTALL)
install(
FILES
${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/linker.hpp
DESTINATION
${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/)
endif(ENABLE_SPIRV_TOOLS_INSTALL)
if (NOT "${SPIRV_SKIP_TESTS}")
add_test(NAME spirv-tools-copyrights
COMMAND Python3::Interpreter utils/check_copyright.py
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif()
set(SPIRV_LIBRARIES "-lSPIRV-Tools-opt -lSPIRV-Tools -lSPIRV-Tools-link")
set(SPIRV_SHARED_LIBRARIES "-lSPIRV-Tools-shared")
# Build pkg-config file
# Use a first-class target so it's regenerated when relevant files are updated.
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc
COMMAND ${CMAKE_COMMAND}
-DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES
-DTEMPLATE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools.pc.in
-DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
-DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR}
-DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR}
-DSPIRV_LIBRARIES=${SPIRV_LIBRARIES}
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake
DEPENDS "CHANGES" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools.pc.in" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake")
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc
COMMAND ${CMAKE_COMMAND}
-DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES
-DTEMPLATE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools-shared.pc.in
-DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc
-DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
-DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR}
-DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR}
-DSPIRV_SHARED_LIBRARIES=${SPIRV_SHARED_LIBRARIES}
-P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake
DEPENDS "CHANGES" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools-shared.pc.in" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake")
add_custom_target(spirv-tools-pkg-config
ALL
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc)
# Install pkg-config file
if (ENABLE_SPIRV_TOOLS_INSTALL)
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc
${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc
DESTINATION
${CMAKE_INSTALL_LIBDIR}/pkgconfig)
endif()