Skip to main content

cxx_library

name

def name(
*,
name: str,
default_target_platform: None | str = ...,
target_compatible_with: list[str] = ...,
compatible_with: list[str] = ...,
exec_compatible_with: list[str] = ...,
visibility: list[str] = ...,
within_view: list[str] = ...,
metadata: OpaqueMetadata = ...,
tests: list[str] = ...,
modifiers: OpaqueMetadata = ...,
_apple_platforms: dict[str, str] = ...,
_create_third_party_build_root: str = ...,
_cxx_hacks: str = ...,
_cxx_toolchain: str = ...,
_is_building_android_binary: bool = ...,
_meta_apple_library_validation_enabled: bool = ...,
allow_cache_upload: None | bool = ...,
archive_allow_cache_upload: bool = ...,
auto_link_groups: bool = ...,
bridging_header: None | str = ...,
can_be_asset: None | bool = ...,
compiler_flags: list[str] = ...,
contacts: list[str] = ...,
coverage_instrumentation_compiler_flags: list[str] = ...,
cuda_compile_style: str = ...,
cxx_runtime_type: None | str = ...,
default_host_platform: None | str = ...,
default_platform: None | str = ...,
defaults: dict[str, str] = ...,
deffile: None | str = ...,
deps: list[str] = ...,
deps_query: None | str = ...,
devirt_enabled: bool = ...,
diagnostics: dict[str, str] = ...,
executable_name: None | str = ...,
export_header_unit: None | str = ...,
export_header_unit_filter: list[str] = ...,
exported_deps: list[str] = ...,
exported_header_style: str = ...,
exported_headers: list[str] | dict[str, str] = ...,
exported_lang_platform_preprocessor_flags: dict[str, list[(str, list[str])]] = ...,
exported_lang_preprocessor_flags: dict[str, list[str]] = ...,
exported_linker_flags: list[str] = ...,
exported_needs_coverage_instrumentation: bool = ...,
exported_platform_deps: list[(str, list[str])] = ...,
exported_platform_headers: list[(str, list[str] | dict[str, str])] = ...,
exported_platform_linker_flags: list[(str, list[str])] = ...,
exported_platform_preprocessor_flags: list[(str, list[str])] = ...,
exported_post_linker_flags: list[str] = ...,
exported_post_platform_linker_flags: list[(str, list[str])] = ...,
exported_preprocessor_flags: list[str] = ...,
extra_xcode_files: list[str] = ...,
extra_xcode_sources: list[str] = ...,
fat_lto: bool = ...,
focused_list_target: None | str = ...,
force_static: None | bool = ...,
frameworks: list[str] = ...,
header_mode: None | str = ...,
header_namespace: None | str = ...,
headers: list[str] | dict[str, str] = ...,
headers_as_raw_headers_mode: None | str = ...,
include_directories: list[str] = ...,
include_in_android_merge_map_output: bool = ...,
labels: list[str] = ...,
lang_compiler_flags: dict[str, list[str]] = ...,
lang_platform_compiler_flags: dict[str, list[(str, list[str])]] = ...,
lang_platform_preprocessor_flags: dict[str, list[(str, list[str])]] = ...,
lang_preprocessor_flags: dict[str, list[str]] = ...,
libraries: list[str] = ...,
licenses: list[str] = ...,
link_deps_query_whole: bool = ...,
link_execution_preference: None | str = ...,
link_group: None | str = ...,
link_group_map: None | str | list[(str, list[(None | str | list[None | str], str, None | str | list[str], None | str)], None | dict[str, typing.Any])] = ...,
link_ordering: None | str = ...,
link_style: None | str = ...,
link_whole: None | bool = ...,
linker_extra_outputs: list[str] = ...,
linker_flags: list[str] = ...,
local_linker_flags: list[str] = ...,
module_name: None | str = ...,
platform_compiler_flags: list[(str, list[str])] = ...,
platform_deps: list[(str, list[str])] = ...,
platform_headers: list[(str, list[str] | dict[str, str])] = ...,
platform_linker_flags: list[(str, list[str])] = ...,
platform_preprocessor_flags: list[(str, list[str])] = ...,
platform_srcs: list[(str, list[str | (str, list[str])])] = ...,
post_linker_flags: list[str] = ...,
post_platform_linker_flags: list[(str, list[str])] = ...,
precompiled_header: None | str = ...,
prefer_stripped_objects: bool = ...,
preferred_linkage: str = ...,
prefix_header: None | str = ...,
preprocessor_flags: list[str] = ...,
public_include_directories: list[str] = ...,
public_system_include_directories: list[str] = ...,
raw_headers: list[str] = ...,
raw_headers_as_headers_mode: None | str = ...,
reexport_all_header_dependencies: None | bool = ...,
resources: list[str] | dict[str, str] = ...,
sdk_modules: list[str] = ...,
separate_debug_info: bool = ...,
soname: None | str = ...,
srcs: list[str | (str, list[str])] = ...,
static_library_basename: None | str = ...,
stub: bool = ...,
supported_platforms_regex: None | str = ...,
supports_header_symlink_subtarget: bool = ...,
supports_merged_linking: None | bool = ...,
supports_python_dlopen: None | bool = ...,
supports_shlib_interfaces: bool = ...,
thin_lto: bool = ...,
use_archive: None | bool = ...,
use_header_units: bool = ...,
used_by_wrap_script: bool = ...,
uses_cxx_explicit_modules: bool = ...,
uses_explicit_modules: bool = ...,
version: None | str = ...,
version_universe: None | str = ...,
weak_framework_names: list[str] = ...,
) -> None

