1*4882a593Smuzhiyun// -*- mode:doc; -*- 2*4882a593Smuzhiyun// vim: set syntax=asciidoc: 3*4882a593Smuzhiyun 4*4882a593Smuzhiyun=== Infrastructure for packages building kernel modules 5*4882a593Smuzhiyun 6*4882a593SmuzhiyunBuildroot offers a helper infrastructure to make it easy to write packages that 7*4882a593Smuzhiyunbuild and install Linux kernel modules. Some packages only contain a kernel 8*4882a593Smuzhiyunmodule, other packages contain programs and libraries in addition to kernel 9*4882a593Smuzhiyunmodules. Buildroot's helper infrastructure supports either case. 10*4882a593Smuzhiyun 11*4882a593Smuzhiyun[[kernel-module-tutorial]] 12*4882a593Smuzhiyun==== +kernel-module+ tutorial 13*4882a593Smuzhiyun 14*4882a593SmuzhiyunLet's start with an example on how to prepare a simple package that only 15*4882a593Smuzhiyunbuilds a kernel module, and no other component: 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun---- 18*4882a593Smuzhiyun01: ################################################################################ 19*4882a593Smuzhiyun02: # 20*4882a593Smuzhiyun03: # foo 21*4882a593Smuzhiyun04: # 22*4882a593Smuzhiyun05: ################################################################################ 23*4882a593Smuzhiyun06: 24*4882a593Smuzhiyun07: FOO_VERSION = 1.2.3 25*4882a593Smuzhiyun08: FOO_SOURCE = foo-$(FOO_VERSION).tar.xz 26*4882a593Smuzhiyun09: FOO_SITE = http://www.foosoftware.org/download 27*4882a593Smuzhiyun10: FOO_LICENSE = GPL-2.0 28*4882a593Smuzhiyun11: FOO_LICENSE_FILES = COPYING 29*4882a593Smuzhiyun12: 30*4882a593Smuzhiyun13: $(eval $(kernel-module)) 31*4882a593Smuzhiyun14: $(eval $(generic-package)) 32*4882a593Smuzhiyun---- 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunLines 7-11 define the usual meta-data to specify the version, archive name, 35*4882a593Smuzhiyunremote URI where to find the package source, licensing information. 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunOn line 13, we invoke the +kernel-module+ helper infrastructure, that 38*4882a593Smuzhiyungenerates all the appropriate Makefile rules and variables to build 39*4882a593Smuzhiyunthat kernel module. 40*4882a593Smuzhiyun 41*4882a593SmuzhiyunFinally, on line 14, we invoke the 42*4882a593Smuzhiyunxref:generic-package-tutorial[+generic-package+ infrastructure]. 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunThe dependency on +linux+ is automatically added, so it is not needed to 45*4882a593Smuzhiyunspecify it in +FOO_DEPENDENCIES+. 46*4882a593Smuzhiyun 47*4882a593SmuzhiyunWhat you may have noticed is that, unlike other package infrastructures, 48*4882a593Smuzhiyunwe explicitly invoke a second infrastructure. This allows a package to 49*4882a593Smuzhiyunbuild a kernel module, but also, if needed, use any one of other package 50*4882a593Smuzhiyuninfrastructures to build normal userland components (libraries, 51*4882a593Smuzhiyunexecutables...). Using the +kernel-module+ infrastructure on its own is 52*4882a593Smuzhiyunnot sufficient; another package infrastructure *must* be used. 53*4882a593Smuzhiyun 54*4882a593SmuzhiyunLet's look at a more complex example: 55*4882a593Smuzhiyun 56*4882a593Smuzhiyun---- 57*4882a593Smuzhiyun01: ################################################################################ 58*4882a593Smuzhiyun02: # 59*4882a593Smuzhiyun03: # foo 60*4882a593Smuzhiyun04: # 61*4882a593Smuzhiyun05: ################################################################################ 62*4882a593Smuzhiyun06: 63*4882a593Smuzhiyun07: FOO_VERSION = 1.2.3 64*4882a593Smuzhiyun08: FOO_SOURCE = foo-$(FOO_VERSION).tar.xz 65*4882a593Smuzhiyun09: FOO_SITE = http://www.foosoftware.org/download 66*4882a593Smuzhiyun10: FOO_LICENSE = GPL-2.0 67*4882a593Smuzhiyun11: FOO_LICENSE_FILES = COPYING 68*4882a593Smuzhiyun12: 69*4882a593Smuzhiyun13: FOO_MODULE_SUBDIRS = driver/base 70*4882a593Smuzhiyun14: FOO_MODULE_MAKE_OPTS = KVERSION=$(LINUX_VERSION_PROBED) 71*4882a593Smuzhiyun15: 72*4882a593Smuzhiyun16: ifeq ($(BR2_PACKAGE_LIBBAR),y) 73*4882a593Smuzhiyun17: FOO_DEPENDENCIES = libbar 74*4882a593Smuzhiyun18: FOO_CONF_OPTS = --enable-bar 75*4882a593Smuzhiyun19: FOO_MODULE_SUBDIRS += driver/bar 76*4882a593Smuzhiyun20: else 77*4882a593Smuzhiyun21: FOO_CONF_OPTS = --disable-bar 78*4882a593Smuzhiyun22: endif 79*4882a593Smuzhiyun23: 80*4882a593Smuzhiyun24: $(eval $(kernel-module)) 81*4882a593Smuzhiyun26: $(eval $(autotools-package)) 82*4882a593Smuzhiyun---- 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunHere, we see that we have an autotools-based package, that also builds 85*4882a593Smuzhiyunthe kernel module located in sub-directory +driver/base+ and, if libbar 86*4882a593Smuzhiyunis enabled, the kernel module located in sub-directory +driver/bar+, and 87*4882a593Smuzhiyundefines the variable +KVERSION+ to be passed to the Linux buildsystem 88*4882a593Smuzhiyunwhen building the module(s). 89*4882a593Smuzhiyun 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun[[kernel-module-reference]] 92*4882a593Smuzhiyun==== +kernel-module+ reference 93*4882a593Smuzhiyun 94*4882a593SmuzhiyunThe main macro for the kernel module infrastructure is +kernel-module+. 95*4882a593SmuzhiyunUnlike other package infrastructures, it is not stand-alone, and requires 96*4882a593Smuzhiyunany of the other +*-package+ macros be called after it. 97*4882a593Smuzhiyun 98*4882a593SmuzhiyunThe +kernel-module+ macro defines post-build and post-target-install 99*4882a593Smuzhiyunhooks to build the kernel modules. If the package's +.mk+ needs access 100*4882a593Smuzhiyunto the built kernel modules, it should do so in a post-build hook, 101*4882a593Smuzhiyun*registered after* the call to +kernel-module+. Similarly, if the 102*4882a593Smuzhiyunpackage's +.mk+ needs access to the kernel module after it has been 103*4882a593Smuzhiyuninstalled, it should do so in a post-install hook, *registered after* 104*4882a593Smuzhiyunthe call to +kernel-module+. Here's an example: 105*4882a593Smuzhiyun 106*4882a593Smuzhiyun---- 107*4882a593Smuzhiyun$(eval $(kernel-module)) 108*4882a593Smuzhiyun 109*4882a593Smuzhiyundefine FOO_DO_STUFF_WITH_KERNEL_MODULE 110*4882a593Smuzhiyun # Do something with it... 111*4882a593Smuzhiyunendef 112*4882a593SmuzhiyunFOO_POST_BUILD_HOOKS += FOO_DO_STUFF_WITH_KERNEL_MODULE 113*4882a593Smuzhiyun 114*4882a593Smuzhiyun$(eval $(generic-package)) 115*4882a593Smuzhiyun---- 116*4882a593Smuzhiyun 117*4882a593SmuzhiyunFinally, unlike the other package infrastructures, there is no 118*4882a593Smuzhiyun+host-kernel-module+ variant to build a host kernel module. 119*4882a593Smuzhiyun 120*4882a593SmuzhiyunThe following additional variables can optionally be defined to further 121*4882a593Smuzhiyunconfigure the build of the kernel module: 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun* +FOO_MODULE_SUBDIRS+ may be set to one or more sub-directories (relative 124*4882a593Smuzhiyun to the package source top-directory) where the kernel module sources are. 125*4882a593Smuzhiyun If empty or not set, the sources for the kernel module(s) are considered 126*4882a593Smuzhiyun to be located at the top of the package source tree. 127*4882a593Smuzhiyun 128*4882a593Smuzhiyun* +FOO_MODULE_MAKE_OPTS+ may be set to contain extra variable definitions 129*4882a593Smuzhiyun to pass to the Linux buildsystem. 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun[[kernel-variables]] 132*4882a593SmuzhiyunYou may also reference (but you may *not* set!) those variables: 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun * +LINUX_DIR+ contains the path to where the Linux kernel has been 135*4882a593Smuzhiyun extracted and built. 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun * +LINUX_VERSION+ contains the version string as configured by the user. 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun * +LINUX_VERSION_PROBED+ contains the real version string of the kernel, 140*4882a593Smuzhiyun retrieved with running `make -C $(LINUX_DIR) kernelrelease` 141*4882a593Smuzhiyun 142*4882a593Smuzhiyun * +KERNEL_ARCH+ contains the name of the current architecture, like `arm`, 143*4882a593Smuzhiyun `mips`... 144