Extensions > API reference > cc_common

cc_common

Utilities for C++ compilation, linking, and command line generation.

action_is_enabled

bool cc_common.action_is_enabled(feature_configuration, action_name)

Returns True if given action_config is enabled in the feature configuration.

Parameters

Parameter Description
feature_configuration

FeatureConfiguration

Feature configuration to be queried.

action_name

Name of the action_config.

CcToolchainInfo

Provider cc_common.CcToolchainInfo

The key used to retrieve the provider that contains information about the C++ toolchain being used

compile

tuple cc_common.compile(actions, feature_configuration, cc_toolchain, srcs=[], public_hdrs=[], private_hdrs=[], includes=[], quote_includes=[], system_includes=[], framework_includes=[], defines=[], local_defines=[], user_compile_flags=[], compilation_contexts=[], name, disallow_pic_outputs=False, disallow_nopic_outputs=False, additional_inputs=[])

Should be used for C++ compilation. Returns tuple of (CompilationContext, CcCompilationOutputs).

Parameters

Parameter Description
actions

actions

actions object.

feature_configuration

FeatureConfiguration

feature_configuration to be queried.

cc_toolchain

CcToolchainInfo

CcToolchainInfo provider to be used.

srcs

sequence

The list of source files to be compiled.

public_hdrs

sequence

List of headers needed for compilation of srcs and may be included by dependent rules transitively.

private_hdrs

sequence

List of headers needed for compilation of srcs and NOT to be included by dependent rules.

includes

sequence

Search paths for header files referenced both by angle bracket and quotes. Usually passed with -I. Propagated to dependents transitively.

quote_includes

sequence

Search paths for header files referenced by quotes, e.g. #include "foo/bar/header.h". They can be either relative to the exec root or absolute. Usually passed with -iquote. Propagated to dependents transitively.

system_includes

sequence

Search paths for header files referenced by angle brackets, e.g. #include <foo/bar/header.h>. They can be either relative to the exec root or absolute. Usually passed with -isystem. Propagated to dependents transitively.

framework_includes

sequence

Search paths for header files from Apple frameworks. They can be either relative to the exec root or absolute. Usually passed with -F. Propagated to dependents transitively.

defines

sequence

Set of defines needed to compile this target. Each define is a string. Propagated to dependents transitively.

local_defines

sequence

Set of defines needed to compile this target. Each define is a string. Not propagated to dependents transitively.

user_compile_flags

sequence

Additional list of compilation options.

compilation_contexts

sequence

Headers from dependencies used for compilation.

name

string

This is used for naming the output artifacts of actions created by this method.

disallow_pic_outputs

bool

Whether PIC outputs should be created.

disallow_nopic_outputs

bool

Whether NOPIC outputs should be created.

additional_inputs

sequence

List of additional files needed for compilation of srcs

configure_features

FeatureConfiguration cc_common.configure_features(ctx=None, cc_toolchain, requested_features=[], unsupported_features=[])

Creates a feature_configuration instance. Requires the cpp configuration fragment.

Parameters

Parameter Description
ctx

ctx

The rule context.

cc_toolchain

CcToolchainInfo

cc_toolchain for which we configure features.

requested_features

sequence

List of features to be enabled.

unsupported_features

sequence

List of features that are unsupported by the current rule.

create_cc_toolchain_config_info

CcToolchainConfigInfo cc_common.create_cc_toolchain_config_info(ctx, features=[], action_configs=[], artifact_name_patterns=[], cxx_builtin_include_directories=[], toolchain_identifier, host_system_name, target_system_name, target_cpu, target_libc, compiler, abi_version, abi_libc_version, tool_paths=[], make_variables=[], builtin_sysroot=None, cc_target_os=None)

Creates a CcToolchainConfigInfo provider

Parameters

Parameter Description
ctx

ctx

The rule context.

features

sequence

A list of features.

action_configs

sequence

A list of action_configs.

artifact_name_patterns

sequence

A list of artifact_name_patterns.

cxx_builtin_include_directories

sequence

Built-in include directories for C++ compilation. These should be the exact paths used by the compiler, and are generally relative to the exec root.

The paths used by the compiler can be determined by 'gcc -E -xc++ - -v'.

We currently use the C++ paths also for C compilation, which is safe as long as there are no name clashes between C++ and C header files.

Relative paths are resolved relative to the configuration file directory.

If the compiler has --sysroot support, then these paths should use %sysroot% rather than the include path, and specify the sysroot attribute in order to give bazel the information necessary to make the correct replacements.

toolchain_identifier

string