A cxx_library() rule specifies a set of C/C++ source files and also provides flags that specify how those files should be built.

Parameters

  • name: name of the target

  • default_target_platform: specifies the default target platform, used when no platforms are specified on the command line

  • target_compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with a configuration

  • exec_compatible_with: a list of constraints that are required to be satisfied for this target to be compatible with an execution platform

  • visibility: a list of visibility patterns restricting what targets can depend on this one

  • within_view: a list of visibility patterns restricting what this target can depend on

  • metadata: a key-value map of metadata associated with this target

  • tests: a list of targets that provide tests for this one

  • modifiers: an array of modifiers associated with this target

  • allow_cache_upload: Whether to allow uploading the output of this rule to be uploaded to cache when the action is executed locally if the configuration allows (i.e. there is a cache configured and the client has permission to write to it).

  • compiler_flags: Flags to use when compiling any of the above sources (which require compilation).

  • deffile: Specifies the *.def file used on windows to modify a dll's exports in place of explicit __declspec(dllexport) declarations. The default is to not use a defile.

  • export_header_unit: If not None, export a C++20 header unit visible to dependants (including recursively) with use_header_units set to True.

    "include": replace includes of each file in exported_headers or raw_headers with an import of the precompiled header unit; files that do not include any of those headers do not load the header unit.

    "preload": automatically load the precompiled header unit in any dependant that uses header units.

  • export_header_unit_filter: A list of regexes. Each regex should match a set of headers in exported_headers or raw_headers to be precompiled together into one C++20 header unit.

    When used with export_header_unit="include", this allows different subsets of headers to be loaded only by files that use them. Each group should only depend on headers in previous groups.

    If a header is not matched by any group, it is not precompiled and will be included textually. If no filter is specified, the rule excludes inline headers based on a name heuristics (e.g. "-inl.h").

  • exported_deps: Dependencies that will also appear to belong to any rules that depend on this one. This has two effects: * Exported dependencies will also be included in the link line of dependents of this rules, but normal dependencies will not. * When reexport_all_header_dependencies = False, only exported headers of the rules specified here are re-exported.

  • exported_header_style: How dependents should include exported headers from this rule. Can be either local (e.g. -I) or system (e.g. -isystem).

  • exported_headers: The set of header files that are made available for inclusion to the source files in the target and all targets that transitively depend on it. These should be specified as either a list of header files or a dictionary of header names to header files. The headers can be included with #include "$HEADER_NAMESPACE/$HEADER_NAME" or #include <$HEADER_NAMESPACE/$HEADER_NAME>, where $HEADER_NAMESPACE is the value of the target's header_namespace attribute, and $HEADER_NAME is the header name if specified, and the filename of the header file otherwise. Note that the header name can contain forward slashes (/). See header_namespace for more information.

  • exported_lang_platform_preprocessor_flags: Just as lang_platform_preprocessor_flags, but these flags also apply to rules that transitively depend on this rule.

  • exported_lang_preprocessor_flags: Just as lang_preprocessor_flags, but these flags also apply to rules that transitively depend on this rule.

  • exported_linker_flags: Flags to add to the linker command line when the output from this rule, or the output from any rule that transitively depends on this rule, is used in a link operation.

  • exported_platform_deps: Platform specific dependencies that will also appear to belong to any rules that depend on this one. These should be specified as a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched, and the second element is a list of external dependencies (same format as exported_deps) that are exported if the platform matches the regex. See exported_deps for more information.

  • exported_platform_headers: Platform specific header files. These should be specified as a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched, and the second element is either a list of header files or a dictionary of header names to header files that will be made available for inclusion to the source files in the target and all targets that transitively depend on it if the platform matches the regex. See headers for more information.

  • exported_platform_linker_flags: Platform-specific linker flags for this rule and for all rules that transitively depend on this rule. This argument is specified as a list of pairs where the first element in each pair is an un-anchored regex against which the platform name is matched. The regex should use java.util.regex.Pattern syntax. The second element in each pair is a list of linker flags. If the regex matches the platform, these flags are added to the linker command line when the output from this rule, or the output from any rule that transitively depends on this rule, is used in a link operation.

  • exported_platform_preprocessor_flags: Platform specific exported preprocessor flags. These should be specified as a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched, and the second element is a list of flags to use when preprocessing the source files in the target and all targets that transitively depend on it if the platform matches the regex. See exported_preprocessor_flags for more information.

  • exported_post_linker_flags: Flags to add to the linker command line when the output from this rule, or the output from any rule that transitively depends on this rule, is used in a link operation—with the additional feature that these flags are guaranteed to be placed after the compiled object (.o) files on the linker command line.

  • exported_post_platform_linker_flags: Platform-specific linker flags for this rule and for all rules that transitively depend on this rule—and that are guaranteed to be placed after the compiled object (.o) files on the linker command line. In other respects, the syntax and semantics of this argument are the same as for the exported_platform_linker_flags argument.

  • extra_xcode_files: When the project is generated, this is the list of files that will added to the project. Those files won't be added to the build phase "Compile Sources".

  • header_namespace: A path prefix when including headers of this target. Defaults to the path from the root of the repository to the directory where this target is defined. Can contain forward slashes (/), but cannot start with one. See headers for more information.

  • headers: The set of header files that are made available for inclusion to the source files in this target. These should be specified as either a list of header files or a dictionary of header names to header files. The header name can contain forward slashes (/). The headers can be included with #include "$HEADER_NAMESPACE/$HEADER_NAME" or #include <$HEADER_NAMESPACE/$HEADER_NAME> , where $HEADER_NAMESPACE is the value of the target's header_namespace attribute, and $HEADER_NAME is the header name if specified, and the filename of the header file otherwise. See header_namespace for more information.

  • include_directories: A list of include directories (with raw_headers) to be added to the compile command for compiling this target (via -I). An include directory is relative to the current package.

  • lang_compiler_flags: Language-specific compiler flags. These should be specified as a map of C-family language short names to lists of flags and is used to target flags to sources files for a specific language in the C-family (C, C++, assembler, etc.). The keys in the map can be: * cpp-output for C * c++-cpp-output for C++ * objective-c-cpp-output for Objective-C * objective-c++-cpp-output for Objective-C++ * cuda-cpp-output for Cuda * assembler for Assembly * asm for ASM

  • lang_platform_compiler_flags: Language- and platform-specific compiler flags. These should be specified as a map of C-family language short names, as described in lang_compiler_flags, to lists of pairs, as described in platform_compiler_flags.

  • lang_platform_preprocessor_flags: Language- and platform-specific preprocessor flags. These should be specified as a map of C-family language short names, as described in lang_preprocessor_flags, to lists of pairs, as described in platform_preprocessor_flags.

  • lang_preprocessor_flags: Language-specific preprocessor flags. These should be specified as a map of C-family language short names to lists of flags and is used to target flags to sources files for a specific language in the C-family (C, C++, assembler, etc.). The keys in the map can be: * c for C * c++ for C++ * objective-c for Objective-C * objective-c++ for Objective-C++ * cuda for Cuda * assembler-with-cpp for Assembly * asm-with-cpp for ASM

  • link_execution_preference: The execution preference for linking. Options are:

    • any : No preference is set, and the link action will be performed based on buck2's executor configuration.
    • full_hybrid : The link action will execute both locally and remotely, regardless of buck2's executor configuration (if the executor is capable of hybrid execution). The use_limited_hybrid setting of the hybrid executor is ignored.
    • local : The link action will execute locally if compatible on current host platform.
    • local_only : The link action will execute locally, and error if the current platform is not compatible.
    • remote : The link action will execute remotely if a compatible remote platform exists, otherwise locally.

    The default is None, expressing that no preference has been set on the target itself.

  • link_style: Determines whether to build and link this rule's dependencies statically or dynamically. Can be either static, static_pic or shared.

  • linker_extra_outputs: Declares extra outputs that the linker emits. These identifiers can be used in $(output ...) macros in linker_flags to interpolate the output path into the linker command line. Useful for custom linkers that emit extra output files.

  • linker_flags: Flags to add to the linker command line whenever the output from this rule is used in a link operation, such as linked into an executable or a shared library.

  • local_linker_flags: Flags to add to the linker command line whenever the output from this rule is used in a link operation driven by this rule (e.g. when this rule links a shared library, but not when the output is linked into a shared library by another rule's link group links).

  • platform_compiler_flags: Platform specific compiler flags. These should be specified as a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched, and the second element is a list of flags to use when compiling the target's sources. See compiler_flags for more information.

  • platform_headers: Platform specific header files. These should be specified as a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched, and the second element is either a list of header files or a dictionary of header names to header files that will be made available for inclusion to the source files in the target if the platform matches the regex. See headers for more information.

  • platform_linker_flags: Platform-specific linker flags. This argument is specified as a list of pairs where the first element in each pair is an un-anchored regex against which the platform name is matched. The regex should use java.util.regex.Pattern syntax. The second element in each pair is a list of linker flags. If the regex matches the platform, these flags are added to the linker command line when the output from this rule is used in a link operation.

  • platform_preprocessor_flags: Platform specific preprocessor flags. These should be specified as a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched, and the second element is a list of flags to use when preprocessing the target's sources. See preprocessor_flags for more information.

  • platform_srcs: Platform specific source files. These should be specified as a list of pairs where the first element is an un-anchored regex (in java.util.regex.Pattern syntax) against which the platform name is matched, and the second element is either a list of source files or a list of tuples of source files and a list of compilation flags to be preprocessed, compiled and assembled if the platform matches the regex. See srcs for more information.

  • preferred_linkage: Determines what linkage is used when the library is depended on by another target. To control how the dependencies of this library are linked, use link_style instead.

  • preprocessor_flags: Flags to use when preprocessing any of the above sources (which require preprocessing).

  • public_include_directories: A list of include directories (with raw_headers) to be added to the compile command for compiling this target and every target that depends on it (via -I). An include directory is relative to the current package.

  • public_system_include_directories: A list of include directories (with raw_headers) to be added to the compile command for compiling this target and every target that depends on it (via -isystem if the compiler supports it of via -I otherwise). An include directory is relative to the current package.

  • raw_headers: The set of header files that can be used for inclusion to the source files in the target and all targets that transitively depend on it. Buck doesn't add raw headers to the search path of a compiler/preprocessor automatically. include_directories and public_include_directories are the recommended way to add raw headers to the search path (they will be added via -I). compiler_flags, preprocessor_flags and exported_preprocessor_flags can also be used to add such raw headers to the search path if inclusion via -isystem or -iquote is needed. raw_headers cannot be used together with headers or exported_headers in the same target.

  • raw_headers_as_headers_mode: Controls whether raw_headers and *include_directories attributes should be automatically converted to headers and symlink trees and/or header maps via headers. Only has an effect if the cxx_toolchain has explicitly opted into supporting this behavior via a non-default value, even if the value is disabled.

  • reexport_all_header_dependencies: Whether to automatically re-export the exported headers of all dependencies.

    When this is set to false, only exported headers from exported_deps are re-exported.

  • soname: Sets the soname ("shared object name") of any shared library produced from this rule. The default value is based on the full rule name. The macro $(ext) will be replaced with a platform-appropriate extension. An argument can be provided, which is a library version. For example soname = 'libfoo.$(ext 2.3)' will be libfoo.2.3.dylib on Mac and libfoo.so.2.3 on Linux.

  • srcs: The set of C, C++, Objective-C, Objective-C++, or assembly source files to be preprocessed, compiled, and assembled by this rule. We determine which stages to run on each input source based on its file extension. See the GCC documentation for more detail on how file extensions are interpreted. Each element can be either a string specifying a source file (e.g. '') or a tuple of a string specifying a source file and a list of compilation flags (e.g. ('', ['-Wall', '-Werror']) ). In the latter case the specified flags will be used in addition to the rule's other flags when preprocessing and compiling that file (if applicable).

  • supported_platforms_regex: If present, an un-anchored regex (in java.util.regex.Pattern syntax) that matches all platforms that this library supports. It will not be built for other platforms.

  • use_header_units: If True, makes any header unit exported by a dependency (including recursively) through export_header_unit available to the compiler. If false, the compilation ignores header units, regardless of what is exported by dependencies.

  • used_by_wrap_script: When using an exopackage Android, if this parameter is set to True, then the library is included in the primary APK even if native libraries would otherwise not be placed in it. This is intended for native libraries that are used by a wrap.sh script, which must be placed in the primary APK. Only one of can_be_asset and used_by_wrap_script can be set for a rule.

  • version: A string denoting a meaningful version of this rule that is optionally passed to the linker as extra metadata.

Details

Building requires a specified top-level target

Whether a Buck command builds the cxx_library is determined by the inclusion of a top-level target, such as a cxx_binary() or android_binary(), that transitively depends on the cxx_library. The set of targets specified to the Buck command (buck build, buck run, etc) must include one of these top-level targets in order for Buck to build the cxx_library. Note that you could specify the top-level target implicitly using a build target pattern or you could also specify the top-level target using a buckconfig alias defined in .buckconfig.

How Buck builds the library also depends on the specified top-level target. For example, a C/C++ binary (cxx_binary) would require a static non-PIC build of the library, whereas an Android APK (android_binary) would require a shared PIC-enabled build. (PIC stands for position-independent code.)

Dependencies of the cxx_library also require a top-level target

Similarly, in order for Buck to build a target that the cxx_library depends on, such as a cxx_genrule(), you must specify in the Buck command a top-level target that depends on the cxx_library. For example, you could specify to build a cxx_binary that depends on the cxx_library. If you specify as your build target the cxx_library itself, the build targets that the cxx_library depends on might not be built.

Examples:


# A rule that includes a single .cpp file and its corresponding header and
# also supplies an additional flag for compilation.
cxx_library(
name = 'fileutil',
srcs = [
'fileutil.cpp',
],
exported_headers = [
'fileutil.h',
],
compiler_flags = [
'-fno-omit-frame-pointer',
],
)

# A rule that defines explicit names for its headers
cxx_library(
name = 'mathutils',
header_namespace = 'math',
srcs = [
'trig/src/cos.cpp',
'trig/src/tan.cpp',
],
exported_headers = {
# These are included as <math/trig/cos.h> and <math/trig/tan.h>
'trig/cos.h': 'trig/include/cos.h',
'trig/tan.h': 'trig/include/tan.h',
},
compiler_flags = [
'-fno-omit-frame-pointer',
],
)

# A rule that uses different headers and sources per platform
cxx_library(
name = 'vector',
# Because of platform_headers, this file can include "config.h"
# and get the architecture specific header
srcs = ['vector.cpp'],
platform_srcs = [
('.*armv7$', 'armv7.S'),
('.*x86_64$', 'x86_64.S'),
],
exported_headers = [
'vector.h',
],
platform_headers = [
(
'.*armv7$',
{
'config.h': 'config-armv7.h',
}
),
(
'.*x86_64$',
{
'config.h': 'config-x86_64.h',
}
),
],
)