1# SPDX-License-Identifier: GPL-2.0
2DT_DOC_CHECKER ?= dt-doc-validate
3DT_EXTRACT_EX ?= dt-extract-example
4DT_MK_SCHEMA ?= dt-mk-schema
5
6DT_SCHEMA_LINT = $(shell which yamllint)
7
8DT_SCHEMA_MIN_VERSION = 2021.2.1
9
10PHONY += check_dtschema_version
11check_dtschema_version:
12	@{ echo $(DT_SCHEMA_MIN_VERSION); \
13	$(DT_DOC_CHECKER) --version 2>/dev/null || echo 0; } | sort -Vc >/dev/null || \
14	{ echo "ERROR: dtschema minimum version is v$(DT_SCHEMA_MIN_VERSION)" >&2; false; }
15
16quiet_cmd_extract_ex = DTEX    $@
17      cmd_extract_ex = $(DT_EXTRACT_EX) $< > $@
18
19$(obj)/%.example.dts: $(src)/%.yaml check_dtschema_version FORCE
20	$(call if_changed,extract_ex)
21
22# Use full schemas when checking %.example.dts
23DT_TMP_SCHEMA := $(obj)/processed-schema-examples.json
24
25find_cmd = find $(srctree)/$(src) \( -name '*.yaml' ! \
26		-name 'processed-schema*' ! \
27		-name '*.example.dt.yaml' \)
28
29quiet_cmd_yamllint = LINT    $(src)
30      cmd_yamllint = ($(find_cmd) | \
31                     xargs $(DT_SCHEMA_LINT) -f parsable -c $(srctree)/$(src)/.yamllint) || true
32
33quiet_cmd_chk_bindings = CHKDT   $@
34      cmd_chk_bindings = ($(find_cmd) | \
35                         xargs -n200 -P$$(nproc) $(DT_DOC_CHECKER) -u $(srctree)/$(src)) || true
36
37quiet_cmd_mk_schema = SCHEMA  $@
38      cmd_mk_schema = f=$$(mktemp) ; \
39                      $(if $(DT_MK_SCHEMA_FLAGS), \
40                           printf '%s\n' $(real-prereqs), \
41                           $(find_cmd)) > $$f ; \
42                      $(DT_MK_SCHEMA) -j $(DT_MK_SCHEMA_FLAGS) @$$f > $@ ; \
43		      rm -f $$f
44
45define rule_chkdt
46	$(if $(DT_SCHEMA_LINT),$(call cmd,yamllint),)
47	$(call cmd,chk_bindings)
48	$(call cmd,mk_schema)
49endef
50
51DT_DOCS = $(patsubst $(srctree)/%,%,$(shell $(find_cmd)))
52
53override DTC_FLAGS := \
54	-Wno-avoid_unnecessary_addr_size \
55	-Wno-graph_child_address \
56	-Wno-interrupt_provider
57
58# Disable undocumented compatible checks until warning free
59override DT_CHECKER_FLAGS ?=
60
61$(obj)/processed-schema-examples.json: $(DT_DOCS) $(src)/.yamllint check_dtschema_version FORCE
62	$(call if_changed_rule,chkdt)
63
64ifeq ($(DT_SCHEMA_FILES),)
65
66# Unless DT_SCHEMA_FILES is specified, use the full schema for dtbs_check too.
67# Just copy processed-schema-examples.json
68
69$(obj)/processed-schema.json: $(obj)/processed-schema-examples.json FORCE
70	$(call if_changed,copy)
71
72DT_SCHEMA_FILES = $(DT_DOCS)
73
74else
75
76# If DT_SCHEMA_FILES is specified, use it for processed-schema.json
77
78$(obj)/processed-schema.json: DT_MK_SCHEMA_FLAGS := -u
79$(obj)/processed-schema.json: $(DT_SCHEMA_FILES) check_dtschema_version FORCE
80	$(call if_changed,mk_schema)
81
82endif
83
84always-$(CHECK_DT_BINDING) += processed-schema-examples.json
85always-$(CHECK_DTBS)       += processed-schema.json
86always-$(CHECK_DT_BINDING) += $(patsubst $(src)/%.yaml,%.example.dts, $(DT_SCHEMA_FILES))
87always-$(CHECK_DT_BINDING) += $(patsubst $(src)/%.yaml,%.example.dt.yaml, $(DT_SCHEMA_FILES))
88
89# Hack: avoid 'Argument list too long' error for 'make clean'. Remove most of
90# build artifacts here before they are processed by scripts/Makefile.clean
91clean-files = $(shell find $(obj) \( -name '*.example.dts' -o \
92			-name '*.example.dt.yaml' \) -delete 2>/dev/null)
93