Skip to main content
Utilities for C++ compilation, linking, and command line generation.

Members

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

ParameterDescriptionfeature_configurationFeatureConfiguration; required Feature configuration to be queried. action_namestring; required 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=[], include_prefix='', strip_include_prefix='', user_compile_flags=[], conly_flags=[], cxx_flags=[], compilation_contexts=[], name, disallow_pic_outputs=False, disallow_nopic_outputs=False, additional_inputs=[], module_interfaces=unbound)
Should be used for C++ compilation. Returns tuple of ( CompilationContext, CcCompilationOutputs).

Parameters

ParameterDescriptionactionsactions; required actions object. feature_configurationFeatureConfiguration; required feature_configuration to be queried. cc_toolchain Info; required CcToolchainInfo provider to be used. srcssequence; default is [] The list of source files to be compiled. public_hdrssequence; default is [] List of headers needed for compilation of srcs and may be included by dependent rules transitively. private_hdrssequence; default is [] List of headers needed for compilation of srcs and NOT to be included by dependent rules. includessequence; or depset; default is [] Search paths for header files referenced both by angle bracket and quotes. Usually passed with -I. Propagated to dependents transitively. quote_includessequence; default is [] 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_includessequence; default is [] 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_includessequence; default is [] 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. definessequence; default is [] Set of defines needed to compile this target. Each define is a string. Propagated to dependents transitively. local_definessequence; default is [] Set of defines needed to compile this target. Each define is a string. Not propagated to dependents transitively. include_prefixstring; default is '' The prefix to add to the paths of the headers of this rule. When set, the headers in the hdrs attribute of this rule are accessible at is the value of this attribute prepended to their repository-relative path. The prefix in the strip_include_prefix attribute is removed before this prefix is added. strip_include_prefixstring; default is '' The prefix to strip from the paths of the headers of this rule. When set, the headers in the hdrs attribute of this rule are accessible at their path with this prefix cut off. If it’s a relative path, it’s taken as a package-relative one. If it’s an absolute one, it’s understood as a repository-relative path. The prefix in the include_prefix attribute is added after this prefix is stripped. user_compile_flagssequence; default is [] Additional list of compilation options. conly_flagssequence; default is [] Additional list of compilation options for C compiles. cxx_flagssequence; default is [] Additional list of compilation options for C++ compiles. compilation_contextssequence; default is [] Headers from dependencies used for compilation. namestring; required This is used for naming the output artifacts of actions created by this method. See also the `main_output` arg. disallow_pic_outputsbool; default is False Whether PIC outputs should be created. disallow_nopic_outputsbool; default is False Whether NOPIC outputs should be created. additional_inputssequence; default is [] List of additional files needed for compilation of srcs module_interfacessequence; default is unbound The list of module interfaces source files to be compiled. Note: this is an experimental feature, only enabled with —experimental_cpp_modules

configure_features

FeatureConfiguration cc_common.configure_features(*, ctx, cc_toolchain, language=None, requested_features=[], unsupported_features=[])
Creates a feature_configuration instance. Requires the cpp configuration fragment.

Parameters

ParameterDescriptionctxctx; required The rule context. cc_toolchain Info; required cc_toolchain for which we configure features. languagestring; or None; default is None The language to configure for: either c++ or objc (default c++) requested_featuressequence; default is [] List of features to be enabled. unsupported_featuressequence; default is [] List of features that are unsupported by the current rule.

create_cc_toolchain_config_info

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

Parameters

