1 # Copyright (c) 2015-2016 The Khronos Group Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 cmake_minimum_required(VERSION 2.8.12)
17 # Avoid dereferencing variables or interpret keywords that have been
18 # quoted or bracketed.
19 # https://cmake.org/cmake/help/v3.1/policy/CMP0054.html
20 cmake_policy(SET CMP0054 NEW)
25 set(SPIRV_TOOLS "SPIRV-Tools")
27 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
29 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
30 add_definitions(-DSPIRV_LINUX)
31 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
32 add_definitions(-DSPIRV_WINDOWS)
33 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
34 add_definitions(-DSPIRV_WINDOWS)
35 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
36 add_definitions(-DSPIRV_MAC)
37 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
38 add_definitions(-DSPIRV_ANDROID)
40 message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
43 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
44 message(STATUS "No build type selected, default to Debug")
45 set(CMAKE_BUILD_TYPE "Debug")
48 option(SPIRV_WERROR "Enable error on warning" ON)
49 if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
50 set(COMPILER_IS_LIKE_GNU TRUE)
52 if(${COMPILER_IS_LIKE_GNU})
53 set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
55 option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
56 if(${SPIRV_WARN_EVERYTHING})
57 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
58 set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
59 -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
60 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
61 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
63 message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
64 "so SPIRV_WARN_EVERYTHING has no effect")
69 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
72 set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
75 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
79 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
81 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
82 if(${SPIRV_COLOR_TERMINAL})
83 add_definitions(-DSPIRV_COLOR_TERMINAL)
86 option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
87 if(${SPIRV_LOG_DEBUG})
88 add_definitions(-DSPIRV_LOG_DEBUG)
91 function(spvtools_default_compile_options TARGET)
92 target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
94 if (${COMPILER_IS_LIKE_GNU})
95 target_compile_options(${TARGET} PRIVATE
96 -std=c++11 -fno-exceptions -fno-rtti)
97 target_compile_options(${TARGET} PRIVATE
98 -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
100 # For good call stacks in profiles, keep the frame pointers.
101 if(NOT "${SPIRV_PERF}" STREQUAL "")
102 target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
104 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
105 set(SPIRV_USE_SANITIZER "" CACHE STRING
106 "Use the clang sanitizer [address|memory|thread|...]")
107 if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
108 target_compile_options(${TARGET} PRIVATE
109 -fsanitize=${SPIRV_USE_SANITIZER})
112 target_compile_options(${TARGET} PRIVATE
113 -Wno-missing-field-initializers)
117 # For MinGW cross compile, statically link to the C++ runtime.
118 # But it still depends on MSVCRT.dll.
119 if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
120 if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
121 set_target_properties(${TARGET} PROPERTIES
122 LINK_FLAGS -static -static-libgcc -static-libstdc++)
127 if(NOT COMMAND find_host_package)
128 macro(find_host_package)
129 find_package(${ARGN})
132 if(NOT COMMAND find_host_program)
133 macro(find_host_program)
134 find_program(${ARGN})
138 find_host_package(PythonInterp)
141 # Defaults to OFF if the user didn't set it.
142 option(SPIRV_SKIP_EXECUTABLES
143 "Skip building the executable and tests along with the library"
144 ${SPIRV_SKIP_EXECUTABLES})
145 option(SPIRV_SKIP_TESTS
146 "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
147 if ("${SPIRV_SKIP_EXECUTABLES}")
148 set(SPIRV_SKIP_TESTS ON)
151 add_subdirectory(external)
153 add_subdirectory(source)
154 add_subdirectory(tools)
156 add_subdirectory(test)
157 add_subdirectory(examples)
161 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
162 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
163 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
165 include/spirv-tools/)
167 add_test(NAME spirv-tools-copyrights
168 COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py
169 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})