defining-binaries.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. .. _defining_binaries:
  2. ======================================
  3. Defining Binaries for the Build System
  4. ======================================
  5. One part of what the build system does is compile C/C++ and link the resulting
  6. objects to produce executables and/or libraries. This document describes the
  7. basics of defining what is going to be built and how. All the following
  8. describes constructs to use in moz.build files.
  9. Source files
  10. ============
  11. Source files to be used in a given directory are registered in the ``SOURCES``
  12. and ``UNIFIED_SOURCES`` variables. ``UNIFIED_SOURCES`` have a special behavior
  13. in that they are aggregated by batches of 16, requiring, for example, that there
  14. are no conflicting variables in those source files.
  15. ``SOURCES`` and ``UNIFIED_SOURCES`` are lists which must be appended to, and
  16. each append requires the given list to be alphanumerically ordered.
  17. .. code-block:: python
  18. UNIFIED_SOURCES += [
  19. 'FirstSource.cpp',
  20. 'SecondSource.cpp',
  21. 'ThirdSource.cpp',
  22. ]
  23. SOURCES += [
  24. 'OtherSource.cpp',
  25. ]
  26. ``SOURCES`` and ``UNIFIED_SOURCES`` can contain a mix of different file types,
  27. for C, C++, and Objective C.
  28. Static Libraries
  29. ================
  30. To build a static library, other than defining the source files (see above), one
  31. just needs to define a library name with the ``Library`` template.
  32. .. code-block:: python
  33. Library('foo')
  34. The library file name will be ``libfoo.a`` on UNIX systems and ``foo.lib`` on
  35. Windows.
  36. If the static library needs to aggregate other static libraries, a list of
  37. ``Library`` names can be added to the ``USE_LIBS`` variable. Like ``SOURCES``, it
  38. requires the appended list to be alphanumerically ordered.
  39. .. code-block:: python
  40. USE_LIBS += ['bar', 'baz']
  41. If there are multiple directories containing the same ``Library`` name, it is
  42. possible to disambiguate by prefixing with the path to the wanted one (relative
  43. or absolute):
  44. .. code-block:: python
  45. USE_LIBS += [
  46. '/path/from/topsrcdir/to/bar',
  47. '../relative/baz',
  48. ]
  49. Note that the leaf name in those paths is the ``Library`` name, not an actual
  50. file name.
  51. Note that currently, the build system may not create an actual library for
  52. static libraries. It is an implementation detail that shouldn't need to be
  53. worried about.
  54. As a special rule, ``USE_LIBS`` is allowed to contain references to shared
  55. libraries. In such cases, programs and shared libraries linking this static
  56. library will inherit those shared library dependencies.
  57. Intermediate (Static) Libraries
  58. ===============================
  59. In many cases in the tree, static libraries are built with the only purpose
  60. of being linked into another, bigger one (like libxul). Instead of adding all
  61. required libraries to ``USE_LIBS`` for the bigger one, it is possible to tell
  62. the build system that the library built in the current directory is meant to
  63. be linked to that bigger library, with the ``FINAL_LIBRARY`` variable.
  64. .. code-block:: python
  65. FINAL_LIBRARY = 'xul'
  66. The ``FINAL_LIBRARY`` value must match a unique ``Library`` name somewhere
  67. in the tree.
  68. As a special rule, those intermediate libraries don't need a ``Library`` name
  69. for themselves.
  70. Shared Libraries
  71. ================
  72. Sometimes, we want shared libraries, a.k.a. dynamic libraries. Such libraries
  73. are defined similarly to static libraries, using the ``SharedLibrary`` template
  74. instead of ``Library``.
  75. .. code-block:: python
  76. SharedLibrary('foo')
  77. When this template is used, no static library is built. See further below to
  78. build both types of libraries.
  79. With a ``SharedLibrary`` name of ``foo``, the library file name will be
  80. ``libfoo.dylib`` on OSX, ``libfoo.so`` on ELF systems (Linux, etc.), and
  81. ``foo.dll`` on Windows. On Windows, there is also an import library named
  82. ``foo.lib``, used on the linker command line. ``libfoo.dylib`` and
  83. ``libfoo.so`` are considered the import library name for, resp. OSX and ELF
  84. systems.
  85. On OSX, one may want to create a special kind of dynamic library: frameworks.
  86. This is done with the ``Framework`` template.
  87. .. code-block:: python
  88. Framework('foo')
  89. With a ``Framework`` name of ``foo``, the framework file name will be ``foo``.
  90. This template however affects the behavior on all platforms, so it needs to
  91. be set only on OSX.
  92. Executables
  93. ===========
  94. Executables, a.k.a. programs, are, in the simplest form, defined with the
  95. ``Program`` template.
  96. .. code-block:: python
  97. Program('foobar')
  98. On UNIX systems, the executable file name will be ``foobar``, while on Windows,
  99. it will be ``foobar.exe``.
  100. Like static and shared libraries, the build system can be instructed to link
  101. libraries to the executable with ``USE_LIBS``, listing various ``Library``
  102. names.
  103. In some cases, we want to create an executable per source file in the current
  104. directory, in which case we can use the ``SimplePrograms`` template
  105. .. code-block:: python
  106. SimplePrograms([
  107. 'FirstProgram',
  108. 'SecondProgram',
  109. ])
  110. Contrary to ``Program``, which requires corresponding ``SOURCES``, when using
  111. ``SimplePrograms``, the corresponding ``SOURCES`` are implied. If the
  112. corresponding ``sources`` have an extension different from ``.cpp``, it is
  113. possible to specify the proper extension:
  114. .. code-block:: python
  115. SimplePrograms([
  116. 'ThirdProgram',
  117. 'FourthProgram',
  118. ], ext='.c')
  119. Please note this construct was added for compatibility with what already lives
  120. in the mozilla tree ; it is recommended not to add new simple programs with
  121. sources with a different extension than ``.cpp``.
  122. Similar to ``SimplePrograms``, is the ``CppUnitTests`` template, which defines,
  123. with the same rules, C++ unit tests programs. Like ``SimplePrograms``, it takes
  124. an ``ext`` argument to specify the extension for the corresponding ``SOURCES``,
  125. if it's different from ``.cpp``.
  126. Linking with system libraries
  127. =============================
  128. Programs and libraries usually need to link with system libraries, such as a
  129. widget toolkit, etc. Those required dependencies can be given with the
  130. ``OS_LIBS`` variable.
  131. .. code-block:: python
  132. OS_LIBS += [
  133. 'foo',
  134. 'bar',
  135. ]
  136. This expands to ``foo.lib bar.lib`` when building with MSVC, and
  137. ``-lfoo -lbar`` otherwise.
  138. For convenience with ``pkg-config``, ``OS_LIBS`` can also take linker flags
  139. such as ``-L/some/path`` and ``-llib``, such that it is possible to directly
  140. assign ``LIBS`` variables from ``CONFIG``, such as:
  141. .. code-block:: python
  142. OS_LIBS += CONFIG['MOZ_PANGO_LIBS']
  143. (assuming ``CONFIG['MOZ_PANGO_LIBS']`` is a list, not a string)
  144. Like ``USE_LIBS``, this variable applies to static and shared libraries, as
  145. well as programs.
  146. Libraries from third party build system
  147. =======================================
  148. Some libraries in the tree are not built by the moz.build-governed build
  149. system, and there is no ``Library`` corresponding to them.
  150. However, ``USE_LIBS`` allows to reference such libraries by giving a full
  151. path (like when disambiguating identical ``Library`` names). The same naming
  152. rules apply as other uses of ``USE_LIBS``, so only the library name without
  153. prefix and suffix shall be given.
  154. .. code-block:: python
  155. USE_LIBS += [
  156. '/path/from/topsrcdir/to/third-party/bar',
  157. '../relative/third-party/baz',
  158. ]
  159. Note that ``/path/from/topsrcdir/to/third-party`` and
  160. ``../relative/third-party/baz`` must lead under a subconfigured directory (a
  161. directory with an AC_OUTPUT_SUBDIRS in configure.in), or ``security/nss``.
  162. Building both static and shared libraries
  163. =========================================
  164. When both types of libraries are required, one needs to set both
  165. ``FORCE_SHARED_LIB`` and ``FORCE_STATIC_LIB`` boolean variables.
  166. .. code-block:: python
  167. FORCE_SHARED_LIB = True
  168. FORCE_STATIC_LIB = True
  169. But because static libraries and Windows import libraries have the same file
  170. names, either the static or the shared library name needs to be different
  171. than the name given to the ``Library`` template.
  172. The ``STATIC_LIBRARY_NAME`` and ``SHARED_LIBRARY_NAME`` variables can be used
  173. to change either the static or the shared library name.
  174. .. code-block:: python
  175. Library('foo')
  176. STATIC_LIBRARY_NAME = 'foo_s'
  177. With the above, on Windows, ``foo_s.lib`` will be the static library,
  178. ``foo.dll`` the shared library, and ``foo.lib`` the import library.
  179. In some cases, for convenience, it is possible to set both
  180. ``STATIC_LIBRARY_NAME`` and ``SHARED_LIBRARY_NAME``. For example:
  181. .. code-block:: python
  182. Library('mylib')
  183. STATIC_LIBRARY_NAME = 'mylib_s'
  184. SHARED_LIBRARY_NAME = CONFIG['SHARED_NAME']
  185. This allows to use ``mylib`` in the ``USE_LIBS`` of another library or
  186. executable.
  187. When refering to a ``Library`` name building both types of libraries in
  188. ``USE_LIBS``, the shared library is chosen to be linked. But sometimes,
  189. it is wanted to link the static version, in which case the ``Library`` name
  190. needs to be prefixed with ``static:`` in ``USE_LIBS``
  191. ::
  192. a/moz.build:
  193. Library('mylib')
  194. FORCE_SHARED_LIB = True
  195. FORCE_STATIC_LIB = True
  196. STATIC_LIBRARY_NAME = 'mylib_s'
  197. b/moz.build:
  198. Program('myprog')
  199. USE_LIBS += [
  200. 'static:mylib',
  201. ]
  202. Miscellaneous
  203. =============
  204. The ``SDK_LIBRARY`` boolean variable defines whether the library in the current
  205. directory is going to be installed in the SDK.
  206. The ``SONAME`` variable declares a "shared object name" for the library. It
  207. defaults to the ``Library`` name or the ``SHARED_LIBRARY_NAME`` if set. When
  208. linking to a library with a ``SONAME``, the resulting library or program will
  209. have a dependency on the library with the name corresponding to the ``SONAME``
  210. instead of the ``Library`` name. This only impacts ELF systems.
  211. ::
  212. a/moz.build:
  213. Library('mylib')
  214. b/moz.build:
  215. Library('otherlib')
  216. SONAME = 'foo'
  217. c/moz.build:
  218. Program('myprog')
  219. USE_LIBS += [
  220. 'mylib',
  221. 'otherlib',
  222. ]
  223. On e.g. Linux, the above ``myprog`` will have DT_NEEDED markers for
  224. ``libmylib.so`` and ``libfoo.so`` instead of ``libmylib.so`` and
  225. ``libotherlib.so`` if there weren't a ``SONAME``. This means the runtime
  226. requirement for ``myprog`` is ``libfoo.so`` instead of ``libotherlib.so``.
  227. Gecko-related binaries
  228. ======================
  229. Some programs or libraries are totally independent of Gecko, and can use the
  230. above mentioned templates. Others are Gecko-related in some way, and may
  231. need XPCOM linkage, mozglue. These things are tedious. A set of additional
  232. templates exists to ease defining such programs and libraries. They are
  233. essentially the same as the above mentioned templates, prefixed with "Gecko":
  234. - ``GeckoProgram``
  235. - ``GeckoSimplePrograms``
  236. - ``GeckoCppUnitTests``
  237. - ``GeckoSharedLibrary``
  238. - ``GeckoFramework``
  239. There is also ``XPCOMBinaryComponent`` for XPCOM components, which is a
  240. special kind of library.
  241. All the Gecko-prefixed templates take the same arguments as their
  242. non-Gecko-prefixed counterparts, and can take a few more arguments
  243. for non-standard cases. See the definition of ``GeckoBinary`` in
  244. build/gecko_templates.mozbuild for more details, but most usecases
  245. should not require these additional arguments.