makefiles.rst 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698
  1. ======================
  2. Linux Kernel Makefiles
  3. ======================
  4. This document describes the Linux kernel Makefiles.
  5. Overview
  6. ========
  7. The Makefiles have five parts::
  8. Makefile the top Makefile.
  9. .config the kernel configuration file.
  10. arch/$(SRCARCH)/Makefile the arch Makefile.
  11. scripts/Makefile.* common rules etc. for all kbuild Makefiles.
  12. kbuild Makefiles exist in every subdirectory
  13. The top Makefile reads the .config file, which comes from the kernel
  14. configuration process.
  15. The top Makefile is responsible for building two major products: vmlinux
  16. (the resident kernel image) and modules (any module files).
  17. It builds these goals by recursively descending into the subdirectories of
  18. the kernel source tree.
  19. The list of subdirectories which are visited depends upon the kernel
  20. configuration. The top Makefile textually includes an arch Makefile
  21. with the name arch/$(SRCARCH)/Makefile. The arch Makefile supplies
  22. architecture-specific information to the top Makefile.
  23. Each subdirectory has a kbuild Makefile which carries out the commands
  24. passed down from above. The kbuild Makefile uses information from the
  25. .config file to construct various file lists used by kbuild to build
  26. any built-in or modular targets.
  27. scripts/Makefile.* contains all the definitions/rules etc. that
  28. are used to build the kernel based on the kbuild makefiles.
  29. Who does what
  30. =============
  31. People have four different relationships with the kernel Makefiles.
  32. *Users* are people who build kernels. These people type commands such as
  33. ``make menuconfig`` or ``make``. They usually do not read or edit
  34. any kernel Makefiles (or any other source files).
  35. *Normal developers* are people who work on features such as device
  36. drivers, file systems, and network protocols. These people need to
  37. maintain the kbuild Makefiles for the subsystem they are
  38. working on. In order to do this effectively, they need some overall
  39. knowledge about the kernel Makefiles, plus detailed knowledge about the
  40. public interface for kbuild.
  41. *Arch developers* are people who work on an entire architecture, such
  42. as sparc or x86. Arch developers need to know about the arch Makefile
  43. as well as kbuild Makefiles.
  44. *Kbuild developers* are people who work on the kernel build system itself.
  45. These people need to know about all aspects of the kernel Makefiles.
  46. This document is aimed towards normal developers and arch developers.
  47. The kbuild files
  48. ================
  49. Most Makefiles within the kernel are kbuild Makefiles that use the
  50. kbuild infrastructure. This chapter introduces the syntax used in the
  51. kbuild makefiles.
  52. The preferred name for the kbuild files are ``Makefile`` but ``Kbuild`` can
  53. be used and if both a ``Makefile`` and a ``Kbuild`` file exists, then the ``Kbuild``
  54. file will be used.
  55. Section `Goal definitions`_ is a quick intro; further chapters provide
  56. more details, with real examples.
  57. Goal definitions
  58. ----------------
  59. Goal definitions are the main part (heart) of the kbuild Makefile.
  60. These lines define the files to be built, any special compilation
  61. options, and any subdirectories to be entered recursively.
  62. The most simple kbuild makefile contains one line:
  63. Example::
  64. obj-y += foo.o
  65. This tells kbuild that there is one object in that directory, named
  66. foo.o. foo.o will be built from foo.c or foo.S.
  67. If foo.o shall be built as a module, the variable obj-m is used.
  68. Therefore the following pattern is often used:
  69. Example::
  70. obj-$(CONFIG_FOO) += foo.o
  71. $(CONFIG_FOO) evaluates to either y (for built-in) or m (for module).
  72. If CONFIG_FOO is neither y nor m, then the file will not be compiled
  73. nor linked.
  74. Built-in object goals - obj-y
  75. -----------------------------
  76. The kbuild Makefile specifies object files for vmlinux
  77. in the $(obj-y) lists. These lists depend on the kernel
  78. configuration.
  79. Kbuild compiles all the $(obj-y) files. It then calls
  80. ``$(AR) rcSTP`` to merge these files into one built-in.a file.
  81. This is a thin archive without a symbol table. It will be later
  82. linked into vmlinux by scripts/link-vmlinux.sh
  83. The order of files in $(obj-y) is significant. Duplicates in
  84. the lists are allowed: the first instance will be linked into
  85. built-in.a and succeeding instances will be ignored.
  86. Link order is significant, because certain functions
  87. (module_init() / __initcall) will be called during boot in the
  88. order they appear. So keep in mind that changing the link
  89. order may e.g. change the order in which your SCSI
  90. controllers are detected, and thus your disks are renumbered.
  91. Example::
  92. #drivers/isdn/i4l/Makefile
  93. # Makefile for the kernel ISDN subsystem and device drivers.
  94. # Each configuration option enables a list of files.
  95. obj-$(CONFIG_ISDN_I4L) += isdn.o
  96. obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o
  97. Loadable module goals - obj-m
  98. -----------------------------
  99. $(obj-m) specifies object files which are built as loadable
  100. kernel modules.
  101. A module may be built from one source file or several source
  102. files. In the case of one source file, the kbuild makefile
  103. simply adds the file to $(obj-m).
  104. Example::
  105. #drivers/isdn/i4l/Makefile
  106. obj-$(CONFIG_ISDN_PPP_BSDCOMP) += isdn_bsdcomp.o
  107. Note: In this example $(CONFIG_ISDN_PPP_BSDCOMP) evaluates to "m"
  108. If a kernel module is built from several source files, you specify
  109. that you want to build a module in the same way as above; however,
  110. kbuild needs to know which object files you want to build your
  111. module from, so you have to tell it by setting a $(<module_name>-y)
  112. variable.
  113. Example::
  114. #drivers/isdn/i4l/Makefile
  115. obj-$(CONFIG_ISDN_I4L) += isdn.o
  116. isdn-y := isdn_net_lib.o isdn_v110.o isdn_common.o
  117. In this example, the module name will be isdn.o. Kbuild will
  118. compile the objects listed in $(isdn-y) and then run
  119. ``$(LD) -r`` on the list of these files to generate isdn.o.
  120. Due to kbuild recognizing $(<module_name>-y) for composite objects,
  121. you can use the value of a ``CONFIG_`` symbol to optionally include an
  122. object file as part of a composite object.
  123. Example::
  124. #fs/ext2/Makefile
  125. obj-$(CONFIG_EXT2_FS) += ext2.o
  126. ext2-y := balloc.o dir.o file.o ialloc.o inode.o ioctl.o \
  127. namei.o super.o symlink.o
  128. ext2-$(CONFIG_EXT2_FS_XATTR) += xattr.o xattr_user.o \
  129. xattr_trusted.o
  130. In this example, xattr.o, xattr_user.o and xattr_trusted.o are only
  131. part of the composite object ext2.o if $(CONFIG_EXT2_FS_XATTR)
  132. evaluates to "y".
  133. Note: Of course, when you are building objects into the kernel,
  134. the syntax above will also work. So, if you have CONFIG_EXT2_FS=y,
  135. kbuild will build an ext2.o file for you out of the individual
  136. parts and then link this into built-in.a, as you would expect.
  137. Library file goals - lib-y
  138. --------------------------
  139. Objects listed with obj-* are used for modules, or
  140. combined in a built-in.a for that specific directory.
  141. There is also the possibility to list objects that will
  142. be included in a library, lib.a.
  143. All objects listed with lib-y are combined in a single
  144. library for that directory.
  145. Objects that are listed in obj-y and additionally listed in
  146. lib-y will not be included in the library, since they will
  147. be accessible anyway.
  148. For consistency, objects listed in lib-m will be included in lib.a.
  149. Note that the same kbuild makefile may list files to be built-in
  150. and to be part of a library. Therefore the same directory
  151. may contain both a built-in.a and a lib.a file.
  152. Example::
  153. #arch/x86/lib/Makefile
  154. lib-y := delay.o
  155. This will create a library lib.a based on delay.o. For kbuild to
  156. actually recognize that there is a lib.a being built, the directory
  157. shall be listed in libs-y.
  158. See also `List directories to visit when descending`_.
  159. Use of lib-y is normally restricted to ``lib/`` and ``arch/*/lib``.
  160. Descending down in directories
  161. ------------------------------
  162. A Makefile is only responsible for building objects in its own
  163. directory. Files in subdirectories should be taken care of by
  164. Makefiles in these subdirs. The build system will automatically
  165. invoke make recursively in subdirectories, provided you let it know of
  166. them.
  167. To do so, obj-y and obj-m are used.
  168. ext2 lives in a separate directory, and the Makefile present in fs/
  169. tells kbuild to descend down using the following assignment.
  170. Example::
  171. #fs/Makefile
  172. obj-$(CONFIG_EXT2_FS) += ext2/
  173. If CONFIG_EXT2_FS is set to either "y" (built-in) or "m" (modular)
  174. the corresponding obj- variable will be set, and kbuild will descend
  175. down in the ext2 directory.
  176. Kbuild uses this information not only to decide that it needs to visit
  177. the directory, but also to decide whether or not to link objects from
  178. the directory into vmlinux.
  179. When Kbuild descends into the directory with "y", all built-in objects
  180. from that directory are combined into the built-in.a, which will be
  181. eventually linked into vmlinux.
  182. When Kbuild descends into the directory with "m", in contrast, nothing
  183. from that directory will be linked into vmlinux. If the Makefile in
  184. that directory specifies obj-y, those objects will be left orphan.
  185. It is very likely a bug of the Makefile or of dependencies in Kconfig.
  186. Kbuild also supports dedicated syntax, subdir-y and subdir-m, for
  187. descending into subdirectories. It is a good fit when you know they
  188. do not contain kernel-space objects at all. A typical usage is to let
  189. Kbuild descend into subdirectories to build tools.
  190. Examples::
  191. # scripts/Makefile
  192. subdir-$(CONFIG_GCC_PLUGINS) += gcc-plugins
  193. subdir-$(CONFIG_MODVERSIONS) += genksyms
  194. subdir-$(CONFIG_SECURITY_SELINUX) += selinux
  195. Unlike obj-y/m, subdir-y/m does not need the trailing slash since this
  196. syntax is always used for directories.
  197. It is good practice to use a ``CONFIG_`` variable when assigning directory
  198. names. This allows kbuild to totally skip the directory if the
  199. corresponding ``CONFIG_`` option is neither "y" nor "m".
  200. Non-builtin vmlinux targets - extra-y
  201. -------------------------------------
  202. extra-y specifies targets which are needed for building vmlinux,
  203. but not combined into built-in.a.
  204. Examples are:
  205. 1) vmlinux linker script
  206. The linker script for vmlinux is located at
  207. arch/$(SRCARCH)/kernel/vmlinux.lds
  208. Example::
  209. # arch/x86/kernel/Makefile
  210. extra-y += vmlinux.lds
  211. extra-y is now deprecated because this is equivalent to:
  212. always-$(KBUILD_BUILTIN) += vmlinux.lds
  213. $(extra-y) should only contain targets needed for vmlinux.
  214. Kbuild skips extra-y when vmlinux is apparently not a final goal.
  215. (e.g. ``make modules``, or building external modules)
  216. If you intend to build targets unconditionally, always-y (explained
  217. in the next section) is the correct syntax to use.
  218. Always built goals - always-y
  219. -----------------------------
  220. always-y specifies targets which are literally always built when
  221. Kbuild visits the Makefile.
  222. Example::
  223. # ./Kbuild
  224. offsets-file := include/generated/asm-offsets.h
  225. always-y += $(offsets-file)
  226. Compilation flags
  227. -----------------
  228. ccflags-y, asflags-y and ldflags-y
  229. These three flags apply only to the kbuild makefile in which they
  230. are assigned. They are used for all the normal cc, as and ld
  231. invocations happening during a recursive build.
  232. ccflags-y specifies options for compiling with $(CC).
  233. Example::
  234. # drivers/acpi/acpica/Makefile
  235. ccflags-y := -Os -D_LINUX -DBUILDING_ACPICA
  236. ccflags-$(CONFIG_ACPI_DEBUG) += -DACPI_DEBUG_OUTPUT
  237. This variable is necessary because the top Makefile owns the
  238. variable $(KBUILD_CFLAGS) and uses it for compilation flags for the
  239. entire tree.
  240. asflags-y specifies assembler options.
  241. Example::
  242. #arch/sparc/kernel/Makefile
  243. asflags-y := -ansi
  244. ldflags-y specifies options for linking with $(LD).
  245. Example::
  246. #arch/cris/boot/compressed/Makefile
  247. ldflags-y += -T $(src)/decompress_$(arch-y).lds
  248. subdir-ccflags-y, subdir-asflags-y
  249. The two flags listed above are similar to ccflags-y and asflags-y.
  250. The difference is that the subdir- variants have effect for the kbuild
  251. file where they are present and all subdirectories.
  252. Options specified using subdir-* are added to the commandline before
  253. the options specified using the non-subdir variants.
  254. Example::
  255. subdir-ccflags-y := -Werror
  256. ccflags-remove-y, asflags-remove-y
  257. These flags are used to remove particular flags for the compiler,
  258. assembler invocations.
  259. Example::
  260. ccflags-remove-$(CONFIG_MCOUNT) += -pg
  261. CFLAGS_$@, AFLAGS_$@
  262. CFLAGS_$@ and AFLAGS_$@ only apply to commands in current
  263. kbuild makefile.
  264. $(CFLAGS_$@) specifies per-file options for $(CC). The $@
  265. part has a literal value which specifies the file that it is for.
  266. CFLAGS_$@ has the higher priority than ccflags-remove-y; CFLAGS_$@
  267. can re-add compiler flags that were removed by ccflags-remove-y.
  268. Example::
  269. # drivers/scsi/Makefile
  270. CFLAGS_aha152x.o = -DAHA152X_STAT -DAUTOCONF
  271. This line specify compilation flags for aha152x.o.
  272. $(AFLAGS_$@) is a similar feature for source files in assembly
  273. languages.
  274. AFLAGS_$@ has the higher priority than asflags-remove-y; AFLAGS_$@
  275. can re-add assembler flags that were removed by asflags-remove-y.
  276. Example::
  277. # arch/arm/kernel/Makefile
  278. AFLAGS_head.o := -DTEXT_OFFSET=$(TEXT_OFFSET)
  279. AFLAGS_crunch-bits.o := -Wa,-mcpu=ep9312
  280. AFLAGS_iwmmxt.o := -Wa,-mcpu=iwmmxt
  281. Dependency tracking
  282. -------------------
  283. Kbuild tracks dependencies on the following:
  284. 1) All prerequisite files (both ``*.c`` and ``*.h``)
  285. 2) ``CONFIG_`` options used in all prerequisite files
  286. 3) Command-line used to compile target
  287. Thus, if you change an option to $(CC) all affected files will
  288. be re-compiled.
  289. Custom Rules
  290. ------------
  291. Custom rules are used when the kbuild infrastructure does
  292. not provide the required support. A typical example is
  293. header files generated during the build process.
  294. Another example are the architecture-specific Makefiles which
  295. need custom rules to prepare boot images etc.
  296. Custom rules are written as normal Make rules.
  297. Kbuild is not executing in the directory where the Makefile is
  298. located, so all custom rules shall use a relative
  299. path to prerequisite files and target files.
  300. Two variables are used when defining custom rules:
  301. $(src)
  302. $(src) is the directory where the Makefile is located. Always use $(src) when
  303. referring to files located in the src tree.
  304. $(obj)
  305. $(obj) is the directory where the target is saved. Always use $(obj) when
  306. referring to generated files. Use $(obj) for pattern rules that need to work
  307. for both generated files and real sources (VPATH will help to find the
  308. prerequisites not only in the object tree but also in the source tree).
  309. Example::
  310. #drivers/scsi/Makefile
  311. $(obj)/53c8xx_d.h: $(src)/53c7,8xx.scr $(src)/script_asm.pl
  312. $(CPP) -DCHIP=810 - < $< | ... $(src)/script_asm.pl
  313. This is a custom rule, following the normal syntax
  314. required by make.
  315. The target file depends on two prerequisite files. References
  316. to the target file are prefixed with $(obj), references
  317. to prerequisites are referenced with $(src) (because they are not
  318. generated files).
  319. $(srcroot)
  320. $(srcroot) refers to the root of the source you are building, which can be
  321. either the kernel source or the external modules source, depending on whether
  322. KBUILD_EXTMOD is set. This can be either a relative or an absolute path, but
  323. if KBUILD_ABS_SRCTREE=1 is set, it is always an absolute path.
  324. $(srctree)
  325. $(srctree) refers to the root of the kernel source tree. When building the
  326. kernel, this is the same as $(srcroot).
  327. $(objtree)
  328. $(objtree) refers to the root of the kernel object tree. It is ``.`` when
  329. building the kernel, but it is different when building external modules.
  330. $(kecho)
  331. echoing information to user in a rule is often a good practice
  332. but when execution ``make -s`` one does not expect to see any output
  333. except for warnings/errors.
  334. To support this kbuild defines $(kecho) which will echo out the
  335. text following $(kecho) to stdout except if ``make -s`` is used.
  336. Example::
  337. # arch/arm/Makefile
  338. $(BOOT_TARGETS): vmlinux
  339. $(Q)$(MAKE) $(build)=$(boot) MACHINE=$(MACHINE) $(boot)/$@
  340. @$(kecho) ' Kernel: $(boot)/$@ is ready'
  341. When kbuild is executing with KBUILD_VERBOSE unset, then only a shorthand
  342. of a command is normally displayed.
  343. To enable this behaviour for custom commands kbuild requires
  344. two variables to be set::
  345. quiet_cmd_<command> - what shall be echoed
  346. cmd_<command> - the command to execute
  347. Example::
  348. # lib/Makefile
  349. quiet_cmd_crc32 = GEN $@
  350. cmd_crc32 = $< > $@
  351. $(obj)/crc32table.h: $(obj)/gen_crc32table
  352. $(call cmd,crc32)
  353. When updating the $(obj)/crc32table.h target, the line::
  354. GEN lib/crc32table.h
  355. will be displayed with ``make KBUILD_VERBOSE=``.
  356. Command change detection
  357. ------------------------
  358. When the rule is evaluated, timestamps are compared between the target
  359. and its prerequisite files. GNU Make updates the target when any of the
  360. prerequisites is newer than that.
  361. The target should be rebuilt also when the command line has changed
  362. since the last invocation. This is not supported by Make itself, so
  363. Kbuild achieves this by a kind of meta-programming.
  364. if_changed is the macro used for this purpose, in the following form::
  365. quiet_cmd_<command> = ...
  366. cmd_<command> = ...
  367. <target>: <source(s)> FORCE
  368. $(call if_changed,<command>)
  369. Any target that utilizes if_changed must be listed in $(targets),
  370. otherwise the command line check will fail, and the target will
  371. always be built.
  372. If the target is already listed in the recognized syntax such as
  373. obj-y/m, lib-y/m, extra-y/m, always-y/m, hostprogs, userprogs, Kbuild
  374. automatically adds it to $(targets). Otherwise, the target must be
  375. explicitly added to $(targets).
  376. Assignments to $(targets) are without $(obj)/ prefix. if_changed may be
  377. used in conjunction with custom rules as defined in `Custom Rules`_.
  378. Note: It is a typical mistake to forget the FORCE prerequisite.
  379. Another common pitfall is that whitespace is sometimes significant; for
  380. instance, the below will fail (note the extra space after the comma)::
  381. target: source(s) FORCE
  382. **WRONG!** $(call if_changed, objcopy)
  383. Note:
  384. if_changed should not be used more than once per target.
  385. It stores the executed command in a corresponding .cmd
  386. file and multiple calls would result in overwrites and
  387. unwanted results when the target is up to date and only the
  388. tests on changed commands trigger execution of commands.
  389. $(CC) support functions
  390. -----------------------
  391. The kernel may be built with several different versions of
  392. $(CC), each supporting a unique set of features and options.
  393. kbuild provides basic support to check for valid options for $(CC).
  394. $(CC) is usually the gcc compiler, but other alternatives are
  395. available.
  396. as-option
  397. as-option is used to check if $(CC) -- when used to compile
  398. assembler (``*.S``) files -- supports the given option. An optional
  399. second option may be specified if the first option is not supported.
  400. Example::
  401. #arch/sh/Makefile
  402. cflags-y += $(call as-option,-Wa$(comma)-isa=$(isa-y),)
  403. In the above example, cflags-y will be assigned the option
  404. -Wa$(comma)-isa=$(isa-y) if it is supported by $(CC).
  405. The second argument is optional, and if supplied will be used
  406. if first argument is not supported.
  407. as-instr
  408. as-instr checks if the assembler reports a specific instruction
  409. and then outputs either option1 or option2
  410. C escapes are supported in the test instruction
  411. Note: as-instr-option uses KBUILD_AFLAGS for assembler options
  412. cc-option
  413. cc-option is used to check if $(CC) supports a given option, and if
  414. not supported to use an optional second option.
  415. Example::
  416. #arch/x86/Makefile
  417. cflags-y += $(call cc-option,-march=pentium-mmx,-march=i586)
  418. In the above example, cflags-y will be assigned the option
  419. -march=pentium-mmx if supported by $(CC), otherwise -march=i586.
  420. The second argument to cc-option is optional, and if omitted,
  421. cflags-y will be assigned no value if first option is not supported.
  422. Note: cc-option uses KBUILD_CFLAGS for $(CC) options
  423. cc-option-yn
  424. cc-option-yn is used to check if $(CC) supports a given option
  425. and return "y" if supported, otherwise "n".
  426. Example::
  427. #arch/ppc/Makefile
  428. biarch := $(call cc-option-yn, -m32)
  429. aflags-$(biarch) += -a32
  430. cflags-$(biarch) += -m32
  431. In the above example, $(biarch) is set to y if $(CC) supports the -m32
  432. option. When $(biarch) equals "y", the expanded variables $(aflags-y)
  433. and $(cflags-y) will be assigned the values -a32 and -m32,
  434. respectively.
  435. Note: cc-option-yn uses KBUILD_CFLAGS for $(CC) options
  436. cc-disable-warning
  437. cc-disable-warning checks if $(CC) supports a given warning and returns
  438. the commandline switch to disable it. This special function is needed,
  439. because gcc 4.4 and later accept any unknown -Wno-* option and only
  440. warn about it if there is another warning in the source file.
  441. Example::
  442. KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
  443. In the above example, -Wno-unused-but-set-variable will be added to
  444. KBUILD_CFLAGS only if $(CC) really accepts it.
  445. gcc-min-version
  446. gcc-min-version tests if the value of $(CONFIG_GCC_VERSION) is greater than
  447. or equal to the provided value and evaluates to y if so.
  448. Example::
  449. cflags-$(call gcc-min-version, 110100) := -foo
  450. In this example, cflags-y will be assigned the value -foo if $(CC) is gcc and
  451. $(CONFIG_GCC_VERSION) is >= 11.1.
  452. clang-min-version
  453. clang-min-version tests if the value of $(CONFIG_CLANG_VERSION) is greater
  454. than or equal to the provided value and evaluates to y if so.
  455. Example::
  456. cflags-$(call clang-min-version, 110000) := -foo
  457. In this example, cflags-y will be assigned the value -foo if $(CC) is clang
  458. and $(CONFIG_CLANG_VERSION) is >= 11.0.0.
  459. cc-cross-prefix
  460. cc-cross-prefix is used to check if there exists a $(CC) in path with
  461. one of the listed prefixes. The first prefix where there exist a
  462. prefix$(CC) in the PATH is returned - and if no prefix$(CC) is found
  463. then nothing is returned.
  464. Additional prefixes are separated by a single space in the
  465. call of cc-cross-prefix.
  466. This functionality is useful for architecture Makefiles that try
  467. to set CROSS_COMPILE to well-known values but may have several
  468. values to select between.
  469. It is recommended only to try to set CROSS_COMPILE if it is a cross
  470. build (host arch is different from target arch). And if CROSS_COMPILE
  471. is already set then leave it with the old value.
  472. Example::
  473. #arch/m68k/Makefile
  474. ifneq ($(SUBARCH),$(ARCH))
  475. ifeq ($(CROSS_COMPILE),)
  476. CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu-)
  477. endif
  478. endif
  479. $(RUSTC) support functions
  480. --------------------------
  481. rustc-min-version
  482. rustc-min-version tests if the value of $(CONFIG_RUSTC_VERSION) is greater
  483. than or equal to the provided value and evaluates to y if so.
  484. Example::
  485. rustflags-$(call rustc-min-version, 108500) := -Cfoo
  486. In this example, rustflags-y will be assigned the value -Cfoo if
  487. $(CONFIG_RUSTC_VERSION) is >= 1.85.0.
  488. $(LD) support functions
  489. -----------------------
  490. ld-option
  491. ld-option is used to check if $(LD) supports the supplied option.
  492. ld-option takes two options as arguments.
  493. The second argument is an optional option that can be used if the
  494. first option is not supported by $(LD).
  495. Example::
  496. #Makefile
  497. LDFLAGS_vmlinux += $(call ld-option, -X)
  498. Script invocation
  499. -----------------
  500. Make rules may invoke scripts to build the kernel. The rules shall
  501. always provide the appropriate interpreter to execute the script. They
  502. shall not rely on the execute bits being set, and shall not invoke the
  503. script directly. For the convenience of manual script invocation, such
  504. as invoking ./scripts/checkpatch.pl, it is recommended to set execute
  505. bits on the scripts nonetheless.
  506. Kbuild provides variables $(CONFIG_SHELL), $(AWK), $(PERL),
  507. and $(PYTHON3) to refer to interpreters for the respective
  508. scripts.
  509. Example::
  510. #Makefile
  511. cmd_depmod = $(CONFIG_SHELL) $(srctree)/scripts/depmod.sh $(DEPMOD) \
  512. $(KERNELRELEASE)
  513. Host Program support
  514. ====================
  515. Kbuild supports building executables on the host for use during the
  516. compilation stage.
  517. Two steps are required in order to use a host executable.
  518. The first step is to tell kbuild that a host program exists. This is
  519. done utilising the variable ``hostprogs``.
  520. The second step is to add an explicit dependency to the executable.
  521. This can be done in two ways. Either add the dependency in a rule,
  522. or utilise the variable ``always-y``.
  523. Both possibilities are described in the following.
  524. Simple Host Program
  525. -------------------
  526. In some cases there is a need to compile and run a program on the
  527. computer where the build is running.
  528. The following line tells kbuild that the program bin2hex shall be
  529. built on the build host.
  530. Example::
  531. hostprogs := bin2hex
  532. Kbuild assumes in the above example that bin2hex is made from a single
  533. c-source file named bin2hex.c located in the same directory as
  534. the Makefile.
  535. Composite Host Programs
  536. -----------------------
  537. Host programs can be made up based on composite objects.
  538. The syntax used to define composite objects for host programs is
  539. similar to the syntax used for kernel objects.
  540. $(<executable>-objs) lists all objects used to link the final
  541. executable.
  542. Example::
  543. #scripts/lxdialog/Makefile
  544. hostprogs := lxdialog
  545. lxdialog-objs := checklist.o lxdialog.o
  546. Objects with extension .o are compiled from the corresponding .c
  547. files. In the above example, checklist.c is compiled to checklist.o
  548. and lxdialog.c is compiled to lxdialog.o.
  549. Finally, the two .o files are linked to the executable, lxdialog.
  550. Note: The syntax <executable>-y is not permitted for host-programs.
  551. Using C++ for host programs
  552. ---------------------------
  553. kbuild offers support for host programs written in C++. This was
  554. introduced solely to support kconfig, and is not recommended
  555. for general use.
  556. Example::
  557. #scripts/kconfig/Makefile
  558. hostprogs := qconf
  559. qconf-cxxobjs := qconf.o
  560. In the example above the executable is composed of the C++ file
  561. qconf.cc - identified by $(qconf-cxxobjs).
  562. If qconf is composed of a mixture of .c and .cc files, then an
  563. additional line can be used to identify this.
  564. Example::
  565. #scripts/kconfig/Makefile
  566. hostprogs := qconf
  567. qconf-cxxobjs := qconf.o
  568. qconf-objs := check.o
  569. Using Rust for host programs
  570. ----------------------------
  571. Kbuild offers support for host programs written in Rust. However,
  572. since a Rust toolchain is not mandatory for kernel compilation,
  573. it may only be used in scenarios where Rust is required to be
  574. available (e.g. when ``CONFIG_RUST`` is enabled).
  575. Example::
  576. hostprogs := target
  577. target-rust := y
  578. Kbuild will compile ``target`` using ``target.rs`` as the crate root,
  579. located in the same directory as the ``Makefile``. The crate may
  580. consist of several source files (see ``samples/rust/hostprogs``).
  581. Controlling compiler options for host programs
  582. ----------------------------------------------
  583. When compiling host programs, it is possible to set specific flags.
  584. The programs will always be compiled utilising $(HOSTCC) passed
  585. the options specified in $(KBUILD_HOSTCFLAGS).
  586. To set flags that will take effect for all host programs created
  587. in that Makefile, use the variable HOST_EXTRACFLAGS.
  588. Example::
  589. #scripts/lxdialog/Makefile
  590. HOST_EXTRACFLAGS += -I/usr/include/ncurses
  591. To set specific flags for a single file the following construction
  592. is used:
  593. Example::
  594. #arch/ppc64/boot/Makefile
  595. HOSTCFLAGS_piggyback.o := -DKERNELBASE=$(KERNELBASE)
  596. It is also possible to specify additional options to the linker.
  597. Example::
  598. #scripts/kconfig/Makefile
  599. HOSTLDLIBS_qconf := -L$(QTDIR)/lib
  600. When linking qconf, it will be passed the extra option
  601. ``-L$(QTDIR)/lib``.
  602. When host programs are actually built
  603. -------------------------------------
  604. Kbuild will only build host-programs when they are referenced
  605. as a prerequisite.
  606. This is possible in two ways:
  607. (1) List the prerequisite explicitly in a custom rule.
  608. Example::
  609. #drivers/pci/Makefile
  610. hostprogs := gen-devlist
  611. $(obj)/devlist.h: $(src)/pci.ids $(obj)/gen-devlist
  612. ( cd $(obj); ./gen-devlist ) < $<
  613. The target $(obj)/devlist.h will not be built before
  614. $(obj)/gen-devlist is updated. Note that references to
  615. the host programs in custom rules must be prefixed with $(obj).
  616. (2) Use always-y
  617. When there is no suitable custom rule, and the host program
  618. shall be built when a makefile is entered, the always-y
  619. variable shall be used.
  620. Example::
  621. #scripts/lxdialog/Makefile
  622. hostprogs := lxdialog
  623. always-y := $(hostprogs)
  624. Kbuild provides the following shorthand for this::
  625. hostprogs-always-y := lxdialog
  626. This will tell kbuild to build lxdialog even if not referenced in
  627. any rule.
  628. Userspace Program support
  629. =========================
  630. Just like host programs, Kbuild also supports building userspace executables
  631. for the target architecture (i.e. the same architecture as you are building
  632. the kernel for).
  633. The syntax is quite similar. The difference is to use ``userprogs`` instead of
  634. ``hostprogs``.
  635. Simple Userspace Program
  636. ------------------------
  637. The following line tells kbuild that the program bpf-direct shall be
  638. built for the target architecture.
  639. Example::
  640. userprogs := bpf-direct
  641. Kbuild assumes in the above example that bpf-direct is made from a
  642. single C source file named bpf-direct.c located in the same directory
  643. as the Makefile.
  644. Composite Userspace Programs
  645. ----------------------------
  646. Userspace programs can be made up based on composite objects.
  647. The syntax used to define composite objects for userspace programs is
  648. similar to the syntax used for kernel objects.
  649. $(<executable>-objs) lists all objects used to link the final
  650. executable.
  651. Example::
  652. #samples/seccomp/Makefile
  653. userprogs := bpf-fancy
  654. bpf-fancy-objs := bpf-fancy.o bpf-helper.o
  655. Objects with extension .o are compiled from the corresponding .c
  656. files. In the above example, bpf-fancy.c is compiled to bpf-fancy.o
  657. and bpf-helper.c is compiled to bpf-helper.o.
  658. Finally, the two .o files are linked to the executable, bpf-fancy.
  659. Note: The syntax <executable>-y is not permitted for userspace programs.
  660. Controlling compiler options for userspace programs
  661. ---------------------------------------------------
  662. When compiling userspace programs, it is possible to set specific flags.
  663. The programs will always be compiled utilising $(CC) passed
  664. the options specified in $(KBUILD_USERCFLAGS).
  665. To set flags that will take effect for all userspace programs created
  666. in that Makefile, use the variable userccflags.
  667. Example::
  668. # samples/seccomp/Makefile
  669. userccflags += -I usr/include
  670. To set specific flags for a single file the following construction
  671. is used:
  672. Example::
  673. bpf-helper-userccflags += -I user/include
  674. It is also possible to specify additional options to the linker.
  675. Example::
  676. # net/bpfilter/Makefile
  677. bpfilter_umh-userldflags += -static
  678. To specify libraries linked to a userspace program, you can use
  679. ``<executable>-userldlibs``. The ``userldlibs`` syntax specifies libraries
  680. linked to all userspace programs created in the current Makefile.
  681. When linking bpfilter_umh, it will be passed the extra option -static.
  682. From command line, :ref:`USERCFLAGS and USERLDFLAGS <userkbuildflags>` will also be used.
  683. When userspace programs are actually built
  684. ------------------------------------------
  685. Kbuild builds userspace programs only when told to do so.
  686. There are two ways to do this.
  687. (1) Add it as the prerequisite of another file
  688. Example::
  689. #net/bpfilter/Makefile
  690. userprogs := bpfilter_umh
  691. $(obj)/bpfilter_umh_blob.o: $(obj)/bpfilter_umh
  692. $(obj)/bpfilter_umh is built before $(obj)/bpfilter_umh_blob.o
  693. (2) Use always-y
  694. Example::
  695. userprogs := binderfs_example
  696. always-y := $(userprogs)
  697. Kbuild provides the following shorthand for this::
  698. userprogs-always-y := binderfs_example
  699. This will tell Kbuild to build binderfs_example when it visits this
  700. Makefile.
  701. Kbuild clean infrastructure
  702. ===========================
  703. ``make clean`` deletes most generated files in the obj tree where the kernel
  704. is compiled. This includes generated files such as host programs.
  705. Kbuild knows targets listed in $(hostprogs), $(always-y), $(always-m),
  706. $(always-), $(extra-y), $(extra-) and $(targets). They are all deleted
  707. during ``make clean``. Files matching the patterns ``*.[oas]``, ``*.ko``, plus
  708. some additional files generated by kbuild are deleted all over the kernel
  709. source tree when ``make clean`` is executed.
  710. Additional files or directories can be specified in kbuild makefiles by use of
  711. $(clean-files).
  712. Example::
  713. #lib/Makefile
  714. clean-files := crc32table.h
  715. When executing ``make clean``, the file ``crc32table.h`` will be deleted.
  716. Kbuild will assume files to be in the same relative directory as the
  717. Makefile.
  718. To exclude certain files or directories from make clean, use the
  719. $(no-clean-files) variable.
  720. Usually kbuild descends down in subdirectories due to ``obj-* := dir/``,
  721. but in the architecture makefiles where the kbuild infrastructure
  722. is not sufficient this sometimes needs to be explicit.
  723. Example::
  724. #arch/x86/boot/Makefile
  725. subdir- := compressed
  726. The above assignment instructs kbuild to descend down in the
  727. directory compressed/ when ``make clean`` is executed.
  728. Note 1: arch/$(SRCARCH)/Makefile cannot use ``subdir-``, because that file is
  729. included in the top level makefile. Instead, arch/$(SRCARCH)/Kbuild can use
  730. ``subdir-``.
  731. Note 2: All directories listed in core-y, libs-y, drivers-y and net-y will
  732. be visited during ``make clean``.
  733. Architecture Makefiles
  734. ======================
  735. The top level Makefile sets up the environment and does the preparation,
  736. before starting to descend down in the individual directories.
  737. The top level makefile contains the generic part, whereas
  738. arch/$(SRCARCH)/Makefile contains what is required to set up kbuild
  739. for said architecture.
  740. To do so, arch/$(SRCARCH)/Makefile sets up a number of variables and defines
  741. a few targets.
  742. When kbuild executes, the following steps are followed (roughly):
  743. 1) Configuration of the kernel => produce .config
  744. 2) Store kernel version in include/linux/version.h
  745. 3) Updating all other prerequisites to the target prepare:
  746. - Additional prerequisites are specified in arch/$(SRCARCH)/Makefile
  747. 4) Recursively descend down in all directories listed in
  748. init-* core* drivers-* net-* libs-* and build all targets.
  749. - The values of the above variables are expanded in arch/$(SRCARCH)/Makefile.
  750. 5) All object files are then linked and the resulting file vmlinux is
  751. located at the root of the obj tree.
  752. The very first objects linked are listed in scripts/head-object-list.txt.
  753. 6) Finally, the architecture-specific part does any required post processing
  754. and builds the final bootimage.
  755. - This includes building boot records
  756. - Preparing initrd images and the like
  757. Set variables to tweak the build to the architecture
  758. ----------------------------------------------------
  759. KBUILD_LDFLAGS
  760. Generic $(LD) options
  761. Flags used for all invocations of the linker.
  762. Often specifying the emulation is sufficient.
  763. Example::
  764. #arch/s390/Makefile
  765. KBUILD_LDFLAGS := -m elf_s390
  766. Note: ldflags-y can be used to further customise
  767. the flags used. See `Non-builtin vmlinux targets - extra-y`_.
  768. LDFLAGS_vmlinux
  769. Options for $(LD) when linking vmlinux
  770. LDFLAGS_vmlinux is used to specify additional flags to pass to
  771. the linker when linking the final vmlinux image.
  772. LDFLAGS_vmlinux uses the LDFLAGS_$@ support.
  773. Example::
  774. #arch/x86/Makefile
  775. LDFLAGS_vmlinux := -e stext
  776. OBJCOPYFLAGS
  777. objcopy flags
  778. When $(call if_changed,objcopy) is used to translate a .o file,
  779. the flags specified in OBJCOPYFLAGS will be used.
  780. $(call if_changed,objcopy) is often used to generate raw binaries on
  781. vmlinux.
  782. Example::
  783. #arch/s390/Makefile
  784. OBJCOPYFLAGS := -O binary
  785. #arch/s390/boot/Makefile
  786. $(obj)/image: vmlinux FORCE
  787. $(call if_changed,objcopy)
  788. In this example, the binary $(obj)/image is a binary version of
  789. vmlinux. The usage of $(call if_changed,xxx) will be described later.
  790. KBUILD_AFLAGS
  791. Assembler flags
  792. Default value - see top level Makefile.
  793. Append or modify as required per architecture.
  794. Example::
  795. #arch/sparc64/Makefile
  796. KBUILD_AFLAGS += -m64 -mcpu=ultrasparc
  797. KBUILD_CFLAGS
  798. $(CC) compiler flags
  799. Default value - see top level Makefile.
  800. Append or modify as required per architecture.
  801. Often, the KBUILD_CFLAGS variable depends on the configuration.
  802. Example::
  803. #arch/x86/boot/compressed/Makefile
  804. cflags-$(CONFIG_X86_32) := -march=i386
  805. cflags-$(CONFIG_X86_64) := -mcmodel=small
  806. KBUILD_CFLAGS += $(cflags-y)
  807. Many arch Makefiles dynamically run the target C compiler to
  808. probe supported options::
  809. #arch/x86/Makefile
  810. ...
  811. cflags-$(CONFIG_MPENTIUMII) += $(call cc-option,\
  812. -march=pentium2,-march=i686)
  813. ...
  814. # Disable unit-at-a-time mode ...
  815. KBUILD_CFLAGS += $(call cc-option,-fno-unit-at-a-time)
  816. ...
  817. The first example utilises the trick that a config option expands
  818. to "y" when selected.
  819. KBUILD_RUSTFLAGS
  820. $(RUSTC) compiler flags
  821. Default value - see top level Makefile.
  822. Append or modify as required per architecture.
  823. Often, the KBUILD_RUSTFLAGS variable depends on the configuration.
  824. Note that target specification file generation (for ``--target``)
  825. is handled in ``scripts/generate_rust_target.rs``.
  826. KBUILD_AFLAGS_KERNEL
  827. Assembler options specific for built-in
  828. $(KBUILD_AFLAGS_KERNEL) contains extra C compiler flags used to compile
  829. resident kernel code.
  830. KBUILD_AFLAGS_MODULE
  831. Assembler options specific for modules
  832. $(KBUILD_AFLAGS_MODULE) is used to add arch-specific options that
  833. are used for assembler.
  834. From commandline AFLAGS_MODULE shall be used (see kbuild.rst).
  835. KBUILD_CFLAGS_KERNEL
  836. $(CC) options specific for built-in
  837. $(KBUILD_CFLAGS_KERNEL) contains extra C compiler flags used to compile
  838. resident kernel code.
  839. KBUILD_CFLAGS_MODULE
  840. Options for $(CC) when building modules
  841. $(KBUILD_CFLAGS_MODULE) is used to add arch-specific options that
  842. are used for $(CC).
  843. From commandline CFLAGS_MODULE shall be used (see kbuild.rst).
  844. KBUILD_RUSTFLAGS_KERNEL
  845. $(RUSTC) options specific for built-in
  846. $(KBUILD_RUSTFLAGS_KERNEL) contains extra Rust compiler flags used to
  847. compile resident kernel code.
  848. KBUILD_RUSTFLAGS_MODULE
  849. Options for $(RUSTC) when building modules
  850. $(KBUILD_RUSTFLAGS_MODULE) is used to add arch-specific options that
  851. are used for $(RUSTC).
  852. From commandline RUSTFLAGS_MODULE shall be used (see kbuild.rst).
  853. KBUILD_LDFLAGS_MODULE
  854. Options for $(LD) when linking modules
  855. $(KBUILD_LDFLAGS_MODULE) is used to add arch-specific options
  856. used when linking modules. This is often a linker script.
  857. From commandline LDFLAGS_MODULE shall be used (see kbuild.rst).
  858. KBUILD_LDS
  859. The linker script with full path. Assigned by the top-level Makefile.
  860. KBUILD_VMLINUX_OBJS
  861. All object files for vmlinux. They are linked to vmlinux in the same
  862. order as listed in KBUILD_VMLINUX_OBJS.
  863. The objects listed in scripts/head-object-list.txt are exceptions;
  864. they are placed before the other objects.
  865. KBUILD_VMLINUX_LIBS
  866. All .a ``lib`` files for vmlinux. KBUILD_VMLINUX_OBJS and
  867. KBUILD_VMLINUX_LIBS together specify all the object files used to
  868. link vmlinux.
  869. Add prerequisites to archheaders
  870. --------------------------------
  871. The archheaders: rule is used to generate header files that
  872. may be installed into user space by ``make headers_install``.
  873. It is run before ``make archprepare`` when run on the
  874. architecture itself.
  875. Add prerequisites to archprepare
  876. --------------------------------
  877. The archprepare: rule is used to list prerequisites that need to be
  878. built before starting to descend down in the subdirectories.
  879. This is usually used for header files containing assembler constants.
  880. Example::
  881. #arch/arm/Makefile
  882. archprepare: maketools
  883. In this example, the file target maketools will be processed
  884. before descending down in the subdirectories.
  885. See also chapter XXX-TODO that describes how kbuild supports
  886. generating offset header files.
  887. List directories to visit when descending
  888. -----------------------------------------
  889. An arch Makefile cooperates with the top Makefile to define variables
  890. which specify how to build the vmlinux file. Note that there is no
  891. corresponding arch-specific section for modules; the module-building
  892. machinery is all architecture-independent.
  893. core-y, libs-y, drivers-y
  894. $(libs-y) lists directories where a lib.a archive can be located.
  895. The rest list directories where a built-in.a object file can be
  896. located.
  897. Then the rest follows in this order:
  898. $(core-y), $(libs-y), $(drivers-y)
  899. The top level Makefile defines values for all generic directories,
  900. and arch/$(SRCARCH)/Makefile only adds architecture-specific
  901. directories.
  902. Example::
  903. # arch/sparc/Makefile
  904. core-y += arch/sparc/
  905. libs-y += arch/sparc/prom/
  906. libs-y += arch/sparc/lib/
  907. drivers-$(CONFIG_PM) += arch/sparc/power/
  908. Architecture-specific boot images
  909. ---------------------------------
  910. An arch Makefile specifies goals that take the vmlinux file, compress
  911. it, wrap it in bootstrapping code, and copy the resulting files
  912. somewhere. This includes various kinds of installation commands.
  913. The actual goals are not standardized across architectures.
  914. It is common to locate any additional processing in a boot/
  915. directory below arch/$(SRCARCH)/.
  916. Kbuild does not provide any smart way to support building a
  917. target specified in boot/. Therefore arch/$(SRCARCH)/Makefile shall
  918. call make manually to build a target in boot/.
  919. The recommended approach is to include shortcuts in
  920. arch/$(SRCARCH)/Makefile, and use the full path when calling down
  921. into the arch/$(SRCARCH)/boot/Makefile.
  922. Example::
  923. #arch/x86/Makefile
  924. boot := arch/x86/boot
  925. bzImage: vmlinux
  926. $(Q)$(MAKE) $(build)=$(boot) $(boot)/$@
  927. ``$(Q)$(MAKE) $(build)=<dir>`` is the recommended way to invoke
  928. make in a subdirectory.
  929. There are no rules for naming architecture-specific targets,
  930. but executing ``make help`` will list all relevant targets.
  931. To support this, $(archhelp) must be defined.
  932. Example::
  933. #arch/x86/Makefile
  934. define archhelp
  935. echo '* bzImage - Compressed kernel image (arch/x86/boot/bzImage)'
  936. endif
  937. When make is executed without arguments, the first goal encountered
  938. will be built. In the top level Makefile the first goal present
  939. is all:.
  940. An architecture shall always, per default, build a bootable image.
  941. In ``make help``, the default goal is highlighted with a ``*``.
  942. Add a new prerequisite to all: to select a default goal different
  943. from vmlinux.
  944. Example::
  945. #arch/x86/Makefile
  946. all: bzImage
  947. When ``make`` is executed without arguments, bzImage will be built.
  948. Commands useful for building a boot image
  949. -----------------------------------------
  950. Kbuild provides a few macros that are useful when building a
  951. boot image.
  952. ld
  953. Link target. Often, LDFLAGS_$@ is used to set specific options to ld.
  954. Example::
  955. #arch/x86/boot/Makefile
  956. LDFLAGS_bootsect := -Ttext 0x0 -s --oformat binary
  957. LDFLAGS_setup := -Ttext 0x0 -s --oformat binary -e begtext
  958. targets += setup setup.o bootsect bootsect.o
  959. $(obj)/setup $(obj)/bootsect: %: %.o FORCE
  960. $(call if_changed,ld)
  961. In this example, there are two possible targets, requiring different
  962. options to the linker. The linker options are specified using the
  963. LDFLAGS_$@ syntax - one for each potential target.
  964. $(targets) are assigned all potential targets, by which kbuild knows
  965. the targets and will:
  966. 1) check for commandline changes
  967. 2) delete target during make clean
  968. The ``: %: %.o`` part of the prerequisite is a shorthand that
  969. frees us from listing the setup.o and bootsect.o files.
  970. Note:
  971. It is a common mistake to forget the ``targets :=`` assignment,
  972. resulting in the target file being recompiled for no
  973. obvious reason.
  974. objcopy
  975. Copy binary. Uses OBJCOPYFLAGS usually specified in
  976. arch/$(SRCARCH)/Makefile.
  977. OBJCOPYFLAGS_$@ may be used to set additional options.
  978. gzip
  979. Compress target. Use maximum compression to compress target.
  980. Example::
  981. #arch/x86/boot/compressed/Makefile
  982. $(obj)/vmlinux.bin.gz: $(vmlinux.bin.all-y) FORCE
  983. $(call if_changed,gzip)
  984. dtc
  985. Create flattened device tree blob object suitable for linking
  986. into vmlinux. Device tree blobs linked into vmlinux are placed
  987. in an init section in the image. Platform code *must* copy the
  988. blob to non-init memory prior to calling unflatten_device_tree().
  989. To use this command, simply add ``*.dtb`` into obj-y or targets, or make
  990. some other target depend on ``%.dtb``
  991. A central rule exists to create ``$(obj)/%.dtb`` from ``$(src)/%.dts``;
  992. architecture Makefiles do no need to explicitly write out that rule.
  993. Example::
  994. targets += $(dtb-y)
  995. DTC_FLAGS ?= -p 1024
  996. Preprocessing linker scripts
  997. ----------------------------
  998. When the vmlinux image is built, the linker script
  999. arch/$(SRCARCH)/kernel/vmlinux.lds is used.
  1000. The script is a preprocessed variant of the file vmlinux.lds.S
  1001. located in the same directory.
  1002. kbuild knows .lds files and includes a rule ``*lds.S`` -> ``*lds``.
  1003. Example::
  1004. #arch/x86/kernel/Makefile
  1005. extra-y := vmlinux.lds
  1006. The assignment to extra-y is used to tell kbuild to build the
  1007. target vmlinux.lds.
  1008. The assignment to $(CPPFLAGS_vmlinux.lds) tells kbuild to use the
  1009. specified options when building the target vmlinux.lds.
  1010. When building the ``*.lds`` target, kbuild uses the variables::
  1011. KBUILD_CPPFLAGS : Set in top-level Makefile
  1012. cppflags-y : May be set in the kbuild makefile
  1013. CPPFLAGS_$(@F) : Target-specific flags.
  1014. Note that the full filename is used in this
  1015. assignment.
  1016. The kbuild infrastructure for ``*lds`` files is used in several
  1017. architecture-specific files.
  1018. Generic header files
  1019. --------------------
  1020. The directory include/asm-generic contains the header files
  1021. that may be shared between individual architectures.
  1022. The recommended approach how to use a generic header file is
  1023. to list the file in the Kbuild file.
  1024. See `generic-y`_ for further info on syntax etc.
  1025. Post-link pass
  1026. --------------
  1027. If the file arch/xxx/Makefile.postlink exists, this makefile
  1028. will be invoked for post-link objects (vmlinux and modules.ko)
  1029. for architectures to run post-link passes on. Must also handle
  1030. the clean target.
  1031. This pass runs after kallsyms generation. If the architecture
  1032. needs to modify symbol locations, rather than manipulate the
  1033. kallsyms, it may be easier to add another postlink target for
  1034. .tmp_vmlinux? targets to be called from link-vmlinux.sh.
  1035. For example, powerpc uses this to check relocation sanity of
  1036. the linked vmlinux file.
  1037. Kbuild syntax for exported headers
  1038. ==================================
  1039. The kernel includes a set of headers that is exported to userspace.
  1040. Many headers can be exported as-is but other headers require a
  1041. minimal pre-processing before they are ready for user-space.
  1042. The pre-processing does:
  1043. - drop kernel-specific annotations
  1044. - drop include of compiler.h
  1045. - drop all sections that are kernel internal (guarded by ``ifdef __KERNEL__``)
  1046. All headers under include/uapi/, include/generated/uapi/,
  1047. arch/<arch>/include/uapi/ and arch/<arch>/include/generated/uapi/
  1048. are exported.
  1049. A Kbuild file may be defined under arch/<arch>/include/uapi/asm/ and
  1050. arch/<arch>/include/asm/ to list asm files coming from asm-generic.
  1051. See subsequent chapter for the syntax of the Kbuild file.
  1052. no-export-headers
  1053. -----------------
  1054. no-export-headers is essentially used by include/uapi/linux/Kbuild to
  1055. avoid exporting specific headers (e.g. kvm.h) on architectures that do
  1056. not support it. It should be avoided as much as possible.
  1057. generic-y
  1058. ---------
  1059. If an architecture uses a verbatim copy of a header from
  1060. include/asm-generic then this is listed in the file
  1061. arch/$(SRCARCH)/include/asm/Kbuild like this:
  1062. Example::
  1063. #arch/x86/include/asm/Kbuild
  1064. generic-y += termios.h
  1065. generic-y += rtc.h
  1066. During the prepare phase of the build a wrapper include
  1067. file is generated in the directory::
  1068. arch/$(SRCARCH)/include/generated/asm
  1069. When a header is exported where the architecture uses
  1070. the generic header a similar wrapper is generated as part
  1071. of the set of exported headers in the directory::
  1072. usr/include/asm
  1073. The generated wrapper will in both cases look like the following:
  1074. Example: termios.h::
  1075. #include <asm-generic/termios.h>
  1076. generated-y
  1077. -----------
  1078. If an architecture generates other header files alongside generic-y
  1079. wrappers, generated-y specifies them.
  1080. This prevents them being treated as stale asm-generic wrappers and
  1081. removed.
  1082. Example::
  1083. #arch/x86/include/asm/Kbuild
  1084. generated-y += syscalls_32.h
  1085. mandatory-y
  1086. -----------
  1087. mandatory-y is essentially used by include/(uapi/)asm-generic/Kbuild
  1088. to define the minimum set of ASM headers that all architectures must have.
  1089. This works like optional generic-y. If a mandatory header is missing
  1090. in arch/$(SRCARCH)/include/(uapi/)/asm, Kbuild will automatically
  1091. generate a wrapper of the asm-generic one.
  1092. Kbuild Variables
  1093. ================
  1094. The top Makefile exports the following variables:
  1095. VERSION, PATCHLEVEL, SUBLEVEL, EXTRAVERSION
  1096. These variables define the current kernel version. A few arch
  1097. Makefiles actually use these values directly; they should use
  1098. $(KERNELRELEASE) instead.
  1099. $(VERSION), $(PATCHLEVEL), and $(SUBLEVEL) define the basic
  1100. three-part version number, such as "2", "4", and "0". These three
  1101. values are always numeric.
  1102. $(EXTRAVERSION) defines an even tinier sublevel for pre-patches
  1103. or additional patches. It is usually some non-numeric string
  1104. such as "-pre4", and is often blank.
  1105. KERNELRELEASE
  1106. $(KERNELRELEASE) is a single string such as "2.4.0-pre4", suitable
  1107. for constructing installation directory names or showing in
  1108. version strings. Some arch Makefiles use it for this purpose.
  1109. ARCH
  1110. This variable defines the target architecture, such as "i386",
  1111. "arm", or "sparc". Some kbuild Makefiles test $(ARCH) to
  1112. determine which files to compile.
  1113. By default, the top Makefile sets $(ARCH) to be the same as the
  1114. host system architecture. For a cross build, a user may
  1115. override the value of $(ARCH) on the command line::
  1116. make ARCH=m68k ...
  1117. SRCARCH
  1118. This variable specifies the directory in arch/ to build.
  1119. ARCH and SRCARCH may not necessarily match. A couple of arch
  1120. directories are biarch, that is, a single ``arch/*/`` directory supports
  1121. both 32-bit and 64-bit.
  1122. For example, you can pass in ARCH=i386, ARCH=x86_64, or ARCH=x86.
  1123. For all of them, SRCARCH=x86 because arch/x86/ supports both i386 and
  1124. x86_64.
  1125. INSTALL_PATH
  1126. This variable defines a place for the arch Makefiles to install
  1127. the resident kernel image and System.map file.
  1128. Use this for architecture-specific install targets.
  1129. INSTALL_MOD_PATH, MODLIB
  1130. $(INSTALL_MOD_PATH) specifies a prefix to $(MODLIB) for module
  1131. installation. This variable is not defined in the Makefile but
  1132. may be passed in by the user if desired.
  1133. $(MODLIB) specifies the directory for module installation.
  1134. The top Makefile defines $(MODLIB) to
  1135. $(INSTALL_MOD_PATH)/lib/modules/$(KERNELRELEASE). The user may
  1136. override this value on the command line if desired.
  1137. INSTALL_MOD_STRIP
  1138. If this variable is specified, it will cause modules to be stripped
  1139. after they are installed. If INSTALL_MOD_STRIP is "1", then the
  1140. default option --strip-debug will be used. Otherwise, the
  1141. INSTALL_MOD_STRIP value will be used as the option(s) to the strip
  1142. command.
  1143. INSTALL_DTBS_PATH
  1144. This variable specifies a prefix for relocations required by build
  1145. roots. It defines a place for installing the device tree blobs. Like
  1146. INSTALL_MOD_PATH, it isn't defined in the Makefile, but can be passed
  1147. by the user if desired. Otherwise it defaults to the kernel install
  1148. path.
  1149. Makefile language
  1150. =================
  1151. The kernel Makefiles are designed to be run with GNU Make. The Makefiles
  1152. use only the documented features of GNU Make, but they do use many
  1153. GNU extensions.
  1154. GNU Make supports elementary list-processing functions. The kernel
  1155. Makefiles use a novel style of list building and manipulation with few
  1156. ``if`` statements.
  1157. GNU Make has two assignment operators, ``:=`` and ``=``. ``:=`` performs
  1158. immediate evaluation of the right-hand side and stores an actual string
  1159. into the left-hand side. ``=`` is like a formula definition; it stores the
  1160. right-hand side in an unevaluated form and then evaluates this form each
  1161. time the left-hand side is used.
  1162. There are some cases where ``=`` is appropriate. Usually, though, ``:=``
  1163. is the right choice.
  1164. Credits
  1165. =======
  1166. - Original version made by Michael Elizabeth Chastain, <mailto:mec@shout.net>
  1167. - Updates by Kai Germaschewski <kai@tp1.ruhr-uni-bochum.de>
  1168. - Updates by Sam Ravnborg <sam@ravnborg.org>
  1169. - Language QA by Jan Engelhardt <jengelh@gmx.de>
  1170. TODO
  1171. ====
  1172. - Generating offset header files.
  1173. - Add more variables to chapters 7 or 9?