kernel-build.mk: add support for compiling only DTS
[openwrt/staging/stintel.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_IMAGE += \
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 export HOST_EXTRACFLAGS=-I$(STAGING_DIR_HOST)/include
13
14 # defined in quilt.mk
15 Kernel/Patch:=$(Kernel/Patch/Default)
16
17 ifneq (,$(findstring .xz,$(LINUX_SOURCE)))
18 LINUX_CAT:=xzcat
19 else
20 LINUX_CAT:=$(STAGING_DIR_HOST)/bin/libdeflate-gzip -dc
21 endif
22
23 ifeq ($(strip $(CONFIG_EXTERNAL_KERNEL_TREE)),"")
24 ifeq ($(strip $(CONFIG_KERNEL_GIT_CLONE_URI)),"")
25 define Kernel/Prepare/Default
26 $(LINUX_CAT) $(DL_DIR)/$(LINUX_SOURCE) | $(TAR) -C $(KERNEL_BUILD_DIR) $(TAR_OPTIONS)
27 $(Kernel/Patch)
28 $(if $(QUILT),touch $(LINUX_DIR)/.quilt_used)
29 endef
30 else
31 define Kernel/Prepare/Default
32 $(LINUX_CAT) $(DL_DIR)/$(LINUX_SOURCE) | $(TAR) -C $(KERNEL_BUILD_DIR) $(TAR_OPTIONS)
33 endef
34 endif
35 else
36 define Kernel/Prepare/Default
37 mkdir -p $(KERNEL_BUILD_DIR)
38 if [ -d $(LINUX_DIR) ]; then \
39 rmdir $(LINUX_DIR); \
40 fi
41 ln -s $(CONFIG_EXTERNAL_KERNEL_TREE) $(LINUX_DIR)
42 if [ -d $(LINUX_DIR)/user_headers ]; then \
43 rm -rf $(LINUX_DIR)/user_headers; \
44 fi
45 endef
46 endif
47
48 ifeq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),y)
49 ifeq ($(CONFIG_TARGET_ROOTFS_INITRAMFS_SEPARATE),y)
50 define Kernel/SetInitramfs/PreConfigure
51 grep -v -e CONFIG_BLK_DEV_INITRD $(LINUX_DIR)/.config.old > $(LINUX_DIR)/.config
52 echo 'CONFIG_BLK_DEV_INITRD=y' >> $(LINUX_DIR)/.config
53 echo 'CONFIG_INITRAMFS_SOURCE=""' >> $(LINUX_DIR)/.config
54 endef
55 else
56 ifeq ($(strip $(CONFIG_EXTERNAL_CPIO)),"")
57 define Kernel/SetInitramfs/PreConfigure
58 grep -v -e INITRAMFS -e CONFIG_RD_ -e CONFIG_BLK_DEV_INITRD $(LINUX_DIR)/.config.old > $(LINUX_DIR)/.config
59 echo 'CONFIG_BLK_DEV_INITRD=y' >> $(LINUX_DIR)/.config
60 echo 'CONFIG_INITRAMFS_SOURCE="$(strip $(TARGET_DIR) $(INITRAMFS_EXTRA_FILES))"' >> $(LINUX_DIR)/.config
61 endef
62 else
63 define Kernel/SetInitramfs/PreConfigure
64 grep -v INITRAMFS $(LINUX_DIR)/.config.old > $(LINUX_DIR)/.config
65 echo 'CONFIG_INITRAMFS_SOURCE="$(call qstrip,$(CONFIG_EXTERNAL_CPIO))"' >> $(LINUX_DIR)/.config
66 endef
67 endif
68 endif
69
70 define Kernel/SetInitramfs
71 rm -f $(LINUX_DIR)/.config.prev
72 mv $(LINUX_DIR)/.config $(LINUX_DIR)/.config.old
73 $(call Kernel/SetInitramfs/PreConfigure)
74 echo "# CONFIG_INITRAMFS_PRESERVE_MTIME is not set" >> $(LINUX_DIR)/.config
75 ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS_SEPARATE),y)
76 echo 'CONFIG_INITRAMFS_ROOT_UID=$(shell id -u)' >> $(LINUX_DIR)/.config
77 echo 'CONFIG_INITRAMFS_ROOT_GID=$(shell id -g)' >> $(LINUX_DIR)/.config
78 echo "$(if $(CONFIG_TARGET_INITRAMFS_FORCE),CONFIG_INITRAMFS_FORCE=y,# CONFIG_INITRAMFS_FORCE is not set)" >> $(LINUX_DIR)/.config
79 else
80 echo "# CONFIG_INITRAMFS_FORCE is not set" >> $(LINUX_DIR)/.config
81 endif
82 echo "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_NONE),CONFIG_INITRAMFS_COMPRESSION_NONE=y,# CONFIG_INITRAMFS_COMPRESSION_NONE is not set)" >> $(LINUX_DIR)/.config
83 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
84 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
85 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
86 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
87 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
88 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
89 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_ZSTD),CONFIG_INITRAMFS_COMPRESSION_ZSTD=y\nCONFIG_RD_ZSTD=y,# CONFIG_INITRAMFS_COMPRESSION_ZSTD is not set\n# CONFIG_RD_ZSTD is not set)" >> $(LINUX_DIR)/.config
90 endef
91 else
92 endif
93
94 define Kernel/SetNoInitramfs
95 mv $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.old
96 grep -v INITRAMFS $(LINUX_DIR)/.config.old > $(LINUX_DIR)/.config.set
97 echo 'CONFIG_INITRAMFS_SOURCE=""' >> $(LINUX_DIR)/.config.set
98 echo '# CONFIG_INITRAMFS_FORCE is not set' >> $(LINUX_DIR)/.config.set
99 echo "# CONFIG_INITRAMFS_PRESERVE_MTIME is not set" >> $(LINUX_DIR)/.config.set
100 endef
101
102 define Kernel/Configure/Default
103 rm -f $(LINUX_DIR)/localversion
104 $(LINUX_CONF_CMD) > $(LINUX_DIR)/.config.target
105 # copy CONFIG_KERNEL_* settings over to .config.target
106 awk '/^(#[[:space:]]+)?CONFIG_KERNEL/{sub("CONFIG_KERNEL_","CONFIG_");print}' $(TOPDIR)/.config >> $(LINUX_DIR)/.config.target
107 echo "# CONFIG_KALLSYMS_EXTRA_PASS is not set" >> $(LINUX_DIR)/.config.target
108 echo "# CONFIG_KALLSYMS_ALL is not set" >> $(LINUX_DIR)/.config.target
109 echo "CONFIG_KALLSYMS_UNCOMPRESSED=y" >> $(LINUX_DIR)/.config.target
110 $(SCRIPT_DIR)/package-metadata.pl kconfig $(TMP_DIR)/.packageinfo $(TOPDIR)/.config $(KERNEL_PATCHVER) > $(LINUX_DIR)/.config.override
111 $(SCRIPT_DIR)/kconfig.pl 'm+' '+' $(LINUX_DIR)/.config.target /dev/null $(LINUX_DIR)/.config.override > $(LINUX_DIR)/.config.set
112 $(call Kernel/SetNoInitramfs)
113 rm -rf $(KERNEL_BUILD_DIR)/modules
114 cmp -s $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.prev || { \
115 cp $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config; \
116 cp $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.prev; \
117 }
118 $(_SINGLE) [ -d $(LINUX_DIR)/user_headers ] || $(KERNEL_MAKE) $(if $(findstring uml,$(BOARD)),ARCH=$(ARCH)) INSTALL_HDR_PATH=$(LINUX_DIR)/user_headers headers_install
119 grep '=[ym]' $(LINUX_DIR)/.config.set | LC_ALL=C sort | $(MKHASH) md5 > $(LINUX_DIR)/.vermagic
120 endef
121
122 define Kernel/Configure/Initramfs
123 $(call Kernel/SetInitramfs)
124 endef
125
126 define Kernel/CompileModules/Default
127 rm -f $(LINUX_DIR)/vmlinux $(LINUX_DIR)/System.map
128 +$(KERNEL_MAKE) $(if $(KERNELNAME),$(KERNELNAME),all) modules
129 # If .config did not change, use the previous timestamp to avoid package rebuilds
130 cmp -s $(LINUX_DIR)/.config $(LINUX_DIR)/.config.modules.save && \
131 mv $(LINUX_DIR)/.config.modules.save $(LINUX_DIR)/.config; \
132 $(CP) $(LINUX_DIR)/.config $(LINUX_DIR)/.config.modules.save
133 endef
134
135 OBJCOPY_STRIP = -R .reginfo -R .notes -R .note -R .comment -R .mdebug -R .note.gnu.build-id
136
137 # AMD64 shares the location with x86
138 ifeq ($(LINUX_KARCH),x86_64)
139 IMAGES_DIR:=../../x86/boot
140 endif
141
142 define Kernel/CopyImage
143 cmp -s $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/vmlinux$(1).debug || { \
144 $(KERNEL_CROSS)objcopy -O binary $(OBJCOPY_STRIP) -S $(LINUX_DIR)/vmlinux $(LINUX_KERNEL)$(1); \
145 $(KERNEL_CROSS)objcopy $(OBJCOPY_STRIP) -S $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/vmlinux$(1).elf; \
146 $(CP) $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/vmlinux$(1).debug; \
147 $(foreach k, \
148 $(if $(KERNEL_IMAGES),$(KERNEL_IMAGES),$(filter-out vmlinux dtbs,$(KERNELNAME))), \
149 $(CP) $(LINUX_DIR)/arch/$(LINUX_KARCH)/boot/$(IMAGES_DIR)/$(k) $(KERNEL_BUILD_DIR)/$(k)$(1); \
150 ) \
151 }
152 endef
153
154 define Kernel/CompileImage/Default
155 rm -f $(TARGET_DIR)/init
156 +$(KERNEL_MAKE) $(KERNEL_MAKEOPTS_IMAGE) $(if $(KERNELNAME),$(KERNELNAME),all)
157 $(call Kernel/CopyImage)
158 endef
159
160 ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
161 define Kernel/CompileImage/Initramfs
162 $(call Kernel/Configure/Initramfs)
163 $(CP) $(GENERIC_PLATFORM_DIR)/other-files/init $(TARGET_DIR)/init
164 $(if $(SOURCE_DATE_EPOCH),touch -hcd "@$(SOURCE_DATE_EPOCH)" $(TARGET_DIR) $(TARGET_DIR)/init)
165 rm -rf $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)/usr/initramfs_data.cpio*
166 ifeq ($(CONFIG_TARGET_ROOTFS_INITRAMFS_SEPARATE),y)
167 ifneq ($(call qstrip,$(CONFIG_EXTERNAL_CPIO)),)
168 $(CP) $(CONFIG_EXTERNAL_CPIO) $(KERNEL_BUILD_DIR)/initrd.cpio
169 else
170 ( cd $(TARGET_DIR); find . | LC_ALL=C sort | $(STAGING_DIR_HOST)/bin/cpio --reproducible -o -H newc -R 0:0 > $(KERNEL_BUILD_DIR)/initrd.cpio )
171 endif
172 $(if $(SOURCE_DATE_EPOCH),touch -hcd "@$(SOURCE_DATE_EPOCH)" $(KERNEL_BUILD_DIR)/initrd.cpio)
173 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_BZIP2),$(STAGING_DIR_HOST)/bin/bzip2 -9 -c < $(KERNEL_BUILD_DIR)/initrd.cpio > $(KERNEL_BUILD_DIR)/initrd.cpio.bzip2)
174 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_GZIP),$(STAGING_DIR_HOST)/bin/libdeflate-gzip -n -f -S .gzip -12 $(KERNEL_BUILD_DIR)/initrd.cpio)
175 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZ4),$(STAGING_DIR_HOST)/bin/lz4c -l -c1 -fz --favor-decSpeed $(KERNEL_BUILD_DIR)/initrd.cpio)
176 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZMA),$(STAGING_DIR_HOST)/bin/lzma e -lc1 -lp2 -pb2 $(KERNEL_BUILD_DIR)/initrd.cpio $(KERNEL_BUILD_DIR)/initrd.cpio.lzma)
177 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZO),$(STAGING_DIR_HOST)/bin/lzop -9 -f $(KERNEL_BUILD_DIR)/initrd.cpio)
178 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_XZ),$(STAGING_DIR_HOST)/bin/xz -T$(if $(filter 1,$(NPROC)),2,0) -9 -fz --check=crc32 $(KERNEL_BUILD_DIR)/initrd.cpio)
179 $(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_ZSTD),$(STAGING_DIR_HOST)/bin/zstd -T0 -f -o $(KERNEL_BUILD_DIR)/initrd.cpio.zstd $(KERNEL_BUILD_DIR)/initrd.cpio)
180 endif
181 +$(KERNEL_MAKE) $(KERNEL_MAKEOPTS_IMAGE) $(if $(KERNELNAME),$(KERNELNAME),all)
182 $(call Kernel/CopyImage,-initramfs)
183 endef
184 else
185 define Kernel/CompileImage/Initramfs
186 endef
187 endif
188
189 define Kernel/Clean/Default
190 rm -f $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)/.configured
191 rm -f $(LINUX_KERNEL)
192 $(_SINGLE)$(MAKE) -C $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION) clean
193 endef