kernel.mk 9.2 KB

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