xref: /linux-6.15/scripts/Makefile.modinst (revision 5e02797b)
1# SPDX-License-Identifier: GPL-2.0
2# ==========================================================================
3# Installing modules
4# ==========================================================================
5
6PHONY := __modinst
7__modinst:
8
9include include/config/auto.conf
10include $(srctree)/scripts/Kbuild.include
11
12install-y :=
13
14ifeq ($(KBUILD_EXTMOD)$(modules_sign_only),)
15
16# remove the old directory and symlink
17$(shell rm -fr $(MODLIB)/kernel $(MODLIB)/build)
18
19install-$(CONFIG_MODULES) += $(addprefix $(MODLIB)/, build modules.order)
20
21$(MODLIB)/build: FORCE
22	$(call cmd,symlink)
23
24quiet_cmd_symlink = SYMLINK $@
25      cmd_symlink = ln -s $(CURDIR) $@
26
27$(MODLIB)/modules.order: modules.order FORCE
28	$(call cmd,install_modorder)
29
30quiet_cmd_install_modorder = INSTALL $@
31      cmd_install_modorder = sed 's:^\(.*\)\.o$$:kernel/\1.ko:' $< > $@
32
33# Install modules.builtin(.modinfo) even when CONFIG_MODULES is disabled.
34install-y += $(addprefix $(MODLIB)/, modules.builtin modules.builtin.modinfo)
35
36$(addprefix $(MODLIB)/, modules.builtin modules.builtin.modinfo): $(MODLIB)/%: % FORCE
37	$(call cmd,install)
38
39endif
40
41modules := $(call read-file, $(MODORDER))
42
43ifeq ($(KBUILD_EXTMOD),)
44dst := $(MODLIB)/kernel
45else
46INSTALL_MOD_DIR ?= updates
47dst := $(MODLIB)/$(INSTALL_MOD_DIR)
48endif
49
50$(foreach x, % :, $(if $(findstring $x, $(dst)), \
51	$(error module installation path cannot contain '$x')))
52
53suffix-y				:=
54suffix-$(CONFIG_MODULE_COMPRESS_GZIP)	:= .gz
55suffix-$(CONFIG_MODULE_COMPRESS_XZ)	:= .xz
56suffix-$(CONFIG_MODULE_COMPRESS_ZSTD)	:= .zst
57
58modules := $(patsubst $(extmod_prefix)%.o, $(dst)/%.ko$(suffix-y), $(modules))
59install-$(CONFIG_MODULES) += $(modules)
60
61__modinst: $(install-y)
62	@:
63
64#
65# Installation
66#
67quiet_cmd_install = INSTALL $@
68      cmd_install = cp $< $@
69
70# Strip
71#
72# INSTALL_MOD_STRIP, if defined, will cause modules to be stripped after they
73# are installed. If INSTALL_MOD_STRIP is '1', then the default option
74# --strip-debug will be used. Otherwise, INSTALL_MOD_STRIP value will be used
75# as the options to the strip command.
76ifdef INSTALL_MOD_STRIP
77
78ifeq ($(INSTALL_MOD_STRIP),1)
79strip-option := --strip-debug
80else
81strip-option := $(INSTALL_MOD_STRIP)
82endif
83
84quiet_cmd_strip = STRIP   $@
85      cmd_strip = $(STRIP) $(strip-option) $@
86
87else
88
89quiet_cmd_strip =
90      cmd_strip = :
91
92endif
93
94#
95# Signing
96# Don't stop modules_install even if we can't sign external modules.
97#
98ifeq ($(CONFIG_MODULE_SIG_ALL),y)
99ifeq ($(filter pkcs11:%, $(CONFIG_MODULE_SIG_KEY)),)
100sig-key := $(if $(wildcard $(CONFIG_MODULE_SIG_KEY)),,$(srctree)/)$(CONFIG_MODULE_SIG_KEY)
101else
102sig-key := $(CONFIG_MODULE_SIG_KEY)
103endif
104quiet_cmd_sign = SIGN    $@
105      cmd_sign = scripts/sign-file $(CONFIG_MODULE_SIG_HASH) "$(sig-key)" certs/signing_key.x509 $@ \
106                 $(if $(KBUILD_EXTMOD),|| true)
107else
108quiet_cmd_sign :=
109      cmd_sign := :
110endif
111
112ifeq ($(modules_sign_only),)
113
114# Create necessary directories
115$(shell mkdir -p $(sort $(dir $(install-y))))
116
117$(dst)/%.ko: $(extmod_prefix)%.ko FORCE
118	$(call cmd,install)
119	$(call cmd,strip)
120	$(call cmd,sign)
121
122ifdef CONFIG_MODULES
123__modinst: depmod
124
125PHONY += depmod
126depmod: $(install-y)
127	$(call cmd,depmod)
128
129quiet_cmd_depmod = DEPMOD  $(MODLIB)
130      cmd_depmod = $(srctree)/scripts/depmod.sh $(KERNELRELEASE)
131endif
132
133else
134
135$(dst)/%.ko: FORCE
136	$(call cmd,sign)
137
138endif
139
140#
141# Compression
142#
143quiet_cmd_gzip = GZIP    $@
144      cmd_gzip = $(KGZIP) -n -f $<
145quiet_cmd_xz = XZ      $@
146      cmd_xz = $(XZ) --lzma2=dict=2MiB -f $<
147quiet_cmd_zstd = ZSTD    $@
148      cmd_zstd = $(ZSTD) -T0 --rm -f -q $<
149
150$(dst)/%.ko.gz: $(dst)/%.ko FORCE
151	$(call cmd,gzip)
152
153$(dst)/%.ko.xz: $(dst)/%.ko FORCE
154	$(call cmd,xz)
155
156$(dst)/%.ko.zst: $(dst)/%.ko FORCE
157	$(call cmd,zstd)
158
159PHONY += FORCE
160FORCE:
161
162.PHONY: $(PHONY)
163