e5a0ba367bffd5a09556a039d6c81940bed9dcd8
[openwrt/openwrt.git] / include / kernel-defaults.mk
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # Copyright (C) 2006-2020 OpenWrt.org
4
5 ifdef CONFIG_STRIP_KERNEL_EXPORTS
6 KERNEL_MAKEOPTS += \
7 EXTRA_LDSFLAGS="-I$(KERNEL_BUILD_DIR) -include symtab.h"
8 endif
9
10 INITRAMFS_EXTRA_FILES ?= $(GENERIC_PLATFORM_DIR)/image/initramfs-base-files.txt
11
12 ifneq (,$(KERNEL_CC))
13 KERNEL_MAKEOPTS += CC="$(KERNEL_CC)"
14 endif
15
16 export HOST_EXTRACFLAGS=-I$(STAGING_DIR_HOST)/include
17
18 # defined in quilt.mk
19 Kernel/Patch:=$(Kernel/Patch/Default)
20
21 ifneq (,$(findstring .xz,$(LINUX_SOURCE)))
22 LINUX_CAT:=xzcat
23 else
24 LINUX_CAT:=gzip -dc
25 endif
26
27 ifeq ($(strip $(CONFIG_EXTERNAL_KERNEL_TREE)),"")
28 ifeq ($(strip $(CONFIG_KERNEL_GIT_CLONE_URI)),"")
29 define Kernel/Prepare/Default
30 $(LINUX_CAT) $(DL_DIR)/$(LINUX_SOURCE) | $(TAR) -C $(KERNEL_BUILD_DIR) $(TAR_OPTIONS)
31 $(Kernel/Patch)
32 $(if $(QUILT),touch $(LINUX_DIR)/.quilt_used)
33 endef
34 else
35 define Kernel/Prepare/Default
36 $(LINUX_CAT) $(DL_DIR)/$(LINUX_SOURCE) | $(TAR) -C $(KERNEL_BUILD_DIR) $(TAR_OPTIONS)
37 endef
38 endif
39 else
40 define Kernel/Prepare/Default
41 mkdir -p $(KERNEL_BUILD_DIR)
42 if [ -d $(LINUX_DIR) ]; then \
43 rmdir $(LINUX_DIR); \
44 fi
45 ln -s $(CONFIG_EXTERNAL_KERNEL_TREE) $(LINUX_DIR)
46 $(_SINGLE) [ -d $(LINUX_DIR)/user_headers ] && rm -rf $(LINUX_DIR)/user_headers
47 endef
48 endif
49
50 ifeq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),y)
51 ifeq ($(strip $(CONFIG_EXTERNAL_CPIO)),"")
52 define Kernel/SetInitramfs/PreConfigure
53 grep -v -e INITRAMFS -e CONFIG_RD_ -e CONFIG_BLK_DEV_INITRD $(LINUX_DIR)/.config.old > $(LINUX_DIR)/.config
54 echo 'CONFIG_BLK_DEV_INITRD=y' >> $(LINUX_DIR)/.config
55 echo 'CONFIG_INITRAMFS_SOURCE="$(strip $(TARGET_DIR) $(INITRAMFS_EXTRA_FILES))"' >> $(LINUX_DIR)/.config
56 endef
57 else
58 define Kernel/SetInitramfs/PreConfigure
59 grep -v INITRAMFS $(LINUX_DIR)/.config.old > $(LINUX_DIR)/.config
60 echo 'CONFIG_INITRAMFS_SOURCE="$(call qstrip,$(CONFIG_EXTERNAL_CPIO))"' >> $(LINUX_DIR)/.config
61 endef
62 endif
63
64 define Kernel/SetInitramfs
65 rm -f $(LINUX_DIR)/.config.prev
66 mv $(LINUX_DIR)/.config $(LINUX_DIR)/.config.old
67 $(call Kernel/SetInitramfs/PreConfigure)
68 echo 'CONFIG_INITRAMFS_ROOT_UID=$(shell id -u)' >> $(LINUX_DIR)/.config
69 echo 'CONFIG_INITRAMFS_ROOT_GID=$(shell id -g)' >> $(LINUX_DIR)/.config
70 echo "$(if $(CONFIG_TARGET_INITRAMFS_FORCE),CONFIG_INITRAMFS_FORCE=y,# CONFIG_INITRAMFS_FORCE is not set)" >> $(LINUX_DIR)/.config
71 echo "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_NONE),CONFIG_INITRAMFS_COMPRESSION_NONE=y,# CONFIG_INITRAMFS_COMPRESSION_NONE is not set)" >> $(LINUX_DIR)/.config
72 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_GZIP),CONFIG_INITRAMFS_COMPRESSION_GZIP=y\nCONFIG_RD_GZIP=y,# CONFIG_INITRAMFS_COMPRESSION_GZIP is not set\n# CONFIG_RD_GZIP is not set)" >> $(LINUX_DIR)/.config
73 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_BZIP2),CONFIG_INITRAMFS_COMPRESSION_BZIP2=y\nCONFIG_RD_BZIP2=y,# CONFIG_INITRAMFS_COMPRESSION_BZIP2 is not set\n# CONFIG_RD_BZIP2 is not set)" >> $(LINUX_DIR)/.config
74 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZMA),CONFIG_INITRAMFS_COMPRESSION_LZMA=y\nCONFIG_RD_LZMA=y,# CONFIG_INITRAMFS_COMPRESSION_LZMA is not set\n# CONFIG_RD_LZMA is not set)" >> $(LINUX_DIR)/.config
75 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZO),CONFIG_INITRAMFS_COMPRESSION_LZO=y\nCONFIG_RD_LZO=y,# CONFIG_INITRAMFS_COMPRESSION_LZO is not set\n# CONFIG_RD_LZO is not set)" >> $(LINUX_DIR)/.config
76 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_XZ),CONFIG_INITRAMFS_COMPRESSION_XZ=y\nCONFIG_RD_XZ=y,# CONFIG_INITRAMFS_COMPRESSION_XZ is not set\n# CONFIG_RD_XZ is not set)" >> $(LINUX_DIR)/.config
77 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZ4),CONFIG_INITRAMFS_COMPRESSION_LZ4=y\nCONFIG_RD_LZ4=y,# CONFIG_INITRAMFS_COMPRESSION_LZ4 is not set\n# CONFIG_RD_LZ4 is not set)" >> $(LINUX_DIR)/.config
78 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_ZSTD),CONFIG_INITRAMFS_COMPRESSION_ZSTD=y\nCONFIG_RD_STD=y,# CONFIG_INITRAMFS_COMPRESSION_ZSTD is not set\n# CONFIG_RD_ZSTD is not set)" >> $(LINUX_DIR)/.config
79 endef
80 else
81 endif
82
83 define Kernel/SetNoInitramfs
84 mv $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.old
85 grep -v INITRAMFS $(LINUX_DIR)/.config.old > $(LINUX_DIR)/.config.set
86 echo 'CONFIG_INITRAMFS_SOURCE=""' >> $(LINUX_DIR)/.config.set
87 echo '# CONFIG_INITRAMFS_FORCE is not set' >> $(LINUX_DIR)/.config.set
88 endef
89
90 define Kernel/Configure/Default
91 rm -f $(LINUX_DIR)/localversion
92 $(LINUX_CONF_CMD) > $(LINUX_DIR)/.config.target
93 # copy CONFIG_KERNEL_* settings over to .config.target
94 awk '/^(#[[:space:]]+)?CONFIG_KERNEL/{sub("CONFIG_KERNEL_","CONFIG_");print}' $(TOPDIR)/.config >> $(LINUX_DIR)/.config.target
95 echo "# CONFIG_KALLSYMS_EXTRA_PASS is not set" >> $(LINUX_DIR)/.config.target
96 echo "# CONFIG_KALLSYMS_ALL is not set" >> $(LINUX_DIR)/.config.target
97 echo "CONFIG_KALLSYMS_UNCOMPRESSED=y" >> $(LINUX_DIR)/.config.target
98 $(SCRIPT_DIR)/package-metadata.pl kconfig $(TMP_DIR)/.packageinfo $(TOPDIR)/.config $(KERNEL_PATCHVER) > $(LINUX_DIR)/.config.override
99 $(SCRIPT_DIR)/kconfig.pl 'm+' '+' $(LINUX_DIR)/.config.target /dev/null $(LINUX_DIR)/.config.override > $(LINUX_DIR)/.config.set
100 $(call Kernel/SetNoInitramfs)
101 rm -rf $(KERNEL_BUILD_DIR)/modules
102 cmp -s $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.prev || { \
103 cp $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config; \
104 cp $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.prev; \
105 }
106 $(_SINGLE) [ -d $(LINUX_DIR)/user_headers ] || $(KERNEL_MAKE) INSTALL_HDR_PATH=$(LINUX_DIR)/user_headers headers_install
107 grep '=[ym]' $(LINUX_DIR)/.config.set | LC_ALL=C sort | mkhash md5 > $(LINUX_DIR)/.vermagic
108 endef
109
110 define Kernel/Configure/Initramfs
111 $(call Kernel/SetInitramfs)
112 endef
113
114 define Kernel/CompileModules/Default
115 rm -f $(LINUX_DIR)/vmlinux $(LINUX_DIR)/System.map
116 +$(KERNEL_MAKE) modules
117 endef
118
119 OBJCOPY_STRIP = -R .reginfo -R .notes -R .note -R .comment -R .mdebug -R .note.gnu.build-id
120
121 # AMD64 shares the location with x86
122 ifeq ($(LINUX_KARCH),x86_64)
123 IMAGES_DIR:=../../x86/boot
124 endif
125
126 define Kernel/CopyImage
127 cmp -s $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/vmlinux$(1).debug || { \
128 $(KERNEL_CROSS)objcopy -O binary $(OBJCOPY_STRIP) -S $(LINUX_DIR)/vmlinux $(LINUX_KERNEL)$(1); \
129 $(KERNEL_CROSS)objcopy $(OBJCOPY_STRIP) -S $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/vmlinux$(1).elf; \
130 $(CP) $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/vmlinux$(1).debug; \
131 $(foreach k, \
132 $(if $(KERNEL_IMAGES),$(KERNEL_IMAGES),$(filter-out vmlinux dtbs,$(KERNELNAME))), \
133 $(CP) $(LINUX_DIR)/arch/$(LINUX_KARCH)/boot/$(IMAGES_DIR)/$(k) $(KERNEL_BUILD_DIR)/$(k)$(1); \
134 ) \
135 }
136 endef
137
138 define Kernel/CompileImage/Default
139 rm -f $(TARGET_DIR)/init
140 +$(KERNEL_MAKE) $(if $(KERNELNAME),$(KERNELNAME),all) modules
141 $(call Kernel/CopyImage)
142 endef
143
144 ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
145 define Kernel/CompileImage/Initramfs
146 $(call Kernel/Configure/Initramfs)
147 $(CP) $(GENERIC_PLATFORM_DIR)/other-files/init $(TARGET_DIR)/init
148 $(if $(SOURCE_DATE_EPOCH),touch -hcd "@$(SOURCE_DATE_EPOCH)" $(TARGET_DIR)/init)
149 rm -rf $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)/usr/initramfs_data.cpio*
150 +$(KERNEL_MAKE) $(if $(KERNELNAME),$(KERNELNAME),all) modules
151 $(call Kernel/CopyImage,-initramfs)
152 endef
153 else
154 define Kernel/CompileImage/Initramfs
155 endef
156 endif
157
158 define Kernel/Clean/Default
159 rm -f $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)/.configured
160 rm -f $(LINUX_KERNEL)
161 $(_SINGLE)$(MAKE) -C $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION) clean
162 endef
163
164