kernel: collect module symvers for external modules
[openwrt/staging/chunkeey.git] / include / kernel.mk
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
8 ifneq ($(filter check,$(MAKECMDGOALS)),)
9 CHECK:=1
10 DUMP:=1
11 endif
12
13 ifneq ($(SOURCE_DATE_EPOCH),)
14 ifndef DUMP
15 KBUILD_BUILD_TIMESTAMP:=$(shell perl -e 'print scalar gmtime($(SOURCE_DATE_EPOCH))')
16 endif
17 endif
18
19 ifeq ($(__target_inc),)
20 ifndef CHECK
21 include $(INCLUDE_DIR)/target.mk
22 endif
23 endif
24
25 ifeq ($(DUMP),1)
26 KERNEL?=<KERNEL>
27 BOARD?=<BOARD>
28 LINUX_VERSION?=<LINUX_VERSION>
29 LINUX_VERMAGIC?=<LINUX_VERMAGIC>
30 else
31 ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
32 export GCC_HONOUR_COPTS=s
33 endif
34
35 LINUX_KMOD_SUFFIX=ko
36
37 ifneq (,$(findstring uml,$(BOARD)))
38 KERNEL_CC?=$(HOSTCC)
39 KERNEL_CROSS?=
40 else
41 KERNEL_CC?=$(TARGET_CC)
42 KERNEL_CROSS?=$(TARGET_CROSS)
43 endif
44
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)")
48 endif
49 KERNEL_BUILD_DIR ?= $(BUILD_DIR)/linux-$(BOARD)$(if $(SUBTARGET),_$(SUBTARGET))
50 LINUX_DIR ?= $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)
51 LINUX_UAPI_DIR=uapi/
52 LINUX_VERMAGIC:=$(strip $(shell cat $(LINUX_DIR)/.vermagic 2>/dev/null))
53 LINUX_VERMAGIC:=$(if $(LINUX_VERMAGIC),$(LINUX_VERMAGIC),unknown)
54
55 LINUX_UNAME_VERSION:=$(if $(word 3,$(subst ., ,$(KERNEL_BASE))),$(KERNEL_BASE),$(KERNEL_BASE).0)
56 ifneq ($(findstring -rc,$(LINUX_VERSION)),)
57 LINUX_UNAME_VERSION:=$(LINUX_UNAME_VERSION)-$(strip $(lastword $(subst -, ,$(LINUX_VERSION))))
58 endif
59
60 LINUX_KERNEL:=$(KERNEL_BUILD_DIR)/vmlinux
61
62 LINUX_SOURCE:=linux-$(LINUX_VERSION).tar.xz
63 TESTING:=$(if $(findstring -rc,$(LINUX_VERSION)),/testing,)
64 ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
65 LINUX_SITE:=@KERNEL/linux/kernel/v$(word 1,$(subst ., ,$(KERNEL_BASE))).x$(TESTING)
66 else
67 LINUX_UNAME_VERSION:=$(strip $(shell cat $(LINUX_DIR)/include/config/kernel.release 2>/dev/null))
68 endif
69
70 MODULES_SUBDIR:=lib/modules/$(LINUX_UNAME_VERSION)
71 TARGET_MODULES_DIR:=$(LINUX_TARGET_DIR)/$(MODULES_SUBDIR)
72
73 ifneq ($(TARGET_BUILD),1)
74 PKG_BUILD_DIR ?= $(KERNEL_BUILD_DIR)/$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
75 endif
76 endif
77
78 ifneq (,$(findstring uml,$(BOARD)))
79 LINUX_KARCH=um
80 else ifneq (,$(findstring $(ARCH) , aarch64 aarch64_be ))
81 LINUX_KARCH := arm64
82 else ifneq (,$(findstring $(ARCH) , arceb ))
83 LINUX_KARCH := arc
84 else ifneq (,$(findstring $(ARCH) , armeb ))
85 LINUX_KARCH := arm
86 else ifneq (,$(findstring $(ARCH) , mipsel mips64 mips64el ))
87 LINUX_KARCH := mips
88 else ifneq (,$(findstring $(ARCH) , powerpc64 ))
89 LINUX_KARCH := powerpc
90 else ifneq (,$(findstring $(ARCH) , sh2 sh3 sh4 ))
91 LINUX_KARCH := sh
92 else ifneq (,$(findstring $(ARCH) , i386 x86_64 ))
93 LINUX_KARCH := x86
94 else
95 LINUX_KARCH := $(ARCH)
96 endif
97
98 KERNEL_MAKE = $(MAKE) $(KERNEL_MAKEOPTS)
99
100 KERNEL_MAKE_FLAGS := \
101 HOSTCFLAGS="$(HOST_CFLAGS) -Wall -Wmissing-prototypes -Wstrict-prototypes" \
102 CROSS_COMPILE="$(KERNEL_CROSS)" \
103 ARCH="$(LINUX_KARCH)" \
104 KBUILD_HAVE_NLS=no \
105 KBUILD_BUILD_USER="$(call qstrip,$(CONFIG_KERNEL_BUILD_USER))" \
106 KBUILD_BUILD_HOST="$(call qstrip,$(CONFIG_KERNEL_BUILD_DOMAIN))" \
107 KBUILD_BUILD_TIMESTAMP="$(KBUILD_BUILD_TIMESTAMP)" \
108 KBUILD_BUILD_VERSION="0" \
109 HOST_LOADLIBES="-L$(STAGING_DIR_HOST)/lib" \
110 CONFIG_SHELL="$(BASH)" \
111 $(if $(findstring c,$(OPENWRT_VERBOSE)),V=1,V='') \
112 $(if $(PKG_BUILD_ID),LDFLAGS_MODULE=--build-id=0x$(PKG_BUILD_ID)) \
113 cmd_syscalls=
114
115 ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
116 KERNEL_MAKE_FLAGS += \
117 KERNELRELEASE=$(LINUX_VERSION)
118 endif
119
120 KERNEL_MAKEOPTS := -C $(LINUX_DIR) $(KERNEL_MAKE_FLAGS)
121
122 ifdef CONFIG_USE_SPARSE
123 KERNEL_MAKEOPTS += C=1 CHECK=$(STAGING_DIR_HOST)/bin/sparse
124 endif
125
126 PKG_EXTMOD_SUBDIRS ?= .
127
128 define populate_module_symvers
129 cat /dev/null > $(PKG_INFO_DIR)/$(PKG_NAME).symvers; \
130 for subdir in $(PKG_EXTMOD_SUBDIRS); do \
131 cat $(PKG_INFO_DIR)/*.symvers 2>/dev/null > $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers; \
132 done
133 endef
134
135 define collect_module_symvers
136 for subdir in $(PKG_EXTMOD_SUBDIRS); do \
137 grep -F $(PKG_BUILD_DIR) $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers >> $(PKG_BUILD_DIR)/Module.symvers.tmp; \
138 done; \
139 sort -u $(PKG_BUILD_DIR)/Module.symvers.tmp > $(PKG_BUILD_DIR)/Module.symvers; \
140 mv $(PKG_BUILD_DIR)/Module.symvers $(PKG_INFO_DIR)/$(PKG_NAME).symvers
141 endef
142
143 define KernelPackage/hooks
144 ifneq ($(PKG_NAME),kernel)
145 Hooks/Compile/Pre += populate_module_symvers
146 Hooks/Compile/Post += collect_module_symvers
147 endif
148 define KernelPackage/hooks
149 endef
150 endef
151
152 define KernelPackage/Defaults
153 FILES:=
154 AUTOLOAD:=
155 PKGFLAGS+=nonshared
156 endef
157
158 define ModuleAutoLoad
159 $(SH_FUNC) \
160 export modules=; \
161 probe_module() { \
162 local mods="$$$$$$$$1"; \
163 local boot="$$$$$$$$2"; \
164 local mod; \
165 shift 2; \
166 for mod in $$$$$$$$mods; do \
167 mkdir -p $(2)/etc/modules.d; \
168 echo "$$$$$$$$mod" >> $(2)/etc/modules.d/$(1); \
169 done; \
170 if [ -e $(2)/etc/modules.d/$(1) ]; then \
171 if [ "$$$$$$$$boot" = "1" -a ! -e $(2)/etc/modules-boot.d/$(1) ]; then \
172 mkdir -p $(2)/etc/modules-boot.d; \
173 ln -s ../modules.d/$(1) $(2)/etc/modules-boot.d/; \
174 fi; \
175 modules="$$$$$$$${modules:+$$$$$$$$modules }$$$$$$$$mods"; \
176 fi; \
177 }; \
178 add_module() { \
179 local priority="$$$$$$$$1"; \
180 local mods="$$$$$$$$2"; \
181 local boot="$$$$$$$$3"; \
182 local mod; \
183 shift 3; \
184 for mod in $$$$$$$$mods; do \
185 mkdir -p $(2)/etc/modules.d; \
186 echo "$$$$$$$$mod" >> $(2)/etc/modules.d/$$$$$$$$priority-$(1); \
187 done; \
188 if [ -e $(2)/etc/modules.d/$$$$$$$$priority-$(1) ]; then \
189 if [ "$$$$$$$$boot" = "1" -a ! -e $(2)/etc/modules-boot.d/$$$$$$$$priority-$(1) ]; then \
190 mkdir -p $(2)/etc/modules-boot.d; \
191 ln -s ../modules.d/$$$$$$$$priority-$(1) $(2)/etc/modules-boot.d/; \
192 fi; \
193 modules="$$$$$$$${modules:+$$$$$$$$modules }$$$$$$$$priority-$(1)"; \
194 fi; \
195 }; \
196 $(3) \
197 if [ -n "$$$$$$$$modules" ]; then \
198 modules="$$$$$$$$(echo "$$$$$$$$modules" | tr ' ' '\n' | sort | uniq | paste -s -d' ' -)"; \
199 mkdir -p $(2)/etc/modules.d; \
200 mkdir -p $(2)/CONTROL; \
201 echo "#!/bin/sh" > $(2)/CONTROL/postinst-pkg; \
202 echo "[ -z \"\$$$$$$$$IPKG_INSTROOT\" ] || exit 0" >> $(2)/CONTROL/postinst-pkg; \
203 echo ". /lib/functions.sh" >> $(2)/CONTROL/postinst-pkg; \
204 echo "insert_modules $$$$$$$$modules" >> $(2)/CONTROL/postinst-pkg; \
205 chmod 0755 $(2)/CONTROL/postinst-pkg; \
206 fi
207 endef
208
209 ifeq ($(DUMP)$(TARGET_BUILD),)
210 -include $(LINUX_DIR)/.config
211 endif
212
213 define KernelPackage/depends
214 $(STAMP_BUILT): $(LINUX_DIR)/.config
215 define KernelPackage/depends
216 endef
217 endef
218
219 define KernelPackage
220 NAME:=$(1)
221 $(eval $(call Package/Default))
222 $(eval $(call KernelPackage/Defaults))
223 $(eval $(call KernelPackage/$(1)))
224 $(eval $(call KernelPackage/$(1)/$(BOARD)))
225 $(eval $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic)))
226
227 define Package/kmod-$(1)
228 TITLE:=$(TITLE)
229 SECTION:=kernel
230 CATEGORY:=Kernel modules
231 DESCRIPTION:=$(DESCRIPTION)
232 EXTRA_DEPENDS:=kernel (=$(LINUX_VERSION)-$(LINUX_RELEASE)-$(LINUX_VERMAGIC))
233 VERSION:=$(LINUX_VERSION)$(if $(PKG_VERSION),+$(PKG_VERSION))-$(if $(PKG_RELEASE),$(PKG_RELEASE),$(LINUX_RELEASE))
234 PKGFLAGS:=$(PKGFLAGS)
235 $(call KernelPackage/$(1))
236 $(call KernelPackage/$(1)/$(BOARD))
237 $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic))
238 endef
239
240 ifdef KernelPackage/$(1)/conffiles
241 define Package/kmod-$(1)/conffiles
242 $(call KernelPackage/$(1)/conffiles)
243 endef
244 endif
245
246 ifdef KernelPackage/$(1)/description
247 define Package/kmod-$(1)/description
248 $(call KernelPackage/$(1)/description)
249 endef
250 endif
251
252 ifdef KernelPackage/$(1)/config
253 define Package/kmod-$(1)/config
254 $(call KernelPackage/$(1)/config)
255 endef
256 endif
257
258 $(call KernelPackage/depends)
259 $(call KernelPackage/hooks)
260
261 ifneq ($(if $(filter-out %=y %=n %=m,$(KCONFIG)),$(filter m y,$(foreach c,$(filter-out %=y %=n %=m,$(KCONFIG)),$($(c)))),.),)
262 ifneq ($(strip $(FILES)),)
263 define Package/kmod-$(1)/install
264 @for mod in $$(call version_filter,$$(FILES)); do \
265 if grep -q "$$$$$$$${mod##$(LINUX_DIR)/}" "$(LINUX_DIR)/modules.builtin"; then \
266 echo "NOTICE: module '$$$$$$$$mod' is built-in."; \
267 elif [ -e $$$$$$$$mod ]; then \
268 mkdir -p $$(1)/$(MODULES_SUBDIR) ; \
269 $(CP) -L $$$$$$$$mod $$(1)/$(MODULES_SUBDIR)/ ; \
270 else \
271 echo "ERROR: module '$$$$$$$$mod' is missing." >&2; \
272 exit 1; \
273 fi; \
274 done;
275 $(call ModuleAutoLoad,$(1),$$(1),$(AUTOLOAD))
276 $(call KernelPackage/$(1)/install,$$(1))
277 endef
278 endif
279 $(if $(CONFIG_PACKAGE_kmod-$(1)),
280 else
281 compile: $(1)-disabled
282 $(1)-disabled:
283 @echo "WARNING: kmod-$(1) is not available in the kernel config - generating empty package" >&2
284
285 define Package/kmod-$(1)/install
286 true
287 endef
288 )
289 endif
290 $$(eval $$(call BuildPackage,kmod-$(1)))
291
292 $$(IPKG_kmod-$(1)): $$(wildcard $$(FILES))
293 endef
294
295 version_filter=$(if $(findstring @,$(1)),$(shell $(SCRIPT_DIR)/package-metadata.pl version_filter $(KERNEL_PATCHVER) $(1)),$(1))
296
297 define AutoLoad
298 add_module "$(1)" "$(call version_filter,$(2))" "$(3)";
299 endef
300
301 define AutoProbe
302 probe_module "$(call version_filter,$(1))" "$(2)";
303 endef
304
305 version_field=$(if $(word $(1),$(2)),$(word $(1),$(2)),0)
306 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)) ))
307
308 ifdef DUMP
309 kernel_version_cmp=
310 else
311 kernel_version_cmp=$(shell [ $(call kernel_version_merge,$(call split_version,$(2))) $(1) $(call kernel_version_merge,$(call split_version,$(3))) ] && echo 1 )
312 endif
313
314 CompareKernelPatchVer=$(if $(call kernel_version_cmp,-$(2),$(1),$(3)),1,0)
315
316 kernel_patchver_gt=$(call kernel_version_cmp,-gt,$(KERNEL_PATCHVER),$(1))
317 kernel_patchver_ge=$(call kernel_version_cmp,-ge,$(KERNEL_PATCHVER),$(1))
318 kernel_patchver_eq=$(call kernel_version_cmp,-eq,$(KERNEL_PATCHVER),$(1))
319 kernel_patchver_le=$(call kernel_version_cmp,-le,$(KERNEL_PATCHVER),$(1))
320 kernel_patchver_lt=$(call kernel_version_cmp,-lt,$(KERNEL_PATCHVER),$(1))
321