Buck: prebuilt_cxx_library()

prebuilt_cxx_library()

This is liable to change in the future.

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.

Arguments

  • name (required) #

    The name of the rule.

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

  • header_dirs (defaults to []) #

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

  • platform_header_dirs (defaults to []) #

    Platform specific header directories. 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 directories. See header_dirs for more information.

  • static_lib (defaults to []) #

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

  • platform_static_lib (defaults to []) #

    Platform specific static library. 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 the path to the library. See static_lib for more information.

  • static_pic_lib (defaults to []) #

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

  • platform_static_pic_lib (defaults to []) #

    Platform specific static PIC library. 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 the path to the library. See static_pic_lib for more information.

  • shared_lib (defaults to []) #

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

  • platform_shared_lib (defaults to []) #

    Platform specific shared library. 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 the path to the library. See shared_lib for more information.

  • supported_platforms_regex (defaults to None) #

    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.

  • 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_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 and all targets that transitively depend on it if the platform matches the regex. See headers for more information.

  • header_namespace (defaults to name) #

    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.

  • exported_preprocessor_flags (defaults to []) #

    Just as preprocessor_flags, flags to use when preprocessing any of the above sources (which require preprocessing). However, unlike preprocessor_flags, these preprocessor flags are also used by rules that transitively depend on this rule when preprocessing their own sources.

  • exported_platform_preprocessor_flags (defaults to []) #

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

    Linker flags to add to this library and to libraries that depend on this library.

  • force_static (defaults to False) #

    DEPRECATED: See preferred_linkage. If true, the library will always be linked statically, even if the target that depends on it specifies link_style to be something other than static. Note that this may still cause the library to be linked into its own shared library, if it happens to be the root of the linkable dependency tree (e.g. if a Python library directly depends on the library with force_static=True). Also note this will cause duplicate symbols if multiple targets that depend on the library are linked together.

  • preferred_linkage (defaults to any) #

    Controls how a library should be linked.

    any
    The library will be linked based on its dependents link_style.
    shared
    The library will be always be linked as a shared library.
    static
    The library will be linked as a static library.
    Note: since shared libraries re-export its dependencies, depending on multiple shared libraries which themselves have overlapping static dependencies will cause duplicate symbols.

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

  • supports_merged_linking (defaults to True) #

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

  • raw_headers (defaults to []) #

    Status: experimental/unstable. 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. compiler_flags or exported_compiler_flags can be used to add such raw headers to the search path. raw_headers are considered to be experimental for now and cannot be used together with headers or exported_headers in the same target. See CxxRawHeadersIntegrationTest for examples.

  • lib_name (defaults to {name}) #

    DEPRECATED: Please use newer *_lib parameters.
    The name of the library. If your library file was named libmylib.a, this should be mylib. This defaults to the name of the rule.

  • lib_dir (defaults to lib) #

    DEPRECATED: Please use newer *_lib parameters.
    The directory the library lib_name can be found. This can use the$(platform) macro to specify platform-dependent versions of the library.

  • include_dirs (defaults to None) #

    DEPRECATED: Please use newer header_dirs parameter.
    A directory that headers can be included from. This can use the $(platform)macro to specify platform-dependent versions of the directory.

  • visibility (defaults to []) #

    List of build target patterns that identify the build rules that can include this rule in its deps.

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

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

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',
  ],
)

A prebuilt library with multiple builds for multiple platforms.

prebuilt_cxx_library(
  name = 'mylib',
  soname = 'libmylib.so',
  platform_shared_lib = [
    ('android-arm', 'android-arm/libmylib.so'),
    ('android-x86', 'android-x86/libmylib.so'),
    ('iphonesimulator-x86_64', 'iphonesimulator-x86_64/libmylib.so'),
  ],
  platform_static_lib = [
    ('android-arm', 'android-arm/libmylib.a'),
    ('android-x86', 'android-x86/libmylib.a'),
    ('iphonesimulator-x86_64', 'iphonesimulator-x86_64/libmylib.a'),
  ],
  exported_headers = [
    'mylib.h',
  ],
)

Porting from deprecated API to the newer API.

# Porting this rule which uses the deprecated API...
prebuilt_cxx_library(
  name = 'mylib',
  lib_dir = 'lib',
  lib_name = 'mylib-1.0',
  include_dirs = [
    'include',
  ],
)
# ... becomes this using the new API.
prebuilt_cxx_library(
  name = 'mylib',
  soname = 'libmylib-1.0.so',
  static_lib = 'lib/libmylib-1.0.a',
  shared_lib = 'lib/libmylib-1.0.so',
  header_dirs = [
    'include',
  ],
)

Porting from deprecated API to the newer API when using the $(platform) macro.

# Porting this rule which uses the deprecated API...
prebuilt_cxx_library(
  name = 'mylib',
  lib_dir = 'lib/$(platform)',
  lib_name = 'mylib-1.0',
  include_dirs = [
    'include',
  ],
)
# ... becomes this using the new API.
prebuilt_cxx_library(
  name = 'mylib',
  soname = 'libmylib.so',
  platform_static_lib = [
    ('android-arm', 'lib/android-arm/libmylib-1.0.a'),
    ('android-x86', 'lib/android-x86/libmylib-1.0.a'),
    ('iphonesimulator-x86_64', 'iphonesimulator-x86_64/libmylib.a'),
  ],
  platform_shared_lib = [
    ('android-arm', 'lib/android-arm/libmylib-1.0.so'),
    ('android-x86', 'lib/android-x86/libmylib-1.0.so'),
    ('iphonesimulator-x86_64', 'iphonesimulator-x86_64/libmylib.so'),
  ],
  header_dirs = [
    'include',
  ],
)

While porting from the deprecated API to the newer API, it may also be useful to add helper macros which re-implement the old API using the new API:

import os


__all__ = ['deprecated_prebuilt_cxx_library']


def _get_lib(name, lib_dir, lib_name, ext):
    """
    Get a base-path relative library path with the given extension.
    """

    parts = []
    parts.append(lib_dir)
    parts.append('lib{}{}'.format(lib_name or name, ext))
    return os.path.join(*parts)


def _find_lib(name, lib_dir, lib_name, ext):
    """
    Return the base-path relative library path with the given extension if it
    exists, otherwise return `None`.
    """

    lib = _get_lib(name, lib_dir, lib_name, ext)
    lib_path = os.path.join(get_base_path(), lib)
    add_build_file_dep('//' + lib_path)

  ` # NOTE: While necessary to re-implement the deprecated API, I/O in BUCK
    # files is a *bad* idea and support for this may very likely be removed.
    if os.path.exists(lib_path):
        return lib


def deprecated_prebuilt_cxx_library(
        name,
        lib_dir='lib',
        lib_name=None,
        include_dirs=None,
        header_only=False,
        **kwargs):

    static_lib = None
    static_pic_lib = None
    shared_lib = None
    preferred_linkage = None

    if not header_only:
        static_lib = _find_lib(name, lib_dir, lib_name, '.a')
        static_pic_lib = _find_lib(name, lib_dir, lib_name, '_pic.a')
        shared_lib = _find_lib(name, lib_dir, lib_name, '.so')

        if static_lib is None and static_pic_lib is None:
            preferred_linkage = 'shared'

    prebuilt_cxx_library(
        name,
        static_lib=static_lib,
        static_pic_lib=static_pic_lib,
        shared_lib=shared_lib,
        preferred_linkage=preferred_linkage,
        header_only=header_only,
        header_dirs=include_dirs,
        **kwargs
    )