18c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci# ==========================================================================
38c2ecf20Sopenharmony_ci# Installing headers
48c2ecf20Sopenharmony_ci#
58c2ecf20Sopenharmony_ci# All headers under include/uapi, include/generated/uapi,
68c2ecf20Sopenharmony_ci# arch/<arch>/include/uapi and arch/<arch>/include/generated/uapi are
78c2ecf20Sopenharmony_ci# exported.
88c2ecf20Sopenharmony_ci# They are preprocessed to remove __KERNEL__ section of the file.
98c2ecf20Sopenharmony_ci#
108c2ecf20Sopenharmony_ci# ==========================================================================
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ciPHONY := __headers
138c2ecf20Sopenharmony_ci__headers:
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ciinclude scripts/Kbuild.include
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cisrc := $(srctree)/$(obj)
188c2ecf20Sopenharmony_cigen := $(objtree)/$(subst include/,include/generated/,$(obj))
198c2ecf20Sopenharmony_cidst := usr/include
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci-include $(src)/Kbuild
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci# $(filter %/, ...) is a workaround for GNU Make <= 4.2.1, where
248c2ecf20Sopenharmony_ci# $(wildcard $(src)/*/) contains not only directories but also regular files.
258c2ecf20Sopenharmony_cisrc-subdirs := $(patsubst $(src)/%/,%,$(filter %/, $(wildcard $(src)/*/)))
268c2ecf20Sopenharmony_cigen-subdirs := $(patsubst $(gen)/%/,%,$(filter %/, $(wildcard $(gen)/*/)))
278c2ecf20Sopenharmony_ciall-subdirs := $(sort $(src-subdirs) $(gen-subdirs))
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cisrc-headers := $(if $(src-subdirs), $(shell cd $(src) && find $(src-subdirs) -name '*.h'))
308c2ecf20Sopenharmony_cisrc-headers := $(filter-out $(no-export-headers), $(src-headers))
318c2ecf20Sopenharmony_cigen-headers := $(if $(gen-subdirs), $(shell cd $(gen) && find $(gen-subdirs) -name '*.h'))
328c2ecf20Sopenharmony_cigen-headers := $(filter-out $(no-export-headers), $(gen-headers))
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci# If the same header is exported from source and generated directories,
358c2ecf20Sopenharmony_ci# the former takes precedence, but this should be warned.
368c2ecf20Sopenharmony_ciduplicated := $(filter $(gen-headers), $(src-headers))
378c2ecf20Sopenharmony_ci$(if $(duplicated), $(warning duplicated header export: $(duplicated)))
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cigen-headers := $(filter-out $(duplicated), $(gen-headers))
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci# Add dst path prefix
428c2ecf20Sopenharmony_ciall-subdirs := $(addprefix $(dst)/, $(all-subdirs))
438c2ecf20Sopenharmony_cisrc-headers := $(addprefix $(dst)/, $(src-headers))
448c2ecf20Sopenharmony_cigen-headers := $(addprefix $(dst)/, $(gen-headers))
458c2ecf20Sopenharmony_ciall-headers := $(src-headers) $(gen-headers)
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci# Work out what needs to be removed
488c2ecf20Sopenharmony_ciold-subdirs := $(wildcard $(all-subdirs))
498c2ecf20Sopenharmony_ciold-headers := $(if $(old-subdirs),$(shell find $(old-subdirs) -name '*.h'))
508c2ecf20Sopenharmony_ciunwanted    := $(filter-out $(all-headers), $(old-headers))
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci# Create directories
538c2ecf20Sopenharmony_ciexisting-dirs := $(sort $(dir $(old-headers)))
548c2ecf20Sopenharmony_ciwanted-dirs   := $(sort $(dir $(all-headers)))
558c2ecf20Sopenharmony_cinew-dirs      := $(filter-out $(existing-dirs), $(wanted-dirs))
568c2ecf20Sopenharmony_ci$(if $(new-dirs), $(shell mkdir -p $(new-dirs)))
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci# Rules
598c2ecf20Sopenharmony_ciquiet_cmd_install = HDRINST $@
608c2ecf20Sopenharmony_ci      cmd_install = $(CONFIG_SHELL) $(srctree)/scripts/headers_install.sh $< $@
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci$(src-headers): $(dst)/%.h: $(src)/%.h $(srctree)/scripts/headers_install.sh FORCE
638c2ecf20Sopenharmony_ci	$(call if_changed,install)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci$(gen-headers): $(dst)/%.h: $(gen)/%.h $(srctree)/scripts/headers_install.sh FORCE
668c2ecf20Sopenharmony_ci	$(call if_changed,install)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciquiet_cmd_remove = REMOVE  $(unwanted)
698c2ecf20Sopenharmony_ci      cmd_remove = rm -f $(unwanted)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci__headers: $(all-headers)
728c2ecf20Sopenharmony_ciifneq ($(unwanted),)
738c2ecf20Sopenharmony_ci	$(call cmd,remove)
748c2ecf20Sopenharmony_ciendif
758c2ecf20Sopenharmony_ci	@:
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciexisting-headers := $(filter $(old-headers), $(all-headers))
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci-include $(foreach f,$(existing-headers),$(dir $(f)).$(notdir $(f)).cmd)
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ciPHONY += FORCE
828c2ecf20Sopenharmony_ciFORCE:
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci.PHONY: $(PHONY)
85