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