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 include(GNUInstallDirs)
30 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
32 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
33 add_definitions(-DSPIRV_LINUX)
34 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
35 add_definitions(-DSPIRV_WINDOWS)
36 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
37 add_definitions(-DSPIRV_WINDOWS)
38 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
39 add_definitions(-DSPIRV_MAC)
40 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
41 add_definitions(-DSPIRV_ANDROID)
42 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
43 add_definitions(-DSPIRV_FREEBSD)
45 message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
48 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
49 message(STATUS "No build type selected, default to Debug")
50 set(CMAKE_BUILD_TYPE "Debug")
53 option(SKIP_SPIRV_TOOLS_INSTALL "Skip installation" ${SKIP_SPIRV_TOOLS_INSTALL})
54 if(NOT ${SKIP_SPIRV_TOOLS_INSTALL})
55 set(ENABLE_SPIRV_TOOLS_INSTALL ON)
58 option(SPIRV_BUILD_COMPRESSION "Build SPIR-V compressing codec" OFF)
60 option(SPIRV_WERROR "Enable error on warning" ON)
61 if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
62 set(COMPILER_IS_LIKE_GNU TRUE)
64 if(${COMPILER_IS_LIKE_GNU})
65 set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
67 option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
68 if(${SPIRV_WARN_EVERYTHING})
69 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
70 set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
71 -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
72 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
73 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
75 message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
76 "so SPIRV_WARN_EVERYTHING has no effect")
81 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
84 set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
87 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
91 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
93 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
94 if(${SPIRV_COLOR_TERMINAL})
95 add_definitions(-DSPIRV_COLOR_TERMINAL)
98 option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
99 if(${SPIRV_LOG_DEBUG})
100 add_definitions(-DSPIRV_LOG_DEBUG)
103 if (DEFINED SPIRV_TOOLS_EXTRA_DEFINITIONS)
104 add_definitions(${SPIRV_TOOLS_EXTRA_DEFINITIONS})
107 function(spvtools_default_compile_options TARGET)
108 target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
110 if (${COMPILER_IS_LIKE_GNU})
111 target_compile_options(${TARGET} PRIVATE
112 -std=c++11 -fno-exceptions -fno-rtti)
113 target_compile_options(${TARGET} PRIVATE
114 -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
115 -Wno-sign-conversion)
116 # For good call stacks in profiles, keep the frame pointers.
117 if(NOT "${SPIRV_PERF}" STREQUAL "")
118 target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
120 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
121 set(SPIRV_USE_SANITIZER "" CACHE STRING
122 "Use the clang sanitizer [address|memory|thread|...]")
123 if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
124 target_compile_options(${TARGET} PRIVATE
125 -fsanitize=${SPIRV_USE_SANITIZER})
128 target_compile_options(${TARGET} PRIVATE
129 -Wno-missing-field-initializers)
134 # Specify /EHs for exception handling. This makes using SPIRV-Tools as
135 # dependencies in other projects easier.
136 target_compile_options(${TARGET} PRIVATE /EHs)
139 # For MinGW cross compile, statically link to the C++ runtime.
140 # But it still depends on MSVCRT.dll.
141 if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
142 if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
143 set_target_properties(${TARGET} PROPERTIES
144 LINK_FLAGS -static -static-libgcc -static-libstdc++)
149 if(NOT COMMAND find_host_package)
150 macro(find_host_package)
151 find_package(${ARGN})
154 if(NOT COMMAND find_host_program)
155 macro(find_host_program)
156 find_program(${ARGN})
160 find_host_package(PythonInterp)
163 # Defaults to OFF if the user didn't set it.
164 option(SPIRV_SKIP_EXECUTABLES
165 "Skip building the executable and tests along with the library"
166 ${SPIRV_SKIP_EXECUTABLES})
167 option(SPIRV_SKIP_TESTS
168 "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
169 if ("${SPIRV_SKIP_EXECUTABLES}")
170 set(SPIRV_SKIP_TESTS ON)
173 add_subdirectory(external)
175 add_subdirectory(source)
176 add_subdirectory(tools)
178 add_subdirectory(test)
179 add_subdirectory(examples)
181 if(ENABLE_SPIRV_TOOLS_INSTALL)
184 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
185 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
186 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
187 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/linker.hpp
189 ${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/)
190 endif(ENABLE_SPIRV_TOOLS_INSTALL)
192 if (NOT "${SPIRV_SKIP_TESTS}")
193 add_test(NAME spirv-tools-copyrights
194 COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py
195 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})