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