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

Add $(location_dirname) template for resolving the directory of a label #23516

Open
jmillikin opened this issue Sep 5, 2024 · 4 comments · May be fixed by #23518
Open

Add $(location_dirname) template for resolving the directory of a label #23516

jmillikin opened this issue Sep 5, 2024 · 4 comments · May be fixed by #23518
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: feature request

Comments

@jmillikin
Copy link
Contributor

jmillikin commented Sep 5, 2024

Description of the feature request:

Allow $(location_dirname //some:target) to resolve to the directory that contains //some:target.

For example, if $(location //foo:bar) resolves to bazel-out/k8-fastbuild/bin/foo/bar.txt, then $(location_dirname //foo:bar) should resolve to bazel-out/k8-fastbuild/bin/foo.

Which category does this issue belong to?

Rules API

What underlying problem are you trying to solve with this feature?

This is useful when interacting with tools that write outputs to a named directory. Currently wrapping such a tool requires writing a custom build rule, but a $(location_dirname) template expanded by ctx.expand_location() would let them be wrapped in a genrule or Skylib's run_binary().

load("@bazel_skylib//rules:run_binary.bzl", "run_binary")

run_binary(
    name = "javacc_stage1_out",
    tool = "//bootstrap:javacc_stage1",
    srcs = ["src/main/javacc/JavaCC.jj"],
    outs = [
        "src/main/java/org/javacc/parser/JavaCCParser.java",
        "src/main/java/org/javacc/parser/JavaCCParserConstants.java"
    ],
    args = [
        # vvv New feature used here to specify the output directory vvv
        "-OUTPUT_DIRECTORY:$(location_dirname src/main/java/org/javacc/parser/JavaCCParser.java)",
        "$(location src/main/javacc/JavaCC.jj)",
    ],
)
@github-actions github-actions bot added the team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts label Sep 5, 2024
@fmeum
Copy link
Collaborator

fmeum commented Sep 5, 2024

In the case of an output directory, you should be able to use this instead of a custom rule today:

"-OUTPUT_DIRECTORY:$(BINDIR)/{}/src/main/java/org/javacc/parser".format(package_name())

I would still like to see a way to support expansions to directory paths more easily, especially for multiple files.

@jmillikin
Copy link
Contributor Author

I just tried the $(BINDIR) approach, and it doesn't work because @bazel_skylib//rules:run_binary.bzl only expands locations, not make variables.

run_binary(
    name = "javacc_java_jj",
    tool = "@javacc//:javacc",
    srcs = ["src/main/javacc/java.jj"],
    outs = [
        "GeneratedJavaParserConstants.java",
        "GeneratedJavaParser.java",
        "GeneratedJavaParserTokenManager.java",
    ],
    args = [
        "-OUTPUT_DIRECTORY:$(BINDIR)/javaparser-core",
        "$(location src/main/javacc/java.jj)",
    ],
)
(cd /home/john/.cache/bazel/_bazel_john/d1588efe31a7420c307ba88a28892df0/execroot/_main && \
  exec env - \
  bazel-out/k8-opt-exec-ST-d57f47055a04/bin/external/javacc~/javacc '-OUTPUT_DIRECTORY:$(BINDIR)/javaparser-core' javaparser-core/src/main/javacc/java.jj)

There is a comment to this effect within the skylib sources, so it seems like the behavior is intentional-ish:

# To keep the rule simple, do not expand Make Variables (like *_binary.args usually would).
# (We can add this feature later if users ask for it.)

@comius comius added P3 We're not considering working on this, but happy to review a PR. (No assignee) and removed untriaged labels Sep 10, 2024
@comius
Copy link
Contributor

comius commented Sep 10, 2024

location_dirname makes sense.

Also expanding make variables in run_binary makes sense. If you prepare a PR I'll review it.

@jmillikin
Copy link
Contributor Author

The proposed PR to Bazel is #23518 -- it adds $(execpath_dirname) and $(rootpath_dirname). After filing this issue, I discovered that $(location) appears to be deprecated in favor of $(execpath) and $(rootpath).

I'm hesitant to add make variable expansions in places where they're currently not enabled, because I've had builds break in the past from unexpected expansions. I'll leave the skylib changes to someone else who might have stronger preference for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants