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