Add /EHs for targets for MSVC
[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 "CYGWIN")
34   add_definitions(-DSPIRV_WINDOWS)
35 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
36   add_definitions(-DSPIRV_MAC)
37 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
38   add_definitions(-DSPIRV_ANDROID)
39 else()
40   message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
41 endif()
42
43 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
44   message(STATUS "No build type selected, default to Debug")
45   set(CMAKE_BUILD_TYPE "Debug")
46 endif()
47
48 option(SPIRV_WERROR "Enable error on warning" ON)
49 if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
50   set(COMPILER_IS_LIKE_GNU TRUE)
51 endif()
52 if(${COMPILER_IS_LIKE_GNU})
53   set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
54
55   option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
56   if(${SPIRV_WARN_EVERYTHING})
57     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
58       set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
59         -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
60     elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
61       set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
62     else()
63       message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
64                      "so SPIRV_WARN_EVERYTHING has no effect")
65     endif()
66   endif()
67
68   if(${SPIRV_WERROR})
69     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
70   endif()
71 elseif(MSVC)
72   set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
73
74   if(${SPIRV_WERROR})
75     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
76   endif()
77 endif()
78
79 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
80
81 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
82 if(${SPIRV_COLOR_TERMINAL})
83   add_definitions(-DSPIRV_COLOR_TERMINAL)
84 endif()
85
86 option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
87 if(${SPIRV_LOG_DEBUG})
88   add_definitions(-DSPIRV_LOG_DEBUG)
89 endif()
90
91 function(spvtools_default_compile_options TARGET)
92   target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
93
94   if (${COMPILER_IS_LIKE_GNU})
95     target_compile_options(${TARGET} PRIVATE
96       -std=c++11 -fno-exceptions -fno-rtti)
97     target_compile_options(${TARGET} PRIVATE
98       -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
99       -Wno-sign-conversion)
100     # For good call stacks in profiles, keep the frame pointers.
101     if(NOT "${SPIRV_PERF}" STREQUAL "")
102       target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
103     endif()
104     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
105       set(SPIRV_USE_SANITIZER "" CACHE STRING
106         "Use the clang sanitizer [address|memory|thread|...]")
107       if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
108         target_compile_options(${TARGET} PRIVATE
109           -fsanitize=${SPIRV_USE_SANITIZER})
110       endif()
111     else()
112       target_compile_options(${TARGET} PRIVATE
113          -Wno-missing-field-initializers)
114     endif()
115   endif()
116
117   if (MSVC)
118     # Specify /EHs for exception handling. This makes using SPIRV-Tools as
119     # dependencies in other projects easier.
120     target_compile_options(${TARGET} PRIVATE /EHs)
121   endif()
122
123   # For MinGW cross compile, statically link to the C++ runtime.
124   # But it still depends on MSVCRT.dll.
125   if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
126     if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
127       set_target_properties(${TARGET} PROPERTIES
128         LINK_FLAGS -static -static-libgcc -static-libstdc++)
129     endif()
130   endif()
131 endfunction()
132
133 if(NOT COMMAND find_host_package)
134   macro(find_host_package)
135     find_package(${ARGN})
136   endmacro()
137 endif()
138 if(NOT COMMAND find_host_program)
139   macro(find_host_program)
140     find_program(${ARGN})
141   endmacro()
142 endif()
143
144 find_host_package(PythonInterp)
145
146
147 # Defaults to OFF if the user didn't set it.
148 option(SPIRV_SKIP_EXECUTABLES
149   "Skip building the executable and tests along with the library"
150   ${SPIRV_SKIP_EXECUTABLES})
151 option(SPIRV_SKIP_TESTS
152   "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
153 if ("${SPIRV_SKIP_EXECUTABLES}")
154   set(SPIRV_SKIP_TESTS ON)
155 endif()
156
157 add_subdirectory(external)
158
159 add_subdirectory(source)
160 add_subdirectory(tools)
161
162 add_subdirectory(test)
163 add_subdirectory(examples)
164
165 install(
166   FILES
167     ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
168     ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
169     ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
170   DESTINATION
171     include/spirv-tools/)
172
173 add_test(NAME spirv-tools-copyrights
174          COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py
175          WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})