blob: 1ed09d5c27ce65ee6964d52f59644fcb2f46fa11 [file] [log] [blame] [view]
spomorskiab39ffb2018-10-02 11:19:32 -07001---
2layout: documentation
rosica29e91ca2019-02-18 02:46:34 -08003title: C++ toolchain configuration
spomorskiab39ffb2018-10-02 11:19:32 -07004---
5
rosica29e91ca2019-02-18 02:46:34 -08006# C++ toolchain configuration
spomorskiab39ffb2018-10-02 11:19:32 -07007
laurentlb27bb9482018-11-12 12:50:34 -08008* ToC
9{:toc}
10
spomorskiab39ffb2018-10-02 11:19:32 -070011
spomorskiab39ffb2018-10-02 11:19:32 -070012## Overview
13
rosica0e0f4a32019-02-19 11:27:52 -080014To invoke the compiler with the right options, Bazel needs some knowledge about
15the compiler internals, such as include directories and important flags.
16In other words, Bazel needs a simplified model of the compiler to understand its
17workings.
18
19Bazel needs to know the following:
20
21* Whether the compiler supports thinLTO, modules, dynamic linking, or PIC
22 (position independent code).
23* Paths to the required tools such as gcc, ld, ar, objcopy, and so on.
24* The built-in system include directories. Bazel needs these to validate that
25 all headers that were included in the source file were properly declared in
26 the build file.
27* The default sysroot.
28* Which flags to use for compilation, linking, archiving.
29* Which flags to use for the supported compilation modes (opt, dbg, fastbuild).
30* Make variables specifically required by the compiler.
31
32If the compiler has support for multiple architectures, Bazel needs to configure
33them separately.
34
rosica29e91ca2019-02-18 02:46:34 -080035[`CcToolchainConfigInfo`](skylark/lib/CcToolchainConfigInfo.html) is a provider that provides the necessary level of
36granularity for configuring the behavior of Bazel's C++ rules. By default,
37Bazel automatically configures `CcToolchainConfigInfo` for your build, but you
38have the option to configure it manually. For that, you need a Starlark rule
39that provides the `CcToolchainConfigInfo` and you need to point the
40[`toolchain_config`](be/c-cpp.html#cc_toolchain.toolchain_config) attribute of the `cc_toolchain` to your rule.
41You can create the `CcToolchainConfigInfo` by calling
42[`cc_common.create_cc_toolchain_config_info()`](skylark/lib/cc_common.html#create_cc_toolchain_config_info).
43You can find Starlark constructors for all structs you'll need in the process in
Yannic75ae4792020-02-18 06:00:09 -080044[`@rules_cc//cc:cc_toolchain_config_lib.bzl`](https://github.com/bazelbuild/rules_cc/blob/master/cc/cc_toolchain_config_lib.bzl).
rosica29e91ca2019-02-18 02:46:34 -080045
spomorskiab39ffb2018-10-02 11:19:32 -070046
47When a C++ target enters the analysis phase, Bazel selects the appropriate
rosica29e91ca2019-02-18 02:46:34 -080048`cc_toolchain` target based on the BUILD file, and obtains the
49`CcToolchainConfigInfo` provider from the target specified in the
50`cc_toolchain.toolchain_config` attribute. The `cc_toolchain` target
51passes this information to the C++ target through a `CcToolchainProvider`.
spomorskiab39ffb2018-10-02 11:19:32 -070052
53For example, a compile or link action, instantiated by a rule such as
54`cc_binary` or `cc_library`, needs the following information:
55
56* The compiler or linker to use
57* Command-line flags for the compiler/linker
58* Configuration flags passed through the `--copt/--linkopt` options
59* Environment variables
60* Artifacts needed in the sandbox in which the action executes
61
spomorski20ca4e12018-10-03 07:44:47 -070062All of the above information except the artifacts required in the sandbox is
rosica29e91ca2019-02-18 02:46:34 -080063specified in the Starlark target that the `cc_toolchain` points to.
spomorskiab39ffb2018-10-02 11:19:32 -070064
65The artifacts to be shipped to the sandbox are declared in the `cc_toolchain`
66target. For example, with the `cc_toolchain.linker_files` attribute you can
67specify the linker binary and toolchain libraries to ship into the sandbox.
68
spomorskiab39ffb2018-10-02 11:19:32 -070069## Toolchain selection
70
71The toolchain selection logic operates as follows:
72
731. User specifies a `cc_toolchain_suite` target in the `BUILD` file and points
74 Bazel to the target using the
Jingwen Chen0f4544d2018-12-14 16:28:16 -080075 [`--crosstool_top` option](user-manual.html#flag--crosstool_top).
spomorskiab39ffb2018-10-02 11:19:32 -070076
rosica29e91ca2019-02-18 02:46:34 -0800772. The `cc_toolchain_suite` target references multiple toolchains. The
78 values of the `--cpu` and `--compiler` flags determine which of those
79 toolchains is selected, either based only on the `--cpu` flag value, or
80 based on a joint `--cpu | --compiler` value. The selection process is as
81 follows:
spomorskiab39ffb2018-10-02 11:19:32 -070082
83 * If the `--compiler` option is specified, Bazel selects the
84 corresponding entry from the `cc_toolchain_suite.toolchains`
85 attribute with `--cpu | --compiler`. If Bazel does not find
86 a corresponding entry, it throws an error.
87
88 * If the `--compiler` option is not specified, Bazel selects
89 the corresponding entry from the `cc_toolchain_suite.toolchains`
90 attribute with just `--cpu`.
91
spomorskiab39ffb2018-10-02 11:19:32 -070092 * If no flags are specified, Bazel inspects the host system and selects a
93 `--cpu` value based on its findings. See the
94 [inspection mechanism code](https://source.bazel.build/bazel/+/1b73bc37e184e71651eb631223dcce321ba16211:src/main/java/com/google/devtools/build/lib/analysis/config/AutoCpuConverter.java).
95
96Once a toolchain has been selected, corresponding `feature` and `action_config`
rosica29e91ca2019-02-18 02:46:34 -080097objects in the Starlark rule govern the configuration of the build (that is,
98items described later in this document). These messages allow the
spomorskiab39ffb2018-10-02 11:19:32 -070099implementation of fully fledged C++ features in Bazel without modifying the
100Bazel binary. C++ rules support multiple unique actions documented in detail
101[in the Bazel source code](https://source.bazel.build/bazel/+/4f547a7ea86df80e4c76145ffdbb0c8b75ba3afa:tools/build_defs/cc/action_names.bzl).
102
rosica29e91ca2019-02-18 02:46:34 -0800103## Features
spomorskiab39ffb2018-10-02 11:19:32 -0700104
hlopko0bd4b922018-12-20 06:01:40 -0800105A feature is an entity that requires command-line flags, actions,
spomorskiab39ffb2018-10-02 11:19:32 -0700106constraints on the execution environment, or dependency alterations. A feature
107can be something as simple as allowing BUILD files to select configurations of
108flags, such as `treat_warnings_as_errors`, or interact with the C++ rules and
109include new compile actions and inputs to the compilation, such as
110`header_modules` or `thin_lto`.
111
rosica29e91ca2019-02-18 02:46:34 -0800112Ideally, `CcToolchainConfigInfo` contains a list of features, where each
hlopko0bd4b922018-12-20 06:01:40 -0800113feature consists of one or more flag groups, each defining a list of flags
114that apply to specific Bazel actions.
spomorskiab39ffb2018-10-02 11:19:32 -0700115
rosica29e91ca2019-02-18 02:46:34 -0800116A feature is specified by name, which allows full decoupling of the Starlark
117rule configuration from Bazel releases. In other words, a Bazel release does not
118affect the behavior of `CcToolchainConfigInfo` configurations as long as those
spomorski20ca4e12018-10-03 07:44:47 -0700119configurations do not require the use of new features.
spomorskiab39ffb2018-10-02 11:19:32 -0700120
hlopko0bd4b922018-12-20 06:01:40 -0800121A feature is enabled in one of the following ways:
122
rosica29e91ca2019-02-18 02:46:34 -0800123* The feature's `enabled` field is set to `true`.
hlopko0bd4b922018-12-20 06:01:40 -0800124* Bazel or the rule owner explicitly enable it.
125* The user enables it through the `--feature` Bazel option or `features` rule
126 attribute.
spomorskiab39ffb2018-10-02 11:19:32 -0700127
128Features can have interdependencies, depend on command line flags, `BUILD` file
129settings, and other variables.
130
131### Feature relationships
132
133Dependencies are typically managed directly with Bazel, which simply enforces
134the requirements and manages conflicts intrinsic to the nature of the features
135defined in the build. The toolchain specification allows for more granular
rosica29e91ca2019-02-18 02:46:34 -0800136constraints for use directly within the Starlark rule that govern feature
spomorskiab39ffb2018-10-02 11:19:32 -0700137support and expansion. These are:
138
139<table>
140 <col width="300">
141 <col width="600">
142 <tr>
143 <td><strong>Constraint</strong>
144 </td>
145 <td><strong>Description</strong>
146 </td>
147 </tr>
148 <tr>
rosica29e91ca2019-02-18 02:46:34 -0800149 <td><pre>requires = [
150 feature_set (features = [
151 'feature-name-1',
152 'feature-name-2'
153 ]),
154]</pre>
spomorskiab39ffb2018-10-02 11:19:32 -0700155 </td>
156 <td>Feature-level. The feature is supported only if the specified required
157 features are enabled. For example, when a feature is only supported in
rosica29e91ca2019-02-18 02:46:34 -0800158 certain build modes (<code>opt</code>, <code>dbg</code>, or
159 <code>fastbuild</code>). If `requires` contains multiple `feature_set`s
160 the feature is supported if any of the `feature_set`s is satisfied
161 (when all specified features are enabled).
spomorskiab39ffb2018-10-02 11:19:32 -0700162 </td>
163 </tr>
164 <tr>
rosica29e91ca2019-02-18 02:46:34 -0800165 <td><code>implies = ['feature']</code>
spomorskiab39ffb2018-10-02 11:19:32 -0700166 </td>
rosica29e91ca2019-02-18 02:46:34 -0800167 <td><p>Feature-level. This feature implies the specified feature(s).
168 Enabling a feature also implicitly enables all features implied by it
169 (that is, it functions recursively).</p>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800170 <p>Also provides the ability to factor common subsets of functionality out of
171 a set of features, such as the common parts of sanitizers. Implied
172 features cannot be disabled.</p>
spomorskiab39ffb2018-10-02 11:19:32 -0700173 </td>
174 </tr>
175 <tr>
rosica29e91ca2019-02-18 02:46:34 -0800176 <td><code>provides = ['feature']</code>
spomorskiab39ffb2018-10-02 11:19:32 -0700177 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800178 <td><p>Feature-level. Indicates that this feature is one of several mutually
spomorskiab39ffb2018-10-02 11:19:32 -0700179 exclusive alternate features. For example, all of the sanitizers could
rosica29e91ca2019-02-18 02:46:34 -0800180 specify <code>provides = ["sanitizer"]</code>.</p>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800181 <p>This improves error handling by listing the alternatives if the user asks
182 for two or more mutually exclusive features at once.</p>
spomorskiab39ffb2018-10-02 11:19:32 -0700183 </td>
184 </tr>
185 <tr>
rosica29e91ca2019-02-18 02:46:34 -0800186 <td><pre>with_features = [
187 with_feature_set(
188 features = ['feature-1'],
189 not_features = ['feature-2'],
190 ),
191]</pre>
spomorskiab39ffb2018-10-02 11:19:32 -0700192 </td>
rosica29e91ca2019-02-18 02:46:34 -0800193 <td>Flag set-level. A feature can specify multiple flag sets with multiple.
194 When <code>with_features</code> is specified, the flag set will only expand
195 to the build command if there is at least one <code>with_feature_set</code>
196 for which all of the features in the specified <code>features</code> set
197 are enabled, and all the features specified in <code>not_features</code>
198 set are disabled.
199 If <code>with_features</code> is not specified, the flag set will be
200 applied unconditionally for every action specified.
spomorskiab39ffb2018-10-02 11:19:32 -0700201 </td>
202 </tr>
203</table>
204
rosica29e91ca2019-02-18 02:46:34 -0800205## Actions
spomorskiab39ffb2018-10-02 11:19:32 -0700206
rosica29e91ca2019-02-18 02:46:34 -0800207Actions provide the flexibility to modify the circumstances under
spomorskiab39ffb2018-10-02 11:19:32 -0700208which an action executes without assuming how the action will be run. An
spomorski20ca4e12018-10-03 07:44:47 -0700209`action_config` specifies the tool binary that an action invokes, while a
210`feature` specifies the configuration (flags) that determine how that tool
211behaves when the action is invoked.
spomorskiab39ffb2018-10-02 11:19:32 -0700212
rosica29e91ca2019-02-18 02:46:34 -0800213[Features](#features) reference actions to signal which Bazel actions
214they affect since actions can modify the Bazel action graph. The
215`CcToolchainConfigInfo` provider contains actions that have flags and tools
spomorski863bc872018-10-02 12:20:05 -0700216associated with them, such as `c++-compile`. Flags are assigned to each action
217by associating them with a feature.
spomorskiab39ffb2018-10-02 11:19:32 -0700218
rosica29e91ca2019-02-18 02:46:34 -0800219Each action name represents a single type of action performed by Bazel, such as
220compiling or linking. There is, however, a many-to-one relationship between
221actions and Bazel action types, where a Bazel action type refers to a Java class
222that implements an action (such as `CppCompileAction`). In particular, the
223"assembler actions" and "compiler actions" in the table below are
224`CppCompileAction`, while the link actions are `CppLinkAction`.
spomorskiab39ffb2018-10-02 11:19:32 -0700225
226### Assembler actions
227
228<table>
229 <col width="300">
230 <col width="600">
231 <tr>
232 <td><strong>Action</strong>
233 </td>
234 <td><strong>Description</strong>
235 </td>
236 </tr>
237 <tr>
238 <td><code>preprocess-assemble</code>
239 </td>
240 <td>Assemble with preprocessing. Typically for <code>.S</code> files.
241 </td>
242 </tr>
243 <tr>
244 <td><code>assemble</code>
245 </td>
246 <td>Assemble without preprocessing. Typically for <code>.s</code> files.
247 </td>
248 </tr>
249</table>
250
251### Compiler actions
252
253<table>
254 <col width="300">
255 <col width="600">
256 <tr>
257 <td><strong>Action</strong>
258 </td>
259 <td><strong>Description</strong>
260 </td>
261 </tr>
262 <tr>
263 <td><code>cc-flags-make-variable</code>
264 </td>
265 <td>Propagates <code>CC_FLAGS</code> to genrules.
266 </td>
267 </tr>
268 <tr>
269 <td><code>c-compile</code>
270 </td>
271 <td>Compile as C.
272 </td>
273 </tr>
274 <tr>
275 <td><code>c++-compile</code>
276 </td>
277 <td>Compile as C++.
278 </td>
279 </tr>
280 <tr>
281 <td><code>c++-header-parsing</code>
282 </td>
283 <td>Run the compiler's parser on a header file to ensure that the header is
284 self-contained, as it will otherwise produce compilation errors. Applies
285 only to toolchains that support modules.
286 </td>
287 </tr>
288</table>
289
290### Link actions
291
292<table>
293 <col width="300">
294 <col width="600">
295 <tr>
296 <td><strong>Action</strong>
297 </td>
298 <td><strong>Description</strong>
299 </td>
300 </tr>
301 <tr>
302 <td><code>c++-link-dynamic-library</code>
303 </td>
304 <td>Link a shared library containing all of its dependencies.
305 </td>
306 </tr>
307 <tr>
308 <td><code>c++-link-nodeps-dynamic-library</code>
309 </td>
310 <td>Link a shared library only containing <code>cc_library</code> sources.
311 </td>
312 </tr>
313 <tr>
314 <td><code>c++-link-executable</code>
315 </td>
316 <td>Link a final ready-to-run library.
317 </td>
318 </tr>
319</table>
320
321### AR actions
322
323AR actions assemble object files into archive libraries (`.a` files) via `ar`
324and encode some semantics into the name.
325
326<table>
327 <col width="300">
328 <col width="600">
329 <tr>
330 <td><strong>Action</strong>
331 </td>
332 <td><strong>Description</strong>
333 </td>
334 </tr>
335 <tr>
336 <td><code>c++-link-static-library</code>
337 </td>
338 <td>Create a static library (archive).
339 </td>
340 </tr>
341</table>
342
343### LTO actions
344
345<table>
346 <col width="300">
347 <col width="600">
348 <tr>
349 <td><strong>Action</strong>
350 </td>
351 <td><strong>Description</strong>
352 </td>
353 </tr>
354 <tr>
355 <td><code>lto-backend</code>
356 </td>
357 <td>ThinLTO action compiling bitcodes into native objects.
358 </td>
359 </tr>
360 <tr>
361 <td><code>lto-index</code>
362 </td>
363 <td>ThinLTO action generating global index.
364 </td>
365 </tr>
366</table>
367
rosica29e91ca2019-02-18 02:46:34 -0800368## Action config
spomorskiab39ffb2018-10-02 11:19:32 -0700369
rosica29e91ca2019-02-18 02:46:34 -0800370A `action_config` is a Starlark struct that describes a Bazel action
spomorski20ca4e12018-10-03 07:44:47 -0700371by specifying the tool (binary) to invoke during the action and sets of flags,
rosica29e91ca2019-02-18 02:46:34 -0800372defined by features, that apply constraints to the action's execution. The
373`action_config()` constructor has the following parameters:
spomorskiab39ffb2018-10-02 11:19:32 -0700374
375<table>
376 <col width="300">
377 <col width="600">
378 <tr>
379 <td><strong>Attribute</strong>
380 </td>
381 <td><strong>Description</strong>
382 </td>
383 </tr>
384 <tr>
385 <td><code>action_name</code>
386 </td>
rosica29e91ca2019-02-18 02:46:34 -0800387 <td>The Bazel action to which this action corresponds.
spomorski20ca4e12018-10-03 07:44:47 -0700388 Bazel uses this attribute to discover per-action tool and execution
389 requirements.
spomorskiab39ffb2018-10-02 11:19:32 -0700390 </td>
391 </tr>
392 <tr>
rosica29e91ca2019-02-18 02:46:34 -0800393 <td><code>tools</code>
spomorskiab39ffb2018-10-02 11:19:32 -0700394 </td>
rosica29e91ca2019-02-18 02:46:34 -0800395 <td>The executable to invoke. The tool applied to the action will be the
396 first tool in the list with a feature set that matches the feature
397 configuration. Default value must be provided.
spomorskiab39ffb2018-10-02 11:19:32 -0700398 </td>
399 </tr>
400 <tr>
rosica29e91ca2019-02-18 02:46:34 -0800401 <td><code>flag_sets</code>
spomorskiab39ffb2018-10-02 11:19:32 -0700402 </td>
rosica29e91ca2019-02-18 02:46:34 -0800403 <td>A list of flags that applies to a group of actions. Same as for a
404 feature.
spomorskiab39ffb2018-10-02 11:19:32 -0700405 </td>
406 </tr>
407 <tr>
rosica29e91ca2019-02-18 02:46:34 -0800408 <td><code>env_sets</code>
spomorskiab39ffb2018-10-02 11:19:32 -0700409 </td>
rosica29e91ca2019-02-18 02:46:34 -0800410 <td>A list of environment constraints that applies to a group of actions.
411 Same as for a feature.
spomorskiab39ffb2018-10-02 11:19:32 -0700412 </td>
413 </tr>
414</table>
415
rosica29e91ca2019-02-18 02:46:34 -0800416An `action_config` can require and imply other features and
spomorskiab39ffb2018-10-02 11:19:32 -0700417<code>action_config</code>s as dictated by the
418[feature relationships](#feature-relationships) described earlier. This behavior
419is similar to that of a feature.
420
421The last two attributes are redundant against the corresponding attributes on
422features and are included because some Bazel actions require certain flags or
spomorski20ca4e12018-10-03 07:44:47 -0700423environment variables and we want to avoid unnecessary `action_config`+`feature`
spomorskiab39ffb2018-10-02 11:19:32 -0700424pairs. Typically, sharing a single feature across multiple `action_config`s is
425preferred.
426
rosica29e91ca2019-02-18 02:46:34 -0800427You can not define more than one `action_config` with the same `action_name`
428within the same toolchain. This prevents ambiguity in tool paths
spomorskiab39ffb2018-10-02 11:19:32 -0700429and enforces the intention behind `action_config` - that an action's properties
430are clearly described in a single place in the toolchain.
431
rosica29e91ca2019-02-18 02:46:34 -0800432### `tool`
spomorskiab39ffb2018-10-02 11:19:32 -0700433
rosica29e91ca2019-02-18 02:46:34 -0800434An`action_config` can specify a set of tools via its `tools` parameter.
435The `tool()` constructor takes in the following parameters:
spomorskiab39ffb2018-10-02 11:19:32 -0700436
437
438<table>
439 <col width="300">
440 <col width="600">
441 <tr>
442 <td><strong>Field</strong>
443 </td>
444 <td><strong>Description</strong>
445 </td>
446 </tr>
447 <tr>
448 <td><code>tool_path</code>
449 </td>
rosica29e91ca2019-02-18 02:46:34 -0800450 <td>Path to the tool in question (relative to the current location).
spomorskiab39ffb2018-10-02 11:19:32 -0700451 </td>
452 </tr>
453 <tr>
rosica29e91ca2019-02-18 02:46:34 -0800454 <td><code>with_features</code>
spomorskiab39ffb2018-10-02 11:19:32 -0700455 </td>
rosica29e91ca2019-02-18 02:46:34 -0800456 <td>A a list of feature sets out of which at least one must be satisfied
457 for this tool to apply.
spomorskiab39ffb2018-10-02 11:19:32 -0700458 </td>
459 </tr>
460</table>
461
rosica29e91ca2019-02-18 02:46:34 -0800462For a given `action_config`, only a single `tool` applies
spomorski20ca4e12018-10-03 07:44:47 -0700463its tool path and execution requirements to the Bazel action. A tool is selected
rosica29e91ca2019-02-18 02:46:34 -0800464by iterating through the `tools` attribute on an `action_config` until a tool
465with a `with_feature` set matching the feature configuration is found
spomorskiab39ffb2018-10-02 11:19:32 -0700466(see [Feature relationships](#feature-relationships) earlier in this document
467for more information). We recommend that you end your tool lists with a default
468tool that corresponds to an empty feature configuration.
469
spomorskiab39ffb2018-10-02 11:19:32 -0700470### Example usage
471
rosica29e91ca2019-02-18 02:46:34 -0800472Features and actions can be used together to implement Bazel actions
spomorskiab39ffb2018-10-02 11:19:32 -0700473with diverse cross-platform semantics. For example, debug symbol generation on
474macOS requires generating symbols in the compile action, then invoking a
475specialized tool during the link action to create compressed dsym archive, and
476then decompressing that archive to produce the application bundle and `.plist`
477files consumable by Xcode.
478
479With Bazel, this process can instead be implemented as follows, with
480`unbundle-debuginfo` being a Bazel action:
481
Yannic75ae4792020-02-18 06:00:09 -0800482 load("@rules_cc//cc:defs.bzl", "ACTION_NAMES")
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800483
rosica29e91ca2019-02-18 02:46:34 -0800484 action_configs = [
485 action_config (
486 config_name = "ACTION_NAMES.cpp_link_executable",
487 action_name = "ACTION_NAMES.cpp_link_executable",
488 tools = [
489 tool(
490 with_features = [
491 with_feature(features=["generate-debug-symbols"]),
492 ],
493 tool_path = "toolchain/mac/ld-with-dsym-packaging",
494 ),
495 tool (tool_path = "toolchain/mac/ld"),
496 ],
497 ),
498 ]
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800499
rosica29e91ca2019-02-18 02:46:34 -0800500 features = [
501 feature(
502 name = "generate-debug-symbols",
503 flag_sets = [
504 flag_set (
505 actions = [
506 "ACTION_NAMES.c_compile",
507 "ACTION_NAMES.cpp_compile"
508 ],
509 flag_groups = [
510 flag_group(
511 flags = ["-g"],
512 ),
513 ],
514 )
515 ],
516 implies = ["unbundle-debuginfo"],
517 ),
518 ]
spomorskiab39ffb2018-10-02 11:19:32 -0700519
spomorskiab39ffb2018-10-02 11:19:32 -0700520
521This same feature can be implemented entirely differently for Linux, which uses
522`fission`, or for Windows, which produces `.pdb` files. For example, the
523implementation for `fission`-based debug symbol generation might look as
524follows:
525
Yannic75ae4792020-02-18 06:00:09 -0800526 load("@rules_cc//cc:defs.bzl", "ACTION_NAMES")
spomorskiab39ffb2018-10-02 11:19:32 -0700527
rosica29e91ca2019-02-18 02:46:34 -0800528 action_configs = [
529 action_config (
530 name = "ACTION_NAMES.cpp_compile",
531 tools = [
532 tool(
533 tool_path = "toolchain/bin/gcc",
534 ),
535 ],
536 ),
537 ]
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800538
rosica29e91ca2019-02-18 02:46:34 -0800539 features = [
540 feature (
541 name = "generate-debug-symbols",
542 requires = [with_feature_set(features = ["dbg"])],
543 flag_sets = [
544 flag_set(
545 actions = ["ACTION_NAMES.cpp_compile"],
546 flag_groups = [
547 flag_group(
548 flags = ["-gsplit-dwarf"],
549 ),
550 ],
551 ),
552 flag_set(
553 actions = ["ACTION_NAMES.cpp_link_executable"],
554 flag_groups = [
555 flag_group(
556 flags = ["-Wl", "--gdb-index"],
557 ),
558 ],
559 ),
560 ],
561 ),
562 ]
spomorskiab39ffb2018-10-02 11:19:32 -0700563
spomorskiab39ffb2018-10-02 11:19:32 -0700564
565### Flag groups
566
rosica29e91ca2019-02-18 02:46:34 -0800567`CcToolchainConfigInfo` allows you to bundle flags into groups that serve a
568specific purpose. You can specify a flag within using pre-defined variables
spomorskiab39ffb2018-10-02 11:19:32 -0700569within the flag value, which the compiler expands when adding the flag to the
570build command. For example:
571
rosica29e91ca2019-02-18 02:46:34 -0800572 flag_group (
573 flags = ["%{output_file_path}"],
574 )
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800575
spomorskiab39ffb2018-10-02 11:19:32 -0700576
577In this case, the contents of the flag will be replaced by the output file path
578of the action.
579
580Flag groups are expanded to the build command in the order in which they appear
rosica29e91ca2019-02-18 02:46:34 -0800581in the list, top-to-bottom, left-to-right.
spomorskiab39ffb2018-10-02 11:19:32 -0700582
583For flags that need to repeat with different values when added to the build
584command, the flag group can iterate variables of type `list`. For example, the
585variable `include_path` of type `list`:
586
rosica29e91ca2019-02-18 02:46:34 -0800587 flag_group (
588 iterate_over = "include_paths",
589 flags = ["-I%{include_paths}"],
590 )
spomorskiab39ffb2018-10-02 11:19:32 -0700591
592expands to `-I<path>` for each path element in the `include_paths` list. All
593flags (or `flag_group`s) in the body of a flag group declaration are expanded as
594a unit. For example:
595
rosica29e91ca2019-02-18 02:46:34 -0800596 flag_group (
597 iterate_over = "include_paths",
598 flags = ["-I", "%{include_paths}"],
599 )
spomorskiab39ffb2018-10-02 11:19:32 -0700600
601expands to `-I <path>` for each path element in the `include_paths` list.
602
603A variable can repeat multiple times. For example:
604
rosica29e91ca2019-02-18 02:46:34 -0800605 flag_group (
606 iterate_over = "include_paths",
607 flags = ["-iprefix=%{include_paths}", "-isystem=%{include_paths}"],
608 )
spomorskiab39ffb2018-10-02 11:19:32 -0700609
610expands to:
611
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800612 -iprefix=<inc0> -isystem=<inc0> -iprefix=<inc1> -isystem=<inc1>
spomorskiab39ffb2018-10-02 11:19:32 -0700613
614Variables can correspond to structures accessible using dot-notation. For
615example:
616
rosica29e91ca2019-02-18 02:46:34 -0800617 flag_group (
618 flags = ["-l%{libraries_to_link.name}"],
619 )
spomorskiab39ffb2018-10-02 11:19:32 -0700620
621Structures can be nested and may also contain sequences. To prevent name clashes
622and to be explicit, you must specify the full path through the fields. For
623example:
624
rosica29e91ca2019-02-18 02:46:34 -0800625 flag_group (
626 iterate_over = "libraries_to_link",
627 flag_groups = [
628 flag_group (
629 iterate_over = "libraries_to_link.shared_libraries",
630 flags = ["-l%{libraries_to_link.shared_libraries.name}"],
631 ),
632 ],
633 )
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800634
spomorskiab39ffb2018-10-02 11:19:32 -0700635
636### Conditional expansion
637
638Flag groups support conditional expansion based on the presence of a particular
rosica29e91ca2019-02-18 02:46:34 -0800639variable or its field using the `expand_if_available`, `expand_if_not_available`,
640`expand_if_true`, `expand_if_false`, or `expand_if_equal` attributes. For example:
spomorskiab39ffb2018-10-02 11:19:32 -0700641
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800642
rosica29e91ca2019-02-18 02:46:34 -0800643 flag_group (
644 iterate_over = "libraries_to_link",
645 flag_groups = [
646 flag_group (
647 iterate_over = "libraries_to_link.shared_libraries",
648 flag_groups = [
649 flag_group (
650 expand_if_available = "libraries_to_link.shared_libraries.is_whole_archive",
651 flags = ["--whole_archive"],
652 ),
653 flag_group (
654 flags = ["-l%{libraries_to_link.shared_libraries.name}"],
655 ),
656 flag_group (
657 expand_if_available = "libraries_to_link.shared_libraries.is_whole_archive",
658 flags = ["--no_whole_archive"],
659 ),
660 ],
661 ),
662 ],
663 )
spomorskiab39ffb2018-10-02 11:19:32 -0700664
665**Note:** The `--whole_archive` and `--no_whole_archive` options are added to
666the build command only when a currently iterated library has an
667`is_whole_archive` field.
668
rosica29e91ca2019-02-18 02:46:34 -0800669## `CcToolchainConfigInfo` reference
spomorskiab39ffb2018-10-02 11:19:32 -0700670
671This section provides a reference of build variables, features, and other
rosica29e91ca2019-02-18 02:46:34 -0800672information required to successfully configure C++ rules.
spomorskiab39ffb2018-10-02 11:19:32 -0700673
rosica29e91ca2019-02-18 02:46:34 -0800674### `CcToolchainConfigInfo` build variables
spomorskiab39ffb2018-10-02 11:19:32 -0700675
rosica29e91ca2019-02-18 02:46:34 -0800676The following is a reference of `CcToolchainConfigInfo` build variables.
spomorskiab39ffb2018-10-02 11:19:32 -0700677
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800678**Note:** The **Action** column indicates the relevant action type, if applicable.
spomorskiab39ffb2018-10-02 11:19:32 -0700679
680<table>
spomorskiab39ffb2018-10-02 11:19:32 -0700681 <tr>
682 <td><strong>Variable</strong>
683 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800684 <td><strong>Action</strong>
685 </td>
spomorskiab39ffb2018-10-02 11:19:32 -0700686 <td><strong>Description</strong>
687 </td>
688 </tr>
689 <tr>
690 <td><strong><code>source_file</code></strong>
691 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800692 <td>compile</td>
693 <td>Source file to compile.
spomorskiab39ffb2018-10-02 11:19:32 -0700694 </td>
695 </tr>
696 <tr>
697 <td><strong><code>input_file</code></strong>
698 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800699 <td>strip</td>
700 <td>Artifact to strip.
spomorskiab39ffb2018-10-02 11:19:32 -0700701 </td>
702 </tr>
703 <tr>
704 <td><strong><code>output_file</code></strong>
705 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800706 <td>compile</td>
707 <td>Compilation output.
spomorskiab39ffb2018-10-02 11:19:32 -0700708 </td>
709 </tr>
710 <tr>
711 <td><strong><code>output_assembly_file</code></strong>
712 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800713 <td>compile</td>
714 <td>Emitted assembly file. Applies only when the
spomorskiab39ffb2018-10-02 11:19:32 -0700715 <code>compile</code> action emits assembly text, typically when using the
716 <code>--save_temps</code> flag. The contents are the same as for
717 <code>output_file</code>.
718 </td>
719 </tr>
720 <tr>
721 <td><strong><code>output_preprocess_file</code></strong>
722 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800723 <td>compile</td>
724 <td>Preprocessed output. Applies only to compile
spomorskiab39ffb2018-10-02 11:19:32 -0700725 actions that only preprocess the source files, typically when using the
726 <code>--save_temps</code> flag. The contents are the same as for
727 <code>output_file</code>.
728 </td>
729 </tr>
730 <tr>
731 <td><strong><code>includes</code></strong>
732 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800733 <td>compile</td>
734 <td>Sequence of files the compiler must
spomorskiab39ffb2018-10-02 11:19:32 -0700735 unconditionally include in the compiled source.
736 </td>
737 </tr>
738 <tr>
739 <td><strong><code>include_paths</code></strong>
740 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800741 <td>compile</td>
742 <td>Sequence directories in which the compiler
spomorskiab39ffb2018-10-02 11:19:32 -0700743 searches for headers included using <code>#include&lt;foo.h&gt;</code>
744 and <code>#include "foo.h"</code>.
745 </td>
746 </tr>
747 <tr>
748 <td><strong><code>quote_include_paths</code></strong>
hlopko9a14e422019-03-05 06:14:12 -0800749 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800750 <td>compile</td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800751 <td>Sequence of <code>-iquote</code> includes -
spomorskiab39ffb2018-10-02 11:19:32 -0700752 directories in which the compiler searches for headers included using
Brian Silverman5fdeb6c2020-01-02 07:05:49 -0800753 <code>#include "foo.h"</code>.
spomorskiab39ffb2018-10-02 11:19:32 -0700754 </td>
755 </tr>
756 <tr>
757 <td><strong><code>system_include_paths</code></strong>
758 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800759 <td>compile</td>
760 <td>Sequence of <code>-isystem</code> includes -
spomorskiab39ffb2018-10-02 11:19:32 -0700761 directories in which the compiler searches for headers included using
Brian Silverman5fdeb6c2020-01-02 07:05:49 -0800762 <code>#include &lt;foo.h&gt;</code>.
spomorskiab39ffb2018-10-02 11:19:32 -0700763 </td>
764 </tr>
765 <tr>
766 <td><strong><code>dependency_file</code></strong>
767 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800768 <td>compile</td>
769 <td>The <code>.d</code> dependency file generated by the compiler.
spomorskiab39ffb2018-10-02 11:19:32 -0700770 </td>
771 </tr>
772 <tr>
773 <td><strong><code>preprocessor_defines</code></strong>
774 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800775 <td>compile</td>
776 <td>Sequence of <code>defines</code>, such as <code>--DDEBUG</code>.
spomorskiab39ffb2018-10-02 11:19:32 -0700777 </td>
778 </tr>
779 <tr>
780 <td><strong><code>pic</code></strong>
781 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800782 <td>compile</td>
783 <td>Compiles the output as position-independent code.
spomorskiab39ffb2018-10-02 11:19:32 -0700784 </td>
785 </tr>
786 <tr>
787 <td><strong><code>gcov_gcno_file</code></strong>
788 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800789 <td>compile</td>
790 <td>The <code>gcov</code> coverage file.
spomorskiab39ffb2018-10-02 11:19:32 -0700791 </td>
792 </tr>
793 <tr>
794 <td><strong><code>per_object_debug_info_file</code></strong>
795 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800796 <td>compile</td>
797 <td>The per-object debug info (<code>.dwp</code>) file.
spomorskiab39ffb2018-10-02 11:19:32 -0700798 </td>
799 </tr>
800 <tr>
801 <td><strong><code>stripotps</code></strong>
802 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800803 <td>strip</td>
804 <td>Sequence of <code>stripopts</code>.
spomorskiab39ffb2018-10-02 11:19:32 -0700805 </td>
806 </tr>
807 <tr>
808 <td><strong><code>legacy_compile_flags</code></strong>
809 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800810 <td>compile</td>
811 <td>Sequence of flags from legacy
812 <code>CROSSTOOL</code> fields such as <code>compiler_flag</code>,
813 <code>optional_compiler_flag</code>, <code>cxx_flag</code>, and
814 <code>optional_cxx_flag</code>.
spomorskiab39ffb2018-10-02 11:19:32 -0700815 </td>
816 </tr>
817 <tr>
818 <td><strong><code>user_compile_flags</code></strong>
819 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800820 <td>compile</td>
821 <td>Sequence of flags from either the
spomorskiab39ffb2018-10-02 11:19:32 -0700822 <code>copt</code> rule attribute or the <code>--copt</code>,
823 <code>--cxxopt</code>, and <code>--conlyopt</code> flags.
824 </td>
825 </tr>
826 <tr>
827 <td><strong><code>unfiltered_compile_flags</code></strong>
828 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800829 <td>compile</td>
830 <td>Sequence of flags from the
spomorski20ca4e12018-10-03 07:44:47 -0700831 <code>unfiltered_cxx_flag</code> legacy <code>CROSSTOOL</code> field or the
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800832 <code>unfiltered_compile_flags</code> feature. These are not filtered by
spomorskiab39ffb2018-10-02 11:19:32 -0700833 the <code>nocopts</code> rule attribute.
834 </td>
835 </tr>
836 <tr>
837 <td><strong><code>sysroot</code></strong>
838 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800839 <td></td>
spomorskiab39ffb2018-10-02 11:19:32 -0700840 <td>The <code>sysroot</code>.
841 </td>
842 </tr>
843 <tr>
844 <td><strong><code>runtime_library_search_directories</code></strong>
845 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800846 <td>link</td>
847 <td>Entries in the linker runtime search path (usually
spomorskiab39ffb2018-10-02 11:19:32 -0700848 set with the <code>-rpath</code> flag).
849 </td>
850 </tr>
851 <tr>
852 <td><strong><code>library_search_directories</code></strong>
853 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800854 <td>link</td>
855 <td>Entries in the linker search path (usually set with
spomorskiab39ffb2018-10-02 11:19:32 -0700856 the <code>-L</code> flag).
857 </td>
858 </tr>
859 <tr>
860 <td><strong><code>libraries_to_link</code></strong>
861 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800862 <td>link</td>
863 <td>Flags providing files to link as inputs in the linker invocation.
spomorskiab39ffb2018-10-02 11:19:32 -0700864 </td>
865 </tr>
866 <tr>
867 <td><strong><code>def_file_path</code></strong>
868 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800869 <td>link</td>
870 <td>Location of def file used on Windows with MSVC.
spomorskiab39ffb2018-10-02 11:19:32 -0700871 </td>
872 </tr>
873 <tr>
874 <td><strong><code>linker_param_file</code></strong>
875 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800876 <td>link</td>
877 <td>Location of linker param file created by bazel to
spomorskiab39ffb2018-10-02 11:19:32 -0700878 overcome command line length limit.
879 </td>
880 </tr>
881 <tr>
882 <td><strong><code>output_execpath</code></strong>
883 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800884 <td>link</td>
885 <td>Execpath of the output of the linker.
spomorskiab39ffb2018-10-02 11:19:32 -0700886 </td>
887 </tr>
888 <tr>
889 <td><strong><code>generate_interface_library</code></strong>
890 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800891 <td>link</td>
892 <td><code>"yes"</code> or <code>"no"</code> depending on whether interface library should
893 be generated.
spomorskiab39ffb2018-10-02 11:19:32 -0700894 </td>
895 </tr>
896 <tr>
897 <td><strong><code>interface_library_builder_path</code></strong>
898 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800899 <td>link</td>
900 <td>Path to the interface library builder tool.
spomorskiab39ffb2018-10-02 11:19:32 -0700901 </td>
902 </tr>
903 <tr>
904 <td><strong><code>interface_library_input_path</code></strong>
905 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800906 <td>link</td>
907 <td>Input for the interface library <code>ifso</code> builder tool.
spomorskiab39ffb2018-10-02 11:19:32 -0700908 </td>
909 </tr>
910 <tr>
911 <td><strong><code>interface_library_output_path</code></strong>
912 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800913 <td>link</td>
914 <td>Path where to generate interface library using the <code>ifso</code> builder tool.
spomorskiab39ffb2018-10-02 11:19:32 -0700915 </td>
916 </tr>
917 <tr>
918 <td><strong><code>legacy_link_flags</code></strong>
919 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800920 <td>link</td>
hlopko70191312018-12-28 07:44:21 -0800921 <td>Linker flags coming from the legacy <code>CROSSTOOL</code> fields.
spomorskiab39ffb2018-10-02 11:19:32 -0700922 </td>
923 </tr>
924 <tr>
925 <td><strong><code>user_link_flags</code></strong>
926 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800927 <td>link</td>
928 <td>Linker flags coming from the <code>--linkopt</code>
spomorskiab39ffb2018-10-02 11:19:32 -0700929 or <code>linkopts</code> attribute.
930 </td>
931 </tr>
932 <tr>
933 <td><strong><code>symbol_counts_output</code></strong>
934 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800935 <td>link</td>
936 <td>Path to which to write symbol counts.
spomorskiab39ffb2018-10-02 11:19:32 -0700937 </td>
938 </tr>
939 <tr>
940 <td><strong><code>linkstamp_paths</code></strong>
941 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800942 <td>link</td>
943 <td>A build variable giving linkstamp paths.
spomorskiab39ffb2018-10-02 11:19:32 -0700944 </td>
945 </tr>
946 <tr>
947 <td><strong><code>force_pic</code></strong>
948 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800949 <td>link</td>
hlopko70191312018-12-28 07:44:21 -0800950 <td>Presence of this variable indicates that PIC/PIE code should
951 be generated (Bazel option `--force_pic` was passed).
spomorskiab39ffb2018-10-02 11:19:32 -0700952 </td>
953 </tr>
954 <tr>
955 <td><strong><code>strip_debug_symbols</code></strong>
956 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800957 <td>link</td>
958 <td>Presence of this variable indicates that the debug
spomorskiab39ffb2018-10-02 11:19:32 -0700959 symbols should be stripped.
960 </td>
961 </tr>
962 <tr>
963 <td><strong><code>is_cc_test</code></strong>
964 </td>
Laszlo Csomorb2f0e262018-12-20 02:56:25 -0800965 <td>link</td>
966 <td>Truthy when current action is a <code>cc_test</code>
spomorskiab39ffb2018-10-02 11:19:32 -0700967 linking action, false otherwise.
968 </td>
969 </tr>
970 <tr>
971 <td><strong><code>is_using_fission</code></strong>
972 </td>
hlopko123d5ac2019-03-06 05:59:21 -0800973 <td>compile, link</td>
974 <td>Presence of this variable indicates that fission (per-object debug info)
975 is activated. Debug info will be in <code>.dwo</code> files instead
976 of <code>.o</code> files and the compiler and linker need to know this.
spomorskiab39ffb2018-10-02 11:19:32 -0700977 </td>
978 </tr>
Googler3eb2f512019-04-09 09:54:01 -0700979 <tr>
980 <td><strong><code>fdo_instrument_path</code></strong>
981 </td>
982 <td>compile, link</td>
983 <td> Path to the directory that stores FDO instrumentation profile.
984 </td>
985 </tr>
986 <tr>
987 <td><strong><code>fdo_profile_path</code></strong>
988 </td>
989 <td>compile</td>
990 <td> Path to FDO profile.
991 </td>
992 </tr>
993 <tr>
994 <td><strong><code>fdo_prefetch_hints_path</code></strong>
995 </td>
996 <td>compile</td>
997 <td> Path to the cache prefetch profile.
998 </td>
999 </tr>
1000 <tr>
1001 <td><strong><code>csfdo_instrument_path</code></strong>
1002 </td>
1003 <td>compile, link</td>
1004 <td> Path to the directory that stores context sensitive FDO
1005 instrumentation profile.
1006 </td>
1007 </tr>
spomorskiab39ffb2018-10-02 11:19:32 -07001008</table>
1009
1010
1011
hlopko07629482018-12-20 00:28:26 -08001012### Well-known features
spomorskiab39ffb2018-10-02 11:19:32 -07001013
rosica29e91ca2019-02-18 02:46:34 -08001014The following is a reference of features and their activation
spomorskiab39ffb2018-10-02 11:19:32 -07001015conditions.
1016
1017<table>
1018 <col width="300">
1019 <col width="600">
1020 <tr>
1021 <td><strong>Feature</strong>
1022 </td>
hlopko07629482018-12-20 00:28:26 -08001023 <td><strong>Documentation</strong>
spomorskiab39ffb2018-10-02 11:19:32 -07001024 </td>
1025 </tr>
1026 <tr>
1027 <td><strong><code>opt | dbg | fastbuild</code></strong>
1028 </td>
1029 <td>Enabled by default based on compilation mode.
1030 </td>
1031 </tr>
1032 <tr>
1033 <td><strong><code>static_linking_mode | dynamic_linking_mode</code></strong>
1034 </td>
1035 <td>Enabled by default based on linking mode.
1036 </td>
1037 </tr>
1038 <tr>
spomorskiab39ffb2018-10-02 11:19:32 -07001039 <td><strong><code>per_object_debug_info</code></strong>
1040 </td>
rosica29e91ca2019-02-18 02:46:34 -08001041 <td>Enabled if the <code>supports_fission</code> feature is specified and
1042 enabled and the current compilation mode is specified in the
spomorski20ca4e12018-10-03 07:44:47 -07001043 <code>--fission</code> flag.
spomorskiab39ffb2018-10-02 11:19:32 -07001044 </td>
1045 </tr>
1046 <tr>
hlopko07629482018-12-20 00:28:26 -08001047 <td><strong><code>supports_start_end_lib</code></strong>
1048 </td>
1049 <td>If enabled (and the option <code>--start_end_lib</code> is set), Bazel
1050 will not link against static libraries but instead use the
1051 <code>--start-lib/--end-lib</code> linker options to link against objects
1052 directly. This speeds up the build since Bazel doesn't have to build
1053 static libraries.
1054 </td>
1055 </tr>
hlopkoa5ba3442018-12-20 01:58:43 -08001056 <tr>
1057 <td><strong><code>supports_interface_shared_libraries</code></strong>
1058 </td>
1059 <td>If enabled (and the option <code>--interface_shared_objects</code> is
1060 set), Bazel will link targets that have <code>linkstatic</code> set to
1061 False (<code>cc_test</code>s by default) against interface shared
1062 libraries. This makes incremental relinking faster.
1063 </td>
1064 </tr>
hlopko8098e592018-12-22 14:45:24 -08001065 <tr>
1066 <td><strong><code>supports_dynamic_linker</code></strong>
1067 </td>
1068 <td>If enabled, C++ rules will know the toolchain can produce shared
1069 libraries.
1070 </td>
1071 </tr>
hlopkod7171182018-12-27 07:09:21 -08001072 <tr>
1073 <td><strong><code>static_link_cpp_runtimes</code></strong>
1074 </td>
1075 <td>If enabled, Bazel will link the C++ runtime statically in static linking
1076 mode and dynamically in dynamic linking mode. Artifacts
1077 specified in the <code>cc_toolchain.static_runtime_lib</code> or
1078 <code>cc_toolchain.dynamic_runtime_lib</code> attribute (depending on the
1079 linking mode) will be added to the linking actions.
1080 </td>
1081 </tr>
hlopko70191312018-12-28 07:44:21 -08001082 <tr>
1083 <td><strong><code>supports_pic</code></strong>
1084 </td>
1085 <td>If enabled, toolchain will know to use PIC objects for dynamic libraries.
1086 The `pic` variable is present whenever PIC compilation is needed. If not enabled
1087 by default, and `--force_pic` is passed, Bazel will request `supports_pic` and
1088 validate that the feature is enabled. If the feature is missing, or couldn't
1089 be enabled, `--force_pic` cannot be used.
1090 </td>
1091 </tr>
hlopkoee8b3572019-01-15 02:49:55 -08001092 <tr>
1093 <td>
1094 <strong><code>static_linking_mode | dynamic_linking_mode</code></strong>
1095 </td>
1096 <td>Enabled by default based on linking mode.</td>
1097 </tr>
1098 <tr>
1099 <td><strong><code>no_legacy_features</code></strong>
1100 </td>
1101 <td>
1102 Prevents Bazel from adding legacy features to
rosica29e91ca2019-02-18 02:46:34 -08001103 the C++ configuration when present. See the complete list of
hlopkoee8b3572019-01-15 02:49:55 -08001104 features below.
1105 </td>
1106 </tr>
spomorskiab39ffb2018-10-02 11:19:32 -07001107</table>
hlopkoee8b3572019-01-15 02:49:55 -08001108
1109#### Legacy features patching logic
1110
1111<p>
1112 Bazel does following changes to the features and their order to stay backwards
1113 compatible:
1114
1115 <ul>
1116 <li>Moves <code>legacy_compile_flags</code> feature to the top of the toolchain</li>
1117 <li>Moves <code>default_compile_flags</code> feature to the top of the toolchain</li>
1118 <li>Adds <code>dependency_file</code> (if not present) feature to the top of the toolchain</li>
1119 <li>Adds <code>pic</code> (if not present) feature to the top of the toolchain</li>
1120 <li>Adds <code>per_object_debug_info</code> (if not present) feature to the top of the toolchain</li>
1121 <li>Adds <code>preprocessor_defines</code> (if not present) feature to the top of the toolchain</li>
1122 <li>Adds <code>includes</code> (if not present) feature to the top of the toolchain</li>
1123 <li>Adds <code>include_paths</code> (if not present) feature to the top of the toolchain</li>
1124 <li>Adds <code>fdo_instrument</code> (if not present) feature to the top of the toolchain</li>
1125 <li>Adds <code>fdo_optimize</code> (if not present) feature to the top of the toolchain</li>
Googler3eb2f512019-04-09 09:54:01 -07001126 <li>Adds <code>cs_fdo_instrument</code> (if not present) feature to the top of the toolchain</li>
1127 <li>Adds <code>cs_fdo_optimize</code> (if not present) feature to the top of the toolchain</li>
hlopkoee8b3572019-01-15 02:49:55 -08001128 <li>Adds <code>fdo_prefetch_hints</code> (if not present) feature to the top of the toolchain</li>
1129 <li>Adds <code>autofdo</code> (if not present) feature to the top of the toolchain</li>
1130 <li>Adds <code>build_interface_libraries</code> (if not present) feature to the top of the toolchain</li>
1131 <li>Adds <code>dynamic_library_linker_tool</code> (if not present) feature to the top of the toolchain</li>
1132 <li>Adds <code>symbol_counts</code> (if not present) feature to the top of the toolchain</li>
1133 <li>Adds <code>shared_flag</code> (if not present) feature to the top of the toolchain</li>
1134 <li>Adds <code>linkstamps</code> (if not present) feature to the top of the toolchain</li>
1135 <li>Adds <code>output_execpath_flags</code> (if not present) feature to the top of the toolchain</li>
1136 <li>Adds <code>runtime_library_search_directories</code> (if not present) feature to the top of the toolchain</li>
1137 <li>Adds <code>library_search_directories</code> (if not present) feature to the top of the toolchain</li>
1138 <li>Adds <code>archiver_flags</code> (if not present) feature to the top of the toolchain</li>
1139 <li>Adds <code>libraries_to_link</code> (if not present) feature to the top of the toolchain</li>
1140 <li>Adds <code>force_pic_flags</code> (if not present) feature to the top of the toolchain</li>
1141 <li>Adds <code>user_link_flags</code> (if not present) feature to the top of the toolchain</li>
1142 <li>Adds <code>legacy_link_flags</code> (if not present) feature to the top of the toolchain</li>
1143 <li>Adds <code>static_libgcc</code> (if not present) feature to the top of the toolchain</li>
1144 <li>Adds <code>fission_support</code> (if not present) feature to the top of the toolchain</li>
1145 <li>Adds <code>strip_debug_symbols</code> (if not present) feature to the top of the toolchain</li>
1146 <li>Adds <code>coverage</code> (if not present) feature to the top of the toolchain</li>
1147 <li>Adds <code>llvm_coverage_map_format</code> (if not present) feature to the top of the toolchain</li>
1148 <li>Adds <code>gcc_coverage_map_format</code> (if not present) feature to the top of the toolchain</li>
1149 <li>Adds <code>fully_static_link</code> (if not present) feature to the bottom of the toolchain</li>
1150 <li>Adds <code>user_compile_flags</code> (if not present) feature to the bottom of the toolchain</li>
1151 <li>Adds <code>sysroot</code> (if not present) feature to the bottom of the toolchain</li>
1152 <li>Adds <code>unfiltered_compile_flags</code> (if not present) feature to the bottom of the toolchain</li>
1153 <li>Adds <code>linker_param_file</code> (if not present) feature to the bottom of the toolchain</li>
1154 <li>Adds <code>compiler_input_flags</code> (if not present) feature to the bottom of the toolchain</li>
1155 <li>Adds <code>compiler_output_flags</code> (if not present) feature to the bottom of the toolchain</li>
1156 </ul>
1157</p>
1158
1159This is a long list of features. The plan is to get rid of them once
1160[Crosstool in Starlark](https://github.com/bazelbuild/bazel/issues/5380) is
1161done. For the curious reader see the implementation in
1162[CppActionConfigs](https://source.bazel.build/bazel/+/master:src/main/java/com/google/devtools/build/lib/rules/cpp/CppActionConfigs.java?q=cppactionconfigs&ss=bazel),
1163and for production toolchains consider adding `no_legacy_features` to make
1164the toolchain more standalone.
1165