Search code examples
binarydirectorypackagingbazel

How do I package all of my binaries in bazel?


There is a plethora of BUILD files scattered throughout the hierarchy of my mono repo.
Some of these files contain cc_binary rules.
I know they are all built into bazel-bin, but I'd like to get easy access to them all.
How can I package them all up, and put them all into ~/.bin/ for example?

I see the packaging rules, but its not clear to me how to write a rule that captures every single program and packages them together.


Solution

  • It may not be the most elegant solution (plus I hope I got the question), but this is how we do it by packaging/"tarring" each binary in its own bazel package / BUILD file:

    cc_binary(
        name = "hello"
        ...
    )
    
    load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
    pkg_tar(
        name = "hello_pkg",
        srcs = [":hello"],
        mode = "0755",
        package_dir = "/usr/bin",
    )
    

    And then we'd collect all those into a one overall tarball/package in project root:

    pkg_tar(
        name = "mypkg",
        extension = "tar.gz",
        deps = [
            "//hello:hello_pkg",
            ...
        ],
    )
    

    Sometimes we'd actually have multiple such rules for hello to collect for instance executables under bin and libraries in lib with intermediary hello_bin and hello_lib targets. Which would in the same fashion as mypkg above be first aggregated into hello_pkg and that in turn would be used in mypkg.