Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

477 rindas
14KB

  1. .. highlight:: js
  2. Toolchains
  3. ##########
  4. One of the core components of ``dds`` is that of the *toolchain*. A toolchain
  5. encompasses the environment used to build and link source code, including, but
  6. not limited to:
  7. #. The executable binaries that constitute the language implementation:
  8. Compilers, linkers, and archive managers.
  9. #. The configuration of those tools, including most options given to those
  10. tools when they are invoked.
  11. #. The set of preprocessor macros and language features that are active during
  12. compilation.
  13. When a build is run, every file in the entire tree (including dependencies)
  14. will be compiled, archived, and linked using the same toolchain.
  15. This page provides an introduction on how one can make use of toolchains most
  16. effectively in your project.
  17. .. note::
  18. **IMPORTANT**: ``dds`` will *not* automatically load the Visual C++
  19. environment. To use Visual C++, ``dds`` must be executed from the
  20. appropriate environment in order for the Visual C++ toolchain executables
  21. and files to be available.
  22. Passing a Toolchain
  23. *******************
  24. In ``dds``, the default format of a toolchain is that of a single JSON5 file
  25. that describes the entire toolchain. When running a build for a project, the
  26. ``dds`` executable will look in a few locations for a default toolchain, and
  27. generate an error if no default toolchain file is found (Refer to
  28. :ref:`toolchains.default`). A different toolchain can be provided by passing
  29. the toolchain file for the ``--toolchain`` (or ``-t``) option on the command
  30. line::
  31. $ dds build -t my-toolchain.json5
  32. Alternatively, you can pass the name of a built-in toolchain. See below.
  33. .. _toolchains.builtin:
  34. Built-in Toolchains
  35. *******************
  36. For convenience, ``dds`` includes several built-in toolchains that can be
  37. accessed in the ``--toolchain`` command-line option using a colon ``:``
  38. prefix::
  39. $ dds build -t :gcc
  40. ``dds`` will treat the leading colon (``:``) as a name for a built-in
  41. toolchain (this means that a toolchain's filepath may not begin with a colon).
  42. There are several built-in toolchains that may be specified:
  43. ``:gcc``
  44. Uses the default ``gcc`` and ``g++`` executables, linkers, and options
  45. thereof.
  46. ``:gcc-N`` (for some integer ``N``)
  47. Equivalent to ``:gcc``, but uses the ``gcc-N`` and ``g++-N`` executables.
  48. ``:clang``
  49. Equivalent to ``:gcc``, but uses the ``clang`` and ``clang++`` executables.
  50. ``:clang-N`` (for some integer ``N``)
  51. Equivalent to ``:clang``, but uses the ``clang-N`` and ``clang++-N``
  52. executables.
  53. ``:msvc``
  54. Compiles and links using the Visual C++ toolchain.
  55. The following pseudo-toolchains are also available:
  56. ``:debug:XYZ``
  57. Uses built-in toolchain ``:XYZ``, but generates debugging information.
  58. ``:ccache:XYZ``
  59. Uses built-in toolchain ``:XYZ``, but prefixes all compile commands with
  60. ``ccache``.
  61. ``:c++UV:XYZ`` (for two integers ``UV``)
  62. Sets the C++ version to ``C++UV`` and uses the ``:XYZ`` toolchain.
  63. .. _toolchains.default:
  64. Providing a Default Toolchain File
  65. **********************************
  66. If you do not which to provide a new toolchain for every individual project,
  67. and the built-in toolchains do not suit your needs, you can write a toolchain
  68. file to one of a few predefined paths, and ``dds`` will find and use it for the
  69. build. The following directories are searched, in order:
  70. #. ``$pwd/`` - If the working directory contains a toolchain file, it will be
  71. used as the default.
  72. #. ``$dds_config_dir/`` - Searches for a toolchain file in ``dds``'s user-local
  73. configuration directory (see below).
  74. #. ``$user_home/`` - Searches for a toolchain file at the root of the current
  75. user's home directory. (``$HOME`` on Unix-like systems, and ``$PROFILE`` on
  76. Windows.)
  77. In each directory, it will search for ``toolchain.json5``, ``toolchain.jsonc``,
  78. or ``toolchain.json``.
  79. The ``$dds_user_config`` directory is the ``dds`` subdirectory of the
  80. user-local configuration directory.
  81. The user-local config directory is ``$XDG_CONFIG_DIR`` or ``~/.config`` on
  82. Linux, ``~/Library/Preferences`` on macOS, and ``~/AppData/Roaming`` on
  83. Windows.
  84. Toolchain Definitions
  85. *********************
  86. Besides using the built-in toolchains, it is likely that you'll soon want to
  87. customize a toolchain further. Further customization must be done with a
  88. file that contains the toolchain definition. The most basic toolchain file is
  89. simply one line:
  90. .. code-block::
  91. {
  92. compiler_id: "<compiler-id>"
  93. }
  94. where ``<compiler-id>`` is one of the known ``compiler_id`` options (See the
  95. toolchain option reference). ``dds`` will infer common suitable defaults for
  96. the remaining options based on the value of ``compiler_id``.
  97. For example, if you specify ``gnu``, then ``dds`` will assume ``gcc`` to be the
  98. C compiler, ``g++`` to be the C++ compiler, and ``ar`` to be the library
  99. archiving tool.
  100. If you know that your compiler executable has a different name, you can
  101. specify them with additional options:
  102. .. code-block::
  103. {
  104. compiler_id: 'gnu',
  105. c_compiler: 'gcc-9',
  106. cxx_compiler: 'g++-9',
  107. }
  108. ``dds`` will continue to infer other options based on the ``compiler_id``, but
  109. will use the provided executable names when compiling files for the respective
  110. languages.
  111. To specify compilation flags, the ``flags`` option can be used:
  112. .. code-block::
  113. {
  114. // [...]
  115. flags: '-fsanitize=address -fno-inline',
  116. }
  117. .. note::
  118. Use ``warning_flags`` to specify options regarding compiler warnings.
  119. Flags for linking executables can be specified with ``link_flags``:
  120. .. code-block::
  121. {
  122. // [...]
  123. link_flags: '-fsanitize=address -fPIE'
  124. }
  125. .. _toolchains.opt-ref:
  126. Toolchain Option Reference
  127. **************************
  128. Understanding Flags and Shell Parsing
  129. -------------------------------------
  130. Many of the ``dds`` toolchain parameters accept argument lists or shell-string
  131. lists. If such an option is given a single string, then that string is split
  132. using the syntax of a POSIX shell command parser. It accepts both single ``'``
  133. and double ``"`` quote characters as argument delimiters.
  134. If an option is given a list of strings instead, then each string in that
  135. array is treated as a full command line argument and is passed as such.
  136. For example, this sample with ``flags``::
  137. {
  138. flags: "-fsanitize=address -fPIC"
  139. }
  140. is equivalent to this one::
  141. {
  142. flags: ["-fsanitize=address", "-fPIC"]
  143. }
  144. Despite splitting strings as-if they were shell commands, ``dds`` does nothing
  145. else shell-like. It does not expand environment variables, nor does it expand
  146. globs.
  147. ``compiler_id``
  148. ---------------
  149. Specify the identity of the compiler. This option is used to infer many other
  150. facts about the toolchain. If specifying the full toolchain with the command
  151. templates, this option is not required.
  152. Valid values are:
  153. ``gnu``
  154. For GCC
  155. ``clang``
  156. For LLVM/Clang
  157. ``msvc``
  158. For Microsoft Visual C++
  159. ``c_compiler`` and ``cxx_compiler``
  160. -----------------------------------
  161. Names/paths of the C and C++ compilers, respectively. Defaults will be inferred
  162. from ``compiler_id``.
  163. ``c_version`` and ``cxx_version``
  164. ---------------------------------
  165. Specify the language versions for C and C++, respectively. By default, ``dds``
  166. will not set any language version. Using this option requires that the
  167. ``compiler_id`` be specified. Setting this value will cause the corresponding
  168. language-version flag to be passed to the compiler.
  169. Valid ``c_version`` values are:
  170. - ``c89``
  171. - ``c99``
  172. - ``c11``
  173. - ``c18``
  174. Valid ``cxx_version`` values are:
  175. - ``c++98``
  176. - ``c++03``
  177. - ``c++11``
  178. - ``c++14``
  179. - ``c++17``
  180. - ``c++20``
  181. .. warning::
  182. ``dds`` will not do any "smarts" to infer the exact option to pass to have
  183. the required effect. If you ask for ``c++20`` from ``gcc 4.8``, ``dds``
  184. will simply pass ``-std=c++20`` with no questions asked. If you need
  185. finer-grained control, use the ``c_flags`` and ``cxx_flags`` options.
  186. ``warning_flags``
  187. -----------------
  188. Override the compiler flags that should be used to enable warnings. This option
  189. is stored separately from ``flags``, as these options may be enabled/disabled
  190. separately depending on how ``dds`` is invoked.
  191. .. note::
  192. If ``compiler_id`` is provided, a default value will be used that enables
  193. common warning levels.
  194. If you need to tweak warnings further, use this option.
  195. On GNU-like compilers, the default flags are ``-Wall -Wextra -Wpedantic
  196. -Wconversion``. On MSVC the default flag is ``/W4``.
  197. ``flags``, ``c_flags``, and ``cxx_flags``
  198. -----------------------------------------
  199. Specify *additional* compiler options, possibly per-language.
  200. ``link_flags``
  201. --------------
  202. Specify *additional* link options to use when linking executables.
  203. ``optimize``
  204. ------------
  205. Boolean option (``true`` or ``false``) to enable/disable optimizations. Default
  206. is ``false``.
  207. ``debug``
  208. ---------
  209. Boolean option (``true`` or ``false``) to enable/disable the generation of
  210. debugging information. Default is ``false``.
  211. ``compiler_launcher``
  212. ---------------------
  213. Provide a command prefix that should be used on all compiler executions.
  214. e.g. ``ccache``.
  215. ``advanced``
  216. ------------
  217. A nested object that contains advanced toolchain options. Refer to section on
  218. advanced toolchain options.
  219. Advanced Options Reference
  220. **************************
  221. The options below are probably not good to tweak unless you *really* know what
  222. you are doing. Their values will be inferred from ``compiler_id``.
  223. Command Templates
  224. -----------------
  225. Many of the below options take the form of command-line templates. These are
  226. templates from which ``dds`` will create a command-line for a subprocess,
  227. possibly by combining them together.
  228. Each command template allows some set of placeholders. Each instance of the
  229. placeholder string will be replaced in the final command line. Refer to each
  230. respective option for more information.
  231. ``deps_mode``
  232. -------------
  233. Specify the way in which ``dds`` should track compilation dependencies. One
  234. of ``gnu``, ``msvc``, or ``none``.
  235. .. note::
  236. If ``none``, then dependency tracking will be disabled entirely. This will
  237. prevent ``dds`` from tracking interdependencies of source files, and
  238. inhibits incremental compilation.
  239. ``c_compile_file`` and ``cxx_compile_file``
  240. -------------------------------------------
  241. Override the *command template* that is used to compile source files.
  242. This template expects three placeholders:
  243. - ``[in]`` is the path to the file that will be compiled.
  244. - ``[out]`` is the path to the object file that will be generated.
  245. - ``[flags]`` is the placeholder of the compilation flags. This placeholder
  246. must not be attached to any other arguments. The compilation flag argument
  247. list will be inserted in place of ``[flags]``.
  248. Defaults::
  249. {
  250. // On GNU-like compilers (GCC, Clang):
  251. c_compile_file: "<compiler> -fPIC -pthread [flags] -c [in] -o[out]",
  252. cxx_compile_file: "<compiler> -fPIC -pthread [flags] -c [in] -o[out]",
  253. // When `optimize` is enabled, `-O2` is added as a flag
  254. // When `debug` is enabled, `-g` is added as a flag
  255. // On MSVC:
  256. c_compile_file: "cl.exe /MT /nologo /permissive- [flags] /c [in] /Fo[out]",
  257. cxx_compile_file: "cl.exe /MT /EHsc /nologo /permissive- [flags] /c [in] /Fo[out]",
  258. // When `optimize` is enabled, `/O2` is added as a flag
  259. // When `debug` is enabled, `/Z7` and `/DEBUG` are added, and `/MT` becomes `/MTd`
  260. }
  261. ``create_archive``
  262. ------------------
  263. Override the *command template* that is used to generate static library archive
  264. files.
  265. This template expects three placeholders:
  266. - ``[in]`` is the a placeholder for the list of inputs. It must not be attached
  267. to any other arguments. The list of input paths will be inserted in place of
  268. ``[in]``.
  269. - ``[out]`` is the placeholder for the output path for the static library
  270. archive.
  271. Defaults::
  272. {
  273. // On GNU-like:
  274. create_archive: "ar rcs [out] [in]",
  275. // On MSVC:
  276. create_archive: "lib /nologo /OUT:[out] [in]",
  277. }
  278. ``link_executable``
  279. -------------------
  280. Override the *command template* that is used to link executables.
  281. This template expects the same placeholders as ``create_archive``, but
  282. ``[out]`` is a placeholder for the executable file rather than a static
  283. library.
  284. Defaults::
  285. {
  286. // For GNU-like:
  287. link_executable: "<compiler> -fPIC [in] -pthread -o[out] [flags]",
  288. // For MSVC:
  289. link_executable: "cl.exe /nologo /EHsc [in] /Fe[out]",
  290. }
  291. ``include_template`` and ``external_include_template``
  292. ------------------------------------------------------
  293. Override the *command template* for the flags to specify a header search path.
  294. ``external_include_template`` will be used to specify the include search path
  295. for a directory that is "external" (i.e. does not live within the main project).
  296. For each directory added to the ``#include`` search path, this argument
  297. template is instantiated in the ``[flags]`` for the compilation.
  298. This template expects only a single placeholder: ``[path]``, which will be
  299. replaced with the path to the directory to be added to the search path.
  300. On MSVC, this defaults to ``/I [path]``. On GNU-like, ``-isystem [path]`` is
  301. used for ``external_include_template`` and ``-I [path]`` for
  302. ``include_template``.
  303. ``define_template``
  304. -------------------
  305. Override the *command template* for the flags to set a preprocessor definition.
  306. This template expects only a single placeholder: ``[def]``, which is the
  307. preprocessor macro definition argument.
  308. On MSVC, this defaults to ``/D [def]``. On GNU-like compilers, this is
  309. ``-D [def]``.
  310. ``tty_flags``
  311. -------------
  312. Supply additional flags when compiling/linking that will only be applied if
  313. standard output is an ANSI-capable terminal.
  314. On GNU and Clang this will be ``-fdiagnostics-color`` by default.
  315. ``obj_prefix``, ``obj_suffix``, ``archive_prefix``, ``archive_suffix``,
  316. ``exe_prefix``, and ``exe_suffix``
  317. ----------------------------------
  318. Set the filename prefixes and suffixes for object files, library archive files,
  319. and executable files, respectively.