Fix InsertFeedingExtract rule when extract remains.
[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
52 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
53   message(STATUS "No build type selected, default to Debug")
54   set(CMAKE_BUILD_TYPE "Debug")
55 endif()
56
57 option(SKIP_SPIRV_TOOLS_INSTALL "Skip installation" ${SKIP_SPIRV_TOOLS_INSTALL})
58 if(NOT ${SKIP_SPIRV_TOOLS_INSTALL})
59   set(ENABLE_SPIRV_TOOLS_INSTALL ON)
60 endif()
61
62 option(SPIRV_BUILD_COMPRESSION "Build SPIR-V compressing codec" OFF)
63
64 option(SPIRV_WERROR "Enable error on warning" ON)
65 if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
66   set(COMPILER_IS_LIKE_GNU TRUE)
67 endif()
68 if(${COMPILER_IS_LIKE_GNU})
69   set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
70
71   option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
72   if(${SPIRV_WARN_EVERYTHING})
73     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
74       set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
75         -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
76     elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
77       set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
78     else()
79       message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
80                      "so SPIRV_WARN_EVERYTHING has no effect")
81     endif()
82   endif()
83
84   if(${SPIRV_WERROR})
85     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
86   endif()
87 elseif(MSVC)
88   set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS /wd4800)
89
90   if(${SPIRV_WERROR})
91     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
92   endif()
93 endif()
94
95 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
96
97 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
98 if(${SPIRV_COLOR_TERMINAL})
99   add_definitions(-DSPIRV_COLOR_TERMINAL)
100 endif()
101
102 option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
103 if(${SPIRV_LOG_DEBUG})
104   add_definitions(-DSPIRV_LOG_DEBUG)
105 endif()
106
107 if (DEFINED SPIRV_TOOLS_EXTRA_DEFINITIONS)
108   add_definitions(${SPIRV_TOOLS_EXTRA_DEFINITIONS})
109 endif()
110
111 function(spvtools_default_compile_options TARGET)
112   target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
113
114   if (${COMPILER_IS_LIKE_GNU})
115     target_compile_options(${TARGET} PRIVATE
116       -std=c++11 -fno-exceptions -fno-rtti)
117     target_compile_options(${TARGET} PRIVATE
118       -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
119       -Wno-sign-conversion)
120     # For good call stacks in profiles, keep the frame pointers.
121     if(NOT "${SPIRV_PERF}" STREQUAL "")
122       target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
123     endif()
124     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
125       set(SPIRV_USE_SANITIZER "" CACHE STRING
126         "Use the clang sanitizer [address|memory|thread|...]")
127       if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
128         target_compile_options(${TARGET} PRIVATE
129           -fsanitize=${SPIRV_USE_SANITIZER})
130       endif()
131     else()
132       target_compile_options(${TARGET} PRIVATE
133          -Wno-missing-field-initializers)
134     endif()
135   endif()
136
137   if (MSVC)
138     # Specify /EHs for exception handling. This makes using SPIRV-Tools as
139     # dependencies in other projects easier.
140     target_compile_options(${TARGET} PRIVATE /EHs)
141   endif()
142
143   # For MinGW cross compile, statically link to the C++ runtime.
144   # But it still depends on MSVCRT.dll.
145   if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
146     if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
147       set_target_properties(${TARGET} PROPERTIES
148         LINK_FLAGS -static -static-libgcc -static-libstdc++)
149     endif()
150   endif()
151 endfunction()
152
153 if(NOT COMMAND find_host_package)
154   macro(find_host_package)
155     find_package(${ARGN})
156   endmacro()
157 endif()
158 if(NOT COMMAND find_host_program)
159   macro(find_host_program)
160     find_program(${ARGN})
161   endmacro()
162 endif()
163
164 find_host_package(PythonInterp)
165
166 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
167   macro(spvtools_check_symbol_exports TARGET)
168     add_test(NAME spirv-tools-symbol-exports-${TARGET}
169              COMMAND ${PYTHON_EXECUTABLE}
170              ${spirv-tools_SOURCE_DIR}/utils/check_symbol_exports.py "$<TARGET_FILE:${TARGET}>")
171   endmacro()
172 else()
173   macro(spvtools_check_symbol_exports TARGET)
174     message("Skipping symbol exports test for ${TARGET}")
175   endmacro()
176 endif()
177
178 # Defaults to OFF if the user didn't set it.
179 option(SPIRV_SKIP_EXECUTABLES
180   "Skip building the executable and tests along with the library"
181   ${SPIRV_SKIP_EXECUTABLES})
182 option(SPIRV_SKIP_TESTS
183   "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
184 if ("${SPIRV_SKIP_EXECUTABLES}")
185   set(SPIRV_SKIP_TESTS ON)
186 endif()
187
188 # Defaults to ON.  The checks can be time consuming.
189 # Turn off if they take too long.
190 option(SPIRV_CHECK_CONTEXT "In a debug build, check if the IR context is in a valid state." ON)
191 if (${SPIRV_CHECK_CONTEXT})
192   add_definitions(-DSPIRV_CHECK_CONTEXT)
193 endif()
194
195 add_subdirectory(external)
196
197 if (TARGET effcee)
198   add_definitions(-DSPIRV_EFFCEE)
199 endif()
200
201 add_subdirectory(source)
202 add_subdirectory(tools)
203
204 add_subdirectory(test)
205 add_subdirectory(examples)
206
207 if(ENABLE_SPIRV_TOOLS_INSTALL)
208   install(
209     FILES
210       ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
211       ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
212       ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
213       ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/linker.hpp
214     DESTINATION
215       ${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/)
216 endif(ENABLE_SPIRV_TOOLS_INSTALL)
217
218 if (NOT "${SPIRV_SKIP_TESTS}")
219   add_test(NAME spirv-tools-copyrights
220            COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py
221            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
222 endif()
223
224 set(SPIRV_LIBRARIES "-lSPIRV-Tools -lSPIRV-Tools-link -lSPIRV-Tools-opt")
225 set(SPIRV_SHARED_LIBRARIES "-lSPIRV-Tools-shared")
226 if(SPIRV_BUILD_COMPRESSION)
227   set(SPIRV_LIBRARIES "${SPIRV_LIBRARIES} -lSPIRV-Tools-comp")
228 endif(SPIRV_BUILD_COMPRESSION)
229
230 # Build pkg-config file
231 # Use a first-class target so it's regenerated when relevant files are updated.
232 add_custom_target(spirv-tools-pkg-config ALL
233         COMMAND ${CMAKE_COMMAND}
234                       -DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES
235                       -DTEMPLATE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools.pc.in
236                       -DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc
237                       -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
238                       -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR}
239                       -DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR}
240                       -DSPIRV_LIBRARIES=${SPIRV_LIBRARIES}
241                       -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake
242         DEPENDS "CHANGES" "cmake/SPIRV-Tools.pc.in" "cmake/write_pkg_config.cmake")
243 add_custom_target(spirv-tools-shared-pkg-config ALL
244         COMMAND ${CMAKE_COMMAND}
245                       -DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES
246                       -DTEMPLATE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools-shared.pc.in
247                       -DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc
248                       -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
249                       -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR}
250                       -DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR}
251                       -DSPIRV_SHARED_LIBRARIES=${SPIRV_SHARED_LIBRARIES}
252                       -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake
253         DEPENDS "CHANGES" "cmake/SPIRV-Tools-shared.pc.in" "cmake/write_pkg_config.cmake")
254
255 # Install pkg-config file
256 if (ENABLE_SPIRV_TOOLS_INSTALL)
257   install(
258     FILES
259       ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc
260       ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc
261     DESTINATION
262       ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
263 endif()