xref: /openbmc/linux/Documentation/kbuild/makefiles.rst (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1cd238effSMauro Carvalho Chehab======================
2cd238effSMauro Carvalho ChehabLinux Kernel Makefiles
3cd238effSMauro Carvalho Chehab======================
4cd238effSMauro Carvalho Chehab
5cd238effSMauro Carvalho ChehabThis document describes the Linux kernel Makefiles.
6cd238effSMauro Carvalho Chehab
71a4c1c9dSJani NikulaOverview
81a4c1c9dSJani Nikula========
9cd238effSMauro Carvalho Chehab
10cd238effSMauro Carvalho ChehabThe Makefiles have five parts::
11cd238effSMauro Carvalho Chehab
12cd238effSMauro Carvalho Chehab	Makefile                    the top Makefile.
13cd238effSMauro Carvalho Chehab	.config                     the kernel configuration file.
148c4d9b14SMasahiro Yamada	arch/$(SRCARCH)/Makefile    the arch Makefile.
15cd238effSMauro Carvalho Chehab	scripts/Makefile.*          common rules etc. for all kbuild Makefiles.
16b044a535SMasahiro Yamada	kbuild Makefiles            exist in every subdirectory
17cd238effSMauro Carvalho Chehab
18cd238effSMauro Carvalho ChehabThe top Makefile reads the .config file, which comes from the kernel
19cd238effSMauro Carvalho Chehabconfiguration process.
20cd238effSMauro Carvalho Chehab
21cd238effSMauro Carvalho ChehabThe top Makefile is responsible for building two major products: vmlinux
22cd238effSMauro Carvalho Chehab(the resident kernel image) and modules (any module files).
23cd238effSMauro Carvalho ChehabIt builds these goals by recursively descending into the subdirectories of
24cd238effSMauro Carvalho Chehabthe kernel source tree.
259f1fe2bbSJani Nikula
26cd238effSMauro Carvalho ChehabThe list of subdirectories which are visited depends upon the kernel
27cd238effSMauro Carvalho Chehabconfiguration. The top Makefile textually includes an arch Makefile
288c4d9b14SMasahiro Yamadawith the name arch/$(SRCARCH)/Makefile. The arch Makefile supplies
29cd238effSMauro Carvalho Chehabarchitecture-specific information to the top Makefile.
30cd238effSMauro Carvalho Chehab
31cd238effSMauro Carvalho ChehabEach subdirectory has a kbuild Makefile which carries out the commands
32cd238effSMauro Carvalho Chehabpassed down from above. The kbuild Makefile uses information from the
33cd238effSMauro Carvalho Chehab.config file to construct various file lists used by kbuild to build
34cd238effSMauro Carvalho Chehabany built-in or modular targets.
35cd238effSMauro Carvalho Chehab
36cd238effSMauro Carvalho Chehabscripts/Makefile.* contains all the definitions/rules etc. that
37cd238effSMauro Carvalho Chehabare used to build the kernel based on the kbuild makefiles.
38cd238effSMauro Carvalho Chehab
391a4c1c9dSJani NikulaWho does what
401a4c1c9dSJani Nikula=============
41cd238effSMauro Carvalho Chehab
42cd238effSMauro Carvalho ChehabPeople have four different relationships with the kernel Makefiles.
43cd238effSMauro Carvalho Chehab
44cd238effSMauro Carvalho Chehab*Users* are people who build kernels.  These people type commands such as
45*2f0e2a39SJani Nikula``make menuconfig`` or ``make``.  They usually do not read or edit
46cd238effSMauro Carvalho Chehabany kernel Makefiles (or any other source files).
47cd238effSMauro Carvalho Chehab
48cd238effSMauro Carvalho Chehab*Normal developers* are people who work on features such as device
49cd238effSMauro Carvalho Chehabdrivers, file systems, and network protocols.  These people need to
50cd238effSMauro Carvalho Chehabmaintain the kbuild Makefiles for the subsystem they are
51cd238effSMauro Carvalho Chehabworking on.  In order to do this effectively, they need some overall
52cd238effSMauro Carvalho Chehabknowledge about the kernel Makefiles, plus detailed knowledge about the
53cd238effSMauro Carvalho Chehabpublic interface for kbuild.
54cd238effSMauro Carvalho Chehab
55cd238effSMauro Carvalho Chehab*Arch developers* are people who work on an entire architecture, such
56cd238effSMauro Carvalho Chehabas sparc or ia64.  Arch developers need to know about the arch Makefile
57cd238effSMauro Carvalho Chehabas well as kbuild Makefiles.
58cd238effSMauro Carvalho Chehab
59cd238effSMauro Carvalho Chehab*Kbuild developers* are people who work on the kernel build system itself.
60cd238effSMauro Carvalho ChehabThese people need to know about all aspects of the kernel Makefiles.
61cd238effSMauro Carvalho Chehab
62cd238effSMauro Carvalho ChehabThis document is aimed towards normal developers and arch developers.
63cd238effSMauro Carvalho Chehab
64cd238effSMauro Carvalho Chehab
651a4c1c9dSJani NikulaThe kbuild files
661a4c1c9dSJani Nikula================
67cd238effSMauro Carvalho Chehab
68cd238effSMauro Carvalho ChehabMost Makefiles within the kernel are kbuild Makefiles that use the
69cd238effSMauro Carvalho Chehabkbuild infrastructure. This chapter introduces the syntax used in the
70cd238effSMauro Carvalho Chehabkbuild makefiles.
719f1fe2bbSJani Nikula
72*2f0e2a39SJani NikulaThe preferred name for the kbuild files are ``Makefile`` but ``Kbuild`` can
73*2f0e2a39SJani Nikulabe used and if both a ``Makefile`` and a ``Kbuild`` file exists, then the ``Kbuild``
74cd238effSMauro Carvalho Chehabfile will be used.
75cd238effSMauro Carvalho Chehab
761a4c1c9dSJani NikulaSection `Goal definitions`_ is a quick intro; further chapters provide
77cd238effSMauro Carvalho Chehabmore details, with real examples.
78cd238effSMauro Carvalho Chehab
791a4c1c9dSJani NikulaGoal definitions
801a4c1c9dSJani Nikula----------------
81cd238effSMauro Carvalho Chehab
82cd238effSMauro Carvalho ChehabGoal definitions are the main part (heart) of the kbuild Makefile.
83cd238effSMauro Carvalho ChehabThese lines define the files to be built, any special compilation
84cd238effSMauro Carvalho Chehaboptions, and any subdirectories to be entered recursively.
85cd238effSMauro Carvalho Chehab
86cd238effSMauro Carvalho ChehabThe most simple kbuild makefile contains one line:
87cd238effSMauro Carvalho Chehab
88cd238effSMauro Carvalho ChehabExample::
89cd238effSMauro Carvalho Chehab
90cd238effSMauro Carvalho Chehab  obj-y += foo.o
91cd238effSMauro Carvalho Chehab
92cd238effSMauro Carvalho ChehabThis tells kbuild that there is one object in that directory, named
93cd238effSMauro Carvalho Chehabfoo.o. foo.o will be built from foo.c or foo.S.
94cd238effSMauro Carvalho Chehab
95cd238effSMauro Carvalho ChehabIf foo.o shall be built as a module, the variable obj-m is used.
96cd238effSMauro Carvalho ChehabTherefore the following pattern is often used:
97cd238effSMauro Carvalho Chehab
98cd238effSMauro Carvalho ChehabExample::
99cd238effSMauro Carvalho Chehab
100cd238effSMauro Carvalho Chehab  obj-$(CONFIG_FOO) += foo.o
101cd238effSMauro Carvalho Chehab
102cd238effSMauro Carvalho Chehab$(CONFIG_FOO) evaluates to either y (for built-in) or m (for module).
103cd238effSMauro Carvalho ChehabIf CONFIG_FOO is neither y nor m, then the file will not be compiled
104cd238effSMauro Carvalho Chehabnor linked.
105cd238effSMauro Carvalho Chehab
1061a4c1c9dSJani NikulaBuilt-in object goals - obj-y
1071a4c1c9dSJani Nikula-----------------------------
108cd238effSMauro Carvalho Chehab
109cd238effSMauro Carvalho ChehabThe kbuild Makefile specifies object files for vmlinux
110cd238effSMauro Carvalho Chehabin the $(obj-y) lists.  These lists depend on the kernel
111cd238effSMauro Carvalho Chehabconfiguration.
112cd238effSMauro Carvalho Chehab
113cd238effSMauro Carvalho ChehabKbuild compiles all the $(obj-y) files.  It then calls
114*2f0e2a39SJani Nikula``$(AR) rcSTP`` to merge these files into one built-in.a file.
115cd238effSMauro Carvalho ChehabThis is a thin archive without a symbol table. It will be later
116cd238effSMauro Carvalho Chehablinked into vmlinux by scripts/link-vmlinux.sh
117cd238effSMauro Carvalho Chehab
118cd238effSMauro Carvalho ChehabThe order of files in $(obj-y) is significant.  Duplicates in
119cd238effSMauro Carvalho Chehabthe lists are allowed: the first instance will be linked into
120cd238effSMauro Carvalho Chehabbuilt-in.a and succeeding instances will be ignored.
121cd238effSMauro Carvalho Chehab
122cd238effSMauro Carvalho ChehabLink order is significant, because certain functions
123cd238effSMauro Carvalho Chehab(module_init() / __initcall) will be called during boot in the
124cd238effSMauro Carvalho Chehaborder they appear. So keep in mind that changing the link
125cd238effSMauro Carvalho Chehaborder may e.g. change the order in which your SCSI
126cd238effSMauro Carvalho Chehabcontrollers are detected, and thus your disks are renumbered.
127cd238effSMauro Carvalho Chehab
128cd238effSMauro Carvalho ChehabExample::
129cd238effSMauro Carvalho Chehab
130cd238effSMauro Carvalho Chehab  #drivers/isdn/i4l/Makefile
131cd238effSMauro Carvalho Chehab  # Makefile for the kernel ISDN subsystem and device drivers.
132cd238effSMauro Carvalho Chehab  # Each configuration option enables a list of files.
133cd238effSMauro Carvalho Chehab  obj-$(CONFIG_ISDN_I4L)         += isdn.o
134cd238effSMauro Carvalho Chehab  obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o
135cd238effSMauro Carvalho Chehab
1361a4c1c9dSJani NikulaLoadable module goals - obj-m
1371a4c1c9dSJani Nikula-----------------------------
138cd238effSMauro Carvalho Chehab
139cd238effSMauro Carvalho Chehab$(obj-m) specifies object files which are built as loadable
140cd238effSMauro Carvalho Chehabkernel modules.
141cd238effSMauro Carvalho Chehab
142cd238effSMauro Carvalho ChehabA module may be built from one source file or several source
143cd238effSMauro Carvalho Chehabfiles. In the case of one source file, the kbuild makefile
144cd238effSMauro Carvalho Chehabsimply adds the file to $(obj-m).
145cd238effSMauro Carvalho Chehab
146cd238effSMauro Carvalho ChehabExample::
147cd238effSMauro Carvalho Chehab
148cd238effSMauro Carvalho Chehab  #drivers/isdn/i4l/Makefile
149cd238effSMauro Carvalho Chehab  obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o
150cd238effSMauro Carvalho Chehab
151*2f0e2a39SJani NikulaNote: In this example $(CONFIG_ISDN_PPP_BSDCOMP) evaluates to "m"
152cd238effSMauro Carvalho Chehab
153cd238effSMauro Carvalho ChehabIf a kernel module is built from several source files, you specify
154cd238effSMauro Carvalho Chehabthat you want to build a module in the same way as above; however,
155cd238effSMauro Carvalho Chehabkbuild needs to know which object files you want to build your
156cd238effSMauro Carvalho Chehabmodule from, so you have to tell it by setting a $(<module_name>-y)
157cd238effSMauro Carvalho Chehabvariable.
158cd238effSMauro Carvalho Chehab
159cd238effSMauro Carvalho ChehabExample::
160cd238effSMauro Carvalho Chehab
161cd238effSMauro Carvalho Chehab  #drivers/isdn/i4l/Makefile
162cd238effSMauro Carvalho Chehab  obj-$(CONFIG_ISDN_I4L) += isdn.o
163cd238effSMauro Carvalho Chehab  isdn-y := isdn_net_lib.o isdn_v110.o isdn_common.o
164cd238effSMauro Carvalho Chehab
165cd238effSMauro Carvalho ChehabIn this example, the module name will be isdn.o. Kbuild will
166cd238effSMauro Carvalho Chehabcompile the objects listed in $(isdn-y) and then run
167*2f0e2a39SJani Nikula``$(LD) -r`` on the list of these files to generate isdn.o.
168cd238effSMauro Carvalho Chehab
169cd238effSMauro Carvalho ChehabDue to kbuild recognizing $(<module_name>-y) for composite objects,
170*2f0e2a39SJani Nikulayou can use the value of a ``CONFIG_`` symbol to optionally include an
171cd238effSMauro Carvalho Chehabobject file as part of a composite object.
172cd238effSMauro Carvalho Chehab
173cd238effSMauro Carvalho ChehabExample::
174cd238effSMauro Carvalho Chehab
175cd238effSMauro Carvalho Chehab  #fs/ext2/Makefile
176cd238effSMauro Carvalho Chehab  obj-$(CONFIG_EXT2_FS) += ext2.o
177cd238effSMauro Carvalho Chehab  ext2-y := balloc.o dir.o file.o ialloc.o inode.o ioctl.o \
178cd238effSMauro Carvalho Chehab    namei.o super.o symlink.o
179cd238effSMauro Carvalho Chehab  ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o xattr_user.o \
180cd238effSMauro Carvalho Chehab    xattr_trusted.o
181cd238effSMauro Carvalho Chehab
182cd238effSMauro Carvalho ChehabIn this example, xattr.o, xattr_user.o and xattr_trusted.o are only
183cd238effSMauro Carvalho Chehabpart of the composite object ext2.o if $(CONFIG_EXT2_FS_XATTR)
184*2f0e2a39SJani Nikulaevaluates to "y".
185cd238effSMauro Carvalho Chehab
186cd238effSMauro Carvalho ChehabNote: Of course, when you are building objects into the kernel,
187cd238effSMauro Carvalho Chehabthe syntax above will also work. So, if you have CONFIG_EXT2_FS=y,
188cd238effSMauro Carvalho Chehabkbuild will build an ext2.o file for you out of the individual
189cd238effSMauro Carvalho Chehabparts and then link this into built-in.a, as you would expect.
190cd238effSMauro Carvalho Chehab
1911a4c1c9dSJani NikulaLibrary file goals - lib-y
1921a4c1c9dSJani Nikula--------------------------
193cd238effSMauro Carvalho Chehab
194cd238effSMauro Carvalho ChehabObjects listed with obj-* are used for modules, or
195cd238effSMauro Carvalho Chehabcombined in a built-in.a for that specific directory.
196cd238effSMauro Carvalho ChehabThere is also the possibility to list objects that will
197cd238effSMauro Carvalho Chehabbe included in a library, lib.a.
198cd238effSMauro Carvalho ChehabAll objects listed with lib-y are combined in a single
199cd238effSMauro Carvalho Chehablibrary for that directory.
200cd238effSMauro Carvalho ChehabObjects that are listed in obj-y and additionally listed in
201cd238effSMauro Carvalho Chehablib-y will not be included in the library, since they will
202cd238effSMauro Carvalho Chehabbe accessible anyway.
203cd238effSMauro Carvalho ChehabFor consistency, objects listed in lib-m will be included in lib.a.
204cd238effSMauro Carvalho Chehab
205cd238effSMauro Carvalho ChehabNote that the same kbuild makefile may list files to be built-in
206cd238effSMauro Carvalho Chehaband to be part of a library. Therefore the same directory
207cd238effSMauro Carvalho Chehabmay contain both a built-in.a and a lib.a file.
208cd238effSMauro Carvalho Chehab
209cd238effSMauro Carvalho ChehabExample::
210cd238effSMauro Carvalho Chehab
211cd238effSMauro Carvalho Chehab  #arch/x86/lib/Makefile
212cd238effSMauro Carvalho Chehab  lib-y    := delay.o
213cd238effSMauro Carvalho Chehab
214cd238effSMauro Carvalho ChehabThis will create a library lib.a based on delay.o. For kbuild to
215cd238effSMauro Carvalho Chehabactually recognize that there is a lib.a being built, the directory
216cd238effSMauro Carvalho Chehabshall be listed in libs-y.
217cd238effSMauro Carvalho Chehab
2181a4c1c9dSJani NikulaSee also `List directories to visit when descending`_.
219cd238effSMauro Carvalho Chehab
220*2f0e2a39SJani NikulaUse of lib-y is normally restricted to ``lib/`` and ``arch/*/lib``.
221cd238effSMauro Carvalho Chehab
2221a4c1c9dSJani NikulaDescending down in directories
2231a4c1c9dSJani Nikula------------------------------
224cd238effSMauro Carvalho Chehab
225cd238effSMauro Carvalho ChehabA Makefile is only responsible for building objects in its own
226cd238effSMauro Carvalho Chehabdirectory. Files in subdirectories should be taken care of by
227cd238effSMauro Carvalho ChehabMakefiles in these subdirs. The build system will automatically
228cd238effSMauro Carvalho Chehabinvoke make recursively in subdirectories, provided you let it know of
229cd238effSMauro Carvalho Chehabthem.
230cd238effSMauro Carvalho Chehab
231cd238effSMauro Carvalho ChehabTo do so, obj-y and obj-m are used.
232cd238effSMauro Carvalho Chehabext2 lives in a separate directory, and the Makefile present in fs/
233cd238effSMauro Carvalho Chehabtells kbuild to descend down using the following assignment.
234cd238effSMauro Carvalho Chehab
235cd238effSMauro Carvalho ChehabExample::
236cd238effSMauro Carvalho Chehab
237cd238effSMauro Carvalho Chehab  #fs/Makefile
238cd238effSMauro Carvalho Chehab  obj-$(CONFIG_EXT2_FS) += ext2/
239cd238effSMauro Carvalho Chehab
240*2f0e2a39SJani NikulaIf CONFIG_EXT2_FS is set to either "y" (built-in) or "m" (modular)
241cd238effSMauro Carvalho Chehabthe corresponding obj- variable will be set, and kbuild will descend
242cd238effSMauro Carvalho Chehabdown in the ext2 directory.
24328f94a44SMasahiro Yamada
24428f94a44SMasahiro YamadaKbuild uses this information not only to decide that it needs to visit
24528f94a44SMasahiro Yamadathe directory, but also to decide whether or not to link objects from
24628f94a44SMasahiro Yamadathe directory into vmlinux.
24728f94a44SMasahiro Yamada
248*2f0e2a39SJani NikulaWhen Kbuild descends into the directory with "y", all built-in objects
24928f94a44SMasahiro Yamadafrom that directory are combined into the built-in.a, which will be
25028f94a44SMasahiro Yamadaeventually linked into vmlinux.
25128f94a44SMasahiro Yamada
252*2f0e2a39SJani NikulaWhen Kbuild descends into the directory with "m", in contrast, nothing
25328f94a44SMasahiro Yamadafrom that directory will be linked into vmlinux. If the Makefile in
25428f94a44SMasahiro Yamadathat directory specifies obj-y, those objects will be left orphan.
25528f94a44SMasahiro YamadaIt is very likely a bug of the Makefile or of dependencies in Kconfig.
256cd238effSMauro Carvalho Chehab
257c0ea806fSMasahiro YamadaKbuild also supports dedicated syntax, subdir-y and subdir-m, for
258c0ea806fSMasahiro Yamadadescending into subdirectories. It is a good fit when you know they
259c0ea806fSMasahiro Yamadado not contain kernel-space objects at all. A typical usage is to let
260c0ea806fSMasahiro YamadaKbuild descend into subdirectories to build tools.
261c0ea806fSMasahiro Yamada
262c0ea806fSMasahiro YamadaExamples::
263c0ea806fSMasahiro Yamada
264c0ea806fSMasahiro Yamada  # scripts/Makefile
265c0ea806fSMasahiro Yamada  subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins
266c0ea806fSMasahiro Yamada  subdir-$(CONFIG_MODVERSIONS) += genksyms
267c0ea806fSMasahiro Yamada  subdir-$(CONFIG_SECURITY_SELINUX) += selinux
268c0ea806fSMasahiro Yamada
269c0ea806fSMasahiro YamadaUnlike obj-y/m, subdir-y/m does not need the trailing slash since this
270c0ea806fSMasahiro Yamadasyntax is always used for directories.
271c0ea806fSMasahiro Yamada
272*2f0e2a39SJani NikulaIt is good practice to use a ``CONFIG_`` variable when assigning directory
273cd238effSMauro Carvalho Chehabnames. This allows kbuild to totally skip the directory if the
274*2f0e2a39SJani Nikulacorresponding ``CONFIG_`` option is neither "y" nor "m".
275cd238effSMauro Carvalho Chehab
2761a4c1c9dSJani NikulaNon-builtin vmlinux targets - extra-y
2771a4c1c9dSJani Nikula-------------------------------------
278d0e628cdSMasahiro Yamada
279d0e628cdSMasahiro Yamadaextra-y specifies targets which are needed for building vmlinux,
280d0e628cdSMasahiro Yamadabut not combined into built-in.a.
281d0e628cdSMasahiro Yamada
282d0e628cdSMasahiro YamadaExamples are:
283d0e628cdSMasahiro Yamada
28432164845SMasahiro Yamada1) vmlinux linker script
285d0e628cdSMasahiro Yamada
286d0e628cdSMasahiro Yamada   The linker script for vmlinux is located at
287d0e628cdSMasahiro Yamada   arch/$(SRCARCH)/kernel/vmlinux.lds
288d0e628cdSMasahiro Yamada
289d0e628cdSMasahiro YamadaExample::
290d0e628cdSMasahiro Yamada
291d0e628cdSMasahiro Yamada  # arch/x86/kernel/Makefile
292d0e628cdSMasahiro Yamada  extra-y	+= vmlinux.lds
293d0e628cdSMasahiro Yamada
294d0e628cdSMasahiro Yamada$(extra-y) should only contain targets needed for vmlinux.
295d0e628cdSMasahiro Yamada
296d0e628cdSMasahiro YamadaKbuild skips extra-y when vmlinux is apparently not a final goal.
297*2f0e2a39SJani Nikula(e.g. ``make modules``, or building external modules)
298d0e628cdSMasahiro Yamada
299d0e628cdSMasahiro YamadaIf you intend to build targets unconditionally, always-y (explained
300d0e628cdSMasahiro Yamadain the next section) is the correct syntax to use.
301d0e628cdSMasahiro Yamada
3021a4c1c9dSJani NikulaAlways built goals - always-y
3031a4c1c9dSJani Nikula-----------------------------
304d0e628cdSMasahiro Yamada
305d0e628cdSMasahiro Yamadaalways-y specifies targets which are literally always built when
306d0e628cdSMasahiro YamadaKbuild visits the Makefile.
307d0e628cdSMasahiro Yamada
308d0e628cdSMasahiro YamadaExample::
3099f1fe2bbSJani Nikula
310d0e628cdSMasahiro Yamada  # ./Kbuild
311d0e628cdSMasahiro Yamada  offsets-file := include/generated/asm-offsets.h
312d0e628cdSMasahiro Yamada  always-y += $(offsets-file)
313d0e628cdSMasahiro Yamada
3141a4c1c9dSJani NikulaCompilation flags
3151a4c1c9dSJani Nikula-----------------
316cd238effSMauro Carvalho Chehab
317cd238effSMauro Carvalho Chehabccflags-y, asflags-y and ldflags-y
318cd238effSMauro Carvalho Chehab  These three flags apply only to the kbuild makefile in which they
319cd238effSMauro Carvalho Chehab  are assigned. They are used for all the normal cc, as and ld
320cd238effSMauro Carvalho Chehab  invocations happening during a recursive build.
321cd238effSMauro Carvalho Chehab  Note: Flags with the same behaviour were previously named:
322cd238effSMauro Carvalho Chehab  EXTRA_CFLAGS, EXTRA_AFLAGS and EXTRA_LDFLAGS.
323cd238effSMauro Carvalho Chehab  They are still supported but their usage is deprecated.
324cd238effSMauro Carvalho Chehab
325cd238effSMauro Carvalho Chehab  ccflags-y specifies options for compiling with $(CC).
326cd238effSMauro Carvalho Chehab
327cd238effSMauro Carvalho Chehab  Example::
328cd238effSMauro Carvalho Chehab
329cd238effSMauro Carvalho Chehab    # drivers/acpi/acpica/Makefile
330cd238effSMauro Carvalho Chehab    ccflags-y				:= -Os -D_LINUX -DBUILDING_ACPICA
331cd238effSMauro Carvalho Chehab    ccflags-$(CONFIG_ACPI_DEBUG)	+= -DACPI_DEBUG_OUTPUT
332cd238effSMauro Carvalho Chehab
333cd238effSMauro Carvalho Chehab  This variable is necessary because the top Makefile owns the
334cd238effSMauro Carvalho Chehab  variable $(KBUILD_CFLAGS) and uses it for compilation flags for the
335cd238effSMauro Carvalho Chehab  entire tree.
336cd238effSMauro Carvalho Chehab
3375ef87263SMasahiro Yamada  asflags-y specifies assembler options.
338cd238effSMauro Carvalho Chehab
339cd238effSMauro Carvalho Chehab  Example::
340cd238effSMauro Carvalho Chehab
341cd238effSMauro Carvalho Chehab    #arch/sparc/kernel/Makefile
342cd238effSMauro Carvalho Chehab    asflags-y := -ansi
343cd238effSMauro Carvalho Chehab
344cd238effSMauro Carvalho Chehab  ldflags-y specifies options for linking with $(LD).
345cd238effSMauro Carvalho Chehab
346cd238effSMauro Carvalho Chehab  Example::
347cd238effSMauro Carvalho Chehab
348cd238effSMauro Carvalho Chehab    #arch/cris/boot/compressed/Makefile
349cd238effSMauro Carvalho Chehab    ldflags-y += -T $(srctree)/$(src)/decompress_$(arch-y).lds
350cd238effSMauro Carvalho Chehab
351cd238effSMauro Carvalho Chehabsubdir-ccflags-y, subdir-asflags-y
352cd238effSMauro Carvalho Chehab  The two flags listed above are similar to ccflags-y and asflags-y.
353cd238effSMauro Carvalho Chehab  The difference is that the subdir- variants have effect for the kbuild
354cd238effSMauro Carvalho Chehab  file where they are present and all subdirectories.
355cd238effSMauro Carvalho Chehab  Options specified using subdir-* are added to the commandline before
356cd238effSMauro Carvalho Chehab  the options specified using the non-subdir variants.
357cd238effSMauro Carvalho Chehab
358cd238effSMauro Carvalho Chehab  Example::
359cd238effSMauro Carvalho Chehab
360cd238effSMauro Carvalho Chehab    subdir-ccflags-y := -Werror
361cd238effSMauro Carvalho Chehab
36215d5761aSMasahiro Yamadaccflags-remove-y, asflags-remove-y
36315d5761aSMasahiro Yamada  These flags are used to remove particular flags for the compiler,
36415d5761aSMasahiro Yamada  assembler invocations.
36515d5761aSMasahiro Yamada
36615d5761aSMasahiro Yamada  Example::
36715d5761aSMasahiro Yamada
36815d5761aSMasahiro Yamada    ccflags-remove-$(CONFIG_MCOUNT) += -pg
36915d5761aSMasahiro Yamada
370cd238effSMauro Carvalho ChehabCFLAGS_$@, AFLAGS_$@
371cd238effSMauro Carvalho Chehab  CFLAGS_$@ and AFLAGS_$@ only apply to commands in current
372cd238effSMauro Carvalho Chehab  kbuild makefile.
373cd238effSMauro Carvalho Chehab
374cd238effSMauro Carvalho Chehab  $(CFLAGS_$@) specifies per-file options for $(CC).  The $@
375cd238effSMauro Carvalho Chehab  part has a literal value which specifies the file that it is for.
376cd238effSMauro Carvalho Chehab
37715d5761aSMasahiro Yamada  CFLAGS_$@ has the higher priority than ccflags-remove-y; CFLAGS_$@
37815d5761aSMasahiro Yamada  can re-add compiler flags that were removed by ccflags-remove-y.
37915d5761aSMasahiro Yamada
380cd238effSMauro Carvalho Chehab  Example::
381cd238effSMauro Carvalho Chehab
382cd238effSMauro Carvalho Chehab    # drivers/scsi/Makefile
383cd238effSMauro Carvalho Chehab    CFLAGS_aha152x.o =   -DAHA152X_STAT -DAUTOCONF
384cd238effSMauro Carvalho Chehab
3850653c358SHannes Reinecke  This line specify compilation flags for aha152x.o.
386cd238effSMauro Carvalho Chehab
387cd238effSMauro Carvalho Chehab  $(AFLAGS_$@) is a similar feature for source files in assembly
388cd238effSMauro Carvalho Chehab  languages.
389cd238effSMauro Carvalho Chehab
39015d5761aSMasahiro Yamada  AFLAGS_$@ has the higher priority than asflags-remove-y; AFLAGS_$@
39115d5761aSMasahiro Yamada  can re-add assembler flags that were removed by asflags-remove-y.
39215d5761aSMasahiro Yamada
393cd238effSMauro Carvalho Chehab  Example::
394cd238effSMauro Carvalho Chehab
395cd238effSMauro Carvalho Chehab    # arch/arm/kernel/Makefile
396cd238effSMauro Carvalho Chehab    AFLAGS_head.o        := -DTEXT_OFFSET=$(TEXT_OFFSET)
397cd238effSMauro Carvalho Chehab    AFLAGS_crunch-bits.o := -Wa,-mcpu=ep9312
398cd238effSMauro Carvalho Chehab    AFLAGS_iwmmxt.o      := -Wa,-mcpu=iwmmxt
399cd238effSMauro Carvalho Chehab
4001a4c1c9dSJani NikulaDependency tracking
4011a4c1c9dSJani Nikula-------------------
402cd238effSMauro Carvalho Chehab
403cd238effSMauro Carvalho ChehabKbuild tracks dependencies on the following:
40416886949SMauro Carvalho Chehab
405*2f0e2a39SJani Nikula1) All prerequisite files (both ``*.c`` and ``*.h``)
406*2f0e2a39SJani Nikula2) ``CONFIG_`` options used in all prerequisite files
407cd238effSMauro Carvalho Chehab3) Command-line used to compile target
408cd238effSMauro Carvalho Chehab
409cd238effSMauro Carvalho ChehabThus, if you change an option to $(CC) all affected files will
410cd238effSMauro Carvalho Chehabbe re-compiled.
411cd238effSMauro Carvalho Chehab
4121a4c1c9dSJani NikulaCustom Rules
4131a4c1c9dSJani Nikula------------
414cd238effSMauro Carvalho Chehab
41541cac083SMasahiro YamadaCustom rules are used when the kbuild infrastructure does
416cd238effSMauro Carvalho Chehabnot provide the required support. A typical example is
417cd238effSMauro Carvalho Chehabheader files generated during the build process.
418cd238effSMauro Carvalho ChehabAnother example are the architecture-specific Makefiles which
41941cac083SMasahiro Yamadaneed custom rules to prepare boot images etc.
420cd238effSMauro Carvalho Chehab
42141cac083SMasahiro YamadaCustom rules are written as normal Make rules.
422cd238effSMauro Carvalho ChehabKbuild is not executing in the directory where the Makefile is
42341cac083SMasahiro Yamadalocated, so all custom rules shall use a relative
424cd238effSMauro Carvalho Chehabpath to prerequisite files and target files.
425cd238effSMauro Carvalho Chehab
42641cac083SMasahiro YamadaTwo variables are used when defining custom rules:
427cd238effSMauro Carvalho Chehab
428cd238effSMauro Carvalho Chehab$(src)
429cd238effSMauro Carvalho Chehab  $(src) is a relative path which points to the directory
430cd238effSMauro Carvalho Chehab  where the Makefile is located. Always use $(src) when
431cd238effSMauro Carvalho Chehab  referring to files located in the src tree.
432cd238effSMauro Carvalho Chehab
433cd238effSMauro Carvalho Chehab$(obj)
434cd238effSMauro Carvalho Chehab  $(obj) is a relative path which points to the directory
435cd238effSMauro Carvalho Chehab  where the target is saved. Always use $(obj) when
436cd238effSMauro Carvalho Chehab  referring to generated files.
437cd238effSMauro Carvalho Chehab
438cd238effSMauro Carvalho Chehab  Example::
439cd238effSMauro Carvalho Chehab
440cd238effSMauro Carvalho Chehab    #drivers/scsi/Makefile
441cd238effSMauro Carvalho Chehab    $(obj)/53c8xx_d.h: $(src)/53c7,8xx.scr $(src)/script_asm.pl
442cd238effSMauro Carvalho Chehab    $(CPP) -DCHIP=810 - < $< | ... $(src)/script_asm.pl
443cd238effSMauro Carvalho Chehab
44441cac083SMasahiro Yamada  This is a custom rule, following the normal syntax
445cd238effSMauro Carvalho Chehab  required by make.
446cd238effSMauro Carvalho Chehab
447cd238effSMauro Carvalho Chehab  The target file depends on two prerequisite files. References
448cd238effSMauro Carvalho Chehab  to the target file are prefixed with $(obj), references
449cd238effSMauro Carvalho Chehab  to prerequisites are referenced with $(src) (because they are not
450cd238effSMauro Carvalho Chehab  generated files).
451cd238effSMauro Carvalho Chehab
452cd238effSMauro Carvalho Chehab$(kecho)
453cd238effSMauro Carvalho Chehab  echoing information to user in a rule is often a good practice
454*2f0e2a39SJani Nikula  but when execution ``make -s`` one does not expect to see any output
455cd238effSMauro Carvalho Chehab  except for warnings/errors.
456cd238effSMauro Carvalho Chehab  To support this kbuild defines $(kecho) which will echo out the
457*2f0e2a39SJani Nikula  text following $(kecho) to stdout except if ``make -s`` is used.
458cd238effSMauro Carvalho Chehab
459cd238effSMauro Carvalho Chehab  Example::
460cd238effSMauro Carvalho Chehab
46141cac083SMasahiro Yamada    # arch/arm/Makefile
46241cac083SMasahiro Yamada    $(BOOT_TARGETS): vmlinux
46341cac083SMasahiro Yamada            $(Q)$(MAKE) $(build)=$(boot) MACHINE=$(MACHINE) $(boot)/$@
46441cac083SMasahiro Yamada            @$(kecho) '  Kernel: $(boot)/$@ is ready'
465cd238effSMauro Carvalho Chehab
46683d98d73SMasahiro Yamada  When kbuild is executing with KBUILD_VERBOSE unset, then only a shorthand
46741cac083SMasahiro Yamada  of a command is normally displayed.
46841cac083SMasahiro Yamada  To enable this behaviour for custom commands kbuild requires
46941cac083SMasahiro Yamada  two variables to be set::
47041cac083SMasahiro Yamada
47141cac083SMasahiro Yamada    quiet_cmd_<command> - what shall be echoed
47241cac083SMasahiro Yamada          cmd_<command> - the command to execute
47341cac083SMasahiro Yamada
47441cac083SMasahiro Yamada  Example::
47541cac083SMasahiro Yamada
47641cac083SMasahiro Yamada    # lib/Makefile
47741cac083SMasahiro Yamada    quiet_cmd_crc32 = GEN     $@
47841cac083SMasahiro Yamada          cmd_crc32 = $< > $@
47941cac083SMasahiro Yamada
48041cac083SMasahiro Yamada    $(obj)/crc32table.h: $(obj)/gen_crc32table
48141cac083SMasahiro Yamada            $(call cmd,crc32)
48241cac083SMasahiro Yamada
4839f1fe2bbSJani Nikula  When updating the $(obj)/crc32table.h target, the line::
48441cac083SMasahiro Yamada
48541cac083SMasahiro Yamada    GEN     lib/crc32table.h
48641cac083SMasahiro Yamada
487*2f0e2a39SJani Nikula  will be displayed with ``make KBUILD_VERBOSE=``.
488cd238effSMauro Carvalho Chehab
4891a4c1c9dSJani NikulaCommand change detection
4901a4c1c9dSJani Nikula------------------------
49139bb232aSMasahiro Yamada
49239bb232aSMasahiro YamadaWhen the rule is evaluated, timestamps are compared between the target
49339bb232aSMasahiro Yamadaand its prerequisite files. GNU Make updates the target when any of the
49439bb232aSMasahiro Yamadaprerequisites is newer than that.
49539bb232aSMasahiro Yamada
49639bb232aSMasahiro YamadaThe target should be rebuilt also when the command line has changed
49739bb232aSMasahiro Yamadasince the last invocation. This is not supported by Make itself, so
49839bb232aSMasahiro YamadaKbuild achieves this by a kind of meta-programming.
49939bb232aSMasahiro Yamada
50039bb232aSMasahiro Yamadaif_changed is the macro used for this purpose, in the following form::
50139bb232aSMasahiro Yamada
50239bb232aSMasahiro Yamada  quiet_cmd_<command> = ...
50339bb232aSMasahiro Yamada        cmd_<command> = ...
50439bb232aSMasahiro Yamada
50539bb232aSMasahiro Yamada  <target>: <source(s)> FORCE
50639bb232aSMasahiro Yamada          $(call if_changed,<command>)
50739bb232aSMasahiro Yamada
50839bb232aSMasahiro YamadaAny target that utilizes if_changed must be listed in $(targets),
50939bb232aSMasahiro Yamadaotherwise the command line check will fail, and the target will
51039bb232aSMasahiro Yamadaalways be built.
51139bb232aSMasahiro Yamada
51239bb232aSMasahiro YamadaIf the target is already listed in the recognized syntax such as
51339bb232aSMasahiro Yamadaobj-y/m, lib-y/m, extra-y/m, always-y/m, hostprogs, userprogs, Kbuild
51439bb232aSMasahiro Yamadaautomatically adds it to $(targets). Otherwise, the target must be
51539bb232aSMasahiro Yamadaexplicitly added to $(targets).
51639bb232aSMasahiro Yamada
51739bb232aSMasahiro YamadaAssignments to $(targets) are without $(obj)/ prefix. if_changed may be
5181a4c1c9dSJani Nikulaused in conjunction with custom rules as defined in `Custom Rules`_.
51939bb232aSMasahiro Yamada
52039bb232aSMasahiro YamadaNote: It is a typical mistake to forget the FORCE prerequisite.
52139bb232aSMasahiro YamadaAnother common pitfall is that whitespace is sometimes significant; for
52239bb232aSMasahiro Yamadainstance, the below will fail (note the extra space after the comma)::
52339bb232aSMasahiro Yamada
52439bb232aSMasahiro Yamada  target: source(s) FORCE
52539bb232aSMasahiro Yamada
52639bb232aSMasahiro Yamada**WRONG!**	$(call if_changed, objcopy)
52739bb232aSMasahiro Yamada
52839bb232aSMasahiro YamadaNote:
52939bb232aSMasahiro Yamada  if_changed should not be used more than once per target.
53039bb232aSMasahiro Yamada  It stores the executed command in a corresponding .cmd
53139bb232aSMasahiro Yamada  file and multiple calls would result in overwrites and
53239bb232aSMasahiro Yamada  unwanted results when the target is up to date and only the
53339bb232aSMasahiro Yamada  tests on changed commands trigger execution of commands.
53439bb232aSMasahiro Yamada
5351a4c1c9dSJani Nikula$(CC) support functions
5361a4c1c9dSJani Nikula-----------------------
537cd238effSMauro Carvalho Chehab
538cd238effSMauro Carvalho ChehabThe kernel may be built with several different versions of
539cd238effSMauro Carvalho Chehab$(CC), each supporting a unique set of features and options.
540cd238effSMauro Carvalho Chehabkbuild provides basic support to check for valid options for $(CC).
541cd238effSMauro Carvalho Chehab$(CC) is usually the gcc compiler, but other alternatives are
542cd238effSMauro Carvalho Chehabavailable.
543cd238effSMauro Carvalho Chehab
544cd238effSMauro Carvalho Chehabas-option
545cd238effSMauro Carvalho Chehab  as-option is used to check if $(CC) -- when used to compile
546*2f0e2a39SJani Nikula  assembler (``*.S``) files -- supports the given option. An optional
547cd238effSMauro Carvalho Chehab  second option may be specified if the first option is not supported.
548cd238effSMauro Carvalho Chehab
549cd238effSMauro Carvalho Chehab  Example::
550cd238effSMauro Carvalho Chehab
551cd238effSMauro Carvalho Chehab    #arch/sh/Makefile
552cd238effSMauro Carvalho Chehab    cflags-y += $(call as-option,-Wa$(comma)-isa=$(isa-y),)
553cd238effSMauro Carvalho Chehab
554cd238effSMauro Carvalho Chehab  In the above example, cflags-y will be assigned the option
555cd238effSMauro Carvalho Chehab  -Wa$(comma)-isa=$(isa-y) if it is supported by $(CC).
556cd238effSMauro Carvalho Chehab  The second argument is optional, and if supplied will be used
557cd238effSMauro Carvalho Chehab  if first argument is not supported.
558cd238effSMauro Carvalho Chehab
559cd238effSMauro Carvalho Chehabas-instr
560cd238effSMauro Carvalho Chehab  as-instr checks if the assembler reports a specific instruction
561cd238effSMauro Carvalho Chehab  and then outputs either option1 or option2
562cd238effSMauro Carvalho Chehab  C escapes are supported in the test instruction
5635ef87263SMasahiro Yamada  Note: as-instr-option uses KBUILD_AFLAGS for assembler options
564cd238effSMauro Carvalho Chehab
565cd238effSMauro Carvalho Chehabcc-option
566cd238effSMauro Carvalho Chehab  cc-option is used to check if $(CC) supports a given option, and if
567cd238effSMauro Carvalho Chehab  not supported to use an optional second option.
568cd238effSMauro Carvalho Chehab
569cd238effSMauro Carvalho Chehab  Example::
570cd238effSMauro Carvalho Chehab
571cd238effSMauro Carvalho Chehab    #arch/x86/Makefile
572cd238effSMauro Carvalho Chehab    cflags-y += $(call cc-option,-march=pentium-mmx,-march=i586)
573cd238effSMauro Carvalho Chehab
574cd238effSMauro Carvalho Chehab  In the above example, cflags-y will be assigned the option
575cd238effSMauro Carvalho Chehab  -march=pentium-mmx if supported by $(CC), otherwise -march=i586.
576cd238effSMauro Carvalho Chehab  The second argument to cc-option is optional, and if omitted,
577cd238effSMauro Carvalho Chehab  cflags-y will be assigned no value if first option is not supported.
578cd238effSMauro Carvalho Chehab  Note: cc-option uses KBUILD_CFLAGS for $(CC) options
579cd238effSMauro Carvalho Chehab
580cd238effSMauro Carvalho Chehabcc-option-yn
581cd238effSMauro Carvalho Chehab  cc-option-yn is used to check if gcc supports a given option
582*2f0e2a39SJani Nikula  and return "y" if supported, otherwise "n".
583cd238effSMauro Carvalho Chehab
584cd238effSMauro Carvalho Chehab  Example::
585cd238effSMauro Carvalho Chehab
586cd238effSMauro Carvalho Chehab    #arch/ppc/Makefile
587cd238effSMauro Carvalho Chehab    biarch := $(call cc-option-yn, -m32)
588cd238effSMauro Carvalho Chehab    aflags-$(biarch) += -a32
589cd238effSMauro Carvalho Chehab    cflags-$(biarch) += -m32
590cd238effSMauro Carvalho Chehab
591cd238effSMauro Carvalho Chehab  In the above example, $(biarch) is set to y if $(CC) supports the -m32
592*2f0e2a39SJani Nikula  option. When $(biarch) equals "y", the expanded variables $(aflags-y)
593cd238effSMauro Carvalho Chehab  and $(cflags-y) will be assigned the values -a32 and -m32,
594cd238effSMauro Carvalho Chehab  respectively.
5959f1fe2bbSJani Nikula
596cd238effSMauro Carvalho Chehab  Note: cc-option-yn uses KBUILD_CFLAGS for $(CC) options
597cd238effSMauro Carvalho Chehab
598cd238effSMauro Carvalho Chehabcc-disable-warning
599cd238effSMauro Carvalho Chehab  cc-disable-warning checks if gcc supports a given warning and returns
600cd238effSMauro Carvalho Chehab  the commandline switch to disable it. This special function is needed,
601cd238effSMauro Carvalho Chehab  because gcc 4.4 and later accept any unknown -Wno-* option and only
602cd238effSMauro Carvalho Chehab  warn about it if there is another warning in the source file.
603cd238effSMauro Carvalho Chehab
604cd238effSMauro Carvalho Chehab  Example::
605cd238effSMauro Carvalho Chehab
606cd238effSMauro Carvalho Chehab    KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
607cd238effSMauro Carvalho Chehab
608cd238effSMauro Carvalho Chehab  In the above example, -Wno-unused-but-set-variable will be added to
609cd238effSMauro Carvalho Chehab  KBUILD_CFLAGS only if gcc really accepts it.
610cd238effSMauro Carvalho Chehab
61188b61e3bSNick Desaulniersgcc-min-version
61288b61e3bSNick Desaulniers  gcc-min-version tests if the value of $(CONFIG_GCC_VERSION) is greater than
61388b61e3bSNick Desaulniers  or equal to the provided value and evaluates to y if so.
614cd238effSMauro Carvalho Chehab
615cd238effSMauro Carvalho Chehab  Example::
616cd238effSMauro Carvalho Chehab
61788b61e3bSNick Desaulniers    cflags-$(call gcc-min-version, 70100) := -foo
618cd238effSMauro Carvalho Chehab
61988b61e3bSNick Desaulniers  In this example, cflags-y will be assigned the value -foo if $(CC) is gcc and
62088b61e3bSNick Desaulniers  $(CONFIG_GCC_VERSION) is >= 7.1.
62188b61e3bSNick Desaulniers
62288b61e3bSNick Desaulniersclang-min-version
62388b61e3bSNick Desaulniers  clang-min-version tests if the value of $(CONFIG_CLANG_VERSION) is greater
62488b61e3bSNick Desaulniers  than or equal to the provided value and evaluates to y if so.
62588b61e3bSNick Desaulniers
62688b61e3bSNick Desaulniers  Example::
62788b61e3bSNick Desaulniers
62888b61e3bSNick Desaulniers    cflags-$(call clang-min-version, 110000) := -foo
62988b61e3bSNick Desaulniers
63088b61e3bSNick Desaulniers  In this example, cflags-y will be assigned the value -foo if $(CC) is clang
63188b61e3bSNick Desaulniers  and $(CONFIG_CLANG_VERSION) is >= 11.0.0.
632cd238effSMauro Carvalho Chehab
633cd238effSMauro Carvalho Chehabcc-cross-prefix
634cd238effSMauro Carvalho Chehab  cc-cross-prefix is used to check if there exists a $(CC) in path with
635cd238effSMauro Carvalho Chehab  one of the listed prefixes. The first prefix where there exist a
636cd238effSMauro Carvalho Chehab  prefix$(CC) in the PATH is returned - and if no prefix$(CC) is found
637cd238effSMauro Carvalho Chehab  then nothing is returned.
6389f1fe2bbSJani Nikula
639cd238effSMauro Carvalho Chehab  Additional prefixes are separated by a single space in the
640cd238effSMauro Carvalho Chehab  call of cc-cross-prefix.
6419f1fe2bbSJani Nikula
642cd238effSMauro Carvalho Chehab  This functionality is useful for architecture Makefiles that try
643cd238effSMauro Carvalho Chehab  to set CROSS_COMPILE to well-known values but may have several
644cd238effSMauro Carvalho Chehab  values to select between.
6459f1fe2bbSJani Nikula
646cd238effSMauro Carvalho Chehab  It is recommended only to try to set CROSS_COMPILE if it is a cross
647cd238effSMauro Carvalho Chehab  build (host arch is different from target arch). And if CROSS_COMPILE
648cd238effSMauro Carvalho Chehab  is already set then leave it with the old value.
649cd238effSMauro Carvalho Chehab
650cd238effSMauro Carvalho Chehab  Example::
651cd238effSMauro Carvalho Chehab
652cd238effSMauro Carvalho Chehab    #arch/m68k/Makefile
653cd238effSMauro Carvalho Chehab    ifneq ($(SUBARCH),$(ARCH))
654cd238effSMauro Carvalho Chehab            ifeq ($(CROSS_COMPILE),)
655cd238effSMauro Carvalho Chehab                    CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu-)
656cd238effSMauro Carvalho Chehab            endif
657cd238effSMauro Carvalho Chehab    endif
658cd238effSMauro Carvalho Chehab
6591a4c1c9dSJani Nikula$(LD) support functions
6601a4c1c9dSJani Nikula-----------------------
661cd238effSMauro Carvalho Chehab
662cd238effSMauro Carvalho Chehabld-option
663cd238effSMauro Carvalho Chehab  ld-option is used to check if $(LD) supports the supplied option.
664cd238effSMauro Carvalho Chehab  ld-option takes two options as arguments.
6659f1fe2bbSJani Nikula
666cd238effSMauro Carvalho Chehab  The second argument is an optional option that can be used if the
667cd238effSMauro Carvalho Chehab  first option is not supported by $(LD).
668cd238effSMauro Carvalho Chehab
669cd238effSMauro Carvalho Chehab  Example::
670cd238effSMauro Carvalho Chehab
671cd238effSMauro Carvalho Chehab    #Makefile
672cd238effSMauro Carvalho Chehab    LDFLAGS_vmlinux += $(call ld-option, -X)
673cd238effSMauro Carvalho Chehab
6741a4c1c9dSJani NikulaScript invocation
6751a4c1c9dSJani Nikula-----------------
676eb38f37cSLukas Bulwahn
677eb38f37cSLukas BulwahnMake rules may invoke scripts to build the kernel. The rules shall
678eb38f37cSLukas Bulwahnalways provide the appropriate interpreter to execute the script. They
679eb38f37cSLukas Bulwahnshall not rely on the execute bits being set, and shall not invoke the
680eb38f37cSLukas Bulwahnscript directly. For the convenience of manual script invocation, such
681eb38f37cSLukas Bulwahnas invoking ./scripts/checkpatch.pl, it is recommended to set execute
682eb38f37cSLukas Bulwahnbits on the scripts nonetheless.
683eb38f37cSLukas Bulwahn
684eb38f37cSLukas BulwahnKbuild provides variables $(CONFIG_SHELL), $(AWK), $(PERL),
685d8d2d382SMasahiro Yamadaand $(PYTHON3) to refer to interpreters for the respective
686eb38f37cSLukas Bulwahnscripts.
687eb38f37cSLukas Bulwahn
688eb38f37cSLukas BulwahnExample::
689eb38f37cSLukas Bulwahn
690eb38f37cSLukas Bulwahn  #Makefile
691eb38f37cSLukas Bulwahn  cmd_depmod = $(CONFIG_SHELL) $(srctree)/scripts/depmod.sh $(DEPMOD) \
692eb38f37cSLukas Bulwahn          $(KERNELRELEASE)
693cd238effSMauro Carvalho Chehab
6941a4c1c9dSJani NikulaHost Program support
6951a4c1c9dSJani Nikula====================
696cd238effSMauro Carvalho Chehab
697cd238effSMauro Carvalho ChehabKbuild supports building executables on the host for use during the
698cd238effSMauro Carvalho Chehabcompilation stage.
6999f1fe2bbSJani Nikula
700cd238effSMauro Carvalho ChehabTwo steps are required in order to use a host executable.
701cd238effSMauro Carvalho Chehab
702cd238effSMauro Carvalho ChehabThe first step is to tell kbuild that a host program exists. This is
703*2f0e2a39SJani Nikuladone utilising the variable ``hostprogs``.
704cd238effSMauro Carvalho Chehab
705cd238effSMauro Carvalho ChehabThe second step is to add an explicit dependency to the executable.
706cd238effSMauro Carvalho ChehabThis can be done in two ways. Either add the dependency in a rule,
707*2f0e2a39SJani Nikulaor utilise the variable ``always-y``.
708cd238effSMauro Carvalho ChehabBoth possibilities are described in the following.
709cd238effSMauro Carvalho Chehab
7101a4c1c9dSJani NikulaSimple Host Program
7111a4c1c9dSJani Nikula-------------------
712cd238effSMauro Carvalho Chehab
713cd238effSMauro Carvalho ChehabIn some cases there is a need to compile and run a program on the
714cd238effSMauro Carvalho Chehabcomputer where the build is running.
7159f1fe2bbSJani Nikula
716cd238effSMauro Carvalho ChehabThe following line tells kbuild that the program bin2hex shall be
717cd238effSMauro Carvalho Chehabbuilt on the build host.
718cd238effSMauro Carvalho Chehab
719cd238effSMauro Carvalho ChehabExample::
720cd238effSMauro Carvalho Chehab
7215f2fb52fSMasahiro Yamada  hostprogs := bin2hex
722cd238effSMauro Carvalho Chehab
723cd238effSMauro Carvalho ChehabKbuild assumes in the above example that bin2hex is made from a single
724cd238effSMauro Carvalho Chehabc-source file named bin2hex.c located in the same directory as
725cd238effSMauro Carvalho Chehabthe Makefile.
726cd238effSMauro Carvalho Chehab
7271a4c1c9dSJani NikulaComposite Host Programs
7281a4c1c9dSJani Nikula-----------------------
729cd238effSMauro Carvalho Chehab
730cd238effSMauro Carvalho ChehabHost programs can be made up based on composite objects.
731cd238effSMauro Carvalho ChehabThe syntax used to define composite objects for host programs is
732cd238effSMauro Carvalho Chehabsimilar to the syntax used for kernel objects.
733cd238effSMauro Carvalho Chehab$(<executable>-objs) lists all objects used to link the final
734cd238effSMauro Carvalho Chehabexecutable.
735cd238effSMauro Carvalho Chehab
736cd238effSMauro Carvalho ChehabExample::
737cd238effSMauro Carvalho Chehab
738cd238effSMauro Carvalho Chehab  #scripts/lxdialog/Makefile
7395f2fb52fSMasahiro Yamada  hostprogs     := lxdialog
740cd238effSMauro Carvalho Chehab  lxdialog-objs := checklist.o lxdialog.o
741cd238effSMauro Carvalho Chehab
742cd238effSMauro Carvalho ChehabObjects with extension .o are compiled from the corresponding .c
743cd238effSMauro Carvalho Chehabfiles. In the above example, checklist.c is compiled to checklist.o
744cd238effSMauro Carvalho Chehaband lxdialog.c is compiled to lxdialog.o.
745cd238effSMauro Carvalho Chehab
746cd238effSMauro Carvalho ChehabFinally, the two .o files are linked to the executable, lxdialog.
747cd238effSMauro Carvalho ChehabNote: The syntax <executable>-y is not permitted for host-programs.
748cd238effSMauro Carvalho Chehab
7491a4c1c9dSJani NikulaUsing C++ for host programs
7501a4c1c9dSJani Nikula---------------------------
751cd238effSMauro Carvalho Chehab
752cd238effSMauro Carvalho Chehabkbuild offers support for host programs written in C++. This was
753cd238effSMauro Carvalho Chehabintroduced solely to support kconfig, and is not recommended
754cd238effSMauro Carvalho Chehabfor general use.
755cd238effSMauro Carvalho Chehab
756cd238effSMauro Carvalho ChehabExample::
757cd238effSMauro Carvalho Chehab
758cd238effSMauro Carvalho Chehab  #scripts/kconfig/Makefile
7595f2fb52fSMasahiro Yamada  hostprogs     := qconf
760cd238effSMauro Carvalho Chehab  qconf-cxxobjs := qconf.o
761cd238effSMauro Carvalho Chehab
762cd238effSMauro Carvalho ChehabIn the example above the executable is composed of the C++ file
763cd238effSMauro Carvalho Chehabqconf.cc - identified by $(qconf-cxxobjs).
764cd238effSMauro Carvalho Chehab
765cd238effSMauro Carvalho ChehabIf qconf is composed of a mixture of .c and .cc files, then an
766cd238effSMauro Carvalho Chehabadditional line can be used to identify this.
767cd238effSMauro Carvalho Chehab
768cd238effSMauro Carvalho ChehabExample::
769cd238effSMauro Carvalho Chehab
770cd238effSMauro Carvalho Chehab  #scripts/kconfig/Makefile
7715f2fb52fSMasahiro Yamada  hostprogs     := qconf
772cd238effSMauro Carvalho Chehab  qconf-cxxobjs := qconf.o
773cd238effSMauro Carvalho Chehab  qconf-objs    := check.o
774cd238effSMauro Carvalho Chehab
7751a4c1c9dSJani NikulaUsing Rust for host programs
7761a4c1c9dSJani Nikula----------------------------
777d07479b2SMiguel Ojeda
778d07479b2SMiguel OjedaKbuild offers support for host programs written in Rust. However,
779d07479b2SMiguel Ojedasince a Rust toolchain is not mandatory for kernel compilation,
780d07479b2SMiguel Ojedait may only be used in scenarios where Rust is required to be
781d07479b2SMiguel Ojedaavailable (e.g. when  ``CONFIG_RUST`` is enabled).
782d07479b2SMiguel Ojeda
783d07479b2SMiguel OjedaExample::
784d07479b2SMiguel Ojeda
785d07479b2SMiguel Ojeda  hostprogs     := target
786d07479b2SMiguel Ojeda  target-rust   := y
787d07479b2SMiguel Ojeda
788d07479b2SMiguel OjedaKbuild will compile ``target`` using ``target.rs`` as the crate root,
789d07479b2SMiguel Ojedalocated in the same directory as the ``Makefile``. The crate may
790d07479b2SMiguel Ojedaconsist of several source files (see ``samples/rust/hostprogs``).
791d07479b2SMiguel Ojeda
7921a4c1c9dSJani NikulaControlling compiler options for host programs
7931a4c1c9dSJani Nikula----------------------------------------------
794cd238effSMauro Carvalho Chehab
795cd238effSMauro Carvalho ChehabWhen compiling host programs, it is possible to set specific flags.
796cd238effSMauro Carvalho ChehabThe programs will always be compiled utilising $(HOSTCC) passed
797cd238effSMauro Carvalho Chehabthe options specified in $(KBUILD_HOSTCFLAGS).
7989f1fe2bbSJani Nikula
799cd238effSMauro Carvalho ChehabTo set flags that will take effect for all host programs created
800cd238effSMauro Carvalho Chehabin that Makefile, use the variable HOST_EXTRACFLAGS.
801cd238effSMauro Carvalho Chehab
802cd238effSMauro Carvalho ChehabExample::
803cd238effSMauro Carvalho Chehab
804cd238effSMauro Carvalho Chehab  #scripts/lxdialog/Makefile
805cd238effSMauro Carvalho Chehab  HOST_EXTRACFLAGS += -I/usr/include/ncurses
806cd238effSMauro Carvalho Chehab
807cd238effSMauro Carvalho ChehabTo set specific flags for a single file the following construction
808cd238effSMauro Carvalho Chehabis used:
809cd238effSMauro Carvalho Chehab
810cd238effSMauro Carvalho ChehabExample::
811cd238effSMauro Carvalho Chehab
812cd238effSMauro Carvalho Chehab  #arch/ppc64/boot/Makefile
813cd238effSMauro Carvalho Chehab  HOSTCFLAGS_piggyback.o := -DKERNELBASE=$(KERNELBASE)
814cd238effSMauro Carvalho Chehab
815cd238effSMauro Carvalho ChehabIt is also possible to specify additional options to the linker.
816cd238effSMauro Carvalho Chehab
817cd238effSMauro Carvalho ChehabExample::
818cd238effSMauro Carvalho Chehab
819cd238effSMauro Carvalho Chehab  #scripts/kconfig/Makefile
820cd238effSMauro Carvalho Chehab  HOSTLDLIBS_qconf := -L$(QTDIR)/lib
821cd238effSMauro Carvalho Chehab
822cd238effSMauro Carvalho ChehabWhen linking qconf, it will be passed the extra option
823*2f0e2a39SJani Nikula``-L$(QTDIR)/lib``.
824cd238effSMauro Carvalho Chehab
8251a4c1c9dSJani NikulaWhen host programs are actually built
8261a4c1c9dSJani Nikula-------------------------------------
827cd238effSMauro Carvalho Chehab
828cd238effSMauro Carvalho ChehabKbuild will only build host-programs when they are referenced
829cd238effSMauro Carvalho Chehabas a prerequisite.
8309f1fe2bbSJani Nikula
831cd238effSMauro Carvalho ChehabThis is possible in two ways:
832cd238effSMauro Carvalho Chehab
83341cac083SMasahiro Yamada(1) List the prerequisite explicitly in a custom rule.
834cd238effSMauro Carvalho Chehab
835cd238effSMauro Carvalho Chehab    Example::
836cd238effSMauro Carvalho Chehab
837cd238effSMauro Carvalho Chehab      #drivers/pci/Makefile
8385f2fb52fSMasahiro Yamada      hostprogs := gen-devlist
839cd238effSMauro Carvalho Chehab      $(obj)/devlist.h: $(src)/pci.ids $(obj)/gen-devlist
840cd238effSMauro Carvalho Chehab      ( cd $(obj); ./gen-devlist ) < $<
841cd238effSMauro Carvalho Chehab
842cd238effSMauro Carvalho Chehab    The target $(obj)/devlist.h will not be built before
843cd238effSMauro Carvalho Chehab    $(obj)/gen-devlist is updated. Note that references to
84441cac083SMasahiro Yamada    the host programs in custom rules must be prefixed with $(obj).
845cd238effSMauro Carvalho Chehab
8465f2fb52fSMasahiro Yamada(2) Use always-y
847cd238effSMauro Carvalho Chehab
84841cac083SMasahiro Yamada    When there is no suitable custom rule, and the host program
8495f2fb52fSMasahiro Yamada    shall be built when a makefile is entered, the always-y
850cd238effSMauro Carvalho Chehab    variable shall be used.
851cd238effSMauro Carvalho Chehab
852cd238effSMauro Carvalho Chehab    Example::
853cd238effSMauro Carvalho Chehab
854cd238effSMauro Carvalho Chehab      #scripts/lxdialog/Makefile
8555f2fb52fSMasahiro Yamada      hostprogs     := lxdialog
8565f2fb52fSMasahiro Yamada      always-y      := $(hostprogs)
857cd238effSMauro Carvalho Chehab
8589f1fe2bbSJani Nikula    Kbuild provides the following shorthand for this::
859faabed29SMasahiro Yamada
860faabed29SMasahiro Yamada      hostprogs-always-y := lxdialog
861faabed29SMasahiro Yamada
862cd238effSMauro Carvalho Chehab    This will tell kbuild to build lxdialog even if not referenced in
863cd238effSMauro Carvalho Chehab    any rule.
864cd238effSMauro Carvalho Chehab
8651a4c1c9dSJani NikulaUserspace Program support
8661a4c1c9dSJani Nikula=========================
867e079a08cSMasahiro Yamada
868e079a08cSMasahiro YamadaJust like host programs, Kbuild also supports building userspace executables
869e079a08cSMasahiro Yamadafor the target architecture (i.e. the same architecture as you are building
870e079a08cSMasahiro Yamadathe kernel for).
871e079a08cSMasahiro Yamada
872*2f0e2a39SJani NikulaThe syntax is quite similar. The difference is to use ``userprogs`` instead of
873*2f0e2a39SJani Nikula``hostprogs``.
874e079a08cSMasahiro Yamada
8751a4c1c9dSJani NikulaSimple Userspace Program
8761a4c1c9dSJani Nikula------------------------
877e079a08cSMasahiro Yamada
878e079a08cSMasahiro YamadaThe following line tells kbuild that the program bpf-direct shall be
879e079a08cSMasahiro Yamadabuilt for the target architecture.
880e079a08cSMasahiro Yamada
881e079a08cSMasahiro YamadaExample::
882e079a08cSMasahiro Yamada
883e079a08cSMasahiro Yamada  userprogs := bpf-direct
884e079a08cSMasahiro Yamada
885e079a08cSMasahiro YamadaKbuild assumes in the above example that bpf-direct is made from a
886e079a08cSMasahiro Yamadasingle C source file named bpf-direct.c located in the same directory
887e079a08cSMasahiro Yamadaas the Makefile.
888e079a08cSMasahiro Yamada
8891a4c1c9dSJani NikulaComposite Userspace Programs
8901a4c1c9dSJani Nikula----------------------------
891e079a08cSMasahiro Yamada
892e079a08cSMasahiro YamadaUserspace programs can be made up based on composite objects.
893e079a08cSMasahiro YamadaThe syntax used to define composite objects for userspace programs is
894e079a08cSMasahiro Yamadasimilar to the syntax used for kernel objects.
895e079a08cSMasahiro Yamada$(<executable>-objs) lists all objects used to link the final
896e079a08cSMasahiro Yamadaexecutable.
897e079a08cSMasahiro Yamada
898e079a08cSMasahiro YamadaExample::
899e079a08cSMasahiro Yamada
900e079a08cSMasahiro Yamada  #samples/seccomp/Makefile
901e079a08cSMasahiro Yamada  userprogs      := bpf-fancy
902e079a08cSMasahiro Yamada  bpf-fancy-objs := bpf-fancy.o bpf-helper.o
903e079a08cSMasahiro Yamada
904e079a08cSMasahiro YamadaObjects with extension .o are compiled from the corresponding .c
905e079a08cSMasahiro Yamadafiles. In the above example, bpf-fancy.c is compiled to bpf-fancy.o
906e079a08cSMasahiro Yamadaand bpf-helper.c is compiled to bpf-helper.o.
907e079a08cSMasahiro Yamada
908e079a08cSMasahiro YamadaFinally, the two .o files are linked to the executable, bpf-fancy.
909e079a08cSMasahiro YamadaNote: The syntax <executable>-y is not permitted for userspace programs.
910e079a08cSMasahiro Yamada
9111a4c1c9dSJani NikulaControlling compiler options for userspace programs
9121a4c1c9dSJani Nikula---------------------------------------------------
913e079a08cSMasahiro Yamada
914e079a08cSMasahiro YamadaWhen compiling userspace programs, it is possible to set specific flags.
915e079a08cSMasahiro YamadaThe programs will always be compiled utilising $(CC) passed
916e079a08cSMasahiro Yamadathe options specified in $(KBUILD_USERCFLAGS).
9179f1fe2bbSJani Nikula
918e079a08cSMasahiro YamadaTo set flags that will take effect for all userspace programs created
919e079a08cSMasahiro Yamadain that Makefile, use the variable userccflags.
920e079a08cSMasahiro Yamada
921e079a08cSMasahiro YamadaExample::
922e079a08cSMasahiro Yamada
923e079a08cSMasahiro Yamada  # samples/seccomp/Makefile
924e079a08cSMasahiro Yamada  userccflags += -I usr/include
925e079a08cSMasahiro Yamada
926e079a08cSMasahiro YamadaTo set specific flags for a single file the following construction
927e079a08cSMasahiro Yamadais used:
928e079a08cSMasahiro Yamada
929e079a08cSMasahiro YamadaExample::
930e079a08cSMasahiro Yamada
931e079a08cSMasahiro Yamada  bpf-helper-userccflags += -I user/include
932e079a08cSMasahiro Yamada
933e079a08cSMasahiro YamadaIt is also possible to specify additional options to the linker.
934e079a08cSMasahiro Yamada
935e079a08cSMasahiro YamadaExample::
936e079a08cSMasahiro Yamada
937e079a08cSMasahiro Yamada  # net/bpfilter/Makefile
938e079a08cSMasahiro Yamada  bpfilter_umh-userldflags += -static
939e079a08cSMasahiro Yamada
940e079a08cSMasahiro YamadaWhen linking bpfilter_umh, it will be passed the extra option -static.
941e079a08cSMasahiro Yamada
942f67695c9SElliot BermanFrom command line, :ref:`USERCFLAGS and USERLDFLAGS <userkbuildflags>` will also be used.
943f67695c9SElliot Berman
9441a4c1c9dSJani NikulaWhen userspace programs are actually built
9451a4c1c9dSJani Nikula------------------------------------------
946e079a08cSMasahiro Yamada
947faabed29SMasahiro YamadaKbuild builds userspace programs only when told to do so.
948faabed29SMasahiro YamadaThere are two ways to do this.
949faabed29SMasahiro Yamada
950faabed29SMasahiro Yamada(1) Add it as the prerequisite of another file
951faabed29SMasahiro Yamada
952faabed29SMasahiro Yamada    Example::
953faabed29SMasahiro Yamada
954faabed29SMasahiro Yamada      #net/bpfilter/Makefile
955faabed29SMasahiro Yamada      userprogs := bpfilter_umh
956faabed29SMasahiro Yamada      $(obj)/bpfilter_umh_blob.o: $(obj)/bpfilter_umh
957faabed29SMasahiro Yamada
958faabed29SMasahiro Yamada    $(obj)/bpfilter_umh is built before $(obj)/bpfilter_umh_blob.o
959faabed29SMasahiro Yamada
960faabed29SMasahiro Yamada(2) Use always-y
961faabed29SMasahiro Yamada
962faabed29SMasahiro Yamada    Example::
963faabed29SMasahiro Yamada
964faabed29SMasahiro Yamada      userprogs := binderfs_example
965faabed29SMasahiro Yamada      always-y := $(userprogs)
966faabed29SMasahiro Yamada
9679f1fe2bbSJani Nikula    Kbuild provides the following shorthand for this::
968faabed29SMasahiro Yamada
969faabed29SMasahiro Yamada      userprogs-always-y := binderfs_example
970faabed29SMasahiro Yamada
971faabed29SMasahiro Yamada    This will tell Kbuild to build binderfs_example when it visits this
972faabed29SMasahiro Yamada    Makefile.
973e079a08cSMasahiro Yamada
9741a4c1c9dSJani NikulaKbuild clean infrastructure
9751a4c1c9dSJani Nikula===========================
976cd238effSMauro Carvalho Chehab
977*2f0e2a39SJani Nikula``make clean`` deletes most generated files in the obj tree where the kernel
978cd238effSMauro Carvalho Chehabis compiled. This includes generated files such as host programs.
9795f2fb52fSMasahiro YamadaKbuild knows targets listed in $(hostprogs), $(always-y), $(always-m),
9805f2fb52fSMasahiro Yamada$(always-), $(extra-y), $(extra-) and $(targets). They are all deleted
981*2f0e2a39SJani Nikuladuring ``make clean``. Files matching the patterns ``*.[oas]``, ``*.ko``, plus
9825f2fb52fSMasahiro Yamadasome additional files generated by kbuild are deleted all over the kernel
983*2f0e2a39SJani Nikulasource tree when ``make clean`` is executed.
984cd238effSMauro Carvalho Chehab
9851634f2bfSMasahiro YamadaAdditional files or directories can be specified in kbuild makefiles by use of
9861634f2bfSMasahiro Yamada$(clean-files).
987cd238effSMauro Carvalho Chehab
988cd238effSMauro Carvalho ChehabExample::
989cd238effSMauro Carvalho Chehab
990cd238effSMauro Carvalho Chehab  #lib/Makefile
991cd238effSMauro Carvalho Chehab  clean-files := crc32table.h
992cd238effSMauro Carvalho Chehab
993*2f0e2a39SJani NikulaWhen executing ``make clean``, the file ``crc32table.h`` will be deleted.
994cd238effSMauro Carvalho ChehabKbuild will assume files to be in the same relative directory as the
995e3c9405eSMiguel OjedaMakefile.
996cd238effSMauro Carvalho Chehab
9971634f2bfSMasahiro YamadaTo exclude certain files or directories from make clean, use the
9981634f2bfSMasahiro Yamada$(no-clean-files) variable.
999cd238effSMauro Carvalho Chehab
1000*2f0e2a39SJani NikulaUsually kbuild descends down in subdirectories due to ``obj-* := dir/``,
1001cd238effSMauro Carvalho Chehabbut in the architecture makefiles where the kbuild infrastructure
1002cd238effSMauro Carvalho Chehabis not sufficient this sometimes needs to be explicit.
1003cd238effSMauro Carvalho Chehab
1004cd238effSMauro Carvalho ChehabExample::
1005cd238effSMauro Carvalho Chehab
1006cd238effSMauro Carvalho Chehab  #arch/x86/boot/Makefile
1007eabc8bcbSMasahiro Yamada  subdir- := compressed
1008cd238effSMauro Carvalho Chehab
1009cd238effSMauro Carvalho ChehabThe above assignment instructs kbuild to descend down in the
1010*2f0e2a39SJani Nikuladirectory compressed/ when ``make clean`` is executed.
1011cd238effSMauro Carvalho Chehab
1012*2f0e2a39SJani NikulaNote 1: arch/$(SRCARCH)/Makefile cannot use ``subdir-``, because that file is
10138212f898SMasahiro Yamadaincluded in the top level makefile. Instead, arch/$(SRCARCH)/Kbuild can use
1014*2f0e2a39SJani Nikula``subdir-``.
1015cd238effSMauro Carvalho Chehab
1016cd238effSMauro Carvalho ChehabNote 2: All directories listed in core-y, libs-y, drivers-y and net-y will
1017*2f0e2a39SJani Nikulabe visited during ``make clean``.
1018cd238effSMauro Carvalho Chehab
10191a4c1c9dSJani NikulaArchitecture Makefiles
10201a4c1c9dSJani Nikula======================
1021cd238effSMauro Carvalho Chehab
1022cd238effSMauro Carvalho ChehabThe top level Makefile sets up the environment and does the preparation,
1023cd238effSMauro Carvalho Chehabbefore starting to descend down in the individual directories.
10249f1fe2bbSJani Nikula
1025cd238effSMauro Carvalho ChehabThe top level makefile contains the generic part, whereas
10268c4d9b14SMasahiro Yamadaarch/$(SRCARCH)/Makefile contains what is required to set up kbuild
1027cd238effSMauro Carvalho Chehabfor said architecture.
10289f1fe2bbSJani Nikula
10298c4d9b14SMasahiro YamadaTo do so, arch/$(SRCARCH)/Makefile sets up a number of variables and defines
1030cd238effSMauro Carvalho Chehaba few targets.
1031cd238effSMauro Carvalho Chehab
1032cd238effSMauro Carvalho ChehabWhen kbuild executes, the following steps are followed (roughly):
1033cd238effSMauro Carvalho Chehab
1034cd238effSMauro Carvalho Chehab1) Configuration of the kernel => produce .config
10359f1fe2bbSJani Nikula
1036cd238effSMauro Carvalho Chehab2) Store kernel version in include/linux/version.h
10379f1fe2bbSJani Nikula
1038cd238effSMauro Carvalho Chehab3) Updating all other prerequisites to the target prepare:
10399f1fe2bbSJani Nikula
10408c4d9b14SMasahiro Yamada   - Additional prerequisites are specified in arch/$(SRCARCH)/Makefile
10419f1fe2bbSJani Nikula
1042cd238effSMauro Carvalho Chehab4) Recursively descend down in all directories listed in
1043cd238effSMauro Carvalho Chehab   init-* core* drivers-* net-* libs-* and build all targets.
10449f1fe2bbSJani Nikula
10458c4d9b14SMasahiro Yamada   - The values of the above variables are expanded in arch/$(SRCARCH)/Makefile.
10469f1fe2bbSJani Nikula
1047cd238effSMauro Carvalho Chehab5) All object files are then linked and the resulting file vmlinux is
1048cd238effSMauro Carvalho Chehab   located at the root of the obj tree.
1049ce697cceSMasahiro Yamada   The very first objects linked are listed in scripts/head-object-list.txt.
10509f1fe2bbSJani Nikula
1051cd238effSMauro Carvalho Chehab6) Finally, the architecture-specific part does any required post processing
1052cd238effSMauro Carvalho Chehab   and builds the final bootimage.
10539f1fe2bbSJani Nikula
1054cd238effSMauro Carvalho Chehab   - This includes building boot records
1055cd238effSMauro Carvalho Chehab   - Preparing initrd images and the like
1056cd238effSMauro Carvalho Chehab
10571a4c1c9dSJani NikulaSet variables to tweak the build to the architecture
10581a4c1c9dSJani Nikula----------------------------------------------------
1059cd238effSMauro Carvalho Chehab
1060c0901577SMasahiro YamadaKBUILD_LDFLAGS
1061cd238effSMauro Carvalho Chehab  Generic $(LD) options
1062cd238effSMauro Carvalho Chehab
1063cd238effSMauro Carvalho Chehab  Flags used for all invocations of the linker.
1064cd238effSMauro Carvalho Chehab  Often specifying the emulation is sufficient.
1065cd238effSMauro Carvalho Chehab
1066cd238effSMauro Carvalho Chehab  Example::
1067cd238effSMauro Carvalho Chehab
1068cd238effSMauro Carvalho Chehab    #arch/s390/Makefile
1069c0901577SMasahiro Yamada    KBUILD_LDFLAGS         := -m elf_s390
1070cd238effSMauro Carvalho Chehab
1071cd238effSMauro Carvalho Chehab  Note: ldflags-y can be used to further customise
10721a4c1c9dSJani Nikula  the flags used. See `Non-builtin vmlinux targets - extra-y`_.
1073cd238effSMauro Carvalho Chehab
1074cd238effSMauro Carvalho ChehabLDFLAGS_vmlinux
1075cd238effSMauro Carvalho Chehab  Options for $(LD) when linking vmlinux
1076cd238effSMauro Carvalho Chehab
1077cd238effSMauro Carvalho Chehab  LDFLAGS_vmlinux is used to specify additional flags to pass to
1078cd238effSMauro Carvalho Chehab  the linker when linking the final vmlinux image.
10799f1fe2bbSJani Nikula
1080cd238effSMauro Carvalho Chehab  LDFLAGS_vmlinux uses the LDFLAGS_$@ support.
1081cd238effSMauro Carvalho Chehab
1082cd238effSMauro Carvalho Chehab  Example::
1083cd238effSMauro Carvalho Chehab
1084cd238effSMauro Carvalho Chehab    #arch/x86/Makefile
1085cd238effSMauro Carvalho Chehab    LDFLAGS_vmlinux := -e stext
1086cd238effSMauro Carvalho Chehab
1087cd238effSMauro Carvalho ChehabOBJCOPYFLAGS
1088cd238effSMauro Carvalho Chehab  objcopy flags
1089cd238effSMauro Carvalho Chehab
1090cd238effSMauro Carvalho Chehab  When $(call if_changed,objcopy) is used to translate a .o file,
1091cd238effSMauro Carvalho Chehab  the flags specified in OBJCOPYFLAGS will be used.
10929f1fe2bbSJani Nikula
1093cd238effSMauro Carvalho Chehab  $(call if_changed,objcopy) is often used to generate raw binaries on
1094cd238effSMauro Carvalho Chehab  vmlinux.
1095cd238effSMauro Carvalho Chehab
1096cd238effSMauro Carvalho Chehab  Example::
1097cd238effSMauro Carvalho Chehab
1098cd238effSMauro Carvalho Chehab    #arch/s390/Makefile
1099cd238effSMauro Carvalho Chehab    OBJCOPYFLAGS := -O binary
1100cd238effSMauro Carvalho Chehab
1101cd238effSMauro Carvalho Chehab    #arch/s390/boot/Makefile
1102cd238effSMauro Carvalho Chehab    $(obj)/image: vmlinux FORCE
1103cd238effSMauro Carvalho Chehab            $(call if_changed,objcopy)
1104cd238effSMauro Carvalho Chehab
1105cd238effSMauro Carvalho Chehab  In this example, the binary $(obj)/image is a binary version of
1106cd238effSMauro Carvalho Chehab  vmlinux. The usage of $(call if_changed,xxx) will be described later.
1107cd238effSMauro Carvalho Chehab
1108cd238effSMauro Carvalho ChehabKBUILD_AFLAGS
11095ef87263SMasahiro Yamada  Assembler flags
1110cd238effSMauro Carvalho Chehab
11119f1fe2bbSJani Nikula  Default value - see top level Makefile.
11129f1fe2bbSJani Nikula
1113cd238effSMauro Carvalho Chehab  Append or modify as required per architecture.
1114cd238effSMauro Carvalho Chehab
1115cd238effSMauro Carvalho Chehab  Example::
1116cd238effSMauro Carvalho Chehab
1117cd238effSMauro Carvalho Chehab    #arch/sparc64/Makefile
1118cd238effSMauro Carvalho Chehab    KBUILD_AFLAGS += -m64 -mcpu=ultrasparc
1119cd238effSMauro Carvalho Chehab
1120cd238effSMauro Carvalho ChehabKBUILD_CFLAGS
1121cd238effSMauro Carvalho Chehab  $(CC) compiler flags
1122cd238effSMauro Carvalho Chehab
11239f1fe2bbSJani Nikula  Default value - see top level Makefile.
11249f1fe2bbSJani Nikula
1125cd238effSMauro Carvalho Chehab  Append or modify as required per architecture.
1126cd238effSMauro Carvalho Chehab
1127cd238effSMauro Carvalho Chehab  Often, the KBUILD_CFLAGS variable depends on the configuration.
1128cd238effSMauro Carvalho Chehab
1129cd238effSMauro Carvalho Chehab  Example::
1130cd238effSMauro Carvalho Chehab
1131cd238effSMauro Carvalho Chehab    #arch/x86/boot/compressed/Makefile
1132cd238effSMauro Carvalho Chehab    cflags-$(CONFIG_X86_32) := -march=i386
1133cd238effSMauro Carvalho Chehab    cflags-$(CONFIG_X86_64) := -mcmodel=small
1134cd238effSMauro Carvalho Chehab    KBUILD_CFLAGS += $(cflags-y)
1135cd238effSMauro Carvalho Chehab
1136cd238effSMauro Carvalho Chehab  Many arch Makefiles dynamically run the target C compiler to
1137cd238effSMauro Carvalho Chehab  probe supported options::
1138cd238effSMauro Carvalho Chehab
1139cd238effSMauro Carvalho Chehab    #arch/x86/Makefile
1140cd238effSMauro Carvalho Chehab
1141cd238effSMauro Carvalho Chehab    ...
1142cd238effSMauro Carvalho Chehab    cflags-$(CONFIG_MPENTIUMII)     += $(call cc-option,\
1143cd238effSMauro Carvalho Chehab						-march=pentium2,-march=i686)
1144cd238effSMauro Carvalho Chehab    ...
1145cd238effSMauro Carvalho Chehab    # Disable unit-at-a-time mode ...
1146cd238effSMauro Carvalho Chehab    KBUILD_CFLAGS += $(call cc-option,-fno-unit-at-a-time)
1147cd238effSMauro Carvalho Chehab    ...
1148cd238effSMauro Carvalho Chehab
1149cd238effSMauro Carvalho Chehab
1150cd238effSMauro Carvalho Chehab  The first example utilises the trick that a config option expands
1151*2f0e2a39SJani Nikula  to "y" when selected.
1152cd238effSMauro Carvalho Chehab
1153d07479b2SMiguel OjedaKBUILD_RUSTFLAGS
1154d07479b2SMiguel Ojeda  $(RUSTC) compiler flags
1155d07479b2SMiguel Ojeda
11569f1fe2bbSJani Nikula  Default value - see top level Makefile.
11579f1fe2bbSJani Nikula
1158d07479b2SMiguel Ojeda  Append or modify as required per architecture.
1159d07479b2SMiguel Ojeda
1160d07479b2SMiguel Ojeda  Often, the KBUILD_RUSTFLAGS variable depends on the configuration.
1161d07479b2SMiguel Ojeda
1162d07479b2SMiguel Ojeda  Note that target specification file generation (for ``--target``)
1163d07479b2SMiguel Ojeda  is handled in ``scripts/generate_rust_target.rs``.
1164d07479b2SMiguel Ojeda
1165cd238effSMauro Carvalho ChehabKBUILD_AFLAGS_KERNEL
11665ef87263SMasahiro Yamada  Assembler options specific for built-in
1167cd238effSMauro Carvalho Chehab
1168cd238effSMauro Carvalho Chehab  $(KBUILD_AFLAGS_KERNEL) contains extra C compiler flags used to compile
1169cd238effSMauro Carvalho Chehab  resident kernel code.
1170cd238effSMauro Carvalho Chehab
1171cd238effSMauro Carvalho ChehabKBUILD_AFLAGS_MODULE
11725ef87263SMasahiro Yamada  Assembler options specific for modules
1173cd238effSMauro Carvalho Chehab
1174cd238effSMauro Carvalho Chehab  $(KBUILD_AFLAGS_MODULE) is used to add arch-specific options that
11755ef87263SMasahiro Yamada  are used for assembler.
1176cd238effSMauro Carvalho Chehab
11772eebb7abSMasahiro Yamada  From commandline AFLAGS_MODULE shall be used (see kbuild.rst).
1178cd238effSMauro Carvalho Chehab
1179cd238effSMauro Carvalho ChehabKBUILD_CFLAGS_KERNEL
1180cd238effSMauro Carvalho Chehab  $(CC) options specific for built-in
1181cd238effSMauro Carvalho Chehab
1182cd238effSMauro Carvalho Chehab  $(KBUILD_CFLAGS_KERNEL) contains extra C compiler flags used to compile
1183cd238effSMauro Carvalho Chehab  resident kernel code.
1184cd238effSMauro Carvalho Chehab
1185cd238effSMauro Carvalho ChehabKBUILD_CFLAGS_MODULE
1186cd238effSMauro Carvalho Chehab  Options for $(CC) when building modules
1187cd238effSMauro Carvalho Chehab
1188cd238effSMauro Carvalho Chehab  $(KBUILD_CFLAGS_MODULE) is used to add arch-specific options that
1189cd238effSMauro Carvalho Chehab  are used for $(CC).
11909f1fe2bbSJani Nikula
11912eebb7abSMasahiro Yamada  From commandline CFLAGS_MODULE shall be used (see kbuild.rst).
1192cd238effSMauro Carvalho Chehab
1193d07479b2SMiguel OjedaKBUILD_RUSTFLAGS_KERNEL
1194d07479b2SMiguel Ojeda  $(RUSTC) options specific for built-in
1195d07479b2SMiguel Ojeda
1196d07479b2SMiguel Ojeda  $(KBUILD_RUSTFLAGS_KERNEL) contains extra Rust compiler flags used to
1197d07479b2SMiguel Ojeda  compile resident kernel code.
1198d07479b2SMiguel Ojeda
1199d07479b2SMiguel OjedaKBUILD_RUSTFLAGS_MODULE
1200d07479b2SMiguel Ojeda  Options for $(RUSTC) when building modules
1201d07479b2SMiguel Ojeda
1202d07479b2SMiguel Ojeda  $(KBUILD_RUSTFLAGS_MODULE) is used to add arch-specific options that
1203d07479b2SMiguel Ojeda  are used for $(RUSTC).
12049f1fe2bbSJani Nikula
1205d07479b2SMiguel Ojeda  From commandline RUSTFLAGS_MODULE shall be used (see kbuild.rst).
1206d07479b2SMiguel Ojeda
1207cd238effSMauro Carvalho ChehabKBUILD_LDFLAGS_MODULE
1208cd238effSMauro Carvalho Chehab  Options for $(LD) when linking modules
1209cd238effSMauro Carvalho Chehab
1210cd238effSMauro Carvalho Chehab  $(KBUILD_LDFLAGS_MODULE) is used to add arch-specific options
1211cd238effSMauro Carvalho Chehab  used when linking modules. This is often a linker script.
1212cd238effSMauro Carvalho Chehab
12132eebb7abSMasahiro Yamada  From commandline LDFLAGS_MODULE shall be used (see kbuild.rst).
1214cd238effSMauro Carvalho Chehab
1215888f0c34SMasahiro YamadaKBUILD_LDS
1216888f0c34SMasahiro Yamada  The linker script with full path. Assigned by the top-level Makefile.
1217888f0c34SMasahiro Yamada
1218888f0c34SMasahiro YamadaKBUILD_VMLINUX_OBJS
1219888f0c34SMasahiro Yamada  All object files for vmlinux. They are linked to vmlinux in the same
1220888f0c34SMasahiro Yamada  order as listed in KBUILD_VMLINUX_OBJS.
1221888f0c34SMasahiro Yamada
1222ce697cceSMasahiro Yamada  The objects listed in scripts/head-object-list.txt are exceptions;
1223ce697cceSMasahiro Yamada  they are placed before the other objects.
1224ce697cceSMasahiro Yamada
1225888f0c34SMasahiro YamadaKBUILD_VMLINUX_LIBS
1226*2f0e2a39SJani Nikula  All .a ``lib`` files for vmlinux. KBUILD_VMLINUX_OBJS and
1227888f0c34SMasahiro Yamada  KBUILD_VMLINUX_LIBS together specify all the object files used to
1228888f0c34SMasahiro Yamada  link vmlinux.
1229cd238effSMauro Carvalho Chehab
12301a4c1c9dSJani NikulaAdd prerequisites to archheaders
12311a4c1c9dSJani Nikula--------------------------------
1232cd238effSMauro Carvalho Chehab
1233cd238effSMauro Carvalho ChehabThe archheaders: rule is used to generate header files that
1234*2f0e2a39SJani Nikulamay be installed into user space by ``make header_install``.
1235cd238effSMauro Carvalho Chehab
1236*2f0e2a39SJani NikulaIt is run before ``make archprepare`` when run on the
1237cd238effSMauro Carvalho Chehabarchitecture itself.
1238cd238effSMauro Carvalho Chehab
12391a4c1c9dSJani NikulaAdd prerequisites to archprepare
12401a4c1c9dSJani Nikula--------------------------------
1241cd238effSMauro Carvalho Chehab
1242cd238effSMauro Carvalho ChehabThe archprepare: rule is used to list prerequisites that need to be
1243cd238effSMauro Carvalho Chehabbuilt before starting to descend down in the subdirectories.
12449f1fe2bbSJani Nikula
1245cd238effSMauro Carvalho ChehabThis is usually used for header files containing assembler constants.
1246cd238effSMauro Carvalho Chehab
1247cd238effSMauro Carvalho ChehabExample::
1248cd238effSMauro Carvalho Chehab
1249cd238effSMauro Carvalho Chehab  #arch/arm/Makefile
1250cd238effSMauro Carvalho Chehab  archprepare: maketools
1251cd238effSMauro Carvalho Chehab
1252cd238effSMauro Carvalho ChehabIn this example, the file target maketools will be processed
1253cd238effSMauro Carvalho Chehabbefore descending down in the subdirectories.
12549f1fe2bbSJani Nikula
1255b26ff488SRandy DunlapSee also chapter XXX-TODO that describes how kbuild supports
1256cd238effSMauro Carvalho Chehabgenerating offset header files.
1257cd238effSMauro Carvalho Chehab
12581a4c1c9dSJani NikulaList directories to visit when descending
12591a4c1c9dSJani Nikula-----------------------------------------
1260cd238effSMauro Carvalho Chehab
1261cd238effSMauro Carvalho ChehabAn arch Makefile cooperates with the top Makefile to define variables
1262cd238effSMauro Carvalho Chehabwhich specify how to build the vmlinux file.  Note that there is no
1263cd238effSMauro Carvalho Chehabcorresponding arch-specific section for modules; the module-building
1264cd238effSMauro Carvalho Chehabmachinery is all architecture-independent.
1265cd238effSMauro Carvalho Chehab
1266ce697cceSMasahiro Yamadacore-y, libs-y, drivers-y
1267cd238effSMauro Carvalho Chehab  $(libs-y) lists directories where a lib.a archive can be located.
1268cd238effSMauro Carvalho Chehab
1269cd238effSMauro Carvalho Chehab  The rest list directories where a built-in.a object file can be
1270cd238effSMauro Carvalho Chehab  located.
1271cd238effSMauro Carvalho Chehab
1272cd238effSMauro Carvalho Chehab  Then the rest follows in this order:
1273cd238effSMauro Carvalho Chehab
127423b53061SMasahiro Yamada    $(core-y), $(libs-y), $(drivers-y)
1275cd238effSMauro Carvalho Chehab
1276cd238effSMauro Carvalho Chehab  The top level Makefile defines values for all generic directories,
12778c4d9b14SMasahiro Yamada  and arch/$(SRCARCH)/Makefile only adds architecture-specific
1278cd238effSMauro Carvalho Chehab  directories.
1279cd238effSMauro Carvalho Chehab
1280cd238effSMauro Carvalho Chehab  Example::
1281cd238effSMauro Carvalho Chehab
128223b53061SMasahiro Yamada    # arch/sparc/Makefile
128323b53061SMasahiro Yamada    core-y                 += arch/sparc/
1284cd238effSMauro Carvalho Chehab
128523b53061SMasahiro Yamada    libs-y                 += arch/sparc/prom/
128623b53061SMasahiro Yamada    libs-y                 += arch/sparc/lib/
128723b53061SMasahiro Yamada
128823b53061SMasahiro Yamada    drivers-$(CONFIG_PM) += arch/sparc/power/
1289cd238effSMauro Carvalho Chehab
12901a4c1c9dSJani NikulaArchitecture-specific boot images
12911a4c1c9dSJani Nikula---------------------------------
1292cd238effSMauro Carvalho Chehab
1293cd238effSMauro Carvalho ChehabAn arch Makefile specifies goals that take the vmlinux file, compress
1294cd238effSMauro Carvalho Chehabit, wrap it in bootstrapping code, and copy the resulting files
1295cd238effSMauro Carvalho Chehabsomewhere. This includes various kinds of installation commands.
1296cd238effSMauro Carvalho ChehabThe actual goals are not standardized across architectures.
1297cd238effSMauro Carvalho Chehab
1298cd238effSMauro Carvalho ChehabIt is common to locate any additional processing in a boot/
12998c4d9b14SMasahiro Yamadadirectory below arch/$(SRCARCH)/.
1300cd238effSMauro Carvalho Chehab
1301cd238effSMauro Carvalho ChehabKbuild does not provide any smart way to support building a
13028c4d9b14SMasahiro Yamadatarget specified in boot/. Therefore arch/$(SRCARCH)/Makefile shall
1303cd238effSMauro Carvalho Chehabcall make manually to build a target in boot/.
1304cd238effSMauro Carvalho Chehab
1305cd238effSMauro Carvalho ChehabThe recommended approach is to include shortcuts in
13068c4d9b14SMasahiro Yamadaarch/$(SRCARCH)/Makefile, and use the full path when calling down
13078c4d9b14SMasahiro Yamadainto the arch/$(SRCARCH)/boot/Makefile.
1308cd238effSMauro Carvalho Chehab
1309cd238effSMauro Carvalho ChehabExample::
1310cd238effSMauro Carvalho Chehab
1311cd238effSMauro Carvalho Chehab  #arch/x86/Makefile
1312cd238effSMauro Carvalho Chehab  boot := arch/x86/boot
1313cd238effSMauro Carvalho Chehab  bzImage: vmlinux
1314cd238effSMauro Carvalho Chehab          $(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
1315cd238effSMauro Carvalho Chehab
1316*2f0e2a39SJani Nikula``$(Q)$(MAKE) $(build)=<dir>`` is the recommended way to invoke
1317cd238effSMauro Carvalho Chehabmake in a subdirectory.
1318cd238effSMauro Carvalho Chehab
1319cd238effSMauro Carvalho ChehabThere are no rules for naming architecture-specific targets,
1320*2f0e2a39SJani Nikulabut executing ``make help`` will list all relevant targets.
1321cd238effSMauro Carvalho ChehabTo support this, $(archhelp) must be defined.
1322cd238effSMauro Carvalho Chehab
1323cd238effSMauro Carvalho ChehabExample::
1324cd238effSMauro Carvalho Chehab
1325cd238effSMauro Carvalho Chehab  #arch/x86/Makefile
1326cd238effSMauro Carvalho Chehab  define archhelp
13278c4d9b14SMasahiro Yamada    echo  '* bzImage      - Compressed kernel image (arch/x86/boot/bzImage)'
1328cd238effSMauro Carvalho Chehab  endif
1329cd238effSMauro Carvalho Chehab
1330cd238effSMauro Carvalho ChehabWhen make is executed without arguments, the first goal encountered
1331cd238effSMauro Carvalho Chehabwill be built. In the top level Makefile the first goal present
1332cd238effSMauro Carvalho Chehabis all:.
13339f1fe2bbSJani Nikula
1334cd238effSMauro Carvalho ChehabAn architecture shall always, per default, build a bootable image.
1335*2f0e2a39SJani NikulaIn ``make help``, the default goal is highlighted with a ``*``.
13369f1fe2bbSJani Nikula
1337cd238effSMauro Carvalho ChehabAdd a new prerequisite to all: to select a default goal different
1338cd238effSMauro Carvalho Chehabfrom vmlinux.
1339cd238effSMauro Carvalho Chehab
1340cd238effSMauro Carvalho ChehabExample::
1341cd238effSMauro Carvalho Chehab
1342cd238effSMauro Carvalho Chehab  #arch/x86/Makefile
1343cd238effSMauro Carvalho Chehab  all: bzImage
1344cd238effSMauro Carvalho Chehab
1345*2f0e2a39SJani NikulaWhen ``make`` is executed without arguments, bzImage will be built.
1346cd238effSMauro Carvalho Chehab
13471a4c1c9dSJani NikulaCommands useful for building a boot image
13481a4c1c9dSJani Nikula-----------------------------------------
1349cd238effSMauro Carvalho Chehab
1350cd238effSMauro Carvalho ChehabKbuild provides a few macros that are useful when building a
1351cd238effSMauro Carvalho Chehabboot image.
1352cd238effSMauro Carvalho Chehab
1353cd238effSMauro Carvalho Chehabld
1354cd238effSMauro Carvalho Chehab  Link target. Often, LDFLAGS_$@ is used to set specific options to ld.
1355cd238effSMauro Carvalho Chehab
1356cd238effSMauro Carvalho Chehab  Example::
1357cd238effSMauro Carvalho Chehab
1358cd238effSMauro Carvalho Chehab    #arch/x86/boot/Makefile
1359cd238effSMauro Carvalho Chehab    LDFLAGS_bootsect := -Ttext 0x0 -s --oformat binary
1360cd238effSMauro Carvalho Chehab    LDFLAGS_setup    := -Ttext 0x0 -s --oformat binary -e begtext
1361cd238effSMauro Carvalho Chehab
1362cd238effSMauro Carvalho Chehab    targets += setup setup.o bootsect bootsect.o
1363cd238effSMauro Carvalho Chehab    $(obj)/setup $(obj)/bootsect: %: %.o FORCE
1364cd238effSMauro Carvalho Chehab            $(call if_changed,ld)
1365cd238effSMauro Carvalho Chehab
1366cd238effSMauro Carvalho Chehab  In this example, there are two possible targets, requiring different
1367cd238effSMauro Carvalho Chehab  options to the linker. The linker options are specified using the
1368cd238effSMauro Carvalho Chehab  LDFLAGS_$@ syntax - one for each potential target.
13699f1fe2bbSJani Nikula
1370cd238effSMauro Carvalho Chehab  $(targets) are assigned all potential targets, by which kbuild knows
1371cd238effSMauro Carvalho Chehab  the targets and will:
1372cd238effSMauro Carvalho Chehab
1373cd238effSMauro Carvalho Chehab  1) check for commandline changes
1374cd238effSMauro Carvalho Chehab  2) delete target during make clean
1375cd238effSMauro Carvalho Chehab
1376*2f0e2a39SJani Nikula  The ``: %: %.o`` part of the prerequisite is a shorthand that
1377cd238effSMauro Carvalho Chehab  frees us from listing the setup.o and bootsect.o files.
1378cd238effSMauro Carvalho Chehab
1379cd238effSMauro Carvalho Chehab  Note:
1380*2f0e2a39SJani Nikula  It is a common mistake to forget the ``targets :=`` assignment,
1381cd238effSMauro Carvalho Chehab  resulting in the target file being recompiled for no
1382cd238effSMauro Carvalho Chehab  obvious reason.
1383cd238effSMauro Carvalho Chehab
1384cd238effSMauro Carvalho Chehabobjcopy
1385cd238effSMauro Carvalho Chehab  Copy binary. Uses OBJCOPYFLAGS usually specified in
13868c4d9b14SMasahiro Yamada  arch/$(SRCARCH)/Makefile.
13879f1fe2bbSJani Nikula
1388cd238effSMauro Carvalho Chehab  OBJCOPYFLAGS_$@ may be used to set additional options.
1389cd238effSMauro Carvalho Chehab
1390cd238effSMauro Carvalho Chehabgzip
1391cd238effSMauro Carvalho Chehab  Compress target. Use maximum compression to compress target.
1392cd238effSMauro Carvalho Chehab
1393cd238effSMauro Carvalho Chehab  Example::
1394cd238effSMauro Carvalho Chehab
1395cd238effSMauro Carvalho Chehab    #arch/x86/boot/compressed/Makefile
1396cd238effSMauro Carvalho Chehab    $(obj)/vmlinux.bin.gz: $(vmlinux.bin.all-y) FORCE
1397cd238effSMauro Carvalho Chehab            $(call if_changed,gzip)
1398cd238effSMauro Carvalho Chehab
1399cd238effSMauro Carvalho Chehabdtc
1400cd238effSMauro Carvalho Chehab  Create flattened device tree blob object suitable for linking
1401cd238effSMauro Carvalho Chehab  into vmlinux. Device tree blobs linked into vmlinux are placed
1402cd238effSMauro Carvalho Chehab  in an init section in the image. Platform code *must* copy the
1403cd238effSMauro Carvalho Chehab  blob to non-init memory prior to calling unflatten_device_tree().
1404cd238effSMauro Carvalho Chehab
1405*2f0e2a39SJani Nikula  To use this command, simply add ``*.dtb`` into obj-y or targets, or make
1406*2f0e2a39SJani Nikula  some other target depend on ``%.dtb``
1407cd238effSMauro Carvalho Chehab
1408*2f0e2a39SJani Nikula  A central rule exists to create ``$(obj)/%.dtb`` from ``$(src)/%.dts``;
1409cd238effSMauro Carvalho Chehab  architecture Makefiles do no need to explicitly write out that rule.
1410cd238effSMauro Carvalho Chehab
1411cd238effSMauro Carvalho Chehab  Example::
1412cd238effSMauro Carvalho Chehab
1413cd238effSMauro Carvalho Chehab    targets += $(dtb-y)
1414cd238effSMauro Carvalho Chehab    DTC_FLAGS ?= -p 1024
1415cd238effSMauro Carvalho Chehab
14161a4c1c9dSJani NikulaPreprocessing linker scripts
14171a4c1c9dSJani Nikula----------------------------
1418cd238effSMauro Carvalho Chehab
1419cd238effSMauro Carvalho ChehabWhen the vmlinux image is built, the linker script
14208c4d9b14SMasahiro Yamadaarch/$(SRCARCH)/kernel/vmlinux.lds is used.
14219f1fe2bbSJani Nikula
1422cd238effSMauro Carvalho ChehabThe script is a preprocessed variant of the file vmlinux.lds.S
1423cd238effSMauro Carvalho Chehablocated in the same directory.
14249f1fe2bbSJani Nikula
1425*2f0e2a39SJani Nikulakbuild knows .lds files and includes a rule ``*lds.S`` -> ``*lds``.
1426cd238effSMauro Carvalho Chehab
1427cd238effSMauro Carvalho ChehabExample::
1428cd238effSMauro Carvalho Chehab
1429cd238effSMauro Carvalho Chehab  #arch/x86/kernel/Makefile
1430faa7bdd7SMasahiro Yamada  extra-y := vmlinux.lds
1431cd238effSMauro Carvalho Chehab
1432faa7bdd7SMasahiro YamadaThe assignment to extra-y is used to tell kbuild to build the
1433cd238effSMauro Carvalho Chehabtarget vmlinux.lds.
14349f1fe2bbSJani Nikula
1435cd238effSMauro Carvalho ChehabThe assignment to $(CPPFLAGS_vmlinux.lds) tells kbuild to use the
1436cd238effSMauro Carvalho Chehabspecified options when building the target vmlinux.lds.
1437cd238effSMauro Carvalho Chehab
1438*2f0e2a39SJani NikulaWhen building the ``*.lds`` target, kbuild uses the variables::
1439cd238effSMauro Carvalho Chehab
1440cd238effSMauro Carvalho Chehab  KBUILD_CPPFLAGS      : Set in top-level Makefile
1441cd238effSMauro Carvalho Chehab  cppflags-y           : May be set in the kbuild makefile
1442cd238effSMauro Carvalho Chehab  CPPFLAGS_$(@F)       : Target-specific flags.
1443cd238effSMauro Carvalho Chehab                         Note that the full filename is used in this
1444cd238effSMauro Carvalho Chehab                         assignment.
1445cd238effSMauro Carvalho Chehab
1446*2f0e2a39SJani NikulaThe kbuild infrastructure for ``*lds`` files is used in several
1447cd238effSMauro Carvalho Chehabarchitecture-specific files.
1448cd238effSMauro Carvalho Chehab
14491a4c1c9dSJani NikulaGeneric header files
14501a4c1c9dSJani Nikula--------------------
1451cd238effSMauro Carvalho Chehab
1452cd238effSMauro Carvalho ChehabThe directory include/asm-generic contains the header files
1453cd238effSMauro Carvalho Chehabthat may be shared between individual architectures.
14549f1fe2bbSJani Nikula
1455cd238effSMauro Carvalho ChehabThe recommended approach how to use a generic header file is
1456cd238effSMauro Carvalho Chehabto list the file in the Kbuild file.
14579f1fe2bbSJani Nikula
14581a4c1c9dSJani NikulaSee `generic-y`_ for further info on syntax etc.
1459cd238effSMauro Carvalho Chehab
14601a4c1c9dSJani NikulaPost-link pass
14611a4c1c9dSJani Nikula--------------
1462cd238effSMauro Carvalho Chehab
1463cd238effSMauro Carvalho ChehabIf the file arch/xxx/Makefile.postlink exists, this makefile
1464cd238effSMauro Carvalho Chehabwill be invoked for post-link objects (vmlinux and modules.ko)
1465cd238effSMauro Carvalho Chehabfor architectures to run post-link passes on. Must also handle
1466cd238effSMauro Carvalho Chehabthe clean target.
1467cd238effSMauro Carvalho Chehab
1468cd238effSMauro Carvalho ChehabThis pass runs after kallsyms generation. If the architecture
1469cd238effSMauro Carvalho Chehabneeds to modify symbol locations, rather than manipulate the
1470cd238effSMauro Carvalho Chehabkallsyms, it may be easier to add another postlink target for
1471cd238effSMauro Carvalho Chehab.tmp_vmlinux? targets to be called from link-vmlinux.sh.
1472cd238effSMauro Carvalho Chehab
1473cd238effSMauro Carvalho ChehabFor example, powerpc uses this to check relocation sanity of
1474cd238effSMauro Carvalho Chehabthe linked vmlinux file.
1475cd238effSMauro Carvalho Chehab
14761a4c1c9dSJani NikulaKbuild syntax for exported headers
14771a4c1c9dSJani Nikula==================================
1478cd238effSMauro Carvalho Chehab
1479cd238effSMauro Carvalho ChehabThe kernel includes a set of headers that is exported to userspace.
1480cd238effSMauro Carvalho ChehabMany headers can be exported as-is but other headers require a
1481cd238effSMauro Carvalho Chehabminimal pre-processing before they are ready for user-space.
14829f1fe2bbSJani Nikula
1483cd238effSMauro Carvalho ChehabThe pre-processing does:
1484cd238effSMauro Carvalho Chehab
1485cd238effSMauro Carvalho Chehab- drop kernel-specific annotations
1486cd238effSMauro Carvalho Chehab- drop include of compiler.h
1487*2f0e2a39SJani Nikula- drop all sections that are kernel internal (guarded by ``ifdef __KERNEL__``)
1488cd238effSMauro Carvalho Chehab
1489cd238effSMauro Carvalho ChehabAll headers under include/uapi/, include/generated/uapi/,
1490cd238effSMauro Carvalho Chehabarch/<arch>/include/uapi/ and arch/<arch>/include/generated/uapi/
1491cd238effSMauro Carvalho Chehabare exported.
1492cd238effSMauro Carvalho Chehab
1493cd238effSMauro Carvalho ChehabA Kbuild file may be defined under arch/<arch>/include/uapi/asm/ and
1494cd238effSMauro Carvalho Chehabarch/<arch>/include/asm/ to list asm files coming from asm-generic.
14959f1fe2bbSJani Nikula
1496cd238effSMauro Carvalho ChehabSee subsequent chapter for the syntax of the Kbuild file.
1497cd238effSMauro Carvalho Chehab
14981a4c1c9dSJani Nikulano-export-headers
14991a4c1c9dSJani Nikula-----------------
1500cd238effSMauro Carvalho Chehab
1501cd238effSMauro Carvalho Chehabno-export-headers is essentially used by include/uapi/linux/Kbuild to
1502cd238effSMauro Carvalho Chehabavoid exporting specific headers (e.g. kvm.h) on architectures that do
1503cd238effSMauro Carvalho Chehabnot support it. It should be avoided as much as possible.
1504cd238effSMauro Carvalho Chehab
15051a4c1c9dSJani Nikulageneric-y
15061a4c1c9dSJani Nikula---------
1507cd238effSMauro Carvalho Chehab
1508cd238effSMauro Carvalho ChehabIf an architecture uses a verbatim copy of a header from
1509cd238effSMauro Carvalho Chehabinclude/asm-generic then this is listed in the file
15108c4d9b14SMasahiro Yamadaarch/$(SRCARCH)/include/asm/Kbuild like this:
1511cd238effSMauro Carvalho Chehab
1512cd238effSMauro Carvalho ChehabExample::
1513cd238effSMauro Carvalho Chehab
1514cd238effSMauro Carvalho Chehab  #arch/x86/include/asm/Kbuild
1515cd238effSMauro Carvalho Chehab  generic-y += termios.h
1516cd238effSMauro Carvalho Chehab  generic-y += rtc.h
1517cd238effSMauro Carvalho Chehab
1518cd238effSMauro Carvalho ChehabDuring the prepare phase of the build a wrapper include
1519cd238effSMauro Carvalho Chehabfile is generated in the directory::
1520cd238effSMauro Carvalho Chehab
15218c4d9b14SMasahiro Yamada  arch/$(SRCARCH)/include/generated/asm
1522cd238effSMauro Carvalho Chehab
1523cd238effSMauro Carvalho ChehabWhen a header is exported where the architecture uses
1524cd238effSMauro Carvalho Chehabthe generic header a similar wrapper is generated as part
1525cd238effSMauro Carvalho Chehabof the set of exported headers in the directory::
1526cd238effSMauro Carvalho Chehab
1527cd238effSMauro Carvalho Chehab  usr/include/asm
1528cd238effSMauro Carvalho Chehab
1529cd238effSMauro Carvalho ChehabThe generated wrapper will in both cases look like the following:
1530cd238effSMauro Carvalho Chehab
1531cd238effSMauro Carvalho ChehabExample: termios.h::
1532cd238effSMauro Carvalho Chehab
1533cd238effSMauro Carvalho Chehab  #include <asm-generic/termios.h>
1534cd238effSMauro Carvalho Chehab
15351a4c1c9dSJani Nikulagenerated-y
15361a4c1c9dSJani Nikula-----------
1537cd238effSMauro Carvalho Chehab
1538cd238effSMauro Carvalho ChehabIf an architecture generates other header files alongside generic-y
1539cd238effSMauro Carvalho Chehabwrappers, generated-y specifies them.
1540cd238effSMauro Carvalho Chehab
1541cd238effSMauro Carvalho ChehabThis prevents them being treated as stale asm-generic wrappers and
1542cd238effSMauro Carvalho Chehabremoved.
1543cd238effSMauro Carvalho Chehab
1544cd238effSMauro Carvalho ChehabExample::
1545cd238effSMauro Carvalho Chehab
1546cd238effSMauro Carvalho Chehab  #arch/x86/include/asm/Kbuild
1547cd238effSMauro Carvalho Chehab  generated-y += syscalls_32.h
1548cd238effSMauro Carvalho Chehab
15491a4c1c9dSJani Nikulamandatory-y
15501a4c1c9dSJani Nikula-----------
1551cd238effSMauro Carvalho Chehab
1552cd238effSMauro Carvalho Chehabmandatory-y is essentially used by include/(uapi/)asm-generic/Kbuild
1553cd238effSMauro Carvalho Chehabto define the minimum set of ASM headers that all architectures must have.
1554cd238effSMauro Carvalho Chehab
1555cd238effSMauro Carvalho ChehabThis works like optional generic-y. If a mandatory header is missing
15568c4d9b14SMasahiro Yamadain arch/$(SRCARCH)/include/(uapi/)/asm, Kbuild will automatically
15578c4d9b14SMasahiro Yamadagenerate a wrapper of the asm-generic one.
1558cd238effSMauro Carvalho Chehab
15591a4c1c9dSJani NikulaKbuild Variables
15601a4c1c9dSJani Nikula================
1561cd238effSMauro Carvalho Chehab
1562cd238effSMauro Carvalho ChehabThe top Makefile exports the following variables:
1563cd238effSMauro Carvalho Chehab
1564cd238effSMauro Carvalho ChehabVERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION
1565cd238effSMauro Carvalho Chehab  These variables define the current kernel version.  A few arch
1566cd238effSMauro Carvalho Chehab  Makefiles actually use these values directly; they should use
1567cd238effSMauro Carvalho Chehab  $(KERNELRELEASE) instead.
1568cd238effSMauro Carvalho Chehab
1569cd238effSMauro Carvalho Chehab  $(VERSION), $(PATCHLEVEL), and $(SUBLEVEL) define the basic
1570cd238effSMauro Carvalho Chehab  three-part version number, such as "2", "4", and "0".  These three
1571cd238effSMauro Carvalho Chehab  values are always numeric.
1572cd238effSMauro Carvalho Chehab
1573cd238effSMauro Carvalho Chehab  $(EXTRAVERSION) defines an even tinier sublevel for pre-patches
1574cd238effSMauro Carvalho Chehab  or additional patches.	It is usually some non-numeric string
1575cd238effSMauro Carvalho Chehab  such as "-pre4", and is often blank.
1576cd238effSMauro Carvalho Chehab
1577cd238effSMauro Carvalho ChehabKERNELRELEASE
1578cd238effSMauro Carvalho Chehab  $(KERNELRELEASE) is a single string such as "2.4.0-pre4", suitable
1579cd238effSMauro Carvalho Chehab  for constructing installation directory names or showing in
1580cd238effSMauro Carvalho Chehab  version strings.  Some arch Makefiles use it for this purpose.
1581cd238effSMauro Carvalho Chehab
1582cd238effSMauro Carvalho ChehabARCH
1583cd238effSMauro Carvalho Chehab  This variable defines the target architecture, such as "i386",
1584cd238effSMauro Carvalho Chehab  "arm", or "sparc". Some kbuild Makefiles test $(ARCH) to
1585cd238effSMauro Carvalho Chehab  determine which files to compile.
1586cd238effSMauro Carvalho Chehab
1587cd238effSMauro Carvalho Chehab  By default, the top Makefile sets $(ARCH) to be the same as the
1588cd238effSMauro Carvalho Chehab  host system architecture.  For a cross build, a user may
1589cd238effSMauro Carvalho Chehab  override the value of $(ARCH) on the command line::
1590cd238effSMauro Carvalho Chehab
1591cd238effSMauro Carvalho Chehab    make ARCH=m68k ...
1592cd238effSMauro Carvalho Chehab
15938c4d9b14SMasahiro YamadaSRCARCH
15948c4d9b14SMasahiro Yamada  This variable specifies the directory in arch/ to build.
15958c4d9b14SMasahiro Yamada
15968c4d9b14SMasahiro Yamada  ARCH and SRCARCH may not necessarily match. A couple of arch
1597*2f0e2a39SJani Nikula  directories are biarch, that is, a single ``arch/*/`` directory supports
15988c4d9b14SMasahiro Yamada  both 32-bit and 64-bit.
15998c4d9b14SMasahiro Yamada
16008c4d9b14SMasahiro Yamada  For example, you can pass in ARCH=i386, ARCH=x86_64, or ARCH=x86.
16018c4d9b14SMasahiro Yamada  For all of them, SRCARCH=x86 because arch/x86/ supports both i386 and
16028c4d9b14SMasahiro Yamada  x86_64.
1603cd238effSMauro Carvalho Chehab
1604cd238effSMauro Carvalho ChehabINSTALL_PATH
1605cd238effSMauro Carvalho Chehab  This variable defines a place for the arch Makefiles to install
1606cd238effSMauro Carvalho Chehab  the resident kernel image and System.map file.
1607cd238effSMauro Carvalho Chehab  Use this for architecture-specific install targets.
1608cd238effSMauro Carvalho Chehab
1609cd238effSMauro Carvalho ChehabINSTALL_MOD_PATH, MODLIB
1610cd238effSMauro Carvalho Chehab  $(INSTALL_MOD_PATH) specifies a prefix to $(MODLIB) for module
1611cd238effSMauro Carvalho Chehab  installation.  This variable is not defined in the Makefile but
1612cd238effSMauro Carvalho Chehab  may be passed in by the user if desired.
1613cd238effSMauro Carvalho Chehab
1614cd238effSMauro Carvalho Chehab  $(MODLIB) specifies the directory for module installation.
1615cd238effSMauro Carvalho Chehab  The top Makefile defines $(MODLIB) to
1616cd238effSMauro Carvalho Chehab  $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE).  The user may
1617cd238effSMauro Carvalho Chehab  override this value on the command line if desired.
1618cd238effSMauro Carvalho Chehab
1619cd238effSMauro Carvalho ChehabINSTALL_MOD_STRIP
1620cd238effSMauro Carvalho Chehab  If this variable is specified, it will cause modules to be stripped
1621*2f0e2a39SJani Nikula  after they are installed.  If INSTALL_MOD_STRIP is "1", then the
1622cd238effSMauro Carvalho Chehab  default option --strip-debug will be used.  Otherwise, the
1623cd238effSMauro Carvalho Chehab  INSTALL_MOD_STRIP value will be used as the option(s) to the strip
1624cd238effSMauro Carvalho Chehab  command.
1625cd238effSMauro Carvalho Chehab
16261a4c1c9dSJani NikulaMakefile language
16271a4c1c9dSJani Nikula=================
1628cd238effSMauro Carvalho Chehab
1629cd238effSMauro Carvalho ChehabThe kernel Makefiles are designed to be run with GNU Make.  The Makefiles
1630cd238effSMauro Carvalho Chehabuse only the documented features of GNU Make, but they do use many
1631cd238effSMauro Carvalho ChehabGNU extensions.
1632cd238effSMauro Carvalho Chehab
1633cd238effSMauro Carvalho ChehabGNU Make supports elementary list-processing functions.  The kernel
1634cd238effSMauro Carvalho ChehabMakefiles use a novel style of list building and manipulation with few
1635*2f0e2a39SJani Nikula``if`` statements.
1636cd238effSMauro Carvalho Chehab
1637*2f0e2a39SJani NikulaGNU Make has two assignment operators, ``:=`` and ``=``.  ``:=`` performs
1638cd238effSMauro Carvalho Chehabimmediate evaluation of the right-hand side and stores an actual string
1639*2f0e2a39SJani Nikulainto the left-hand side.  ``=`` is like a formula definition; it stores the
1640cd238effSMauro Carvalho Chehabright-hand side in an unevaluated form and then evaluates this form each
1641cd238effSMauro Carvalho Chehabtime the left-hand side is used.
1642cd238effSMauro Carvalho Chehab
1643*2f0e2a39SJani NikulaThere are some cases where ``=`` is appropriate.  Usually, though, ``:=``
1644cd238effSMauro Carvalho Chehabis the right choice.
1645cd238effSMauro Carvalho Chehab
16461a4c1c9dSJani NikulaCredits
16471a4c1c9dSJani Nikula=======
1648cd238effSMauro Carvalho Chehab
1649cd238effSMauro Carvalho Chehab- Original version made by Michael Elizabeth Chastain, <mailto:mec@shout.net>
1650cd238effSMauro Carvalho Chehab- Updates by Kai Germaschewski <kai@tp1.ruhr-uni-bochum.de>
1651cd238effSMauro Carvalho Chehab- Updates by Sam Ravnborg <sam@ravnborg.org>
1652cd238effSMauro Carvalho Chehab- Language QA by Jan Engelhardt <jengelh@gmx.de>
1653cd238effSMauro Carvalho Chehab
16541a4c1c9dSJani NikulaTODO
16551a4c1c9dSJani Nikula====
1656cd238effSMauro Carvalho Chehab
1657cd238effSMauro Carvalho Chehab- Describe how kbuild supports shipped files with _shipped.
1658cd238effSMauro Carvalho Chehab- Generating offset header files.
1659b26ff488SRandy Dunlap- Add more variables to chapters 7 or 9?
1660