kernel.mk 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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:=$(if $(word 3,$(subst ., ,$(KERNEL_BASE))),$(KERNEL_BASE),$(KERNEL_BASE).0)
  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-$(LINUX_VERSION).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/linux/kernel/v$(word 1,$(subst ., ,$(KERNEL_BASE))).x$(TESTING)
  56. else
  57. LINUX_UNAME_VERSION:=$(strip $(shell cat $(LINUX_DIR)/include/config/kernel.release))
  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) , sh2 sh3 sh4 ))
  76. LINUX_KARCH := sh
  77. else ifneq (,$(findstring $(ARCH) , i386 x86_64 ))
  78. LINUX_KARCH := x86
  79. else
  80. LINUX_KARCH := $(ARCH)
  81. endif
  82. KERNEL_MAKE = $(MAKE) $(KERNEL_MAKEOPTS)
  83. KERNEL_MAKE_FLAGS := \
  84. HOSTCFLAGS="$(HOST_CFLAGS) -Wall -Wmissing-prototypes -Wstrict-prototypes" \
  85. CROSS_COMPILE="$(KERNEL_CROSS)" \
  86. ARCH="$(LINUX_KARCH)" \
  87. KBUILD_HAVE_NLS=no \
  88. KBUILD_BUILD_USER="$(call qstrip,$(CONFIG_KERNEL_BUILD_USER))" \
  89. KBUILD_BUILD_HOST="$(call qstrip,$(CONFIG_KERNEL_BUILD_DOMAIN))" \
  90. KBUILD_BUILD_TIMESTAMP="$(KBUILD_BUILD_TIMESTAMP)" \
  91. KBUILD_BUILD_VERSION="0" \
  92. HOST_LOADLIBES="-L$(STAGING_DIR_HOST)/lib" \
  93. CONFIG_SHELL="$(BASH)" \
  94. $(if $(findstring c,$(OPENWRT_VERBOSE)),V=1,V='') \
  95. $(if $(PKG_BUILD_ID),LDFLAGS_MODULE=--build-id=0x$(PKG_BUILD_ID)) \
  96. cmd_syscalls=
  97. ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
  98. KERNEL_MAKE_FLAGS += \
  99. KERNELRELEASE=$(LINUX_VERSION)
  100. endif
  101. KERNEL_MAKEOPTS := -C $(LINUX_DIR) $(KERNEL_MAKE_FLAGS)
  102. ifdef CONFIG_USE_SPARSE
  103. KERNEL_MAKEOPTS += C=1 CHECK=$(STAGING_DIR_HOST)/bin/sparse
  104. endif
  105. define KernelPackage/Defaults
  106. FILES:=
  107. AUTOLOAD:=
  108. PKGFLAGS+=nonshared
  109. endef
  110. define ModuleAutoLoad
  111. $(SH_FUNC) \
  112. export modules=; \
  113. probe_module() { \
  114. local mods="$$$$$$$$1"; \
  115. local boot="$$$$$$$$2"; \
  116. local mod; \
  117. shift 2; \
  118. for mod in $$$$$$$$mods; do \
  119. mkdir -p $(2)/etc/modules.d; \
  120. echo "$$$$$$$$mod" >> $(2)/etc/modules.d/$(1); \
  121. done; \
  122. if [ -e $(2)/etc/modules.d/$(1) ]; then \
  123. if [ "$$$$$$$$boot" = "1" -a ! -e $(2)/etc/modules-boot.d/$(1) ]; then \
  124. mkdir -p $(2)/etc/modules-boot.d; \
  125. ln -s ../modules.d/$(1) $(2)/etc/modules-boot.d/; \
  126. fi; \
  127. modules="$$$$$$$${modules:+$$$$$$$$modules }$$$$$$$$mods"; \
  128. fi; \
  129. }; \
  130. add_module() { \
  131. local priority="$$$$$$$$1"; \
  132. local mods="$$$$$$$$2"; \
  133. local boot="$$$$$$$$3"; \
  134. local mod; \
  135. shift 3; \
  136. for mod in $$$$$$$$mods; do \
  137. mkdir -p $(2)/etc/modules.d; \
  138. echo "$$$$$$$$mod" >> $(2)/etc/modules.d/$$$$$$$$priority-$(1); \
  139. done; \
  140. if [ -e $(2)/etc/modules.d/$$$$$$$$priority-$(1) ]; then \
  141. if [ "$$$$$$$$boot" = "1" -a ! -e $(2)/etc/modules-boot.d/$$$$$$$$priority-$(1) ]; then \
  142. mkdir -p $(2)/etc/modules-boot.d; \
  143. ln -s ../modules.d/$$$$$$$$priority-$(1) $(2)/etc/modules-boot.d/; \
  144. fi; \
  145. modules="$$$$$$$${modules:+$$$$$$$$modules }$$$$$$$$priority-$(1)"; \
  146. fi; \
  147. }; \
  148. $(3) \
  149. if [ -n "$$$$$$$$modules" ]; then \
  150. modules="$$$$$$$$(echo "$$$$$$$$modules" | tr ' ' '\n' | sort | uniq | paste -s -d' ' -)"; \
  151. mkdir -p $(2)/etc/modules.d; \
  152. mkdir -p $(2)/CONTROL; \
  153. echo "#!/bin/sh" > $(2)/CONTROL/postinst-pkg; \
  154. echo "[ -z \"\$$$$$$$$IPKG_INSTROOT\" ] || exit 0" >> $(2)/CONTROL/postinst-pkg; \
  155. echo ". /lib/functions.sh" >> $(2)/CONTROL/postinst-pkg; \
  156. echo "insert_modules $$$$$$$$modules" >> $(2)/CONTROL/postinst-pkg; \
  157. chmod 0755 $(2)/CONTROL/postinst-pkg; \
  158. fi
  159. endef
  160. ifeq ($(DUMP)$(TARGET_BUILD),)
  161. -include $(LINUX_DIR)/.config
  162. endif
  163. define KernelPackage/depends
  164. $(STAMP_BUILT): $(LINUX_DIR)/.config
  165. define KernelPackage/depends
  166. endef
  167. endef
  168. define KernelPackage
  169. NAME:=$(1)
  170. $(eval $(call Package/Default))
  171. $(eval $(call KernelPackage/Defaults))
  172. $(eval $(call KernelPackage/$(1)))
  173. $(eval $(call KernelPackage/$(1)/$(BOARD)))
  174. $(eval $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic)))
  175. define Package/kmod-$(1)
  176. TITLE:=$(TITLE)
  177. SECTION:=kernel
  178. CATEGORY:=Kernel modules
  179. DESCRIPTION:=$(DESCRIPTION)
  180. EXTRA_DEPENDS:=kernel (=$(LINUX_VERSION)-$(LINUX_RELEASE)-$(LINUX_VERMAGIC))
  181. VERSION:=$(LINUX_VERSION)$(if $(PKG_VERSION),+$(PKG_VERSION))-$(if $(PKG_RELEASE),$(PKG_RELEASE),$(LINUX_RELEASE))
  182. PKGFLAGS:=$(PKGFLAGS)
  183. $(call KernelPackage/$(1))
  184. $(call KernelPackage/$(1)/$(BOARD))
  185. $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic))
  186. endef
  187. ifdef KernelPackage/$(1)/conffiles
  188. define Package/kmod-$(1)/conffiles
  189. $(call KernelPackage/$(1)/conffiles)
  190. endef
  191. endif
  192. ifdef KernelPackage/$(1)/description
  193. define Package/kmod-$(1)/description
  194. $(call KernelPackage/$(1)/description)
  195. endef
  196. endif
  197. ifdef KernelPackage/$(1)/config
  198. define Package/kmod-$(1)/config
  199. $(call KernelPackage/$(1)/config)
  200. endef
  201. endif
  202. $(call KernelPackage/depends)
  203. ifneq ($(if $(filter-out %=y %=n %=m,$(KCONFIG)),$(filter m y,$(foreach c,$(filter-out %=y %=n %=m,$(KCONFIG)),$($(c)))),.),)
  204. ifneq ($(strip $(FILES)),)
  205. define Package/kmod-$(1)/install
  206. @for mod in $$(call version_filter,$$(FILES)); do \
  207. if grep -q "$$$$$$$${mod##$(LINUX_DIR)/}" "$(LINUX_DIR)/modules.builtin"; then \
  208. echo "NOTICE: module '$$$$$$$$mod' is built-in."; \
  209. elif [ -e $$$$$$$$mod ]; then \
  210. mkdir -p $$(1)/$(MODULES_SUBDIR) ; \
  211. $(CP) -L $$$$$$$$mod $$(1)/$(MODULES_SUBDIR)/ ; \
  212. else \
  213. echo "ERROR: module '$$$$$$$$mod' is missing." >&2; \
  214. exit 1; \
  215. fi; \
  216. done;
  217. $(call ModuleAutoLoad,$(1),$$(1),$(AUTOLOAD))
  218. $(call KernelPackage/$(1)/install,$$(1))
  219. endef
  220. endif
  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 $$(FILES))
  233. endef
  234. version_filter=$(if $(findstring @,$(1)),$(shell $(SCRIPT_DIR)/package-metadata.pl version_filter $(KERNEL_PATCHVER) $(1)),$(1))
  235. define AutoLoad
  236. add_module "$(1)" "$(call version_filter,$(2))" "$(3)";
  237. endef
  238. define AutoProbe
  239. probe_module "$(call version_filter,$(1))" "$(2)";
  240. endef
  241. version_field=$(if $(word $(1),$(2)),$(word $(1),$(2)),0)
  242. 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)) ))
  243. ifdef DUMP
  244. kernel_version_cmp=
  245. else
  246. kernel_version_cmp=$(shell [ $(call kernel_version_merge,$(call split_version,$(2))) $(1) $(call kernel_version_merge,$(call split_version,$(3))) ] && echo 1 )
  247. endif
  248. CompareKernelPatchVer=$(if $(call kernel_version_cmp,-$(2),$(1),$(3)),1,0)
  249. kernel_patchver_gt=$(call kernel_version_cmp,-gt,$(KERNEL_PATCHVER),$(1))
  250. kernel_patchver_ge=$(call kernel_version_cmp,-ge,$(KERNEL_PATCHVER),$(1))
  251. kernel_patchver_eq=$(call kernel_version_cmp,-eq,$(KERNEL_PATCHVER),$(1))
  252. kernel_patchver_le=$(call kernel_version_cmp,-le,$(KERNEL_PATCHVER),$(1))
  253. kernel_patchver_lt=$(call kernel_version_cmp,-lt,$(KERNEL_PATCHVER),$(1))