1
0

kernel-build.mk 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. #
  3. # Copyright (C) 2006-2020 OpenWrt.org
  4. include $(INCLUDE_DIR)/prereq.mk
  5. include $(INCLUDE_DIR)/depends.mk
  6. ifneq ($(DUMP),1)
  7. all: compile
  8. endif
  9. KERNEL_FILE_DEPENDS=$(GENERIC_BACKPORT_DIR) $(GENERIC_PATCH_DIR) $(GENERIC_HACK_DIR) $(PATCH_DIR) $(GENERIC_FILES_DIR) $(FILES_DIR)
  10. STAMP_PREPARED=$(LINUX_DIR)/.prepared$(if $(QUILT)$(DUMP),,_$(shell $(call $(if $(CONFIG_AUTOREMOVE),find_md5_reproducible,find_md5),$(KERNEL_FILE_DEPENDS),)))
  11. STAMP_CONFIGURED:=$(LINUX_DIR)/.configured
  12. include $(INCLUDE_DIR)/download.mk
  13. include $(INCLUDE_DIR)/quilt.mk
  14. include $(INCLUDE_DIR)/kernel-defaults.mk
  15. define Kernel/Prepare
  16. $(call Kernel/Prepare/Default)
  17. endef
  18. define Kernel/Configure
  19. $(call Kernel/Configure/Default)
  20. endef
  21. define Kernel/CompileModules
  22. $(call Kernel/CompileModules/Default)
  23. endef
  24. define Kernel/CompileImage
  25. $(call Kernel/CompileImage/Default)
  26. $(call Kernel/CompileImage/Initramfs)
  27. endef
  28. define Kernel/Clean
  29. $(call Kernel/Clean/Default)
  30. endef
  31. define Download/kernel
  32. URL:=$(LINUX_SITE)
  33. FILE:=$(LINUX_SOURCE)
  34. HASH:=$(LINUX_KERNEL_HASH)
  35. endef
  36. KERNEL_GIT_OPTS:=
  37. ifneq ($(strip $(CONFIG_KERNEL_GIT_LOCAL_REPOSITORY)),"")
  38. KERNEL_GIT_OPTS+=--reference $(CONFIG_KERNEL_GIT_LOCAL_REPOSITORY)
  39. endif
  40. define Download/git-kernel
  41. URL:=$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI))
  42. PROTO:=git
  43. VERSION:=$(CONFIG_KERNEL_GIT_REF)
  44. FILE:=$(LINUX_SOURCE)
  45. SUBDIR:=linux-$(LINUX_VERSION)
  46. OPTS:=$(KERNEL_GIT_OPTS)
  47. endef
  48. ifdef CONFIG_COLLECT_KERNEL_DEBUG
  49. define Kernel/CollectDebug
  50. rm -rf $(KERNEL_BUILD_DIR)/debug
  51. mkdir -p $(KERNEL_BUILD_DIR)/debug/modules
  52. $(CP) $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/debug/
  53. -$(CP) \
  54. $(STAGING_DIR_ROOT)/lib/modules/$(LINUX_VERSION)/* \
  55. $(KERNEL_BUILD_DIR)/debug/modules/
  56. $(FIND) $(KERNEL_BUILD_DIR)/debug -type f | $(XARGS) $(KERNEL_CROSS)strip --only-keep-debug
  57. $(TAR) c -C $(KERNEL_BUILD_DIR) debug \
  58. $(if $(SOURCE_DATE_EPOCH),--mtime="@$(SOURCE_DATE_EPOCH)") \
  59. | zstd -T0 -f -o $(BIN_DIR)/kernel-debug.tar.zst
  60. endef
  61. endif
  62. ifeq ($(DUMP)$(filter prereq clean refresh update,$(MAKECMDGOALS)),)
  63. ifneq ($(if $(QUILT),,$(CONFIG_AUTOREBUILD)),)
  64. define Kernel/Autoclean
  65. $(PKG_BUILD_DIR)/.dep_files: $(STAMP_PREPARED)
  66. $(call rdep,$(KERNEL_FILE_DEPENDS),$(STAMP_PREPARED),$(PKG_BUILD_DIR)/.dep_files,-x "*/.dep_*")
  67. endef
  68. endif
  69. endif
  70. define BuildKernel
  71. $(if $(QUILT),$(Build/Quilt))
  72. $(if $(LINUX_SITE),$(call Download,kernel))
  73. $(if $(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),$(call Download,git-kernel))
  74. .NOTPARALLEL:
  75. $(Kernel/Autoclean)
  76. $(STAMP_PREPARED): $(if $(LINUX_SITE),$(DL_DIR)/$(LINUX_SOURCE))
  77. -rm -rf $(KERNEL_BUILD_DIR)
  78. -mkdir -p $(KERNEL_BUILD_DIR)
  79. $(Kernel/Prepare)
  80. touch $$@
  81. $(KERNEL_BUILD_DIR)/symtab.h: FORCE
  82. rm -f $(KERNEL_BUILD_DIR)/symtab.h
  83. touch $(KERNEL_BUILD_DIR)/symtab.h
  84. +$(KERNEL_MAKE) vmlinux
  85. find $(LINUX_DIR) $(STAGING_DIR_ROOT)/lib/modules -name \*.ko | \
  86. xargs $(TARGET_CROSS)nm | \
  87. awk '$$$$1 == "U" { print $$$$2 } ' | \
  88. sort -u > $(KERNEL_BUILD_DIR)/mod_symtab.txt
  89. $(TARGET_CROSS)nm -n $(LINUX_DIR)/vmlinux.o | awk '/^[0-9a-f]+ [rR] __ksymtab_/ {print substr($$$$3,11)}' > $(KERNEL_BUILD_DIR)/kernel_symtab.txt
  90. grep -Ff $(KERNEL_BUILD_DIR)/mod_symtab.txt $(KERNEL_BUILD_DIR)/kernel_symtab.txt > $(KERNEL_BUILD_DIR)/sym_include.txt
  91. grep -Fvf $(KERNEL_BUILD_DIR)/mod_symtab.txt $(KERNEL_BUILD_DIR)/kernel_symtab.txt > $(KERNEL_BUILD_DIR)/sym_exclude.txt
  92. ( \
  93. echo '#define SYMTAB_KEEP \'; \
  94. cat $(KERNEL_BUILD_DIR)/sym_include.txt | \
  95. awk '{print "KEEP(*(___ksymtab+" $$$$1 ")) \\" }'; \
  96. echo; \
  97. echo '#define SYMTAB_KEEP_GPL \'; \
  98. cat $(KERNEL_BUILD_DIR)/sym_include.txt | \
  99. awk '{print "KEEP(*(___ksymtab_gpl+" $$$$1 ")) \\" }'; \
  100. echo; \
  101. echo '#define SYMTAB_DISCARD \'; \
  102. cat $(KERNEL_BUILD_DIR)/sym_exclude.txt | \
  103. awk '{print "*(___ksymtab+" $$$$1 ") \\" }'; \
  104. echo; \
  105. echo '#define SYMTAB_DISCARD_GPL \'; \
  106. cat $(KERNEL_BUILD_DIR)/sym_exclude.txt | \
  107. awk '{print "*(___ksymtab_gpl+" $$$$1 ") \\" }'; \
  108. echo; \
  109. ) > $$@
  110. $(STAMP_CONFIGURED): $(STAMP_PREPARED) $(LINUX_KCONFIG_LIST) $(TOPDIR)/.config FORCE
  111. $(Kernel/Configure)
  112. touch $$@
  113. $(LINUX_DIR)/.modules: export STAGING_PREFIX=$$(STAGING_DIR_HOST)
  114. $(LINUX_DIR)/.modules: export PKG_CONFIG_PATH=$$(STAGING_DIR_HOST)/lib/pkgconfig
  115. $(LINUX_DIR)/.modules: export PKG_CONFIG_LIBDIR=$$(STAGING_DIR_HOST)/lib/pkgconfig
  116. $(LINUX_DIR)/.modules: export FAIL_ON_UNCONFIGURED=1
  117. $(LINUX_DIR)/.modules: $(STAMP_CONFIGURED) $(LINUX_DIR)/.config FORCE
  118. $(Kernel/CompileModules)
  119. touch $$@
  120. $(LINUX_DIR)/.image: export STAGING_PREFIX=$$(STAGING_DIR_HOST)
  121. $(LINUX_DIR)/.image: export PKG_CONFIG_PATH=$$(STAGING_DIR_HOST)/lib/pkgconfig
  122. $(LINUX_DIR)/.image: export PKG_CONFIG_LIBDIR=$$(STAGING_DIR_HOST)/lib/pkgconfig
  123. $(LINUX_DIR)/.image: $(STAMP_CONFIGURED) $(if $(CONFIG_STRIP_KERNEL_EXPORTS),$(KERNEL_BUILD_DIR)/symtab.h) FORCE
  124. $(Kernel/CompileImage)
  125. $(Kernel/CollectDebug)
  126. touch $$@
  127. mostlyclean: FORCE
  128. $(Kernel/Clean)
  129. define BuildKernel
  130. endef
  131. download: $(if $(LINUX_SITE),$(DL_DIR)/$(LINUX_SOURCE))
  132. prepare: $(STAMP_PREPARED)
  133. compile: $(LINUX_DIR)/.modules
  134. $(MAKE) -C image compile TARGET_BUILD=
  135. dtb: $(STAMP_CONFIGURED)
  136. $(_SINGLE)$(KERNEL_MAKE) scripts_dtc
  137. $(MAKE) -C image compile-dtb TARGET_BUILD=
  138. oldconfig menuconfig nconfig xconfig: $(STAMP_PREPARED) $(STAMP_CHECKED) FORCE
  139. rm -f $(LINUX_DIR)/.config.prev
  140. rm -f $(STAMP_CONFIGURED)
  141. $(LINUX_RECONF_CMD) > $(LINUX_DIR)/.config
  142. $(_SINGLE)$(KERNEL_MAKE) \
  143. $(if $(findstring Darwin,$(HOST_OS)), \
  144. HOSTLDLIBS_mconf="-L$(STAGING_DIR_HOST)/lib -lncurses" \
  145. filechk_conf_cfg=" :" \
  146. ) \
  147. YACC=$(STAGING_DIR_HOST)/bin/bison \
  148. $$@
  149. $(call LINUX_RECONF_DIFF,$(LINUX_DIR)/.config) > $(LINUX_RECONFIG_TARGET)
  150. install: $(LINUX_DIR)/.image
  151. +$(MAKE) -C image compile install TARGET_BUILD=
  152. clean: FORCE
  153. rm -rf $(KERNEL_BUILD_DIR)
  154. image-prereq:
  155. @+$(NO_TRACE_MAKE) -s -C image prereq TARGET_BUILD=
  156. prereq: image-prereq
  157. endef