xref: /aosp_15_r20/external/ltp/doc/old/Build-System.asciidoc (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard WorkerShort introduction into LTP build system
2*49cdfc7eSAndroid Build Coastguard Worker========================================
3*49cdfc7eSAndroid Build Coastguard Worker
4*49cdfc7eSAndroid Build Coastguard Worker******************************************************************************
5*49cdfc7eSAndroid Build Coastguard WorkerThe following document briefly describes the steps and methodologies used for
6*49cdfc7eSAndroid Build Coastguard Workerthe new and improved Makefile system.
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard WorkerChangelog:
9*49cdfc7eSAndroid Build Coastguard Worker
10*49cdfc7eSAndroid Build Coastguard Worker * Initial version: Ngie Cooper <[email protected]>
11*49cdfc7eSAndroid Build Coastguard Worker * Reformated for asciidoc: Cyril Hrubis <[email protected]>
12*49cdfc7eSAndroid Build Coastguard Worker******************************************************************************
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard WorkerThe Problem
15*49cdfc7eSAndroid Build Coastguard Worker-----------
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard WorkerThe problem with the old Makefile system is that it was very difficult to
18*49cdfc7eSAndroid Build Coastguard Workermaintain and it lacked any sense of formal structure, thus developing for LTP
19*49cdfc7eSAndroid Build Coastguard Workerand including new targets was more difficult than it should have been
20*49cdfc7eSAndroid Build Coastguard Worker(maintenance). Furthermore, proper option-based cross-compilation was
21*49cdfc7eSAndroid Build Coastguard Workerimpossible due to the fact that the Makefiles didn't support a prefixing
22*49cdfc7eSAndroid Build Coastguard Workersystem, and the appropriate implicit / static rules hadn't been configured to
23*49cdfc7eSAndroid Build Coastguard Workercompile into multiple object directories for out-of-tree build support (ease of
24*49cdfc7eSAndroid Build Coastguard Workeruse / functionality). Finally, there wasn't a means to setup dependencies
25*49cdfc7eSAndroid Build Coastguard Workerbetween components, such that if a component required libltp.a in order to
26*49cdfc7eSAndroid Build Coastguard Workercompile, it would go off and compile libltp.a first (ease of use).
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard WorkerThese items needed to be fixed to reduce maintenance nightmares for the
29*49cdfc7eSAndroid Build Coastguard Workerdevelopment community contributing to LTP, and the project maintainers.
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard WorkerDesign
32*49cdfc7eSAndroid Build Coastguard Worker------
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard WorkerThe system was designed such that including a single GNU Makefile compatible
35*49cdfc7eSAndroid Build Coastguard Workerset in each new directory component is all that's essentially required to
36*49cdfc7eSAndroid Build Coastguard Workerbuild the system.
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard WorkerSay you had a directory like the following (with .c files in them which
39*49cdfc7eSAndroid Build Coastguard Workerdirectly tie into applications, e.g. baz.c -> baz):
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
42*49cdfc7eSAndroid Build Coastguard Worker.../foo/
43*49cdfc7eSAndroid Build Coastguard Worker     |--> Makefile
44*49cdfc7eSAndroid Build Coastguard Worker     |
45*49cdfc7eSAndroid Build Coastguard Worker      --> bar/
46*49cdfc7eSAndroid Build Coastguard Worker	   |
47*49cdfc7eSAndroid Build Coastguard Worker	    --> Makefile
48*49cdfc7eSAndroid Build Coastguard Worker           |
49*49cdfc7eSAndroid Build Coastguard Worker            --> baz.c
50*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard WorkerHere's an example of how one would accomplish that:
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
55*49cdfc7eSAndroid Build Coastguard Worker.../foo/Makefile:
56*49cdfc7eSAndroid Build Coastguard Worker#
57*49cdfc7eSAndroid Build Coastguard Worker# Copyright disclaimer goes here -- please use GPLv2.
58*49cdfc7eSAndroid Build Coastguard Worker#
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Workertop_srcdir		?= ..
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Workerinclude $(top_srcdir)/include/mk/env_pre.mk
63*49cdfc7eSAndroid Build Coastguard Workerinclude $(top_srcdir)/include/mk/generic_trunk_target.mk
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker.../foo/bar/Makefile:
66*49cdfc7eSAndroid Build Coastguard Worker#
67*49cdfc7eSAndroid Build Coastguard Worker# Copyright disclaimer goes here -- please use GPLv2.
68*49cdfc7eSAndroid Build Coastguard Worker#
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Workertop_srcdir		?= ..
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Workerinclude $(top_srcdir)/include/mk/env_pre.mk
73*49cdfc7eSAndroid Build Coastguard Workerinclude $(top_srcdir)/include/mk/generic_leaf_target.mk
74*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard WorkerKernel Modules
77*49cdfc7eSAndroid Build Coastguard Worker--------------
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard WorkerSome of the tests need to build kernel modules, happily LTP has
80*49cdfc7eSAndroid Build Coastguard Workerinfrastructure for this.
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
83*49cdfc7eSAndroid Build Coastguard Workerifneq ($(KERNELRELEASE),)
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Workerobj-m := module01.o
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Workerelse
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Workertop_srcdir	?= ../../../..
90*49cdfc7eSAndroid Build Coastguard Workerinclude $(top_srcdir)/include/mk/testcases.mk
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard WorkerREQ_VERSION_MAJOR	:= 2
93*49cdfc7eSAndroid Build Coastguard WorkerREQ_VERSION_PATCH	:= 6
94*49cdfc7eSAndroid Build Coastguard WorkerMAKE_TARGETS		:= test01 test02 module01.ko
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Workerinclude $(top_srcdir)/include/mk/module.mk
97*49cdfc7eSAndroid Build Coastguard Workerinclude $(top_srcdir)/include/mk/generic_leaf_target.mk
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Workerendif
100*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard WorkerThis is example Makefile that allows you build kernel modules inside of LTP.
103*49cdfc7eSAndroid Build Coastguard WorkerThe prerequisites for the build are detected by the 'configure' script.
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard WorkerThe 'REQ_VERSION_MAJOR' and 'REQ_VERSION_PATCH' describe minimal kernel
106*49cdfc7eSAndroid Build Coastguard Workerversion for which the build system tries to build the module.
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard WorkerThe buildsystem is also forward compatible with changes in Linux kernel
109*49cdfc7eSAndroid Build Coastguard Workerinternal API so that if module fails to build the failure is ignored both on
110*49cdfc7eSAndroid Build Coastguard Workerbuild and installation. If the userspace counterpart of the test fails to load
111*49cdfc7eSAndroid Build Coastguard Workerthe module because the file does not exists, the test is skipped.
112*49cdfc7eSAndroid Build Coastguard Worker
113*49cdfc7eSAndroid Build Coastguard WorkerNote the 'ifneq($(KERNELRELEASE),)', the reason it's there is that the
114*49cdfc7eSAndroid Build Coastguard WorkerMakefile is executed twice, once by LTP build system and once by kernel
115*49cdfc7eSAndroid Build Coastguard Workerkbuild, see 'Documentation/kbuild/modules.rst' in the Linux kernel tree for
116*49cdfc7eSAndroid Build Coastguard Workerdetails on external module build.
117*49cdfc7eSAndroid Build Coastguard Worker
118*49cdfc7eSAndroid Build Coastguard WorkerMake Rules and Make Variables
119*49cdfc7eSAndroid Build Coastguard Worker-----------------------------
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard WorkerWhen using make rules, avoid writing ad hoc rules like:
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
124*49cdfc7eSAndroid Build Coastguard Worker[prog]: [dependencies]
125*49cdfc7eSAndroid Build Coastguard Worker	cc -I../../include $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(LDLIBS) \
126*49cdfc7eSAndroid Build Coastguard Worker	    -o [prog] [dependencies]
127*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
128*49cdfc7eSAndroid Build Coastguard Worker
129*49cdfc7eSAndroid Build Coastguard Workeretc. This makes cross-compilation and determinism difficult, if not impossible.
130*49cdfc7eSAndroid Build Coastguard WorkerBesides, implicit rules are your friends and as long as you use `MAKEOPTS=;' in
131*49cdfc7eSAndroid Build Coastguard Workerthe top-level caller (or do $(subst r,$(MAKEOPTS)) to remove -r), the compile
132*49cdfc7eSAndroid Build Coastguard Workerwill complete successfully, assuming all other prerequisites have been
133*49cdfc7eSAndroid Build Coastguard Workerfulfilled (libraries, headers, etc).
134*49cdfc7eSAndroid Build Coastguard Worker
135*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
136*49cdfc7eSAndroid Build Coastguard Worker$(AR)			: The library archiver.
137*49cdfc7eSAndroid Build Coastguard Worker
138*49cdfc7eSAndroid Build Coastguard Worker$(CC)			: The system C compiler.
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker$(CPP)			: The system C preprocessor.
141*49cdfc7eSAndroid Build Coastguard Worker
142*49cdfc7eSAndroid Build Coastguard Worker$(CFLAGS)		: C compiler flags.
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker$(CPPFLAGS)		: Preprocessor flags, e.g. -I arguments.
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard Worker$(DEBUG_CFLAGS)		: Debug flags to pass to $(CC), -g, etc.
147*49cdfc7eSAndroid Build Coastguard Worker
148*49cdfc7eSAndroid Build Coastguard Worker$(KVM_LD)		: Special linker for wrapping KVM payload binaries
149*49cdfc7eSAndroid Build Coastguard Worker			  into linkable object files. Defaults to $(LD).
150*49cdfc7eSAndroid Build Coastguard Worker			  Change this variable if the KVM Makefile fails
151*49cdfc7eSAndroid Build Coastguard Worker			  to build files named *-payload.o.
152*49cdfc7eSAndroid Build Coastguard Worker
153*49cdfc7eSAndroid Build Coastguard Worker$(LD)			: The system linker (typically $(CC), but not
154*49cdfc7eSAndroid Build Coastguard Worker			  necessarily).
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker$(LDFLAGS)		: What to pass in to the linker, including -L arguments
157*49cdfc7eSAndroid Build Coastguard Worker			  and other ld arguments, apart from -l library
158*49cdfc7eSAndroid Build Coastguard Worker			  includes (see $(LDLIBS)).
159*49cdfc7eSAndroid Build Coastguard Worker
160*49cdfc7eSAndroid Build Coastguard Worker			  This should be done in the $(CC) args passing style
161*49cdfc7eSAndroid Build Coastguard Worker			  when LD := $(CC), e.g. `-Wl,-foo', as opposed to
162*49cdfc7eSAndroid Build Coastguard Worker			  `-foo'.
163*49cdfc7eSAndroid Build Coastguard Worker
164*49cdfc7eSAndroid Build Coastguard Worker$(LDLIBS)		: Libraries to pass to the linker (e.g. -lltp, etc).
165*49cdfc7eSAndroid Build Coastguard Worker
166*49cdfc7eSAndroid Build Coastguard Worker$(LTPLDLIBS)            : LTP internal libraries i.e. these in libs/ directory.
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard Worker$(OPT_CFLAGS)		: Optimization flags to pass into the C compiler, -O2,
169*49cdfc7eSAndroid Build Coastguard Worker			  etc. If you specify -O2 or higher, you should also
170*49cdfc7eSAndroid Build Coastguard Worker			  specify -fno-strict-aliasing, because of gcc
171*49cdfc7eSAndroid Build Coastguard Worker			  fstrict-aliasing optimization bugs in the tree
172*49cdfc7eSAndroid Build Coastguard Worker			  optimizer. Search for `fstrict-aliasing optimization
173*49cdfc7eSAndroid Build Coastguard Worker			  bug' with your favorite search engine.
174*49cdfc7eSAndroid Build Coastguard Worker
175*49cdfc7eSAndroid Build Coastguard Worker			  Examples of more recent bugs:
176*49cdfc7eSAndroid Build Coastguard Worker			  1. tree-optimization/17510
177*49cdfc7eSAndroid Build Coastguard Worker			  2. tree-optimization/39100
178*49cdfc7eSAndroid Build Coastguard Worker
179*49cdfc7eSAndroid Build Coastguard Worker			  Various bugs have occurred in the past due to buggy
180*49cdfc7eSAndroid Build Coastguard Worker			  logic in the tree-optimization portion of the gcc
181*49cdfc7eSAndroid Build Coastguard Worker			  compiler, from 3.3.x to 4.4.
182*49cdfc7eSAndroid Build Coastguard Worker
183*49cdfc7eSAndroid Build Coastguard Worker$(RANLIB)		: What to run after archiving a library.
184*49cdfc7eSAndroid Build Coastguard Worker
185*49cdfc7eSAndroid Build Coastguard Worker$(WCFLAGS)		: Warning flags to pass to $(CC), e.g. -Werror,
186*49cdfc7eSAndroid Build Coastguard Worker			  -Wall, etc.
187*49cdfc7eSAndroid Build Coastguard Worker-------------------------------------------------------------------------------
188*49cdfc7eSAndroid Build Coastguard Worker
189*49cdfc7eSAndroid Build Coastguard WorkerMake System Variables
190*49cdfc7eSAndroid Build Coastguard Worker---------------------
191*49cdfc7eSAndroid Build Coastguard Worker
192*49cdfc7eSAndroid Build Coastguard WorkerA series of variables are used within the make system that direct what actions
193*49cdfc7eSAndroid Build Coastguard Workerneed to be taken. Rather than me listing the variables here, please with their
194*49cdfc7eSAndroid Build Coastguard Workerintended uses, please refer to the comments contained in
195*49cdfc7eSAndroid Build Coastguard Worker+.../include/mk/env_pre.mk+.
196*49cdfc7eSAndroid Build Coastguard Worker
197*49cdfc7eSAndroid Build Coastguard WorkerGuidelines and Recommendations
198*49cdfc7eSAndroid Build Coastguard Worker------------------------------
199*49cdfc7eSAndroid Build Coastguard Worker
200*49cdfc7eSAndroid Build Coastguard WorkerOf course, the GNU Make manual is key to understanding the Make system, but
201*49cdfc7eSAndroid Build Coastguard Workerhere are the following sections and chapters I suggest reviewing:
202*49cdfc7eSAndroid Build Coastguard Worker
203*49cdfc7eSAndroid Build Coastguard Workerlink:http://www.gnu.org/software/make/manual/make.html#Implicit-Rules[Implicit Rules]
204*49cdfc7eSAndroid Build Coastguard Workerlink:http://www.gnu.org/software/make/manual/make.html#Using-Variables[Variables and Expansion]
205*49cdfc7eSAndroid Build Coastguard Workerlink:http://www.gnu.org/software/make/manual/make.html#Origin-Function[Origin Use]
206*49cdfc7eSAndroid Build Coastguard Workerlink:http://www.gnu.org/software/make/manual/make.html#Directory-Search[VPath Use]
207*49cdfc7eSAndroid Build Coastguard Worker
208*49cdfc7eSAndroid Build Coastguard WorkerBefore Committing
209*49cdfc7eSAndroid Build Coastguard Worker-----------------
210*49cdfc7eSAndroid Build Coastguard Worker
211*49cdfc7eSAndroid Build Coastguard WorkerOne should rebuild from scratch before committing. Please see INSTALL for more
212*49cdfc7eSAndroid Build Coastguard Workerdetails.
213*49cdfc7eSAndroid Build Coastguard Worker
214*49cdfc7eSAndroid Build Coastguard WorkerOther Errata
215*49cdfc7eSAndroid Build Coastguard Worker------------
216*49cdfc7eSAndroid Build Coastguard Worker
217*49cdfc7eSAndroid Build Coastguard WorkerPlease see TODO for any issues related to the Makefile infrastructure, and
218*49cdfc7eSAndroid Build Coastguard Workerbuild structure / source tree in general.
219