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