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_WERROR "Enable error on warning" ON)
59 if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
60 set(COMPILER_IS_LIKE_GNU TRUE)
62 if(${COMPILER_IS_LIKE_GNU})
63 set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
65 option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
66 if(${SPIRV_WARN_EVERYTHING})
67 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
68 set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
69 -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
70 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
71 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
73 message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
74 "so SPIRV_WARN_EVERYTHING has no effect")
79 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
82 set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
85 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
89 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
91 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
92 if(${SPIRV_COLOR_TERMINAL})
93 add_definitions(-DSPIRV_COLOR_TERMINAL)
96 option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
97 if(${SPIRV_LOG_DEBUG})
98 add_definitions(-DSPIRV_LOG_DEBUG)
101 if (DEFINED SPIRV_TOOLS_EXTRA_DEFINITIONS)
102 add_definitions(${SPIRV_TOOLS_EXTRA_DEFINITIONS})
105 function(spvtools_default_compile_options TARGET)
106 target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
108 if (${COMPILER_IS_LIKE_GNU})
109 target_compile_options(${TARGET} PRIVATE
110 -std=c++11 -fno-exceptions -fno-rtti)
111 target_compile_options(${TARGET} PRIVATE
112 -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
113 -Wno-sign-conversion)
114 # For good call stacks in profiles, keep the frame pointers.
115 if(NOT "${SPIRV_PERF}" STREQUAL "")
116 target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
118 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
119 set(SPIRV_USE_SANITIZER "" CACHE STRING
120 "Use the clang sanitizer [address|memory|thread|...]")
121 if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
122 target_compile_options(${TARGET} PRIVATE
123 -fsanitize=${SPIRV_USE_SANITIZER})
126 target_compile_options(${TARGET} PRIVATE
127 -Wno-missing-field-initializers)
132 # Specify /EHs for exception handling. This makes using SPIRV-Tools as
133 # dependencies in other projects easier.
134 target_compile_options(${TARGET} PRIVATE /EHs)
137 # For MinGW cross compile, statically link to the C++ runtime.
138 # But it still depends on MSVCRT.dll.
139 if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
140 if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
141 set_target_properties(${TARGET} PROPERTIES
142 LINK_FLAGS -static -static-libgcc -static-libstdc++)
147 if(NOT COMMAND find_host_package)
148 macro(find_host_package)
149 find_package(${ARGN})
152 if(NOT COMMAND find_host_program)
153 macro(find_host_program)
154 find_program(${ARGN})
158 find_host_package(PythonInterp)
161 # Defaults to OFF if the user didn't set it.
162 option(SPIRV_SKIP_EXECUTABLES
163 "Skip building the executable and tests along with the library"
164 ${SPIRV_SKIP_EXECUTABLES})
165 option(SPIRV_SKIP_TESTS
166 "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
167 if ("${SPIRV_SKIP_EXECUTABLES}")
168 set(SPIRV_SKIP_TESTS ON)
171 add_subdirectory(external)
173 add_subdirectory(source)
174 add_subdirectory(tools)
176 add_subdirectory(test)
177 add_subdirectory(examples)
179 if(ENABLE_SPIRV_TOOLS_INSTALL)
182 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
183 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
184 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
186 ${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/)
187 endif(ENABLE_SPIRV_TOOLS_INSTALL)
189 add_test(NAME spirv-tools-copyrights
190 COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py
191 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})