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