0

I have certain build targets which need to be build in two ways - one for my production application and one for my test code. I would like it if I only had to specify which targets had this requirement one time. For example, I have a macro that does something like the following:

# //my_macro.bzl

def my_macro(name, srcs, test_deps=[], deps=[], **kwargs):
  native.cc_library(
    name = name,
    srcs = srcs,
    deps = deps + test_deps,
    **kwargs
  )

  native.cc_library(
    name = name + "_test",
    srcs = srcs,
    deps = deps + [x + "_test" for x in test_deps],
    **kwargs
  )

Then if I had the following build file:

# //BUILD.bazel
...

my_macro(
  name = "A",
  srcs = ["A.cpp"],
  hdrs = ["A.hpp"],
)

cc_library(
  name = "B",
  srcs = ["B.cpp"],
  hdrs = ["B.hpp"],
)

my_macro(
  name = "C",
  srcs = ["C.cpp"],
  hdrs = ["C.hpp"],
  test_deps = [":A"],
  deps = [":B"]
)

the targets that I could build are:

//A
//A_test
//B
//C (linked with //A)
//C_test (linked with //A_test)

I would love it if I could have a rule or macro that could basically take one list of deps and figure out automatically if something should be in deps or test_deps. Then I could change the build file to be the following:

# //BUILD.bazel
...

my_macro(
  name = "A",
  srcs = ["A.cpp"],
  hdrs = ["A.hpp"],
)

cc_library(
  name = "B",
  srcs = ["B.cpp"],
  hdrs = ["B.hpp"],
)

my_macro(
  name = "C",
  srcs = ["C.cpp"],
  hdrs = ["C.hpp"],
  deps = [":A", ":B"]
)

which would correspond to the same build targets. I was trying to accomplish this with aspects but then I discovered you can't edit the build graph in the analysis phase so I got stuck. Has anyone done something similar to this or knows how to accomplish what I'm trying to do?

Any help is appreciated.

Andrew King
  • 145
  • 10

0 Answers0