@rules_cc_hdrs_map@rules_cc_hdrs_map//cc_hdrs_map:defs.bzl
rules_cc_hdrs_map
This project extends Bazel CC build capabilities with headers map implementation (allowing for easy support for most bizzare include path schemes).
In addition, it exposes CC compilation and linking functions in form of Bazel subrules.
See examples for how to use rules_cc_hdrs_map (and why).
Shortest possible example
$ cat foo.hpp
const std::string GREETINGS = "Hello";
$ cat foo.cpp
#include "bar/foo.h"
...
$ cat BUILD.bazel
load("@rules_cc_hdrs_map//cc_hdrs_map:defs.bzl", "cc_bin")
cc_bin(
name = "foo",
srcs = [
"foo.cpp",
"foo.hpp",
],
hdrs_map = {
"**/foo.hpp": ["bar/{filename}"],
}
)
Table of contents
- What issue is being addressed?
- How the issue is being addressed?
- What issue is being addressed?
- Rules
- HdrsMapInfo provider
What issue is being addressed?
Creation of arbitrary include paths from existing sources.
Scenario: we want to build a C/CPP codebase with Bazel.
One of its key characteristics is that most of the include statements do not reflect the code structure in the project - for example, header file located under path “name/a.hpp” is never included as “name/a.hpp”, instead an arbitrary list of aliases is used in the code (“x/y/z/a.hpp”, “b.hpp” etc.). There is no overarching convention that could be used to generalize those statements into another file file hierarchy - in other words, every header file is a special case of its own.
Unfortunately we are forbidden from modifying the code itself and the directory structure (hello from enterprise word).
As Bazel rules_cc have the expectation of header files being included in a way that resembles the file structure in the WORKSPACE (and one can only provide single “include prefix” per library), we need to prepare the “expected file structure” before passing them into the rules_cc.
In the most naive approach, said “expected file structure” is being prepared for each compilable target (copying over files), passing on the already created structure to targets that depend on it. Very quickly conflicts occur and change of a single header file may cascade into rebuilding hundreds of targets.
There has to be a better way!
How the issue is being addressed?
The concept of header map is introduced - it is a dictionary, containing mapping between simple glob paths and their desired include paths. For example: “**/a.hpp”: “x/y/z/a.hpp” expresses the intent to import any header file with name “a.hpp”, present during compilation , as “x/y/z/a.hpp”.
Said header map is propagated across all compatible C/C++ rules (meaning those from this WORKSPACE) and is being merged with all other header maps present.
No action is being performed up until the moment of compilation - header mappings, resulting from the header map dictionary, are created only for the purposes of compilation and are NOT part of any rule output. This ensures the impact for the Bazel cache is minimal and the compatibility with original rules_cc.
Functions & Macros
providers_helper.materialize_hdrs_mappingMaterialize the expected file hierarchy.
Creates the header file hierarchy accordingly to specifications
in passed-in hdrs_map under 'vhm' directory.
Parameters
*invoker_label | label of rule invoking the method |
*actions | bazel ctx.actions |
*hdrs_map | HdrsMapInfo representing the headers mapping |
*hdrs | list of all header files that should be matched against the map |
providers_helper.merge_hdrs_map_infosMerge all HdrsMapInfo providers from targets into singular one.
Parameters
targets | Default: [] |
hdrs | additional header files to include, Default: None |
implementation_hdrs | additional implementation headers to include, Default: None |
hdrs_map | initial hdrs_map to use as a foundation for merge, Default: None |
hdrs_map_deps | additional dependencies to include, Default: None |
pin_down_non_globs | wheather the final hdrs_map should have its Default: True |
providers_helper.new_hdrs_mapCreate new instance of HdrsMap struct.
Parameters
from_dict | Default: {} |
_glob | Default: None |
_non_glob | Default: None |
providers_helper.quotient_map_hdrs_map_infosTake all HdrsMapInfo key-values and group them by keys.
Parameters
targets | Default: [] |
hdrs | additional header files to include, Default: None |
implementation_hdrs | additional implementation headers to include, Default: None |
hdrs_map | initial hdrs_map to use as a foundation for merge, Default: None |
hdrs_map_deps | additional dependencies to include Default: None |
traverse_deps | wheather to gather HdrsMapInfos transitvely Default: True |
actions.compile_kwargsParameters
*ctx | |
*rule_attrs |
actions.link_to_archive_kwargsParameters
*ctx | |
*rule_attrs |
actions.link_to_binary_kwargsParameters
*ctx | |
*rule_attrs |
actions.link_to_so_kwargsParameters
*ctx | |
*rule_attrs |
actions.prepare_for_compilationMaterialize information from hdrs map.
This function creates a epheremal directory, that contains all of the
patterns specified within hdrs_map providers, thus making them all
available under singular, temporary include statment.
Parameters
*sctx | subrule context |
*input_hdrs_map | list of HdrsMapInfo which should be used for materialization of compilation context |
*input_hdrs | direct headers provided to the action |
*input_implementation_hdrs | direct headers provided to the action |
*input_deps | dependencies specified for the action |
*input_includes | include statements specified for the action |
cc_helper.expand_make_variables_in_coptsParameters
*ctx | |
*cc_info | |
*action_kwargs | |
*opts |
cc_helper.expand_make_variables_in_definesParameters
*ctx | |
*cc_info | |
*action_kwargs | |
*defines | |
local | Default: False |
cc_helper.expand_make_variables_in_linkoptsParameters
*ctx | |
*cc_info | |
*action_kwargs | |
*opts |
cc_helper.extensions.cc_headercc_helper.extensions.cc_sourcecc_helper.extract_headersParameters
*files |
cc_helper.extract_sourcesParameters
*files |
cc_helper.get_cc_flags_make_variableParameters
*cc_toolchain | |
*feature_configuration |
cc_helper.get_compilation_definesParameters
*ctx | |
defines | Default: [] |
deps | Default: [] |
additional_make_variable_substitutions | Default: {} |
additional_targets | Default: [] |
cc_helper.get_compilation_optsParameters
*ctx | |
*opts | |
*feature_configuration | |
additional_make_variable_substitutions | Default: {} |
additional_inputs | Default: [] |
cc_helper.get_linking_optsParameters
*ctx | |
*opts | |
additional_make_variable_substitutions | Default: {} |
additional_inputs | Default: [] |
cc_helper.get_local_defines_for_runfiles_lookupParameters
*ctx | |
*all_deps |
cc_helper.get_toolchain_global_make_variablesParameters
*cc_toolchain |
cc_helper.prepare_for_compilationMaterialize information from hdrs map.
This function creates a epheremal directory, that contains all of the
patterns specified within hdrs_map providers, thus making them all
available under singular, temporary include statment.
Parameters
*sctx | subrule context |
*input_hdrs_map | list of HdrsMapInfo which should be used for materialization of compilation context |
*input_hdrs | direct headers provided to the action |
*input_implementation_hdrs | direct headers provided to the action |
*input_deps | dependencies specified for the action |
*input_includes | include statements specified for the action |
Rules
cc_archiveProduce an archive file.
The intended major difference between this rule and rules_cc's cc_static_library,
is that this rule does not intend to pull-in all static dependencies.
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
archive_lib_name | string | Specify the name of the created .a file (that is decoupled from the rule instance name). Default: "" |
reexport_deps | list of labels | Default: [] |
data | list of labels | The list of files needed by this target at runtime. See general comments about data at Typical attributes defined by most build rules. Default: [] |
deps | list of labels | The list of dependencies of current target Default: [] |
*srcs | list of labels | The list of source files. |
hdrs | list of labels | List of headers that may be included by dependent rules transitively. Default: [] |
implementation_hdrs | list of labels | List of headers that CANNOT be included by dependent rules. Default: [] |
hdrs_map | dictionary: String → List of strings | Dictionary describing paths under which header files should be avaiable as. Keys are simple glob pathnames, used to match agains all header files avaiable in the rule. '{filename}' is special token used to signify to matching file name. For example: Default: {} |
module_interfaces | list of labels | The list of files that are regarded as C++20 Modules Interface. C++ Standard has no restriction about module interface file extension
The use is guarded by the flag --experimental_cpp_modules. Default: [] |
win_def_file | label | The Windows DEF file to be passed to linker. This attribute should only be used when Windows is the target platform. It can be used to export symbols during linking a shared library. Default: None |
additional_compiler_inputs | list of labels | Any additional files you might want to pass to the compiler command line, such as sanitizer ignorelists, for example. Default: [] |
additional_linker_inputs | list of labels | Any additional files that you may want to pass to the linker, for example, linker scripts. Default: [] |
conlyopts | list of strings | Add these options to the C compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. Default: [] |
copts | list of strings | Add these options to the C++ compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. If the package declares the feature no_copts_tokenization, Bourne shell tokenization applies only to strings that consist of a single "Make" variable. Default: [] |
cxxopts | list of strings | Add these options to the C++ compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. Default: [] |
defines | list of strings | List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D and added to the compile command line to this target, as well as to every rule that depends on it. Be very careful, since this may have far-reaching effects. When in doubt, add define values to local_defines instead. Default: [] |
dynamic_deps | list of labels | In contrast to Default: [] |
implementation_deps | list of labels | The list of other libraries that the library target depends on. Unlike with deps, the headers and include paths of these libraries (and all their transitive deps) are only used for compilation of this library, and not libraries that depend on it. Libraries specified with implementation_deps are still linked in binary targets that depend on this library. Default: [] |
includes | list of strings | List of include dirs to be added to the compile line. Headers must be added to srcs or hdrs, otherwise they will not be available to dependent rules when compilation is sandboxed (the default). Default: [] |
linkopts | list of strings | Add these flags to the C++ linker command. Subject to "Make" variable substitution, Bourne shell tokenization and label expansion. Each string in this attribute is added to LINKOPTS before linking the binary target. Default: [] |
linkshared | boolean | Create a shared library. To enable this attribute, include linkshared=True in your rule. By default this option is off. Default: False |
linkstamp | label | Simultaneously compiles and links the specified C++ source file into the final binary. This trickery is required to introduce timestamp information into binaries; if we compiled the source file to an object file in the usual way, the timestamp would be incorrect. A linkstamp compilation may not include any particular set of compiler flags and so should not depend on any particular header, compiler option, or other build variable. This option should only be needed in the base package. Default: None |
linkstatic | boolean | If enabled and this is a binary or test, this option tells the build tool to link in .a's instead of .so's for user libraries whenever possible. System libraries such as libc (but not the C/C++ runtime libraries, see below) are still linked dynamically, as are libraries for which there is no static library. So the resulting executable will still be dynamically linked, hence only mostly static. Default: False |
local_defines | list of strings | List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D and added to the compile command line for this target, but not to its dependents. Default: [] |
nocopts | string | Remove matching options from the C++ compilation command. Subject to "Make" variable substitution. The value of this attribute is interpreted as a regular expression. Any preexisting COPTS that match this regular expression (including values explicitly specified in the rule's copts attribute) will be removed from COPTS for purposes of compiling this rule. This attribute should not be needed or used outside of third_party. The values are not preprocessed in any way other than the "Make" variable substitution. Default: "" |
include_prefix | string | The prefix to add to the paths of the headers of this rule. The prefix in the strip_include_prefix attribute is removed before this prefix is added. Default: "" |
strip_include_prefix | string | The prefix to strip from the paths of the headers of this rule. 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. Default: "" |
textual_hdrs | list of labels | The list of header files published by this library to be textually included by sources in dependent rules. This is the location for declaring header files that cannot be compiled on their own; that is, they always need to be textually included by other source files to build valid code. Default: [] |
cc_binProduce exectuable binary.
It is intended for this rule, to differ from rules_cc's cc_binary in the following
fashion: it aims to automatically gather all of its dynamic dependencies and make them
available during binary execution.
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
stamp | integer | Whether to encode build information into the binary. Possible values:
Stamped binaries are not rebuilt unless their dependencies change. Default: -1 |
reexport_deps | list of labels | Default: [] |
data | list of labels | The list of files needed by this target at runtime. See general comments about data at Typical attributes defined by most build rules. Default: [] |
deps | list of labels | The list of dependencies of current target Default: [] |
*srcs | list of labels | The list of source files. |
hdrs | list of labels | List of headers that may be included by dependent rules transitively. Default: [] |
hdrs_map | dictionary: String → List of strings | Dictionary describing paths under which header files should be avaiable as. Keys are simple glob pathnames, used to match agains all header files avaiable in the rule. '{filename}' is special token used to signify to matching file name. For example: Default: {} |
module_interfaces | list of labels | The list of files that are regarded as C++20 Modules Interface. C++ Standard has no restriction about module interface file extension
The use is guarded by the flag --experimental_cpp_modules. Default: [] |
win_def_file | label | The Windows DEF file to be passed to linker. This attribute should only be used when Windows is the target platform. It can be used to export symbols during linking a shared library. Default: None |
additional_compiler_inputs | list of labels | Any additional files you might want to pass to the compiler command line, such as sanitizer ignorelists, for example. Default: [] |
additional_linker_inputs | list of labels | Any additional files that you may want to pass to the linker, for example, linker scripts. Default: [] |
conlyopts | list of strings | Add these options to the C compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. Default: [] |
copts | list of strings | Add these options to the C++ compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. If the package declares the feature no_copts_tokenization, Bourne shell tokenization applies only to strings that consist of a single "Make" variable. Default: [] |
cxxopts | list of strings | Add these options to the C++ compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. Default: [] |
defines | list of strings | List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D and added to the compile command line to this target, as well as to every rule that depends on it. Be very careful, since this may have far-reaching effects. When in doubt, add define values to local_defines instead. Default: [] |
dynamic_deps | list of labels | In contrast to Default: [] |
includes | list of strings | List of include dirs to be added to the compile line. Headers must be added to srcs or hdrs, otherwise they will not be available to dependent rules when compilation is sandboxed (the default). Default: [] |
link_extra_lib | label | Control linking of extra libraries. By default, C++ binaries are linked against //tools/cpp:link_extra_lib, which by default depends on the label flag //tools/cpp:link_extra_libs. Without setting the flag, this library is empty by default. Setting the label flag allows linking optional dependencies, such as overrides for weak symbols, interceptors for shared library functions, or special runtime libraries (for malloc replacements, prefer malloc or --custom_malloc). Setting this attribute to None disables this behaviour. Default: None |
linkopts | list of strings | Add these flags to the C++ linker command. Subject to "Make" variable substitution, Bourne shell tokenization and label expansion. Each string in this attribute is added to LINKOPTS before linking the binary target. Default: [] |
linkshared | boolean | Create a shared library. To enable this attribute, include linkshared=True in your rule. By default this option is off. Default: False |
linkstatic | boolean | If enabled and this is a binary or test, this option tells the build tool to link in .a's instead of .so's for user libraries whenever possible. System libraries such as libc (but not the C/C++ runtime libraries, see below) are still linked dynamically, as are libraries for which there is no static library. So the resulting executable will still be dynamically linked, hence only mostly static. Default: False |
local_defines | list of strings | List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D and added to the compile command line for this target, but not to its dependents. Default: [] |
malloc | label | Override the default dependency on malloc. By default, C++ binaries are linked against //tools/cpp:malloc, which is an empty library so the binary ends up using libc malloc. This label must refer to a cc_library. If compilation is for a non-C++ rule, this option has no effect. The value of this attribute is ignored if linkshared=True is specified. Default: None |
nocopts | string | Remove matching options from the C++ compilation command. Subject to "Make" variable substitution. The value of this attribute is interpreted as a regular expression. Any preexisting COPTS that match this regular expression (including values explicitly specified in the rule's copts attribute) will be removed from COPTS for purposes of compiling this rule. This attribute should not be needed or used outside of third_party. The values are not preprocessed in any way other than the "Make" variable substitution. Default: "" |
cc_hdrsDefine header files properties.
This rule groups headers into a singular target and allows
to attach 'include_path' modifications information to them,
so that wherever the header files are being used, they can
be used with their intended include paths.
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
data | list of labels | The list of files needed by this target at runtime. See general comments about data at Typical attributes defined by most build rules. Default: [] |
deps | list of labels | The list of dependencies of current target Default: [] |
hdrs | list of labels | List of headers that may be included by dependent rules transitively. Default: [] |
implementation_hdrs | list of labels | List of headers that CANNOT be included by dependent rules. Default: [] |
hdrs_map | dictionary: String → List of strings | Dictionary describing paths under which header files should be avaiable as. Keys are simple glob pathnames, used to match agains all header files avaiable in the rule. '{filename}' is special token used to signify to matching file name. For example: Default: {} |
module_interfaces | list of labels | The list of files that are regarded as C++20 Modules Interface. C++ Standard has no restriction about module interface file extension
The use is guarded by the flag --experimental_cpp_modules. Default: [] |
win_def_file | label | The Windows DEF file to be passed to linker. This attribute should only be used when Windows is the target platform. It can be used to export symbols during linking a shared library. Default: None |
cc_soProduce shared object library.
The intended difference between this rule and the rules_cc's cc_shared_library is to:
- remove 'cc_library' out of the equation (no more targets that produce either archive or sol)
- unify handling of dependencies that are equipped with CcInfo and CcSharedLibraryInfo
(use singular attribute of deps to track them both).
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
alwayslink | boolean | If 1, any binary that depends (directly or indirectly) on this C++ precompiled library will link in all the object files archived in the static library, even if some contain no symbols referenced by the binary. This is useful if your code isn't explicitly called by code in the binary, e.g., if your code registers to receive some callback provided by some service. Default: False |
exports_filter | list of strings | This attribute contains a list of targets that are claimed to be exported by the current shared library. Any target deps is already understood to be exported by the shared library. This attribute should be used to list any targets that are exported by the shared library but are transitive dependencies of deps. Note that this attribute is not actually adding a dependency edge to those targets, the dependency edge should instead be created by deps.The entries in this attribute are just strings. Keep in mind that when placing a target in this attribute, this is considered a claim that the shared library exports the symbols from that target. The cc_shared_library logic doesn't actually handle telling the linker which symbols should be exported. The following syntax is allowed: //foo:pkg to account for any target in foo/BUILD //foo:subpackages to account for any target in foo/BUILD or any other package below foo/ like foo/bar/BUILD Default: [] |
roots | list of labels | (Not yet implemented) Default: [] |
shared_lib_name | string | Specify the name of the created SOL file (that is decoupled from the rule instance name). Default: "" |
data | list of labels | The list of files needed by this target at runtime. See general comments about data at Typical attributes defined by most build rules. Default: [] |
deps | list of labels | The list of dependencies of current target Default: [] |
*srcs | list of labels | The list of source files. |
hdrs | list of labels | List of headers that may be included by dependent rules transitively. Default: [] |
implementation_hdrs | list of labels | List of headers that CANNOT be included by dependent rules. Default: [] |
hdrs_map | dictionary: String → List of strings | Dictionary describing paths under which header files should be avaiable as. Keys are simple glob pathnames, used to match agains all header files avaiable in the rule. '{filename}' is special token used to signify to matching file name. For example: Default: {} |
module_interfaces | list of labels | The list of files that are regarded as C++20 Modules Interface. C++ Standard has no restriction about module interface file extension
The use is guarded by the flag --experimental_cpp_modules. Default: [] |
win_def_file | label | The Windows DEF file to be passed to linker. This attribute should only be used when Windows is the target platform. It can be used to export symbols during linking a shared library. Default: None |
additional_compiler_inputs | list of labels | Any additional files you might want to pass to the compiler command line, such as sanitizer ignorelists, for example. Default: [] |
additional_linker_inputs | list of labels | Any additional files that you may want to pass to the linker, for example, linker scripts. Default: [] |
conlyopts | list of strings | Add these options to the C compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. Default: [] |
copts | list of strings | Add these options to the C++ compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. If the package declares the feature no_copts_tokenization, Bourne shell tokenization applies only to strings that consist of a single "Make" variable. Default: [] |
cxxopts | list of strings | Add these options to the C++ compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. Default: [] |
defines | list of strings | List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D and added to the compile command line to this target, as well as to every rule that depends on it. Be very careful, since this may have far-reaching effects. When in doubt, add define values to local_defines instead. Default: [] |
dynamic_deps | list of labels | In contrast to Default: [] |
implementation_deps | list of labels | The list of other libraries that the library target depends on. Unlike with deps, the headers and include paths of these libraries (and all their transitive deps) are only used for compilation of this library, and not libraries that depend on it. Libraries specified with implementation_deps are still linked in binary targets that depend on this library. Default: [] |
includes | list of strings | List of include dirs to be added to the compile line. Headers must be added to srcs or hdrs, otherwise they will not be available to dependent rules when compilation is sandboxed (the default). Default: [] |
link_extra_lib | label | Control linking of extra libraries. By default, C++ binaries are linked against //tools/cpp:link_extra_lib, which by default depends on the label flag //tools/cpp:link_extra_libs. Without setting the flag, this library is empty by default. Setting the label flag allows linking optional dependencies, such as overrides for weak symbols, interceptors for shared library functions, or special runtime libraries (for malloc replacements, prefer malloc or --custom_malloc). Setting this attribute to None disables this behaviour. Default: None |
linkopts | list of strings | Add these flags to the C++ linker command. Subject to "Make" variable substitution, Bourne shell tokenization and label expansion. Each string in this attribute is added to LINKOPTS before linking the binary target. Default: [] |
linkshared | boolean | Create a shared library. To enable this attribute, include linkshared=True in your rule. By default this option is off. Default: False |
linkstamp | label | Simultaneously compiles and links the specified C++ source file into the final binary. This trickery is required to introduce timestamp information into binaries; if we compiled the source file to an object file in the usual way, the timestamp would be incorrect. A linkstamp compilation may not include any particular set of compiler flags and so should not depend on any particular header, compiler option, or other build variable. This option should only be needed in the base package. Default: None |
linkstatic | boolean | If enabled and this is a binary or test, this option tells the build tool to link in .a's instead of .so's for user libraries whenever possible. System libraries such as libc (but not the C/C++ runtime libraries, see below) are still linked dynamically, as are libraries for which there is no static library. So the resulting executable will still be dynamically linked, hence only mostly static. Default: False |
local_defines | list of strings | List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D and added to the compile command line for this target, but not to its dependents. Default: [] |
malloc | label | Override the default dependency on malloc. By default, C++ binaries are linked against //tools/cpp:malloc, which is an empty library so the binary ends up using libc malloc. This label must refer to a cc_library. If compilation is for a non-C++ rule, this option has no effect. The value of this attribute is ignored if linkshared=True is specified. Default: None |
nocopts | string | Remove matching options from the C++ compilation command. Subject to "Make" variable substitution. The value of this attribute is interpreted as a regular expression. Any preexisting COPTS that match this regular expression (including values explicitly specified in the rule's copts attribute) will be removed from COPTS for purposes of compiling this rule. This attribute should not be needed or used outside of third_party. The values are not preprocessed in any way other than the "Make" variable substitution. Default: "" |
include_prefix | string | The prefix to add to the paths of the headers of this rule. The prefix in the strip_include_prefix attribute is removed before this prefix is added. Default: "" |
strip_include_prefix | string | The prefix to strip from the paths of the headers of this rule. 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. Default: "" |
textual_hdrs | list of labels | The list of header files published by this library to be textually included by sources in dependent rules. This is the location for declaring header files that cannot be compiled on their own; that is, they always need to be textually included by other source files to build valid code. Default: [] |
cc_so_importImport precompiled C/C++ shared object library.
The intended difference between this rule and the rules_cc's cc_import is to directly
provide CcSharedLibrary info to force linking and early deps cutoff.
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
*shared_library | label | A single precompiled shared library. This ruleset will ensure it will be available to depending cc_hdrs_map targets. |
cascade | boolean | Please DO NOT USE. This makes the SOL being propagated upwards to every dependee of this target. Default: False |
data | list of labels | The list of files needed by this target at runtime. See general comments about data at Typical attributes defined by most build rules. Default: [] |
deps | list of labels | The list of dependencies of current target Default: [] |
hdrs | list of labels | List of headers that may be included by dependent rules transitively. Default: [] |
hdrs_map | dictionary: String → List of strings | Dictionary describing paths under which header files should be avaiable as. Keys are simple glob pathnames, used to match agains all header files avaiable in the rule. '{filename}' is special token used to signify to matching file name. For example: Default: {} |
additional_linker_inputs | list of labels | Any additional files that you may want to pass to the linker, for example, linker scripts. Default: [] |
dynamic_deps | list of labels | In contrast to Default: [] |
implementation_deps | list of labels | The list of other libraries that the library target depends on. Unlike with deps, the headers and include paths of these libraries (and all their transitive deps) are only used for compilation of this library, and not libraries that depend on it. Libraries specified with implementation_deps are still linked in binary targets that depend on this library. Default: [] |
linkopts | list of strings | Add these flags to the C++ linker command. Subject to "Make" variable substitution, Bourne shell tokenization and label expansion. Each string in this attribute is added to LINKOPTS before linking the binary target. Default: [] |
include_prefix | string | The prefix to add to the paths of the headers of this rule. The prefix in the strip_include_prefix attribute is removed before this prefix is added. Default: "" |
strip_include_prefix | string | The prefix to strip from the paths of the headers of this rule. 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. Default: "" |
Providers
HdrsMapInfoRepresents grouping of CC header files, alongsdie with their intended include paths.
Fields
hdrs | Headers which should be exposed after the compilation is done. |
implementation_hdrs | Headers that should not be propagated after the compilation. |
hdrs_map | (hdrs_map struct) object describing desired header file mappings |
deps | CcInfo/CcSharedLibraryInfo-aware dependencies that need to be propagated, for this provider to compile and link |
@rules_cc_hdrs_map//cc_hdrs_map:defs.bzl
rules_cc_hdrs_map
This project extends Bazel CC build capabilities with headers map implementation (allowing for easy support for most bizzare include path schemes).
In addition, it exposes CC compilation and linking functions in form of Bazel subrules.
See examples for how to use rules_cc_hdrs_map (and why).
Shortest possible example
$ cat foo.hpp
const std::string GREETINGS = "Hello";
$ cat foo.cpp
#include "bar/foo.h"
...
$ cat BUILD.bazel
load("@rules_cc_hdrs_map//cc_hdrs_map:defs.bzl", "cc_bin")
cc_bin(
name = "foo",
srcs = [
"foo.cpp",
"foo.hpp",
],
hdrs_map = {
"**/foo.hpp": ["bar/{filename}"],
}
)
Table of contents
- What issue is being addressed?
- How the issue is being addressed?
- What issue is being addressed?
- Rules
- HdrsMapInfo provider
What issue is being addressed?
Creation of arbitrary include paths from existing sources.
Scenario: we want to build a C/CPP codebase with Bazel.
One of its key characteristics is that most of the include statements do not reflect the code structure in the project - for example, header file located under path “name/a.hpp” is never included as “name/a.hpp”, instead an arbitrary list of aliases is used in the code (“x/y/z/a.hpp”, “b.hpp” etc.). There is no overarching convention that could be used to generalize those statements into another file file hierarchy - in other words, every header file is a special case of its own.
Unfortunately we are forbidden from modifying the code itself and the directory structure (hello from enterprise word).
As Bazel rules_cc have the expectation of header files being included in a way that resembles the file structure in the WORKSPACE (and one can only provide single “include prefix” per library), we need to prepare the “expected file structure” before passing them into the rules_cc.
In the most naive approach, said “expected file structure” is being prepared for each compilable target (copying over files), passing on the already created structure to targets that depend on it. Very quickly conflicts occur and change of a single header file may cascade into rebuilding hundreds of targets.
There has to be a better way!
How the issue is being addressed?
The concept of header map is introduced - it is a dictionary, containing mapping between simple glob paths and their desired include paths. For example: “**/a.hpp”: “x/y/z/a.hpp” expresses the intent to import any header file with name “a.hpp”, present during compilation , as “x/y/z/a.hpp”.
Said header map is propagated across all compatible C/C++ rules (meaning those from this WORKSPACE) and is being merged with all other header maps present.
No action is being performed up until the moment of compilation - header mappings, resulting from the header map dictionary, are created only for the purposes of compilation and are NOT part of any rule output. This ensures the impact for the Bazel cache is minimal and the compatibility with original rules_cc.
Functions & Macros
providers_helper.materialize_hdrs_mappingMaterialize the expected file hierarchy.
Creates the header file hierarchy accordingly to specifications
in passed-in hdrs_map under 'vhm' directory.
Parameters
*invoker_label | label of rule invoking the method |
*actions | bazel ctx.actions |
*hdrs_map | HdrsMapInfo representing the headers mapping |
*hdrs | list of all header files that should be matched against the map |
providers_helper.merge_hdrs_map_infosMerge all HdrsMapInfo providers from targets into singular one.
Parameters
targets | Default: [] |
hdrs | additional header files to include, Default: None |
implementation_hdrs | additional implementation headers to include, Default: None |
hdrs_map | initial hdrs_map to use as a foundation for merge, Default: None |
hdrs_map_deps | additional dependencies to include, Default: None |
pin_down_non_globs | wheather the final hdrs_map should have its Default: True |
providers_helper.new_hdrs_mapCreate new instance of HdrsMap struct.
Parameters
from_dict | Default: {} |
_glob | Default: None |
_non_glob | Default: None |
providers_helper.quotient_map_hdrs_map_infosTake all HdrsMapInfo key-values and group them by keys.
Parameters
targets | Default: [] |
hdrs | additional header files to include, Default: None |
implementation_hdrs | additional implementation headers to include, Default: None |
hdrs_map | initial hdrs_map to use as a foundation for merge, Default: None |
hdrs_map_deps | additional dependencies to include Default: None |
traverse_deps | wheather to gather HdrsMapInfos transitvely Default: True |
actions.compile_kwargsParameters
*ctx | |
*rule_attrs |
actions.link_to_archive_kwargsParameters
*ctx | |
*rule_attrs |
actions.link_to_binary_kwargsParameters
*ctx | |
*rule_attrs |
actions.link_to_so_kwargsParameters
*ctx | |
*rule_attrs |
actions.prepare_for_compilationMaterialize information from hdrs map.
This function creates a epheremal directory, that contains all of the
patterns specified within hdrs_map providers, thus making them all
available under singular, temporary include statment.
Parameters
*sctx | subrule context |
*input_hdrs_map | list of HdrsMapInfo which should be used for materialization of compilation context |
*input_hdrs | direct headers provided to the action |
*input_implementation_hdrs | direct headers provided to the action |
*input_deps | dependencies specified for the action |
*input_includes | include statements specified for the action |
cc_helper.expand_make_variables_in_coptsParameters
*ctx | |
*cc_info | |
*action_kwargs | |
*opts |
cc_helper.expand_make_variables_in_definesParameters
*ctx | |
*cc_info | |
*action_kwargs | |
*defines | |
local | Default: False |
cc_helper.expand_make_variables_in_linkoptsParameters
*ctx | |
*cc_info | |
*action_kwargs | |
*opts |
cc_helper.extensions.cc_headercc_helper.extensions.cc_sourcecc_helper.extract_headersParameters
*files |
cc_helper.extract_sourcesParameters
*files |
cc_helper.get_cc_flags_make_variableParameters
*cc_toolchain | |
*feature_configuration |
cc_helper.get_compilation_definesParameters
*ctx | |
defines | Default: [] |
deps | Default: [] |
additional_make_variable_substitutions | Default: {} |
additional_targets | Default: [] |
cc_helper.get_compilation_optsParameters
*ctx | |
*opts | |
*feature_configuration | |
additional_make_variable_substitutions | Default: {} |
additional_inputs | Default: [] |
cc_helper.get_linking_optsParameters
*ctx | |
*opts | |
additional_make_variable_substitutions | Default: {} |
additional_inputs | Default: [] |
cc_helper.get_local_defines_for_runfiles_lookupParameters
*ctx | |
*all_deps |
cc_helper.get_toolchain_global_make_variablesParameters
*cc_toolchain |
cc_helper.prepare_for_compilationMaterialize information from hdrs map.
This function creates a epheremal directory, that contains all of the
patterns specified within hdrs_map providers, thus making them all
available under singular, temporary include statment.
Parameters
*sctx | subrule context |
*input_hdrs_map | list of HdrsMapInfo which should be used for materialization of compilation context |
*input_hdrs | direct headers provided to the action |
*input_implementation_hdrs | direct headers provided to the action |
*input_deps | dependencies specified for the action |
*input_includes | include statements specified for the action |
Rules
cc_archiveProduce an archive file.
The intended major difference between this rule and rules_cc's cc_static_library,
is that this rule does not intend to pull-in all static dependencies.
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
archive_lib_name | string | Specify the name of the created .a file (that is decoupled from the rule instance name). Default: "" |
reexport_deps | list of labels | Default: [] |
data | list of labels | The list of files needed by this target at runtime. See general comments about data at Typical attributes defined by most build rules. Default: [] |
deps | list of labels | The list of dependencies of current target Default: [] |
*srcs | list of labels | The list of source files. |
hdrs | list of labels | List of headers that may be included by dependent rules transitively. Default: [] |
implementation_hdrs | list of labels | List of headers that CANNOT be included by dependent rules. Default: [] |
hdrs_map | dictionary: String → List of strings | Dictionary describing paths under which header files should be avaiable as. Keys are simple glob pathnames, used to match agains all header files avaiable in the rule. '{filename}' is special token used to signify to matching file name. For example: Default: {} |
module_interfaces | list of labels | The list of files that are regarded as C++20 Modules Interface. C++ Standard has no restriction about module interface file extension
The use is guarded by the flag --experimental_cpp_modules. Default: [] |
win_def_file | label | The Windows DEF file to be passed to linker. This attribute should only be used when Windows is the target platform. It can be used to export symbols during linking a shared library. Default: None |
additional_compiler_inputs | list of labels | Any additional files you might want to pass to the compiler command line, such as sanitizer ignorelists, for example. Default: [] |
additional_linker_inputs | list of labels | Any additional files that you may want to pass to the linker, for example, linker scripts. Default: [] |
conlyopts | list of strings | Add these options to the C compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. Default: [] |
copts | list of strings | Add these options to the C++ compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. If the package declares the feature no_copts_tokenization, Bourne shell tokenization applies only to strings that consist of a single "Make" variable. Default: [] |
cxxopts | list of strings | Add these options to the C++ compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. Default: [] |
defines | list of strings | List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D and added to the compile command line to this target, as well as to every rule that depends on it. Be very careful, since this may have far-reaching effects. When in doubt, add define values to local_defines instead. Default: [] |
dynamic_deps | list of labels | In contrast to Default: [] |
implementation_deps | list of labels | The list of other libraries that the library target depends on. Unlike with deps, the headers and include paths of these libraries (and all their transitive deps) are only used for compilation of this library, and not libraries that depend on it. Libraries specified with implementation_deps are still linked in binary targets that depend on this library. Default: [] |
includes | list of strings | List of include dirs to be added to the compile line. Headers must be added to srcs or hdrs, otherwise they will not be available to dependent rules when compilation is sandboxed (the default). Default: [] |
linkopts | list of strings | Add these flags to the C++ linker command. Subject to "Make" variable substitution, Bourne shell tokenization and label expansion. Each string in this attribute is added to LINKOPTS before linking the binary target. Default: [] |
linkshared | boolean | Create a shared library. To enable this attribute, include linkshared=True in your rule. By default this option is off. Default: False |
linkstamp | label | Simultaneously compiles and links the specified C++ source file into the final binary. This trickery is required to introduce timestamp information into binaries; if we compiled the source file to an object file in the usual way, the timestamp would be incorrect. A linkstamp compilation may not include any particular set of compiler flags and so should not depend on any particular header, compiler option, or other build variable. This option should only be needed in the base package. Default: None |
linkstatic | boolean | If enabled and this is a binary or test, this option tells the build tool to link in .a's instead of .so's for user libraries whenever possible. System libraries such as libc (but not the C/C++ runtime libraries, see below) are still linked dynamically, as are libraries for which there is no static library. So the resulting executable will still be dynamically linked, hence only mostly static. Default: False |
local_defines | list of strings | List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D and added to the compile command line for this target, but not to its dependents. Default: [] |
nocopts | string | Remove matching options from the C++ compilation command. Subject to "Make" variable substitution. The value of this attribute is interpreted as a regular expression. Any preexisting COPTS that match this regular expression (including values explicitly specified in the rule's copts attribute) will be removed from COPTS for purposes of compiling this rule. This attribute should not be needed or used outside of third_party. The values are not preprocessed in any way other than the "Make" variable substitution. Default: "" |
include_prefix | string | The prefix to add to the paths of the headers of this rule. The prefix in the strip_include_prefix attribute is removed before this prefix is added. Default: "" |
strip_include_prefix | string | The prefix to strip from the paths of the headers of this rule. 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. Default: "" |
textual_hdrs | list of labels | The list of header files published by this library to be textually included by sources in dependent rules. This is the location for declaring header files that cannot be compiled on their own; that is, they always need to be textually included by other source files to build valid code. Default: [] |
cc_binProduce exectuable binary.
It is intended for this rule, to differ from rules_cc's cc_binary in the following
fashion: it aims to automatically gather all of its dynamic dependencies and make them
available during binary execution.
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
stamp | integer | Whether to encode build information into the binary. Possible values:
Stamped binaries are not rebuilt unless their dependencies change. Default: -1 |
reexport_deps | list of labels | Default: [] |
data | list of labels | The list of files needed by this target at runtime. See general comments about data at Typical attributes defined by most build rules. Default: [] |
deps | list of labels | The list of dependencies of current target Default: [] |
*srcs | list of labels | The list of source files. |
hdrs | list of labels | List of headers that may be included by dependent rules transitively. Default: [] |
hdrs_map | dictionary: String → List of strings | Dictionary describing paths under which header files should be avaiable as. Keys are simple glob pathnames, used to match agains all header files avaiable in the rule. '{filename}' is special token used to signify to matching file name. For example: Default: {} |
module_interfaces | list of labels | The list of files that are regarded as C++20 Modules Interface. C++ Standard has no restriction about module interface file extension
The use is guarded by the flag --experimental_cpp_modules. Default: [] |
win_def_file | label | The Windows DEF file to be passed to linker. This attribute should only be used when Windows is the target platform. It can be used to export symbols during linking a shared library. Default: None |
additional_compiler_inputs | list of labels | Any additional files you might want to pass to the compiler command line, such as sanitizer ignorelists, for example. Default: [] |
additional_linker_inputs | list of labels | Any additional files that you may want to pass to the linker, for example, linker scripts. Default: [] |
conlyopts | list of strings | Add these options to the C compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. Default: [] |
copts | list of strings | Add these options to the C++ compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. If the package declares the feature no_copts_tokenization, Bourne shell tokenization applies only to strings that consist of a single "Make" variable. Default: [] |
cxxopts | list of strings | Add these options to the C++ compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. Default: [] |
defines | list of strings | List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D and added to the compile command line to this target, as well as to every rule that depends on it. Be very careful, since this may have far-reaching effects. When in doubt, add define values to local_defines instead. Default: [] |
dynamic_deps | list of labels | In contrast to Default: [] |
includes | list of strings | List of include dirs to be added to the compile line. Headers must be added to srcs or hdrs, otherwise they will not be available to dependent rules when compilation is sandboxed (the default). Default: [] |
link_extra_lib | label | Control linking of extra libraries. By default, C++ binaries are linked against //tools/cpp:link_extra_lib, which by default depends on the label flag //tools/cpp:link_extra_libs. Without setting the flag, this library is empty by default. Setting the label flag allows linking optional dependencies, such as overrides for weak symbols, interceptors for shared library functions, or special runtime libraries (for malloc replacements, prefer malloc or --custom_malloc). Setting this attribute to None disables this behaviour. Default: None |
linkopts | list of strings | Add these flags to the C++ linker command. Subject to "Make" variable substitution, Bourne shell tokenization and label expansion. Each string in this attribute is added to LINKOPTS before linking the binary target. Default: [] |
linkshared | boolean | Create a shared library. To enable this attribute, include linkshared=True in your rule. By default this option is off. Default: False |
linkstatic | boolean | If enabled and this is a binary or test, this option tells the build tool to link in .a's instead of .so's for user libraries whenever possible. System libraries such as libc (but not the C/C++ runtime libraries, see below) are still linked dynamically, as are libraries for which there is no static library. So the resulting executable will still be dynamically linked, hence only mostly static. Default: False |
local_defines | list of strings | List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D and added to the compile command line for this target, but not to its dependents. Default: [] |
malloc | label | Override the default dependency on malloc. By default, C++ binaries are linked against //tools/cpp:malloc, which is an empty library so the binary ends up using libc malloc. This label must refer to a cc_library. If compilation is for a non-C++ rule, this option has no effect. The value of this attribute is ignored if linkshared=True is specified. Default: None |
nocopts | string | Remove matching options from the C++ compilation command. Subject to "Make" variable substitution. The value of this attribute is interpreted as a regular expression. Any preexisting COPTS that match this regular expression (including values explicitly specified in the rule's copts attribute) will be removed from COPTS for purposes of compiling this rule. This attribute should not be needed or used outside of third_party. The values are not preprocessed in any way other than the "Make" variable substitution. Default: "" |
cc_hdrsDefine header files properties.
This rule groups headers into a singular target and allows
to attach 'include_path' modifications information to them,
so that wherever the header files are being used, they can
be used with their intended include paths.
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
data | list of labels | The list of files needed by this target at runtime. See general comments about data at Typical attributes defined by most build rules. Default: [] |
deps | list of labels | The list of dependencies of current target Default: [] |
hdrs | list of labels | List of headers that may be included by dependent rules transitively. Default: [] |
implementation_hdrs | list of labels | List of headers that CANNOT be included by dependent rules. Default: [] |
hdrs_map | dictionary: String → List of strings | Dictionary describing paths under which header files should be avaiable as. Keys are simple glob pathnames, used to match agains all header files avaiable in the rule. '{filename}' is special token used to signify to matching file name. For example: Default: {} |
module_interfaces | list of labels | The list of files that are regarded as C++20 Modules Interface. C++ Standard has no restriction about module interface file extension
The use is guarded by the flag --experimental_cpp_modules. Default: [] |
win_def_file | label | The Windows DEF file to be passed to linker. This attribute should only be used when Windows is the target platform. It can be used to export symbols during linking a shared library. Default: None |
cc_soProduce shared object library.
The intended difference between this rule and the rules_cc's cc_shared_library is to:
- remove 'cc_library' out of the equation (no more targets that produce either archive or sol)
- unify handling of dependencies that are equipped with CcInfo and CcSharedLibraryInfo
(use singular attribute of deps to track them both).
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
alwayslink | boolean | If 1, any binary that depends (directly or indirectly) on this C++ precompiled library will link in all the object files archived in the static library, even if some contain no symbols referenced by the binary. This is useful if your code isn't explicitly called by code in the binary, e.g., if your code registers to receive some callback provided by some service. Default: False |
exports_filter | list of strings | This attribute contains a list of targets that are claimed to be exported by the current shared library. Any target deps is already understood to be exported by the shared library. This attribute should be used to list any targets that are exported by the shared library but are transitive dependencies of deps. Note that this attribute is not actually adding a dependency edge to those targets, the dependency edge should instead be created by deps.The entries in this attribute are just strings. Keep in mind that when placing a target in this attribute, this is considered a claim that the shared library exports the symbols from that target. The cc_shared_library logic doesn't actually handle telling the linker which symbols should be exported. The following syntax is allowed: //foo:pkg to account for any target in foo/BUILD //foo:subpackages to account for any target in foo/BUILD or any other package below foo/ like foo/bar/BUILD Default: [] |
roots | list of labels | (Not yet implemented) Default: [] |
shared_lib_name | string | Specify the name of the created SOL file (that is decoupled from the rule instance name). Default: "" |
data | list of labels | The list of files needed by this target at runtime. See general comments about data at Typical attributes defined by most build rules. Default: [] |
deps | list of labels | The list of dependencies of current target Default: [] |
*srcs | list of labels | The list of source files. |
hdrs | list of labels | List of headers that may be included by dependent rules transitively. Default: [] |
implementation_hdrs | list of labels | List of headers that CANNOT be included by dependent rules. Default: [] |
hdrs_map | dictionary: String → List of strings | Dictionary describing paths under which header files should be avaiable as. Keys are simple glob pathnames, used to match agains all header files avaiable in the rule. '{filename}' is special token used to signify to matching file name. For example: Default: {} |
module_interfaces | list of labels | The list of files that are regarded as C++20 Modules Interface. C++ Standard has no restriction about module interface file extension
The use is guarded by the flag --experimental_cpp_modules. Default: [] |
win_def_file | label | The Windows DEF file to be passed to linker. This attribute should only be used when Windows is the target platform. It can be used to export symbols during linking a shared library. Default: None |
additional_compiler_inputs | list of labels | Any additional files you might want to pass to the compiler command line, such as sanitizer ignorelists, for example. Default: [] |
additional_linker_inputs | list of labels | Any additional files that you may want to pass to the linker, for example, linker scripts. Default: [] |
conlyopts | list of strings | Add these options to the C compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. Default: [] |
copts | list of strings | Add these options to the C++ compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. If the package declares the feature no_copts_tokenization, Bourne shell tokenization applies only to strings that consist of a single "Make" variable. Default: [] |
cxxopts | list of strings | Add these options to the C++ compilation command. Subject to "Make variable" substitution and Bourne shell tokenization. Default: [] |
defines | list of strings | List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D and added to the compile command line to this target, as well as to every rule that depends on it. Be very careful, since this may have far-reaching effects. When in doubt, add define values to local_defines instead. Default: [] |
dynamic_deps | list of labels | In contrast to Default: [] |
implementation_deps | list of labels | The list of other libraries that the library target depends on. Unlike with deps, the headers and include paths of these libraries (and all their transitive deps) are only used for compilation of this library, and not libraries that depend on it. Libraries specified with implementation_deps are still linked in binary targets that depend on this library. Default: [] |
includes | list of strings | List of include dirs to be added to the compile line. Headers must be added to srcs or hdrs, otherwise they will not be available to dependent rules when compilation is sandboxed (the default). Default: [] |
link_extra_lib | label | Control linking of extra libraries. By default, C++ binaries are linked against //tools/cpp:link_extra_lib, which by default depends on the label flag //tools/cpp:link_extra_libs. Without setting the flag, this library is empty by default. Setting the label flag allows linking optional dependencies, such as overrides for weak symbols, interceptors for shared library functions, or special runtime libraries (for malloc replacements, prefer malloc or --custom_malloc). Setting this attribute to None disables this behaviour. Default: None |
linkopts | list of strings | Add these flags to the C++ linker command. Subject to "Make" variable substitution, Bourne shell tokenization and label expansion. Each string in this attribute is added to LINKOPTS before linking the binary target. Default: [] |
linkshared | boolean | Create a shared library. To enable this attribute, include linkshared=True in your rule. By default this option is off. Default: False |
linkstamp | label | Simultaneously compiles and links the specified C++ source file into the final binary. This trickery is required to introduce timestamp information into binaries; if we compiled the source file to an object file in the usual way, the timestamp would be incorrect. A linkstamp compilation may not include any particular set of compiler flags and so should not depend on any particular header, compiler option, or other build variable. This option should only be needed in the base package. Default: None |
linkstatic | boolean | If enabled and this is a binary or test, this option tells the build tool to link in .a's instead of .so's for user libraries whenever possible. System libraries such as libc (but not the C/C++ runtime libraries, see below) are still linked dynamically, as are libraries for which there is no static library. So the resulting executable will still be dynamically linked, hence only mostly static. Default: False |
local_defines | list of strings | List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. Each string, which must consist of a single Bourne shell token, is prepended with -D and added to the compile command line for this target, but not to its dependents. Default: [] |
malloc | label | Override the default dependency on malloc. By default, C++ binaries are linked against //tools/cpp:malloc, which is an empty library so the binary ends up using libc malloc. This label must refer to a cc_library. If compilation is for a non-C++ rule, this option has no effect. The value of this attribute is ignored if linkshared=True is specified. Default: None |
nocopts | string | Remove matching options from the C++ compilation command. Subject to "Make" variable substitution. The value of this attribute is interpreted as a regular expression. Any preexisting COPTS that match this regular expression (including values explicitly specified in the rule's copts attribute) will be removed from COPTS for purposes of compiling this rule. This attribute should not be needed or used outside of third_party. The values are not preprocessed in any way other than the "Make" variable substitution. Default: "" |
include_prefix | string | The prefix to add to the paths of the headers of this rule. The prefix in the strip_include_prefix attribute is removed before this prefix is added. Default: "" |
strip_include_prefix | string | The prefix to strip from the paths of the headers of this rule. 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. Default: "" |
textual_hdrs | list of labels | The list of header files published by this library to be textually included by sources in dependent rules. This is the location for declaring header files that cannot be compiled on their own; that is, they always need to be textually included by other source files to build valid code. Default: [] |
cc_so_importImport precompiled C/C++ shared object library.
The intended difference between this rule and the rules_cc's cc_import is to directly
provide CcSharedLibrary info to force linking and early deps cutoff.
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
*shared_library | label | A single precompiled shared library. This ruleset will ensure it will be available to depending cc_hdrs_map targets. |
cascade | boolean | Please DO NOT USE. This makes the SOL being propagated upwards to every dependee of this target. Default: False |
data | list of labels | The list of files needed by this target at runtime. See general comments about data at Typical attributes defined by most build rules. Default: [] |
deps | list of labels | The list of dependencies of current target Default: [] |
hdrs | list of labels | List of headers that may be included by dependent rules transitively. Default: [] |
hdrs_map | dictionary: String → List of strings | Dictionary describing paths under which header files should be avaiable as. Keys are simple glob pathnames, used to match agains all header files avaiable in the rule. '{filename}' is special token used to signify to matching file name. For example: Default: {} |
additional_linker_inputs | list of labels | Any additional files that you may want to pass to the linker, for example, linker scripts. Default: [] |
dynamic_deps | list of labels | In contrast to Default: [] |
implementation_deps | list of labels | The list of other libraries that the library target depends on. Unlike with deps, the headers and include paths of these libraries (and all their transitive deps) are only used for compilation of this library, and not libraries that depend on it. Libraries specified with implementation_deps are still linked in binary targets that depend on this library. Default: [] |
linkopts | list of strings | Add these flags to the C++ linker command. Subject to "Make" variable substitution, Bourne shell tokenization and label expansion. Each string in this attribute is added to LINKOPTS before linking the binary target. Default: [] |
include_prefix | string | The prefix to add to the paths of the headers of this rule. The prefix in the strip_include_prefix attribute is removed before this prefix is added. Default: "" |
strip_include_prefix | string | The prefix to strip from the paths of the headers of this rule. 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. Default: "" |
Providers
HdrsMapInfoRepresents grouping of CC header files, alongsdie with their intended include paths.
Fields
hdrs | Headers which should be exposed after the compilation is done. |
implementation_hdrs | Headers that should not be propagated after the compilation. |
hdrs_map | (hdrs_map struct) object describing desired header file mappings |
deps | CcInfo/CcSharedLibraryInfo-aware dependencies that need to be propagated, for this provider to compile and link |
@rules_cc_hdrs_map//cc_hdrs_map:workspace_deps.bzl
This module exposes a way to load rule dependencies in non-bzlmod usage.
Functions & Macros
cc_hdrs_map_workspace_depsLoad all dependencies of cc_hdrs_map.
@rules_cc_hdrs_map//cc_hdrs_map/actions:cc_helper.bzl
This module serves chiefly as a vehicle for exposing 'private' cc_helper methods to current rule set.
Functions & Macros
cc_helper.expand_make_variables_in_coptsParameters
*ctx | |
*cc_info | |
*action_kwargs | |
*opts |
cc_helper.expand_make_variables_in_definesParameters
*ctx | |
*cc_info | |
*action_kwargs | |
*defines | |
local | Default: False |
cc_helper.expand_make_variables_in_linkoptsParameters
*ctx | |
*cc_info | |
*action_kwargs | |
*opts |
cc_helper.extensions.cc_headercc_helper.extensions.cc_sourcecc_helper.extract_headersParameters
*files |
cc_helper.extract_sourcesParameters
*files |
cc_helper.get_cc_flags_make_variableParameters
*cc_toolchain | |
*feature_configuration |
cc_helper.get_compilation_definesParameters
*ctx | |
defines | Default: [] |
deps | Default: [] |
additional_make_variable_substitutions | Default: {} |
additional_targets | Default: [] |
cc_helper.get_compilation_optsParameters
*ctx | |
*opts | |
*feature_configuration | |
additional_make_variable_substitutions | Default: {} |
additional_inputs | Default: [] |
cc_helper.get_linking_optsParameters
*ctx | |
*opts | |
additional_make_variable_substitutions | Default: {} |
additional_inputs | Default: [] |
cc_helper.get_local_defines_for_runfiles_lookupParameters
*ctx | |
*all_deps |
cc_helper.get_toolchain_global_make_variablesParameters
*cc_toolchain |
cc_helper.prepare_for_compilationMaterialize information from hdrs map.
This function creates a epheremal directory, that contains all of the
patterns specified within hdrs_map providers, thus making them all
available under singular, temporary include statment.
Parameters
*sctx | subrule context |
*input_hdrs_map | list of HdrsMapInfo which should be used for materialization of compilation context |
*input_hdrs | direct headers provided to the action |
*input_implementation_hdrs | direct headers provided to the action |
*input_deps | dependencies specified for the action |
*input_includes | include statements specified for the action |
@rules_cc_hdrs_map//cc_hdrs_map/actions:compile.bzl
This module contains logic responsible for HdrsMap handling and compilation phase.
@rules_cc_hdrs_map//cc_hdrs_map/actions:copy_file.bzl
This module exposes the copy_file action. Done to avoid dependency towards skylib (or others).
Functions & Macros
copy_fileCopy file from a to b.
This function will copy file from given source to specified destination.
Parameters
*ctx_actions | |
*src | (File) source file that should be copied. Must exist. |
*dst | (File) destination file that should be created. Must be declared beforehand. |
@rules_cc_hdrs_map//cc_hdrs_map/actions:defs.bzl
This module defines the CC compilation phase actions which are exposed publicalyl as subrules.
Functions & Macros
actions.compile_kwargsParameters
*ctx | |
*rule_attrs |
actions.link_to_archive_kwargsParameters
*ctx | |
*rule_attrs |
actions.link_to_binary_kwargsParameters
*ctx | |
*rule_attrs |
actions.link_to_so_kwargsParameters
*ctx | |
*rule_attrs |
actions.prepare_for_compilationMaterialize information from hdrs map.
This function creates a epheremal directory, that contains all of the
patterns specified within hdrs_map providers, thus making them all
available under singular, temporary include statment.
Parameters
*sctx | subrule context |
*input_hdrs_map | list of HdrsMapInfo which should be used for materialization of compilation context |
*input_hdrs | direct headers provided to the action |
*input_implementation_hdrs | direct headers provided to the action |
*input_deps | dependencies specified for the action |
*input_includes | include statements specified for the action |
@rules_cc_hdrs_map//cc_hdrs_map/actions:link_to_archive.bzl
" This module contains logic responsible for linking into .a file.
@rules_cc_hdrs_map//cc_hdrs_map/actions:link_to_binary.bzl
This module contains logic responsible for creation of executable binary.
@rules_cc_hdrs_map//cc_hdrs_map/actions:link_to_so.bzl
This module contains logic responsible for linking into .so file.
This module contains function that help with dealing with CcSharedLibraryInfo provider.
Providers
This module contains function that help with dealing with CcSharedLibraryInfo provider.
Functions & Macros
@rules_cc_hdrs_map//cc_hdrs_map/providers:hdrs_map_info.bzl
Module describing HdrsMapInfo provider and common operations on it.
Functions & Macros
quotient_map_hdrs_map_infosTake all HdrsMapInfo key-values and group them by keys.
Parameters
targets | Default: [] |
hdrs | additional header files to include, Default: None |
implementation_hdrs | additional implementation headers to include, Default: None |
hdrs_map | initial hdrs_map to use as a foundation for merge, Default: None |
hdrs_map_deps | additional dependencies to include Default: None |
traverse_deps | wheather to gather HdrsMapInfos transitvely Default: True |
merge_hdrs_map_infosMerge all HdrsMapInfo providers from targets into singular one.
Parameters
targets | Default: [] |
hdrs | additional header files to include, Default: None |
implementation_hdrs | additional implementation headers to include, Default: None |
hdrs_map | initial hdrs_map to use as a foundation for merge, Default: None |
hdrs_map_deps | additional dependencies to include, Default: None |
pin_down_non_globs | wheather the final hdrs_map should have its Default: True |
Providers
HdrsMapInfoRepresents grouping of CC header files, alongsdie with their intended include paths.
Fields
hdrs | Headers which should be exposed after the compilation is done. |
implementation_hdrs | Headers that should not be propagated after the compilation. |
hdrs_map | (hdrs_map struct) object describing desired header file mappings |
deps | CcInfo/CcSharedLibraryInfo-aware dependencies that need to be propagated, for this provider to compile and link |
@rules_cc_hdrs_map//cc_hdrs_map/providers:hdrs_map.bzl
Module describing HdrsMap struct and common operations on it.
Functions & Macros
new_hdrs_mapCreate new instance of HdrsMap struct.
Parameters
from_dict | Default: {} |
_glob | Default: None |
_non_glob | Default: None |
materialize_hdrs_mappingMaterialize the expected file hierarchy.
Creates the header file hierarchy accordingly to specifications
in passed-in hdrs_map under 'vhm' directory.
Parameters
*invoker_label | label of rule invoking the method |
*actions | bazel ctx.actions |
*hdrs_map | HdrsMapInfo representing the headers mapping |
*hdrs | list of all header files that should be matched against the map |
@rules_cc_hdrs_map//cc_hdrs_map/providers:helper.bzl
This module exposes the common operatiosn done towards providers as a helper struct.
Functions & Macros
providers_helper.materialize_hdrs_mappingMaterialize the expected file hierarchy.
Creates the header file hierarchy accordingly to specifications
in passed-in hdrs_map under 'vhm' directory.
Parameters
*invoker_label | label of rule invoking the method |
*actions | bazel ctx.actions |
*hdrs_map | HdrsMapInfo representing the headers mapping |
*hdrs | list of all header files that should be matched against the map |
providers_helper.merge_hdrs_map_infosMerge all HdrsMapInfo providers from targets into singular one.
Parameters
targets | Default: [] |
hdrs | additional header files to include, Default: None |
implementation_hdrs | additional implementation headers to include, Default: None |
hdrs_map | initial hdrs_map to use as a foundation for merge, Default: None |
hdrs_map_deps | additional dependencies to include, Default: None |
pin_down_non_globs | wheather the final hdrs_map should have its Default: True |
providers_helper.new_hdrs_mapCreate new instance of HdrsMap struct.
Parameters
from_dict | Default: {} |
_glob | Default: None |
_non_glob | Default: None |
providers_helper.quotient_map_hdrs_map_infosTake all HdrsMapInfo key-values and group them by keys.
Parameters
targets | Default: [] |
hdrs | additional header files to include, Default: None |
implementation_hdrs | additional implementation headers to include, Default: None |
hdrs_map | initial hdrs_map to use as a foundation for merge, Default: None |
hdrs_map_deps | additional dependencies to include Default: None |
traverse_deps | wheather to gather HdrsMapInfos transitvely Default: True |