tar.bzl
v0.3.0
published 3 weeks ago
5 stars
2 forks
3 watchers
Apache License 2.0
public
4 assets
40,458 downloads
55 KB
Compatibility level 1
WlDMpJLoSdUHSuCpAl91Ph7oHCDHpFiCzPme2NX1aTk=
Maintained byAlex Eagle,Şahin Yort
v0.3.0
May 15, 2025

Using Bzlmod with Bazel 6 or greater

  1. (Bazel 6 only) Enable with common --enable_bzlmod in .bazelrc.
  2. Add to your MODULE.bazel file:
bazel_dep(name = "tar.bzl", version = "0.3.0")

Using WORKSPACE

Paste this snippet into your WORKSPACE.bazel file:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
    name = "tar.bzl",
    sha256 = "5a50cca492e849d5074ae0a9025f753e1ee81c20c7a45882ccf99ed8d5f56939",
    strip_prefix = "tar.bzl-0.3.0",
    url = "https://github.com/bazel-contrib/tar.bzl/releases/download/v0.3.0/tar.bzl-v0.3.0.tar.gz",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

######################
# tar.bzl setup #
######################
http_archive(
    name = "bazel_skylib",
    sha256 = "bc283cdfcd526a52c3201279cda4bc298652efa898b10b4db0837dc51652756f",
    urls = [
        "https://github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz",
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz",
    ],
)

http_archive(
    name = "aspect_bazel_lib",
    sha256 = "2be8a5df0b20b0ed37604b050da01dbf7ad45ad44768c0d478b64779b9f58412",
    strip_prefix = "bazel-lib-2.15.3",
    url = "https://github.com/bazel-contrib/bazel-lib/releases/download/v2.15.3/bazel-lib-v2.15.3.tar.gz",
)

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "aspect_bazel_lib_register_toolchains")

# Required bazel-lib dependencies

aspect_bazel_lib_dependencies()

# Register bazel-lib toolchains

aspect_bazel_lib_register_toolchains()

What's Changed

Full Changelog: https://github.com/bazel-contrib/tar.bzl/compare/v0.2.1...v0.3.0

Deps:
Assets:

Bazel tar rule

General-purpose rule to create tar archives.

Unlike pkg_tar from rules_pkg:

  • It does not depend on any Python interpreter setup
  • The "manifest" specification is a mature public API and uses a compact tabular format, fixing bazelbuild/rules_pkg#238
  • It doesn't rely custom program to produce the output, instead we rely on the well-known C++ program tar(1). Specifically, we use the BSD variant of tar since it provides a means of controlling mtimes, uid, symlinks, etc.

We also provide full control for tar'ring binaries including their runfiles.

The tar binary is hermetic and fully statically-linked. See Design Notes below.

This rule was originally developed within bazel-lib. Thanks to all the contributors who made it possible!

Examples

Simplest possible usage:

load("@tar.bzl", "tar")

# build this target to produce archive.tar
tar(
    name = "archive",
    srcs = ["my-file.txt"],
)

Mutations allow modification of the archive's structure. For example to strip the package name:

load("@tar.bzl", "mutate", "tar")

tar(
    name = "new",
    srcs = ["my-file.txt"],
    # See arguments documented at
    # https://github.com/bazel-contrib/tar.bzl/blob/main/docs/mtree.md#mtree_mutate
    mutate = mutate(strip_prefix = package_name()),
)

Other examples:

Note; this repository doesn't yet allow modes other than create, such as "append", "list", "update", "extract". See https://registry.bazel.build/modules/rules_tar for this.

API docs

  • tar Run BSD tar(1) to produce archives
  • mtree The intermediate manifest format mtree(8) describing a tar operation

Design notes

  1. We start from libarchive, which is on the BCR: https://registry.bazel.build/modules/libarchive
  2. You could choose to register a toolchain that builds from source, but most users want a pre-built tar binary: https://github.com/aspect-build/bsdtar-prebuilt
  3. bazel-lib defines the toolchain type, and registers a sensible default toolchain: https://github.com/bazel-contrib/bazel-lib/blob/main/lib/private/tar_toolchain.bzl
  4. This repo then contains just the starlark rule code for invoking tar within Bazel actions (aka. build steps)