Use --output=starlark trick for all build path parsing.

There is no reason to limit this to debian paths.

PiperOrigin-RevId: 392469415
This commit is contained in:
Adin Scannell 2021-08-23 10:50:37 -07:00 committed by gVisor bot
parent 0a15a216da
commit 8dc3be7a61
4 changed files with 27 additions and 29 deletions

View File

@ -444,7 +444,7 @@ $(RELEASE_ARTIFACTS)/%:
@mkdir -p $@
@$(call copy,//runsc:runsc,$@)
@$(call copy,//shim:containerd-shim-runsc-v1,$@)
@$(call deb_copy,//debian:debian,$@)
@$(call copy,//debian:debian,$@)
release: $(RELEASE_KEY) $(RELEASE_ARTIFACTS)/$(ARCH)
@mkdir -p $(RELEASE_ROOT)

View File

@ -1,9 +0,0 @@
"""Formatter to extract the output files from pkg_deb."""
def format(target):
provider_map = providers(target)
return "\n".join([
provider_map["OutputGroupInfo"].out.to_list()[0].path,
provider_map["OutputGroupInfo"].deb.to_list()[0].path,
provider_map["OutputGroupInfo"].changes.to_list()[0].path,
])

View File

@ -181,35 +181,17 @@ endif
# build_paths extracts the built binary from the bazel stderr output.
#
# This could be alternately done by parsing the bazel build event stream, but
# this is a complex schema, and begs the question: what will build the thing
# that parses the output? Bazel? Do we need a separate bootstrapping build
# command here? Yikes, let's just stick with the ugly shell pipeline.
#
# The last line is used to prevent terminal shenanigans.
build_paths = \
(set -euo pipefail; \
$(call wrapper,$(BAZEL) build $(BASE_OPTIONS) $(BAZEL_OPTIONS) $(1)) 2>&1 \
| tee /dev/fd/2 \
| sed -n -e '/^Target/,$$p' \
| sed -n -e '/^ \($(subst /,\/,$(subst $(SPACE),\|,$(BUILD_ROOTS)))\)/p' \
| sed -e 's/ /\n/g' \
| awk '{$$1=$$1};1' \
| strings \
| xargs -r -n 1 -I {} readlink -f "{}" \
| xargs -r -n 1 -I {} bash -c 'set -xeuo pipefail; $(2)')
debian_paths = \
(set -euo pipefail; \
$(call wrapper,$(BAZEL) build $(BASE_OPTIONS) $(BAZEL_OPTIONS) $(1)) && \
$(call wrapper,$(BAZEL) cquery $(BASE_OPTIONS) $(BAZEL_OPTIONS) $(1) --output=starlark --starlark:file=debian/show_paths.bzl) \
$(call wrapper,$(BAZEL) cquery $(BASE_OPTIONS) $(BAZEL_OPTIONS) $(1) --output=starlark --starlark:file=tools/show_paths.bzl) \
| xargs -r -n 1 -I {} readlink -f "{}" \
| xargs -r -n 1 -I {} bash -c 'set -xeuo pipefail; $(2)')
clean = $(call header,CLEAN) && $(call wrapper,$(BAZEL) clean)
build = $(call header,BUILD $(1)) && $(call build_paths,$(1),echo {})
copy = $(call header,COPY $(1) $(2)) && $(call build_paths,$(1),cp -fa {} $(2))
deb_copy = $(call header,COPY $(1) $(2)) && $(call debian_paths,$(1),cp -fa {} $(2))
run = $(call header,RUN $(1) $(2)) && $(call build_paths,$(1),{} $(2))
sudo = $(call header,SUDO $(1) $(2)) && $(call build_paths,$(1),sudo -E {} $(2))
test = $(call header,TEST $(1)) && $(call wrapper,$(BAZEL) test $(TEST_OPTIONS) $(1))

25
tools/show_paths.bzl Normal file
View File

@ -0,0 +1,25 @@
"""Formatter to extract the output files from a target."""
def format(target):
provider_map = providers(target)
outputs = dict()
# Try to resolve in order.
files_to_run = provider_map.get("FilesToRunProvider", None)
default_info = provider_map.get("DefaultInfo", None)
output_group_info = provider_map.get("OutputGroupInfo", None)
if files_to_run and files_to_run.executable:
outputs[files_to_run.executable.path] = True
elif default_info:
for x in default_info.files:
outputs[x.path] = True
elif output_group_info:
for entry in dir(output_group_info):
# Filter out all built-ins and anything that is not a depset.
if entry.startswith("_") or not hasattr(getattr(output_group_info, entry), "to_list"):
continue
for x in getattr(output_group_info, entry).to_list():
outputs[x.path] = True
# Return all found files.
return "\n".join(outputs.keys())