1# SPDX-License-Identifier: GPL-2.0
2# ==========================================================================
3# Installing headers
4#
5# All headers under include/uapi, include/generated/uapi,
6# arch/<arch>/include/uapi and arch/<arch>/include/generated/uapi are
7# exported.
8# They are preprocessed to remove __KERNEL__ section of the file.
9#
10# ==========================================================================
11
12PHONY := __headers
13__headers:
14
15include scripts/Kbuild.include
16
17srcdir        := $(srctree)/$(obj)
18
19# When make is run under a fakechroot environment, the function
20# $(wildcard $(srcdir)/*/.) doesn't only return directories, but also regular
21# files. So, we are using a combination of sort/dir/wildcard which works
22# with fakechroot.
23subdirs       := $(patsubst $(srcdir)/%/,%,\
24		 $(filter-out $(srcdir)/,\
25		 $(sort $(dir $(wildcard $(srcdir)/*/)))))
26
27# Recursion
28__headers: $(subdirs)
29
30PHONY += $(subdirs)
31$(subdirs):
32	$(Q)$(MAKE) $(hdr-inst)=$(obj)/$@ dst=$(dst)/$@
33
34# Skip header install/check for include/uapi and arch/$(SRCARCH)/include/uapi.
35# We have only sub-directories there.
36skip-inst := $(if $(filter %/uapi,$(obj)),1)
37
38ifeq ($(skip-inst),)
39
40# Kbuild file is optional
41kbuild-file := $(srctree)/$(obj)/Kbuild
42-include $(kbuild-file)
43
44installdir    := $(INSTALL_HDR_PATH)/$(dst)
45gendir        := $(objtree)/$(subst include/,include/generated/,$(obj))
46header-files  := $(notdir $(wildcard $(srcdir)/*.h))
47header-files  := $(filter-out $(no-export-headers), $(header-files))
48genhdr-files  := $(notdir $(wildcard $(gendir)/*.h))
49genhdr-files  := $(filter-out $(header-files), $(genhdr-files))
50
51# files used to track state of install/check
52install-file  := $(installdir)/.install
53check-file    := $(installdir)/.check
54
55# all headers files for this dir
56all-files     := $(header-files) $(genhdr-files)
57output-files  := $(addprefix $(installdir)/, $(all-files))
58
59ifneq ($(mandatory-y),)
60missing       := $(filter-out $(all-files),$(mandatory-y))
61ifneq ($(missing),)
62$(error Some mandatory headers ($(missing)) are missing in $(obj))
63endif
64endif
65
66# Work out what needs to be removed
67oldheaders    := $(patsubst $(installdir)/%,%,$(wildcard $(installdir)/*.h))
68unwanted      := $(filter-out $(all-files),$(oldheaders))
69
70# Prefix unwanted with full paths to $(INSTALL_HDR_PATH)
71unwanted-file := $(addprefix $(installdir)/, $(unwanted))
72
73printdir = $(patsubst $(INSTALL_HDR_PATH)/%/,%,$(dir $@))
74
75quiet_cmd_install = INSTALL $(printdir) ($(words $(all-files))\
76                            file$(if $(word 2, $(all-files)),s))
77      cmd_install = \
78        $(CONFIG_SHELL) $< $(installdir) $(srcdir) $(header-files); \
79        $(CONFIG_SHELL) $< $(installdir) $(gendir) $(genhdr-files); \
80        touch $@
81
82quiet_cmd_remove = REMOVE  $(unwanted)
83      cmd_remove = rm -f $(unwanted-file)
84
85quiet_cmd_check = CHECK   $(printdir) ($(words $(all-files)) files)
86# Headers list can be pretty long, xargs helps to avoid
87# the "Argument list too long" error.
88      cmd_check = for f in $(all-files); do                          \
89                  echo "$(installdir)/$${f}"; done                      \
90                  | xargs                                            \
91                  $(PERL) $< $(INSTALL_HDR_PATH)/include $(SRCARCH); \
92	          touch $@
93
94ifndef HDRCHECK
95# Rules for installing headers
96__headers: $(install-file)
97	@:
98
99targets += $(install-file)
100$(install-file): scripts/headers_install.sh \
101		 $(addprefix $(srcdir)/,$(header-files)) \
102		 $(addprefix $(gendir)/,$(genhdr-files)) FORCE
103	$(if $(unwanted),$(call cmd,remove),)
104	$(if $(wildcard $(dir $@)),,$(shell mkdir -p $(dir $@)))
105	$(call if_changed,install)
106
107else
108__headers: $(check-file)
109	@:
110
111targets += $(check-file)
112$(check-file): scripts/headers_check.pl $(output-files) FORCE
113	$(call if_changed,check)
114
115endif
116
117cmd_files := $(wildcard \
118             $(foreach f,$(sort $(targets)),$(dir $(f)).$(notdir $(f)).cmd))
119
120ifneq ($(cmd_files),)
121	include $(cmd_files)
122endif
123
124endif # skip-inst
125
126PHONY += FORCE
127FORCE: ;
128
129.PHONY: $(PHONY)
130