1
0

kernel.mk 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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-libre-$(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:=
  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)/$(if $(BUILD_VARIANT),$(PKG_NAME)-$(BUILD_VARIANT)/)$(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. ifeq ($(HOST_OS),Darwin)
  113. export SKIP_STACK_VALIDATION:=1
  114. endif
  115. PKG_EXTMOD_SUBDIRS ?= .
  116. define populate_module_symvers
  117. @mkdir -p $(PKG_INFO_DIR)
  118. cat /dev/null > $(PKG_INFO_DIR)/$(PKG_NAME).symvers; \
  119. for subdir in $(PKG_EXTMOD_SUBDIRS); do \
  120. cat $(PKG_INFO_DIR)/*.symvers 2>/dev/null > $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers; \
  121. done
  122. endef
  123. define collect_module_symvers
  124. for subdir in $(PKG_EXTMOD_SUBDIRS); do \
  125. realdir=$$$$(readlink -f $(PKG_BUILD_DIR)); \
  126. grep -F $(PKG_BUILD_DIR) $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers >> $(PKG_BUILD_DIR)/Module.symvers.tmp; \
  127. [ "$(PKG_BUILD_DIR)" = "$$$$realdir" ] || \
  128. grep -F $$$$realdir $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers >> $(PKG_BUILD_DIR)/Module.symvers.tmp; \
  129. done; \
  130. sort -u $(PKG_BUILD_DIR)/Module.symvers.tmp > $(PKG_BUILD_DIR)/Module.symvers; \
  131. mv $(PKG_BUILD_DIR)/Module.symvers $(PKG_INFO_DIR)/$(PKG_NAME).symvers
  132. endef
  133. define KernelPackage/hooks
  134. ifneq ($(PKG_NAME),kernel)
  135. Hooks/Compile/Pre += populate_module_symvers
  136. Hooks/Compile/Post += collect_module_symvers
  137. endif
  138. define KernelPackage/hooks
  139. endef
  140. endef
  141. define KernelPackage/Defaults
  142. FILES:=
  143. AUTOLOAD:=
  144. MODPARAMS:=
  145. PKGFLAGS+=nonshared
  146. endef
  147. # 1: name
  148. # 2: install prefix
  149. # 3: module priority prefix
  150. # 4: required for boot
  151. # 5: module list
  152. define ModuleAutoLoad
  153. $(if $(5), \
  154. mkdir -p $(2)/etc/modules.d; \
  155. ($(foreach mod,$(5), \
  156. echo "$(mod)$(if $(MODPARAMS.$(mod)), $(MODPARAMS.$(mod)),$(if $(MODPARAMS), $(MODPARAMS)))"; )) > $(2)/etc/modules.d/$(3)$(1); \
  157. $(if $(4), \
  158. mkdir -p $(2)/etc/modules-boot.d; \
  159. ln -sf ../modules.d/$(3)$(1) $(2)/etc/modules-boot.d/;))
  160. endef
  161. ifeq ($(DUMP)$(TARGET_BUILD),)
  162. -include $(LINUX_DIR)/.config
  163. endif
  164. define KernelPackage/depends
  165. $(STAMP_BUILT): $(LINUX_DIR)/.config
  166. define KernelPackage/depends
  167. endef
  168. endef
  169. define KernelPackage
  170. NAME:=$(1)
  171. $(eval $(call Package/Default))
  172. $(eval $(call KernelPackage/Defaults))
  173. $(eval $(call KernelPackage/$(1)))
  174. $(eval $(call KernelPackage/$(1)/$(BOARD)))
  175. $(eval $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic)))
  176. define Package/kmod-$(1)
  177. TITLE:=$(TITLE)
  178. SECTION:=kernel
  179. CATEGORY:=Kernel modules
  180. DESCRIPTION:=$(DESCRIPTION)
  181. EXTRA_DEPENDS:=kernel (=$(LINUX_VERSION)-$(LINUX_RELEASE)-$(LINUX_VERMAGIC))
  182. VERSION:=$(LINUX_VERSION)$(if $(PKG_VERSION),+$(PKG_VERSION))-$(if $(PKG_RELEASE),$(PKG_RELEASE),$(LINUX_RELEASE))
  183. PKGFLAGS:=$(PKGFLAGS)
  184. $(call KernelPackage/$(1))
  185. $(call KernelPackage/$(1)/$(BOARD))
  186. $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic))
  187. endef
  188. ifdef KernelPackage/$(1)/conffiles
  189. define Package/kmod-$(1)/conffiles
  190. $(call KernelPackage/$(1)/conffiles)
  191. endef
  192. endif
  193. ifdef KernelPackage/$(1)/description
  194. define Package/kmod-$(1)/description
  195. $(call KernelPackage/$(1)/description)
  196. endef
  197. endif
  198. ifdef KernelPackage/$(1)/config
  199. define Package/kmod-$(1)/config
  200. $(call KernelPackage/$(1)/config)
  201. endef
  202. endif
  203. $(call KernelPackage/depends)
  204. $(call KernelPackage/hooks)
  205. ifneq ($(if $(filter-out %=y %=n %=m,$(KCONFIG)),$(filter m y,$(foreach c,$(filter-out %=y %=n %=m,$(KCONFIG)),$($(c)))),.),)
  206. define Package/kmod-$(1)/install
  207. @for mod in $$(call version_filter,$$(FILES)); do \
  208. if grep -q "$$$$$$$${mod##$(LINUX_DIR)/}" "$(LINUX_DIR)/modules.builtin"; then \
  209. echo "NOTICE: module '$$$$$$$$mod' is built-in."; \
  210. elif [ -e $$$$$$$$mod ]; then \
  211. mkdir -p $$(1)/$(MODULES_SUBDIR) ; \
  212. $(CP) -L $$$$$$$$mod $$(1)/$(MODULES_SUBDIR)/ ; \
  213. else \
  214. echo "ERROR: module '$$$$$$$$mod' is missing." >&2; \
  215. exit 1; \
  216. fi; \
  217. done;
  218. $(call ModuleAutoLoad,$(1),$$(1),$(filter-out 0-,$(word 1,$(AUTOLOAD))-),$(filter-out 0,$(word 2,$(AUTOLOAD))),$(sort $(wordlist 3,99,$(AUTOLOAD))))
  219. $(call KernelPackage/$(1)/install,$$(1))
  220. endef
  221. $(if $(CONFIG_PACKAGE_kmod-$(1)),
  222. else
  223. compile: $(1)-disabled
  224. $(1)-disabled:
  225. @echo "WARNING: kmod-$(1) is not available in the kernel config - generating empty package" >&2
  226. define Package/kmod-$(1)/install
  227. true
  228. endef
  229. )
  230. endif
  231. $$(eval $$(call BuildPackage,kmod-$(1)))
  232. $$(IPKG_kmod-$(1)): $$(wildcard $$(call version_filter,$$(FILES)))
  233. endef
  234. version_filter=$(if $(findstring @,$(1)),$(shell $(SCRIPT_DIR)/package-metadata.pl version_filter $(KERNEL_PATCHVER) $(1)),$(1))
  235. # 1: priority (optional)
  236. # 2: module list
  237. # 3: boot flag
  238. define AutoLoad
  239. $(if $(1),$(1),0) $(if $(3),1,0) $(call version_filter,$(2))
  240. endef
  241. # 1: module list
  242. # 2: boot flag
  243. define AutoProbe
  244. $(call AutoLoad,,$(1),$(2))
  245. endef
  246. version_field=$(if $(word $(1),$(2)),$(word $(1),$(2)),0)
  247. 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)) ))
  248. ifdef DUMP
  249. kernel_version_cmp=
  250. else
  251. kernel_version_cmp=$(shell [ $(call kernel_version_merge,$(call split_version,$(2))) $(1) $(call kernel_version_merge,$(call split_version,$(3))) ] && echo 1 )
  252. endif
  253. CompareKernelPatchVer=$(if $(call kernel_version_cmp,-$(2),$(1),$(3)),1,0)
  254. kernel_patchver_gt=$(call kernel_version_cmp,-gt,$(KERNEL_PATCHVER),$(1))
  255. kernel_patchver_ge=$(call kernel_version_cmp,-ge,$(KERNEL_PATCHVER),$(1))
  256. kernel_patchver_eq=$(call kernel_version_cmp,-eq,$(KERNEL_PATCHVER),$(1))
  257. kernel_patchver_le=$(call kernel_version_cmp,-le,$(KERNEL_PATCHVER),$(1))
  258. kernel_patchver_lt=$(call kernel_version_cmp,-lt,$(KERNEL_PATCHVER),$(1))