Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: add a new make variable to genrule's cmd #7248

Closed
ity opened this issue Jan 25, 2019 · 2 comments
Closed

Feature request: add a new make variable to genrule's cmd #7248

ity opened this issue Jan 25, 2019 · 2 comments
Labels
team-Rules-Server Issues for serverside rules included with Bazel untriaged

Comments

@ity
Copy link
Contributor

ity commented Jan 25, 2019

Description of the problem / feature request:

Add new Make variable to genrule's cmd which always points to the output directory containing output from running genrule

Feature requests: what underlying problem are you trying to solve with this feature?

Currently, when writing a genrule, the cmd attribute gets some values substituted into it (see https://docs.bazel.build/versions/master/be/make-variables.html). One of these is the output directory (substituted for $(@D)), but where outs are nested in directories, what directory this refers to differs depending on whether there is one, or more than one, out. Specifically:

    name = "gr",
    outs = [ 
        "foo/bar",
        #"baz/bar",
    ],  
    cmd = "for out in $(OUTS); do echo hello > $(@D)/$$(basename $${out}); done",
)```
If only `foo/bar` is present as an output, `$(@D)` points to the directory which will contain bar and this genrule works with output like:

```$ tree bazel-genfiles/tools/test/
bazel-genfiles/tools/test/
└── foo
    └── bar

1 directory, 1 file

If baz/bar is uncommented, $(@D) now points to the directory which will contain foo and baz:

$ tree bazel-genfiles/tools/test/
bazel-genfiles/tools/test/
├── baz
└── foo

2 directories, 0 files

and neither file is correctly generated and returns an error. The proposed improvement is a new Make variable named something like $(OUT_ROOT_DIR) which will always point at the directory that contains foo and baz, rather than having dynamic behaviour based on the number of outs.

Then the genrule would look like:
genrule(
name = "gr",
outs = [
"foo/bar",
"baz/bar",
],
cmd = "for out in foo baz; do echo hello > $(OUT_ROOT_DIR)/$${out}/bar; done",
)

Any other information, logs, or outputs that you want to share?

Attaching a draft wip PR with this issue to kickstart the work and happy to hear thoughts/ideas.

@lberki
Copy link
Contributor

lberki commented Jan 28, 2019

$@D does indeed have awful semantics. Thinking out loud:

  1. We do have $GENDIR and $BINDIR
  2. The author of the rule knows both its label and whether it outputs to the genfiles or the bin directory
  3. However, we don't have access to the repository to the genrule is in

So $OUT_ROOT_DIR is actually useful (contrary to what I said earlier in the code review). Let me take a look again.

bazel-io pushed a commit that referenced this issue Mar 4, 2019
###Description of the problem / feature request:
Add new Make variable to genrule's cmd which always points to the output directory containing output from running genrule. See #7248 for details.

Closes #7249.

RELNOTES[NEW]: genrules now support a $(RULEDIR) variable that resolves to the directory where the outputs of the rule are put.

PiperOrigin-RevId: 236612681
@lberki
Copy link
Contributor

lberki commented Mar 13, 2019

The pull request is now merged so I'll mark this as fixed.

@lberki lberki closed this as completed Mar 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-Rules-Server Issues for serverside rules included with Bazel untriaged
Projects
None yet
Development

No branches or pull requests

3 participants