build-system.rst 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. ==================================
  2. The QEMU build system architecture
  3. ==================================
  4. This document aims to help developers understand the architecture of the
  5. QEMU build system. As with projects using GNU autotools, the QEMU build
  6. system has two stages, first the developer runs the "configure" script
  7. to determine the local build environment characteristics, then they run
  8. "make" to build the project. There is about where the similarities with
  9. GNU autotools end, so try to forget what you know about them.
  10. Stage 1: configure
  11. ==================
  12. The QEMU configure script is written directly in shell, and should be
  13. compatible with any POSIX shell, hence it uses #!/bin/sh. An important
  14. implication of this is that it is important to avoid using bash-isms on
  15. development platforms where bash is the primary host.
  16. In contrast to autoconf scripts, QEMU's configure is expected to be
  17. silent while it is checking for features. It will only display output
  18. when an error occurs, or to show the final feature enablement summary
  19. on completion.
  20. Because QEMU uses the Meson build system under the hood, only VPATH
  21. builds are supported. There are two general ways to invoke configure &
  22. perform a build:
  23. - VPATH, build artifacts outside of QEMU source tree entirely::
  24. cd ../
  25. mkdir build
  26. cd build
  27. ../qemu/configure
  28. make
  29. - VPATH, build artifacts in a subdir of QEMU source tree::
  30. mkdir build
  31. cd build
  32. ../configure
  33. make
  34. The configure script automatically recognizes
  35. command line options for which a same-named Meson option exists;
  36. dashes in the command line are replaced with underscores.
  37. Many checks on the compilation environment are still found in configure
  38. rather than ``meson.build``, but new checks should be added directly to
  39. ``meson.build``.
  40. Patches are also welcome to move existing checks from the configure
  41. phase to ``meson.build``. When doing so, ensure that ``meson.build`` does
  42. not use anymore the keys that you have removed from ``config-host.mak``.
  43. Typically these will be replaced in ``meson.build`` by boolean variables,
  44. ``get_option('optname')`` invocations, or ``dep.found()`` expressions.
  45. In general, the remaining checks have little or no interdependencies,
  46. so they can be moved one by one.
  47. Helper functions
  48. ----------------
  49. The configure script provides a variety of helper functions to assist
  50. developers in checking for system features:
  51. ``do_cc $ARGS...``
  52. Attempt to run the system C compiler passing it $ARGS...
  53. ``do_cxx $ARGS...``
  54. Attempt to run the system C++ compiler passing it $ARGS...
  55. ``compile_object $CFLAGS``
  56. Attempt to compile a test program with the system C compiler using
  57. $CFLAGS. The test program must have been previously written to a file
  58. called $TMPC. The replacement in Meson is the compiler object ``cc``,
  59. which has methods such as ``cc.compiles()``,
  60. ``cc.check_header()``, ``cc.has_function()``.
  61. ``compile_prog $CFLAGS $LDFLAGS``
  62. Attempt to compile a test program with the system C compiler using
  63. $CFLAGS and link it with the system linker using $LDFLAGS. The test
  64. program must have been previously written to a file called $TMPC.
  65. The replacement in Meson is ``cc.find_library()`` and ``cc.links()``.
  66. ``has $COMMAND``
  67. Determine if $COMMAND exists in the current environment, either as a
  68. shell builtin, or executable binary, returning 0 on success. The
  69. replacement in Meson is ``find_program()``.
  70. ``check_define $NAME``
  71. Determine if the macro $NAME is defined by the system C compiler
  72. ``check_include $NAME``
  73. Determine if the include $NAME file is available to the system C
  74. compiler. The replacement in Meson is ``cc.has_header()``.
  75. ``write_c_skeleton``
  76. Write a minimal C program main() function to the temporary file
  77. indicated by $TMPC
  78. ``error_exit $MESSAGE $MORE...``
  79. Print $MESSAGE to stderr, followed by $MORE... and then exit from the
  80. configure script with non-zero status
  81. ``query_pkg_config $ARGS...``
  82. Run pkg-config passing it $ARGS. If QEMU is doing a static build,
  83. then --static will be automatically added to $ARGS
  84. Stage 2: Meson
  85. ==============
  86. The Meson build system is currently used to describe the build
  87. process for:
  88. 1) executables, which include:
  89. - Tools - ``qemu-img``, ``qemu-nbd``, ``qga`` (guest agent), etc
  90. - System emulators - ``qemu-system-$ARCH``
  91. - Userspace emulators - ``qemu-$ARCH``
  92. - Unit tests
  93. 2) documentation
  94. 3) ROMs, which can be either installed as binary blobs or compiled
  95. 4) other data files, such as icons or desktop files
  96. All executables are built by default, except for some ``contrib/``
  97. binaries that are known to fail to build on some platforms (for example
  98. 32-bit or big-endian platforms). Tests are also built by default,
  99. though that might change in the future.
  100. The source code is highly modularized, split across many files to
  101. facilitate building of all of these components with as little duplicated
  102. compilation as possible. Using the Meson "sourceset" functionality,
  103. ``meson.build`` files group the source files in rules that are
  104. enabled according to the available system libraries and to various
  105. configuration symbols. Sourcesets belong to one of four groups:
  106. Subsystem sourcesets:
  107. Various subsystems that are common to both tools and emulators have
  108. their own sourceset, for example ``block_ss`` for the block device subsystem,
  109. ``chardev_ss`` for the character device subsystem, etc. These sourcesets
  110. are then turned into static libraries as follows::
  111. libchardev = static_library('chardev', chardev_ss.sources(),
  112. name_suffix: 'fa',
  113. build_by_default: false)
  114. chardev = declare_dependency(link_whole: libchardev)
  115. As of Meson 0.55.1, the special ``.fa`` suffix should be used for everything
  116. that is used with ``link_whole``, to ensure that the link flags are placed
  117. correctly in the command line.
  118. Target-independent emulator sourcesets:
  119. Various general purpose helper code is compiled only once and
  120. the .o files are linked into all output binaries that need it.
  121. This includes error handling infrastructure, standard data structures,
  122. platform portability wrapper functions, etc.
  123. Target-independent code lives in the ``common_ss``, ``softmmu_ss`` and
  124. ``user_ss`` sourcesets. ``common_ss`` is linked into all emulators,
  125. ``softmmu_ss`` only in system emulators, ``user_ss`` only in user-mode
  126. emulators.
  127. Target-independent sourcesets must exercise particular care when using
  128. ``if_false`` rules. The ``if_false`` rule will be used correctly when linking
  129. emulator binaries; however, when *compiling* target-independent files
  130. into .o files, Meson may need to pick *both* the ``if_true`` and
  131. ``if_false`` sides to cater for targets that want either side. To
  132. achieve that, you can add a special rule using the ``CONFIG_ALL``
  133. symbol::
  134. # Some targets have CONFIG_ACPI, some don't, so this is not enough
  135. softmmu_ss.add(when: 'CONFIG_ACPI', if_true: files('acpi.c'),
  136. if_false: files('acpi-stub.c'))
  137. # This is required as well:
  138. softmmu_ss.add(when: 'CONFIG_ALL', if_true: files('acpi-stub.c'))
  139. Target-dependent emulator sourcesets:
  140. In the target-dependent set lives CPU emulation, some device emulation and
  141. much glue code. This sometimes also has to be compiled multiple times,
  142. once for each target being built. Target-dependent files are included
  143. in the ``specific_ss`` sourceset.
  144. Each emulator also includes sources for files in the ``hw/`` and ``target/``
  145. subdirectories. The subdirectory used for each emulator comes
  146. from the target's definition of ``TARGET_BASE_ARCH`` or (if missing)
  147. ``TARGET_ARCH``, as found in ``default-configs/targets/*.mak``.
  148. Each subdirectory in ``hw/`` adds one sourceset to the ``hw_arch`` dictionary,
  149. for example::
  150. arm_ss = ss.source_set()
  151. arm_ss.add(files('boot.c'), fdt)
  152. ...
  153. hw_arch += {'arm': arm_ss}
  154. The sourceset is only used for system emulators.
  155. Each subdirectory in ``target/`` instead should add one sourceset to each
  156. of the ``target_arch`` and ``target_softmmu_arch``, which are used respectively
  157. for all emulators and for system emulators only. For example::
  158. arm_ss = ss.source_set()
  159. arm_softmmu_ss = ss.source_set()
  160. ...
  161. target_arch += {'arm': arm_ss}
  162. target_softmmu_arch += {'arm': arm_softmmu_ss}
  163. Module sourcesets:
  164. There are two dictionaries for modules: ``modules`` is used for
  165. target-independent modules and ``target_modules`` is used for
  166. target-dependent modules. When modules are disabled the ``module``
  167. source sets are added to ``softmmu_ss`` and the ``target_modules``
  168. source sets are added to ``specific_ss``.
  169. Both dictionaries are nested. One dictionary is created per
  170. subdirectory, and these per-subdirectory dictionaries are added to
  171. the toplevel dictionaries. For example::
  172. hw_display_modules = {}
  173. qxl_ss = ss.source_set()
  174. ...
  175. hw_display_modules += { 'qxl': qxl_ss }
  176. modules += { 'hw-display': hw_display_modules }
  177. Utility sourcesets:
  178. All binaries link with a static library ``libqemuutil.a``. This library
  179. is built from several sourcesets; most of them however host generated
  180. code, and the only two of general interest are ``util_ss`` and ``stub_ss``.
  181. The separation between these two is purely for documentation purposes.
  182. ``util_ss`` contains generic utility files. Even though this code is only
  183. linked in some binaries, sometimes it requires hooks only in some of
  184. these and depend on other functions that are not fully implemented by
  185. all QEMU binaries. ``stub_ss`` links dummy stubs that will only be linked
  186. into the binary if the real implementation is not present. In a way,
  187. the stubs can be thought of as a portable implementation of the weak
  188. symbols concept.
  189. The following files concur in the definition of which files are linked
  190. into each emulator:
  191. ``default-configs/devices/*.mak``
  192. The files under ``default-configs/devices/`` control the boards and devices
  193. that are built into each QEMU system emulation targets. They merely contain
  194. a list of config variable definitions such as::
  195. include arm-softmmu.mak
  196. CONFIG_XLNX_ZYNQMP_ARM=y
  197. CONFIG_XLNX_VERSAL=y
  198. ``*/Kconfig``
  199. These files are processed together with ``default-configs/devices/*.mak`` and
  200. describe the dependencies between various features, subsystems and
  201. device models. They are described in :ref:`kconfig`
  202. ``default-configs/targets/*.mak``
  203. These files mostly define symbols that appear in the ``*-config-target.h``
  204. file for each emulator [#cfgtarget]_. However, the ``TARGET_ARCH``
  205. and ``TARGET_BASE_ARCH`` will also be used to select the ``hw/`` and
  206. ``target/`` subdirectories that are compiled into each target.
  207. .. [#cfgtarget] This header is included by ``qemu/osdep.h`` when
  208. compiling files from the target-specific sourcesets.
  209. These files rarely need changing unless you are adding a completely
  210. new target, or enabling new devices or hardware for a particular
  211. system/userspace emulation target
  212. Adding checks
  213. -------------
  214. New checks should be added to Meson. Compiler checks can be as simple as
  215. the following::
  216. config_host_data.set('HAVE_BTRFS_H', cc.has_header('linux/btrfs.h'))
  217. A more complex task such as adding a new dependency usually
  218. comprises the following tasks:
  219. - Add a Meson build option to meson_options.txt.
  220. - Add code to perform the actual feature check.
  221. - Add code to include the feature status in ``config-host.h``
  222. - Add code to print out the feature status in the configure summary
  223. upon completion.
  224. Taking the probe for SDL2_Image as an example, we have the following
  225. in ``meson_options.txt``::
  226. option('sdl_image', type : 'feature', value : 'auto',
  227. description: 'SDL Image support for icons')
  228. Unless the option was given a non-``auto`` value (on the configure
  229. command line), the detection code must be performed only if the
  230. dependency will be used::
  231. sdl_image = not_found
  232. if not get_option('sdl_image').auto() or have_system
  233. sdl_image = dependency('SDL2_image', required: get_option('sdl_image'),
  234. method: 'pkg-config',
  235. static: enable_static)
  236. endif
  237. This avoids warnings on static builds of user-mode emulators, for example.
  238. Most of the libraries used by system-mode emulators are not available for
  239. static linking.
  240. The other supporting code is generally simple::
  241. # Create config-host.h (if applicable)
  242. config_host_data.set('CONFIG_SDL_IMAGE', sdl_image.found())
  243. # Summary
  244. summary_info += {'SDL image support': sdl_image.found()}
  245. For the configure script to parse the new option, the
  246. ``scripts/meson-buildoptions.sh`` file must be up-to-date; ``make
  247. update-buildoptions`` (or just ``make``) will take care of updating it.
  248. Support scripts
  249. ---------------
  250. Meson has a special convention for invoking Python scripts: if their
  251. first line is ``#! /usr/bin/env python3`` and the file is *not* executable,
  252. find_program() arranges to invoke the script under the same Python
  253. interpreter that was used to invoke Meson. This is the most common
  254. and preferred way to invoke support scripts from Meson build files,
  255. because it automatically uses the value of configure's --python= option.
  256. In case the script is not written in Python, use a ``#! /usr/bin/env ...``
  257. line and make the script executable.
  258. Scripts written in Python, where it is desirable to make the script
  259. executable (for example for test scripts that developers may want to
  260. invoke from the command line, such as tests/qapi-schema/test-qapi.py),
  261. should be invoked through the ``python`` variable in meson.build. For
  262. example::
  263. test('QAPI schema regression tests', python,
  264. args: files('test-qapi.py'),
  265. env: test_env, suite: ['qapi-schema', 'qapi-frontend'])
  266. This is needed to obey the --python= option passed to the configure
  267. script, which may point to something other than the first python3
  268. binary on the path.
  269. Stage 3: makefiles
  270. ==================
  271. The use of GNU make is required with the QEMU build system.
  272. The output of Meson is a build.ninja file, which is used with the Ninja
  273. build system. QEMU uses a different approach, where Makefile rules are
  274. synthesized from the build.ninja file. The main Makefile includes these
  275. rules and wraps them so that e.g. submodules are built before QEMU.
  276. The resulting build system is largely non-recursive in nature, in
  277. contrast to common practices seen with automake.
  278. Tests are also ran by the Makefile with the traditional ``make check``
  279. phony target, while benchmarks are run with ``make bench``. Meson test
  280. suites such as ``unit`` can be ran with ``make check-unit`` too. It is also
  281. possible to run tests defined in meson.build with ``meson test``.
  282. Useful make targets
  283. -------------------
  284. ``help``
  285. Print a help message for the most common build targets.
  286. ``print-VAR``
  287. Print the value of the variable VAR. Useful for debugging the build
  288. system.
  289. Important files for the build system
  290. ====================================
  291. Statically defined files
  292. ------------------------
  293. The following key files are statically defined in the source tree, with
  294. the rules needed to build QEMU. Their behaviour is influenced by a
  295. number of dynamically created files listed later.
  296. ``Makefile``
  297. The main entry point used when invoking make to build all the components
  298. of QEMU. The default 'all' target will naturally result in the build of
  299. every component. Makefile takes care of recursively building submodules
  300. directly via a non-recursive set of rules.
  301. ``*/meson.build``
  302. The meson.build file in the root directory is the main entry point for the
  303. Meson build system, and it coordinates the configuration and build of all
  304. executables. Build rules for various subdirectories are included in
  305. other meson.build files spread throughout the QEMU source tree.
  306. ``tests/Makefile.include``
  307. Rules for external test harnesses. These include the TCG tests,
  308. ``qemu-iotests`` and the Avocado-based integration tests.
  309. ``tests/docker/Makefile.include``
  310. Rules for Docker tests. Like tests/Makefile, this file is included
  311. directly by the top level Makefile, anything defined in this file will
  312. influence the entire build system.
  313. ``tests/vm/Makefile.include``
  314. Rules for VM-based tests. Like tests/Makefile, this file is included
  315. directly by the top level Makefile, anything defined in this file will
  316. influence the entire build system.
  317. Dynamically created files
  318. -------------------------
  319. The following files are generated dynamically by configure in order to
  320. control the behaviour of the statically defined makefiles. This avoids
  321. the need for QEMU makefiles to go through any pre-processing as seen
  322. with autotools, where Makefile.am generates Makefile.in which generates
  323. Makefile.
  324. Built by configure:
  325. ``config-host.mak``
  326. When configure has determined the characteristics of the build host it
  327. will write a long list of variables to config-host.mak file. This
  328. provides the various install directories, compiler / linker flags and a
  329. variety of ``CONFIG_*`` variables related to optionally enabled features.
  330. This is imported by the top level Makefile and meson.build in order to
  331. tailor the build output.
  332. config-host.mak is also used as a dependency checking mechanism. If make
  333. sees that the modification timestamp on configure is newer than that on
  334. config-host.mak, then configure will be re-run.
  335. The variables defined here are those which are applicable to all QEMU
  336. build outputs. Variables which are potentially different for each
  337. emulator target are defined by the next file...
  338. Built by Meson:
  339. ``${TARGET-NAME}-config-devices.mak``
  340. TARGET-NAME is again the name of a system or userspace emulator. The
  341. config-devices.mak file is automatically generated by make using the
  342. scripts/make_device_config.sh program, feeding it the
  343. default-configs/$TARGET-NAME file as input.
  344. ``config-host.h``, ``$TARGET_NAME-config-target.h``, ``$TARGET_NAME-config-devices.h``
  345. These files are used by source code to determine what features are
  346. enabled. They are generated from the contents of the corresponding
  347. ``*.mak`` files using Meson's ``configure_file()`` function.
  348. ``build.ninja``
  349. The build rules.
  350. Built by Makefile:
  351. ``Makefile.ninja``
  352. A Makefile include that bridges to ninja for the actual build. The
  353. Makefile is mostly a list of targets that Meson included in build.ninja.
  354. ``Makefile.mtest``
  355. The Makefile definitions that let "make check" run tests defined in
  356. meson.build. The rules are produced from Meson's JSON description of
  357. tests (obtained with "meson introspect --tests") through the script
  358. scripts/mtest2make.py.