1 # Copyright (c) 2015-2016 The Khronos Group Inc.
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:
11 # The above copyright notice and this permission notice shall be included
12 # in all copies or substantial portions of the Materials.
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/
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.
27 set(GRAMMAR_PROCESSING_SCRIPT "${spirv-tools_SOURCE_DIR}/utils/generate_grammar_tables.py")
29 # macro() definitions are used in the following because we need to append .inc
30 # file paths into some global lists (*_CPP_DEPENDS). And those global lists are
31 # later used by set_source_files_properties() calls.
32 # function() definitions are not suitable because they create new scopes.
33 macro(spvtools_core_tables VERSION)
34 set(GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/spirv.core.grammar.json")
35 set(GRAMMAR_INSTS_INC_FILE "${spirv-tools_BINARY_DIR}/core.insts-${VERSION}.inc")
36 set(GRAMMAR_KINDS_INC_FILE "${spirv-tools_BINARY_DIR}/operand.kinds-${VERSION}.inc")
37 add_custom_command(OUTPUT ${GRAMMAR_INSTS_INC_FILE} ${GRAMMAR_KINDS_INC_FILE}
38 COMMAND ${PYTHON_EXECUTABLE} ${GRAMMAR_PROCESSING_SCRIPT}
39 --spirv-core-grammar=${GRAMMAR_JSON_FILE}
40 --core-insts-output=${GRAMMAR_INSTS_INC_FILE}
41 --operand-kinds-output=${GRAMMAR_KINDS_INC_FILE}
42 DEPENDS ${GRAMMAR_PROCESSING_SCRIPT} ${GRAMMAR_JSON_FILE}
43 COMMENT "Generate info tables for SPIR-V v${VERSION} core instructions and operands.")
44 list(APPEND OPCODE_CPP_DEPENDS ${GRAMMAR_INSTS_INC_FILE})
45 list(APPEND OPERAND_CPP_DEPENDS ${GRAMMAR_KINDS_INC_FILE})
46 endmacro(spvtools_core_tables)
48 macro(spvtools_glsl_tables VERSION)
49 set(CORE_GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/spirv.core.grammar.json")
50 set(GLSL_GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/extinst.glsl.std.450.grammar.json")
51 set(GRAMMAR_INC_FILE "${spirv-tools_BINARY_DIR}/glsl.std.450.insts-${VERSION}.inc")
52 add_custom_command(OUTPUT ${GRAMMAR_INC_FILE}
53 COMMAND ${PYTHON_EXECUTABLE} ${GRAMMAR_PROCESSING_SCRIPT}
54 --spirv-core-grammar=${CORE_GRAMMAR_JSON_FILE}
55 --extinst-glsl-grammar=${GLSL_GRAMMAR_JSON_FILE}
56 --glsl-insts-output=${GRAMMAR_INC_FILE}
57 DEPENDS ${GRAMMAR_PROCESSING_SCRIPT} ${CORE_GRAMMAR_JSON_FILE} ${GLSL_GRAMMAR_JSON_FILE}
58 COMMENT "Generate info tables for GLSL extended instructions and operands v${VERSION}.")
59 list(APPEND EXTINST_CPP_DEPENDS ${GRAMMAR_INC_FILE})
60 endmacro(spvtools_glsl_tables)
62 macro(spvtools_opencl_tables VERSION)
63 set(CORE_GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/spirv.core.grammar.json")
64 set(OPENCL_GRAMMAR_JSON_FILE "${CMAKE_CURRENT_SOURCE_DIR}/extinst-${VERSION}.opencl.std.grammar.json")
65 set(GRAMMAR_INC_FILE "${spirv-tools_BINARY_DIR}/opencl.std.insts-${VERSION}.inc")
66 add_custom_command(OUTPUT ${GRAMMAR_INC_FILE}
67 COMMAND ${PYTHON_EXECUTABLE} ${GRAMMAR_PROCESSING_SCRIPT}
68 --spirv-core-grammar=${CORE_GRAMMAR_JSON_FILE}
69 --extinst-opencl-grammar=${OPENCL_GRAMMAR_JSON_FILE}
70 --opencl-insts-output=${GRAMMAR_INC_FILE}
71 DEPENDS ${GRAMMAR_PROCESSING_SCRIPT} ${CORE_GRAMMAR_JSON_FILE} ${OPENCL_GRAMMAR_JSON_FILE}
72 COMMENT "Generate info tables for OpenCL extended instructions and operands v${VERSION}.")
73 list(APPEND EXTINST_CPP_DEPENDS ${GRAMMAR_INC_FILE})
74 endmacro(spvtools_opencl_tables)
76 spvtools_core_tables("1.0")
77 spvtools_core_tables("1.1")
78 spvtools_opencl_tables("1.0")
79 spvtools_glsl_tables("1.0")
81 # The following .cpp files include the above generated .inc files.
82 # Add those .inc files as their dependencies.
84 # Why using such an awkward way?
85 # * If we use add_custom_target() to define a target to generate all .inc files
86 # and let ${SPIRV_TOOLS} depend on it, then we need to run ninja twice every
87 # time the grammar is updated: the first time is for generating those .inc
88 # files, and the second time is for rebuilding .cpp files, when ninja finds
89 # out that .inc files are updated.
90 # * If we use add_custom_command() with PRE_BUILD, then the grammar processing
91 # script will always run no matter whether the grammar is updated.
92 # * add_dependencies() is used to add *target* dependencies to a target.
93 # * The following solution only generates .inc files when the script or the
94 # grammar files is updated, and in a single ninja run.
95 set_source_files_properties(
96 ${CMAKE_CURRENT_SOURCE_DIR}/opcode.cpp
97 PROPERTIES OBJECT_DEPENDS "${OPCODE_CPP_DEPENDS}")
98 set_source_files_properties(
99 ${CMAKE_CURRENT_SOURCE_DIR}/operand.cpp
100 PROPERTIES OBJECT_DEPENDS "${OPERAND_CPP_DEPENDS}")
101 set_source_files_properties(
102 ${CMAKE_CURRENT_SOURCE_DIR}/ext_inst.cpp
103 PROPERTIES OBJECT_DEPENDS "${EXTINST_CPP_DEPENDS}")
105 set(SPIRV_TOOLS_BUILD_VERSION_INC
106 ${spirv-tools_BINARY_DIR}/build-version.inc)
107 set(SPIRV_TOOLS_BUILD_VERSION_INC_GENERATOR
108 ${spirv-tools_SOURCE_DIR}/utils/update_build_version.py)
109 set(SPIRV_TOOLS_CHANGES_FILE
110 ${spirv-tools_SOURCE_DIR}/CHANGES)
111 add_custom_command(OUTPUT ${SPIRV_TOOLS_BUILD_VERSION_INC}
112 COMMAND ${PYTHON_EXECUTABLE}
113 ${SPIRV_TOOLS_BUILD_VERSION_INC_GENERATOR}
114 ${spirv-tools_SOURCE_DIR} ${SPIRV_TOOLS_BUILD_VERSION_INC}
115 DEPENDS ${SPIRV_TOOLS_BUILD_VERSION_INC_GENERATOR}
116 ${SPIRV_TOOLS_CHANGES_FILE}
117 COMMENT "Update build-version.inc in the SPIRV-Tools build directory (if necessary).")
118 # Convenience target for standalone generation of the build-version.inc file.
119 # This is not required for any dependence chain.
120 add_custom_target(spirv-tools-build-version
121 DEPENDS ${SPIRV_TOOLS_BUILD_VERSION_INC})
123 add_subdirectory(opt)
126 ${spirv-tools_SOURCE_DIR}/include/spirv-tools/libspirv.h
128 ${CMAKE_CURRENT_SOURCE_DIR}/util/bitutils.h
129 ${CMAKE_CURRENT_SOURCE_DIR}/util/hex_float.h
130 ${CMAKE_CURRENT_SOURCE_DIR}/assembly_grammar.h
131 ${CMAKE_CURRENT_SOURCE_DIR}/binary.h
132 ${CMAKE_CURRENT_SOURCE_DIR}/diagnostic.h
133 ${CMAKE_CURRENT_SOURCE_DIR}/ext_inst.h
134 ${CMAKE_CURRENT_SOURCE_DIR}/instruction.h
135 ${CMAKE_CURRENT_SOURCE_DIR}/macro.h
136 ${CMAKE_CURRENT_SOURCE_DIR}/name_mapper.h
137 ${CMAKE_CURRENT_SOURCE_DIR}/opcode.h
138 ${CMAKE_CURRENT_SOURCE_DIR}/operand.h
139 ${CMAKE_CURRENT_SOURCE_DIR}/print.h
140 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_constant.h
141 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_definition.h
142 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_endian.h
143 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_target_env.h
144 ${CMAKE_CURRENT_SOURCE_DIR}/table.h
145 ${CMAKE_CURRENT_SOURCE_DIR}/text.h
146 ${CMAKE_CURRENT_SOURCE_DIR}/text_handler.h
147 ${CMAKE_CURRENT_SOURCE_DIR}/validate.h
149 ${CMAKE_CURRENT_SOURCE_DIR}/assembly_grammar.cpp
150 ${CMAKE_CURRENT_SOURCE_DIR}/binary.cpp
151 ${CMAKE_CURRENT_SOURCE_DIR}/diagnostic.cpp
152 ${CMAKE_CURRENT_SOURCE_DIR}/disassemble.cpp
153 ${CMAKE_CURRENT_SOURCE_DIR}/ext_inst.cpp
154 ${CMAKE_CURRENT_SOURCE_DIR}/instruction.cpp
155 ${CMAKE_CURRENT_SOURCE_DIR}/name_mapper.cpp
156 ${CMAKE_CURRENT_SOURCE_DIR}/opcode.cpp
157 ${CMAKE_CURRENT_SOURCE_DIR}/operand.cpp
158 ${CMAKE_CURRENT_SOURCE_DIR}/print.cpp
159 ${CMAKE_CURRENT_SOURCE_DIR}/software_version.cpp
160 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_endian.cpp
161 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_target_env.cpp
162 ${CMAKE_CURRENT_SOURCE_DIR}/table.cpp
163 ${CMAKE_CURRENT_SOURCE_DIR}/text.cpp
164 ${CMAKE_CURRENT_SOURCE_DIR}/text_handler.cpp
165 ${CMAKE_CURRENT_SOURCE_DIR}/validate.cpp
166 ${CMAKE_CURRENT_SOURCE_DIR}/validate_cfg.cpp
167 ${CMAKE_CURRENT_SOURCE_DIR}/validate_id.cpp
168 ${CMAKE_CURRENT_SOURCE_DIR}/validate_instruction.cpp
169 ${CMAKE_CURRENT_SOURCE_DIR}/validate_layout.cpp
170 ${CMAKE_CURRENT_SOURCE_DIR}/val/BasicBlock.cpp
171 ${CMAKE_CURRENT_SOURCE_DIR}/val/Construct.cpp
172 ${CMAKE_CURRENT_SOURCE_DIR}/val/Function.cpp
173 ${CMAKE_CURRENT_SOURCE_DIR}/val/Instruction.cpp
174 ${CMAKE_CURRENT_SOURCE_DIR}/val/ValidationState.cpp)
176 # The software_version.cpp file includes build-version.inc.
177 # Rebuild the software_version.cpp object file if it is older than
178 # build-version.inc or whenever build-version.inc itself is out of
179 # date. In the latter case, rebuild build-version.inc first.
180 # CMake is not smart enough to detect this dependency automatically.
181 # Without this, the dependency detection system for #included files
182 # does not kick in on a clean build for the following reason: The
183 # build will fail early because it doesn't know how to build the
184 # missing source file build-version.inc. That occurs before the
185 # preprocessor is run on software_version.cpp to detect the
186 # #include dependency.
187 set_source_files_properties(
188 ${CMAKE_CURRENT_SOURCE_DIR}/software_version.cpp
189 PROPERTIES OBJECT_DEPENDS "${SPIRV_TOOLS_BUILD_VERSION_INC}")
191 add_library(${SPIRV_TOOLS} ${SPIRV_SOURCES})
192 spvtools_default_compile_options(${SPIRV_TOOLS})
193 target_include_directories(${SPIRV_TOOLS}
194 PUBLIC ${spirv-tools_SOURCE_DIR}/include
195 PRIVATE ${spirv-tools_BINARY_DIR}
196 PRIVATE ${SPIRV_HEADER_INCLUDE_DIR}
199 install(TARGETS ${SPIRV_TOOLS}
200 RUNTIME DESTINATION bin
201 LIBRARY DESTINATION lib
202 ARCHIVE DESTINATION lib)