Bazel

From UVOO Tech Wiki
Jump to navigation Jump to search

Bazel for all builds

While the march has begun to get out of packaging Hell has begun by just using a contained & throttled process on a host via containers(docker/lxd) or just simple User Space and the benefits of SELinux/AppArmor, you will still need to package and distribute packaged builds in at least the near future. Bazel is your best friend in this area. Use it.

https://bazel.build/

There are many tools for building packages but many of them suck, especially in Debian land.
Bazel is a wonderful tool built, backed and used by Google.

Getting Started

https://docs.bazel.build/versions/master/getting-started.html

Bazel for deb & rpm packages

Example for Shell

Simple example extending simple shell example above

Get examples

git clone https://github.com/bazelbuild/bazel
cd bazel

Edit shell example BUILD file

nano examples/shell/BUILD
# shell example BUILD text
package(default_visibility = ["//visibility:public"])

sh_binary(
    name = "bin",
    srcs = ["bin.sh"],
    deps = [
        ":lib",
        "@bazel_tools//tools/bash/runfiles",
    ],
)

sh_library(
    name = "lib",
    data = [
        "data/file.txt",
        "lib.sh",
    ],
    deps = ["@bazel_tools//tools/bash/runfiles"],
)

sh_test(
    name = "test",
    size = "small",
    srcs = ["test.sh"],
    data = ["data/test_file.txt"],
    deps = [
        ":lib",
        "@bazel_tools//tools/bash/runfiles",
    ],
)

filegroup(
    name = "srcs",
    srcs = glob(["**"]),
)

# Add in packaging output.
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar", "pkg_deb")

pkg_tar(
    name = "bazel-bin",
    strip_prefix = "/src",
    package_dir = "/usr/bin",
    # srcs = ["//src:bazel"],
    srcs = ["bin.sh"],
    mode = "0755",
)

pkg_tar(
    name = "bazel-tools",
    strip_prefix = "/",
    package_dir = "/usr/share/lib/bazel/tools",
    # srcs = ["//tools:package-srcs"],
    srcs = ["bin.sh"],
    mode = "0644",
)

pkg_tar(
    name = "debian-data",
    srcs = ["bin.sh"],
    extension = "tar.gz",
    deps = [
        ":bazel-bin",
        ":bazel-tools",
    ],
)

pkg_deb(
    name = "bazel-debian",
    architecture = "amd64",
    built_using = "bazel (0.1.1)",
    data = ":debian-data",
    depends = [
        "zlib1g-dev",
        "unzip",
    ],
    description_file = "debian/description",
    homepage = "http://bazel.build",
    maintainer = "The Bazel Authors <bazel-dev@googlegroups.com>",
    package = "bazel",
    version = "0.1.1",
)

Populate the following files with the appropriate information for deb package build lintian.

ref: https://www.debian.org/doc/manuals/maint-guide/dreq.en.html

  • debian/description
  • debian/control
  • debian/copyright
  • debian/changelog
  • debian/rules

Run some commands

bazel build examples/shell:bin
bazel build examples/shell:lib
bazel build examples/shell:bazel-bin
bazel build examples/shell:bazel-tools
bazel build examples/shell:debian-data
bazel build examples/shell:bazel-debian
bazel test examples/shell:test

Run lintian

lintian bazel-bin/examples/shell/bazel_0.1.1_amd64.deb

https://www.debian.org/doc/manuals/maint-guide/dreq.en.html

Get the output

ls bazel-bin/examples/shell/

Notice how much better this is than the garbage build libraries that Debian has.


WIP - Builder script.

#!/usr/bin/env bash
set -exo pipefail
# https://www.debian.org/doc/manuals/maint-guide/dreq.en.html
D="examples/shell"
DD="scripts/packages/debian"
mkdir $D/debian || true

for i in $(find scripts/packages/debian/ -type f | grep -v "BUILD"); do
        cp $i $D/debian/ || true
done


bazel build $D:bin
bazel build $D:lib
bazel build $D:bazel-bin
bazel build $D:bazel-tools
touch examples/shell/debian/description
bazel build $D:debian-data
bazel build $D:bazel-debian
bazel test $D:test
lintian bazel-bin/examples/shell/bazel_0.1.1_amd64.deb
dpkg-deb --contents bazel-bin/examples/shell/bazel_0.1.1_amd64.deb

Other Useful Commands

example

https://github.com/bazelbuild/bazel/blob/master/scripts/packages/debian/BUILD

dpkg-deb --contents bazel-bin/examples/shell/bazel_0.1.1_amd64.deb dpkg-deb --extract bazel-bin/examples/shell/bazel_0.1.1_amd64.deb