Skip to main content

prebuilt_cxx_library

A prebuilt_cxx_library() rule represents a set of native libraries and C/C++ header files and provides various flags to control how they are linked and exported.

Function Signature​

def prebuilt_cxx_library(
*,
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 | target_name_glob] = [],
within_view: list[str | target_name_glob] = ["PUBLIC"],
metadata: OpaqueMetadata = {},
tests: list[str] = [],
modifiers: OpaqueMetadata = [],
allow_cache_upload: None | bool = None,
can_be_asset: bool = False,
contacts: list[str] = [],
default_deps: str = "exported_deps",
deffile: None | str = None,
deps: list[str] = [],
exported_deps: list[str] = [],
exported_header_style: str = "system",
exported_headers: list[str] | dict[str, str] = [],
exported_lang_preprocessor_flags: dict[str, list[str]] = {},
exported_linker_flags: list[str] = [],
exported_post_linker_flags: list[str] = [],
exported_preprocessor_flags: list[str] = [],
extract_soname: bool = False,
force_static: bool = False,
frameworks: list[str] = [],
header_dirs: None | list[str] = None,
header_namespace: None | str = None,
header_only: bool = False,
import_lib: None | str = None,
include_in_android_merge_map_output: bool = True,
labels: list[str] = [],
libraries: list[str] = [],
licenses: list[str] = [],
link_whole: bool = False,
link_without_soname: bool = False,
linker_flags: list[str] = [],
local_linker_flags: list[str] = [],
local_linker_script_flags: list[str] = [],
platform_import_lib: None | list[(str, str)] = None,
post_linker_flags: list[str] = [],
preferred_linkage: str = "any",
prestripped: bool = False,
provided: bool = False,
public_include_directories: list[str] = [],
public_system_include_directories: list[str] = [],
raw_headers: list[str] = [],
shared_lib: None | str = None,
soname: None | str = None,
static_lib: None | str = None,
static_pic_lib: None | str = None,
stub: bool = False,
supported_platforms_regex: None | str = None,
supports_lto: bool = False,
supports_merged_linking: None | bool = None,
supports_python_dlopen: bool = True,
supports_shared_library_interface: bool = True,
third_party_build: None | str = None,
version: None | str = None,
versioned_exported_lang_preprocessor_flags: list[(dict[str, str], dict[str, list[str]])] = [],
versioned_exported_preprocessor_flags: list[(dict[str, str], list[str])] = [],
versioned_header_dirs: None | list[(dict[str, str], list[str])] = None,
versioned_import_lib: None | list[(dict[str, str], str)] = None,
versioned_shared_lib: None | list[(dict[str, str], str)] = None,
versioned_soname: None | list[(dict[str, str], str)] = None,
versioned_static_lib: None | list[(dict[str, str], str)] = None,
versioned_static_pic_lib: None | list[(dict[str, str], str)] = None,
) -> 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

  • 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).

  • contacts: (defaults to: [])

    A list of organizational contacts for this rule. These could be individuals who you would contact in the event of a failure or other issue with the rule.

    contacts = [ 'Joe Sixpack', 'Erika Mustermann' ]
  • default_deps: (defaults to: "exported_deps")

    Whether to include the default deps from the toolchain, and where to include them (deps, exported_deps, etc).

  • deffile: (defaults to: None)

    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.

  • deps: (defaults to: [])

    Libraries that this library needs when it is linked statically into another target: they are added to that link. They are not used when a shared library is linked from static_pic_lib or static_lib, and shared libraries among them are not packaged with the executables, so a dependency that is needed there belongs in exported_deps. Their headers and preprocessor flags are not made available to the targets that depend on this library.

  • exported_deps: (defaults to: [])

    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_headers: (defaults to: [])

    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_linker_flags: (defaults to: [])

    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_post_linker_flags: (defaults to: [])

    Flags to add to the linker command line in the links that take this library as an input. With GNU and Windows linkers they are placed right after the library. They are not used when a shared library is linked from static_pic_lib or static_lib.

  • exported_preprocessor_flags: (defaults to: [])

    Flags to use when preprocessing the sources of the targets that depend on this library. A library that depends on this one passes them on to its own dependents if it lists this one in exported_deps.

  • extract_soname: (defaults to: False)

    If True and soname is not set, the soname is read from shared_lib with objdump instead of being derived from the name of the target.

  • force_static: (defaults to: False)

    If True, this library is always linked statically, even into targets that link their dependencies as shared libraries. Takes precedence over preferred_linkage.

  • header_dirs: (defaults to: None)

    A directory that headers can be included from. These directories are added to the include path using -isystem.

  • 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. See headers for more information.

  • header_only: (defaults to: False)

    Indicates if this library only consists of headers or not. If this is set to True, Buck will not link this library into any library that depends on it.

  • import_lib: (defaults to: None)

    The import library (.lib) that dependents pass to the linker on Windows, in place of the DLL. It has an effect only when this library has a shared library: shared_lib, or one that is linked from static_pic_lib or static_lib.

  • labels: (defaults to: [])

    Set of arbitrary strings which allow you to annotate a build rule with tags that can be searched for over an entire dependency tree using buck query().

  • licenses: (defaults to: [])

    Set of license files for this library. To get the list of license files for a given build rule and all of its dependencies, you can use buck query

  • link_whole: (defaults to: False)

    If True, every object file of the static library is linked into the executables and shared libraries that link it, including the object files that nothing refers to (for example with --whole-archive). By default the linker only takes the object files that it needs.

  • link_without_soname: (defaults to: False)

    If True, dependents link against shared_lib with -L<directory of the library> and -l<name> instead of passing the path of the library to the linker, where <name> is the file name without its lib prefix and without everything from its first dot. This is for a shared library that has no soname, so that the path it has during the build is not recorded in the binaries that link it.

  • linker_flags: (defaults to: [])

    Flags to add to the linker command line when a shared library is linked from static_pic_lib or static_lib, which happens when this library is linked as a shared library and shared_lib is not set. They are not used in the links that take this library as an input: exported_linker_flags is for those.

  • local_linker_flags: (defaults to: [])

    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).

  • local_linker_script_flags: (defaults to: [])

    Linker script lags to add to the linker command line whenever the output from this rule is used in a link operation driven by this rule. Used by rules that need to treat linker script flags different from normal linker flags.

  • preferred_linkage: (defaults to: "any")

    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.

  • prestripped: (defaults to: False)

    When set, skips running strip commands when building this library.

  • provided: (defaults to: False)

    If True, the library is taken to be present already on the system where the built code runs. Dependents link against shared_lib, unless force_static or preferred_linkage says otherwise, and the shared library is not packaged with the executables that use it.

  • public_include_directories: (defaults to: [])

    Directories to add, with -I, to the include path of every target that depends on this library. A directory is relative to the package of this target. List the headers it contains in raw_headers.

  • public_system_include_directories: (defaults to: [])

    Directories to add as system include directories (-isystem, or /external:I with MSVC and clang-cl) to every target that depends on this library. A directory is relative to the package of this target. List the headers it contains in raw_headers.

  • raw_headers: (defaults to: [])

    Header files of this library that dependents include by their path under public_include_directories or public_system_include_directories, rather than under a header_namespace as with exported_headers. Listing a header makes it an input of the compilations that depend on this library. It adds nothing to the include path.

  • shared_lib: (defaults to: None)

    The path to the library to use when performing shared linking.

  • soname: (defaults to: None)

    The file name under which the shared library is made available to the executables that load it. It is also the name and soname of the shared library that is linked from the static library when shared_lib is not set. Defaults to the platform's form of lib<name>.so, where <name> is the name of the target. A .$(ext) at the end expands to the platform's shared library extension, and takes an optional version: libfoo.$(ext 2.3) is libfoo.so.2.3 on Linux and libfoo.2.3.dylib on macOS.

  • static_lib: (defaults to: None)

    The path to the library to use when performing static linking.

  • static_pic_lib: (defaults to: None)

    The path to the library to use when performing static PIC linking.

  • supported_platforms_regex: (defaults to: None)

    Deprecated: do not set this attribute in new code. Instead, use target_compatible_with to restrict the platforms a target can be built for.

    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.

  • supports_merged_linking: (defaults to: None)

    Whether this rule supports building with the merged linking strategy when building for non-native binaries (e.g. when using .buckconfig s merged setting).

  • version: (defaults to: None)

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

Examples​

A prebuilt library containing only headers that other libraries may need.


prebuilt_cxx_library(
name = 'stdutil',
header_only = True,
header_dirs = [
'include',
],
)

A prebuilt library with static and shared libs.


prebuilt_cxx_library(
name = 'mylib',
soname = 'libmylib.so',
static_lib = 'libmylib.a',
static_pic_lib = 'libmylib_pic.a',
shared_lib = 'libmylib.so',
exported_headers = [
'mylib.h',
],
)