Skip to main content

Rules

cc_binary

View rule sourceopen_in_new

Implicit output targets

  • name.stripped (only built if explicitly requested): A stripped version of the binary. strip -g is run on the binary to remove debug symbols. Additional strip options can be provided on the command line using --stripopt=-foo. This output is only built if explicitly requested.
  • name.dwp (only built if explicitly requested): If Fission is enabled: a debug information package file suitable for debugging remotely deployed binaries. Else: an empty file.

Arguments

cc_import

View rule sourceopen_in_new
cc_import rules allows users to import precompiled C/C++ libraries. The following are the typical use cases:
  1. Linking a static library
  1. Linking a shared library (Unix)
  1. Linking a shared library with interface library (Windows)
  1. Linking a shared library with system_provided=True (Windows)
  1. Linking to static or shared library On Unix:
On Windows:
cc_import supports an include attribute. For example:

Arguments

cc_library

View rule sourceopen_in_new

Header inclusion checking

All header files that are used in the build must be declared in the hdrs or srcs of cc_* rules. This is enforced. For cc_library rules, headers in hdrs comprise the public interface of the library and can be directly included both from the files in hdrs and srcs of the library itself as well as from files in hdrs and srcs of cc_* rules that list the library in their deps. Headers in srcs must only be directly included from the files in hdrs and srcs of the library itself. When deciding whether to put a header into hdrs or srcs, you should ask whether you want consumers of this library to be able to directly include it. This is roughly the same decision as between public and private visibility in programming languages. cc_binary and cc_test rules do not have an exported interface, so they also do not have a hdrs attribute. All headers that belong to the binary or test directly should be listed in the srcs. To illustrate these rules, look at the following example.
The allowed direct inclusions in this example are listed in the table below. For example foo.cc is allowed to directly include foo.h and bar.h, but not baz.h. The inclusion checking rules only apply to direct inclusions. In the example above foo.cc is allowed to include bar.h, which may include baz.h, which in turn is allowed to include baz-impl.h. Technically, the compilation of a .cc file may transitively include any header file in the hdrs or srcs in any cc_library in the transitive deps closure. In this case the compiler may read baz.h and baz-impl.h when compiling foo.cc, but foo.cc must not contain #include "baz.h". For that to be allowed, baz must be added to the deps of foo. Bazel depends on toolchain support to enforce the inclusion checking rules. The layering_check feature has to be supported by the toolchain and requested explicitly, for example via the --features=layering_check command-line flag or the features parameter of the package function. The toolchains provided by Bazel only support this feature with clang on Unix and macOS.

Arguments

cc_proto_library

View rule sourceopen_in_new
cc_proto_library generates C++ code from .proto files. deps must point to proto_library rules. Example:

Arguments

cc_shared_library

View rule sourceopen_in_new
It produces a shared library.

Example

In the example foo_shared statically links foo and baz, the latter being a transitive dependency. It doesn’t link bar because it is already provided dynamically by the dynamic_dep bar_shared. foo_shared uses a linker script *.lds file to control which symbols should be exported. The cc_shared_library rule logic does not control which symbols get exported, it only uses what is assumed to be exported to give errors during analysis phase if two shared libraries export the same targets. Every direct dependency of cc_shared_library is assumed to be exported. Therefore, Bazel assumes during analysis that foo is being exported by foo_shared. baz is not assumed to be exported by foo_shared. Every target matched by the exports_filter is also assumed to be exported. Every single cc_library in the example should appear at most in one cc_shared_library. If we wanted to link baz also into bar_shared we would need to add tags = ["LINKABLE_MORE_THAN_ONCE"] to baz. Due to the shared_lib_name attribute, the file produced by bar_shared will have the name bar.so as opposed to the name libbar.so that it would have by default on Linux.

Errors

