Relicense SPIRV-Tools under Apache 2.0
[platform/upstream/SPIRV-Tools.git] / CMakeLists.txt
1 # Copyright (c) 2015-2016 The Khronos Group Inc.
2 #
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 cmake_minimum_required(VERSION 2.8.12)
16 if (POLICY CMP0054)
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)
21 endif()
22
23 project(spirv-tools)
24 enable_testing()
25 set(SPIRV_TOOLS "SPIRV-Tools")
26
27 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
28
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)
37 else()
38   message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
39 endif()
40
41 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
42   message(STATUS "No build type selected, default to Debug")
43   set(CMAKE_BUILD_TYPE "Debug")
44 endif()
45
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)
49 endif()
50 if(${COMPILER_IS_LIKE_GNU})
51   set(SPIRV_WARNINGS -Wall -Wextra -Wno-missing-field-initializers)
52
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)
60     else()
61       message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
62                      "so SPIRV_WARN_EVERYTHING has no effect")
63     endif()
64   endif()
65
66   if(${SPIRV_WERROR})
67     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
68   endif()
69 elseif(MSVC)
70   set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
71
72   if(${SPIRV_WERROR})
73     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
74   endif()
75 endif()
76
77 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
78
79 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
80 if(${SPIRV_COLOR_TERMINAL})
81   add_definitions(-DSPIRV_COLOR_TERMINAL)
82 endif()
83
84 function(spvtools_default_compile_options TARGET)
85   target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
86
87   if (${COMPILER_IS_LIKE_GNU})
88     target_compile_options(${TARGET} PRIVATE
89       -std=c++11 -fno-exceptions -fno-rtti)
90     target_compile_options(${TARGET} PRIVATE
91       -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
92       -Wno-sign-conversion)
93     # For good call stacks in profiles, keep the frame pointers.
94     if(NOT "${SPIRV_PERF}" STREQUAL "")
95       target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
96     endif()
97     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
98       set(SPIRV_USE_SANITIZER "" CACHE STRING
99         "Use the clang sanitizer [address|memory|thread|...]")
100       if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
101         target_compile_options(${TARGET} PRIVATE
102           -fsanitize=${SPIRV_USE_SANITIZER})
103       endif()
104     else()
105       target_compile_options(${TARGET} PRIVATE
106          -Wno-missing-field-initializers)
107     endif()
108   endif()
109
110   # For MinGW cross compile, statically link to the C++ runtime.
111   # But it still depends on MSVCRT.dll.
112   if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
113     if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
114       set_target_properties(${TARGET} PROPERTIES
115         LINK_FLAGS -static -static-libgcc -static-libstdc++)
116     endif()
117   endif()
118 endfunction()
119
120 if(NOT COMMAND find_host_package)
121   macro(find_host_package)
122     find_package(${ARGN})
123   endmacro()
124 endif()
125 if(NOT COMMAND find_host_program)
126   macro(find_host_program)
127     find_program(${ARGN})
128   endmacro()
129 endif()
130
131 find_host_package(PythonInterp)
132
133
134 # Defaults to OFF if the user didn't set it.
135 option(SPIRV_SKIP_EXECUTABLES
136   "Skip building the executable and tests along with the library"
137   ${SPIRV_SKIP_EXECUTABLES})
138 option(SPIRV_SKIP_TESTS
139   "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
140 if ("${SPIRV_SKIP_EXECUTABLES}")
141   set(SPIRV_SKIP_TESTS ON)
142 endif()
143
144 add_subdirectory(external)
145
146 add_subdirectory(source)
147 add_subdirectory(tools)
148
149 add_subdirectory(test)
150
151 install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
152   DESTINATION include/spirv-tools/)