kernel.mk 9.0 KB

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