Implement Linker (module combiner)
[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_BUILD_COMPRESSION "Build SPIR-V compressing codec" OFF)
59
60 option(SPIRV_WERROR "Enable error on warning" ON)
61 if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
62   set(COMPILER_IS_LIKE_GNU TRUE)
63 endif()
64 if(${COMPILER_IS_LIKE_GNU})
65   set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
66
67   option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
68   if(${SPIRV_WARN_EVERYTHING})
69     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
70       set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
71         -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
72     elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
73       set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
74     else()
75       message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
76                      "so SPIRV_WARN_EVERYTHING has no effect")
77     endif()
78   endif()
79
80   if(${SPIRV_WERROR})
81     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
82   endif()
83 elseif(MSVC)
84   set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
85
86   if(${SPIRV_WERROR})
87     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
88   endif()
89 endif()
90
91 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
92
93 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
94 if(${SPIRV_COLOR_TERMINAL})
95   add_definitions(-DSPIRV_COLOR_TERMINAL)
96 endif()
97
98 option(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
99 if(${SPIRV_LOG_DEBUG})
100   add_definitions(-DSPIRV_LOG_DEBUG)
101 endif()
102
103 if (DEFINED SPIRV_TOOLS_EXTRA_DEFINITIONS)
104   add_definitions(${SPIRV_TOOLS_EXTRA_DEFINITIONS})
105 endif()
106
107 function(spvtools_default_compile_options TARGET)
108   target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
109
110   if (${COMPILER_IS_LIKE_GNU})
111     target_compile_options(${TARGET} PRIVATE
112       -std=c++11 -fno-exceptions -fno-rtti)
113     target_compile_options(${TARGET} PRIVATE
114       -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
115       -Wno-sign-conversion)
116     # For good call stacks in profiles, keep the frame pointers.
117     if(NOT "${SPIRV_PERF}" STREQUAL "")
118       target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
119     endif()
120     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
121       set(SPIRV_USE_SANITIZER "" CACHE STRING
122         "Use the clang sanitizer [address|memory|thread|...]")
123       if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
124         target_compile_options(${TARGET} PRIVATE
125           -fsanitize=${SPIRV_USE_SANITIZER})
126       endif()
127     else()
128       target_compile_options(${TARGET} PRIVATE
129          -Wno-missing-field-initializers)
130     endif()
131   endif()
132
133   if (MSVC)
134     # Specify /EHs for exception handling. This makes using SPIRV-Tools as
135     # dependencies in other projects easier.
136     target_compile_options(${TARGET} PRIVATE /EHs)
137   endif()
138
139   # For MinGW cross compile, statically link to the C++ runtime.
140   # But it still depends on MSVCRT.dll.
141   if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
142     if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
143       set_target_properties(${TARGET} PROPERTIES
144         LINK_FLAGS -static -static-libgcc -static-libstdc++)
145     endif()
146   endif()
147 endfunction()
148
149 if(NOT COMMAND find_host_package)
150   macro(find_host_package)
151     find_package(${ARGN})
152   endmacro()
153 endif()
154 if(NOT COMMAND find_host_program)
155   macro(find_host_program)
156     find_program(${ARGN})
157   endmacro()
158 endif()
159
160 find_host_package(PythonInterp)
161
162
163 # Defaults to OFF if the user didn't set it.
164 option(SPIRV_SKIP_EXECUTABLES
165   "Skip building the executable and tests along with the library"
166   ${SPIRV_SKIP_EXECUTABLES})
167 option(SPIRV_SKIP_TESTS
168   "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
169 if ("${SPIRV_SKIP_EXECUTABLES}")
170   set(SPIRV_SKIP_TESTS ON)
171 endif()
172
173 add_subdirectory(external)
174
175 add_subdirectory(source)
176 add_subdirectory(tools)
177
178 add_subdirectory(test)
179 add_subdirectory(examples)
180
181 if(ENABLE_SPIRV_TOOLS_INSTALL)
182   install(
183     FILES
184       ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
185       ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
186       ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
187       ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/linker.hpp
188     DESTINATION
189       ${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/)
190 endif(ENABLE_SPIRV_TOOLS_INSTALL)
191
192 if (NOT "${SPIRV_SKIP_TESTS}")
193   add_test(NAME spirv-tools-copyrights
194            COMMAND ${PYTHON_EXECUTABLE} utils/check_copyright.py
195            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
196  endif()