The unique identifier of the toolchain within the crosstool release. It must be possible to use this as a directory name in a path.

It has to match the following regex: [a-zA-Z_][\.\- \w]*

host_system_name

string

The system name which is required by the toolchain to run.

target_system_name

string

The GNU System Name.

target_cpu

string

The target architecture string.

target_libc

string

The libc version string (e.g. "glibc-2.2.2").

compiler

string

The compiler version string (e.g. "gcc-4.1.1").

abi_version

string

The abi in use, which is a gcc version. E.g.: "gcc-3.4"

abi_libc_version

string

The glibc version used by the abi we're using.

tool_paths

sequence

A list of tool_paths.

make_variables

sequence

A list of make_variables.

builtin_sysroot

string; or None

The built-in sysroot. If this attribute is not present, Bazel does not allow using a different sysroot, i.e. through the --grte_top option.

cc_target_os

string; or None

Internal purpose only, do not use.

create_compilation_context

CompilationContext cc_common.create_compilation_context(headers=unbound, system_includes=unbound, includes=unbound, quote_includes=unbound, framework_includes=unbound, defines=unbound, local_defines=unbound)

Creates a CompilationContext.

Parameters

Parameter Description
headers

Set of headers needed to compile this target

system_includes

Set of search paths for header files referenced by angle brackets, i.e. #include <foo/bar/header.h>. They can be either relative to the exec root or absolute. Usually passed with -isystem

includes

Set of search paths for header files referenced both by angle bracket and quotes.Usually passed with -I

quote_includes

Set of search paths for header files referenced by quotes, i.e. #include "foo/bar/header.h". They can be either relative to the exec root or absolute. Usually passed with -iquote

framework_includes

Set of framework search paths for header files (Apple platform only)

defines

Set of defines needed to compile this target. Each define is a string. Propagated transitively to dependents.

local_defines

Set of defines needed to compile this target. Each define is a string. Not propagated transitively to dependents.

create_compilation_outputs

CcCompilationOutputs cc_common.create_compilation_outputs(objects=None, pic_objects=None)

Create compilation outputs object.

Parameters

Parameter Description
objects

depset; or None

List of object files.

pic_objects

depset; or None

List of pic object files.

create_compile_variables

Variables cc_common.create_compile_variables(cc_toolchain, feature_configuration, source_file=None, output_file=None, user_compile_flags=None, include_directories=None, quote_include_directories=None, system_include_directories=None, framework_include_directories=None, preprocessor_defines=None, thinlto_index=None, thinlto_input_bitcode_file=None, thinlto_output_object_file=None, use_pic=False, add_legacy_cxx_options=False)

Returns variables used for compilation actions.

Parameters

Parameter Description
cc_toolchain

CcToolchainInfo

cc_toolchain for which we are creating build variables.

feature_configuration

FeatureConfiguration

Feature configuration to be queried.

source_file

Optional source file for the compilation. Please prefer passing source_file here over appending it to the end of the command line generated from cc_common.get_memory_inefficient_command_line, as then it's in the power of the toolchain author to properly specify and position compiler flags.

output_file

Optional output file of the compilation. Please prefer passing output_file here over appending it to the end of the command line generated from cc_common.get_memory_inefficient_command_line, as then it's in the power of the toolchain author to properly specify and position compiler flags.

user_compile_flags

None; or sequence

List of additional compilation flags (copts).

include_directories

None; or depset

Depset of include directories.

quote_include_directories

None; or depset

Depset of quote include directories.

system_include_directories

None; or depset

Depset of system include directories.

framework_include_directories

None; or depset

Depset of framework include directories.

preprocessor_defines

None; or depset

Depset of preprocessor defines.

thinlto_index

None; or string

LTO index file path.

thinlto_input_bitcode_file

None; or string

Bitcode file that is input to LTO backend.

thinlto_output_object_file

None; or string

Object file that is output by LTO backend.

use_pic

When true the compilation will generate position independent code.

add_legacy_cxx_options

Unused.

LibraryToLink cc_common.create_library_to_link(actions, feature_configuration, cc_toolchain, static_library=None, pic_static_library=None, dynamic_library=None, interface_library=None, alwayslink=False, dynamic_library_symlink_path='', interface_library_symlink_path='')

Creates LibraryToLink

Parameters

Parameter Description
actions

actions

actions object.

feature_configuration

FeatureConfiguration

feature_configuration to be queried.

cc_toolchain

CcToolchainInfo

CcToolchainInfo provider to be used.

static_library

File

File of static library to be linked.

pic_static_library

File

