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 option to include runfiles files in the select_file search #467

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/select_file_doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Selects a single file from the outputs of a target by given relative path.
## select_file

<pre>
select_file(<a href="#select_file-name">name</a>, <a href="#select_file-srcs">srcs</a>, <a href="#select_file-subpath">subpath</a>)
select_file(<a href="#select_file-name">name</a>, <a href="#select_file-include_runfiles">include_runfiles</a>, <a href="#select_file-srcs">srcs</a>, <a href="#select_file-subpath">subpath</a>)
</pre>

Selects a single file from the outputs of a target by given relative path
Expand All @@ -22,6 +22,7 @@ Selects a single file from the outputs of a target by given relative path
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="select_file-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="select_file-include_runfiles"></a>include_runfiles | Whether to include the runfiles in the search | Boolean | optional | <code>False</code> |
| <a id="select_file-srcs"></a>srcs | The target producing the file among other outputs | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="select_file-subpath"></a>subpath | Relative path to the file | String | required | |

Expand Down
9 changes: 8 additions & 1 deletion rules/select_file.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def _impl(ctx):

out = None
canonical = ctx.attr.subpath.replace("\\", "/")
for file_ in ctx.attr.srcs.files.to_list():
files_ = ctx.attr.srcs.files.to_list()
if ctx.attr.include_runfiles and ctx.attr.srcs.default_runfiles:
files_ += ctx.attr.srcs.default_runfiles.files.to_list()
for file_ in files_:
if file_.path.replace("\\", "/").endswith(canonical):
out = file_
break
Expand All @@ -49,5 +52,9 @@ select_file = rule(
mandatory = True,
doc = "Relative path to the file",
),
"include_runfiles": attr.bool(
default = False,
doc = "Whether to include the runfiles in the search",
),
},
)