cxx_binary
A cxx_binary() rule builds a native executable from the supplied set of C/C++ source files and dependencies. If C/C++ library dependencies are listed, the generated native executable will request and link against their static archives (which are *not* built using PIC).
Function Signature
def cxx_binary(
*,
name: str,
default_target_platform: None | str = None,
target_compatible_with: list[str] = [],
compatible_with: list[str] = [],
exec_compatible_with: list[str] = [],
visibility: list[str] = [],
within_view: list[str] = ["PUBLIC"],
metadata: OpaqueMetadata = {},
tests: list[str] = [],
modifiers: OpaqueMetadata = [],
_apple_platforms: dict[str, str] = {},
_build_info: dict[str, typing.Any] = {},
_cxx_hacks: str = "prelude//cxx/tools:cxx_hacks",
_cxx_toolchain: str = "gh_facebook_buck2_shims_meta//:cxx",
allow_cache_upload: None | bool = None,
anonymous_link_groups: bool = False,
auto_link_groups: bool = False,
binary_linker_flags: list[str] = [],
bolt_flags: list[str] = [],
bolt_profile: None | str = None,
compiler_flags: list[str] = [],
constraint_overrides: list[None | str] = [],
contacts: list[str] = [],
coverage_instrumentation_compiler_flags: list[str] = [],
cuda_compile_style: str = "mono",
cxx_runtime_type: None | str = None,
default_host_platform: None | str = None,
default_platform: None | str = None,
defaults: dict[str, str] = {},
deps: list[str] = [],
deps_query: None | str = None,
devirt_enabled: bool = False,
distributed_thinlto_partial_split_dwarf: bool = False,
enable_distributed_thinlto: bool = False,
executable_name: None | str = None,
exported_needs_coverage_instrumentation: bool = False,
extra_dwp_flags: list[str] = [],
fat_lto: bool = False,
focused_list_target: None | str = None,
frameworks: list[str] = [],
header_namespace: None | str = None,
headers: list[str] | dict[str, str] = [],
headers_as_raw_headers_mode: None | str = None,
include_directories: list[str] = [],
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 = False,
link_execution_preference: None | str = None,
link_group: None | str = None,
link_group_deps: list[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])] = None,
link_group_min_binary_node_count: None | int = None,
link_group_public_deps_label: None | str = None,
link_ordering: None | str = None,
link_style: None | str = None,
link_whole: bool = False,
linker_extra_outputs: list[str] = [],
linker_flags: list[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_override: None | str = None,
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 = None,
prefer_stripped_objects: bool = False,
prefix_header: None | str = None,
preprocessor_flags: list[str] = [],
raw_headers: list[str] = [],
raw_headers_as_headers_mode: None | str = None,
resources: list[str] | dict[str, str] = [],
runtime_dependency_handling: None | str = None,
separate_debug_info: bool = False,
srcs: list[str | (str, list[str])] = [],
thin_lto: bool = False,
use_fbcc_rust_wrapper: bool = False,
use_header_units: bool = False,
version_universe: None | str = None,
weak_framework_names: list[str] = [],
) -> None
Parameters
-
name
: (required)name of the target
-
default_target_platform
: (defaults to:None
)specifies the default target platform, used when no platforms are specified on the command line
-
target_compatible_with
: (defaults to:[]
)a list of constraints that are required to be satisfied for this target to be compatible with a configuration
-
compatible_with
: (defaults to:[]
)a list of constraints that are required to be satisfied for this target to be compatible with a configuration
-
exec_compatible_with
: (defaults to:[]
)a list of constraints that are required to be satisfied for this target to be compatible with an execution platform
-
visibility
: (defaults to:[]
)a list of visibility patterns restricting what targets can depend on this one
-
within_view
: (defaults to:["PUBLIC"]
)a list of visibility patterns restricting what this target can depend on
-
metadata
: (defaults to:{}
)a key-value map of metadata associated with this target
-
tests
: (defaults to:[]
)a list of targets that provide tests for this one
-
modifiers
: (defaults to:[]
)an array of modifiers associated with this target
-
_build_info
: (defaults to:{}
)Build info that is passed along here will be late-stamped into a fb_build_info section on the output binary
-
allow_cache_upload
: (defaults to:None
)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
: (defaults to:[]
)Flags to use when compiling any of the above sources (which require compilation).
-
deps_query
: (defaults to:None
)Status: experimental/unstable. The deps query takes a query string that accepts the following query functions, and appends the output of the query to the declared deps:
attrfilter
deps
except
intersect
filter
kind
set
union
Some example queries:
"filter({name_regex}, deps('//foo:foo'))".format(name_regex='//.*')
"attrfilter(annotation_processors, com.foo.Processor, deps('//foo:foo'))"
"deps('//foo:foo', 1)" -
header_namespace
: (defaults to:None
)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. Seeheaders
for more information. -
headers
: (defaults to:[]
)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'sheader_namespace
attribute, and$HEADER_NAME
is the header name if specified, and the filename of the header file otherwise. Seeheader_namespace
for more information. -
include_directories
: (defaults to:[]
)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. -
link_execution_preference
: (defaults to:None
)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_group_deps
: (defaults to:[]
)Additional targets to traverse when building link groups, but which should not be direct dependencies of the main executable.
-
link_group_public_deps_label
: (defaults to:None
)Surface nodes with this label as "public" nodes in the main executable when linking with with link groups.
-
link_style
: (defaults to:None
)Determines whether to build and link this rule's dependencies statically or dynamically. Can be either
static
,static_pic
orshared
. -
linker_extra_outputs
: (defaults to:[]
)Declares extra outputs that the linker emits. These identifiers can be used in
$(output ...)
macros inlinker_flags
to interpolate the output path into the linker command line. Useful for custom linkers that emit extra output files. -
linker_flags
: (defaults to:[]
)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.
-
platform_compiler_flags
: (defaults to:[]
)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
: (defaults to:[]
)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
: (defaults to:[]
)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
: (defaults to:[]
)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
: (defaults to:[]
)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. -
preprocessor_flags
: (defaults to:[]
)Flags to use when preprocessing any of the above sources (which require preprocessing).
-
raw_headers
: (defaults to:[]
)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
andpublic_include_directories
are the recommended way to add raw headers to the search path (they will be added via-I
).compiler_flags
,preprocessor_flags
andexported_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 withheaders
orexported_headers
in the same target. -
raw_headers_as_headers_mode
: (defaults to:None
)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.
-
runtime_dependency_handling
: (defaults to:None
)When this is set to
symlink
, shared library dependencies are included in a symlink tree alongside the resulting executable, even if the link style is not shared. Can benone
orsymlink
. -
srcs
: (defaults to:[]
)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). -
use_fbcc_rust_wrapper
: (defaults to:False
)Opt-in for the rust version of the fbcode C++ Compiler wrapper (replacing the existing fbcc.py)
Examples
# A rule that builds a C/C++ native executable from a single .cpp file
# its corresponding header, and a C/C++ library dependency.
cxx_binary(
name = 'echo',
srcs = [
'echo.cpp',
],
headers = [
'echo.h',
],
deps = [
':util',
],
)
cxx_library(
name = 'util',
srcs = [
'util.cpp',
],
headers = [
'util.h',
],
)
# To build without stripping:
buck build :echo
# To build with stripping debug symbols only:
buck build :echo#strip-debug