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