1 # Copyright (c) 2015-2016 The Khronos Group Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 set(GRAMMAR_PROCESSING_SCRIPT "${spirv-tools_SOURCE_DIR}/utils/generate_grammar_tables.py")
16 set(VIMSYNTAX_PROCESSING_SCRIPT "${spirv-tools_SOURCE_DIR}/utils/generate_vim_syntax.py")
17 set(XML_REGISTRY_PROCESSING_SCRIPT "${spirv-tools_SOURCE_DIR}/utils/generate_registry_tables.py")
19 # macro() definitions are used in the following because we need to append .inc
20 # file paths into some global lists (*_CPP_DEPENDS). And those global lists are
21 # later used by set_source_files_properties() calls.
22 # function() definitions are not suitable because they create new scopes.
23 macro(spvtools_core_tables VERSION)
24 set(GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/spirv.core.grammar.json")
25 set(GRAMMAR_INSTS_INC_FILE "${spirv-tools_BINARY_DIR}/core.insts-${VERSION}.inc")
26 set(GRAMMAR_KINDS_INC_FILE "${spirv-tools_BINARY_DIR}/operand.kinds-${VERSION}.inc")
27 add_custom_command(OUTPUT ${GRAMMAR_INSTS_INC_FILE} ${GRAMMAR_KINDS_INC_FILE}
28 COMMAND ${PYTHON_EXECUTABLE} ${GRAMMAR_PROCESSING_SCRIPT}
29 --spirv-core-grammar=${GRAMMAR_JSON_FILE}
30 --core-insts-output=${GRAMMAR_INSTS_INC_FILE}
31 --operand-kinds-output=${GRAMMAR_KINDS_INC_FILE}
32 DEPENDS ${GRAMMAR_PROCESSING_SCRIPT} ${GRAMMAR_JSON_FILE}
33 COMMENT "Generate info tables for SPIR-V v${VERSION} core instructions and operands.")
34 list(APPEND OPCODE_CPP_DEPENDS ${GRAMMAR_INSTS_INC_FILE})
35 list(APPEND OPERAND_CPP_DEPENDS ${GRAMMAR_KINDS_INC_FILE})
36 endmacro(spvtools_core_tables)
38 macro(spvtools_enum_string_mapping VERSION)
39 set(GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/spirv.core.grammar.json")
40 set(GRAMMAR_EXTENSION_ENUM_INC_FILE "${spirv-tools_BINARY_DIR}/extension_enum.inc")
41 set(GRAMMAR_ENUM_STRING_MAPPING_INC_FILE "${spirv-tools_BINARY_DIR}/enum_string_mapping.inc")
42 add_custom_command(OUTPUT ${GRAMMAR_EXTENSION_ENUM_INC_FILE}
43 ${GRAMMAR_ENUM_STRING_MAPPING_INC_FILE}
44 COMMAND ${PYTHON_EXECUTABLE} ${GRAMMAR_PROCESSING_SCRIPT}
45 --spirv-core-grammar=${GRAMMAR_JSON_FILE}
46 --extension-enum-output=${GRAMMAR_EXTENSION_ENUM_INC_FILE}
47 --enum-string-mapping-output=${GRAMMAR_ENUM_STRING_MAPPING_INC_FILE}
48 DEPENDS ${GRAMMAR_PROCESSING_SCRIPT} ${GRAMMAR_JSON_FILE}
49 COMMENT "Generate enum-string mapping for SPIR-V v${VERSION}.")
50 list(APPEND EXTENSION_H_DEPENDS ${GRAMMAR_EXTENSION_ENUM_INC_FILE})
51 list(APPEND ENUM_STRING_MAPPING_CPP_DEPENDS ${GRAMMAR_ENUM_STRING_MAPPING_INC_FILE})
52 endmacro(spvtools_enum_string_mapping)
54 macro(spvtools_vimsyntax VERSION CLVERSION)
55 set(GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/spirv.core.grammar.json")
56 set(GLSL_GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/extinst.glsl.std.450.grammar.json")
57 set(OPENCL_GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/extinst.opencl.std.100.grammar.json")
58 set(VIMSYNTAX_FILE "${spirv-tools_BINARY_DIR}/spvasm.vim")
59 add_custom_command(OUTPUT ${VIMSYNTAX_FILE}
60 COMMAND ${PYTHON_EXECUTABLE} ${VIMSYNTAX_PROCESSING_SCRIPT}
61 --spirv-core-grammar=${GRAMMAR_JSON_FILE}
62 --extinst-glsl-grammar=${GLSL_GRAMMAR_JSON_FILE}
63 --extinst-opencl-grammar=${OPENCL_GRAMMAR_JSON_FILE}
65 DEPENDS ${VIMSYNTAX_PROCESSING_SCRIPT} ${GRAMMAR_JSON_FILE}
66 ${GLSL_GRAMMAR_JSON_FILE} ${OPENCL_GRAMMAR_JSON_FILE}
67 COMMENT "Generate spvasm.vim: Vim syntax file for SPIR-V assembly.")
68 endmacro(spvtools_vimsyntax)
70 macro(spvtools_glsl_tables VERSION)
71 set(CORE_GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/spirv.core.grammar.json")
72 set(GLSL_GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/extinst.glsl.std.450.grammar.json")
73 set(GRAMMAR_INC_FILE "${spirv-tools_BINARY_DIR}/glsl.std.450.insts-${VERSION}.inc")
74 add_custom_command(OUTPUT ${GRAMMAR_INC_FILE}
75 COMMAND ${PYTHON_EXECUTABLE} ${GRAMMAR_PROCESSING_SCRIPT}
76 --spirv-core-grammar=${CORE_GRAMMAR_JSON_FILE}
77 --extinst-glsl-grammar=${GLSL_GRAMMAR_JSON_FILE}
78 --glsl-insts-output=${GRAMMAR_INC_FILE}
79 DEPENDS ${GRAMMAR_PROCESSING_SCRIPT} ${CORE_GRAMMAR_JSON_FILE} ${GLSL_GRAMMAR_JSON_FILE}
80 COMMENT "Generate info tables for GLSL extended instructions and operands v${VERSION}.")
81 list(APPEND EXTINST_CPP_DEPENDS ${GRAMMAR_INC_FILE})
82 endmacro(spvtools_glsl_tables)
84 macro(spvtools_opencl_tables VERSION)
85 set(CORE_GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/spirv.core.grammar.json")
86 set(OPENCL_GRAMMAR_JSON_FILE "${SPIRV_HEADER_INCLUDE_DIR}/spirv/${VERSION}/extinst.opencl.std.100.grammar.json")
87 set(GRAMMAR_INC_FILE "${spirv-tools_BINARY_DIR}/opencl.std.insts-${VERSION}.inc")
88 add_custom_command(OUTPUT ${GRAMMAR_INC_FILE}
89 COMMAND ${PYTHON_EXECUTABLE} ${GRAMMAR_PROCESSING_SCRIPT}
90 --spirv-core-grammar=${CORE_GRAMMAR_JSON_FILE}
91 --extinst-opencl-grammar=${OPENCL_GRAMMAR_JSON_FILE}
92 --opencl-insts-output=${GRAMMAR_INC_FILE}
93 DEPENDS ${GRAMMAR_PROCESSING_SCRIPT} ${CORE_GRAMMAR_JSON_FILE} ${OPENCL_GRAMMAR_JSON_FILE}
94 COMMENT "Generate info tables for OpenCL extended instructions and operands v${VERSION}.")
95 list(APPEND EXTINST_CPP_DEPENDS ${GRAMMAR_INC_FILE})
96 endmacro(spvtools_opencl_tables)
98 spvtools_core_tables("1.0")
99 spvtools_core_tables("1.1")
100 spvtools_enum_string_mapping("1.1")
101 spvtools_opencl_tables("1.0")
102 spvtools_glsl_tables("1.0")
104 spvtools_vimsyntax("1.1" "1.0")
105 add_custom_target(spirv-tools-vimsyntax DEPENDS ${VIMSYNTAX_FILE})
108 # Extract the list of known generators from the SPIR-V XML registry file.
109 set(GENERATOR_INC_FILE ${spirv-tools_BINARY_DIR}/generators.inc)
110 set(SPIRV_XML_REGISTRY_FILE ${SPIRV_HEADER_INCLUDE_DIR}/spirv/spir-v.xml)
111 add_custom_command(OUTPUT ${GENERATOR_INC_FILE}
112 COMMAND ${PYTHON_EXECUTABLE} ${XML_REGISTRY_PROCESSING_SCRIPT}
113 --xml=${SPIRV_XML_REGISTRY_FILE}
114 --generator-output=${GENERATOR_INC_FILE}
115 DEPENDS ${XML_REGISTRY_PROCESSING_SCRIPT} ${SPIRV_XML_REGISTRY_FILE}
116 COMMENT "Generate tables based on the SPIR-V XML registry.")
117 list(APPEND OPCODE_CPP_DEPENDS ${GENERATOR_INC_FILE})
119 # The following .cpp files include the above generated .inc files.
120 # Add those .inc files as their dependencies.
122 # Why using such an awkward way?
123 # * If we use add_custom_target() to define a target to generate all .inc files
124 # and let ${SPIRV_TOOLS} depend on it, then we need to run ninja twice every
125 # time the grammar is updated: the first time is for generating those .inc
126 # files, and the second time is for rebuilding .cpp files, when ninja finds
127 # out that .inc files are updated.
128 # * If we use add_custom_command() with PRE_BUILD, then the grammar processing
129 # script will always run no matter whether the grammar is updated.
130 # * add_dependencies() is used to add *target* dependencies to a target.
131 # * The following solution only generates .inc files when the script or the
132 # grammar files is updated, and in a single ninja run.
133 set_source_files_properties(
134 ${CMAKE_CURRENT_SOURCE_DIR}/opcode.cpp
135 PROPERTIES OBJECT_DEPENDS "${OPCODE_CPP_DEPENDS}")
136 set_source_files_properties(
137 ${CMAKE_CURRENT_SOURCE_DIR}/operand.cpp
138 PROPERTIES OBJECT_DEPENDS "${OPERAND_CPP_DEPENDS}")
139 set_source_files_properties(
140 ${CMAKE_CURRENT_SOURCE_DIR}/ext_inst.cpp
141 PROPERTIES OBJECT_DEPENDS "${EXTINST_CPP_DEPENDS}")
142 set_source_files_properties(
143 ${CMAKE_CURRENT_SOURCE_DIR}/enum_string_mapping.cpp
144 PROPERTIES OBJECT_DEPENDS "${ENUM_STRING_MAPPING_CPP_DEPENDS}")
146 set_source_files_properties(
147 ${CMAKE_CURRENT_SOURCE_DIR}/extension.h
148 PROPERTIES HEADER_FILE_ONLY TRUE)
149 set_source_files_properties(
150 ${CMAKE_CURRENT_SOURCE_DIR}/extension.h
151 PROPERTIES OBJECT_DEPENDS "${EXTENSION_H_DEPENDS}")
153 set(SPIRV_TOOLS_BUILD_VERSION_INC
154 ${spirv-tools_BINARY_DIR}/build-version.inc)
155 set(SPIRV_TOOLS_BUILD_VERSION_INC_GENERATOR
156 ${spirv-tools_SOURCE_DIR}/utils/update_build_version.py)
157 set(SPIRV_TOOLS_CHANGES_FILE
158 ${spirv-tools_SOURCE_DIR}/CHANGES)
159 add_custom_command(OUTPUT ${SPIRV_TOOLS_BUILD_VERSION_INC}
160 COMMAND ${PYTHON_EXECUTABLE}
161 ${SPIRV_TOOLS_BUILD_VERSION_INC_GENERATOR}
162 ${spirv-tools_SOURCE_DIR} ${SPIRV_TOOLS_BUILD_VERSION_INC}
163 DEPENDS ${SPIRV_TOOLS_BUILD_VERSION_INC_GENERATOR}
164 ${SPIRV_TOOLS_CHANGES_FILE}
165 COMMENT "Update build-version.inc in the SPIRV-Tools build directory (if necessary).")
166 # Convenience target for standalone generation of the build-version.inc file.
167 # This is not required for any dependence chain.
168 add_custom_target(spirv-tools-build-version
169 DEPENDS ${SPIRV_TOOLS_BUILD_VERSION_INC})
171 add_subdirectory(opt)
174 ${spirv-tools_SOURCE_DIR}/include/spirv-tools/libspirv.h
176 ${CMAKE_CURRENT_SOURCE_DIR}/util/bitutils.h
177 ${CMAKE_CURRENT_SOURCE_DIR}/util/hex_float.h
178 ${CMAKE_CURRENT_SOURCE_DIR}/util/parse_number.h
179 ${CMAKE_CURRENT_SOURCE_DIR}/util/string_utils.h
180 ${CMAKE_CURRENT_SOURCE_DIR}/assembly_grammar.h
181 ${CMAKE_CURRENT_SOURCE_DIR}/binary.h
182 ${CMAKE_CURRENT_SOURCE_DIR}/diagnostic.h
183 ${CMAKE_CURRENT_SOURCE_DIR}/enum_set.h
184 ${CMAKE_CURRENT_SOURCE_DIR}/enum_string_mapping.h
185 ${CMAKE_CURRENT_SOURCE_DIR}/ext_inst.h
186 ${CMAKE_CURRENT_SOURCE_DIR}/extensions.h
187 ${CMAKE_CURRENT_SOURCE_DIR}/instruction.h
188 ${CMAKE_CURRENT_SOURCE_DIR}/macro.h
189 ${CMAKE_CURRENT_SOURCE_DIR}/name_mapper.h
190 ${CMAKE_CURRENT_SOURCE_DIR}/opcode.h
191 ${CMAKE_CURRENT_SOURCE_DIR}/operand.h
192 ${CMAKE_CURRENT_SOURCE_DIR}/parsed_operand.h
193 ${CMAKE_CURRENT_SOURCE_DIR}/print.h
194 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_constant.h
195 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_definition.h
196 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_endian.h
197 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_target_env.h
198 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_validator_options.h
199 ${CMAKE_CURRENT_SOURCE_DIR}/table.h
200 ${CMAKE_CURRENT_SOURCE_DIR}/text.h
201 ${CMAKE_CURRENT_SOURCE_DIR}/text_handler.h
202 ${CMAKE_CURRENT_SOURCE_DIR}/validate.h
204 ${CMAKE_CURRENT_SOURCE_DIR}/util/parse_number.cpp
205 ${CMAKE_CURRENT_SOURCE_DIR}/util/string_utils.cpp
206 ${CMAKE_CURRENT_SOURCE_DIR}/assembly_grammar.cpp
207 ${CMAKE_CURRENT_SOURCE_DIR}/binary.cpp
208 ${CMAKE_CURRENT_SOURCE_DIR}/diagnostic.cpp
209 ${CMAKE_CURRENT_SOURCE_DIR}/disassemble.cpp
210 ${CMAKE_CURRENT_SOURCE_DIR}/enum_string_mapping.cpp
211 ${CMAKE_CURRENT_SOURCE_DIR}/ext_inst.cpp
212 ${CMAKE_CURRENT_SOURCE_DIR}/extensions.cpp
213 ${CMAKE_CURRENT_SOURCE_DIR}/libspirv.cpp
214 ${CMAKE_CURRENT_SOURCE_DIR}/message.cpp
215 ${CMAKE_CURRENT_SOURCE_DIR}/name_mapper.cpp
216 ${CMAKE_CURRENT_SOURCE_DIR}/opcode.cpp
217 ${CMAKE_CURRENT_SOURCE_DIR}/operand.cpp
218 ${CMAKE_CURRENT_SOURCE_DIR}/parsed_operand.cpp
219 ${CMAKE_CURRENT_SOURCE_DIR}/print.cpp
220 ${CMAKE_CURRENT_SOURCE_DIR}/software_version.cpp
221 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_endian.cpp
222 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_target_env.cpp
223 ${CMAKE_CURRENT_SOURCE_DIR}/spirv_validator_options.cpp
224 ${CMAKE_CURRENT_SOURCE_DIR}/table.cpp
225 ${CMAKE_CURRENT_SOURCE_DIR}/text.cpp
226 ${CMAKE_CURRENT_SOURCE_DIR}/text_handler.cpp
227 ${CMAKE_CURRENT_SOURCE_DIR}/validate.cpp
228 ${CMAKE_CURRENT_SOURCE_DIR}/validate_cfg.cpp
229 ${CMAKE_CURRENT_SOURCE_DIR}/validate_id.cpp
230 ${CMAKE_CURRENT_SOURCE_DIR}/validate_instruction.cpp
231 ${CMAKE_CURRENT_SOURCE_DIR}/validate_datarules.cpp
232 ${CMAKE_CURRENT_SOURCE_DIR}/validate_decorations.cpp
233 ${CMAKE_CURRENT_SOURCE_DIR}/validate_layout.cpp
234 ${CMAKE_CURRENT_SOURCE_DIR}/validate_type_unique.cpp
235 ${CMAKE_CURRENT_SOURCE_DIR}/val/decoration.h
236 ${CMAKE_CURRENT_SOURCE_DIR}/val/basic_block.cpp
237 ${CMAKE_CURRENT_SOURCE_DIR}/val/construct.cpp
238 ${CMAKE_CURRENT_SOURCE_DIR}/val/function.cpp
239 ${CMAKE_CURRENT_SOURCE_DIR}/val/instruction.cpp
240 ${CMAKE_CURRENT_SOURCE_DIR}/val/validation_state.cpp)
242 # The software_version.cpp file includes build-version.inc.
243 # Rebuild the software_version.cpp object file if it is older than
244 # build-version.inc or whenever build-version.inc itself is out of
245 # date. In the latter case, rebuild build-version.inc first.
246 # CMake is not smart enough to detect this dependency automatically.
247 # Without this, the dependency detection system for #included files
248 # does not kick in on a clean build for the following reason: The
249 # build will fail early because it doesn't know how to build the
250 # missing source file build-version.inc. That occurs before the
251 # preprocessor is run on software_version.cpp to detect the
252 # #include dependency.
253 set_source_files_properties(
254 ${CMAKE_CURRENT_SOURCE_DIR}/software_version.cpp
255 PROPERTIES OBJECT_DEPENDS "${SPIRV_TOOLS_BUILD_VERSION_INC}")
257 add_library(${SPIRV_TOOLS} ${SPIRV_SOURCES})
258 spvtools_default_compile_options(${SPIRV_TOOLS})
259 target_include_directories(${SPIRV_TOOLS}
260 PUBLIC ${spirv-tools_SOURCE_DIR}/include
261 PRIVATE ${spirv-tools_BINARY_DIR}
262 PRIVATE ${SPIRV_HEADER_INCLUDE_DIR}
265 install(TARGETS ${SPIRV_TOOLS}
266 RUNTIME DESTINATION bin
267 LIBRARY DESTINATION lib
268 ARCHIVE DESTINATION lib)