kernel.mk 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # SPDX-License-Identifier: GPL-2.0-only
  2. #
  3. # Copyright (C) 2006-2020 OpenWrt.org
  4. ifneq ($(filter check,$(MAKECMDGOALS)),)
  5. CHECK:=1
  6. DUMP:=1
  7. endif
  8. ifneq ($(SOURCE_DATE_EPOCH),)
  9. ifndef DUMP
  10. KBUILD_BUILD_TIMESTAMP:=$(shell perl -e 'print scalar gmtime($(SOURCE_DATE_EPOCH))')
  11. endif
  12. endif
  13. ifeq ($(__target_inc),)
  14. ifndef CHECK
  15. include $(INCLUDE_DIR)/target.mk
  16. endif
  17. endif
  18. ifeq ($(DUMP),1)
  19. KERNEL?=<KERNEL>
  20. BOARD?=<BOARD>
  21. LINUX_VERSION?=<LINUX_VERSION>
  22. LINUX_VERMAGIC?=<LINUX_VERMAGIC>
  23. else
  24. ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
  25. export GCC_HONOUR_COPTS=s
  26. endif
  27. LINUX_KMOD_SUFFIX=ko
  28. ifneq (,$(findstring uml,$(BOARD)))
  29. KERNEL_CC?=$(HOSTCC)
  30. KERNEL_CROSS?=
  31. else
  32. KERNEL_CC?=$(TARGET_CC)
  33. KERNEL_CROSS?=$(TARGET_CROSS)
  34. endif
  35. ifeq ($(TARGET_BUILD),1)
  36. PATCH_DIR ?= $(CURDIR)/patches$(if $(wildcard ./patches-$(KERNEL_PATCHVER)),-$(KERNEL_PATCHVER))
  37. FILES_DIR ?= $(foreach dir,$(wildcard $(CURDIR)/files $(CURDIR)/files-$(KERNEL_PATCHVER)),"$(dir)")
  38. endif
  39. KERNEL_BUILD_DIR ?= $(BUILD_DIR)/linux-$(BOARD)$(if $(SUBTARGET),_$(SUBTARGET))
  40. LINUX_DIR ?= $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)
  41. LINUX_UAPI_DIR=uapi/
  42. LINUX_VERMAGIC:=$(strip $(shell cat $(LINUX_DIR)/.vermagic 2>/dev/null))
  43. LINUX_VERMAGIC:=$(if $(LINUX_VERMAGIC),$(LINUX_VERMAGIC),unknown)
  44. LINUX_UNAME_VERSION:=$(KERNEL_BASE)
  45. ifneq ($(findstring -rc,$(LINUX_VERSION)),)
  46. LINUX_UNAME_VERSION:=$(LINUX_UNAME_VERSION)-$(strip $(lastword $(subst -, ,$(LINUX_VERSION))))
  47. endif
  48. LINUX_KERNEL:=$(KERNEL_BUILD_DIR)/vmlinux
  49. ifneq (,$(findstring -rc,$(LINUX_VERSION)))
  50. LINUX_SOURCE:=linux-libre-$(LINUX_VERSION)-gnu$(LIBRE_REV).tar.gz
  51. else
  52. LINUX_SOURCE:=linux-libre-$(LINUX_VERSION)-gnu$(LIBRE_REV).tar.xz
  53. endif
  54. ifneq (,$(findstring -rc,$(LINUX_VERSION)))
  55. LINUX_SITE:=
  56. else ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
  57. LINUX_SITE:=@KERNEL_LIBRE/$(LINUX_VERSION)-gnu$(LIBRE_REV)$(TESTING)
  58. else
  59. LINUX_UNAME_VERSION:=$(strip $(shell cat $(LINUX_DIR)/include/config/kernel.release 2>/dev/null))
  60. endif
  61. MODULES_SUBDIR:=lib/modules/$(LINUX_UNAME_VERSION)
  62. TARGET_MODULES_DIR:=$(LINUX_TARGET_DIR)/$(MODULES_SUBDIR)
  63. ifneq ($(TARGET_BUILD),1)
  64. PKG_BUILD_DIR ?= $(KERNEL_BUILD_DIR)/$(if $(BUILD_VARIANT),$(PKG_NAME)-$(BUILD_VARIANT)/)$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
  65. endif
  66. endif
  67. ifneq (,$(findstring uml,$(BOARD)))
  68. LINUX_KARCH=um
  69. else ifneq (,$(findstring $(ARCH) , aarch64 aarch64_be ))
  70. LINUX_KARCH := arm64
  71. else ifneq (,$(findstring $(ARCH) , arceb ))
  72. LINUX_KARCH := arc
  73. else ifneq (,$(findstring $(ARCH) , armeb ))
  74. LINUX_KARCH := arm
  75. else ifneq (,$(findstring $(ARCH) , mipsel mips64 mips64el ))
  76. LINUX_KARCH := mips
  77. else ifneq (,$(findstring $(ARCH) , powerpc64 ))
  78. LINUX_KARCH := powerpc
  79. else ifneq (,$(findstring $(ARCH) , riscv64 ))
  80. LINUX_KARCH := riscv
  81. else ifneq (,$(findstring $(ARCH) , sh2 sh3 sh4 ))
  82. LINUX_KARCH := sh
  83. else ifneq (,$(findstring $(ARCH) , i386 x86_64 ))
  84. LINUX_KARCH := x86
  85. else
  86. LINUX_KARCH := $(ARCH)
  87. endif
  88. KERNEL_MAKE = $(MAKE) $(KERNEL_MAKEOPTS)
  89. KERNEL_MAKE_FLAGS = \
  90. KCFLAGS="$(call iremap,$(BUILD_DIR),$(notdir $(BUILD_DIR))) $(filter-out -fno-plt,$(call qstrip,$(CONFIG_EXTRA_OPTIMIZATION))) $(call qstrip,$(CONFIG_KERNEL_CFLAGS))" \
  91. HOSTCFLAGS="$(HOST_CFLAGS) -Wall -Wmissing-prototypes -Wstrict-prototypes" \
  92. CROSS_COMPILE="$(KERNEL_CROSS)" \
  93. ARCH="$(LINUX_KARCH)" \
  94. KBUILD_HAVE_NLS=no \
  95. KBUILD_BUILD_USER="$(call qstrip,$(CONFIG_KERNEL_BUILD_USER))" \
  96. KBUILD_BUILD_HOST="$(call qstrip,$(CONFIG_KERNEL_BUILD_DOMAIN))" \
  97. KBUILD_BUILD_TIMESTAMP="$(KBUILD_BUILD_TIMESTAMP)" \
  98. KBUILD_BUILD_VERSION="0" \
  99. KBUILD_HOSTLDFLAGS="-L$(STAGING_DIR_HOST)/lib" \
  100. CONFIG_SHELL="$(BASH)" \
  101. $(if $(findstring c,$(LIBRECMC_VERBOSE)),V=1,V='') \
  102. $(if $(PKG_BUILD_ID),LDFLAGS_MODULE=--build-id=0x$(PKG_BUILD_ID)) \
  103. cmd_syscalls= \
  104. $(if $(__package_mk),KBUILD_EXTRA_SYMBOLS="$(wildcard $(PKG_SYMVERS_DIR)/*.symvers)")
  105. KERNEL_NOSTDINC_FLAGS = \
  106. -nostdinc $(if $(DUMP),, -isystem $(shell $(TARGET_CC) -print-file-name=include))
  107. ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
  108. KERNEL_MAKE_FLAGS += \
  109. KERNELRELEASE=$(LINUX_VERSION)
  110. endif
  111. ifneq ($(HOST_OS),Linux)
  112. KERNEL_MAKE_FLAGS += CONFIG_STACK_VALIDATION=
  113. export SKIP_STACK_VALIDATION:=1
  114. endif
  115. KERNEL_MAKEOPTS = -C $(LINUX_DIR) $(KERNEL_MAKE_FLAGS)
  116. ifdef CONFIG_USE_SPARSE
  117. KERNEL_MAKEOPTS += C=1 CHECK=$(STAGING_DIR_HOST)/bin/sparse
  118. endif
  119. PKG_EXTMOD_SUBDIRS ?= .
  120. PKG_SYMVERS_DIR = $(KERNEL_BUILD_DIR)/symvers
  121. define collect_module_symvers
  122. for subdir in $(PKG_EXTMOD_SUBDIRS); do \
  123. realdir=$$$$(readlink -f $(PKG_BUILD_DIR)); \
  124. grep -F $(PKG_BUILD_DIR) $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers >> $(PKG_BUILD_DIR)/Module.symvers.tmp; \
  125. [ "$(PKG_BUILD_DIR)" = "$$$$realdir" ] || \
  126. grep -F $$$$realdir $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers >> $(PKG_BUILD_DIR)/Module.symvers.tmp; \
  127. done; \
  128. sort -u $(PKG_BUILD_DIR)/Module.symvers.tmp > $(PKG_BUILD_DIR)/Module.symvers; \
  129. mkdir -p $(PKG_SYMVERS_DIR); \
  130. mv $(PKG_BUILD_DIR)/Module.symvers $(PKG_SYMVERS_DIR)/$(PKG_NAME).symvers
  131. endef
  132. define KernelPackage/hooks
  133. ifneq ($(PKG_NAME),kernel)
  134. Hooks/Compile/Post += collect_module_symvers
  135. endif
  136. define KernelPackage/hooks
  137. endef
  138. endef
  139. define KernelPackage/Defaults
  140. FILES:=
  141. AUTOLOAD:=
  142. MODPARAMS:=
  143. PKGFLAGS+=nonshared
  144. endef
  145. # 1: name
  146. # 2: install prefix
  147. # 3: module priority prefix
  148. # 4: required for boot
  149. # 5: module list
  150. define ModuleAutoLoad
  151. $(if $(5), \
  152. mkdir -p $(2)/etc/modules.d; \
  153. ($(foreach mod,$(5), \
  154. echo "$(mod)$(if $(MODPARAMS.$(mod)), $(MODPARAMS.$(mod)),$(if $(MODPARAMS), $(MODPARAMS)))"; )) > $(2)/etc/modules.d/$(3)$(1); \
  155. $(if $(4), \
  156. mkdir -p $(2)/etc/modules-boot.d; \
  157. ln -sf ../modules.d/$(3)$(1) $(2)/etc/modules-boot.d/;))
  158. endef
  159. ifeq ($(DUMP)$(TARGET_BUILD),)
  160. -include $(LINUX_DIR)/.config
  161. endif
  162. define KernelPackage/depends
  163. $(STAMP_BUILT): $(LINUX_DIR)/.config
  164. define KernelPackage/depends
  165. endef
  166. endef
  167. define KernelPackage
  168. NAME:=$(1)
  169. $(eval $(call Package/Default))
  170. $(eval $(call KernelPackage/Defaults))
  171. $(eval $(call KernelPackage/$(1)))
  172. $(eval $(call KernelPackage/$(1)/$(BOARD)))
  173. $(eval $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic)))
  174. define Package/kmod-$(1)
  175. TITLE:=$(TITLE)
  176. SECTION:=kernel
  177. CATEGORY:=Kernel modules
  178. DESCRIPTION:=$(DESCRIPTION)
  179. EXTRA_DEPENDS:=kernel (=$(LINUX_VERSION)-$(LINUX_RELEASE)-$(LINUX_VERMAGIC))
  180. VERSION:=$(LINUX_VERSION)$(if $(PKG_VERSION),+$(PKG_VERSION))-$(if $(PKG_RELEASE),$(PKG_RELEASE),$(LINUX_RELEASE))
  181. PKGFLAGS:=$(PKGFLAGS)
  182. $(call KernelPackage/$(1))
  183. $(call KernelPackage/$(1)/$(BOARD))
  184. $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic))
  185. endef
  186. ifdef KernelPackage/$(1)/conffiles
  187. define Package/kmod-$(1)/conffiles
  188. $(call KernelPackage/$(1)/conffiles)
  189. endef
  190. endif
  191. ifdef KernelPackage/$(1)/description
  192. define Package/kmod-$(1)/description
  193. $(call KernelPackage/$(1)/description)
  194. endef
  195. endif
  196. ifdef KernelPackage/$(1)/config
  197. define Package/kmod-$(1)/config
  198. $(call KernelPackage/$(1)/config)
  199. endef
  200. endif
  201. $(call KernelPackage/depends)
  202. $(call KernelPackage/hooks)
  203. ifneq ($(if $(filter-out %=y %=n %=m,$(KCONFIG)),$(filter m y,$(foreach c,$(call version_filter,$(filter-out %=y %=n %=m,$(KCONFIG))),$($(c)))),.),)
  204. define Package/kmod-$(1)/install
  205. @for mod in $$(call version_filter,$$(FILES)); do \
  206. if grep -q "$$$$$$$${mod##$(LINUX_DIR)/}" "$(LINUX_DIR)/modules.builtin"; then \
  207. echo "NOTICE: module '$$$$$$$$mod' is built-in."; \
  208. elif [ -e $$$$$$$$mod ]; then \
  209. mkdir -p $$(1)/$(MODULES_SUBDIR) ; \
  210. $(CP) -L $$$$$$$$mod $$(1)/$(MODULES_SUBDIR)/ ; \
  211. else \
  212. echo "ERROR: module '$$$$$$$$mod' is missing." >&2; \
  213. exit 1; \
  214. fi; \
  215. done;
  216. $(call ModuleAutoLoad,$(1),$$(1),$(filter-out 0-,$(word 1,$(AUTOLOAD))-),$(filter-out 0,$(word 2,$(AUTOLOAD))),$(sort $(wordlist 3,99,$(AUTOLOAD))))
  217. $(call KernelPackage/$(1)/install,$$(1))
  218. endef
  219. $(if $(CONFIG_PACKAGE_kmod-$(1)),
  220. else
  221. compile: $(1)-disabled
  222. $(1)-disabled:
  223. @echo "WARNING: kmod-$(1) is not available in the kernel config - generating empty package" >&2
  224. define Package/kmod-$(1)/install
  225. true
  226. endef
  227. )
  228. endif
  229. $$(eval $$(call BuildPackage,kmod-$(1)))
  230. $$(IPKG_kmod-$(1)): $$(wildcard $$(call version_filter,$$(FILES)))
  231. endef
  232. version_filter=$(if $(findstring @,$(1)),$(shell $(SCRIPT_DIR)/package-metadata.pl version_filter $(KERNEL_PATCHVER) $(1)),$(1))
  233. # 1: priority (optional)
  234. # 2: module list
  235. # 3: boot flag
  236. define AutoLoad
  237. $(if $(1),$(1),0) $(if $(3),1,0) $(call version_filter,$(2))
  238. endef
  239. # 1: module list
  240. # 2: boot flag
  241. define AutoProbe
  242. $(call AutoLoad,,$(1),$(2))
  243. endef
  244. version_field=$(if $(word $(1),$(2)),$(word $(1),$(2)),0)
  245. kernel_version_merge=$$(( ($(call version_field,1,$(1)) << 24) + ($(call version_field,2,$(1)) << 16) + ($(call version_field,3,$(1)) << 8) + $(call version_field,4,$(1)) ))
  246. ifdef DUMP
  247. kernel_version_cmp=
  248. else
  249. kernel_version_cmp=$(shell [ $(call kernel_version_merge,$(call split_version,$(2))) $(1) $(call kernel_version_merge,$(call split_version,$(3))) ] && echo 1 )
  250. endif
  251. CompareKernelPatchVer=$(if $(call kernel_version_cmp,-$(2),$(1),$(3)),1,0)
  252. kernel_patchver_gt=$(call kernel_version_cmp,-gt,$(KERNEL_PATCHVER),$(1))
  253. kernel_patchver_ge=$(call kernel_version_cmp,-ge,$(KERNEL_PATCHVER),$(1))
  254. kernel_patchver_eq=$(call kernel_version_cmp,-eq,$(KERNEL_PATCHVER),$(1))
  255. kernel_patchver_le=$(call kernel_version_cmp,-le,$(KERNEL_PATCHVER),$(1))
  256. kernel_patchver_lt=$(call kernel_version_cmp,-lt,$(KERNEL_PATCHVER),$(1))