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