2 # Copyright (C) 2006-2015 OpenWrt.org
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
8 ifneq ($(filter check,$(MAKECMDGOALS)),)
13 ifneq ($(SOURCE_DATE_EPOCH),)
15 KBUILD_BUILD_TIMESTAMP:=$(shell perl -e 'print scalar gmtime($(SOURCE_DATE_EPOCH))')
19 ifeq ($(__target_inc),)
21 include $(INCLUDE_DIR)/target.mk
28 LINUX_VERSION?=<LINUX_VERSION>
29 LINUX_VERMAGIC?=<LINUX_VERMAGIC>
31 ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
32 export GCC_HONOUR_COPTS=s
37 ifneq (,$(findstring uml,$(BOARD)))
41 KERNEL_CC?=$(TARGET_CC)
42 KERNEL_CROSS?=$(TARGET_CROSS)
45 ifeq ($(TARGET_BUILD),1)
46 PATCH_DIR ?= $(CURDIR)/patches$(if $(wildcard ./patches-$(KERNEL_PATCHVER)),-$(KERNEL_PATCHVER))
47 FILES_DIR ?= $(foreach dir,$(wildcard $(CURDIR)/files $(CURDIR)/files-$(KERNEL_PATCHVER)),"$(dir)")
49 KERNEL_BUILD_DIR ?= $(BUILD_DIR)/linux-$(BOARD)$(if $(SUBTARGET),_$(SUBTARGET))
50 LINUX_DIR ?= $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)
52 LINUX_VERMAGIC:=$(strip $(shell cat $(LINUX_DIR)/.vermagic 2>/dev/null))
53 LINUX_VERMAGIC:=$(if $(LINUX_VERMAGIC),$(LINUX_VERMAGIC),unknown)
55 LINUX_UNAME_VERSION:=$(KERNEL_BASE)
56 ifneq ($(findstring -rc,$(LINUX_VERSION)),)
57 LINUX_UNAME_VERSION:=$(LINUX_UNAME_VERSION)-$(strip $(lastword $(subst -, ,$(LINUX_VERSION))))
60 LINUX_KERNEL:=$(KERNEL_BUILD_DIR)/vmlinux
62 ifneq (,$(findstring -rc,$(LINUX_VERSION)))
63 LINUX_SOURCE:=linux-$(LINUX_VERSION).tar.gz
65 LINUX_SOURCE:=linux-$(LINUX_VERSION).tar.xz
68 ifneq (,$(findstring -rc,$(LINUX_VERSION)))
69 LINUX_SITE:=https://git.kernel.org/torvalds/t
70 else ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
71 LINUX_SITE:=@KERNEL/linux/kernel/v$(word 1,$(subst ., ,$(KERNEL_BASE))).x
73 LINUX_UNAME_VERSION:=$(strip $(shell cat $(LINUX_DIR)/include/config/kernel.release 2>/dev/null))
76 MODULES_SUBDIR:=lib/modules/$(LINUX_UNAME_VERSION)
77 TARGET_MODULES_DIR:=$(LINUX_TARGET_DIR)/$(MODULES_SUBDIR)
79 ifneq ($(TARGET_BUILD),1)
80 PKG_BUILD_DIR ?= $(KERNEL_BUILD_DIR)/$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
84 ifneq (,$(findstring uml,$(BOARD)))
86 else ifneq (,$(findstring $(ARCH) , aarch64 aarch64_be ))
88 else ifneq (,$(findstring $(ARCH) , arceb ))
90 else ifneq (,$(findstring $(ARCH) , armeb ))
92 else ifneq (,$(findstring $(ARCH) , mipsel mips64 mips64el ))
94 else ifneq (,$(findstring $(ARCH) , powerpc64 ))
95 LINUX_KARCH := powerpc
96 else ifneq (,$(findstring $(ARCH) , sh2 sh3 sh4 ))
98 else ifneq (,$(findstring $(ARCH) , i386 x86_64 ))
101 LINUX_KARCH := $(ARCH)
104 KERNEL_MAKE = $(MAKE) $(KERNEL_MAKEOPTS)
106 KERNEL_MAKE_FLAGS = \
107 HOSTCFLAGS="$(HOST_CFLAGS) -Wall -Wmissing-prototypes -Wstrict-prototypes" \
108 CROSS_COMPILE="$(KERNEL_CROSS)" \
109 ARCH="$(LINUX_KARCH)" \
111 KBUILD_BUILD_USER="$(call qstrip,$(CONFIG_KERNEL_BUILD_USER))" \
112 KBUILD_BUILD_HOST="$(call qstrip,$(CONFIG_KERNEL_BUILD_DOMAIN))" \
113 KBUILD_BUILD_TIMESTAMP="$(KBUILD_BUILD_TIMESTAMP)" \
114 KBUILD_BUILD_VERSION="0" \
115 HOST_LOADLIBES="-L$(STAGING_DIR_HOST)/lib" \
116 CONFIG_SHELL="$(BASH)" \
117 $(if $(findstring c,$(OPENWRT_VERBOSE)),V=1,V='') \
118 $(if $(PKG_BUILD_ID),LDFLAGS_MODULE=--build-id=0x$(PKG_BUILD_ID)) \
121 ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
122 KERNEL_MAKE_FLAGS += \
123 KERNELRELEASE=$(LINUX_VERSION)
126 KERNEL_MAKEOPTS := -C $(LINUX_DIR) $(KERNEL_MAKE_FLAGS)
128 ifdef CONFIG_USE_SPARSE
129 KERNEL_MAKEOPTS += C=1 CHECK=$(STAGING_DIR_HOST)/bin/sparse
132 ifeq ($(HOST_OS),Darwin)
133 export SKIP_STACK_VALIDATION:=1
136 PKG_EXTMOD_SUBDIRS ?= .
138 define populate_module_symvers
139 @mkdir -p $(PKG_INFO_DIR)
140 cat /dev/null > $(PKG_INFO_DIR)/$(PKG_NAME).symvers; \
141 for subdir in $(PKG_EXTMOD_SUBDIRS); do \
142 cat $(PKG_INFO_DIR)/*.symvers 2>/dev/null > $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers; \
146 define collect_module_symvers
147 for subdir in $(PKG_EXTMOD_SUBDIRS); do \
148 realdir=$$$$(readlink -f $(PKG_BUILD_DIR)); \
149 grep -F $(PKG_BUILD_DIR) $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers >> $(PKG_BUILD_DIR)/Module.symvers.tmp; \
150 [ "$(PKG_BUILD_DIR)" = "$$$$realdir" ] || \
151 grep -F $$$$realdir $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers >> $(PKG_BUILD_DIR)/Module.symvers.tmp; \
153 sort -u $(PKG_BUILD_DIR)/Module.symvers.tmp > $(PKG_BUILD_DIR)/Module.symvers; \
154 mv $(PKG_BUILD_DIR)/Module.symvers $(PKG_INFO_DIR)/$(PKG_NAME).symvers
157 define KernelPackage/hooks
158 ifneq ($(PKG_NAME),kernel)
159 Hooks/Compile/Pre += populate_module_symvers
160 Hooks/Compile/Post += collect_module_symvers
162 define KernelPackage/hooks
166 define KernelPackage/Defaults
175 # 3: module priority prefix
176 # 4: required for boot
178 define ModuleAutoLoad
180 mkdir -p $(2)/etc/modules.d; \
181 ($(foreach mod,$(5), \
182 echo "$(mod)$(if $(MODPARAMS.$(mod)), $(MODPARAMS.$(mod)),$(if $(MODPARAMS), $(MODPARAMS)))"; )) > $(2)/etc/modules.d/$(3)$(1); \
184 mkdir -p $(2)/etc/modules-boot.d; \
185 ln -sf ../modules.d/$(3)$(1) $(2)/etc/modules-boot.d/;))
188 ifeq ($(DUMP)$(TARGET_BUILD),)
189 -include $(LINUX_DIR)/.config
192 define KernelPackage/depends
193 $(STAMP_BUILT): $(LINUX_DIR)/.config
194 define KernelPackage/depends
200 $(eval $(call Package/Default))
201 $(eval $(call KernelPackage/Defaults))
202 $(eval $(call KernelPackage/$(1)))
203 $(eval $(call KernelPackage/$(1)/$(BOARD)))
204 $(eval $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic)))
206 define Package/kmod-$(1)
209 CATEGORY:=Kernel modules
210 DESCRIPTION:=$(DESCRIPTION)
211 EXTRA_DEPENDS:=kernel (=$(LINUX_VERSION)-$(LINUX_RELEASE)-$(LINUX_VERMAGIC))
212 VERSION:=$(LINUX_VERSION)$(if $(PKG_VERSION),+$(PKG_VERSION))-$(if $(PKG_RELEASE),$(PKG_RELEASE),$(LINUX_RELEASE))
213 PKGFLAGS:=$(PKGFLAGS)
214 $(call KernelPackage/$(1))
215 $(call KernelPackage/$(1)/$(BOARD))
216 $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic))
219 ifdef KernelPackage/$(1)/conffiles
220 define Package/kmod-$(1)/conffiles
221 $(call KernelPackage/$(1)/conffiles)
225 ifdef KernelPackage/$(1)/description
226 define Package/kmod-$(1)/description
227 $(call KernelPackage/$(1)/description)
231 ifdef KernelPackage/$(1)/config
232 define Package/kmod-$(1)/config
233 $(call KernelPackage/$(1)/config)
237 $(call KernelPackage/depends)
238 $(call KernelPackage/hooks)
240 ifneq ($(if $(filter-out %=y %=n %=m,$(KCONFIG)),$(filter m y,$(foreach c,$(filter-out %=y %=n %=m,$(KCONFIG)),$($(c)))),.),)
241 define Package/kmod-$(1)/install
242 @for mod in $$(call version_filter,$$(FILES)); do \
243 if grep -q "$$$$$$$${mod##$(LINUX_DIR)/}" "$(LINUX_DIR)/modules.builtin"; then \
244 echo "NOTICE: module '$$$$$$$$mod' is built-in."; \
245 elif [ -e $$$$$$$$mod ]; then \
246 mkdir -p $$(1)/$(MODULES_SUBDIR) ; \
247 $(CP) -L $$$$$$$$mod $$(1)/$(MODULES_SUBDIR)/ ; \
249 echo "ERROR: module '$$$$$$$$mod' is missing." >&2; \
253 $(call ModuleAutoLoad,$(1),$$(1),$(filter-out 0-,$(word 1,$(AUTOLOAD))-),$(filter-out 0,$(word 2,$(AUTOLOAD))),$(sort $(wordlist 3,99,$(AUTOLOAD))))
254 $(call KernelPackage/$(1)/install,$$(1))
256 $(if $(CONFIG_PACKAGE_kmod-$(1)),
258 compile: $(1)-disabled
260 @echo "WARNING: kmod-$(1) is not available in the kernel config - generating empty package" >&2
262 define Package/kmod-$(1)/install
267 $$(eval $$(call BuildPackage,kmod-$(1)))
269 $$(IPKG_kmod-$(1)): $$(wildcard $$(call version_filter,$$(FILES)))
273 version_filter=$(if $(findstring @,$(1)),$(shell $(SCRIPT_DIR)/package-metadata.pl version_filter $(KERNEL_PATCHVER) $(1)),$(1))
275 # 1: priority (optional)
279 $(if $(1),$(1),0) $(if $(3),1,0) $(call version_filter,$(2))
285 $(call AutoLoad,,$(1),$(2))
288 version_field=$(if $(word $(1),$(2)),$(word $(1),$(2)),0)
289 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)) ))
294 kernel_version_cmp=$(shell [ $(call kernel_version_merge,$(call split_version,$(2))) $(1) $(call kernel_version_merge,$(call split_version,$(3))) ] && echo 1 )
297 CompareKernelPatchVer=$(if $(call kernel_version_cmp,-$(2),$(1),$(3)),1,0)
299 kernel_patchver_gt=$(call kernel_version_cmp,-gt,$(KERNEL_PATCHVER),$(1))
300 kernel_patchver_ge=$(call kernel_version_cmp,-ge,$(KERNEL_PATCHVER),$(1))
301 kernel_patchver_eq=$(call kernel_version_cmp,-eq,$(KERNEL_PATCHVER),$(1))
302 kernel_patchver_le=$(call kernel_version_cmp,-le,$(KERNEL_PATCHVER),$(1))
303 kernel_patchver_lt=$(call kernel_version_cmp,-lt,$(KERNEL_PATCHVER),$(1))