ParameterDescriptionctxctx; required The rule context. featuressequence; default is [] Contains all flag specifications for one feature. Arguments: name: The feature’s name. It is possible to introduce a feature without a change to Bazel by adding a ‘feature’ section to the toolchain and adding the corresponding string as feature in the BUILD file. enabled: If ‘True’, this feature is enabled unless a rule type explicitly marks it as unsupported. flag_sets: A FlagSet list. If the given feature is enabled, the flag sets will be applied for the actions are specified for. env_sets: an EnvSet list. If the given feature is enabled, the env sets will be applied for the actions they are specified for. requires: A list of feature sets defining when this feature is supported by the toolchain. The feature is supported if any of the feature sets fully apply, that is, when all features of a feature set are enabled. If requires is omitted, the feature is supported independently of which other features are enabled. Use this for example to filter flags depending on the build mode enabled (opt / fastbuild / dbg). implies: A string list of features or action configs that are automatically enabled when this feature is enabled. If any of the implied features or action configs cannot be enabled, this feature will (silently) not be enabled either. provides: A list of names this feature conflicts with. A feature cannot be enabled if: - provides contains the name of a different feature or action config that we want to enable. - provides contains the same value as a ‘provides’ in a different feature or action config that we want to enable. Use this in order to ensure that incompatible features cannot be accidentally activated at the same time, leading to hard to diagnose compiler errors. action_configssequence; default is [] An action config corresponds to a Bazel action, and allows selection of a tool based on activated features. Action config activation occurs by the same semantics as features: a feature can ‘require’ or ‘imply’ an action config in the same way that it would another feature. Arguments: action_name: The name of the Bazel action that this config applies to, e.g. ‘c-compile’ or ‘c-module-compile’. enabled: If ‘True’, this action is enabled unless a rule type explicitly marks it as unsupported. tools: The tool applied to the action will be the first tool with a feature set that matches the feature configuration. An error will be thrown if no tool matches a provided feature configuration - for that reason, it’s a good idea to provide a default tool with an empty feature set. flag_sets: If the given action config is enabled, the flag sets will be applied to the corresponding action. implies: A list of features or action configs that are automatically enabled when this action config is enabled. If any of the implied features or action configs cannot be enabled, this action config will (silently) not be enabled either. artifact_name_patternssequence; default is [] The name for an artifact of a given category of input or output artifacts to an action. Arguments: category_name: The category of artifacts that this selection applies to. This field is compared against a list of categories defined in Bazel. Example categories include “linked_output” or the artifact for this selection. Together with the extension it is used to create an artifact name based on the target name. extension: The extension for creating the artifact for this selection. Together with the prefix it is used to create an artifact name based on the target name. cxx_builtin_include_directoriessequence; default is [] 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 blaze the information necessary to make the correct replacements. toolchain_identifierstring; required 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_namestring; or None; default is None Ignored. target_system_namestring; or None; default is None Deprecated. The GNU System Name. The string is exposed to CcToolchainInfo.target_gnu_system_name. target_cpustring; or None; default is None Deprecated: Use cpu based constraints instead. If the string is “k8”, `target_cpu` will be omitted from the filename of raw FDO profile data. target_libcstring; or None; default is None Deprecated: Use OS based constraints instead. The libc version string (e.g. “glibc-2.2.2”). If the string is “macosx”, platform is assumed to be MacOS. Otherwise, Linux. The string is exposed to CcToolchainInfo.libc. compilerstring; required The compiler string (e.g. “gcc”). The current toolchain’s compiler is exposed to `@bazel_tools//tools/cpp:compiler (compiler_flag)` as a flag value. Targets that require compiler-specific flags can use the config_settings in https://github.com/bazelbuild/rules\_cc/blob/main/cc/compiler/BUILD in select() statements or create custom config_setting if the existing settings don’t suffice. abi_versionstring; or None; default is None The abi in use, which is a gcc version. E.g.: “gcc-3.4”. The string is set to C++ toolchain variable ABI. abi_libc_versionstring; or None; default is None The glibc version used by the abi we’re using. The string is set to C++ toolchain variable ABI_LIBC_VERSION. tool_pathssequence; default is [] Tool locations. Arguments: name: Name of the tool. path: Location of the tool; Can be absolute path (in case of non hermetic toolchain), or path relative to the cc_toolchain’s package. make_variablessequence; default is [] A make variable that is made accessible to rules. builtin_sysrootstring; or None; default is 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.

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

ParameterDescriptionheaders default is unbound Set of headers needed to compile this target system_includes default is unbound 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 default is unbound Set of search paths for header files referenced both by angle bracket and quotes.Usually passed with -I quote_includes default is unbound 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 default is unbound Set of framework search paths for header files (Apple platform only) defines default is unbound Set of defines needed to compile this target. Each define is a string. Propagated transitively to dependents. local_defines default is unbound 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

ParameterDescriptionobjectsdepset; or None; default is None List of object files. pic_objectsdepset; or None; default is 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, variables_extension=unbound)
Returns variables used for compilation actions.

Parameters

