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 example of how to depend one pkg_deb .changes file #484

Merged
merged 19 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions examples/where_is_my_output/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@ pkg_deb(
package = "mwp",
version = "3.14",
)

# We can also depend just on the .changes file

filegroup(
name = "the_changes_file",
srcs = [":deb"],
output_group = "changes",
)
aiuto marked this conversation as resolved.
Show resolved Hide resolved

genrule(
name = "use_changes_file",
srcs = [":the_changes_file"],
outs = ["copy_of_changes.txt"],
cmd = "cp $(location :the_changes_file) $@",
)
37 changes: 34 additions & 3 deletions examples/where_is_my_output/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ to inspect a target and print exactly what we need. Let's try it:

```shell
bazel build :deb
bazel cquery :deb --output=starlark --starlark:file=show_deb_outputs.bzl 2>/dev/null
bazel cquery :deb --output=starlark --starlark:file=show_all_outputs.bzl 2>/dev/null
```

That should produce something like
Expand All @@ -35,15 +35,15 @@ changes: bazel-out/k8-fastbuild/bin/mwp_3.changes

### How it works

show_deb_outputs.bzl is a Starlark script that must contain a function with the
show_all_outputs.bzl is a Starlark script that must contain a function with the
name `format`, that takes a single argument. The argument is typically named
target, and is a configured Bazel target, as you might have access to while
writing a custom rule. We can inspect its providers and print them in a useful
way.

For pkg_deb, there are two files, the .deb file and the .changes, and both are
passed along in the rule's OutputGroupInfo provider. This snippet below (from
show_deb_outputs.bzl) prints them.
show_all_outputs.bzl) prints them.

```python
def format(target):
Expand All @@ -64,3 +64,34 @@ def format(target):
A full explanation of why this works is beyond the scope of this example. It
requires some knowledge of how to write custom Bazel rules. See the Bazel
documentation for more information.

## Using an implicit output as input to another rule.

Sometimes a rule will create an implicit output that the user does not
explicitly specify as an attribute of the target. The .changes file from
pkg_deb is an example. If we want another rule to depend on an implicitly
created file, we can do that with a filegroup that specifies the specific
output group containing that file.

In the example below, `:deb` is a rule producing an explicit .deb output
and an implict .changes output. We refer to the .changes file using the
`filegroup` and specifying the desired output group name. Then, any rule
can use this `filegroup` as an input.

```python
aiuto marked this conversation as resolved.
Show resolved Hide resolved

pkg_deb(name = "deb", ...)

filegroup(
name = "the_changes_file",
srcs = [":deb"],
output_group = "changes",
)

genrule(
name = "use_changes_file",
srcs = [":the_changes_file"],
outs = ["copy_of_changes.txt"],
cmd = "cp $(location :the_changes_file) $@",
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# Extract the paths to the various outputs of pkg_deb
#
# Usage:
# bazel cquery //:debian --output=starlark --starlark:file=show_deb_outputs.bzl
# bazel cquery //:deb --output=starlark --starlark:file=show_all_outputs.bzl
#

def format(target):
Expand Down