linux/scripts/Makefile.modinst

# SPDX-License-Identifier: GPL-2.0
# ==========================================================================
# Installing modules
# ==========================================================================

PHONY := __modinst
__modinst:

include include/config/auto.conf
include $(srctree)/scripts/Kbuild.include

install-y :=

ifeq ($(KBUILD_EXTMOD)$(sign-only),)

# remove the old directory and symlink
$(shell rm -fr $(MODLIB)/kernel $(MODLIB)/build)

install-$(CONFIG_MODULES) += $(addprefix $(MODLIB)/, build modules.order)

$(MODLIB)/build: FORCE
	$(call cmd,symlink)

quiet_cmd_symlink = SYMLINK $@
      cmd_symlink = ln -s $(CURDIR) $@

$(MODLIB)/modules.order: modules.order FORCE
	$(call cmd,install_modorder)

quiet_cmd_install_modorder = INSTALL $@
      cmd_install_modorder = sed 's:^\(.*\)\.o$$:kernel/\1.ko:' $< > $@

# Install modules.builtin(.modinfo) even when CONFIG_MODULES is disabled.
install-y += $(addprefix $(MODLIB)/, modules.builtin modules.builtin.modinfo)

$(addprefix $(MODLIB)/, modules.builtin modules.builtin.modinfo): $(MODLIB)/%: % FORCE
	$(call cmd,install)

endif

modules := $(call read-file, $(MODORDER))

ifeq ($(KBUILD_EXTMOD),)
dst := $(MODLIB)/kernel
else
INSTALL_MOD_DIR ?= updates
dst := $(MODLIB)/$(INSTALL_MOD_DIR)
endif

$(foreach x, % :, $(if $(findstring $x, $(dst)), \
	$(error module installation path cannot contain '$x')))

suffix-y				:=
suffix-$(CONFIG_MODULE_COMPRESS_GZIP)	:= .gz
suffix-$(CONFIG_MODULE_COMPRESS_XZ)	:= .xz
suffix-$(CONFIG_MODULE_COMPRESS_ZSTD)	:= .zst

modules := $(patsubst $(extmod_prefix)%.o, $(dst)/%.ko$(suffix-y), $(modules))
install-$(CONFIG_MODULES) += $(modules)

__modinst: $(install-y)
	@:

#
# Installation
#
quiet_cmd_install = INSTALL $@
      cmd_install = cp $< $@

# Strip
#
# INSTALL_MOD_STRIP, if defined, will cause modules to be stripped after they
# are installed. If INSTALL_MOD_STRIP is '1', then the default option
# --strip-debug will be used. Otherwise, INSTALL_MOD_STRIP value will be used
# as the options to the strip command.
ifdef INSTALL_MOD_STRIP

ifeq ($(INSTALL_MOD_STRIP),1)
strip-option := --strip-debug
else
strip-option := $(INSTALL_MOD_STRIP)
endif

quiet_cmd_strip = STRIP   $@
      cmd_strip = $(STRIP) $(strip-option) $@

else

quiet_cmd_strip =
      cmd_strip = :

endif

#
# Signing
# Don't stop modules_install even if we can't sign external modules.
#
ifeq ($(filter pkcs11:%, $(CONFIG_MODULE_SIG_KEY)),)
sig-key := $(if $(wildcard $(CONFIG_MODULE_SIG_KEY)),,$(srctree)/)$(CONFIG_MODULE_SIG_KEY)
else
sig-key := $(CONFIG_MODULE_SIG_KEY)
endif
quiet_cmd_sign = SIGN    $@
      cmd_sign = scripts/sign-file $(CONFIG_MODULE_SIG_HASH) "$(sig-key)" certs/signing_key.x509 $@ \
                 $(if $(KBUILD_EXTMOD),|| true)

ifeq ($(sign-only),)

# During modules_install, modules are signed only when CONFIG_MODULE_SIG_ALL=y.
ifndef CONFIG_MODULE_SIG_ALL
quiet_cmd_sign :=
      cmd_sign := :
endif

# Create necessary directories
$(foreach dir, $(sort $(dir $(install-y))), $(shell mkdir -p $(dir)))

$(dst)/%.ko: $(extmod_prefix)%.ko FORCE
	$(call cmd,install)
	$(call cmd,strip)
	$(call cmd,sign)

ifdef CONFIG_MODULES
__modinst: depmod

PHONY += depmod
depmod: $(install-y)
	$(call cmd,depmod)

quiet_cmd_depmod = DEPMOD  $(MODLIB)
      cmd_depmod = $(srctree)/scripts/depmod.sh $(KERNELRELEASE)
endif

else

$(dst)/%.ko: FORCE
	$(call cmd,sign)

endif

#
# Compression
#
quiet_cmd_gzip = GZIP    $@
      cmd_gzip = $(KGZIP) -n -f $<
quiet_cmd_xz = XZ      $@
      cmd_xz = $(XZ) --check=crc32 --lzma2=dict=1MiB -f $<
quiet_cmd_zstd = ZSTD    $@
      cmd_zstd = $(ZSTD) -T0 --rm -f -q $<

$(dst)/%.ko.gz: $(dst)/%.ko FORCE
	$(call cmd,gzip)

$(dst)/%.ko.xz: $(dst)/%.ko FORCE
	$(call cmd,xz)

$(dst)/%.ko.zst: $(dst)/%.ko FORCE
	$(call cmd,zstd)

PHONY += FORCE
FORCE:

.PHONY: $(PHONY)