Two shared libraries in dependencies export the same symbols.
This will happen whenever you are creating a target with two different cc_shared_library dependencies that export the same target. To fix this you need to stop the libraries from being exported in one of the cc_shared_library dependencies.
Two shared libraries in dependencies link the same library statically
This will happen whenever you are creating a new cc_shared_library with two different cc_shared_library dependencies that link the same target statically. Similar to the error with exports. One way to fix this is to stop linking the library into one of the cc_shared_library dependencies. At the same time, the one that still links it needs to export the library so that the one not linking it keeps visibility to the symbols. Another way is to pull out a third library that exports the target. A third way is to tag the culprit cc_library with LINKABLE_MORE_THAN_ONCE but this fix should be rare and you should absolutely make sure that the cc_library is indeed safe to link more than once.
'//foo:foo' is already linked statically in '//bar:bar' but not exported`
This means that a library in the transitive closure of your deps is reachable without going through one of the cc_shared_library dependencies but is already linked into a different cc_shared_library in dynamic_deps and is not exported. The solution is to export it from the cc_shared_library dependency or pull out a third cc_shared_library that exports it.
Do not place libraries which only contain a precompiled dynamic library in deps.
If you have a precompiled dynamic library, this doesn’t need to and cannot be linked statically into the current cc_shared_library target that you are currently creating. Therefore, it doesn’t belong in deps of the cc_shared_library. If this precompiled dynamic library is a dependency of one of your cc_libraries, then the cc_library needs to depend on it directly.
Trying to export a library already exported by a different shared library
You will see this error if on the current rule you are claiming to export a target that is already being exported by one of your dynamic dependencies. To fix this, remove the target from deps and just rely on it from the dynamic dependency or make sure that the exports_filter doesn’t catch this target.

Arguments

cc_static_library

View rule sourceopen_in_new
Produces a static library from a list of targets and their transitive dependencies. The resulting static library contains the object files of the targets listed in deps as well as their transitive dependencies, with preference given to PIC objects.

Output groups

linkdeps
A text file containing the labels of those transitive dependencies of targets listed in deps that did not contribute any object files to the static library, but do provide at least one static, dynamic or interface library. The resulting static library may require these libraries to be available at link time.
linkopts
A text file containing the user-provided linkopts of all transitive dependencies of targets listed in deps.

Duplicate symbols

By default, the cc_static_library rule checks that the resulting static library does not contain any duplicate symbols. If it does, the build fails with an error message that lists the duplicate symbols and the object files containing them. This check can be disabled per target or per package by setting features = ["-symbol_check"] or globally via --features=-symbol_check.
Toolchain support for symbol_check
The auto-configured C++ toolchains shipped with Bazel support the symbol_check feature on all platforms. Custom toolchains can add support for it in one of two ways:
  • Implementing the ACTION_NAMES.validate_static_library action and enabling it with the symbol_check feature. The tool set in the action is invoked with two arguments, the static library to check for duplicate symbols and the path of a file that must be created if the check passes.
  • Having the symbol_check feature add archiver flags that cause the action creating the static library to fail on duplicate symbols.

Arguments

fdo_prefetch_hints

View rule sourceopen_in_new
Represents an FDO prefetch hints profile that is either in the workspace or at a specified absolute path. Examples:

Arguments

fdo_profile

View rule sourceopen_in_new
Represents an FDO profile that is either in the workspace or at a specified absolute path. Examples:

Arguments

memprof_profile

View rule sourceopen_in_new
Represents a MEMPROF profile that is either in the workspace or at a specified absolute path. Examples:

Arguments

propeller_optimize

View rule sourceopen_in_new
Represents a Propeller optimization profile in the workspace. Example:

Arguments

cc_test

View rule sourceopen_in_new

Arguments

cc_toolchain

View rule sourceopen_in_new
Represents a C++ toolchain. This rule is responsible for:
  • Collecting all artifacts needed for C++ actions to run. This is done by attributes such as all_files, compiler_files, linker_files, or other attributes ending with _files). These are most commonly filegroups globbing all required files.
  • Generating correct command lines for C++ actions. This is done using CcToolchainConfigInfo provider (details below).
Use toolchain_config attribute to configure the C++ toolchain. See also this page for elaborate C++ toolchain configuration and toolchain selection documentation. Use tags = ["manual"] in order to prevent toolchains from being built and configured unnecessarily when invoking bazel build //...

Arguments

cc_toolchain_suite

View rule sourceopen_in_new
Represents a collections of C++ toolchains. This rule is responsible for:
  • Collecting all relevant C++ toolchains.
  • Selecting one toolchain depending on --cpu and --compiler options passed to Bazel.
See also this page for elaborate C++ toolchain configuration and toolchain selection documentation.

Arguments