Tests: Add optional dependency on Effcee stateful matcher
[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 CMP0048)
17   cmake_policy(SET CMP0048 NEW)
18 endif()
19 if (POLICY CMP0054)
20   # Avoid dereferencing variables or interpret keywords that have been
21   # quoted or bracketed.
22   # https://cmake.org/cmake/help/v3.1/policy/CMP0054.html
23   cmake_policy(SET CMP0054 NEW)
24 endif()
25 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
26
27 project(spirv-tools)
28 enable_testing()
29 set(SPIRV_TOOLS "SPIRV-Tools")
30
31 include(GNUInstallDirs)
32
33 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
34
35 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
36   add_definitions(-DSPIRV_LINUX)
37 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
38   add_definitions(-DSPIRV_WINDOWS)
39 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
40   add_definitions(-DSPIRV_WINDOWS)
41 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
42   add_definitions(-DSPIRV_MAC)
43 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
44   add_definitions(-DSPIRV_ANDROID)
45 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
46   add_definitions(-DSPIRV_FREEBSD)
47 else()
48   message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
49 endif()
50
51 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
52   message(STATUS "No build type selected, default to Debug")
53   set(CMAKE_BUILD_TYPE "Debug")
54 endif()
55
56 option(SKIP_SPIRV_TOOLS_INSTALL "Skip installation" ${SKIP_SPIRV_TOOLS_INSTALL})
57 if(NOT ${SKIP_SPIRV_TOOLS_INSTALL})
58   set(ENABLE_SPIRV_TOOLS_INSTALL ON)
59 endif()
60
61 option(SPIRV_BUILD_COMPRESSION "Build SPIR-V compressing codec" OFF)
62
63 option(SPIRV_WERROR "Enable error on warning" ON)
64 if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
65   set(COMPILER_IS_LIKE_GNU TRUE)
66 endif()
67 if(${COMPILER_IS_LIKE_GNU})
68   set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
69
70   option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
71   if(${SPIRV_WARN_EVERYTHING})
72     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
73       set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
74         -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
75     elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
76       set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
77     else()
78       message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
79                      "so SPIRV_WARN_EVERYTHING has no effect")
80     endif()
81   endif()
82
83   if(${SPIRV_WERROR})
84     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
85   endif()
86 elseif(MSVC)
87   set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
88
89   if(${SPIRV_WERROR})
90     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
91   endif()
92 endif()
93
94 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
95
96 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
97 if(${SPIRV_COLOR_TERMINAL})
98   add_definitions(-DSPIRV_COLOR_TERMINAL)
99 endif()
100
101 option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
102 if(${SPIRV_LOG_DEBUG})
103   add_definitions(-DSPIRV_LOG_DEBUG)
104 endif()
105
106 if (DEFINED SPIRV_TOOLS_EXTRA_DEFINITIONS)
107   add_definitions(${SPIRV_TOOLS_EXTRA_DEFINITIONS})
108 endif()
109
110 function(spvtools_default_compile_options TARGET)
111   target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
112
113   if (${COMPILER_IS_LIKE_GNU})
114     target_compile_options(${TARGET} PRIVATE
115       -std=c++11 -fno-exceptions -fno-rtti)
116     target_compile_options(${TARGET} PRIVATE
117       -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
118       -Wno-sign-conversion)
119     # For good call stacks in profiles, keep the frame pointers.
120     if(NOT "${SPIRV_PERF}" STREQUAL "")
121       target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
122     endif()
123     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
124       set(SPIRV_USE_SANITIZER "" CACHE STRING
125         "Use the clang sanitizer [address|memory|thread|...]")
126       if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
127         target_compile_options(${TARGET} PRIVATE
128           -fsanitize=${SPIRV_USE_SANITIZER})
129       endif()
130     else()
131       target_compile_options(${TARGET} PRIVATE
132          -Wno-missing-field-initializers)
133     endif()
134   endif()
135
136   if (MSVC)
137     # Specify /EHs for exception handling. This makes using SPIRV-Tools as
138     # dependencies in other projects easier.
139     target_compile_options(${TARGET} PRIVATE /EHs)
140   endif()
141
142   # For MinGW cross compile, statically link to the C++ runtime.
143   # But it still depends on MSVCRT.dll.
144   if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
145     if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
146       set_target_properties(${TARGET} PROPERTIES
147         LINK_FLAGS -static -static-libgcc -static-libstdc++)
148     endif()
149   endif()
150 endfunction()
151
152 if(NOT COMMAND find_host_package)
153   macro(find_host_package)
154     find_package(${ARGN})
155   endmacro()
156 endif()
157 if(NOT COMMAND find_host_program)
158   macro(find_host_program)
159     find_program(${ARGN})
160   endmacro()
161 endif()
162
163 find_host_package(PythonInterp)
164
165
166 # Defaults to OFF if the user didn't set it.
167 option(SPIRV_SKIP_EXECUTABLES
168   "Skip building the executable and tests along with the library"
169   ${SPIRV_SKIP_EXECUTABLES})
170 option(SPIRV_SKIP_TESTS
171   "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
172 if ("${SPIRV_SKIP_EXECUTABLES}")
173   set(SPIRV_SKIP_TESTS ON)
174 endif()
175
176 # Defaults to ON.  The checks can be time consuming.
177 # Turn off if they take too long.
178 option(SPIRV_CHECK_CONTEXT "In a debug build, check if the IR context is in a valid state." ON)
179 if (${SPIRV_CHECK_CONTEXT})
180   add_definitions(-DSPIRV_CHECK_CONTEXT)
181 endif()
182
183 add_subdirectory(external)
184
185 if (TARGET effcee)
186   add_definitions(-DSPIRV_EFFCEE)
187 endif()
188
189 add_subdirectory(source)
190 add_subdirectory(tools)
191
192 add_subdirectory(test)
193 add_subdirectory(examples)
194
195 if(ENABLE_SPIRV_TOOLS_INSTALL)
196   install(
197     FILES
198       ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
199       ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
200       ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
201       ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/linker.hpp
202     DESTINATION
203       ${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/)
204 endif(ENABLE_SPIRV_TOOLS_INSTALL)
205
206 if (NOT "${SPIRV_SKIP_TESTS}")
207   add_test(NAME spirv-tools-copyrights
208            COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py
209            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
210  endif()