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 "Darwin")
34 add_definitions(-DSPIRV_MAC)
35 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
36 add_definitions(-DSPIRV_ANDROID)
38 message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
41 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
42 message(STATUS "No build type selected, default to Debug")
43 set(CMAKE_BUILD_TYPE "Debug")
46 option(SPIRV_WERROR "Enable error on warning" ON)
47 if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
48 set(COMPILER_IS_LIKE_GNU TRUE)
50 if(${COMPILER_IS_LIKE_GNU})
51 set(SPIRV_WARNINGS -Wall -Wextra -Wno-missing-field-initializers)
53 option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
54 if(${SPIRV_WARN_EVERYTHING})
55 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
56 set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
57 -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
58 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
59 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
61 message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
62 "so SPIRV_WARN_EVERYTHING has no effect")
67 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
70 set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
73 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
77 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
79 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
80 if(${SPIRV_COLOR_TERMINAL})
81 add_definitions(-DSPIRV_COLOR_TERMINAL)
84 option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
85 if(${SPIRV_LOG_DEBUG})
86 add_definitions(-DSPIRV_LOG_DEBUG)
89 function(spvtools_default_compile_options TARGET)
90 target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
92 if (${COMPILER_IS_LIKE_GNU})
93 target_compile_options(${TARGET} PRIVATE
94 -std=c++11 -fno-exceptions -fno-rtti)
95 target_compile_options(${TARGET} PRIVATE
96 -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
98 # For good call stacks in profiles, keep the frame pointers.
99 if(NOT "${SPIRV_PERF}" STREQUAL "")
100 target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
102 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
103 set(SPIRV_USE_SANITIZER "" CACHE STRING
104 "Use the clang sanitizer [address|memory|thread|...]")
105 if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
106 target_compile_options(${TARGET} PRIVATE
107 -fsanitize=${SPIRV_USE_SANITIZER})
110 target_compile_options(${TARGET} PRIVATE
111 -Wno-missing-field-initializers)
115 # For MinGW cross compile, statically link to the C++ runtime.
116 # But it still depends on MSVCRT.dll.
117 if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
118 if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
119 set_target_properties(${TARGET} PROPERTIES
120 LINK_FLAGS -static -static-libgcc -static-libstdc++)
125 if(NOT COMMAND find_host_package)
126 macro(find_host_package)
127 find_package(${ARGN})
130 if(NOT COMMAND find_host_program)
131 macro(find_host_program)
132 find_program(${ARGN})
136 find_host_package(PythonInterp)
139 # Defaults to OFF if the user didn't set it.
140 option(SPIRV_SKIP_EXECUTABLES
141 "Skip building the executable and tests along with the library"
142 ${SPIRV_SKIP_EXECUTABLES})
143 option(SPIRV_SKIP_TESTS
144 "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
145 if ("${SPIRV_SKIP_EXECUTABLES}")
146 set(SPIRV_SKIP_TESTS ON)
149 add_subdirectory(external)
151 add_subdirectory(source)
152 add_subdirectory(tools)
154 add_subdirectory(test)
158 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
159 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
160 ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
162 include/spirv-tools/)
164 add_test(NAME spirv-tools-copyrights
165 COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py
166 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})