Update cmake files to support Android as a build target.
[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 project(spirv-tools)
29 enable_testing()
30 set(SPIRV_TOOLS "SPIRV-Tools")
31
32 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
33
34 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
35   add_definitions(-DSPIRV_LINUX)
36 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
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 else()
43   message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
44 endif()
45
46 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
47   message(STATUS "No build type selected, default to Debug")
48   set(CMAKE_BUILD_TYPE "Debug")
49 endif()
50
51 option(SPIRV_WERROR "Enable error on warning" ON)
52 if((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") OR (${CMAKE_CXX_COMPILER_ID} MATCHES "Clang"))
53   set(COMPILER_IS_LIKE_GNU TRUE)
54 endif()
55 if(${COMPILER_IS_LIKE_GNU})
56   set(SPIRV_WARNINGS -Wall -Wextra -Wno-missing-field-initializers)
57
58   option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
59   if(${SPIRV_WARN_EVERYTHING})
60     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
61       set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
62         -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
63     elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
64       set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
65     else()
66       message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
67                      "so SPIRV_WARN_EVERYTHING has no effect")
68     endif()
69   endif()
70
71   if(${SPIRV_WERROR})
72     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
73   endif()
74 elseif(MSVC)
75   set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
76
77   if(${SPIRV_WERROR})
78     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
79   endif()
80 endif()
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 function(spvtools_default_compile_options TARGET)
88   target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
89
90   if (${COMPILER_IS_LIKE_GNU})
91     target_compile_options(${TARGET} PRIVATE
92       -std=c++11 -fno-exceptions -fno-rtti)
93     target_compile_options(${TARGET} PRIVATE
94       -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
95       -Wno-sign-conversion)
96     # For good call stacks in profiles, keep the frame pointers.
97     if(NOT "${SPIRV_PERF}" STREQUAL "")
98       target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
99     endif()
100     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
101       set(SPIRV_USE_SANITIZER "" CACHE STRING
102         "Use the clang sanitizer [address|memory|thread|...]")
103       if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
104         target_compile_options(${TARGET} PRIVATE
105           -fsanitize=${SPIRV_USE_SANITIZER})
106       endif()
107     else()
108       target_compile_options(${TARGET} PRIVATE
109          -Wno-missing-field-initializers)
110     endif()
111   endif()
112
113   # For MinGW cross compile, statically link to the C++ runtime.
114   # But it still depends on MSVCRT.dll.
115   if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
116     if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
117       set_target_properties(${TARGET} PROPERTIES LINK_FLAGS
118                             -static -static-libgcc -static-libstdc++)
119     endif()
120   endif()
121 endfunction()
122
123 if(NOT COMMAND find_host_package)
124   macro(find_host_package)
125     find_package(${ARGN})
126   endmacro()
127 endif()
128 if(NOT COMMAND find_host_program)
129   macro(find_host_program)
130     find_program(${ARGN})
131   endmacro()
132 endif()
133
134 find_host_package(PythonInterp)
135
136 add_custom_target(spirv-tools-build-version
137   ${PYTHON_EXECUTABLE}
138   ${CMAKE_CURRENT_SOURCE_DIR}/utils/update_build_version.py
139   ${spirv-tools_SOURCE_DIR}
140   COMMENT "Update build-version.inc in the Spirv-tools build directory (if necessary).")
141
142 function(spvtools_core_tables VERSION)
143   add_custom_target(spirv-tools-build-core-tables-${VERSION}
144     ${PYTHON_EXECUTABLE}
145     ${CMAKE_CURRENT_SOURCE_DIR}/utils/generate_grammar_tables.py
146     --spirv-core-grammar=${spirv-tools_SOURCE_DIR}/source/spirv-${VERSION}.core.grammar.json
147     --core-insts-output=${spirv-tools_BINARY_DIR}/core.insts-${VERSION}.inc
148     --operand-kinds-output=${spirv-tools_BINARY_DIR}/operand.kinds-${VERSION}.inc
149     COMMENT "Generate info tables for SPIR-V v${VERSION} core instructions and operands.")
150 endfunction(spvtools_core_tables)
151
152 function(spvtools_glsl_tables VERSION)
153   add_custom_target(spirv-tools-build-glsl-tables-${VERSION}
154     ${PYTHON_EXECUTABLE}
155     ${CMAKE_CURRENT_SOURCE_DIR}/utils/generate_grammar_tables.py
156     --spirv-core-grammar=${spirv-tools_SOURCE_DIR}/source/spirv-${VERSION}.core.grammar.json
157     --extinst-glsl-grammar=${spirv-tools_SOURCE_DIR}/source/extinst-${VERSION}.glsl.std.450.grammar.json
158     --glsl-insts-output=${spirv-tools_BINARY_DIR}/glsl.std.450.insts-${VERSION}.inc
159     COMMENT "Generate info tables for GLSL extended instructions and operands v${VERSION}.")
160 endfunction(spvtools_glsl_tables)
161
162 function(spvtools_opencl_tables VERSION)
163   add_custom_target(spirv-tools-build-opencl-tables-${VERSION}
164     ${PYTHON_EXECUTABLE}
165     ${CMAKE_CURRENT_SOURCE_DIR}/utils/generate_grammar_tables.py
166     --spirv-core-grammar=${spirv-tools_SOURCE_DIR}/source/spirv-${VERSION}.core.grammar.json
167     --extinst-opencl-grammar=${spirv-tools_SOURCE_DIR}/source/extinst-${VERSION}.opencl.std.grammar.json
168     --opencl-insts-output=${spirv-tools_BINARY_DIR}/opencl.std.insts-${VERSION}.inc
169     COMMENT "Generate info tables for OpenCL extended instructions and operands v${VERSION}.")
170 endfunction(spvtools_opencl_tables)
171
172 spvtools_core_tables("1-0")
173 spvtools_core_tables("1-1")
174 spvtools_opencl_tables("1-0")
175 spvtools_glsl_tables("1-0")
176
177 # Defaults to OFF if the user didn't set it.
178 option(SPIRV_SKIP_EXECUTABLES
179        "Skip building the executable and tests along with the library"
180        ${SPIRV_SKIP_EXECUTABLES})
181
182 add_subdirectory(external)
183
184 add_subdirectory(source)
185 add_subdirectory(tools)
186
187 add_subdirectory(test)
188
189 install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
190   DESTINATION include/spirv-tools/)