makefiles.txt 48 KB

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