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

cxx_test()

This is liable to change in the future.

A cxx_test() rule builds a C/C++ binary against a C/C++ testing framework and runs it as part of buck test.

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

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

  • preprocessor_flags (defaults to []) #

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

  • compiler_flags (defaults to []) #

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

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

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

  • resources (defaults to []) #

    This attribute is currently not implemented, and just causes buck to rebuild the test file if any of the resources change. This will change in the future to provide a more reliable interface for resource files.

    Additional data or source files which this test uses.

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

  • framework (defaults to "gtest") #

    The testing framework to build against and run with. We currently support gtest and boost.

    When set to gtest, you must also set [cxx].gtest_dep.

  • env (defaults to {}) #

    A map of environment names and values to set when running the test.

    It is also possible to expand references to other rules within the values of these environment variables, using builtin string parameter macros:

    $(location //path/to:target)
    Expands to the location of the output of the build rule. This means that you can refer to these without needing to be aware of how Buck is storing data on the disk mid-build.

  • args (defaults to []) #

    A list of additional arguments to pass to the test when it's run.

    It is also possible to expand references to other rules within these arguments, using builtin string parameter macros:

    $(location //path/to:target)
    Expands to the location of the output of the build rule. This means that you can refer to these without needing to be aware of how Buck is storing data on the disk mid-build.

  • contacts (defaults to []) #

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

    contacts = [ 'Joe Sixpack', 'Erika Mustermann' ]
    

  • run_test_separately (defaults to False) #

    If set to True, the test(s) in this rule are run separately from all other tests. (This is useful for integration tests which access a physical device or other limited resource.)

    If unset, the test(s) in this rule in parallel with all other tests.

  • test_rule_timeout_ms (defaults to None) #

    If set specifies the maximum amount of time (in milliseconds) in which all of the tests in this rule should complete. This overrides the default rule_timeout if any has been specified in [test].rule_timeout.

  • 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 builds and runs C/C++ test using gtest.
cxx_test(
  name = 'echo_test',
  srcs = [
    'echo_test.cpp',
  ],
)