1################################################################################
2# kernel module infrastructure for building Linux kernel modules
3#
4# This file implements an infrastructure that eases development of package
5# .mk files for out-of-tree Linux kernel modules. It should be used for all
6# packages that build a Linux kernel module using the kernel's out-of-tree
7# buildsystem, unless they use a complex custom buildsystem.
8#
9# The kernel-module infrastructure requires the packages that use it to also
10# use another package infrastructure. kernel-module only defines post-build
11# and post-install hooks. This allows the package to build both kernel
12# modules and/or user-space components (with any of the other *-package
13# infra).
14#
15# As such, it is to be used in conjunction with another *-package infra,
16# like so:
17#
18#   $(eval $(kernel-module))
19#   $(eval $(generic-package))
20#
21# Note: if the caller needs access to the kernel modules (either after they
22# are built or after they are installed), it will have to define its own
23# post-build/install hooks *after* calling kernel-module, but *before*
24# calling the other *-package infra, like so:
25#
26#   $(eval $(kernel-module))
27#   define FOO_MOD_TWEAK
28#       # do something
29#   endef
30#   FOO_POST_BUILD_HOOKS += FOO_MOD_TWEAK
31#   $(eval $(generic-package))
32#
33# Note: this infra does not check that the kernel is enabled; it is expected
34# to be enforced at the Kconfig level with proper 'depends on'.
35################################################################################
36
37################################################################################
38# inner-kernel-module -- generates the make targets needed to support building
39# a kernel module
40#
41#  argument 1 is the lowercase package name
42#  argument 2 is the uppercase package name
43################################################################################
44
45define inner-kernel-module
46
47# If the package is enabled, ensure the kernel will support modules
48ifeq ($$(BR2_PACKAGE_$(2)),y)
49LINUX_NEEDS_MODULES = y
50endif
51
52# The kernel must be built first.
53$(2)_DEPENDENCIES += \
54	linux \
55	$$(BR2_MAKE_HOST_DEPENDENCY)
56
57# This is only defined in some infrastructures (e.g. autotools, cmake),
58# but not in others (e.g. generic). So define it here as well.
59$(2)_MAKE ?= $$(BR2_MAKE)
60
61# If not specified, consider the source of the kernel module to be at
62# the root of the package.
63$(2)_MODULE_SUBDIRS ?= .
64
65# Build the kernel module(s)
66# Force PWD for those packages that want to use it to find their
67# includes and other support files (Booo!)
68define $(2)_KERNEL_MODULES_BUILD
69	@$$(call MESSAGE,"Building kernel module(s)")
70	$$(foreach d,$$($(2)_MODULE_SUBDIRS), \
71		$$(LINUX_MAKE_ENV) $$($$(PKG)_MAKE) \
72			-C $$(LINUX_DIR) \
73			$$(LINUX_MAKE_FLAGS) \
74			$$($(2)_MODULE_MAKE_OPTS) \
75			PWD=$$(@D)/$$(d) \
76			M=$$(@D)/$$(d) \
77			modules$$(sep))
78endef
79$(2)_POST_BUILD_HOOKS += $(2)_KERNEL_MODULES_BUILD
80
81# Install the kernel module(s)
82# Force PWD for those packages that want to use it to find their
83# includes and other support files (Booo!)
84define $(2)_KERNEL_MODULES_INSTALL
85	@$$(call MESSAGE,"Installing kernel module(s)")
86	$$(foreach d,$$($(2)_MODULE_SUBDIRS), \
87		$$(LINUX_MAKE_ENV) $$($$(PKG)_MAKE) \
88			-C $$(LINUX_DIR) \
89			$$(LINUX_MAKE_FLAGS) \
90			$$($(2)_MODULE_MAKE_OPTS) \
91			PWD=$$(@D)/$$(d) \
92			M=$$(@D)/$$(d) \
93			modules_install$$(sep))
94endef
95$(2)_POST_INSTALL_TARGET_HOOKS += $(2)_KERNEL_MODULES_INSTALL
96
97endef
98
99################################################################################
100# kernel-module -- the target generator macro for kernel module packages
101################################################################################
102
103kernel-module = $(call inner-kernel-module,$(pkgname),$(call UPPERCASE,$(pkgname)))
104