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)
22 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
26 set(SPIRV_TOOLS "SPIRV-Tools")
28 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
30 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
31 add_definitions(-DSPIRV_LINUX)
32 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
33 add_definitions(-DSPIRV_WINDOWS)
34 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
35 add_definitions(-DSPIRV_WINDOWS)
36 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
37 add_definitions(-DSPIRV_MAC)
38 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
39 add_definitions(-DSPIRV_ANDROID)
41 message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
44 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
45 message(STATUS "No build type selected, default to Debug")
46 set(CMAKE_BUILD_TYPE "Debug")
49 option(SPIRV_WERROR "Enable error on warning" ON)
50 if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
51 set(COMPILER_IS_LIKE_GNU TRUE)
53 if(${COMPILER_IS_LIKE_GNU})
54 set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
56 option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
57 if(${SPIRV_WARN_EVERYTHING})
58 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
59 set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
60 -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
61 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
62 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
64 message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
65 "so SPIRV_WARN_EVERYTHING has no effect")
70 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
73 set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
76 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
80 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
82 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
83 if(${SPIRV_COLOR_TERMINAL})
84 add_definitions(-DSPIRV_COLOR_TERMINAL)
87 option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
88 if(${SPIRV_LOG_DEBUG})
89 add_definitions(-DSPIRV_LOG_DEBUG)
92 function(spvtools_default_compile_options TARGET)
93 target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
95 if (${COMPILER_IS_LIKE_GNU})
96 target_compile_options(${TARGET} PRIVATE
97 -std=c++11 -fno-exceptions -fno-rtti)
98 target_compile_options(${TARGET} PRIVATE
99 -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
100 -Wno-sign-conversion)
101 # For good call stacks in profiles, keep the frame pointers.
102 if(NOT "${SPIRV_PERF}" STREQUAL "")
103 target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
105 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
106 set(SPIRV_USE_SANITIZER "" CACHE STRING
107 "Use the clang sanitizer [address|memory|thread|...]")
108 if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
109 target_compile_options(${TARGET} PRIVATE
110 -fsanitize=${SPIRV_USE_SANITIZER})
113 target_compile_options(${TARGET} PRIVATE
114 -Wno-missing-field-initializers)
119 # Specify /EHs for exception handling. This makes using SPIRV-Tools as
120 # dependencies in other projects easier.
121 target_compile_options(${TARGET} PRIVATE /EHs)
124 # For MinGW cross compile, statically link to the C++ runtime.
125 # But it still depends on MSVCRT.dll.
126 if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
127 if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
128 set_target_properties(${TARGET} PROPERTIES
129 LINK_FLAGS -static -static-libgcc -static-libstdc++)
134 if(NOT COMMAND find_host_package)
135 macro(find_host_package)
136 find_package(${ARGN})
139 if(NOT COMMAND find_host_program)
140 macro(find_host_program)
141 find_program(${ARGN})
145 find_host_package(PythonInterp)
148 # Defaults to OFF if the user didn't set it.
149 option(SPIRV_SKIP_EXECUTABLES
150 "Skip building the executable and tests along with the library"
151 ${SPIRV_SKIP_EXECUTABLES})
152 option(SPIRV_SKIP_TESTS
153 "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
154 if ("${SPIRV_SKIP_EXECUTABLES}")
155 set(SPIRV_SKIP_TESTS ON)
158 add_subdirectory(external)
160 add_subdirectory(source)
161 add_subdirectory(tools)
163 add_subdirectory(test)
164 add_subdirectory(examples)
168 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
169 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
170 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
172 include/spirv-tools/)
174 add_test(NAME spirv-tools-copyrights
175 COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py
176 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})