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