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 cmake_policy(SET CMP0048 NEW)
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)
25 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
29 set(SPIRV_TOOLS "SPIRV-Tools")
31 include(GNUInstallDirs)
33 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
35 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
36 add_definitions(-DSPIRV_LINUX)
37 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
38 add_definitions(-DSPIRV_WINDOWS)
39 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
40 add_definitions(-DSPIRV_WINDOWS)
41 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
42 add_definitions(-DSPIRV_MAC)
43 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
44 add_definitions(-DSPIRV_ANDROID)
45 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
46 add_definitions(-DSPIRV_FREEBSD)
48 message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
52 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
53 message(STATUS "No build type selected, default to Debug")
54 set(CMAKE_BUILD_TYPE "Debug")
57 option(SKIP_SPIRV_TOOLS_INSTALL "Skip installation" ${SKIP_SPIRV_TOOLS_INSTALL})
58 if(NOT ${SKIP_SPIRV_TOOLS_INSTALL})
59 set(ENABLE_SPIRV_TOOLS_INSTALL ON)
62 option(SPIRV_BUILD_COMPRESSION "Build SPIR-V compressing codec" OFF)
64 option(SPIRV_WERROR "Enable error on warning" ON)
65 if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
66 set(COMPILER_IS_LIKE_GNU TRUE)
68 if(${COMPILER_IS_LIKE_GNU})
69 set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
71 option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
72 if(${SPIRV_WARN_EVERYTHING})
73 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
74 set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
75 -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
76 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
77 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
79 message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
80 "so SPIRV_WARN_EVERYTHING has no effect")
85 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
88 set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
91 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
95 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
97 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
98 if(${SPIRV_COLOR_TERMINAL})
99 add_definitions(-DSPIRV_COLOR_TERMINAL)
102 option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
103 if(${SPIRV_LOG_DEBUG})
104 add_definitions(-DSPIRV_LOG_DEBUG)
107 if (DEFINED SPIRV_TOOLS_EXTRA_DEFINITIONS)
108 add_definitions(${SPIRV_TOOLS_EXTRA_DEFINITIONS})
111 function(spvtools_default_compile_options TARGET)
112 target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
114 if (${COMPILER_IS_LIKE_GNU})
115 target_compile_options(${TARGET} PRIVATE
116 -std=c++11 -fno-exceptions -fno-rtti)
117 target_compile_options(${TARGET} PRIVATE
118 -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
119 -Wno-sign-conversion)
120 # For good call stacks in profiles, keep the frame pointers.
121 if(NOT "${SPIRV_PERF}" STREQUAL "")
122 target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
124 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
125 set(SPIRV_USE_SANITIZER "" CACHE STRING
126 "Use the clang sanitizer [address|memory|thread|...]")
127 if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
128 target_compile_options(${TARGET} PRIVATE
129 -fsanitize=${SPIRV_USE_SANITIZER})
132 target_compile_options(${TARGET} PRIVATE
133 -Wno-missing-field-initializers)
138 # Specify /EHs for exception handling. This makes using SPIRV-Tools as
139 # dependencies in other projects easier.
140 target_compile_options(${TARGET} PRIVATE /EHs)
143 # For MinGW cross compile, statically link to the C++ runtime.
144 # But it still depends on MSVCRT.dll.
145 if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
146 if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
147 set_target_properties(${TARGET} PROPERTIES
148 LINK_FLAGS -static -static-libgcc -static-libstdc++)
153 if(NOT COMMAND find_host_package)
154 macro(find_host_package)
155 find_package(${ARGN})
158 if(NOT COMMAND find_host_program)
159 macro(find_host_program)
160 find_program(${ARGN})
164 find_host_package(PythonInterp)
166 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
167 macro(spvtools_check_symbol_exports TARGET)
168 add_test(NAME spirv-tools-symbol-exports-${TARGET}
169 COMMAND ${PYTHON_EXECUTABLE}
170 ${spirv-tools_SOURCE_DIR}/utils/check_symbol_exports.py "$<TARGET_FILE:${TARGET}>")
173 macro(spvtools_check_symbol_exports TARGET)
174 message("Skipping symbol exports test for ${TARGET}")
178 # Defaults to OFF if the user didn't set it.
179 option(SPIRV_SKIP_EXECUTABLES
180 "Skip building the executable and tests along with the library"
181 ${SPIRV_SKIP_EXECUTABLES})
182 option(SPIRV_SKIP_TESTS
183 "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
184 if ("${SPIRV_SKIP_EXECUTABLES}")
185 set(SPIRV_SKIP_TESTS ON)
188 # Defaults to ON. The checks can be time consuming.
189 # Turn off if they take too long.
190 option(SPIRV_CHECK_CONTEXT "In a debug build, check if the IR context is in a valid state." ON)
191 if (${SPIRV_CHECK_CONTEXT})
192 add_definitions(-DSPIRV_CHECK_CONTEXT)
195 add_subdirectory(external)
198 add_definitions(-DSPIRV_EFFCEE)
201 add_subdirectory(source)
202 add_subdirectory(tools)
204 add_subdirectory(test)
205 add_subdirectory(examples)
207 if(ENABLE_SPIRV_TOOLS_INSTALL)
210 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
211 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
212 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
213 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/linker.hpp
215 ${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/)
216 endif(ENABLE_SPIRV_TOOLS_INSTALL)
218 if (NOT "${SPIRV_SKIP_TESTS}")
219 add_test(NAME spirv-tools-copyrights
220 COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py
221 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})