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

First cut at runfiles support in pkg_* rules #605

Merged
merged 13 commits into from
Sep 15, 2022
30 changes: 26 additions & 4 deletions pkg/private/pkg_files.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ def add_label_list(
default_group: fallback mode to use for Package*Info elements without group
"""

if hasattr(ctx.attr, "include_runfiles"):
include_runfiles = ctx.attr.include_runfiles
else:
include_runfiles = False
aiuto marked this conversation as resolved.
Show resolved Hide resolved
# Compute the relative path
data_path = compute_data_path(
ctx,
Expand Down Expand Up @@ -289,6 +293,7 @@ def add_label_list(
default_mode = default_mode,
default_user = default_user,
default_group = default_group,
include_runfiles = include_runfiles,
)


Expand All @@ -300,7 +305,8 @@ def add_from_default_info(
data_path_without_prefix,
default_mode = None,
default_user = None,
default_group = None):
default_group = None,
include_runfiles = False):
"""Helper method to add the DefaultInfo of a target to a content_map.

Args:
Expand All @@ -312,6 +318,7 @@ def add_from_default_info(
default_mode: fallback mode to use for Package*Info elements without mode
default_user: fallback user to use for Package*Info elements without user
default_group: fallback mode to use for Package*Info elements without group
include_runfiles: Include runfiles
"""
if not DefaultInfo in src:
return
Expand All @@ -335,14 +342,29 @@ def add_from_default_info(
fmode = "0755" if f == the_executable else default_mode
add_single_file(
content_map,
d_path,
f,
dest_path = d_path,
src = f,
origin = src.label,
mode = fmode,
user = default_user,
group = default_group,
)

if include_runfiles:
aiuto marked this conversation as resolved.
Show resolved Hide resolved
runfiles = src[DefaultInfo].default_runfiles
if runfiles:
base_path = d_path + '.runfiles'
for rf in runfiles.files.to_list():
d_path = base_path + '/' + rf.short_path
fmode = "0755" if rf == the_executable else default_mode
_check_dest(content_map, d_path, rf, src.label)
content_map[d_path] = _DestFile(
src = rf,
entry_type = ENTRY_IS_FILE,
origin = src.label,
mode = fmode,
user = default_user,
group = default_group,
)

def get_my_executable(src):
"""If a target represents an executable, return its file handle.
Expand Down
5 changes: 3 additions & 2 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ py_test(
)

cc_binary(
name = "an_executable",
srcs = ["foo.cc"],
name = "an_executable",
srcs = ["foo.cc"],
data = ["BUILD"],
)

py_test(
Expand Down
3 changes: 2 additions & 1 deletion tests/mappings/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,13 @@ manifest_golden_test(
target = "utf8",
)

# Test handling of executable files
# Test handling of executable + runfiles
write_content_manifest(
name = "executable_manifest",
srcs = [
"//tests:an_executable",
],
include_runfiles = True,
)

alias(
Expand Down
4 changes: 3 additions & 1 deletion tests/mappings/executable.manifest.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[
[0, "an_executable", "tests/an_executable", "0755", null, null]
[0,"an_executable.runfiles/tests/BUILD","tests/BUILD","",null,null],
[0,"an_executable.runfiles/tests/an_executable","tests/an_executable","0755",null,null],
[0,"an_executable","tests/an_executable","0755",null,null]
]
4 changes: 3 additions & 1 deletion tests/mappings/executable.manifest.windows.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[
[0, "an_executable.exe", "tests/an_executable.exe", "0755", null, null]
[0,"an_executable.exe.runfiles/tests/an_executable.exe","tests/an_executable.exe","0755",null,null],
[0,"an_executable.exe.runfiles/tests/BUILD","tests/BUILD","",null,null],
[0,"an_executable.exe","tests/an_executable.exe","0755",null,null]
]
4 changes: 3 additions & 1 deletion tests/mappings/manifest_test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def assertManifestsMatch(self, expected, got):
e_file = ContentManifestTest.run_files.Rlocation('rules_pkg/' + expected)
with open(e_file, mode='rb') as e_fp:
expected = json.load(e_fp)
expected_dict = {x[1]: x for x in expected}
g_file = ContentManifestTest.run_files.Rlocation('rules_pkg/' + got)
with open(g_file, mode='rb') as g_fp:
got = json.load(g_fp)
self.assertEqual(expected, got)
got_dict = {x[1]: x for x in got}
self.assertEqual(expected_dict, got_dict)
10 changes: 7 additions & 3 deletions tests/util/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,19 @@ This is intended only for testing the manifest creation features.""",
""",
default = True,
),
"include_runfiles": attr.bool(),
},
)

def write_content_manifest(name, srcs):
def write_content_manifest(name, srcs, **kwargs):
out = kwargs.pop("out", name + ".manifest")
use_short_path = kwargs.pop("use_short_path", True)
_write_content_manifest(
name = name,
srcs = srcs,
use_short_path = True,
out = name + ".manifest",
out = out,
use_short_path = use_short_path,
**kwargs,
)

############################################################
Expand Down