kernel.mk 8.9 KB

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