makefiles.rst 52 KB

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