rules.bzl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. # Exceptions. See /LICENSE for license information.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. """Rules for building fuzz tests."""
  5. load("@rules_cc//cc:defs.bzl", "cc_test")
  6. def cc_fuzz_test(
  7. name,
  8. corpus,
  9. args = [],
  10. data = [],
  11. features = [],
  12. tags = [],
  13. **kwargs):
  14. """Macro for C++ fuzzing test.
  15. In order to run tests on a single file, run the fuzzer binary under
  16. bazel-bin directly. That will avoid the args being passed by bazel.
  17. Args:
  18. name: The main fuzz test rule name.
  19. corpus: List of files to use as a fuzzing corpus.
  20. args: Will have the locations of the corpus files added and passed down
  21. to the fuzz test.
  22. data: Will have the corpus added and passed down to the fuzz test.
  23. features: Will have the "fuzzer" feature added and passed down to the
  24. fuzz test.
  25. tags: Will have "fuzz_test" added and passed down to the fuzz test.
  26. **kwargs: Remaining arguments passed down to the fuzz test.
  27. """
  28. # Add relevant tag and feature if necessary.
  29. if "fuzz_test" not in tags:
  30. tags = tags + ["fuzz_test"]
  31. if "fuzzer" not in features:
  32. features = features + ["fuzzer"]
  33. # Append the corpus files to the test arguments. When run on a list of
  34. # files rather than a directory, libFuzzer-based fuzzers will perform a
  35. # regression test against the corpus.
  36. data = data + corpus
  37. args = args + ["$(location %s)" % file for file in corpus]
  38. cc_test(
  39. name = name,
  40. args = args,
  41. data = data,
  42. features = features,
  43. tags = tags,
  44. **kwargs
  45. )