Support supplying extra definitions via CMAKE variable
[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 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
23
24 project(spirv-tools)
25 enable_testing()
26 set(SPIRV_TOOLS "SPIRV-Tools")
27
28 include(GNUInstallDirs)
29
30 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
31
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)
44 else()
45   message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
46 endif()
47
48 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
49   message(STATUS "No build type selected, default to Debug")
50   set(CMAKE_BUILD_TYPE "Debug")
51 endif()
52
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)
56 endif()
57  
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)
61 endif()
62 if(${COMPILER_IS_LIKE_GNU})
63   set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
64
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)
72     else()
73       message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
74                      "so SPIRV_WARN_EVERYTHING has no effect")
75     endif()
76   endif()
77
78   if(${SPIRV_WERROR})
79     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
80   endif()
81 elseif(MSVC)
82   set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
83
84   if(${SPIRV_WERROR})
85     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
86   endif()
87 endif()
88
89 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
90
91 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
92 if(${SPIRV_COLOR_TERMINAL})
93   add_definitions(-DSPIRV_COLOR_TERMINAL)
94 endif()
95
96 option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
97 if(${SPIRV_LOG_DEBUG})
98   add_definitions(-DSPIRV_LOG_DEBUG)
99 endif()
100
101 if (DEFINED SPIRV_TOOLS_EXTRA_DEFINITIONS)
102   add_definitions(${SPIRV_TOOLS_EXTRA_DEFINITIONS})
103 endif()
104
105 function(spvtools_default_compile_options TARGET)
106   target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
107
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)
117     endif()
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})
124       endif()
125     else()
126       target_compile_options(${TARGET} PRIVATE
127          -Wno-missing-field-initializers)
128     endif()
129   endif()
130
131   if (MSVC)
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)
135   endif()
136
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++)
143     endif()
144   endif()
145 endfunction()
146
147 if(NOT COMMAND find_host_package)
148   macro(find_host_package)
149     find_package(${ARGN})
150   endmacro()
151 endif()
152 if(NOT COMMAND find_host_program)
153   macro(find_host_program)
154     find_program(${ARGN})
155   endmacro()
156 endif()
157
158 find_host_package(PythonInterp)
159
160
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)
169 endif()
170
171 add_subdirectory(external)
172
173 add_subdirectory(source)
174 add_subdirectory(tools)
175
176 add_subdirectory(test)
177 add_subdirectory(examples)
178
179 if(ENABLE_SPIRV_TOOLS_INSTALL)
180   install(
181     FILES
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
185     DESTINATION
186       ${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/)
187 endif(ENABLE_SPIRV_TOOLS_INSTALL)
188
189 add_test(NAME spirv-tools-copyrights
190          COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py
191          WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})