xref: /openbmc/qemu/docs/devel/build-system.rst (revision c27c1cc3)
1==================================
2The QEMU build system architecture
3==================================
4
5This document aims to help developers understand the architecture of the
6QEMU build system. As with projects using GNU autotools, the QEMU build
7system has two stages, first the developer runs the "configure" script
8to determine the local build environment characteristics, then they run
9"make" to build the project. There is about where the similarities with
10GNU autotools end, so try to forget what you know about them.
11
12
13Stage 1: configure
14==================
15
16The QEMU configure script is written directly in shell, and should be
17compatible with any POSIX shell, hence it uses #!/bin/sh. An important
18implication of this is that it is important to avoid using bash-isms on
19development platforms where bash is the primary host.
20
21In contrast to autoconf scripts, QEMU's configure is expected to be
22silent while it is checking for features. It will only display output
23when an error occurs, or to show the final feature enablement summary
24on completion.
25
26Because QEMU uses the Meson build system under the hood, only VPATH
27builds are supported.  There are two general ways to invoke configure &
28perform a build:
29
30 - VPATH, build artifacts outside of QEMU source tree entirely::
31
32     cd ../
33     mkdir build
34     cd build
35     ../qemu/configure
36     make
37
38 - VPATH, build artifacts in a subdir of QEMU source tree::
39
40     mkdir build
41     cd build
42     ../configure
43     make
44
45For now, checks on the compilation environment are found in configure
46rather than meson.build, though this is expected to change.  The command
47line is parsed in the configure script and, whenever needed, converted
48into the appropriate options to Meson.
49
50New checks should be added to Meson, which usually comprises the
51following tasks:
52
53 - Add a Meson build option to meson_options.txt.
54
55 - Add support to the command line arg parser to handle any new
56   `--enable-XXX`/`--disable-XXX` flags required by the feature.
57
58 - Add information to the help output message to report on the new
59   feature flag.
60
61 - Add code to perform the actual feature check.
62
63 - Add code to include the feature status in `config-host.h`
64
65 - Add code to print out the feature status in the configure summary
66   upon completion.
67
68
69Taking the probe for SDL as an example, we have the following pieces
70in configure::
71
72  # Initial variable state
73  sdl=auto
74
75  ..snip..
76
77  # Configure flag processing
78  --disable-gnutls) sdl=disabled
79  ;;
80  --enable-gnutls) sdl=enabled
81  ;;
82
83  ..snip..
84
85  # Help output feature message
86  sdl             SDL UI
87
88  ..snip..
89
90  # Meson invocation
91  -Dsdl=$sdl
92
93In meson_options.txt::
94
95  option('sdl', type : 'feature', value : 'auto')
96
97In meson.build::
98
99  # Detect dependency
100  sdl = dependency('sdl2',
101                   required: get_option('sdl'),
102                   static: enable_static)
103
104  # Create config-host.h
105  config_host_data.set('CONFIG_SDL', sdl.found())
106
107  # Summary
108  summary_info += {'SDL support':       sdl.found()}
109
110
111
112Helper functions
113----------------
114
115The configure script provides a variety of helper functions to assist
116developers in checking for system features:
117
118`do_cc $ARGS...`
119   Attempt to run the system C compiler passing it $ARGS...
120
121`do_cxx $ARGS...`
122   Attempt to run the system C++ compiler passing it $ARGS...
123
124`compile_object $CFLAGS`
125   Attempt to compile a test program with the system C compiler using
126   $CFLAGS. The test program must have been previously written to a file
127   called $TMPC.
128
129`compile_prog $CFLAGS $LDFLAGS`
130   Attempt to compile a test program with the system C compiler using
131   $CFLAGS and link it with the system linker using $LDFLAGS. The test
132   program must have been previously written to a file called $TMPC.
133
134`has $COMMAND`
135   Determine if $COMMAND exists in the current environment, either as a
136   shell builtin, or executable binary, returning 0 on success.
137
138`path_of $COMMAND`
139   Return the fully qualified path of $COMMAND, printing it to stdout,
140   and returning 0 on success.
141
142`check_define $NAME`
143   Determine if the macro $NAME is defined by the system C compiler
144
145`check_include $NAME`
146   Determine if the include $NAME file is available to the system C
147   compiler
148
149`write_c_skeleton`
150   Write a minimal C program main() function to the temporary file
151   indicated by $TMPC
152
153`feature_not_found $NAME $REMEDY`
154   Print a message to stderr that the feature $NAME was not available
155   on the system, suggesting the user try $REMEDY to address the
156   problem.
157
158`error_exit $MESSAGE $MORE...`
159   Print $MESSAGE to stderr, followed by $MORE... and then exit from the
160   configure script with non-zero status
161
162`query_pkg_config $ARGS...`
163   Run pkg-config passing it $ARGS. If QEMU is doing a static build,
164   then --static will be automatically added to $ARGS
165
166
167Stage 2: Meson
168==============
169
170The Meson build system is currently used to describe the build
171process for:
172
1731) executables, which include:
174
175   - Tools - qemu-img, qemu-nbd, qga (guest agent), etc
176
177   - System emulators - qemu-system-$ARCH
178
179   - Userspace emulators - qemu-$ARCH
180
181   - Some (but not all) unit tests
182
1832) documentation
184
1853) ROMs, which can be either installed as binary blobs or compiled
186
1874) other data files, such as icons or desktop files
188
189The source code is highly modularized, split across many files to
190facilitate building of all of these components with as little duplicated
191compilation as possible. The Meson "sourceset" functionality is used
192to list the files and their dependency on various configuration
193symbols.
194
195Various subsystems that are common to both tools and emulators have
196their own sourceset, for example `block_ss` for the block device subsystem,
197`chardev_ss` for the character device subsystem, etc.  These sourcesets
198are then turned into static libraries as follows::
199
200    libchardev = static_library('chardev', chardev_ss.sources(),
201                                name_suffix: 'fa',
202                                build_by_default: false)
203
204    chardev = declare_dependency(link_whole: libchardev)
205
206The special `.fa` suffix is needed as long as unit tests are built with
207the older Makefile infrastructure, and will go away later.
208
209Files linked into emulator targets there can be split into two distinct groups
210of files, those which are independent of the QEMU emulation target and
211those which are dependent on the QEMU emulation target.
212
213In the target-independent set lives various general purpose helper code,
214such as error handling infrastructure, standard data structures,
215platform portability wrapper functions, etc. This code can be compiled
216once only and the .o files linked into all output binaries.
217Target-independent code lives in the `common_ss`, `softmmu_ss` and
218`user_ss` sourcesets.  `common_ss` is linked into all emulators, `softmmu_ss`
219only in system emulators, `user_ss` only in user-mode emulators.
220
221In the target-dependent set lives CPU emulation, device emulation and
222much glue code. This sometimes also has to be compiled multiple times,
223once for each target being built.
224
225All binaries link with a static library `libqemuutil.a`, which is then
226linked to all the binaries.  `libqemuutil.a` is built from several
227sourcesets; most of them however host generated code, and the only two
228of general interest are `util_ss` and `stub_ss`.
229
230The separation between these two is purely for documentation purposes.
231`util_ss` contains generic utility files.  Even though this code is only
232linked in some binaries, sometimes it requires hooks only in some of
233these and depend on other functions that are not fully implemented by
234all QEMU binaries.  `stub_ss` links dummy stubs that will only be linked
235into the binary if the real implementation is not present.  In a way,
236the stubs can be thought of as a portable implementation of the weak
237symbols concept.
238
239The following files concur in the definition of which files are linked
240into each emulator:
241
242`default-configs/*.mak`
243  The files under default-configs/ control what emulated hardware is built
244  into each QEMU system and userspace emulator targets. They merely contain
245  a list of config variable definitions like the machines that should be
246  included. For example, default-configs/aarch64-softmmu.mak has::
247
248    include arm-softmmu.mak
249    CONFIG_XLNX_ZYNQMP_ARM=y
250    CONFIG_XLNX_VERSAL=y
251
252`*/Kconfig`
253  These files are processed together with `default-configs/*.mak` and
254  describe the dependencies between various features, subsystems and
255  device models.  They are described in kconfig.rst.
256
257These files rarely need changing unless new devices / hardware need to
258be enabled for a particular system/userspace emulation target
259
260
261Support scripts
262---------------
263
264Meson has a special convention for invoking Python scripts: if their
265first line is `#! /usr/bin/env python3` and the file is *not* executable,
266find_program() arranges to invoke the script under the same Python
267interpreter that was used to invoke Meson.  This is the most common
268and preferred way to invoke support scripts from Meson build files,
269because it automatically uses the value of configure's --python= option.
270
271In case the script is not written in Python, use a `#! /usr/bin/env ...`
272line and make the script executable.
273
274Scripts written in Python, where it is desirable to make the script
275executable (for example for test scripts that developers may want to
276invoke from the command line, such as tests/qapi-schema/test-qapi.py),
277should be invoked through the `python` variable in meson.build. For
278example::
279
280  test('QAPI schema regression tests', python,
281       args: files('test-qapi.py'),
282       env: test_env, suite: ['qapi-schema', 'qapi-frontend'])
283
284This is needed to obey the --python= option passed to the configure
285script, which may point to something other than the first python3
286binary on the path.
287
288
289Stage 3: makefiles
290==================
291
292The use of GNU make is required with the QEMU build system.
293
294The output of Meson is a build.ninja file, which is used with the Ninja
295build system.  QEMU uses a different approach, where Makefile rules are
296synthesized from the build.ninja file.  The main Makefile includes these
297rules and wraps them so that e.g. submodules are built before QEMU.
298The resulting build system is largely non-recursive in nature, in
299contrast to common practices seen with automake.
300
301Tests are also ran by the Makefile with the traditional `make check`
302phony target.  Meson test suites such as `unit` can be ran with `make
303check-unit` too.  It is also possible to run tests defined in meson.build
304with `meson test`.
305
306The following text is only relevant for unit tests which still have to
307be converted to Meson.
308
309All binaries should link to `libqemuutil.a`, e.g.:
310
311   qemu-img$(EXESUF): qemu-img.o ..snip.. libqemuutil.a
312
313On Windows, all binaries have the suffix `.exe`, so all Makefile rules
314which create binaries must include the $(EXESUF) variable on the binary
315name. e.g.
316
317   qemu-img$(EXESUF): qemu-img.o ..snip..
318
319This expands to `.exe` on Windows, or an empty string on other platforms.
320
321Variable naming
322---------------
323
324The QEMU convention is to define variables to list different groups of
325object files. These are named with the convention $PREFIX-obj-y.  The
326Meson `chardev` variable in the previous example corresponds to a
327variable 'chardev-obj-y'.
328
329Likewise, tests that are executed by `make check-unit` are grouped into
330a variable check-unit-y, like this:
331
332  check-unit-y += tests/test-visitor-serialization$(EXESUF)
333  check-unit-y += tests/test-iov$(EXESUF)
334  check-unit-y += tests/test-bitmap$(EXESUF)
335
336When a test or object file which needs to be conditionally built based
337on some characteristic of the host system, the configure script will
338define a variable for the conditional. For example, on Windows it will
339define $(CONFIG_POSIX) with a value of 'n' and $(CONFIG_WIN32) with a
340value of 'y'. It is now possible to use the config variables when
341listing object files. For example,
342
343  check-unit-$(CONFIG_POSIX) += tests/test-vmstate$(EXESUF)
344
345On Windows this expands to
346
347  check-unit-n += tests/vmstate.exe
348
349Since the `check-unit` target only runs tests included in `$(check-unit-y)`,
350POSIX specific tests listed in `$(util-obj-n)` are ignored on the Windows
351platform builds.
352
353
354CFLAGS / LDFLAGS / LIBS handling
355--------------------------------
356
357There are many different binaries being built with differing purposes,
358and some of them might even be 3rd party libraries pulled in via git
359submodules. As such the use of the global CFLAGS variable is generally
360avoided in QEMU, since it would apply to too many build targets.
361
362Flags that are needed by any QEMU code (i.e. everything *except* GIT
363submodule projects) are put in $(QEMU_CFLAGS) variable. For linker
364flags the $(LIBS) variable is sometimes used, but a couple of more
365targeted variables are preferred.
366
367In addition to these variables, it is possible to provide cflags and
368libs against individual source code files, by defining variables of the
369form $FILENAME-cflags and $FILENAME-libs. For example, the test
370test-crypto-tlscredsx509 needs to link to the libtasn1 library,
371so tests/Makefile.include defines some variables:
372
373  tests/crypto-tls-x509-helpers.o-cflags := $(TASN1_CFLAGS)
374  tests/crypto-tls-x509-helpers.o-libs := $(TASN1_LIBS)
375
376The scope is a little different between the two variables. The libs get
377used when linking any target binary that includes the curl.o object
378file, while the cflags get used when compiling the curl.c file only.
379
380
381Important files for the build system
382====================================
383
384Statically defined files
385------------------------
386
387The following key files are statically defined in the source tree, with
388the rules needed to build QEMU. Their behaviour is influenced by a
389number of dynamically created files listed later.
390
391`Makefile`
392  The main entry point used when invoking make to build all the components
393  of QEMU. The default 'all' target will naturally result in the build of
394  every component. Makefile takes care of recursively building submodules
395  directly via a non-recursive set of rules.
396
397`*/meson.build`
398  The meson.build file in the root directory is the main entry point for the
399  Meson build system, and it coordinates the configuration and build of all
400  executables.  Build rules for various subdirectories are included in
401  other meson.build files spread throughout the QEMU source tree.
402
403`rules.mak`
404  This file provides the generic helper rules for invoking build tools, in
405  particular the compiler and linker.
406
407`tests/Makefile.include`
408  Rules for building the unit tests. This file is included directly by the
409  top level Makefile, so anything defined in this file will influence the
410  entire build system. Care needs to be taken when writing rules for tests
411  to ensure they only apply to the unit test execution / build.
412
413`tests/docker/Makefile.include`
414  Rules for Docker tests. Like tests/Makefile, this file is included
415  directly by the top level Makefile, anything defined in this file will
416  influence the entire build system.
417
418`tests/vm/Makefile.include`
419  Rules for VM-based tests. Like tests/Makefile, this file is included
420  directly by the top level Makefile, anything defined in this file will
421  influence the entire build system.
422
423Dynamically created files
424-------------------------
425
426The following files are generated dynamically by configure in order to
427control the behaviour of the statically defined makefiles. This avoids
428the need for QEMU makefiles to go through any pre-processing as seen
429with autotools, where Makefile.am generates Makefile.in which generates
430Makefile.
431
432Built by configure:
433
434`config-host.mak`
435  When configure has determined the characteristics of the build host it
436  will write a long list of variables to config-host.mak file. This
437  provides the various install directories, compiler / linker flags and a
438  variety of `CONFIG_*` variables related to optionally enabled features.
439  This is imported by the top level Makefile and meson.build in order to
440  tailor the build output.
441
442  config-host.mak is also used as a dependency checking mechanism. If make
443  sees that the modification timestamp on configure is newer than that on
444  config-host.mak, then configure will be re-run.
445
446  The variables defined here are those which are applicable to all QEMU
447  build outputs. Variables which are potentially different for each
448  emulator target are defined by the next file...
449
450`$TARGET-NAME/config-target.mak`
451  TARGET-NAME is the name of a system or userspace emulator, for example,
452  x86_64-softmmu denotes the system emulator for the x86_64 architecture.
453  This file contains the variables which need to vary on a per-target
454  basis. For example, it will indicate whether KVM or Xen are enabled for
455  the target and any other potential custom libraries needed for linking
456  the target.
457
458
459Built by Meson:
460
461`${TARGET-NAME}-config-devices.mak`
462  TARGET-NAME is again the name of a system or userspace emulator. The
463  config-devices.mak file is automatically generated by make using the
464  scripts/make_device_config.sh program, feeding it the
465  default-configs/$TARGET-NAME file as input.
466
467`config-host.h`, `$TARGET-NAME/config-target.h`, `$TARGET-NAME/config-devices.h`
468  These files are used by source code to determine what features
469  are enabled.  They are generated from the contents of the corresponding
470  `*.h` files using the scripts/create_config program. This extracts
471  relevant variables and formats them as C preprocessor macros.
472
473`build.ninja`
474  The build rules.
475
476
477Built by Makefile:
478
479`Makefile.ninja`
480  A Makefile conversion of the build rules in build.ninja.  The conversion
481  is straightforward and, were it necessary to debug the rules produced
482  by Meson, it should be enough to look at build.ninja.  The conversion
483  is performed by scripts/ninjatool.py.
484
485`Makefile.mtest`
486  The Makefile definitions that let "make check" run tests defined in
487  meson.build.  The rules are produced from Meson's JSON description of
488  tests (obtained with "meson introspect --tests") through the script
489  scripts/mtest2make.py.
490
491
492Useful make targets
493-------------------
494
495`help`
496  Print a help message for the most common build targets.
497
498`print-VAR`
499  Print the value of the variable VAR. Useful for debugging the build
500  system.
501