kernel.mk 7.5 KB

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