1# SPDX-License-Identifier: GPL-2.0
2# include/asm-generic contains a lot of files that are used
3# verbatim by several architectures.
4#
5# This Makefile reads the file arch/$(SRCARCH)/include/(uapi/)/asm/Kbuild
6# and for each file listed in this file with generic-y creates
7# a small wrapper file in arch/$(SRCARCH)/include/generated/(uapi/)/asm.
8
9PHONY := all
10all:
11
12src := $(subst /generated,,$(obj))
13-include $(src)/Kbuild
14
15include scripts/Kbuild.include
16
17# If arch does not implement mandatory headers, fallback to asm-generic ones.
18mandatory-y := $(filter-out $(generated-y), $(mandatory-y))
19generic-y   += $(foreach f, $(mandatory-y), $(if $(wildcard $(srctree)/$(src)/$(f)),,$(f)))
20
21generic-y   := $(addprefix $(obj)/, $(generic-y))
22generated-y := $(addprefix $(obj)/, $(generated-y))
23
24# Remove stale wrappers when the corresponding files are removed from generic-y
25old-headers := $(wildcard $(obj)/*.h)
26unwanted    := $(filter-out $(generic-y) $(generated-y),$(old-headers))
27
28quiet_cmd_wrap = WRAP    $@
29      cmd_wrap = echo "\#include <asm-generic/$*.h>" > $@
30
31quiet_cmd_remove = REMOVE  $(unwanted)
32      cmd_remove = rm -f $(unwanted)
33
34all: $(generic-y)
35	$(if $(unwanted),$(call cmd,remove))
36	@:
37
38$(obj)/%.h:
39	$(call cmd,wrap)
40
41# Create output directory. Skip it if at least one old header exists
42# since we know the output directory already exists.
43ifeq ($(old-headers),)
44$(shell mkdir -p $(obj))
45endif
46
47.PHONY: $(PHONY)
48