1# Copyright (c) 2015-2023 The Khronos Group Inc. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15cmake_minimum_required(VERSION 3.17.2) 16 17project(spirv-tools) 18 19# Avoid a bug in CMake 3.22.1. By default it will set -std=c++11 for 20# targets in test/*, when those tests need -std=c++17. 21# https://github.com/KhronosGroup/SPIRV-Tools/issues/5340 22# The bug is fixed in CMake 3.22.2 23if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.22.1") 24 if (${CMAKE_VERSION} VERSION_LESS "3.22.2") 25 cmake_policy(SET CMP0128 NEW) 26 endif() 27endif() 28 29set_property(GLOBAL PROPERTY USE_FOLDERS ON) 30 31enable_testing() 32set(SPIRV_TOOLS "SPIRV-Tools") 33 34include(GNUInstallDirs) 35 36set(CMAKE_POSITION_INDEPENDENT_CODE ON) 37 38# Require at least C++17 39if(NOT CMAKE_CXX_STANDARD) 40 set(CMAKE_CXX_STANDARD 17) 41endif() 42if(${CMAKE_CXX_STANDARD} LESS 17) 43 message(FATAL_ERROR "SPIRV-Tools requires C++17 or later, but is configured for C++${CMAKE_CXX_STANDARD})") 44endif() 45set(CMAKE_CXX_EXTENSIONS OFF) 46 47 48option(ENABLE_RTTI "Enables RTTI" OFF) 49option(SPIRV_ALLOW_TIMERS "Allow timers via clock_gettime on supported platforms" ON) 50 51if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") 52 add_definitions(-DSPIRV_LINUX) 53 set(SPIRV_TIMER_ENABLED ${SPIRV_ALLOW_TIMERS}) 54elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten") 55 add_definitions(-DSPIRV_EMSCRIPTEN) 56elseif("${CMAKE_SYSTEM_NAME}" MATCHES "Windows") 57 add_definitions(-DSPIRV_WINDOWS) 58elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN") 59 add_definitions(-DSPIRV_WINDOWS) 60elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") 61 add_definitions(-DSPIRV_MAC) 62elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "iOS") 63 add_definitions(-DSPIRV_IOS) 64elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "tvOS") 65 add_definitions(-DSPIRV_TVOS) 66elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "visionOS") 67 add_definitions(-DSPIRV_VISIONOS) 68elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android") 69 add_definitions(-DSPIRV_ANDROID) 70 set(SPIRV_TIMER_ENABLED ${SPIRV_ALLOW_TIMERS}) 71elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD") 72 add_definitions(-DSPIRV_FREEBSD) 73elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "OpenBSD") 74 add_definitions(-DSPIRV_OPENBSD) 75elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia") 76 add_definitions(-DSPIRV_FUCHSIA) 77elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "GNU") 78 add_definitions(-DSPIRV_GNU) 79elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "QNX") 80 add_definitions(-DSPIRV_QNX) 81else() 82 message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!") 83endif() 84 85if (${SPIRV_TIMER_ENABLED}) 86 add_definitions(-DSPIRV_TIMER_ENABLED) 87endif() 88 89if ("${CMAKE_BUILD_TYPE}" STREQUAL "") 90 message(STATUS "No build type selected, default to Debug") 91 set(CMAKE_BUILD_TYPE "Debug") 92endif() 93 94option(SKIP_SPIRV_TOOLS_INSTALL "Skip installation" ${SKIP_SPIRV_TOOLS_INSTALL}) 95if(NOT ${SKIP_SPIRV_TOOLS_INSTALL}) 96 set(ENABLE_SPIRV_TOOLS_INSTALL ON) 97endif() 98 99option(SPIRV_BUILD_COMPRESSION "Build SPIR-V compressing codec" OFF) 100if(SPIRV_BUILD_COMPRESSION) 101 message(FATAL_ERROR "SPIR-V compression codec has been removed from SPIR-V tools. " 102 "Please remove SPIRV_BUILD_COMPRESSION from your build options.") 103endif(SPIRV_BUILD_COMPRESSION) 104 105option(SPIRV_BUILD_FUZZER "Build spirv-fuzz" OFF) 106 107set(SPIRV_LIB_FUZZING_ENGINE_LINK_OPTIONS "" CACHE STRING "Used by OSS-Fuzz to control, via link options, which fuzzing engine should be used") 108 109option(SPIRV_BUILD_LIBFUZZER_TARGETS "Build libFuzzer targets" OFF) 110 111option(SPIRV_WERROR "Enable error on warning" ON) 112if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") AND (NOT CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC"))) 113 set(COMPILER_IS_LIKE_GNU TRUE) 114endif() 115if(${COMPILER_IS_LIKE_GNU}) 116 set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers) 117 118 if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 119 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wno-self-assign) 120 endif() 121 122 option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING}) 123 if(${SPIRV_WARN_EVERYTHING}) 124 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") 125 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} 126 -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded) 127 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") 128 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors) 129 else() 130 message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, " 131 "so SPIRV_WARN_EVERYTHING has no effect") 132 endif() 133 endif() 134 135 if(${SPIRV_WERROR}) 136 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror) 137 endif() 138elseif(MSVC) 139 set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS /wd4800 /wd4819) 140 141 if(${SPIRV_WERROR}) 142 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX) 143 endif() 144endif() 145 146include_directories(${CMAKE_CURRENT_SOURCE_DIR}/) 147 148option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON) 149if(${SPIRV_COLOR_TERMINAL}) 150 add_definitions(-DSPIRV_COLOR_TERMINAL) 151endif() 152 153option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF) 154if(${SPIRV_LOG_DEBUG}) 155 add_definitions(-DSPIRV_LOG_DEBUG) 156endif() 157 158if (DEFINED SPIRV_TOOLS_EXTRA_DEFINITIONS) 159 add_definitions(${SPIRV_TOOLS_EXTRA_DEFINITIONS}) 160endif() 161 162# Library build setting definitions: 163# 164# * SPIRV_TOOLS_BUILD_STATIC - ON or OFF - Defaults to ON. 165# If enabled the following targets will be created: 166# ${SPIRV_TOOLS}-static - STATIC library. 167# Has full public symbol visibility. 168# ${SPIRV_TOOLS}-shared - SHARED library. 169# Has default-hidden symbol visibility. 170# ${SPIRV_TOOLS} - will alias to one of above, based on BUILD_SHARED_LIBS. 171# If disabled the following targets will be created: 172# ${SPIRV_TOOLS} - either STATIC or SHARED based on SPIRV_TOOLS_LIBRARY_TYPE. 173# Has full public symbol visibility. 174# ${SPIRV_TOOLS}-shared - SHARED library. 175# Has default-hidden symbol visibility. 176# 177# * SPIRV_TOOLS_LIBRARY_TYPE - SHARED or STATIC. 178# Specifies the library type used for building SPIRV-Tools libraries. 179# Defaults to SHARED when BUILD_SHARED_LIBS=1, otherwise STATIC. 180# 181# * SPIRV_TOOLS_FULL_VISIBILITY - "${SPIRV_TOOLS}-static" or "${SPIRV_TOOLS}" 182# Evaluates to the SPIRV_TOOLS target library name that has no hidden symbols. 183# This is used by internal targets for accessing symbols that are non-public. 184# Note this target provides no API stability guarantees. 185# 186# Ideally, all of these will go away - see https://github.com/KhronosGroup/SPIRV-Tools/issues/3909. 187option(ENABLE_EXCEPTIONS_ON_MSVC "Build SPIRV-TOOLS with c++ exceptions enabled in MSVC" ON) 188option(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) 189if(SPIRV_TOOLS_BUILD_STATIC) 190 set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS}-static) 191 set(SPIRV_TOOLS_LIBRARY_TYPE "STATIC") 192else(SPIRV_TOOLS_BUILD_STATIC) 193 set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS}) 194 if (NOT DEFINED SPIRV_TOOLS_LIBRARY_TYPE) 195 if(BUILD_SHARED_LIBS) 196 set(SPIRV_TOOLS_LIBRARY_TYPE "SHARED") 197 else() 198 set(SPIRV_TOOLS_LIBRARY_TYPE "STATIC") 199 endif() 200 endif() 201endif(SPIRV_TOOLS_BUILD_STATIC) 202 203function(spvtools_default_compile_options TARGET) 204 target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS}) 205 206 if (${COMPILER_IS_LIKE_GNU}) 207 target_compile_options(${TARGET} PRIVATE 208 -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion 209 -Wno-sign-conversion -fno-exceptions) 210 211 if(NOT ENABLE_RTTI) 212 add_compile_options(-fno-rtti) 213 endif() 214 # For good call stacks in profiles, keep the frame pointers. 215 if(NOT "${SPIRV_PERF}" STREQUAL "") 216 target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer) 217 endif() 218 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") 219 set(SPIRV_USE_SANITIZER "" CACHE STRING 220 "Use the clang sanitizer [address|memory|thread|...]") 221 if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "") 222 target_compile_options(${TARGET} PRIVATE 223 -fsanitize=${SPIRV_USE_SANITIZER}) 224 set_target_properties(${TARGET} PROPERTIES 225 LINK_FLAGS -fsanitize=${SPIRV_USE_SANITIZER}) 226 endif() 227 target_compile_options(${TARGET} PRIVATE 228 -ftemplate-depth=1024) 229 else() 230 target_compile_options(${TARGET} PRIVATE 231 -Wno-missing-field-initializers) 232 endif() 233 endif() 234 235 if (MSVC) 236 # Specify /EHs for exception handling. This makes using SPIRV-Tools as 237 # dependencies in other projects easier. 238 if(ENABLE_EXCEPTIONS_ON_MSVC) 239 target_compile_options(${TARGET} PRIVATE /EHs) 240 endif() 241 endif() 242 243 # For MinGW cross compile, statically link to the C++ runtime. 244 # But it still depends on MSVCRT.dll. 245 if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") 246 if (NOT MSVC) 247 set_target_properties(${TARGET} PROPERTIES 248 LINK_FLAGS -static -static-libgcc -static-libstdc++) 249 endif() 250 endif() 251endfunction() 252 253if(NOT COMMAND find_host_package) 254 macro(find_host_package) 255 find_package(${ARGN}) 256 endmacro() 257endif() 258if(NOT COMMAND find_host_program) 259 macro(find_host_program) 260 find_program(${ARGN}) 261 endmacro() 262endif() 263 264# Tests require Python3 265find_host_package(Python3 REQUIRED) 266 267# Check for symbol exports on Linux. 268# At the moment, this check will fail on the OSX build machines for the Android NDK. 269# It appears they don't have objdump. 270if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") 271 macro(spvtools_check_symbol_exports TARGET) 272 if (NOT "${SPIRV_SKIP_TESTS}") 273 add_test(NAME spirv-tools-symbol-exports-${TARGET} 274 COMMAND Python3::Interpreter 275 ${spirv-tools_SOURCE_DIR}/utils/check_symbol_exports.py "$<TARGET_FILE:${TARGET}>") 276 endif() 277 endmacro() 278else() 279 macro(spvtools_check_symbol_exports TARGET) 280 if (NOT "${SPIRV_SKIP_TESTS}") 281 message("Skipping symbol exports test for ${TARGET}") 282 endif() 283 endmacro() 284endif() 285 286if(ENABLE_SPIRV_TOOLS_INSTALL) 287 if(WIN32 AND NOT MINGW) 288 macro(spvtools_config_package_dir TARGET PATH) 289 set(${PATH} ${TARGET}/cmake) 290 endmacro() 291 else() 292 macro(spvtools_config_package_dir TARGET PATH) 293 set(${PATH} ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET}) 294 endmacro() 295 endif() 296 297 macro(spvtools_generate_config_file TARGET) 298 file(WRITE ${CMAKE_BINARY_DIR}/${TARGET}Config.cmake 299 "include(CMakeFindDependencyMacro)\n" 300 "find_dependency(${SPIRV_TOOLS})\n" 301 "include(\${CMAKE_CURRENT_LIST_DIR}/${TARGET}Targets.cmake)\n" 302 "set(${TARGET}_LIBRARIES ${TARGET})\n" 303 "get_target_property(${TARGET}_INCLUDE_DIRS ${TARGET} INTERFACE_INCLUDE_DIRECTORIES)\n") 304 endmacro() 305endif() 306 307# Currently iOS and Android are very similar. 308# They both have their own packaging (APP/APK). 309# Which makes regular executables/testing problematic. 310# 311# Currently the only deliverables for these platforms are 312# libraries (either STATIC or SHARED). 313# 314# Furthermore testing is equally problematic. 315if (IOS OR ANDROID) 316 set(SPIRV_SKIP_EXECUTABLES ON) 317endif() 318 319option(SPIRV_SKIP_EXECUTABLES "Skip building the executable and tests along with the library") 320if (SPIRV_SKIP_EXECUTABLES) 321 set(SPIRV_SKIP_TESTS ON) 322endif() 323option(SPIRV_SKIP_TESTS "Skip building tests along with the library") 324 325# Defaults to ON. The checks can be time consuming. 326# Turn off if they take too long. 327option(SPIRV_CHECK_CONTEXT "In a debug build, check if the IR context is in a valid state." ON) 328if (${SPIRV_CHECK_CONTEXT}) 329 add_compile_options($<$<CONFIG:Debug>:-DSPIRV_CHECK_CONTEXT>) 330endif() 331 332# Precompiled header macro. Parameters are source file list and filename for pch cpp file. 333macro(spvtools_pch SRCS PCHPREFIX) 334 if(MSVC AND CMAKE_GENERATOR MATCHES "^Visual Studio" AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 335 set(PCH_NAME "$(IntDir)\\${PCHPREFIX}.pch") 336 # make source files use/depend on PCH_NAME 337 set_source_files_properties(${${SRCS}} PROPERTIES COMPILE_FLAGS "/Yu${PCHPREFIX}.h /FI${PCHPREFIX}.h /Fp${PCH_NAME} /Zm300" OBJECT_DEPENDS "${PCH_NAME}") 338 # make PCHPREFIX.cpp file compile and generate PCH_NAME 339 set_source_files_properties("${PCHPREFIX}.cpp" PROPERTIES COMPILE_FLAGS "/Yc${PCHPREFIX}.h /Fp${PCH_NAME} /Zm300" OBJECT_OUTPUTS "${PCH_NAME}") 340 list(APPEND ${SRCS} "${PCHPREFIX}.cpp") 341 endif() 342endmacro(spvtools_pch) 343 344add_subdirectory(external) 345 346# Warning about extra semi-colons. 347# 348# This is not supported on all compilers/versions. so enabling only 349# for clang, since that works for all versions that our bots run. 350# 351# This is intentionally done after adding the external subdirectory, 352# so we don't enforce this flag on our dependencies, some of which do 353# not pass it. 354# 355# If the minimum version of CMake supported is updated to 3.0 or 356# later, then check_cxx_compiler_flag could be used instead. 357if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 358 add_compile_options("-Wextra-semi") 359endif() 360 361add_subdirectory(source) 362add_subdirectory(tools) 363 364add_subdirectory(test) 365add_subdirectory(examples) 366 367if(ENABLE_SPIRV_TOOLS_INSTALL) 368 install( 369 FILES 370 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h 371 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp 372 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp 373 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/linker.hpp 374 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/instrument.hpp 375 DESTINATION 376 ${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/) 377endif(ENABLE_SPIRV_TOOLS_INSTALL) 378 379if (NOT "${SPIRV_SKIP_TESTS}") 380 add_test(NAME spirv-tools-copyrights 381 COMMAND Python3::Interpreter utils/check_copyright.py 382 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) 383endif() 384 385set(SPIRV_LIBRARIES "-lSPIRV-Tools-opt -lSPIRV-Tools -lSPIRV-Tools-link") 386set(SPIRV_SHARED_LIBRARIES "-lSPIRV-Tools-shared") 387 388# Build pkg-config file 389# Use a first-class target so it's regenerated when relevant files are updated. 390add_custom_target(spirv-tools-pkg-config ALL 391 COMMAND ${CMAKE_COMMAND} 392 -DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES 393 -DTEMPLATE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools.pc.in 394 -DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc 395 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} 396 -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR} 397 -DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR} 398 -DSPIRV_LIBRARIES=${SPIRV_LIBRARIES} 399 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake 400 DEPENDS "CHANGES" "cmake/SPIRV-Tools.pc.in" "cmake/write_pkg_config.cmake") 401add_custom_target(spirv-tools-shared-pkg-config ALL 402 COMMAND ${CMAKE_COMMAND} 403 -DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES 404 -DTEMPLATE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools-shared.pc.in 405 -DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc 406 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} 407 -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR} 408 -DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR} 409 -DSPIRV_SHARED_LIBRARIES=${SPIRV_SHARED_LIBRARIES} 410 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake 411 DEPENDS "CHANGES" "cmake/SPIRV-Tools-shared.pc.in" "cmake/write_pkg_config.cmake") 412 413# Install pkg-config file 414if (ENABLE_SPIRV_TOOLS_INSTALL) 415 install( 416 FILES 417 ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc 418 ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc 419 DESTINATION 420 ${CMAKE_INSTALL_LIBDIR}/pkgconfig) 421endif() 422