rules.bzl 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 file tests.
  5. file_test uses the tests_as_input_file rule to transform test dependencies into
  6. a file which can be accessed as a list. This avoids long argument parsing.
  7. """
  8. load("@rules_cc//cc:defs.bzl", "cc_test")
  9. load("//bazel/cc_toolchains:defs.bzl", "cc_env")
  10. load("//bazel/manifest:defs.bzl", "manifest", "manifest_as_cpp")
  11. def file_test(
  12. name,
  13. tests,
  14. srcs = [],
  15. deps = [],
  16. data = [],
  17. args = [],
  18. prebuilt_binary = None,
  19. **kwargs):
  20. """Generates tests using the file_test base.
  21. There will be one main test using `name` that can be sharded, and includes
  22. all files. Additionally, per-file tests will be generated as
  23. `name.file_path`; these per-file tests will be manual.
  24. Args:
  25. name: The base name of the tests.
  26. tests: The list of test files to use as data, typically a glob.
  27. srcs: Passed to cc_test.
  28. deps: Passed to cc_test.
  29. data: Passed to cc_test.
  30. args: Passed to cc_test.
  31. prebuilt_binary: If set, specifies a prebuilt test binary to use instead
  32. of building a new one.
  33. **kwargs: Passed to cc_test.
  34. """
  35. # Ensure tests are always a filegroup for tests_as_input_file_rule.
  36. if prebuilt_binary:
  37. # TODO: The prebuilt_binary support is only used by explorer. We should
  38. # remove this once explorer is removed, and think about better factoring
  39. # approaches if we need it later for toolchain.
  40. tests_file = "{0}.tests".format(name)
  41. manifest(
  42. name = tests_file,
  43. srcs = tests,
  44. testonly = 1,
  45. )
  46. args = ["--explorer_test_targets_file=$(rootpath :{0})".format(tests_file)] + args
  47. native.sh_test(
  48. name = name,
  49. srcs = srcs + [prebuilt_binary],
  50. deps = deps,
  51. data = [":" + tests_file] + tests + data,
  52. args = args,
  53. env = cc_env(),
  54. **kwargs
  55. )
  56. else:
  57. manifest_cpp = "{0}_autogen_manifest.cpp".format(name)
  58. manifest_as_cpp(
  59. name = manifest_cpp,
  60. var_name = "CarbonFileTestManifest",
  61. srcs = tests,
  62. testonly = 1,
  63. )
  64. cc_test(
  65. name = name,
  66. srcs = srcs + [":" + manifest_cpp],
  67. deps = deps + ["//testing/file_test:manifest_impl"],
  68. data = tests + data,
  69. args = args,
  70. env = cc_env(),
  71. **kwargs
  72. )