John Basila
John Basila

Reputation: 163

Bazel rules that use different subsets of genrule outputs

I have a code generator that produces three output files:

The genrule looks like this:

genrule(
  name = 'code_gen',
  tools = [ '//tools:code_gen.sh' ],
  outs = [ 'client.cpp', 'server.cpp', 'data.h' ],
  local = True,
  cmd = '$(location //tools:code_gen.sh) $(@D)')

The 'client.cpp' and 'server.cpp' each have their own cc_library rule.

My question is how to depend on the genrule but only use a specific output file.

What I did is create a macro that defined the genrule with specific outs set to the file required, but this resulted in multiple execution of the genrule:

gen.bzl:

def code_generator(
  name,
  out):
  native.genrule(
    name = name,
    tools = [ '//bazel:gen.sh' ],
    outs = [ out ],
    local = True,
    cmd = '$(location //bazel:gen.sh) $(@D)')

BUILD

load(':gen.bzl', 'code_generator')
code_generator('client_cpp', 'client.cpp')
code_generator('server_cpp', 'server.cpp')
code_generator('data_h', 'data.h')

cc_library(
  name = 'client',
  srcs = [ ':client_cpp' ],
  hdrs = [ ':data_h' ],
)

cc_library(
  name = 'server',
  srcs = [ ':server_cpp' ],
  hdrs = [ ':data_h' ],
)

Is there a way to depend on a genrule making it run once and then use only selected outputs from it?

Upvotes: 1

Views: 1333

Answers (1)

Damien Martin-Guillerez
Damien Martin-Guillerez

Reputation: 2370

You should be able to just use the filename (e.g. :server.cpp) to depend on a specific output of a rule.

Upvotes: 1

Related Questions