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