kernel.mk 9.1 KB

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