kernel-defaults.mk 9.6 KB

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