Buck: cxx_library()

cxx_library()

This is liable to change in the future.

A cxx_library() rule represents a set of C/C++ source files and provides various flags to control how they get built. Other than that, C/C++ libraries by themselves are relatively abstract, and it's dependent on the various top-level targets (e.g. C/C++ binary, Android APK) that transitively depend on this rule how this library actually gets built (e.g. a C/C++ binary will require a static non-PIC build of the library, while an Android APK will require a shared PIC-enabled build).

Arguments

  • name (required) #

    The name of the rule.

  • 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. 'foo/bar.c') or a tuple of a string specifying a source file and a list of compilation flags (e.g. ('foo/bar.c', ['-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).

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

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

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

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

  • preprocessor_flags (defaults to []) #

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

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

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

  • compiler_flags (defaults to []) #

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

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

  • linker_flags (defaults to []) #

    Flags to use whenever this rule is involved in a link operation (e.g linked into an executable or shared library).

  • platform_linker_flags (defaults to []) #

    Platform specific linker 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 this target is involved in a link operation. See linker_flags for more information.

  • exported_linker_flags (defaults to []) #

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

  • exported_platform_linker_flags (defaults to []) #

    Platform specific linker 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 the target and all targets that transitively depend on it are involved in a link operation if the platform matches the regex. See exported_linker_flags for more information.

  • soname (defaults to None) #

    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.

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

  • 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. Currently, this only affects linking: exported dependencies will also be included in the link line of dependents of this rules, but normal dependencies will not.

  • precompiled_header (defaults to None) #

    Path to a cxx_precompiled_header to use when compiling this rule's sources. The precompiled header (PCH) is built on-demand, using compiler flags matching those used in this rule's compile jobs. This is to ensure compatibility between this rule and the PCH. Also, this rule will inherit additional deps from the PCH rule, and as a result, additional include paths as well (e.g. -I, -isystem, -iquote path lists, and framework paths specified with -F).

  • tests (defaults to []) #

    List of build targets that identify the test rules that exercise this target.

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