1LOCAL_PATH := $(call my-dir)
2SPVTOOLS_OUT_PATH=$(if $(call host-path-is-absolute,$(TARGET_OUT)),$(TARGET_OUT),$(abspath $(TARGET_OUT)))
3
4ifeq ($(SPVHEADERS_LOCAL_PATH),)
5	SPVHEADERS_LOCAL_PATH := $(LOCAL_PATH)/external/spirv-headers
6endif
7
8SPVTOOLS_SRC_FILES := \
9		source/assembly_grammar.cpp \
10		source/binary.cpp \
11		source/diagnostic.cpp \
12		source/disassemble.cpp \
13		source/ext_inst.cpp \
14		source/enum_string_mapping.cpp \
15		source/extensions.cpp \
16		source/id_descriptor.cpp \
17		source/libspirv.cpp \
18		source/name_mapper.cpp \
19		source/opcode.cpp \
20		source/operand.cpp \
21		source/parsed_operand.cpp \
22		source/print.cpp \
23		source/software_version.cpp \
24		source/spirv_endian.cpp \
25		source/spirv_optimizer_options.cpp \
26		source/spirv_target_env.cpp \
27		source/spirv_validator_options.cpp \
28		source/table.cpp \
29		source/text.cpp \
30		source/text_handler.cpp \
31		source/util/bit_vector.cpp \
32		source/util/parse_number.cpp \
33		source/util/string_utils.cpp \
34		source/util/timer.cpp \
35		source/val/basic_block.cpp \
36		source/val/construct.cpp \
37		source/val/function.cpp \
38		source/val/instruction.cpp \
39		source/val/validation_state.cpp \
40		source/val/validate.cpp \
41		source/val/validate_adjacency.cpp \
42		source/val/validate_annotation.cpp \
43		source/val/validate_arithmetics.cpp \
44		source/val/validate_atomics.cpp \
45		source/val/validate_barriers.cpp \
46		source/val/validate_bitwise.cpp \
47		source/val/validate_builtins.cpp \
48		source/val/validate_capability.cpp \
49		source/val/validate_cfg.cpp \
50		source/val/validate_composites.cpp \
51		source/val/validate_constants.cpp \
52		source/val/validate_conversion.cpp \
53		source/val/validate_datarules.cpp \
54		source/val/validate_debug.cpp \
55		source/val/validate_decorations.cpp \
56		source/val/validate_derivatives.cpp \
57		source/val/validate_extensions.cpp \
58		source/val/validate_execution_limitations.cpp \
59		source/val/validate_function.cpp \
60		source/val/validate_id.cpp \
61		source/val/validate_image.cpp \
62		source/val/validate_interfaces.cpp \
63		source/val/validate_instruction.cpp \
64		source/val/validate_memory.cpp \
65		source/val/validate_memory_semantics.cpp \
66		source/val/validate_mode_setting.cpp \
67		source/val/validate_layout.cpp \
68		source/val/validate_literals.cpp \
69		source/val/validate_logicals.cpp \
70		source/val/validate_non_uniform.cpp \
71		source/val/validate_primitives.cpp \
72		source/val/validate_scopes.cpp \
73		source/val/validate_type.cpp
74
75SPVTOOLS_OPT_SRC_FILES := \
76		source/opt/aggressive_dead_code_elim_pass.cpp \
77		source/opt/basic_block.cpp \
78		source/opt/block_merge_pass.cpp \
79		source/opt/build_module.cpp \
80		source/opt/cfg.cpp \
81		source/opt/cfg_cleanup_pass.cpp \
82		source/opt/ccp_pass.cpp \
83		source/opt/code_sink.cpp \
84		source/opt/combine_access_chains.cpp \
85		source/opt/common_uniform_elim_pass.cpp \
86		source/opt/compact_ids_pass.cpp \
87		source/opt/composite.cpp \
88		source/opt/const_folding_rules.cpp \
89		source/opt/constants.cpp \
90		source/opt/copy_prop_arrays.cpp \
91		source/opt/dead_branch_elim_pass.cpp \
92		source/opt/dead_insert_elim_pass.cpp \
93		source/opt/dead_variable_elimination.cpp \
94		source/opt/decoration_manager.cpp \
95		source/opt/def_use_manager.cpp \
96		source/opt/dominator_analysis.cpp \
97		source/opt/dominator_tree.cpp \
98		source/opt/eliminate_dead_constant_pass.cpp \
99		source/opt/eliminate_dead_functions_pass.cpp \
100		source/opt/feature_manager.cpp \
101		source/opt/flatten_decoration_pass.cpp \
102		source/opt/fold.cpp \
103		source/opt/folding_rules.cpp \
104		source/opt/fold_spec_constant_op_and_composite_pass.cpp \
105		source/opt/freeze_spec_constant_value_pass.cpp \
106		source/opt/function.cpp \
107		source/opt/if_conversion.cpp \
108		source/opt/inline_pass.cpp \
109		source/opt/inline_exhaustive_pass.cpp \
110		source/opt/inline_opaque_pass.cpp \
111		source/opt/inst_bindless_check_pass.cpp \
112		source/opt/instruction.cpp \
113		source/opt/instruction_list.cpp \
114		source/opt/instrument_pass.cpp \
115		source/opt/ir_context.cpp \
116		source/opt/ir_loader.cpp \
117		source/opt/licm_pass.cpp \
118		source/opt/local_access_chain_convert_pass.cpp \
119		source/opt/local_redundancy_elimination.cpp \
120		source/opt/local_single_block_elim_pass.cpp \
121		source/opt/local_single_store_elim_pass.cpp \
122		source/opt/local_ssa_elim_pass.cpp \
123		source/opt/loop_dependence.cpp \
124		source/opt/loop_dependence_helpers.cpp \
125		source/opt/loop_descriptor.cpp \
126		source/opt/loop_fission.cpp \
127		source/opt/loop_fusion.cpp \
128		source/opt/loop_fusion_pass.cpp \
129		source/opt/loop_peeling.cpp \
130		source/opt/loop_unroller.cpp \
131		source/opt/loop_unswitch_pass.cpp \
132		source/opt/loop_utils.cpp \
133		source/opt/mem_pass.cpp \
134		source/opt/merge_return_pass.cpp \
135		source/opt/module.cpp \
136		source/opt/optimizer.cpp \
137		source/opt/pass.cpp \
138		source/opt/pass_manager.cpp \
139		source/opt/private_to_local_pass.cpp \
140		source/opt/process_lines_pass.cpp \
141		source/opt/propagator.cpp \
142		source/opt/reduce_load_size.cpp \
143		source/opt/redundancy_elimination.cpp \
144		source/opt/register_pressure.cpp \
145		source/opt/remove_duplicates_pass.cpp \
146		source/opt/replace_invalid_opc.cpp \
147		source/opt/scalar_analysis.cpp \
148		source/opt/scalar_analysis_simplification.cpp \
149		source/opt/scalar_replacement_pass.cpp \
150		source/opt/set_spec_constant_default_value_pass.cpp \
151		source/opt/simplification_pass.cpp \
152		source/opt/ssa_rewrite_pass.cpp \
153		source/opt/strength_reduction_pass.cpp \
154		source/opt/strip_debug_info_pass.cpp \
155		source/opt/strip_reflect_info_pass.cpp \
156		source/opt/struct_cfg_analysis.cpp \
157		source/opt/type_manager.cpp \
158		source/opt/types.cpp \
159		source/opt/unify_const_pass.cpp \
160		source/opt/upgrade_memory_model.cpp \
161		source/opt/value_number_table.cpp \
162		source/opt/vector_dce.cpp \
163		source/opt/workaround1209.cpp
164
165# Locations of grammar files.
166#
167# TODO(dneto): Build a single set of tables that embeds versioning differences on
168# a per-item basis.  That must happen before SPIR-V 1.4, etc.
169# https://github.com/KhronosGroup/SPIRV-Tools/issues/1195
170SPV_CORE10_GRAMMAR=$(SPVHEADERS_LOCAL_PATH)/include/spirv/1.0/spirv.core.grammar.json
171SPV_CORE11_GRAMMAR=$(SPVHEADERS_LOCAL_PATH)/include/spirv/1.1/spirv.core.grammar.json
172SPV_CORE12_GRAMMAR=$(SPVHEADERS_LOCAL_PATH)/include/spirv/1.2/spirv.core.grammar.json
173SPV_COREUNIFIED1_GRAMMAR=$(SPVHEADERS_LOCAL_PATH)/include/spirv/unified1/spirv.core.grammar.json
174SPV_CORELATEST_GRAMMAR=$(SPV_COREUNIFIED1_GRAMMAR)
175SPV_GLSL_GRAMMAR=$(SPVHEADERS_LOCAL_PATH)/include/spirv/1.2/extinst.glsl.std.450.grammar.json
176SPV_OPENCL_GRAMMAR=$(SPVHEADERS_LOCAL_PATH)/include/spirv/1.2/extinst.opencl.std.100.grammar.json
177# TODO(dneto): I expect the DebugInfo grammar file to eventually migrate to SPIRV-Headers
178SPV_DEBUGINFO_GRAMMAR=$(LOCAL_PATH)/source/extinst.debuginfo.grammar.json
179
180define gen_spvtools_grammar_tables
181$(call generate-file-dir,$(1)/core.insts-1.0.inc)
182$(1)/core.insts-1.0.inc $(1)/operand.kinds-1.0.inc $(1)/glsl.std.450.insts.inc $(1)/opencl.std.insts.inc: \
183        $(LOCAL_PATH)/utils/generate_grammar_tables.py \
184        $(SPV_CORE10_GRAMMAR) \
185        $(SPV_GLSL_GRAMMAR) \
186        $(SPV_OPENCL_GRAMMAR) \
187        $(SPV_DEBUGINFO_GRAMMAR)
188		@$(HOST_PYTHON) $(LOCAL_PATH)/utils/generate_grammar_tables.py \
189		                --spirv-core-grammar=$(SPV_CORE10_GRAMMAR) \
190		                --extinst-glsl-grammar=$(SPV_GLSL_GRAMMAR) \
191		                --extinst-opencl-grammar=$(SPV_OPENCL_GRAMMAR) \
192		                --extinst-debuginfo-grammar=$(SPV_DEBUGINFO_GRAMMAR) \
193		                --core-insts-output=$(1)/core.insts-1.0.inc \
194		                --glsl-insts-output=$(1)/glsl.std.450.insts.inc \
195		                --opencl-insts-output=$(1)/opencl.std.insts.inc \
196		                --operand-kinds-output=$(1)/operand.kinds-1.0.inc
197		@echo "[$(TARGET_ARCH_ABI)] Grammar v1.0   : instructions & operands <= grammar JSON files"
198$(1)/core.insts-1.1.inc $(1)/operand.kinds-1.1.inc: \
199        $(LOCAL_PATH)/utils/generate_grammar_tables.py \
200        $(SPV_CORE11_GRAMMAR) \
201        $(SPV_DEBUGINFO_GRAMMAR)
202		@$(HOST_PYTHON) $(LOCAL_PATH)/utils/generate_grammar_tables.py \
203		                --spirv-core-grammar=$(SPV_CORE11_GRAMMAR) \
204		                --extinst-debuginfo-grammar=$(SPV_DEBUGINFO_GRAMMAR) \
205		                --core-insts-output=$(1)/core.insts-1.1.inc \
206		                --operand-kinds-output=$(1)/operand.kinds-1.1.inc
207		@echo "[$(TARGET_ARCH_ABI)] Grammar v1.1   : instructions & operands <= grammar JSON files"
208$(1)/core.insts-1.2.inc $(1)/operand.kinds-1.2.inc: \
209        $(LOCAL_PATH)/utils/generate_grammar_tables.py \
210        $(SPV_CORE12_GRAMMAR) \
211        $(SPV_DEBUGINFO_GRAMMAR)
212		@$(HOST_PYTHON) $(LOCAL_PATH)/utils/generate_grammar_tables.py \
213		                --spirv-core-grammar=$(SPV_CORE12_GRAMMAR) \
214		                --extinst-debuginfo-grammar=$(SPV_DEBUGINFO_GRAMMAR) \
215		                --core-insts-output=$(1)/core.insts-1.2.inc \
216		                --operand-kinds-output=$(1)/operand.kinds-1.2.inc
217		@echo "[$(TARGET_ARCH_ABI)] Grammar v1.2   : instructions & operands <= grammar JSON files"
218$(1)/core.insts-unified1.inc $(1)/operand.kinds-unified1.inc: \
219        $(LOCAL_PATH)/utils/generate_grammar_tables.py \
220        $(SPV_COREUNIFIED1_GRAMMAR) \
221        $(SPV_DEBUGINFO_GRAMMAR)
222		@$(HOST_PYTHON) $(LOCAL_PATH)/utils/generate_grammar_tables.py \
223		                --spirv-core-grammar=$(SPV_COREUNIFIED1_GRAMMAR) \
224		                --extinst-debuginfo-grammar=$(SPV_DEBUGINFO_GRAMMAR) \
225		                --core-insts-output=$(1)/core.insts-unified1.inc \
226		                --operand-kinds-output=$(1)/operand.kinds-unified1.inc
227		@echo "[$(TARGET_ARCH_ABI)] Grammar v1.3 (from unified1)  : instructions & operands <= grammar JSON files"
228$(LOCAL_PATH)/source/opcode.cpp: $(1)/core.insts-1.0.inc $(1)/core.insts-1.1.inc $(1)/core.insts-1.2.inc $(1)/core.insts-unified1.inc
229$(LOCAL_PATH)/source/operand.cpp: $(1)/operand.kinds-1.0.inc $(1)/operand.kinds-1.1.inc $(1)/operand.kinds-1.2.inc $(1)/operand.kinds-unified1.inc
230$(LOCAL_PATH)/source/ext_inst.cpp: \
231	$(1)/glsl.std.450.insts.inc \
232	$(1)/opencl.std.insts.inc \
233	$(1)/debuginfo.insts.inc \
234	$(1)/spv-amd-gcn-shader.insts.inc \
235	$(1)/spv-amd-shader-ballot.insts.inc \
236	$(1)/spv-amd-shader-explicit-vertex-parameter.insts.inc \
237	$(1)/spv-amd-shader-trinary-minmax.insts.inc
238endef
239$(eval $(call gen_spvtools_grammar_tables,$(SPVTOOLS_OUT_PATH)))
240
241
242define gen_spvtools_lang_headers
243# Generate language-specific headers.  So far we only generate C headers
244# $1 is the output directory.
245# $2 is the base name of the header file, e.g. "DebugInfo".
246# $3 is the grammar file containing token definitions.
247$(call generate-file-dir,$(1)/$(2).h)
248$(1)/$(2).h : \
249        $(LOCAL_PATH)/utils/generate_language_headers.py \
250        $(3)
251		@$(HOST_PYTHON) $(LOCAL_PATH)/utils/generate_language_headers.py \
252		    --extinst-name=$(2) \
253		    --extinst-grammar=$(3) \
254		    --extinst-output-base=$(1)/$(2)
255		@echo "[$(TARGET_ARCH_ABI)] Generate language specific header for $(2): headers <= grammar"
256$(LOCAL_PATH)/source/ext_inst.cpp: $(1)/$(2).h
257endef
258# We generate language-specific headers for DebugInfo
259$(eval $(call gen_spvtools_lang_headers,$(SPVTOOLS_OUT_PATH),DebugInfo,$(SPV_DEBUGINFO_GRAMMAR)))
260
261
262define gen_spvtools_vendor_tables
263$(call generate-file-dir,$(1)/$(2).insts.inc)
264$(1)/$(2).insts.inc : \
265        $(LOCAL_PATH)/utils/generate_grammar_tables.py \
266        $(LOCAL_PATH)/source/extinst.$(2).grammar.json
267		@$(HOST_PYTHON) $(LOCAL_PATH)/utils/generate_grammar_tables.py \
268		    --extinst-vendor-grammar=$(LOCAL_PATH)/source/extinst.$(2).grammar.json \
269		    --vendor-insts-output=$(1)/$(2).insts.inc
270		@echo "[$(TARGET_ARCH_ABI)] Vendor extended instruction set: $(2) tables <= grammar"
271$(LOCAL_PATH)/source/ext_inst.cpp: $(1)/$(2).insts.inc
272endef
273# Vendor extended instruction sets, with grammars from SPIRV-Tools source tree.
274SPV_NONSTANDARD_EXTINST_GRAMMARS=$(foreach F,$(wildcard $(LOCAL_PATH)/source/extinst.*.grammar.json),$(patsubst extinst.%.grammar.json,%,$(notdir $F)))
275$(foreach E,$(SPV_NONSTANDARD_EXTINST_GRAMMARS),$(eval $(call gen_spvtools_vendor_tables,$(SPVTOOLS_OUT_PATH),$E)))
276
277define gen_spvtools_enum_string_mapping
278$(call generate-file-dir,$(1)/extension_enum.inc.inc)
279$(1)/extension_enum.inc $(1)/enum_string_mapping.inc: \
280        $(LOCAL_PATH)/utils/generate_grammar_tables.py \
281        $(SPV_CORELATEST_GRAMMAR)
282		@$(HOST_PYTHON) $(LOCAL_PATH)/utils/generate_grammar_tables.py \
283		                --spirv-core-grammar=$(SPV_CORELATEST_GRAMMAR) \
284		                --extinst-debuginfo-grammar=$(SPV_DEBUGINFO_GRAMMAR) \
285		                --extension-enum-output=$(1)/extension_enum.inc \
286		                --enum-string-mapping-output=$(1)/enum_string_mapping.inc
287		@echo "[$(TARGET_ARCH_ABI)] Generate enum<->string mapping <= grammar JSON files"
288# Generated header extension_enum.inc is transitively included by table.h, which is
289# used pervasively.  Capture the pervasive dependency.
290$(foreach F,$(SPVTOOLS_SRC_FILES) $(SPVTOOLS_OPT_SRC_FILES),$(LOCAL_PATH)/$F ) \
291  : $(1)/extension_enum.inc
292$(LOCAL_PATH)/source/enum_string_mapping.cpp: $(1)/enum_string_mapping.inc
293endef
294$(eval $(call gen_spvtools_enum_string_mapping,$(SPVTOOLS_OUT_PATH)))
295
296define gen_spvtools_build_version_inc
297$(call generate-file-dir,$(1)/dummy_filename)
298$(1)/build-version.inc: \
299        $(LOCAL_PATH)/utils/update_build_version.py \
300        $(LOCAL_PATH)/CHANGES
301		@$(HOST_PYTHON) $(LOCAL_PATH)/utils/update_build_version.py \
302		                $(LOCAL_PATH) $(1)/build-version.inc
303		@echo "[$(TARGET_ARCH_ABI)] Generate       : build-version.inc <= CHANGES"
304$(LOCAL_PATH)/source/software_version.cpp: $(1)/build-version.inc
305endef
306$(eval $(call gen_spvtools_build_version_inc,$(SPVTOOLS_OUT_PATH)))
307
308define gen_spvtools_generators_inc
309$(call generate-file-dir,$(1)/dummy_filename)
310$(1)/generators.inc: \
311        $(LOCAL_PATH)/utils/generate_registry_tables.py \
312        $(SPVHEADERS_LOCAL_PATH)/include/spirv/spir-v.xml
313		@$(HOST_PYTHON) $(LOCAL_PATH)/utils/generate_registry_tables.py \
314		                --xml=$(SPVHEADERS_LOCAL_PATH)/include/spirv/spir-v.xml \
315				--generator-output=$(1)/generators.inc
316		@echo "[$(TARGET_ARCH_ABI)] Generate       : generators.inc <= spir-v.xml"
317$(LOCAL_PATH)/source/opcode.cpp: $(1)/generators.inc
318endef
319$(eval $(call gen_spvtools_generators_inc,$(SPVTOOLS_OUT_PATH)))
320
321include $(CLEAR_VARS)
322LOCAL_MODULE := SPIRV-Tools
323LOCAL_C_INCLUDES := \
324		$(LOCAL_PATH)/include \
325		$(SPVHEADERS_LOCAL_PATH)/include \
326		$(SPVTOOLS_OUT_PATH)
327LOCAL_EXPORT_C_INCLUDES := \
328		$(LOCAL_PATH)/include
329LOCAL_CXXFLAGS:=-std=c++11 -fno-exceptions -fno-rtti -Werror
330LOCAL_SRC_FILES:= $(SPVTOOLS_SRC_FILES)
331include $(BUILD_STATIC_LIBRARY)
332
333include $(CLEAR_VARS)
334LOCAL_MODULE := SPIRV-Tools-opt
335LOCAL_C_INCLUDES := \
336		$(LOCAL_PATH)/include \
337		$(LOCAL_PATH)/source \
338		$(SPVHEADERS_LOCAL_PATH)/include \
339		$(SPVTOOLS_OUT_PATH)
340LOCAL_CXXFLAGS:=-std=c++11 -fno-exceptions -fno-rtti -Werror
341LOCAL_STATIC_LIBRARIES:=SPIRV-Tools
342LOCAL_SRC_FILES:= $(SPVTOOLS_OPT_SRC_FILES)
343include $(BUILD_STATIC_LIBRARY)
344