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