forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CurrentRepository() to Python runfiles library
`runfiles.CurrentRepository()` can be used to get the canonical name of the Bazel repository containing the caller at runtime. This information is required to look up runfiles while taking repository mappings into account. Work towards bazelbuild#16124 Closes bazelbuild#16341. PiperOrigin-RevId: 483400557 Change-Id: Ia906c42bad6ca0935b86b958e4401c6745789dff
- Loading branch information
1 parent
6578b5b
commit d60ce2c
Showing
6 changed files
with
238 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2022 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""A rule to generate a Python source file containing the main repo's | ||
runfiles directory name.""" | ||
|
||
_RUNFILES_CONSTANTS_TEMPLATE = """# Generated by gen_runfiles_constants.bzl | ||
# Internal-only; do no use. | ||
# The name of the runfiles directory corresponding to the main repository. | ||
MAIN_REPOSITORY_RUNFILES_DIRECTORY = '%s' | ||
""" | ||
|
||
def _gen_runfiles_constants_impl(ctx): | ||
out = ctx.actions.declare_file(ctx.attr.name + ".py") | ||
ctx.actions.write(out, _RUNFILES_CONSTANTS_TEMPLATE % ctx.workspace_name) | ||
|
||
return DefaultInfo( | ||
files = depset([out]), | ||
runfiles = ctx.runfiles([out]), | ||
) | ||
|
||
gen_runfiles_constants = rule( | ||
implementation = _gen_runfiles_constants_impl, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
load("//tools/python:gen_runfiles_constants.bzl", "gen_runfiles_constants") | ||
load("//tools/python:private/defs.bzl", "py_library") | ||
|
||
py_library( | ||
name = "runfiles", | ||
srcs = ["runfiles.py"], | ||
srcs = [ | ||
"runfiles.py", | ||
":_runfiles_constants", | ||
], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
gen_runfiles_constants( | ||
name = "_runfiles_constants", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters