Buck: cxx_library()
Support Ukraine. Help Provide Humanitarian Aid to Ukraine.

cxx_library()

This is liable to change in the future.

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

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 an [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 buck 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.

Arguments

  • name (required) #

    The short name for this build target.

  • 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_header_style (defaults to local) #

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

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

  • lang_preprocessor_flags (defaults to {}) #

    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

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

  • lang_platform_preprocessor_flags (defaults to {}) #

    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.

  • 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_lang_preprocessor_flags (defaults to {}) #

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

  • 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_lang_platform_preprocessor_flags (defaults to {}) #

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

  • compiler_flags (defaults to []) #

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

  • lang_compiler_flags (defaults to {}) #

    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

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

  • lang_platform_compiler_flags (defaults to {}) #

    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.

  • linker_extra_outputs (defaults to []) #

    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 (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_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.

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

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

    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.

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

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

  • public_include_directories (defaults to []) #

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

    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.

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

  • reexport_all_header_dependencies (defaults to True) #

    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.

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

    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.

  • 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 tests that exercise this target.

  • extra_xcode_sources (defaults to []) #

    When the project is generated, this is the list of files that will added to the build phase "Compile Sources" of the given target.

  • extra_xcode_files (defaults to []) #

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

  • visibility (defaults to []) #

    List of build target patterns that identify the build rules that can include this rule as a dependency, for example, by listing it in their deps or exported_deps attributes. For more information, see visibility.

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