File of pic static library to be linked.

dynamic_library

File

File of dynamic library to be linked. Always used for runtime and used for linking if interface_library is not passed.

interface_library

File

File of interface library to be linked.

Whether to link the static library/objects in the --whole_archive block.

string

Override the default path of the dynamic library link in the solib directory. Empty string to use the default.

string

Override the default path of the interface library link in the solib directory. Empty string to use the default.

Variables cc_common.create_link_variables(cc_toolchain, feature_configuration, library_search_directories=None, runtime_library_search_directories=None, user_link_flags=None, output_file=None, param_file=None, def_file=None, is_using_linker=True, is_linking_dynamic_library=False, must_keep_debug=True, use_test_only_flags=False, is_static_linking_mode=True)

Returns link variables used for linking actions.

Parameters

Parameter Description

CcToolchainInfo

cc_toolchain for which we are creating build variables.

FeatureConfiguration

Feature configuration to be queried.

None; or depset

Depset of directories where linker will look for libraries at link time.

None; or depset

Depset of directories where loader will look for libraries at runtime.

None; or sequence

List of additional link flags (linkopts).

Optional output file path.

Optional param file path.

Optional .def file path.

True when using linker, False when archiver. Caller is responsible for keeping this in sync with action name used (is_using_linker = True for linking executable or dynamic library, is_using_linker = False for archiving static library).

True when creating dynamic library, False when executable or static library. Caller is responsible for keeping this in sync with action name used. This field will be removed once b/65151735 is fixed.

When set to True, bazel will expose 'strip_debug_symbols' variable, which is usually used to use the linker to strip debug symbols from the output file.

When set to true, 'is_cc_test' variable will be set.

Unused.

create_linker_input

LinkerInput cc_common.create_linker_input(owner, libraries=None, user_link_flags=None, additional_inputs=None)

Creates a LinkingContext.

Parameters

Parameter Description
owner

Label

The label of the target that produced all files used in this input.

libraries

None; or depset

List of LibraryToLink.

None; or depset

List of user link flags passed as strings.

additional_inputs

None; or depset

For additional inputs to the linking action, e.g.: linking scripts.

create_linking_context

LinkingContext cc_common.create_linking_context(linker_inputs=None, libraries_to_link=None, user_link_flags=None, additional_inputs=None)

Creates a LinkingContext.

Parameters

Parameter Description
linker_inputs

None; or depset

Depset of LinkerInput.

None; or sequence

Deprecated. This parameter is deprecated and will be removed soon. Please do not depend on it. It is disabled with --incompatible_require_linker_input_cc_api. Use this flag to verify your code is compatible with its imminent removal.
List of LibraryToLink.

None; or sequence

Deprecated. This parameter is deprecated and will be removed soon. Please do not depend on it. It is disabled with --incompatible_require_linker_input_cc_api. Use this flag to verify your code is compatible with its imminent removal.
List of user link flags passed as strings.

additional_inputs

None; or sequence

Deprecated. This parameter is deprecated and will be removed soon. Please do not depend on it. It is disabled with --incompatible_require_linker_input_cc_api. Use this flag to verify your code is compatible with its imminent removal.
For additional inputs to the linking action, e.g.: linking scripts.

create_linking_context_from_compilation_outputs

tuple cc_common.create_linking_context_from_compilation_outputs(actions, feature_configuration, cc_toolchain, compilation_outputs, user_link_flags=[], linking_contexts=[], name, language='c++', alwayslink=False, additional_inputs=[], disallow_static_libraries=False, disallow_dynamic_library=False, grep_includes=None)

Should be used for creating library rules that can propagate information downstream in order to be linked later by a top level rule that does transitive linking to create an executable or dynamic library. Returns tuple of (CcLinkingContext, CcLinkingOutputs).

Parameters

Parameter Description
actions

actions

actions object.

feature_configuration

FeatureConfiguration

feature_configuration to be queried.

cc_toolchain

CcToolchainInfo

CcToolchainInfo provider to be used.

compilation_outputs

CcCompilationOutputs

Compilation outputs containing object files to link.

sequence

Additional list of linking options.

linking_contexts

sequence

Libraries from dependencies. These libraries will be linked into the output artifact of the link() call, be it a binary or a library.

name

string

This is used for naming the output artifacts of actions created by this method.

language

string

Only C++ supported for now. Do not use this parameter.

bool

Whether this library should always be linked.

additional_inputs

sequence

For additional inputs to the linking action, e.g.: linking scripts.

disallow_static_libraries

bool

Whether static libraries should be created.

