makefiles.rst 52 KB

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