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