1# ==========================================================================
2# Installing headers
3#
4# All headers under include/uapi, include/generated/uapi,
5# arch/<arch>/include/uapi/asm and arch/<arch>/include/generated/uapi/asm are
6# exported.
7# They are preprocessed to remove __KERNEL__ section of the file.
8#
9# ==========================================================================
10
11# generated header directory
12gen := $(if $(gen),$(gen),$(subst include/,include/generated/,$(obj)))
13
14# Kbuild file is optional
15kbuild-file := $(srctree)/$(obj)/Kbuild
16-include $(kbuild-file)
17
18# called may set destination dir (when installing to asm/)
19_dst := $(if $(dst),$(dst),$(obj))
20
21old-kbuild-file := $(srctree)/$(subst uapi/,,$(obj))/Kbuild
22ifneq ($(wildcard $(old-kbuild-file)),)
23include $(old-kbuild-file)
24endif
25
26include scripts/Kbuild.include
27
28installdir    := $(INSTALL_HDR_PATH)/$(subst uapi/,,$(_dst))
29
30srcdir        := $(srctree)/$(obj)
31gendir        := $(objtree)/$(gen)
32subdirs       := $(patsubst $(srcdir)/%/.,%,$(wildcard $(srcdir)/*/.))
33subdirs       += $(subdir-y)
34header-files  := $(notdir $(wildcard $(srcdir)/*.h))
35header-files  += $(notdir $(wildcard $(srcdir)/*.agh))
36header-files  := $(filter-out $(no-export-headers), $(header-files))
37genhdr-files  := $(notdir $(wildcard $(gendir)/*.h))
38genhdr-files  := $(filter-out $(header-files), $(genhdr-files))
39
40# files used to track state of install/check
41install-file  := $(installdir)/.install
42check-file    := $(installdir)/.check
43
44# generic-y list all files an architecture uses from asm-generic
45# Use this to build a list of headers which require a wrapper
46generic-files := $(notdir $(wildcard $(srctree)/include/uapi/asm-generic/*.h))
47wrapper-files := $(filter $(generic-files), $(generic-y))
48wrapper-files := $(filter-out $(header-files), $(wrapper-files))
49
50# all headers files for this dir
51all-files     := $(header-files) $(genhdr-files) $(wrapper-files)
52output-files  := $(addprefix $(installdir)/, $(all-files))
53
54ifneq ($(mandatory-y),)
55missing       := $(filter-out $(all-files),$(mandatory-y))
56ifneq ($(missing),)
57$(error Some mandatory headers ($(missing)) are missing in $(obj))
58endif
59endif
60
61# Work out what needs to be removed
62oldheaders    := $(patsubst $(installdir)/%,%,$(wildcard $(installdir)/*.h))
63unwanted      := $(filter-out $(all-files),$(oldheaders))
64
65# Prefix unwanted with full paths to $(INSTALL_HDR_PATH)
66unwanted-file := $(addprefix $(installdir)/, $(unwanted))
67
68printdir = $(patsubst $(INSTALL_HDR_PATH)/%/,%,$(dir $@))
69
70quiet_cmd_install = INSTALL $(printdir) ($(words $(all-files))\
71                            file$(if $(word 2, $(all-files)),s))
72      cmd_install = \
73        $(CONFIG_SHELL) $< $(installdir) $(srcdir) $(header-files); \
74        $(CONFIG_SHELL) $< $(installdir) $(gendir) $(genhdr-files); \
75        for F in $(wrapper-files); do                                   \
76                echo "\#include <asm-generic/$$F>" > $(installdir)/$$F;    \
77        done;                                                           \
78        touch $@
79
80quiet_cmd_remove = REMOVE  $(unwanted)
81      cmd_remove = rm -f $(unwanted-file)
82
83quiet_cmd_check = CHECK   $(printdir) ($(words $(all-files)) files)
84# Headers list can be pretty long, xargs helps to avoid
85# the "Argument list too long" error.
86      cmd_check = for f in $(all-files); do                          \
87                  echo "$(installdir)/$${f}"; done                      \
88                  | xargs                                            \
89                  $(PERL) $< $(INSTALL_HDR_PATH)/include $(SRCARCH); \
90	          touch $@
91
92PHONY += __headersinst __headerscheck
93
94ifndef HDRCHECK
95# Rules for installing headers
96__headersinst: $(subdirs) $(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__headerscheck: $(subdirs) $(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
117# Recursion
118.PHONY: $(subdirs)
119$(subdirs):
120	$(Q)$(MAKE) $(hdr-inst)=$(obj)/$@ dst=$(_dst)/$@
121
122targets := $(wildcard $(sort $(targets)))
123cmd_files := $(wildcard \
124             $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
125
126ifneq ($(cmd_files),)
127	include $(cmd_files)
128endif
129
130.PHONY: $(PHONY)
131PHONY += FORCE
132FORCE: ;
133