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 support for repo mapping #878

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions pkg/mappings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,15 @@ def _pkg_files_impl(ctx):
else:
src_dest_paths_map[rf] = dest_path

if (
aiuto marked this conversation as resolved.
Show resolved Hide resolved
getattr(target[DefaultInfo], "files_to_run") and
hasattr(target[DefaultInfo].files_to_run, "repo_mapping_manifest") and
target[DefaultInfo].files_to_run.repo_mapping_manifest != None
):
repo_mapping_manifest = target[DefaultInfo].files_to_run.repo_mapping_manifest
dest_path = paths.join(src_dest_paths_map[src] + ".runfiles", "_repo_mapping")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only adds example.runfiles/_repo_mapping -- example.repo_mapping is still missing.

src_dest_paths_map[repo_mapping_manifest] = dest_path

# At this point, we have a fully valid src -> dest mapping in src_dest_paths_map.
#
# Construct the inverse of this mapping to pass to the output providers, and
Expand Down
43 changes: 39 additions & 4 deletions pkg/private/pkg_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ _MappingContext = provider(
"include_runfiles": "bool: include runfiles",
"workspace_name": "string: name of the main workspace",
"strip_prefix": "strip_prefix",

"path_mapper": "function to map destination paths",

# Defaults
Expand All @@ -95,8 +94,7 @@ def create_mapping_context_from_ctx(
strip_prefix = None,
include_runfiles = None,
default_mode = None,
path_mapper = None
):
path_mapper = None):
"""Construct a MappingContext.

Args: See the provider definition.
Expand Down Expand Up @@ -400,7 +398,8 @@ def add_from_default_info(
all_files = src[DefaultInfo].files.to_list()
for f in all_files:
d_path = mapping_context.path_mapper(
dest_path(f, data_path, data_path_without_prefix))
dest_path(f, data_path, data_path_without_prefix),
)
if f.is_directory:
add_tree_artifact(
mapping_context.content_map,
Expand All @@ -422,6 +421,7 @@ def add_from_default_info(
user = mapping_context.default_user,
group = mapping_context.default_group,
)

if include_runfiles:
runfiles = src[DefaultInfo].default_runfiles
if runfiles:
Expand Down Expand Up @@ -449,6 +449,41 @@ def add_from_default_info(
gid = mapping_context.default_gid,
)

# if bzlmod is enabled, create _repo_mapping under runfiles directory
if (
getattr(src[DefaultInfo], "files_to_run") and
hasattr(src[DefaultInfo].files_to_run, "repo_mapping_manifest") and
src[DefaultInfo].files_to_run.repo_mapping_manifest != None
):
repo_mapping_manifest = src[DefaultInfo].files_to_run.repo_mapping_manifest
mapping_context.file_deps.append(depset([repo_mapping_manifest]))

d_path = mapping_context.path_mapper(dest_path(
repo_mapping_manifest,
data_path,
data_path_without_prefix,
))
add_single_file(
mapping_context,
dest_path = d_path,
src = repo_mapping_manifest,
origin = src.label,
mode = mapping_context.default_mode,
user = mapping_context.default_user,
group = mapping_context.default_group,
)
aiuto marked this conversation as resolved.
Show resolved Hide resolved

runfiles_repo_mapping_path = mapping_context.path_mapper(base_file_path + ".runfiles/_repo_mapping")
add_single_file(
mapping_context,
dest_path = runfiles_repo_mapping_path,
src = repo_mapping_manifest,
origin = src.label,
mode = mapping_context.default_mode,
user = mapping_context.default_user,
group = mapping_context.default_group,
)

def get_my_executable(src):
"""If a target represents an executable, return its file handle.

Expand Down
4 changes: 3 additions & 1 deletion tests/mappings/executable.manifest.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
[
{"dest":"an_executable.runfiles/_main/tests/foo.cc","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src":"tests/foo.cc","type":"file","uid":null,"user":null},
{"dest":"an_executable.runfiles/_main/tests/an_executable","gid":null,"group":null,"mode":"0755","origin":"@//tests:an_executable","src":"tests/an_executable","type":"file","uid":null,"user":null},
{"dest":"an_executable.runfiles/_main/tests/foo.cc","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src":"tests/foo.cc","type":"file","uid":null,"user":null},
{"dest":"an_executable.runfiles/_main/tests/testdata/hello.txt","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src":"tests/testdata/hello.txt","type":"file","uid":null,"user":null},
{"dest":"an_executable","gid":null,"group":null,"mode":"0755","origin":"@//tests:an_executable","src":"tests/an_executable","type":"file","uid":null,"user":null},
{"dest":"an_executable.repo_mapping","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src":"tests/an_executable.repo_mapping","type":"file","uid":null,"user":null},
{"dest":"an_executable.runfiles/_repo_mapping","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src":"tests/an_executable.repo_mapping","type":"file","uid":null,"user":null},
{"dest":"mappings_test.bzl","gid":null,"group":null,"mode":"","origin":"@//tests/mappings:mappings_test.bzl","src":"tests/mappings/mappings_test.bzl","type":"file","uid":null,"user":null}
]
2 changes: 2 additions & 0 deletions tests/mappings/mappings_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,13 @@ def _test_pkg_files_contents():
{
"@bazel_tools//src/conditions:windows": [
"an_executable.exe",
"an_executable.exe.runfiles/_repo_mapping",
"an_executable.exe.runfiles/_main/tests/foo.cc",
"an_executable.exe.runfiles/_main/tests/testdata/hello.txt",
],
"//conditions:default": [
"an_executable",
"an_executable.runfiles/_repo_mapping",
"an_executable.runfiles/_main/tests/foo.cc",
"an_executable.runfiles/_main/tests/testdata/hello.txt",
],
Expand Down