rules.bzl 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. deps = [],
  14. **kwargs):
  15. """Macro for C++ fuzzing test.
  16. In order to run tests on a single file, run the fuzzer binary under
  17. bazel-bin directly. That will avoid the args being passed by Bazel.
  18. Args:
  19. name: The main fuzz test rule name.
  20. corpus: List of files to use as a fuzzing corpus.
  21. args: Will have the locations of the corpus files added and passed down
  22. to the fuzz test.
  23. data: Will have the corpus added and passed down to the fuzz test.
  24. features: Will have the "fuzzer" feature added and passed down to the
  25. fuzz test.
  26. tags: Will have "fuzz_test" added and passed down to the fuzz test.
  27. deps: Will have "@llvm-project//compiler-rt:FuzzerMain" added and passed
  28. down to the fuzz test.
  29. **kwargs: Remaining arguments passed down to the fuzz test.
  30. """
  31. # Add relevant tag and feature if necessary.
  32. if "fuzz_test" not in tags:
  33. tags = tags + ["fuzz_test"]
  34. if "fuzzer" not in features:
  35. features = features + ["fuzzer"]
  36. if "@llvm-project//compiler-rt:FuzzerMain" not in deps:
  37. deps = deps + ["@llvm-project//compiler-rt:FuzzerMain"]
  38. # Append the corpus files to the test arguments. When run on a list of
  39. # files rather than a directory, libFuzzer-based fuzzers will perform a
  40. # regression test against the corpus.
  41. data = data + corpus
  42. args = args + ["$(location %s)" % file for file in corpus]
  43. cc_test(
  44. name = name,
  45. args = args,
  46. data = data,
  47. features = features,
  48. tags = tags,
  49. deps = deps,
  50. **kwargs
  51. )