Use NEW behavior for policy CMP0054.
[platform/upstream/SPIRV-Tools.git] / CMakeLists.txt
1 # Copyright (c) 2015-2016 The Khronos Group Inc.
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a
4 # copy of this software and/or associated documentation files (the
5 # "Materials"), to deal in the Materials without restriction, including
6 # without limitation the rights to use, copy, modify, merge, publish,
7 # distribute, sublicense, and/or sell copies of the Materials, and to
8 # permit persons to whom the Materials are furnished to do so, subject to
9 # the following conditions:
10 #
11 # The above copyright notice and this permission notice shall be included
12 # in all copies or substantial portions of the Materials.
13 #
14 # MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
15 # KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
16 # SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
17 #    https://www.khronos.org/registry/
18 #
19 # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
26
27 cmake_minimum_required(VERSION 2.8.12)
28 if (POLICY CMP0054)
29   # Avoid dereferencing variables or interpret keywords that have been
30   # quoted or bracketed.
31   # https://cmake.org/cmake/help/v3.1/policy/CMP0054.html
32   cmake_policy(SET CMP0054 NEW)
33 endif()
34
35 project(spirv-tools)
36 enable_testing()
37 set(SPIRV_TOOLS "SPIRV-Tools")
38
39 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
40
41 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
42   add_definitions(-DSPIRV_LINUX)
43 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
44   add_definitions(-DSPIRV_WINDOWS)
45 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
46   add_definitions(-DSPIRV_MAC)
47 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
48   add_definitions(-DSPIRV_ANDROID)
49 else()
50   message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
51 endif()
52
53 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
54   message(STATUS "No build type selected, default to Debug")
55   set(CMAKE_BUILD_TYPE "Debug")
56 endif()
57
58 option(SPIRV_WERROR "Enable error on warning" ON)
59 if(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"))
60   set(COMPILER_IS_LIKE_GNU TRUE)
61 endif()
62 if(${COMPILER_IS_LIKE_GNU})
63   set(SPIRV_WARNINGS -Wall -Wextra -Wno-missing-field-initializers)
64
65   option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
66   if(${SPIRV_WARN_EVERYTHING})
67     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
68       set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
69         -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
70     elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
71       set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
72     else()
73       message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
74                      "so SPIRV_WARN_EVERYTHING has no effect")
75     endif()
76   endif()
77
78   if(${SPIRV_WERROR})
79     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
80   endif()
81 elseif(MSVC)
82   set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
83
84   if(${SPIRV_WERROR})
85     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
86   endif()
87 endif()
88
89 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/source)
90
91 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
92 if(${SPIRV_COLOR_TERMINAL})
93   add_definitions(-DSPIRV_COLOR_TERMINAL)
94 endif()
95
96 function(spvtools_default_compile_options TARGET)
97   target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
98
99   if (${COMPILER_IS_LIKE_GNU})
100     target_compile_options(${TARGET} PRIVATE
101       -std=c++11 -fno-exceptions -fno-rtti)
102     target_compile_options(${TARGET} PRIVATE
103       -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
104       -Wno-sign-conversion)
105     # For good call stacks in profiles, keep the frame pointers.
106     if(NOT "${SPIRV_PERF}" STREQUAL "")
107       target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
108     endif()
109     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
110       set(SPIRV_USE_SANITIZER "" CACHE STRING
111         "Use the clang sanitizer [address|memory|thread|...]")
112       if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
113         target_compile_options(${TARGET} PRIVATE
114           -fsanitize=${SPIRV_USE_SANITIZER})
115       endif()
116     else()
117       target_compile_options(${TARGET} PRIVATE
118          -Wno-missing-field-initializers)
119     endif()
120   endif()
121
122   # For MinGW cross compile, statically link to the C++ runtime.
123   # But it still depends on MSVCRT.dll.
124   if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
125     if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
126       set_target_properties(${TARGET} PROPERTIES
127         LINK_FLAGS -static -static-libgcc -static-libstdc++)
128     endif()
129   endif()
130 endfunction()
131
132 if(NOT COMMAND find_host_package)
133   macro(find_host_package)
134     find_package(${ARGN})
135   endmacro()
136 endif()
137 if(NOT COMMAND find_host_program)
138   macro(find_host_program)
139     find_program(${ARGN})
140   endmacro()
141 endif()
142
143 find_host_package(PythonInterp)
144
145
146 # Defaults to OFF if the user didn't set it.
147 option(SPIRV_SKIP_EXECUTABLES
148   "Skip building the executable and tests along with the library"
149   ${SPIRV_SKIP_EXECUTABLES})
150 option(SPIRV_SKIP_TESTS
151   "Skip building tests along with the library" ${SPIRV_SKIP_TESTS})
152 if ("${SPIRV_SKIP_EXECUTABLES}")
153   set(SPIRV_SKIP_TESTS ON)
154 endif()
155
156 add_subdirectory(external)
157
158 add_subdirectory(source)
159 add_subdirectory(tools)
160
161 add_subdirectory(test)
162
163 install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
164   DESTINATION include/spirv-tools/)