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