2
0

build-system.rst 20 KB

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