disallow_dynamic_library

bool

Whether a dynamic library should be created.

grep_includes

File; or None

do_not_use_tools_cpp_compiler_present

None cc_common.do_not_use_tools_cpp_compiler_present

Do not use this field, its only puprose is to help with migration from config_setting.values{'compiler') to config_settings.flag_values{'@bazel_tools//tools/cpp:compiler'}

get_environment_variables

dict cc_common.get_environment_variables(feature_configuration, action_name, variables)

Returns environment variables to be set for given action.

Parameters

Parameter Description
feature_configuration

FeatureConfiguration

Feature configuration to be queried.

action_name

Name of the action. Has to be one of the names in @bazel_tools//tools/build_defs/cc:action_names.bzl (https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/cc/action_names.bzl)

variables

Variables

Build variables to be used for template expansion.

get_execution_requirements

sequence cc_common.get_execution_requirements(feature_configuration, action_name)

Returns execution requirements for given action.

Parameters

Parameter Description
feature_configuration

FeatureConfiguration

Feature configuration to be queried.

action_name

Name of the action. Has to be one of the names in @bazel_tools//tools/build_defs/cc:action_names.bzl (https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/cc/action_names.bzl)

get_memory_inefficient_command_line

sequence cc_common.get_memory_inefficient_command_line(feature_configuration, action_name, variables)

Returns flattened command line flags for given action, using given variables for expansion. Flattens nested sets and ideally should not be used, or at least should not outlive analysis. Work on memory efficient function returning Args is ongoing.

Parameters

Parameter Description
feature_configuration

FeatureConfiguration

Feature configuration to be queried.

action_name

Name of the action. Has to be one of the names in @bazel_tools//tools/build_defs/cc:action_names.bzl (https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/cc/action_names.bzl)

variables

Variables

Build variables to be used for template expansions.

get_tool_for_action

string cc_common.get_tool_for_action(feature_configuration, action_name)

Returns tool path for given action.

Parameters

Parameter Description
feature_configuration

FeatureConfiguration

Feature configuration to be queried.

action_name

Name of the action. Has to be one of the names in @bazel_tools//tools/build_defs/cc:action_names.bzl (https://github.com/bazelbuild/bazel/blob/master/tools/build_defs/cc/action_names.bzl)

is_enabled

bool cc_common.is_enabled(feature_configuration, feature_name)

Returns True if given feature is enabled in the feature configuration.

Parameters

Parameter Description
feature_configuration

FeatureConfiguration

Feature configuration to be queried.

feature_name

Name of the feature.

CcLinkingOutputs cc_common.link(actions, feature_configuration, cc_toolchain, compilation_outputs=None, user_link_flags=[], linking_contexts=[], name, language='c++', output_type='executable', link_deps_statically=True, stamp=0, additional_inputs=[], grep_includes=None)

Should be used for C++ transitive linking.

Parameters

Parameter Description
actions

actions

actions object.

feature_configuration

FeatureConfiguration

feature_configuration to be queried.

cc_toolchain

CcToolchainInfo

CcToolchainInfo provider to be used.

compilation_outputs

CcCompilationOutputs; or None

Compilation outputs containing object files to link.

sequence

Additional list of linker options.

linking_contexts

sequence

Linking contexts from dependencies to be linked into the linking context generated by this rule.

name

string

This is used for naming the output artifacts of actions created by this method.

language

string

Only C++ supported for now. Do not use this parameter.

output_type

string

Can be either 'executable' or 'dynamic_library'.

bool

True to link dependencies statically, False dynamically.

stamp

int

Whether to include build information in the linked executable, if output_type is 'executable'. If 1, build information is always included. If 0 (the default build information is always excluded. If -1, uses the default behavior, which may be overridden by the --[no]stamp flag. This should be unset (or set to 0) when generating the executable output for test rules.

additional_inputs

sequence

For additional inputs to the linking action, e.g.: linking scripts.

grep_includes

File; or None

merge_cc_infos

CcInfo cc_common.merge_cc_infos(direct_cc_infos=[], cc_infos=[])

Merges multiple CcInfos into one.

Parameters

Parameter Description
direct_cc_infos

sequence

List of CcInfos to be merged, whose headers will be exported by the direct fields in the returned provider.

cc_infos

sequence

List of CcInfos to be merged, whose headers will not be exported by the direct fields in the returned provider.

merge_compilation_outputs

CcCompilationOutputs cc_common.merge_compilation_outputs(compilation_outputs=[])

Merge compilation outputs.

Parameters

Parameter Description
compilation_outputs

sequence