ParameterDescriptioncc_toolchain Info; required cc_toolchain for which we are creating build variables. feature_configurationFeatureConfiguration; required Feature configuration to be queried. source_fileFile; or string; or None; default is None Optional source file path 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_fileFile; or string; or None; default is None Optional output file path 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_flagssequence of string s; or None; default is None List of additional compilation flags (copts). include_directoriesdepset; or None; default is None Depset of include directories. quote_include_directoriesdepset; or None; default is None Depset of quote include directories. system_include_directoriesdepset; or None; default is None Depset of system include directories. framework_include_directoriesdepset; or None; default is None Depset of framework include directories. preprocessor_definesdepset; or None; default is None Depset of preprocessor defines. thinlto_indexstring; or None; default is None LTO index file path. thinlto_input_bitcode_filestring; or None; default is None Bitcode file that is input to LTO backend. thinlto_output_object_filestring; or None; default is None Object file that is output by LTO backend. use_picbool; default is False When true the compilation will generate position independent code. add_legacy_cxx_optionsbool; default is False Unused. variables_extensiondict; default is unbound A dictionary of additional variables used by compile actions.
LibraryToLink cc_common.create_library_to_link(*, actions, feature_configuration=None, cc_toolchain=None, static_library=None, pic_static_library=None, dynamic_library=None, interface_library=None, pic_objects=unbound, objects=unbound, alwayslink=False, dynamic_library_symlink_path='', interface_library_symlink_path='')
Creates LibraryToLink

Parameters

ParameterDescriptionactions required actions object. feature_configuration default is None feature_configuration to be queried. cc_toolchain default is None CcToolchainInfo provider to be used. static_libraryFile; or None; default is None File of static library to be linked. pic_static_libraryFile; or None; default is None File of pic static library to be linked. dynamic_libraryFile; or None; default is None File of dynamic library to be linked. Always used for runtime and used for linking if interface_library is not passed. interface_libraryFile; or None; default is None File of interface library to be linked. pic_objectssequence of File s; default is unbound Experimental, do not use objectssequence of File s; default is unbound Experimental, do not use alwayslinkbool; default is False Whether to link the static library/objects in the —whole_archive block. dynamic_library_symlink_pathstring; default is '' Override the default path of the dynamic library link in the solib directory. Empty string to use the default. interface_library_symlink_pathstring; default is '' 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=[], runtime_library_search_directories=[], user_link_flags=[], output_file=None, param_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

ParameterDescriptioncc_toolchain Info; required cc_toolchain for which we are creating build variables. feature_configurationFeatureConfiguration; required Feature configuration to be queried. library_search_directoriesdepset; default is [] Depset of directories where linker will look for libraries at link time. runtime_library_search_directoriesdepset; default is [] Depset of directories where loader will look for libraries at runtime. user_link_flagssequence; default is [] List of additional link flags (linkopts). output_file default is None Optional output file path. param_file default is None Optional param file path. is_using_linkerbool; default is True 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). is_linking_dynamic_librarybool; default is False 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. must_keep_debugbool; default is True When set to False, bazel will expose ‘strip_debug_symbols’ variable, which is usually used to use the linker to strip debug symbols from the output file. use_test_only_flagsbool; default is False When set to true, ‘is_cc_test’ variable will be set. is_static_linking_modebool; default is True Unused.

create_linker_input

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

Parameters

ParameterDescriptionownerLabel; required The label of the target that produced all files used in this input. libraries``None; or depset; default is None List of LibraryToLink. user_link_flags``None; or depset of string s; or sequence of string s; default is None User link flags passed as strings. Accepts either [String], [[String]] or depset(String). The latter is discouraged as it’s only kept for compatibility purposes, the depset is flattened. If you want to propagate user_link_flags via unflattened depsets() wrap them in a LinkerInput so that they are not flattened till the end. additional_inputs``None; or depset; default is None For additional inputs to the linking action, e.g.: linking scripts.

create_linking_context

LinkingContext cc_common.create_linking_context(*, linker_inputs)
Creates a LinkingContext.

Parameters

ParameterDescriptionlinker_inputsdepset; required Depset of LinkerInput.

create_linking_context_from_compilation_outputs

tuple cc_common.create_linking_context_from_compilation_outputs(*, actions, name, feature_configuration, cc_toolchain, language='c++', disallow_static_libraries=False, disallow_dynamic_library=False, compilation_outputs, linking_contexts=[], user_link_flags=[], alwayslink=False, additional_inputs=[], variables_extension=unbound)
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 a dynamic library. Returns tuple of ( CcLinkingContext, CcLinkingOutputs).

