build-overview.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. .. _build_overview:
  2. =====================
  3. Build System Overview
  4. =====================
  5. This document provides an overview on how the build system works. It is
  6. targeted at people wanting to learn about internals of the build system.
  7. It is not meant for persons who casually interact with the build system.
  8. That being said, knowledge empowers, so consider reading on.
  9. The build system is composed of many different components working in
  10. harmony to build the source tree. We begin with a graphic overview.
  11. .. graphviz::
  12. digraph build_components {
  13. rankdir="LR";
  14. "configure" -> "config.status" -> "build backend" -> "build output"
  15. }
  16. Phase 1: Configuration
  17. ======================
  18. Phase 1 centers around the ``configure`` script, which is a bash shell script.
  19. The file is generated from a file called ``configure.in`` which is written in M4
  20. and processed using Autoconf 2.13 to create the final configure script.
  21. You don't have to worry about how you obtain a ``configure`` file: the build
  22. system does this for you.
  23. The primary job of ``configure`` is to determine characteristics of the system
  24. and compiler, apply options passed into it, and validate everything looks OK to
  25. build. The primary output of the ``configure`` script is an executable file
  26. in the object directory called ``config.status``. ``configure`` also produces
  27. some additional files (like ``autoconf.mk``). However, the most important file
  28. in terms of architecture is ``config.status``.
  29. The existence of a ``config.status`` file may be familiar to those who have worked
  30. with Autoconf before. However, Mozilla's ``config.status`` is different from almost
  31. any other ``config.status`` you've ever seen: it's written in Python! Instead of
  32. having our ``configure`` script produce a shell script, we have it generating
  33. Python.
  34. Now is as good a time as any to mention that Python is prevalent in our build
  35. system. If we need to write code for the build system, we do it in Python.
  36. That's just how we roll. For more, see :ref:`python`.
  37. ``config.status`` contains 2 parts: data structures representing the output of
  38. ``configure`` and a command-line interface for preparing/configuring/generating
  39. an appropriate build backend. (A build backend is merely a tool used to build
  40. the tree - like GNU Make or Tup). These data structures essentially describe
  41. the current state of the system and what the existing build configuration looks
  42. like. For example, it defines which compiler to use, how to invoke it, which
  43. application features are enabled, etc. You are encouraged to open up
  44. ``config.status`` to have a look for yourself!
  45. Once we have emitted a ``config.status`` file, we pass into the realm of
  46. phase 2.
  47. Phase 2: Build Backend Preparation and the Build Definition
  48. ===========================================================
  49. Once ``configure`` has determined what the current build configuration is,
  50. we need to apply this to the source tree so we can actually build.
  51. What essentially happens is the automatically-produced ``config.status`` Python
  52. script is executed as soon as ``configure`` has generated it. ``config.status``
  53. is charged with the task of tell a tool how to build the tree. To do this,
  54. ``config.status`` must first scan the build system definition.
  55. The build system definition consists of various ``moz.build`` files in the tree.
  56. There is roughly one ``moz.build`` file per directory or per set of related directories.
  57. Each ``moz.build`` files defines how its part of the build config works. For
  58. example it says *I want these C++ files compiled* or *look for additional
  59. information in these directories.* config.status starts with the ``moz.build``
  60. file from the root directory and then descends into referenced ``moz.build``
  61. files by following ``DIRS`` variables or similar.
  62. As the ``moz.build`` files are read, data structures describing the overall
  63. build system definition are emitted. These data structures are then fed into a
  64. build backend, which then performs actions, such as writing out files to
  65. be read by a build tool. e.g. a ``make`` backend will write a
  66. ``Makefile``.
  67. When ``config.status`` runs, you'll see the following output::
  68. Reticulating splines...
  69. Finished reading 1096 moz.build files into 1276 descriptors in 2.40s
  70. Backend executed in 2.39s
  71. 2188 total backend files. 0 created; 1 updated; 2187 unchanged
  72. Total wall time: 5.03s; CPU time: 3.79s; Efficiency: 75%
  73. What this is saying is that a total of *1096* ``moz.build`` files were read.
  74. Altogether, *1276* data structures describing the build configuration were
  75. derived from them. It took *2.40s* wall time to just read these files and
  76. produce the data structures. The *1276* data structures were fed into the
  77. build backend which then determined it had to manage *2188* files derived
  78. from those data structures. Most of them already existed and didn't need
  79. changed. However, *1* was updated as a result of the new configuration.
  80. The whole process took *5.03s*. Although, only *3.79s* was in
  81. CPU time. That likely means we spent roughly *25%* of the time waiting on
  82. I/O.
  83. For more on how ``moz.build`` files work, see :ref:`mozbuild-files`.
  84. Phase 3: Invokation of the Build Backend
  85. ========================================
  86. When most people think of the build system, they think of phase 3. This is
  87. where we take all the code in the tree and produce Firefox or whatever
  88. application you are creating. Phase 3 effectively takes whatever was
  89. generated by phase 2 and runs it. Since the dawn of Mozilla, this has been
  90. make consuming Makefiles. However, with the transition to moz.build files,
  91. you may soon see non-Make build backends, such as Tup or Visual Studio.
  92. When building the tree, most of the time is spent in phase 3. This is when
  93. header files are installed, C++ files are compiled, files are preprocessed, etc.