Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

492 lines
15KB

  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 wish 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 and wildcards.
  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. Provide *additional* compiler flags that should be used to enable warnings. This option is stored separately from ``flags``, as these options may be enabled/disabled separately depending on how ``dds`` is invoked.
  189. .. note::
  190. If ``compiler_id`` is provided, a default set of warning flags will be provided when warnings are enabled.
  191. Adding flags to this toolchain option will *append* flags to the basis warning flag list rather than overwrite them.
  192. .. seealso::
  193. Refer to :ref:`toolchains.opts.base_warning_flags` for more information.
  194. ``flags``, ``c_flags``, and ``cxx_flags``
  195. -----------------------------------------
  196. Specify *additional* compiler options, possibly per-language.
  197. ``link_flags``
  198. --------------
  199. Specify *additional* link options to use when linking executables.
  200. ``optimize``
  201. ------------
  202. Boolean option (``true`` or ``false``) to enable/disable optimizations. Default
  203. is ``false``.
  204. ``debug``
  205. ---------
  206. Boolean option (``true`` or ``false``) to enable/disable the generation of
  207. debugging information. Default is ``false``.
  208. ``compiler_launcher``
  209. ---------------------
  210. Provide a command prefix that should be used on all compiler executions.
  211. e.g. ``ccache``.
  212. ``advanced``
  213. ------------
  214. A nested object that contains advanced toolchain options. Refer to section on
  215. advanced toolchain options.
  216. Advanced Options Reference
  217. **************************
  218. The options below are probably not good to tweak unless you *really* know what
  219. you are doing. Their values will be inferred from ``compiler_id``.
  220. Command Templates
  221. -----------------
  222. Many of the below options take the form of command-line templates. These are
  223. templates from which ``dds`` will create a command-line for a subprocess,
  224. possibly by combining them together.
  225. Each command template allows some set of placeholders. Each instance of the
  226. placeholder string will be replaced in the final command line. Refer to each
  227. respective option for more information.
  228. ``deps_mode``
  229. -------------
  230. Specify the way in which ``dds`` should track compilation dependencies. One
  231. of ``gnu``, ``msvc``, or ``none``.
  232. .. note::
  233. If ``none``, then dependency tracking will be disabled entirely. This will
  234. prevent ``dds`` from tracking interdependencies of source files, and
  235. inhibits incremental compilation.
  236. ``c_compile_file`` and ``cxx_compile_file``
  237. -------------------------------------------
  238. Override the *command template* that is used to compile source files.
  239. This template expects three placeholders:
  240. - ``[in]`` is the path to the file that will be compiled.
  241. - ``[out]`` is the path to the object file that will be generated.
  242. - ``[flags]`` is the placeholder of the compilation flags. This placeholder
  243. must not be attached to any other arguments. The compilation flag argument
  244. list will be inserted in place of ``[flags]``.
  245. Defaults::
  246. {
  247. // On GNU-like compilers (GCC, Clang):
  248. c_compile_file: "<compiler> -fPIC -pthread [flags] -c [in] -o[out]",
  249. cxx_compile_file: "<compiler> -fPIC -pthread [flags] -c [in] -o[out]",
  250. // When `optimize` is enabled, `-O2` is added as a flag
  251. // When `debug` is enabled, `-g` is added as a flag
  252. // On MSVC:
  253. c_compile_file: "cl.exe /MT /nologo /permissive- [flags] /c [in] /Fo[out]",
  254. cxx_compile_file: "cl.exe /MT /EHsc /nologo /permissive- [flags] /c [in] /Fo[out]",
  255. // When `optimize` is enabled, `/O2` is added as a flag
  256. // When `debug` is enabled, `/Z7` and `/DEBUG` are added, and `/MT` becomes `/MTd`
  257. }
  258. ``create_archive``
  259. ------------------
  260. Override the *command template* that is used to generate static library archive
  261. files.
  262. This template expects three placeholders:
  263. - ``[in]`` is the a placeholder for the list of inputs. It must not be attached
  264. to any other arguments. The list of input paths will be inserted in place of
  265. ``[in]``.
  266. - ``[out]`` is the placeholder for the output path for the static library
  267. archive.
  268. Defaults::
  269. {
  270. // On GNU-like:
  271. create_archive: "ar rcs [out] [in]",
  272. // On MSVC:
  273. create_archive: "lib /nologo /OUT:[out] [in]",
  274. }
  275. ``link_executable``
  276. -------------------
  277. Override the *command template* that is used to link executables.
  278. This template expects the same placeholders as ``create_archive``, but
  279. ``[out]`` is a placeholder for the executable file rather than a static
  280. library.
  281. Defaults::
  282. {
  283. // For GNU-like:
  284. link_executable: "<compiler> -fPIC [in] -pthread -o[out] [flags]",
  285. // For MSVC:
  286. link_executable: "cl.exe /nologo /EHsc [in] /Fe[out]",
  287. }
  288. ``include_template`` and ``external_include_template``
  289. ------------------------------------------------------
  290. Override the *command template* for the flags to specify a header search path.
  291. ``external_include_template`` will be used to specify the include search path
  292. for a directory that is "external" (i.e. does not live within the main project).
  293. For each directory added to the ``#include`` search path, this argument
  294. template is instantiated in the ``[flags]`` for the compilation.
  295. This template expects only a single placeholder: ``[path]``, which will be
  296. replaced with the path to the directory to be added to the search path.
  297. On MSVC, this defaults to ``/I [path]``. On GNU-like, ``-isystem [path]`` is
  298. used for ``external_include_template`` and ``-I [path]`` for
  299. ``include_template``.
  300. ``define_template``
  301. -------------------
  302. Override the *command template* for the flags to set a preprocessor definition.
  303. This template expects only a single placeholder: ``[def]``, which is the
  304. preprocessor macro definition argument.
  305. On MSVC, this defaults to ``/D [def]``. On GNU-like compilers, this is
  306. ``-D [def]``.
  307. ``tty_flags``
  308. -------------
  309. Supply additional flags when compiling/linking that will only be applied if
  310. standard output is an ANSI-capable terminal.
  311. On GNU and Clang this will be ``-fdiagnostics-color`` by default.
  312. ``obj_prefix``, ``obj_suffix``, ``archive_prefix``, ``archive_suffix``, ``exe_prefix``, and ``exe_suffix``
  313. ----------------------------------------------------------------------------------------------------------
  314. Set the filename prefixes and suffixes for object files, library archive files,
  315. and executable files, respectively.
  316. .. _toolchains.opts.base_warning_flags:
  317. ``base_warning_flags``
  318. ----------------------
  319. When you compile your project and request warning flags, ``dds`` will
  320. concatenate the warning flags from this option with the flags provided by
  321. ``warning_flags``. This option is "advanced," because it provides a set of
  322. defaults based on the ``compiler_id``.
  323. On GNU-like compilers, the base warning flags are ``-Wall -Wextra -Wpedantic
  324. -Wconversion``. On MSVC the default flag is ``/W4``.
  325. For example, if you set ``warning_flags`` to ``"-Werror"`` on a GNU-like
  326. compiler, the resulting command line will contain ``-Wall -Wextra -Wpedantic
  327. -Wconversion -Werror``.