Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 1 | ========================= |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 2 | Building External Modules |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 3 | ========================= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 5 | This document describes how to build an out-of-tree kernel module. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 7 | .. Table of Contents |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
| 9 | === 1 Introduction |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 10 | === 2 How to Build External Modules |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 11 | --- 2.1 Command Syntax |
| 12 | --- 2.2 Options |
| 13 | --- 2.3 Targets |
| 14 | --- 2.4 Building Separate Files |
| 15 | === 3. Creating a Kbuild File for an External Module |
| 16 | --- 3.1 Shared Makefile |
| 17 | --- 3.2 Separate Kbuild file and Makefile |
| 18 | --- 3.3 Binary Blobs |
| 19 | --- 3.4 Building Multiple Modules |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 20 | === 4. Include Files |
| 21 | --- 4.1 Kernel Includes |
| 22 | --- 4.2 Single Subdirectory |
| 23 | --- 4.3 Several Subdirectories |
| 24 | === 5. Module Installation |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 25 | --- 5.1 INSTALL_MOD_PATH |
| 26 | --- 5.2 INSTALL_MOD_DIR |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 27 | === 6. Module Versioning |
| 28 | --- 6.1 Symbols From the Kernel (vmlinux + modules) |
| 29 | --- 6.2 Symbols and External Modules |
| 30 | --- 6.3 Symbols From Another External Module |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 31 | === 7. Tips & Tricks |
| 32 | --- 7.1 Testing for CONFIG_FOO_BAR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | |
| 35 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 36 | 1. Introduction |
| 37 | =============== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 39 | "kbuild" is the build system used by the Linux kernel. Modules must use |
| 40 | kbuild to stay compatible with changes in the build infrastructure and |
| 41 | to pick up the right flags to "gcc." Functionality for building modules |
| 42 | both in-tree and out-of-tree is provided. The method for building |
| 43 | either is similar, and all modules are initially developed and built |
| 44 | out-of-tree. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 46 | Covered in this document is information aimed at developers interested |
| 47 | in building out-of-tree (or "external") modules. The author of an |
| 48 | external module should supply a makefile that hides most of the |
| 49 | complexity, so one only has to type "make" to build the module. This is |
| 50 | easily accomplished, and a complete example will be presented in |
| 51 | section 3. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 54 | 2. How to Build External Modules |
| 55 | ================================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 57 | To build external modules, you must have a prebuilt kernel available |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 58 | that contains the configuration and header files used in the build. |
| 59 | Also, the kernel must have been built with modules enabled. If you are |
| 60 | using a distribution kernel, there will be a package for the kernel you |
| 61 | are running provided by your distribution. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 63 | An alternative is to use the "make" target "modules_prepare." This will |
| 64 | make sure the kernel contains the information required. The target |
| 65 | exists solely as a simple way to prepare a kernel source tree for |
| 66 | building external modules. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 68 | NOTE: "modules_prepare" will not build Module.symvers even if |
| 69 | CONFIG_MODVERSIONS is set; therefore, a full kernel build needs to be |
| 70 | executed to make module versioning work. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 72 | 2.1 Command Syntax |
| 73 | ================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 75 | The command to build an external module is:: |
Robert P. J. Day | 99c8b94 | 2006-09-25 15:55:51 -0400 | [diff] [blame] | 76 | |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 77 | $ make -C <path_to_kernel_src> M=$PWD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 79 | The kbuild system knows that an external module is being built |
| 80 | due to the "M=<dir>" option given in the command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 82 | To build against the running kernel use:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 84 | $ make -C /lib/modules/`uname -r`/build M=$PWD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 86 | Then to install the module(s) just built, add the target |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 87 | "modules_install" to the command:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 89 | $ make -C /lib/modules/`uname -r`/build M=$PWD modules_install |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 91 | 2.2 Options |
| 92 | =========== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 94 | ($KDIR refers to the path of the kernel source directory.) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 96 | make -C $KDIR M=$PWD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 98 | -C $KDIR |
| 99 | The directory where the kernel source is located. |
| 100 | "make" will actually change to the specified directory |
| 101 | when executing and will change back when finished. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 103 | M=$PWD |
| 104 | Informs kbuild that an external module is being built. |
| 105 | The value given to "M" is the absolute path of the |
| 106 | directory where the external module (kbuild file) is |
| 107 | located. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 109 | 2.3 Targets |
| 110 | =========== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 112 | When building an external module, only a subset of the "make" |
| 113 | targets are available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 115 | make -C $KDIR M=$PWD [target] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 117 | The default will build the module(s) located in the current |
| 118 | directory, so a target does not need to be specified. All |
| 119 | output files will also be generated in this directory. No |
| 120 | attempts are made to update the kernel source, and it is a |
| 121 | precondition that a successful "make" has been executed for the |
| 122 | kernel. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 124 | modules |
| 125 | The default target for external modules. It has the |
| 126 | same functionality as if no target was specified. See |
| 127 | description above. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 129 | modules_install |
| 130 | Install the external module(s). The default location is |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 131 | /lib/modules/<kernel_release>/extra/, but a prefix may |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 132 | be added with INSTALL_MOD_PATH (discussed in section 5). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 134 | clean |
| 135 | Remove all generated files in the module directory only. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 137 | help |
| 138 | List the available targets for external modules. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 140 | 2.4 Building Separate Files |
| 141 | =========================== |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 142 | |
| 143 | It is possible to build single files that are part of a module. |
| 144 | This works equally well for the kernel, a module, and even for |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 145 | external modules. |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 146 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 147 | Example (The module foo.ko, consist of bar.o and baz.o):: |
| 148 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 149 | make -C $KDIR M=$PWD bar.lst |
| 150 | make -C $KDIR M=$PWD baz.o |
| 151 | make -C $KDIR M=$PWD foo.ko |
Masahiro Yamada | 6d3c94e | 2019-02-14 12:05:17 +0900 | [diff] [blame] | 152 | make -C $KDIR M=$PWD ./ |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 155 | 3. Creating a Kbuild File for an External Module |
| 156 | ================================================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 158 | In the last section we saw the command to build a module for the |
| 159 | running kernel. The module is not actually built, however, because a |
| 160 | build file is required. Contained in this file will be the name of |
| 161 | the module(s) being built, along with the list of requisite source |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 162 | files. The file may be as simple as a single line:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 164 | obj-m := <module_name>.o |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 166 | The kbuild system will build <module_name>.o from <module_name>.c, |
| 167 | and, after linking, will result in the kernel module <module_name>.ko. |
| 168 | The above line can be put in either a "Kbuild" file or a "Makefile." |
| 169 | When the module is built from multiple sources, an additional line is |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 170 | needed listing the files:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 172 | <module_name>-y := <src1>.o <src2>.o ... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 174 | NOTE: Further documentation describing the syntax used by kbuild is |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 175 | located in Documentation/kbuild/makefiles.rst. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 177 | The examples below demonstrate how to create a build file for the |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 178 | module 8123.ko, which is built from the following files:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | 8123_if.c |
| 181 | 8123_if.h |
| 182 | 8123_pci.c |
| 183 | 8123_bin.o_shipped <= Binary blob |
| 184 | |
Dov Murik | 758abb5 | 2020-06-22 12:43:43 +0000 | [diff] [blame] | 185 | 3.1 Shared Makefile |
| 186 | ------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 188 | An external module always includes a wrapper makefile that |
| 189 | supports building the module using "make" with no arguments. |
| 190 | This target is not used by kbuild; it is only for convenience. |
| 191 | Additional functionality, such as test targets, can be included |
| 192 | but should be filtered out from kbuild due to possible name |
| 193 | clashes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 195 | Example 1:: |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | --> filename: Makefile |
| 198 | ifneq ($(KERNELRELEASE),) |
| 199 | # kbuild part of makefile |
| 200 | obj-m := 8123.o |
| 201 | 8123-y := 8123_if.o 8123_pci.o 8123_bin.o |
| 202 | |
| 203 | else |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 204 | # normal makefile |
| 205 | KDIR ?= /lib/modules/`uname -r`/build |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 207 | default: |
| 208 | $(MAKE) -C $(KDIR) M=$$PWD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | # Module specific targets |
| 211 | genbin: |
Brian Strand | 98a1e44 | 2005-11-22 01:23:08 +0000 | [diff] [blame] | 212 | echo "X" > 8123_bin.o_shipped |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | endif |
| 215 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 216 | The check for KERNELRELEASE is used to separate the two parts |
| 217 | of the makefile. In the example, kbuild will only see the two |
| 218 | assignments, whereas "make" will see everything except these |
| 219 | two assignments. This is due to two passes made on the file: |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 220 | the first pass is by the "make" instance run on the command |
| 221 | line; the second pass is by the kbuild system, which is |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 222 | initiated by the parameterized "make" in the default target. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 224 | 3.2 Separate Kbuild File and Makefile |
| 225 | ------------------------------------- |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 226 | |
| 227 | In newer versions of the kernel, kbuild will first look for a |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 228 | file named "Kbuild," and only if that is not found, will it |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 229 | then look for a makefile. Utilizing a "Kbuild" file allows us |
| 230 | to split up the makefile from example 1 into two files: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 232 | Example 2:: |
| 233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | --> filename: Kbuild |
| 235 | obj-m := 8123.o |
| 236 | 8123-y := 8123_if.o 8123_pci.o 8123_bin.o |
| 237 | |
| 238 | --> filename: Makefile |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 239 | KDIR ?= /lib/modules/`uname -r`/build |
| 240 | |
| 241 | default: |
| 242 | $(MAKE) -C $(KDIR) M=$$PWD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | # Module specific targets |
| 245 | genbin: |
Wolfram Sang | baa9187 | 2009-01-06 15:12:27 +0100 | [diff] [blame] | 246 | echo "X" > 8123_bin.o_shipped |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 248 | The split in example 2 is questionable due to the simplicity of |
| 249 | each file; however, some external modules use makefiles |
| 250 | consisting of several hundred lines, and here it really pays |
| 251 | off to separate the kbuild part from the rest. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 253 | The next example shows a backward compatible version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 255 | Example 3:: |
| 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | --> filename: Kbuild |
| 258 | obj-m := 8123.o |
| 259 | 8123-y := 8123_if.o 8123_pci.o 8123_bin.o |
| 260 | |
| 261 | --> filename: Makefile |
| 262 | ifneq ($(KERNELRELEASE),) |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 263 | # kbuild part of makefile |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | include Kbuild |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 266 | else |
| 267 | # normal makefile |
| 268 | KDIR ?= /lib/modules/`uname -r`/build |
| 269 | |
| 270 | default: |
| 271 | $(MAKE) -C $(KDIR) M=$$PWD |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
| 273 | # Module specific targets |
| 274 | genbin: |
Wolfram Sang | baa9187 | 2009-01-06 15:12:27 +0100 | [diff] [blame] | 275 | echo "X" > 8123_bin.o_shipped |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
| 277 | endif |
| 278 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 279 | Here the "Kbuild" file is included from the makefile. This |
| 280 | allows an older version of kbuild, which only knows of |
| 281 | makefiles, to be used when the "make" and kbuild parts are |
| 282 | split into separate files. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 284 | 3.3 Binary Blobs |
| 285 | ---------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 287 | Some external modules need to include an object file as a blob. |
| 288 | kbuild has support for this, but requires the blob file to be |
| 289 | named <filename>_shipped. When the kbuild rules kick in, a copy |
| 290 | of <filename>_shipped is created with _shipped stripped off, |
| 291 | giving us <filename>. This shortened filename can be used in |
| 292 | the assignment to the module. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 294 | Throughout this section, 8123_bin.o_shipped has been used to |
| 295 | build the kernel module 8123.ko; it has been included as |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 296 | 8123_bin.o:: |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | 8123-y := 8123_if.o 8123_pci.o 8123_bin.o |
| 299 | |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 300 | Although there is no distinction between the ordinary source |
| 301 | files and the binary file, kbuild will pick up different rules |
| 302 | when creating the object file for the module. |
| 303 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 304 | 3.4 Building Multiple Modules |
| 305 | ============================= |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 306 | |
| 307 | kbuild supports building multiple modules with a single build |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 308 | file. For example, if you wanted to build two modules, foo.ko |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 309 | and bar.ko, the kbuild lines would be:: |
matt mooney | efdf02c | 2010-09-18 18:33:57 -0700 | [diff] [blame] | 310 | |
| 311 | obj-m := foo.o bar.o |
| 312 | foo-y := <foo_srcs> |
| 313 | bar-y := <bar_srcs> |
| 314 | |
| 315 | It is that simple! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 318 | 4. Include Files |
| 319 | ================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 321 | Within the kernel, header files are kept in standard locations |
| 322 | according to the following rule: |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 323 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 324 | * If the header file only describes the internal interface of a |
| 325 | module, then the file is placed in the same directory as the |
| 326 | source files. |
| 327 | * If the header file describes an interface used by other parts |
| 328 | of the kernel that are located in different directories, then |
| 329 | the file is placed in include/linux/. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 331 | NOTE: |
| 332 | There are two notable exceptions to this rule: larger |
| 333 | subsystems have their own directory under include/, such as |
| 334 | include/scsi; and architecture specific headers are located |
Masahiro Yamada | 8c4d9b1 | 2020-11-28 20:51:03 +0900 | [diff] [blame] | 335 | under arch/$(SRCARCH)/include/. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 337 | 4.1 Kernel Includes |
| 338 | ------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 340 | To include a header file located under include/linux/, simply |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 341 | use:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 343 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 345 | kbuild will add options to "gcc" so the relevant directories |
| 346 | are searched. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 348 | 4.2 Single Subdirectory |
| 349 | ----------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 351 | External modules tend to place header files in a separate |
| 352 | include/ directory where their source is located, although this |
| 353 | is not the usual kernel style. To inform kbuild of the |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 354 | directory, use either ccflags-y or CFLAGS_<filename>.o. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 356 | Using the example from section 3, if we moved 8123_if.h to a |
| 357 | subdirectory named include, the resulting kbuild file would |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 358 | look like:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | --> filename: Kbuild |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 361 | obj-m := 8123.o |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 363 | ccflags-y := -Iinclude |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | 8123-y := 8123_if.o 8123_pci.o 8123_bin.o |
| 365 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 366 | Note that in the assignment there is no space between -I and |
| 367 | the path. This is a limitation of kbuild: there must be no |
| 368 | space present. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 370 | 4.3 Several Subdirectories |
| 371 | -------------------------- |
Sam Ravnborg | 253dfa6 | 2006-01-06 20:33:41 +0100 | [diff] [blame] | 372 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 373 | kbuild can handle files that are spread over several directories. |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 374 | Consider the following example:: |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 375 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 376 | . |
| 377 | |__ src |
| 378 | | |__ complex_main.c |
| 379 | | |__ hal |
| 380 | | |__ hardwareif.c |
| 381 | | |__ include |
| 382 | | |__ hardwareif.h |
| 383 | |__ include |
| 384 | |__ complex.h |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 385 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 386 | To build the module complex.ko, we then need the following |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 387 | kbuild file:: |
Sam Ravnborg | 253dfa6 | 2006-01-06 20:33:41 +0100 | [diff] [blame] | 388 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 389 | --> filename: Kbuild |
Sam Ravnborg | 253dfa6 | 2006-01-06 20:33:41 +0100 | [diff] [blame] | 390 | obj-m := complex.o |
| 391 | complex-y := src/complex_main.o |
| 392 | complex-y += src/hal/hardwareif.o |
| 393 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 394 | ccflags-y := -I$(src)/include |
| 395 | ccflags-y += -I$(src)/src/hal/include |
| 396 | |
| 397 | As you can see, kbuild knows how to handle object files located |
| 398 | in other directories. The trick is to specify the directory |
| 399 | relative to the kbuild file's location. That being said, this |
| 400 | is NOT recommended practice. |
| 401 | |
| 402 | For the header files, kbuild must be explicitly told where to |
| 403 | look. When kbuild executes, the current directory is always the |
| 404 | root of the kernel tree (the argument to "-C") and therefore an |
| 405 | absolute path is needed. $(src) provides the absolute path by |
| 406 | pointing to the directory where the currently executing kbuild |
| 407 | file is located. |
Sam Ravnborg | 253dfa6 | 2006-01-06 20:33:41 +0100 | [diff] [blame] | 408 | |
| 409 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 410 | 5. Module Installation |
| 411 | ====================== |
Sam Ravnborg | 253dfa6 | 2006-01-06 20:33:41 +0100 | [diff] [blame] | 412 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 413 | Modules which are included in the kernel are installed in the |
| 414 | directory: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 416 | /lib/modules/$(KERNELRELEASE)/kernel/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 418 | And external modules are installed in: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 420 | /lib/modules/$(KERNELRELEASE)/extra/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 422 | 5.1 INSTALL_MOD_PATH |
| 423 | -------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 425 | Above are the default directories but as always some level of |
| 426 | customization is possible. A prefix can be added to the |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 427 | installation path using the variable INSTALL_MOD_PATH:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | $ make INSTALL_MOD_PATH=/frodo modules_install |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 430 | => Install dir: /frodo/lib/modules/$(KERNELRELEASE)/kernel/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 432 | INSTALL_MOD_PATH may be set as an ordinary shell variable or, |
| 433 | as shown above, can be specified on the command line when |
| 434 | calling "make." This has effect when installing both in-tree |
| 435 | and out-of-tree modules. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 437 | 5.2 INSTALL_MOD_DIR |
| 438 | ------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 440 | External modules are by default installed to a directory under |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 441 | /lib/modules/$(KERNELRELEASE)/extra/, but you may wish to |
| 442 | locate modules for a specific functionality in a separate |
| 443 | directory. For this purpose, use INSTALL_MOD_DIR to specify an |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 444 | alternative name to "extra.":: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 446 | $ make INSTALL_MOD_DIR=gandalf -C $KDIR \ |
| 447 | M=$PWD modules_install |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 448 | => Install dir: /lib/modules/$(KERNELRELEASE)/gandalf/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 451 | 6. Module Versioning |
| 452 | ==================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 454 | Module versioning is enabled by the CONFIG_MODVERSIONS tag, and is used |
| 455 | as a simple ABI consistency check. A CRC value of the full prototype |
| 456 | for an exported symbol is created. When a module is loaded/used, the |
| 457 | CRC values contained in the kernel are compared with similar values in |
| 458 | the module; if they are not equal, the kernel refuses to load the |
| 459 | module. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 461 | Module.symvers contains a list of all exported symbols from a kernel |
| 462 | build. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 464 | 6.1 Symbols From the Kernel (vmlinux + modules) |
| 465 | ----------------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 467 | During a kernel build, a file named Module.symvers will be |
| 468 | generated. Module.symvers contains all exported symbols from |
| 469 | the kernel and compiled modules. For each symbol, the |
| 470 | corresponding CRC value is also stored. |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 471 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 472 | The syntax of the Module.symvers file is:: |
| 473 | |
Dov Murik | 758abb5 | 2020-06-22 12:43:43 +0000 | [diff] [blame] | 474 | <CRC> <Symbol> <Module> <Export Type> <Namespace> |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 475 | |
Dov Murik | 758abb5 | 2020-06-22 12:43:43 +0000 | [diff] [blame] | 476 | 0xe1cc2a05 usb_stor_suspend drivers/usb/storage/usb-storage EXPORT_SYMBOL_GPL USB_STORAGE |
Matthias Maennich | cb9b55d | 2019-09-06 11:32:28 +0100 | [diff] [blame] | 477 | |
| 478 | The fields are separated by tabs and values may be empty (e.g. |
| 479 | if no namespace is defined for an exported symbol). |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 480 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 481 | For a kernel build without CONFIG_MODVERSIONS enabled, the CRC |
| 482 | would read 0x00000000. |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 483 | |
Jan Engelhardt | d9a7ff6 | 2006-07-27 22:14:29 +0200 | [diff] [blame] | 484 | Module.symvers serves two purposes: |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 485 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 486 | 1) It lists all exported symbols from vmlinux and all modules. |
| 487 | 2) It lists the CRC if CONFIG_MODVERSIONS is enabled. |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 488 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 489 | 6.2 Symbols and External Modules |
| 490 | -------------------------------- |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 491 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 492 | When building an external module, the build system needs access |
| 493 | to the symbols from the kernel to check if all external symbols |
| 494 | are defined. This is done in the MODPOST step. modpost obtains |
| 495 | the symbols by reading Module.symvers from the kernel source |
Masahiro Yamada | 39808e4 | 2019-10-03 19:29:14 +0900 | [diff] [blame] | 496 | tree. During the MODPOST step, a new Module.symvers file will be |
| 497 | written containing all exported symbols from that external module. |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 498 | |
Alex Gaynor | 807f210 | 2019-09-21 15:23:04 -0700 | [diff] [blame] | 499 | 6.3 Symbols From Another External Module |
| 500 | ---------------------------------------- |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 501 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 502 | Sometimes, an external module uses exported symbols from |
Masahiro Yamada | 4349670 | 2019-10-03 19:29:12 +0900 | [diff] [blame] | 503 | another external module. Kbuild needs to have full knowledge of |
Warren Turkal | 4be7f0a | 2014-06-08 22:19:29 -0700 | [diff] [blame] | 504 | all symbols to avoid spitting out warnings about undefined |
Masahiro Yamada | 39808e4 | 2019-10-03 19:29:14 +0900 | [diff] [blame] | 505 | symbols. Two solutions exist for this situation. |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 506 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 507 | NOTE: The method with a top-level kbuild file is recommended |
| 508 | but may be impractical in certain situations. |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 509 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 510 | Use a top-level kbuild file |
| 511 | If you have two modules, foo.ko and bar.ko, where |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 512 | foo.ko needs symbols from bar.ko, you can use a |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 513 | common top-level kbuild file so both modules are |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 514 | compiled in the same build. Consider the following |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 515 | directory layout:: |
Robert P. J. Day | 2e99f31 | 2006-09-21 09:39:41 -0400 | [diff] [blame] | 516 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 517 | ./foo/ <= contains foo.ko |
| 518 | ./bar/ <= contains bar.ko |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 519 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 520 | The top-level kbuild file would then look like:: |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 521 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 522 | #./Kbuild (or ./Makefile): |
Masahiro Yamada | 4349670 | 2019-10-03 19:29:12 +0900 | [diff] [blame] | 523 | obj-m := foo/ bar/ |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 524 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 525 | And executing:: |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 526 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 527 | $ make -C $KDIR M=$PWD |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 528 | |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 529 | will then do the expected and compile both modules with |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 530 | full knowledge of symbols from either module. |
Sam Ravnborg | 040fcc8 | 2006-01-28 22:15:55 +0100 | [diff] [blame] | 531 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 532 | Use "make" variable KBUILD_EXTRA_SYMBOLS |
Masahiro Yamada | 39808e4 | 2019-10-03 19:29:14 +0900 | [diff] [blame] | 533 | If it is impractical to add a top-level kbuild file, |
| 534 | you can assign a space separated list |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 535 | of files to KBUILD_EXTRA_SYMBOLS in your build file. |
| 536 | These files will be loaded by modpost during the |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 537 | initialization of its symbol tables. |
Richard Hacker | 0d96fb2 | 2008-02-28 09:40:58 +0100 | [diff] [blame] | 538 | |
matt mooney | 5793210 | 2010-10-01 21:21:55 -0700 | [diff] [blame] | 539 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 540 | 7. Tips & Tricks |
| 541 | ================ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 543 | 7.1 Testing for CONFIG_FOO_BAR |
| 544 | ------------------------------ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 546 | Modules often need to check for certain `CONFIG_` options to |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 547 | decide if a specific feature is included in the module. In |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 548 | kbuild this is done by referencing the `CONFIG_` variable |
| 549 | directly:: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | #fs/ext2/Makefile |
| 552 | obj-$(CONFIG_EXT2_FS) += ext2.o |
| 553 | |
| 554 | ext2-y := balloc.o bitmap.o dir.o |
| 555 | ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o |
| 556 | |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 557 | External modules have traditionally used "grep" to check for |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 558 | specific `CONFIG_` settings directly in .config. This usage is |
matt mooney | 9f02186 | 2010-09-19 23:06:36 -0700 | [diff] [blame] | 559 | broken. As introduced before, external modules should use |
| 560 | kbuild for building and can therefore use the same methods as |
Mauro Carvalho Chehab | cd238ef | 2019-06-12 14:52:48 -0300 | [diff] [blame] | 561 | in-tree modules when testing for `CONFIG_` definitions. |