Use the JSON grammar file to generate various info tables.
[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 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 "Darwin")
37   add_definitions(-DSPIRV_MAC)
38 else()
39   message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
40 endif()
41
42 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
43   message(STATUS "No build type selected, default to Debug")
44   set(CMAKE_BUILD_TYPE "Debug")
45 endif()
46
47 option(SPIRV_WERROR "Enable error on warning" ON)
48 if((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") OR (${CMAKE_CXX_COMPILER_ID} MATCHES "Clang"))
49   set(COMPILER_IS_LIKE_GNU TRUE)
50 endif()
51 if(${COMPILER_IS_LIKE_GNU})
52   set(SPIRV_WARNINGS -Wall -Wextra -Wno-missing-field-initializers)
53
54   option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
55   if(${SPIRV_WARN_EVERYTHING})
56     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
57       set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
58         -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
59     elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
60       set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
61     else()
62       message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
63                      "so SPIRV_WARN_EVERYTHING has no effect")
64     endif()
65   endif()
66
67   if(${SPIRV_WERROR})
68     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
69   endif()
70 elseif(MSVC)
71   set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
72
73   if(${SPIRV_WERROR})
74     set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
75   endif()
76 endif()
77
78 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
79 if(${SPIRV_COLOR_TERMINAL})
80   add_definitions(-DSPIRV_COLOR_TERMINAL)
81 endif()
82
83 function(spvtools_default_compile_options TARGET)
84   target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
85
86   if (${COMPILER_IS_LIKE_GNU})
87     target_compile_options(${TARGET} PRIVATE
88       -std=c++11 -fno-exceptions -fno-rtti)
89     target_compile_options(${TARGET} PRIVATE
90       -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
91       -Wno-sign-conversion)
92     # For good call stacks in profiles, keep the frame pointers.
93     if(NOT "${SPIRV_PERF}" STREQUAL "")
94       target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
95     endif()
96     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
97       set(SPIRV_USE_SANITIZER "" CACHE STRING
98         "Use the clang sanitizer [address|memory|thread|...]")
99       if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
100         target_compile_options(${TARGET} PRIVATE
101           -fsanitize=${SPIRV_USE_SANITIZER})
102       endif()
103     else()
104       target_compile_options(${TARGET} PRIVATE
105          -Wno-missing-field-initializers)
106     endif()
107   endif()
108
109   # For MinGW cross compile, statically link to the C++ runtime.
110   # But it still depends on MSVCRT.dll.
111   if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
112     if (${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
113       set_target_properties(${TARGET} PROPERTIES LINK_FLAGS
114                             -static -static-libgcc -static-libstdc++)
115     endif()
116   endif()
117 endfunction()
118
119 if(NOT COMMAND find_host_package)
120   macro(find_host_package)
121     find_package(${ARGN})
122   endmacro()
123 endif()
124 if(NOT COMMAND find_host_program)
125   macro(find_host_program)
126     find_program(${ARGN})
127   endmacro()
128 endif()
129
130 find_host_package(PythonInterp)
131
132 add_custom_target(spirv-tools-build-version
133   ${PYTHON_EXECUTABLE}
134   ${CMAKE_CURRENT_SOURCE_DIR}/utils/update_build_version.py
135   ${spirv-tools_SOURCE_DIR}
136   COMMENT "Update build-version.inc in the Spirv-tools build directory (if necessary).")
137
138 add_custom_target(spirv-tools-build-tables
139   ${PYTHON_EXECUTABLE}
140   ${CMAKE_CURRENT_SOURCE_DIR}/utils/generate_grammar_tables.py
141   ${spirv-tools_SOURCE_DIR}/source/grammar.json
142   --opcode-file=${spirv-tools_BINARY_DIR}/opcode.inc
143   --operand-file=${spirv-tools_BINARY_DIR}/operand.inc
144   COMMENT "Generate info tables for SPIR-V instructions and operands.")
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
151 add_subdirectory(external)
152
153 add_subdirectory(source)
154 add_subdirectory(tools)
155
156 add_subdirectory(test)
157
158 install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
159   DESTINATION include/spirv-tools/)
160 # The installation is broken without these header files from the SPIR-V Registry.
161 # The libspirv.h header includes them.
162 install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv/spirv.h
163               ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv/GLSL.std.450.h
164               ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv/OpenCL.std.h
165   DESTINATION include/spirv/)