Skip to content

Commit 447fb8e

Browse files
authored
Add support for repo mapping (#878)
* Add support for repo mapping Esp. when include_runfiles = True Closes #769 Signed-off-by: Thomas Lam <[email protected]> * Update windows golden manifest Signed-off-by: Thomas Lam <[email protected]> * Split repo_mapping manifest checking logic into its own function Signed-off-by: Thomas Lam <[email protected]> * Comment Signed-off-by: Thomas Lam <[email protected]> --------- Signed-off-by: Thomas Lam <[email protected]>
1 parent 6a44f01 commit 447fb8e

File tree

6 files changed

+100
-17
lines changed

6 files changed

+100
-17
lines changed

pkg/mappings.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ here.
2929

3030
load("@bazel_skylib//lib:paths.bzl", "paths")
3131
load("//pkg:providers.bzl", "PackageDirsInfo", "PackageFilegroupInfo", "PackageFilesInfo", "PackageSymlinkInfo")
32+
load("//pkg/private:util.bzl", "get_repo_mapping_manifest")
3233

3334
# TODO(#333): strip_prefix module functions should produce unique outputs. In
3435
# particular, this one and `_sp_from_pkg` can overlap.
@@ -311,6 +312,13 @@ def _pkg_files_impl(ctx):
311312
else:
312313
src_dest_paths_map[rf] = dest_path
313314

315+
# if repo_mapping manifest exists (for e.g. with --enable_bzlmod),
316+
# create _repo_mapping under runfiles directory
317+
repo_mapping_manifest = get_repo_mapping_manifest(target)
318+
if repo_mapping_manifest:
319+
dest_path = paths.join(src_dest_paths_map[src] + ".runfiles", "_repo_mapping")
320+
src_dest_paths_map[repo_mapping_manifest] = dest_path
321+
314322
# At this point, we have a fully valid src -> dest mapping in src_dest_paths_map.
315323
#
316324
# Construct the inverse of this mapping to pass to the output providers, and

pkg/private/pkg_files.bzl

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ load(
3939
"PackageFilesInfo",
4040
"PackageSymlinkInfo",
4141
)
42+
load(
43+
"//pkg/private:util.bzl",
44+
"get_files_to_run_provider",
45+
"get_repo_mapping_manifest",
46+
)
4247

4348
ENTRY_IS_FILE = "file" # Entry is a file: take content from <src>
4449
ENTRY_IS_LINK = "symlink" # Entry is a symlink: dest -> <src>
@@ -75,7 +80,6 @@ _MappingContext = provider(
7580
"include_runfiles": "bool: include runfiles",
7681
"workspace_name": "string: name of the main workspace",
7782
"strip_prefix": "strip_prefix",
78-
7983
"path_mapper": "function to map destination paths",
8084

8185
# Defaults
@@ -95,8 +99,7 @@ def create_mapping_context_from_ctx(
9599
strip_prefix = None,
96100
include_runfiles = None,
97101
default_mode = None,
98-
path_mapper = None
99-
):
102+
path_mapper = None):
100103
"""Construct a MappingContext.
101104
102105
Args: See the provider definition.
@@ -400,7 +403,8 @@ def add_from_default_info(
400403
all_files = src[DefaultInfo].files.to_list()
401404
for f in all_files:
402405
d_path = mapping_context.path_mapper(
403-
dest_path(f, data_path, data_path_without_prefix))
406+
dest_path(f, data_path, data_path_without_prefix),
407+
)
404408
if f.is_directory:
405409
add_tree_artifact(
406410
mapping_context.content_map,
@@ -422,6 +426,7 @@ def add_from_default_info(
422426
user = mapping_context.default_user,
423427
group = mapping_context.default_group,
424428
)
429+
425430
if include_runfiles:
426431
runfiles = src[DefaultInfo].default_runfiles
427432
if runfiles:
@@ -449,6 +454,43 @@ def add_from_default_info(
449454
gid = mapping_context.default_gid,
450455
)
451456

457+
# if repo_mapping manifest exists (for e.g. with --enable_bzlmod),
458+
# create _repo_mapping under runfiles directory
459+
repo_mapping_manifest = get_repo_mapping_manifest(src)
460+
if repo_mapping_manifest:
461+
mapping_context.file_deps.append(depset([repo_mapping_manifest]))
462+
463+
# TODO: This should really be a symlink into .runfiles/_repo_mapping
464+
# that also respects remap_paths. For now this is duplicated with the
465+
# repo_mapping file within the runfiles directory
466+
d_path = mapping_context.path_mapper(dest_path(
467+
repo_mapping_manifest,
468+
data_path,
469+
data_path_without_prefix,
470+
))
471+
add_single_file(
472+
mapping_context,
473+
dest_path = d_path,
474+
src = repo_mapping_manifest,
475+
origin = src.label,
476+
mode = mapping_context.default_mode,
477+
user = mapping_context.default_user,
478+
group = mapping_context.default_group,
479+
)
480+
481+
runfiles_repo_mapping_path = mapping_context.path_mapper(
482+
base_file_path + ".runfiles/_repo_mapping",
483+
)
484+
add_single_file(
485+
mapping_context,
486+
dest_path = runfiles_repo_mapping_path,
487+
src = repo_mapping_manifest,
488+
origin = src.label,
489+
mode = mapping_context.default_mode,
490+
user = mapping_context.default_user,
491+
group = mapping_context.default_group,
492+
)
493+
452494
def get_my_executable(src):
453495
"""If a target represents an executable, return its file handle.
454496
@@ -461,21 +503,15 @@ def get_my_executable(src):
461503
Returns:
462504
File or None.
463505
"""
464-
if not DefaultInfo in src:
465-
return None
466-
di = src[DefaultInfo]
467-
if not hasattr(di, "files_to_run"):
468-
return None
469-
ftr = di.files_to_run
506+
507+
files_to_run_provider = get_files_to_run_provider(src)
470508

471509
# The docs lead you to believe that you could look at
472510
# files_to_run.executable, but that is filled out even for source
473511
# files.
474-
if not hasattr(ftr, "runfiles_manifest"):
475-
return None
476-
if ftr.runfiles_manifest:
477-
# DEBUG print("Got an manifest executable", ftr.executable)
478-
return ftr.executable
512+
if getattr(files_to_run_provider, "runfiles_manifest"):
513+
# DEBUG print("Got an manifest executable", files_to_run_provider.executable)
514+
return files_to_run_provider.executable
479515
return None
480516

481517
def add_single_file(mapping_context, dest_path, src, origin, mode = None, user = None, group = None, uid = None, gid = None):

pkg/private/util.bzl

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,37 @@ def substitute_package_variables(ctx, attribute_value):
9292
attribute_value = attribute_value.replace("$(", "{", 1)
9393
attribute_value = attribute_value.replace(")", "}", 1)
9494

95-
return attribute_value.format(**vars)
95+
return attribute_value.format(**vars)
96+
97+
def get_files_to_run_provider(src):
98+
"""Safely retrieve FilesToRunProvider from a target.
99+
100+
Args:
101+
src: target to get FilesToRunProvider from
102+
103+
Returns:
104+
FilesToRunProvider or None: FilesToRunProvider if found in target
105+
provider, otherwise None
106+
"""
107+
if not DefaultInfo in src:
108+
return None
109+
di = src[DefaultInfo]
110+
if not hasattr(di, "files_to_run"):
111+
return None
112+
return di.files_to_run
113+
114+
def get_repo_mapping_manifest(src):
115+
"""Safely retrieve repo_mapping_manifest from a target if it exists.
116+
117+
Args:
118+
src: target to get repo_mapping_manifest from
119+
120+
Returns:
121+
File or None: repo_mapping_manifest
122+
"""
123+
files_to_run_provider = get_files_to_run_provider(src)
124+
if files_to_run_provider:
125+
# repo_mapping_manifest may not exist in older Bazel versions (<7.0.0)
126+
# https://github.com/bazelbuild/bazel/issues/19937
127+
return getattr(files_to_run_provider, "repo_mapping_manifest")
128+
return None
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
[
2-
{"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},
32
{"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},
3+
{"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},
44
{"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},
55
{"dest":"an_executable","gid":null,"group":null,"mode":"0755","origin":"@//tests:an_executable","src":"tests/an_executable","type":"file","uid":null,"user":null},
6+
{"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},
7+
{"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},
68
{"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}
79
]

tests/mappings/executable.manifest.windows.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
{"dest":"an_executable.exe.runfiles/_main/tests/an_executable.exe","gid":null,"group":null,"mode":"0755","origin":"@//tests:an_executable","src":"tests/an_executable.exe","type":"file","uid":null,"user":null},
44
{"dest":"an_executable.exe.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},
55
{"dest":"an_executable.exe","gid":null,"group":null,"mode":"0755","origin":"@//tests:an_executable","src":"tests/an_executable.exe","type":"file","uid":null,"user":null},
6+
{"dest":"an_executable.exe.repo_mapping","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src": "tests/an_executable.exe.repo_mapping","type": "file","uid":null,"user":null},
7+
{"dest":"an_executable.exe.runfiles/_repo_mapping","gid":null,"group":null,"mode":"","origin":"@//tests:an_executable","src": "tests/an_executable.exe.repo_mapping","type": "file","uid":null,"user":null},
68
{"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}
79
]

tests/mappings/mappings_test.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,13 @@ def _test_pkg_files_contents():
272272
{
273273
"@bazel_tools//src/conditions:windows": [
274274
"an_executable.exe",
275+
"an_executable.exe.runfiles/_repo_mapping",
275276
"an_executable.exe.runfiles/_main/tests/foo.cc",
276277
"an_executable.exe.runfiles/_main/tests/testdata/hello.txt",
277278
],
278279
"//conditions:default": [
279280
"an_executable",
281+
"an_executable.runfiles/_repo_mapping",
280282
"an_executable.runfiles/_main/tests/foo.cc",
281283
"an_executable.runfiles/_main/tests/testdata/hello.txt",
282284
],

0 commit comments

Comments
 (0)