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