1c819e2cfSJiri OlsaBuild Framework
2c819e2cfSJiri Olsa===============
3c819e2cfSJiri Olsa
4c819e2cfSJiri OlsaThe perf build framework was adopted from the kernel build system, hence the
5c819e2cfSJiri Olsaidea and the way how objects are built is the same.
6c819e2cfSJiri Olsa
7c819e2cfSJiri OlsaBasically the user provides set of 'Build' files that list objects and
8c819e2cfSJiri Olsadirectories to nest for specific target to be build.
9c819e2cfSJiri Olsa
10c819e2cfSJiri OlsaUnlike the kernel we don't have a single build object 'obj-y' list that where
11c819e2cfSJiri Olsawe setup source objects, but we support more. This allows one 'Build' file to
12c819e2cfSJiri Olsacarry a sources list for multiple build objects.
13c819e2cfSJiri Olsa
14ab6201d0SJiri Olsa
15ab6201d0SJiri OlsaBuild framework makefiles
16ab6201d0SJiri Olsa-------------------------
17c819e2cfSJiri Olsa
18c819e2cfSJiri OlsaThe build framework consists of 2 Makefiles:
19c819e2cfSJiri Olsa
20c819e2cfSJiri Olsa  Build.include
21c819e2cfSJiri Olsa  Makefile.build
22c819e2cfSJiri Olsa
23c819e2cfSJiri OlsaWhile the 'Build.include' file contains just some generic definitions, the
24c819e2cfSJiri Olsa'Makefile.build' file is the makefile used from the outside. It's
25c819e2cfSJiri Olsainterface/usage is following:
26c819e2cfSJiri Olsa
27ab6201d0SJiri Olsa  $ make -f tools/build/Makefile.build srctree=$(KSRC) dir=$(DIR) obj=$(OBJECT)
28c819e2cfSJiri Olsa
29c819e2cfSJiri Olsawhere:
30c819e2cfSJiri Olsa
31c819e2cfSJiri Olsa  KSRC   - is the path to kernel sources
32c819e2cfSJiri Olsa  DIR    - is the path to the project to be built
33c819e2cfSJiri Olsa  OBJECT - is the name of the build object
34c819e2cfSJiri Olsa
35c819e2cfSJiri OlsaWhen succefully finished the $(DIR) directory contains the final object file
36c819e2cfSJiri Olsacalled $(OBJECT)-in.o:
37c819e2cfSJiri Olsa
38c819e2cfSJiri Olsa  $ ls $(DIR)/$(OBJECT)-in.o
39c819e2cfSJiri Olsa
40c819e2cfSJiri Olsawhich includes all compiled sources described in 'Build' makefiles.
41c819e2cfSJiri Olsa
42ab6201d0SJiri Olsa
43ab6201d0SJiri OlsaBuild makefiles
44ab6201d0SJiri Olsa---------------
45c819e2cfSJiri Olsa
46c819e2cfSJiri OlsaThe user supplies 'Build' makefiles that contains a objects list, and connects
47c819e2cfSJiri Olsathe build to nested directories.
48c819e2cfSJiri Olsa
49c819e2cfSJiri OlsaAssume we have the following project structure:
50c819e2cfSJiri Olsa
51c819e2cfSJiri Olsa  ex/a.c
52c819e2cfSJiri Olsa    /b.c
53c819e2cfSJiri Olsa    /c.c
54c819e2cfSJiri Olsa    /d.c
55c819e2cfSJiri Olsa    /arch/e.c
56c819e2cfSJiri Olsa    /arch/f.c
57c819e2cfSJiri Olsa
58c819e2cfSJiri OlsaOut of which you build the 'ex' binary ' and the 'libex.a' library:
59c819e2cfSJiri Olsa
60c819e2cfSJiri Olsa  'ex'      - consists of 'a.o', 'b.o' and libex.a
61c819e2cfSJiri Olsa  'libex.a' - consists of 'c.o', 'd.o', 'e.o' and 'f.o'
62c819e2cfSJiri Olsa
63c819e2cfSJiri OlsaThe build framework does not create the 'ex' and 'libex.a' binaries for you, it
64c819e2cfSJiri Olsaonly prepares proper objects to be compiled and grouped together.
65c819e2cfSJiri Olsa
66c819e2cfSJiri OlsaTo follow the above example, the user provides following 'Build' files:
67c819e2cfSJiri Olsa
68c819e2cfSJiri Olsa  ex/Build:
69c819e2cfSJiri Olsa    ex-y += a.o
70c819e2cfSJiri Olsa    ex-y += b.o
710bdede8aSJiri Olsa    ex-y += b.o # duplicates in the lists are allowed
72c819e2cfSJiri Olsa
73c819e2cfSJiri Olsa    libex-y += c.o
74c819e2cfSJiri Olsa    libex-y += d.o
75c819e2cfSJiri Olsa    libex-y += arch/
76c819e2cfSJiri Olsa
77c819e2cfSJiri Olsa  ex/arch/Build:
78c819e2cfSJiri Olsa    libex-y += e.o
79c819e2cfSJiri Olsa    libex-y += f.o
80c819e2cfSJiri Olsa
81c819e2cfSJiri Olsaand runs:
82c819e2cfSJiri Olsa
83c819e2cfSJiri Olsa  $ make -f tools/build/Makefile.build dir=. obj=ex
84c819e2cfSJiri Olsa  $ make -f tools/build/Makefile.build dir=. obj=libex
85c819e2cfSJiri Olsa
86c819e2cfSJiri Olsawhich creates the following objects:
87c819e2cfSJiri Olsa
88c819e2cfSJiri Olsa  ex/ex-in.o
89c819e2cfSJiri Olsa  ex/libex-in.o
90c819e2cfSJiri Olsa
91c819e2cfSJiri Olsathat contain request objects names in Build files.
92c819e2cfSJiri Olsa
93c819e2cfSJiri OlsaIt's only a matter of 2 single commands to create the final binaries:
94c819e2cfSJiri Olsa
95c819e2cfSJiri Olsa  $ ar  rcs libex.a libex-in.o
96c819e2cfSJiri Olsa  $ gcc -o ex ex-in.o libex.a
97c819e2cfSJiri Olsa
98c819e2cfSJiri OlsaYou can check the 'ex' example in 'tools/build/tests/ex' for more details.
99c819e2cfSJiri Olsa
100ab6201d0SJiri Olsa
101ab6201d0SJiri OlsaMakefile.include
102ab6201d0SJiri Olsa----------------
103ab6201d0SJiri Olsa
104ab6201d0SJiri OlsaThe tools/build/Makefile.include makefile could be included
105ab6201d0SJiri Olsavia user makefiles to get usefull definitions.
106ab6201d0SJiri Olsa
107ab6201d0SJiri OlsaIt defines following interface:
108ab6201d0SJiri Olsa
109ab6201d0SJiri Olsa  - build macro definition:
110ab6201d0SJiri Olsa      build := -f $(srctree)/tools/build/Makefile.build dir=. obj
111ab6201d0SJiri Olsa
112ab6201d0SJiri Olsa    to make it easier to invoke build like:
113ab6201d0SJiri Olsa      make $(build)=ex
114ab6201d0SJiri Olsa
115ab6201d0SJiri Olsa
1167c422f55SJiri OlsaFixdep
1177c422f55SJiri Olsa------
1187c422f55SJiri OlsaIt is necessary to build the fixdep helper before invoking the build.
1197c422f55SJiri OlsaThe Makefile.include file adds the fixdep target, that could be
1207c422f55SJiri Olsainvoked by the user.
1217c422f55SJiri Olsa
1227c422f55SJiri Olsa
123ab6201d0SJiri OlsaRules
124ab6201d0SJiri Olsa-----
125c819e2cfSJiri Olsa
126c819e2cfSJiri OlsaThe build framework provides standard compilation rules to handle .S and .c
127c819e2cfSJiri Olsacompilation.
128c819e2cfSJiri Olsa
129c819e2cfSJiri OlsaIt's possible to include special rule if needed (like we do for flex or bison
130c819e2cfSJiri Olsacode generation).
131c819e2cfSJiri Olsa
132ab6201d0SJiri Olsa
133ab6201d0SJiri OlsaCFLAGS
134ab6201d0SJiri Olsa------
135c819e2cfSJiri Olsa
136c819e2cfSJiri OlsaIt's possible to alter the standard object C flags in the following way:
137c819e2cfSJiri Olsa
1382ec8107dSJiri Olsa  CFLAGS_perf.o        += '...'  - adds CFLAGS for perf.o object
1392ec8107dSJiri Olsa  CFLAGS_gtk           += '...'  - adds CFLAGS for gtk build object
1402ec8107dSJiri Olsa  CFLAGS_REMOVE_perf.o += '...'  - removes CFLAGS for perf.o object
1412ec8107dSJiri Olsa  CFLAGS_REMOVE_gtk    += '...'  - removes CFLAGS for gtk build object
142c819e2cfSJiri Olsa
143c819e2cfSJiri OlsaThis C flags changes has the scope of the Build makefile they are defined in.
144c819e2cfSJiri Olsa
145c819e2cfSJiri Olsa
146ab6201d0SJiri OlsaDependencies
147ab6201d0SJiri Olsa------------
148c819e2cfSJiri Olsa
149c819e2cfSJiri OlsaFor each built object file 'a.o' the '.a.cmd' is created and holds:
150c819e2cfSJiri Olsa
151c819e2cfSJiri Olsa  - Command line used to built that object
152c819e2cfSJiri Olsa    (for each object)
153c819e2cfSJiri Olsa
154c819e2cfSJiri Olsa  - Dependency rules generated by 'gcc -Wp,-MD,...'
155c819e2cfSJiri Olsa    (for compiled object)
156c819e2cfSJiri Olsa
157c819e2cfSJiri OlsaAll existing '.cmd' files are included in the Build process to follow properly
158c819e2cfSJiri Olsathe dependencies and trigger a rebuild when necessary.
159c819e2cfSJiri Olsa
160c819e2cfSJiri Olsa
161ab6201d0SJiri OlsaSingle rules
162ab6201d0SJiri Olsa------------
163c819e2cfSJiri Olsa
164c819e2cfSJiri OlsaIt's possible to build single object file by choice, like:
165c819e2cfSJiri Olsa
166c819e2cfSJiri Olsa  $ make util/map.o    # objects
167c819e2cfSJiri Olsa  $ make util/map.i    # preprocessor
168c819e2cfSJiri Olsa  $ make util/map.s    # assembly
169