kernel.mk 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. KERNELRELEASE=$(LINUX_VERSION) \
  97. cmd_syscalls=
  98. ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
  99. KERNEL_MAKEOPTS += \
  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. define KernelPackage/Defaults
  107. FILES:=
  108. AUTOLOAD:=
  109. PKGFLAGS+=nonshared
  110. endef
  111. define ModuleAutoLoad
  112. $(SH_FUNC) \
  113. export modules=; \
  114. probe_module() { \
  115. local mods="$$$$$$$$1"; \
  116. local boot="$$$$$$$$2"; \
  117. local mod; \
  118. shift 2; \
  119. for mod in $$$$$$$$mods; do \
  120. mkdir -p $(2)/etc/modules.d; \
  121. echo "$$$$$$$$mod" >> $(2)/etc/modules.d/$(1); \
  122. done; \
  123. if [ -e $(2)/etc/modules.d/$(1) ]; then \
  124. if [ "$$$$$$$$boot" = "1" -a ! -e $(2)/etc/modules-boot.d/$(1) ]; then \
  125. mkdir -p $(2)/etc/modules-boot.d; \
  126. ln -s ../modules.d/$(1) $(2)/etc/modules-boot.d/; \
  127. fi; \
  128. modules="$$$$$$$${modules:+$$$$$$$$modules }$$$$$$$$mods"; \
  129. fi; \
  130. }; \
  131. add_module() { \
  132. local priority="$$$$$$$$1"; \
  133. local mods="$$$$$$$$2"; \
  134. local boot="$$$$$$$$3"; \
  135. local mod; \
  136. shift 3; \
  137. for mod in $$$$$$$$mods; do \
  138. mkdir -p $(2)/etc/modules.d; \
  139. echo "$$$$$$$$mod" >> $(2)/etc/modules.d/$$$$$$$$priority-$(1); \
  140. done; \
  141. if [ -e $(2)/etc/modules.d/$$$$$$$$priority-$(1) ]; then \
  142. if [ "$$$$$$$$boot" = "1" -a ! -e $(2)/etc/modules-boot.d/$$$$$$$$priority-$(1) ]; then \
  143. mkdir -p $(2)/etc/modules-boot.d; \
  144. ln -s ../modules.d/$$$$$$$$priority-$(1) $(2)/etc/modules-boot.d/; \
  145. fi; \
  146. modules="$$$$$$$${modules:+$$$$$$$$modules }$$$$$$$$priority-$(1)"; \
  147. fi; \
  148. }; \
  149. $(3) \
  150. if [ -n "$$$$$$$$modules" ]; then \
  151. modules="$$$$$$$$(echo "$$$$$$$$modules" | tr ' ' '\n' | sort | uniq | paste -s -d' ' -)"; \
  152. mkdir -p $(2)/etc/modules.d; \
  153. mkdir -p $(2)/CONTROL; \
  154. echo "#!/bin/sh" > $(2)/CONTROL/postinst-pkg; \
  155. echo "[ -z \"\$$$$$$$$IPKG_INSTROOT\" ] || exit 0" >> $(2)/CONTROL/postinst-pkg; \
  156. echo ". /lib/functions.sh" >> $(2)/CONTROL/postinst-pkg; \
  157. echo "insert_modules $$$$$$$$modules" >> $(2)/CONTROL/postinst-pkg; \
  158. chmod 0755 $(2)/CONTROL/postinst-pkg; \
  159. fi
  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. ifneq ($(if $(filter-out %=y %=n %=m,$(KCONFIG)),$(filter m y,$(foreach c,$(filter-out %=y %=n %=m,$(KCONFIG)),$($(c)))),.),)
  205. ifneq ($(strip $(FILES)),)
  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),$(AUTOLOAD))
  219. $(call KernelPackage/$(1)/install,$$(1))
  220. endef
  221. endif
  222. $(if $(CONFIG_PACKAGE_kmod-$(1)),
  223. else
  224. compile: $(1)-disabled
  225. $(1)-disabled:
  226. @echo "WARNING: kmod-$(1) is not available in the kernel config - generating empty package" >&2
  227. define Package/kmod-$(1)/install
  228. true
  229. endef
  230. )
  231. endif
  232. $$(eval $$(call BuildPackage,kmod-$(1)))
  233. $$(IPKG_kmod-$(1)): $$(wildcard $$(FILES))
  234. endef
  235. version_filter=$(if $(findstring @,$(1)),$(shell $(SCRIPT_DIR)/package-metadata.pl version_filter $(KERNEL_PATCHVER) $(1)),$(1))
  236. define AutoLoad
  237. add_module "$(1)" "$(call version_filter,$(2))" "$(3)";
  238. endef
  239. define AutoProbe
  240. probe_module "$(call version_filter,$(1))" "$(2)";
  241. endef
  242. version_field=$(if $(word $(1),$(2)),$(word $(1),$(2)),0)
  243. 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)) ))
  244. ifdef DUMP
  245. kernel_version_cmp=
  246. else
  247. kernel_version_cmp=$(shell [ $(call kernel_version_merge,$(call split_version,$(2))) $(1) $(call kernel_version_merge,$(call split_version,$(3))) ] && echo 1 )
  248. endif
  249. CompareKernelPatchVer=$(if $(call kernel_version_cmp,-$(2),$(1),$(3)),1,0)
  250. kernel_patchver_gt=$(call kernel_version_cmp,-gt,$(KERNEL_PATCHVER),$(1))
  251. kernel_patchver_ge=$(call kernel_version_cmp,-ge,$(KERNEL_PATCHVER),$(1))
  252. kernel_patchver_eq=$(call kernel_version_cmp,-eq,$(KERNEL_PATCHVER),$(1))
  253. kernel_patchver_le=$(call kernel_version_cmp,-le,$(KERNEL_PATCHVER),$(1))
  254. kernel_patchver_lt=$(call kernel_version_cmp,-lt,$(KERNEL_PATCHVER),$(1))