build-system.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. The QEMU build system architecture
  2. ==================================
  3. This document aims to help developers understand the architecture of the
  4. QEMU build system. As with projects using GNU autotools, the QEMU build
  5. system has two stages, first the developer runs the "configure" script
  6. to determine the local build environment characteristics, then they run
  7. "make" to build the project. There is about where the similarities with
  8. GNU autotools end, so try to forget what you know about them.
  9. Stage 1: configure
  10. ==================
  11. The QEMU configure script is written directly in shell, and should be
  12. compatible with any POSIX shell, hence it uses #!/bin/sh. An important
  13. implication of this is that it is important to avoid using bash-isms on
  14. development platforms where bash is the primary host.
  15. In contrast to autoconf scripts, QEMU's configure is expected to be
  16. silent while it is checking for features. It will only display output
  17. when an error occurs, or to show the final feature enablement summary
  18. on completion.
  19. Adding new checks to the configure script usually comprises the
  20. following tasks:
  21. - Initialize one or more variables with the default feature state.
  22. Ideally features should auto-detect whether they are present,
  23. so try to avoid hardcoding the initial state to either enabled
  24. or disabled, as that forces the user to pass a --enable-XXX
  25. / --disable-XXX flag on every invocation of configure.
  26. - Add support to the command line arg parser to handle any new
  27. --enable-XXX / --disable-XXX flags required by the feature XXX.
  28. - Add information to the help output message to report on the new
  29. feature flag.
  30. - Add code to perform the actual feature check. As noted above, try to
  31. be fully dynamic in checking enablement/disablement.
  32. - Add code to print out the feature status in the configure summary
  33. upon completion.
  34. - Add any new makefile variables to $config_host_mak on completion.
  35. Taking (a simplified version of) the probe for gnutls from configure,
  36. we have the following pieces:
  37. # Initial variable state
  38. gnutls=""
  39. ..snip..
  40. # Configure flag processing
  41. --disable-gnutls) gnutls="no"
  42. ;;
  43. --enable-gnutls) gnutls="yes"
  44. ;;
  45. ..snip..
  46. # Help output feature message
  47. gnutls GNUTLS cryptography support
  48. ..snip..
  49. # Test for gnutls
  50. if test "$gnutls" != "no"; then
  51. if ! $pkg_config --exists "gnutls"; then
  52. gnutls_cflags=`$pkg_config --cflags gnutls`
  53. gnutls_libs=`$pkg_config --libs gnutls`
  54. libs_softmmu="$gnutls_libs $libs_softmmu"
  55. libs_tools="$gnutls_libs $libs_tools"
  56. QEMU_CFLAGS="$QEMU_CFLAGS $gnutls_cflags"
  57. gnutls="yes"
  58. elif test "$gnutls" = "yes"; then
  59. feature_not_found "gnutls" "Install gnutls devel"
  60. else
  61. gnutls="no"
  62. fi
  63. fi
  64. ..snip..
  65. # Completion feature summary
  66. echo "GNUTLS support $gnutls"
  67. ..snip..
  68. # Define make variables
  69. if test "$gnutls" = "yes" ; then
  70. echo "CONFIG_GNUTLS=y" >> $config_host_mak
  71. fi
  72. Helper functions
  73. ----------------
  74. The configure script provides a variety of helper functions to assist
  75. developers in checking for system features:
  76. - do_cc $ARGS...
  77. Attempt to run the system C compiler passing it $ARGS...
  78. - do_cxx $ARGS...
  79. Attempt to run the system C++ compiler passing it $ARGS...
  80. - compile_object $CFLAGS
  81. Attempt to compile a test program with the system C compiler using
  82. $CFLAGS. The test program must have been previously written to a file
  83. called $TMPC.
  84. - compile_prog $CFLAGS $LDFLAGS
  85. Attempt to compile a test program with the system C compiler using
  86. $CFLAGS and link it with the system linker using $LDFLAGS. The test
  87. program must have been previously written to a file called $TMPC.
  88. - has $COMMAND
  89. Determine if $COMMAND exists in the current environment, either as a
  90. shell builtin, or executable binary, returning 0 on success.
  91. - path_of $COMMAND
  92. Return the fully qualified path of $COMMAND, printing it to stdout,
  93. and returning 0 on success.
  94. - check_define $NAME
  95. Determine if the macro $NAME is defined by the system C compiler
  96. - check_include $NAME
  97. Determine if the include $NAME file is available to the system C
  98. compiler
  99. - write_c_skeleton
  100. Write a minimal C program main() function to the temporary file
  101. indicated by $TMPC
  102. - feature_not_found $NAME $REMEDY
  103. Print a message to stderr that the feature $NAME was not available
  104. on the system, suggesting the user try $REMEDY to address the
  105. problem.
  106. - error_exit $MESSAGE $MORE...
  107. Print $MESSAGE to stderr, followed by $MORE... and then exit from the
  108. configure script with non-zero status
  109. - query_pkg_config $ARGS...
  110. Run pkg-config passing it $ARGS. If QEMU is doing a static build,
  111. then --static will be automatically added to $ARGS
  112. Stage 2: makefiles
  113. ==================
  114. The use of GNU make is required with the QEMU build system.
  115. Although the source code is spread across multiple subdirectories, the
  116. build system should be considered largely non-recursive in nature, in
  117. contrast to common practices seen with automake. There is some recursive
  118. invocation of make, but this is related to the things being built,
  119. rather than the source directory structure.
  120. QEMU currently supports both VPATH and non-VPATH builds, so there are
  121. three general ways to invoke configure & perform a build.
  122. - VPATH, build artifacts outside of QEMU source tree entirely
  123. cd ../
  124. mkdir build
  125. cd build
  126. ../qemu/configure
  127. make
  128. - VPATH, build artifacts in a subdir of QEMU source tree
  129. mkdir build
  130. cd build
  131. ../configure
  132. make
  133. - non-VPATH, build artifacts everywhere
  134. ./configure
  135. make
  136. The QEMU maintainers generally recommend that a VPATH build is used by
  137. developers. Patches to QEMU are expected to ensure VPATH build still
  138. works.
  139. Module structure
  140. ----------------
  141. There are a number of key outputs of the QEMU build system:
  142. - Tools - qemu-img, qemu-nbd, qga (guest agent), etc
  143. - System emulators - qemu-system-$ARCH
  144. - Userspace emulators - qemu-$ARCH
  145. - Unit tests
  146. The source code is highly modularized, split across many files to
  147. facilitate building of all of these components with as little duplicated
  148. compilation as possible. There can be considered to be two distinct
  149. groups of files, those which are independent of the QEMU emulation
  150. target and those which are dependent on the QEMU emulation target.
  151. In the target-independent set lives various general purpose helper code,
  152. such as error handling infrastructure, standard data structures,
  153. platform portability wrapper functions, etc. This code can be compiled
  154. once only and the .o files linked into all output binaries.
  155. In the target-dependent set lives CPU emulation, device emulation and
  156. much glue code. This sometimes also has to be compiled multiple times,
  157. once for each target being built.
  158. The utility code that is used by all binaries is built into a
  159. static archive called libqemuutil.a, which is then linked to all the
  160. binaries. In order to provide hooks that are only needed by some of the
  161. binaries, code in libqemuutil.a may depend on other functions that are
  162. not fully implemented by all QEMU binaries. Dummy stubs for all these
  163. functions are also provided by this library, and will only be linked
  164. into the binary if the real implementation is not present. In a way,
  165. the stubs can be thought of as a portable implementation of the weak
  166. symbols concept.
  167. All binaries should link to libqemuutil.a, e.g.:
  168. qemu-img$(EXESUF): qemu-img.o ..snip.. libqemuutil.a
  169. Windows platform portability
  170. ----------------------------
  171. On Windows, all binaries have the suffix '.exe', so all Makefile rules
  172. which create binaries must include the $(EXESUF) variable on the binary
  173. name. e.g.
  174. qemu-img$(EXESUF): qemu-img.o ..snip..
  175. This expands to '.exe' on Windows, or '' on other platforms.
  176. A further complication for the system emulator binaries is that
  177. two separate binaries need to be generated.
  178. The main binary (e.g. qemu-system-x86_64.exe) is linked against the
  179. Windows console runtime subsystem. These are expected to be run from a
  180. command prompt window, and so will print stderr to the console that
  181. launched them.
  182. The second binary generated has a 'w' on the end of its name (e.g.
  183. qemu-system-x86_64w.exe) and is linked against the Windows graphical
  184. runtime subsystem. These are expected to be run directly from the
  185. desktop and will open up a dedicated console window for stderr output.
  186. The Makefile.target will generate the binary for the graphical subsystem
  187. first, and then use objcopy to relink it against the console subsystem
  188. to generate the second binary.
  189. Object variable naming
  190. ----------------------
  191. The QEMU convention is to define variables to list different groups of
  192. object files. These are named with the convention $PREFIX-obj-y. For
  193. example the libqemuutil.a file will be linked with all objects listed
  194. in a variable 'util-obj-y'. So, for example, util/Makefile.obj will
  195. contain a set of definitions looking like
  196. util-obj-y += bitmap.o bitops.o hbitmap.o
  197. util-obj-y += fifo8.o
  198. util-obj-y += acl.o
  199. util-obj-y += error.o qemu-error.o
  200. When there is an object file which needs to be conditionally built based
  201. on some characteristic of the host system, the configure script will
  202. define a variable for the conditional. For example, on Windows it will
  203. define $(CONFIG_POSIX) with a value of 'n' and $(CONFIG_WIN32) with a
  204. value of 'y'. It is now possible to use the config variables when
  205. listing object files. For example,
  206. util-obj-$(CONFIG_WIN32) += oslib-win32.o qemu-thread-win32.o
  207. util-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o
  208. On Windows this expands to
  209. util-obj-y += oslib-win32.o qemu-thread-win32.o
  210. util-obj-n += oslib-posix.o qemu-thread-posix.o
  211. Since libqemutil.a links in $(util-obj-y), the POSIX specific files
  212. listed against $(util-obj-n) are ignored on the Windows platform builds.
  213. CFLAGS / LDFLAGS / LIBS handling
  214. --------------------------------
  215. There are many different binaries being built with differing purposes,
  216. and some of them might even be 3rd party libraries pulled in via git
  217. submodules. As such the use of the global CFLAGS variable is generally
  218. avoided in QEMU, since it would apply to too many build targets.
  219. Flags that are needed by any QEMU code (i.e. everything *except* GIT
  220. submodule projects) are put in $(QEMU_CFLAGS) variable. For linker
  221. flags the $(LIBS) variable is sometimes used, but a couple of more
  222. targeted variables are preferred. $(libs_softmmu) is used for
  223. libraries that must be linked to system emulator targets, $(LIBS_TOOLS)
  224. is used for tools like qemu-img, qemu-nbd, etc and $(LIBS_QGA) is used
  225. for the QEMU guest agent. There is currently no specific variable for
  226. the userspace emulator targets as the global $(LIBS), or more targeted
  227. variables shown below, are sufficient.
  228. In addition to these variables, it is possible to provide cflags and
  229. libs against individual source code files, by defining variables of the
  230. form $FILENAME-cflags and $FILENAME-libs. For example, the curl block
  231. driver needs to link to the libcurl library, so block/Makefile defines
  232. some variables:
  233. curl.o-cflags := $(CURL_CFLAGS)
  234. curl.o-libs := $(CURL_LIBS)
  235. The scope is a little different between the two variables. The libs get
  236. used when linking any target binary that includes the curl.o object
  237. file, while the cflags get used when compiling the curl.c file only.
  238. Statically defined files
  239. ------------------------
  240. The following key files are statically defined in the source tree, with
  241. the rules needed to build QEMU. Their behaviour is influenced by a
  242. number of dynamically created files listed later.
  243. - Makefile
  244. The main entry point used when invoking make to build all the components
  245. of QEMU. The default 'all' target will naturally result in the build of
  246. every component. The various tools and helper binaries are built
  247. directly via a non-recursive set of rules.
  248. Each system/userspace emulation target needs to have a slightly
  249. different set of make rules / variables. Thus, make will be recursively
  250. invoked for each of the emulation targets.
  251. The recursive invocation will end up processing the toplevel
  252. Makefile.target file (more on that later).
  253. - */Makefile.objs
  254. Since the source code is spread across multiple directories, the rules
  255. for each file are similarly modularized. Thus each subdirectory
  256. containing .c files will usually also contain a Makefile.objs file.
  257. These files are not directly invoked by a recursive make, but instead
  258. they are imported by the top level Makefile and/or Makefile.target
  259. Each Makefile.objs usually just declares a set of variables listing the
  260. .o files that need building from the source files in the directory. They
  261. will also define any custom linker or compiler flags. For example in
  262. block/Makefile.objs
  263. block-obj-$(CONFIG_LIBISCSI) += iscsi.o
  264. block-obj-$(CONFIG_CURL) += curl.o
  265. ..snip...
  266. iscsi.o-cflags := $(LIBISCSI_CFLAGS)
  267. iscsi.o-libs := $(LIBISCSI_LIBS)
  268. curl.o-cflags := $(CURL_CFLAGS)
  269. curl.o-libs := $(CURL_LIBS)
  270. If there are any rules defined in the Makefile.objs file, they should
  271. all use $(obj) as a prefix to the target, e.g.
  272. $(obj)/generated-tcg-tracers.h: $(obj)/generated-tcg-tracers.h-timestamp
  273. - Makefile.target
  274. This file provides the entry point used to build each individual system
  275. or userspace emulator target. Each enabled target has its own
  276. subdirectory. For example if configure is run with the argument
  277. '--target-list=x86_64-softmmu', then a sub-directory 'x86_64-softmmu'
  278. will be created, containing a 'Makefile' which symlinks back to
  279. Makefile.target
  280. So when the recursive '$(MAKE) -C x86_64-softmmu' is invoked, it ends up
  281. using Makefile.target for the build rules.
  282. - rules.mak
  283. This file provides the generic helper rules for invoking build tools, in
  284. particular the compiler and linker. This also contains the magic (hairy)
  285. 'unnest-vars' function which is used to merge the variable definitions
  286. from all Makefile.objs in the source tree down into the main Makefile
  287. context.
  288. - default-configs/*.mak
  289. The files under default-configs/ control what emulated hardware is built
  290. into each QEMU system and userspace emulator targets. They merely contain
  291. a list of config variable definitions like the machines that should be
  292. included. For example, default-configs/aarch64-softmmu.mak has:
  293. include arm-softmmu.mak
  294. CONFIG_XLNX_ZYNQMP_ARM=y
  295. CONFIG_XLNX_VERSAL=y
  296. These files rarely need changing unless new devices / hardware need to
  297. be enabled for a particular system/userspace emulation target
  298. - tests/Makefile
  299. Rules for building the unit tests. This file is included directly by the
  300. top level Makefile, so anything defined in this file will influence the
  301. entire build system. Care needs to be taken when writing rules for tests
  302. to ensure they only apply to the unit test execution / build.
  303. - tests/docker/Makefile.include
  304. Rules for Docker tests. Like tests/Makefile, this file is included
  305. directly by the top level Makefile, anything defined in this file will
  306. influence the entire build system.
  307. - po/Makefile
  308. Rules for building and installing the binary message catalogs from the
  309. text .po file sources. This almost never needs changing for any reason.
  310. Dynamically created files
  311. -------------------------
  312. The following files are generated dynamically by configure in order to
  313. control the behaviour of the statically defined makefiles. This avoids
  314. the need for QEMU makefiles to go through any pre-processing as seen
  315. with autotools, where Makefile.am generates Makefile.in which generates
  316. Makefile.
  317. - config-host.mak
  318. When configure has determined the characteristics of the build host it
  319. will write a long list of variables to config-host.mak file. This
  320. provides the various install directories, compiler / linker flags and a
  321. variety of CONFIG_* variables related to optionally enabled features.
  322. This is imported by the top level Makefile in order to tailor the build
  323. output.
  324. The variables defined here are those which are applicable to all QEMU
  325. build outputs. Variables which are potentially different for each
  326. emulator target are defined by the next file...
  327. It is also used as a dependency checking mechanism. If make sees that
  328. the modification timestamp on configure is newer than that on
  329. config-host.mak, then configure will be re-run.
  330. - config-host.h
  331. The config-host.h file is used by source code to determine what features
  332. are enabled. It is generated from the contents of config-host.mak using
  333. the scripts/create_config program. This extracts all the CONFIG_* variables,
  334. most of the HOST_* variables and a few other misc variables from
  335. config-host.mak, formatting them as C preprocessor macros.
  336. - $TARGET-NAME/config-target.mak
  337. TARGET-NAME is the name of a system or userspace emulator, for example,
  338. x86_64-softmmu denotes the system emulator for the x86_64 architecture.
  339. This file contains the variables which need to vary on a per-target
  340. basis. For example, it will indicate whether KVM or Xen are enabled for
  341. the target and any other potential custom libraries needed for linking
  342. the target.
  343. - $TARGET-NAME/config-devices.mak
  344. TARGET-NAME is again the name of a system or userspace emulator. The
  345. config-devices.mak file is automatically generated by make using the
  346. scripts/make_device_config.sh program, feeding it the
  347. default-configs/$TARGET-NAME file as input.
  348. - $TARGET-NAME/Makefile
  349. This is the entrypoint used when make recurses to build a single system
  350. or userspace emulator target. It is merely a symlink back to the
  351. Makefile.target in the top level.
  352. Useful make targets
  353. ===================
  354. - help
  355. Print a help message for the most common build targets.
  356. - print-VAR
  357. Print the value of the variable VAR. Useful for debugging the build
  358. system.