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) 32include(cmake/setup_build.cmake) 33 34set(CMAKE_POSITION_INDEPENDENT_CODE ON) 35set(CMAKE_CXX_STANDARD 11) 36 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 "Windows") 43 add_definitions(-DSPIRV_WINDOWS) 44elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN") 45 add_definitions(-DSPIRV_WINDOWS) 46elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin") 47 add_definitions(-DSPIRV_MAC) 48elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android") 49 add_definitions(-DSPIRV_ANDROID) 50 set(SPIRV_TIMER_ENABLED ${SPIRV_ALLOW_TIMERS}) 51elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD") 52 add_definitions(-DSPIRV_FREEBSD) 53else() 54 message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!") 55endif() 56 57if (${SPIRV_TIMER_ENABLED}) 58 add_definitions(-DSPIRV_TIMER_ENABLED) 59endif() 60 61if ("${CMAKE_BUILD_TYPE}" STREQUAL "") 62 message(STATUS "No build type selected, default to Debug") 63 set(CMAKE_BUILD_TYPE "Debug") 64endif() 65 66option(SKIP_SPIRV_TOOLS_INSTALL "Skip installation" ${SKIP_SPIRV_TOOLS_INSTALL}) 67if(NOT ${SKIP_SPIRV_TOOLS_INSTALL}) 68 set(ENABLE_SPIRV_TOOLS_INSTALL ON) 69endif() 70 71option(SPIRV_BUILD_COMPRESSION "Build SPIR-V compressing codec" OFF) 72 73option(SPIRV_WERROR "Enable error on warning" ON) 74if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") AND (NOT CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC"))) 75 set(COMPILER_IS_LIKE_GNU TRUE) 76endif() 77if(${COMPILER_IS_LIKE_GNU}) 78 set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers) 79 80 if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 81 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wno-self-assign) 82 endif() 83 84 option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING}) 85 if(${SPIRV_WARN_EVERYTHING}) 86 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") 87 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} 88 -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded) 89 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") 90 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors) 91 else() 92 message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, " 93 "so SPIRV_WARN_EVERYTHING has no effect") 94 endif() 95 endif() 96 97 if(${SPIRV_WERROR}) 98 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror) 99 endif() 100elseif(MSVC) 101 set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS /wd4800) 102 103 if(${SPIRV_WERROR}) 104 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX) 105 endif() 106endif() 107 108include_directories(${CMAKE_CURRENT_SOURCE_DIR}/) 109 110option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON) 111if(${SPIRV_COLOR_TERMINAL}) 112 add_definitions(-DSPIRV_COLOR_TERMINAL) 113endif() 114 115option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF) 116if(${SPIRV_LOG_DEBUG}) 117 add_definitions(-DSPIRV_LOG_DEBUG) 118endif() 119 120if (DEFINED SPIRV_TOOLS_EXTRA_DEFINITIONS) 121 add_definitions(${SPIRV_TOOLS_EXTRA_DEFINITIONS}) 122endif() 123 124function(spvtools_default_compile_options TARGET) 125 target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS}) 126 127 if (${COMPILER_IS_LIKE_GNU}) 128 target_compile_options(${TARGET} PRIVATE 129 -std=c++11 -fno-exceptions -fno-rtti) 130 target_compile_options(${TARGET} PRIVATE 131 -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion 132 -Wno-sign-conversion) 133 # For good call stacks in profiles, keep the frame pointers. 134 if(NOT "${SPIRV_PERF}" STREQUAL "") 135 target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer) 136 endif() 137 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") 138 set(SPIRV_USE_SANITIZER "" CACHE STRING 139 "Use the clang sanitizer [address|memory|thread|...]") 140 if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "") 141 target_compile_options(${TARGET} PRIVATE 142 -fsanitize=${SPIRV_USE_SANITIZER}) 143 endif() 144 target_compile_options(${TARGET} PRIVATE 145 -ftemplate-depth=1024) 146 else() 147 target_compile_options(${TARGET} PRIVATE 148 -Wno-missing-field-initializers) 149 endif() 150 endif() 151 152 if (MSVC) 153 # Specify /EHs for exception handling. This makes using SPIRV-Tools as 154 # dependencies in other projects easier. 155 target_compile_options(${TARGET} PRIVATE /EHs) 156 endif() 157 158 # For MinGW cross compile, statically link to the C++ runtime. 159 # But it still depends on MSVCRT.dll. 160 if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") 161 if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") 162 set_target_properties(${TARGET} PROPERTIES 163 LINK_FLAGS -static -static-libgcc -static-libstdc++) 164 endif() 165 endif() 166endfunction() 167 168if(NOT COMMAND find_host_package) 169 macro(find_host_package) 170 find_package(${ARGN}) 171 endmacro() 172endif() 173if(NOT COMMAND find_host_program) 174 macro(find_host_program) 175 find_program(${ARGN}) 176 endmacro() 177endif() 178 179find_host_package(PythonInterp) 180 181# Check for symbol exports on Linux. 182# At the moment, this check will fail on the OSX build machines for the Android NDK. 183# It appears they don't have objdump. 184if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") 185 macro(spvtools_check_symbol_exports TARGET) 186 if (NOT "${SPIRV_SKIP_TESTS}") 187 add_test(NAME spirv-tools-symbol-exports-${TARGET} 188 COMMAND ${PYTHON_EXECUTABLE} 189 ${spirv-tools_SOURCE_DIR}/utils/check_symbol_exports.py "$<TARGET_FILE:${TARGET}>") 190 endif() 191 endmacro() 192else() 193 macro(spvtools_check_symbol_exports TARGET) 194 if (NOT "${SPIRV_SKIP_TESTS}") 195 message("Skipping symbol exports test for ${TARGET}") 196 endif() 197 endmacro() 198endif() 199 200# Defaults to OFF if the user didn't set it. 201option(SPIRV_SKIP_EXECUTABLES 202 "Skip building the executable and tests along with the library" 203 ${SPIRV_SKIP_EXECUTABLES}) 204option(SPIRV_SKIP_TESTS 205 "Skip building tests along with the library" ${SPIRV_SKIP_TESTS}) 206if ("${SPIRV_SKIP_EXECUTABLES}") 207 set(SPIRV_SKIP_TESTS ON) 208endif() 209 210# Defaults to ON. The checks can be time consuming. 211# Turn off if they take too long. 212option(SPIRV_CHECK_CONTEXT "In a debug build, check if the IR context is in a valid state." ON) 213if (${SPIRV_CHECK_CONTEXT}) 214 add_definitions(-DSPIRV_CHECK_CONTEXT) 215endif() 216 217# Precompiled header macro. Parameters are source file list and filename for pch cpp file. 218macro(spvtools_pch SRCS PCHPREFIX) 219 if(MSVC AND CMAKE_GENERATOR MATCHES "^Visual Studio") 220 set(PCH_NAME "$(IntDir)\\${PCHPREFIX}.pch") 221 # make source files use/depend on PCH_NAME 222 set_source_files_properties(${${SRCS}} PROPERTIES COMPILE_FLAGS "/Yu${PCHPREFIX}.h /FI${PCHPREFIX}.h /Fp${PCH_NAME} /Zm300" OBJECT_DEPENDS "${PCH_NAME}") 223 # make PCHPREFIX.cpp file compile and generate PCH_NAME 224 set_source_files_properties("${PCHPREFIX}.cpp" PROPERTIES COMPILE_FLAGS "/Yc${PCHPREFIX}.h /Fp${PCH_NAME} /Zm300" OBJECT_OUTPUTS "${PCH_NAME}") 225 list(APPEND ${SRCS} "${PCHPREFIX}.cpp") 226 endif() 227endmacro(spvtools_pch) 228 229add_subdirectory(external) 230 231add_subdirectory(source) 232add_subdirectory(tools) 233 234add_subdirectory(test) 235add_subdirectory(examples) 236 237if(ENABLE_SPIRV_TOOLS_INSTALL) 238 install( 239 FILES 240 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h 241 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp 242 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp 243 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/linker.hpp 244 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/instrument.hpp 245 DESTINATION 246 ${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/) 247endif(ENABLE_SPIRV_TOOLS_INSTALL) 248 249if (NOT "${SPIRV_SKIP_TESTS}") 250 add_test(NAME spirv-tools-copyrights 251 COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py 252 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) 253endif() 254 255set(SPIRV_LIBRARIES "-lSPIRV-Tools -lSPIRV-Tools-link -lSPIRV-Tools-opt") 256set(SPIRV_SHARED_LIBRARIES "-lSPIRV-Tools-shared") 257if(SPIRV_BUILD_COMPRESSION) 258 set(SPIRV_LIBRARIES "${SPIRV_LIBRARIES} -lSPIRV-Tools-comp") 259endif(SPIRV_BUILD_COMPRESSION) 260 261# Build pkg-config file 262# Use a first-class target so it's regenerated when relevant files are updated. 263add_custom_target(spirv-tools-pkg-config ALL 264 COMMAND ${CMAKE_COMMAND} 265 -DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES 266 -DTEMPLATE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools.pc.in 267 -DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc 268 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} 269 -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR} 270 -DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR} 271 -DSPIRV_LIBRARIES=${SPIRV_LIBRARIES} 272 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake 273 DEPENDS "CHANGES" "cmake/SPIRV-Tools.pc.in" "cmake/write_pkg_config.cmake") 274add_custom_target(spirv-tools-shared-pkg-config ALL 275 COMMAND ${CMAKE_COMMAND} 276 -DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES 277 -DTEMPLATE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools-shared.pc.in 278 -DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc 279 -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} 280 -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR} 281 -DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR} 282 -DSPIRV_SHARED_LIBRARIES=${SPIRV_SHARED_LIBRARIES} 283 -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake 284 DEPENDS "CHANGES" "cmake/SPIRV-Tools-shared.pc.in" "cmake/write_pkg_config.cmake") 285 286# Install pkg-config file 287if (ENABLE_SPIRV_TOOLS_INSTALL) 288 install( 289 FILES 290 ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc 291 ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc 292 DESTINATION 293 ${CMAKE_INSTALL_LIBDIR}/pkgconfig) 294endif() 295