Parameters

ParameterDescriptionactionsactions; required actions object. namestring; required This is used for naming the output artifacts of actions created by this method. feature_configurationFeatureConfiguration; required feature_configuration to be queried. cc_toolchain Info; required CcToolchainInfo provider to be used. languagestring; default is 'c++' Only C++ supported for now. Do not use this parameter. disallow_static_librariesbool; default is False Whether static libraries should be created. disallow_dynamic_librarybool; default is False Whether a dynamic library should be created. compilation_outputsCcCompilationOutputs; required Compilation outputs containing object files to link. linking_contextssequence; default is [] Libraries from dependencies. These libraries will be linked into the output artifact of the link() call, be it a binary or a library. user_link_flagssequence; default is [] Additional list of linking options. alwayslinkbool; default is False Whether this library should always be linked. additional_inputssequence; default is [] For additional inputs to the linking action, e.g.: linking scripts. variables_extensiondict; default is unbound Additional variables to pass to the toolchain configuration when creating link command line.

create_lto_compilation_context

LtoCompilationContext cc_common.create_lto_compilation_context(*, objects={})
Create LTO compilation context

Parameters

ParameterDescriptionobjectsdict; default is {} map of full object to index object

do_not_use_tools_cpp_compiler_present

None cc_common.do_not_use_tools_cpp_compiler_present
Do not use this field, its only purpose 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

ParameterDescriptionfeature_configurationFeatureConfiguration; required Feature configuration to be queried. action_namestring; required 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; required 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

ParameterDescriptionfeature_configurationFeatureConfiguration; required Feature configuration to be queried. action_namestring; required 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

ParameterDescriptionfeature_configurationFeatureConfiguration; required Feature configuration to be queried. action_namestring; required 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; required 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

ParameterDescriptionfeature_configurationFeatureConfiguration; required Feature configuration to be queried. action_namestring; required 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

ParameterDescriptionfeature_configurationFeatureConfiguration; required Feature configuration to be queried. feature_namestring; required Name of the feature.
CcLinkingOutputs cc_common.link(*, actions, name, feature_configuration, cc_toolchain, language='c++', output_type='executable', link_deps_statically=True, compilation_outputs=None, linking_contexts=[], user_link_flags=[], stamp=0, additional_inputs=[], additional_outputs=[], variables_extension={})
Should be used for C++ transitive linking.

Parameters

ParameterDescriptionactionsactions; required actions object. namestring; required This is used for naming the output artifacts of actions created by this method. feature_configurationFeatureConfiguration; required feature_configuration to be queried. cc_toolchain Info; required CcToolchainInfo provider to be used. languagestring; default is 'c++' Only C++ supported for now. Do not use this parameter. output_typestring; default is 'executable' Can be either ‘executable’ or ‘dynamic_library’. link_deps_staticallybool; default is True True to link dependencies statically, False dynamically. compilation_outputsCcCompilationOutputs; or None; default is None Compilation outputs containing object files to link. linking_contextssequence; default is [] Linking contexts from dependencies to be linked into the linking context generated by this rule. user_link_flagssequence; default is [] Additional list of linker options. stampint; default is 0 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_inputssequence; or depset; default is [] For additional inputs to the linking action, e.g.: linking scripts. additional_outputssequence; default is [] For additional outputs to the linking action, e.g.: map files. variables_extensiondict; default is {} Additional variables to pass to the toolchain configuration when create link command line.

merge_cc_infos

unknown cc_common.merge_cc_infos(*, direct_cc_infos=[], cc_infos=[])
Merges multiple CcInfo s into one.

Parameters

ParameterDescriptiondirect_cc_infossequence; default is [] List of CcInfo s to be merged, whose headers will be exported by the direct fields in the returned provider. cc_infossequence; default is [] List of CcInfo s to be merged, whose headers will not be exported by the direct fields in the returned provider.

merge_compilation_contexts

CompilationContext cc_common.merge_compilation_contexts(*, compilation_contexts=[])
Merges multiple CompilationContexts s into one.

Parameters

ParameterDescriptioncompilation_contextssequence; default is [] List of CompilationContexts s to be merged. The headers of each context will 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

ParameterDescriptioncompilation_outputssequence; default is []