bazelbuild / rules_pkg

Bazel rules for creating packages of many types (zip, tar, deb, rpm, ...)
Apache License 2.0
217 stars 174 forks source link

Use cases for packaging runfiles #642

Open philsc opened 1 year ago

philsc commented 1 year ago

I had the chance to talk to @aiuto at Bazelcon last week and I wanted to note the use cases I had in mind for the discussions we had. The discussions were related to packaging runfiles.

Use case 1

This is already detailed in #631. We have a macro around pkg_tar that solves the problem. But if feels like it should really be a flag in pkg_tar instead.

Use case 2

This use case is around runfiles for deployed binaries. For example, we currently have:

cc_binary(
    name = "bin",
    ...
)
pkg_files(
    name = "bin_dir",
    srcs = [":bin"],
    prefix = "opt/foo/bin",
)
pkg_files(
    name = "systemd_startup_files",
    ...
)
pkg_tar(
    name = "deployable_tar",
    srcs = [
        ":bin_dir",
        ":systemd_startup_files",
    ],
)

I.e. we have a package that gets deployed so that the binaries get started on boot by systemd. This is the most bare example I can think of that is still representative of our use case.

The trouble comes up under 2 scenarios:

  1. bin is compiled against a pre-compiled library libfoo.so, or
  2. bin has data dependencies. E.g. a config file conf.json.

In both scenarios the result is that bin has some runfiles. Unfortunately, it looks like the pkg_files rule discards runfiles information. So the data dependencies and the pre-compiled libraries don't make it into the tarball regardless of the include_runfiles attribute's value.

I don't know what the right answer is here, but it would be kind of nice to say something like this:

pkg_files(
    name = "bin_dir",
    srcs = [":bin"],
    prefix = "opt/foo/bin",
    cc_runfiles_prefix = "opt/foo/lib",
    data_runfiles_prefix = "opt/foo/share",
)

I suspect there's no way for pkg_files to distinguish between the binary's data dependencies and the .so files it links against. In that case, a simplified version that would achieve something similar would be:

pkg_files(
    name = "bin_dir",
    srcs = [":bin"],
    prefix = "opt/foo/bin",
    runfiles_prefix = "opt/foo/data",
)

Right now we work around this by not using pkg_fies when we need runfiles support. Instead, we use pkg_tar because of its include_runfiles support.