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

cxx_binary()

This is liable to change in the future.

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

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.

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

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

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

  • 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
    The macro $declared_deps may be used anywhere a target literal pattern is expected in order to refer to the explicit deps of this rule as they appear in the rule's definition. For example, if your build rule declares
      android_library(
        name = 'lib',
        deps = ['//foo:foo'],
        deps_query = '$declared_deps',
      )
    then the macro $declared_deps would be expanded to a literal set(//foo:foo). Some example queries:
      "filter({name_regex}, $declared_deps)".format(name_regex='//.*')
      "attrfilter(annotation_processors, com.foo.Processor, $declared_deps)"

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

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

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

Flavors

  • #strip-debug (required) #

    If appended to the target name, it will strip debugging symbols, but saving local and global symbols.

  • #strip-non-global (required) #

    If appended to the target name, it will strip non-global symbols, but keeping external symbols. This is preferred for dynamic shared libraries.

  • #strip-all (required) #

    If appended to the target name, it will completely strip the binary, removing the symbol table and relocation information. This is preferred for executable files.

  • 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