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 "Fuchsia") 60 add_definitions(-DSPIRV_FUCHSIA) 61else() 62 message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!") 63endif() 64 65if (${SPIRV_TIMER_ENABLED}) 66 add_definitions(-DSPIRV_TIMER_ENABLED) 67endif() 68 69if ("${CMAKE_BUILD_TYPE}" STREQUAL "") 70 message(STATUS "No build type selected, default to Debug") 71 set(CMAKE_BUILD_TYPE "Debug") 72endif() 73 74option(SKIP_SPIRV_TOOLS_INSTALL "Skip installation" ${SKIP_SPIRV_TOOLS_INSTALL}) 75if(NOT ${SKIP_SPIRV_TOOLS_INSTALL}) 76 set(ENABLE_SPIRV_TOOLS_INSTALL ON) 77endif() 78 79option(SPIRV_BUILD_COMPRESSION "Build SPIR-V compressing codec" OFF) 80if(SPIRV_BUILD_COMPRESSION) 81 message(FATAL_ERROR "SPIR-V compression codec has been removed from SPIR-V tools. " 82 "Please remove SPIRV_BUILD_COMPRESSION from your build options.") 83endif(SPIRV_BUILD_COMPRESSION) 84 85option(SPIRV_BUILD_FUZZER "Build spirv-fuzz" 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) 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(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) 164if(SPIRV_TOOLS_BUILD_STATIC) 165 set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS}-static) 166 set(SPIRV_TOOLS_LIBRARY_TYPE "STATIC") 167else(SPIRV_TOOLS_BUILD_STATIC) 168 set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS}) 169 if (NOT DEFINED SPIRV_TOOLS_LIBRARY_TYPE) 170 if(BUILD_SHARED_LIBS) 171 set(SPIRV_TOOLS_LIBRARY_TYPE "SHARED") 172 else() 173 set(SPIRV_TOOLS_LIBRARY_TYPE "STATIC") 174 endif() 175 endif() 176endif(SPIRV_TOOLS_BUILD_STATIC) 177 178function(spvtools_default_compile_options TARGET) 179 target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS}) 180 181 if (${COMPILER_IS_LIKE_GNU}) 182 target_compile_options(${TARGET} PRIVATE -std=c++11 -fno-exceptions) 183 target_compile_options(${TARGET} PRIVATE 184 -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion 185 -Wno-sign-conversion) 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") 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 target_compile_options(${TARGET} PRIVATE /EHs) 215 endif() 216 217 # For MinGW cross compile, statically link to the C++ runtime. 218 # But it still depends on MSVCRT.dll. 219 if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") 220 if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") 221 set_target_properties(${TARGET} PROPERTIES 222 LINK_FLAGS -static -static-libgcc -static-libstdc++) 223 endif() 224 endif() 225endfunction() 226 227if(NOT COMMAND find_host_package) 228 macro(find_host_package) 229 find_package(${ARGN}) 230 endmacro() 231endif() 232if(NOT COMMAND find_host_program) 233 macro(find_host_program) 234 find_program(${ARGN}) 235 endmacro() 236endif() 237 238# Tests require Python3 239find_host_package(PythonInterp 3 REQUIRED) 240 241# Check for symbol exports on Linux. 242# At the moment, this check will fail on the OSX build machines for the Android NDK. 243# It appears they don't have objdump. 244if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") 245 macro(spvtools_check_symbol_exports TARGET) 246 if (NOT "${SPIRV_SKIP_TESTS}") 247 add_test(NAME spirv-tools-symbol-exports-${TARGET} 248 COMMAND ${PYTHON_EXECUTABLE} 249 ${spirv-tools_SOURCE_DIR}/utils/check_symbol_exports.py "$<TARGET_FILE:${TARGET}>") 250 endif() 251 endmacro() 252else() 253 macro(spvtools_check_symbol_exports TARGET) 254 if (NOT "${SPIRV_SKIP_TESTS}") 255 message("Skipping symbol exports test for ${TARGET}") 256 endif() 257 endmacro() 258endif() 259 260if(ENABLE_SPIRV_TOOLS_INSTALL) 261 if(WIN32) 262 macro(spvtools_config_package_dir TARGET PATH) 263 set(${PATH} ${TARGET}/cmake) 264 endmacro() 265 else() 266 macro(spvtools_config_package_dir TARGET PATH) 267 set(${PATH} ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET}) 268 endmacro() 269 endif() 270 271 macro(spvtools_generate_config_file TARGET) 272 file(WRITE ${CMAKE_BINARY_DIR}/${TARGET}Config.cmake 273 "include(CMakeFindDependencyMacro)\n" 274 "find_dependency(${SPIRV_TOOLS})\n" 275 "include(\${CMAKE_CURRENT_LIST_DIR}/${TARGET}Targets.cmake)\n" 276 "set(${TARGET}_LIBRARIES ${TARGET})\n" 277 "get_target_property(${TARGET}_INCLUDE_DIRS ${TARGET} INTERFACE_INCLUDE_DIRECTORIES)\n") 278 endmacro() 279endif() 280 281# Defaults to OFF if the user didn't set it. 282option(SPIRV_SKIP_EXECUTABLES 283 "Skip building the executable and tests along with the library" 284 ${SPIRV_SKIP_EXECUTABLES}) 285option(SPIRV_SKIP_TESTS 286 "Skip building tests along with the library" ${SPIRV_SKIP_TESTS}) 287if ("${SPIRV_SKIP_EXECUTABLES}") 288 set(SPIRV_SKIP_TESTS ON) 289endif() 290 291# Defaults to ON. The checks can be time consuming. 292# Turn off if they take too long. 293option(SPIRV_CHECK_CONTEXT "In a debug build, check if the IR context is in a valid state." ON) 294if (${SPIRV_CHECK_CONTEXT}) 295 add_definitions(-DSPIRV_CHECK_CONTEXT) 296endif() 297 298# Precompiled header macro. Parameters are source file list and filename for pch cpp file. 299macro(spvtools_pch SRCS PCHPREFIX) 300 if(MSVC AND CMAKE_GENERATOR MATCHES "^Visual Studio" AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 301 set(PCH_NAME "$(IntDir)\\${PCHPREFIX}.pch") 302 # make source files use/depend on PCH_NAME 303 set_source_files_properties(${${SRCS}} PROPERTIES COMPILE_FLAGS "/Yu${PCHPREFIX}.h /FI${PCHPREFIX}.h /Fp${PCH_NAME} /Zm300" OBJECT_DEPENDS "${PCH_NAME}") 304 # make PCHPREFIX.cpp file compile and generate PCH_NAME 305 set_source_files_properties("${PCHPREFIX}.cpp" PROPERTIES COMPILE_FLAGS "/Yc${PCHPREFIX}.h /Fp${PCH_NAME} /Zm300" OBJECT_OUTPUTS "${PCH_NAME}") 306 list(APPEND ${SRCS} "${PCHPREFIX}.cpp") 307 endif() 308endmacro(spvtools_pch) 309 310add_subdirectory(external) 311 312# Warning about extra semi-colons. 313# 314# This is not supported on all compilers/versions. so enabling only 315# for clang, since that works for all versions that our bots run. 316# 317# This is intentionally done after adding the external subdirectory, 318# so we don't enforce this flag on our dependencies, some of which do 319# not pass it. 320# 321# If the minimum version of CMake supported is updated to 3.0 or 322# later, then check_cxx_compiler_flag could be used instead. 323if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 324 add_compile_options("-Wextra-semi") 325endif() 326 327add_subdirectory(source) 328add_subdirectory(tools) 329 330add_subdirectory(test) 331add_subdirectory(examples) 332 333if(ENABLE_SPIRV_TOOLS_INSTALL) 334 install( 335 FILES 336 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h 337 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp 338 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp 339 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/linker.hpp 340 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/instrument.hpp 341 DESTINATION 342 ${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/) 343endif(ENABLE_SPIRV_TOOLS_INSTALL) 344 345if (NOT "${SPIRV_SKIP_TESTS}") 346 add_test(NAME spirv-tools-copyrights 347 COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py 348 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) 349endif() 350 351set(SPIRV_LIBRARIES "-lSPIRV-Tools-opt -lSPIRV-Tools -lSPIRV-Tools-link") 352set(SPIRV_SHARED_LIBRARIES "-lSPIRV-Tools-shared") 353 354# Build pkg-config file 355# Use a first-class target so it's regenerated when relevant files are updated. 356add_custom_target(spirv-tools-pkg-config ALL 357 COMMAND ${CMAKE_COMMAND} 358 -DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES 359 -DTEMPLATE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools.pc.in 360 -DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc 361 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} 362 -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR} 363 -DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR} 364 -DSPIRV_LIBRARIES=${SPIRV_LIBRARIES} 365 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake 366 DEPENDS "CHANGES" "cmake/SPIRV-Tools.pc.in" "cmake/write_pkg_config.cmake") 367add_custom_target(spirv-tools-shared-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-shared.pc.in 371 -DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc 372 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} 373 -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR} 374 -DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR} 375 -DSPIRV_SHARED_LIBRARIES=${SPIRV_SHARED_LIBRARIES} 376 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake 377 DEPENDS "CHANGES" "cmake/SPIRV-Tools-shared.pc.in" "cmake/write_pkg_config.cmake") 378 379# Install pkg-config file 380if (ENABLE_SPIRV_TOOLS_INSTALL) 381 install( 382 FILES 383 ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc 384 ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc 385 DESTINATION 386 ${CMAKE_INSTALL_LIBDIR}/pkgconfig) 387endif() 388