# Packaging for Bazel

Rules

## Overview These build rules are used for building various packaging such as tarball and debian package. ## Basic Example This example is a simplification of the debian packaging of Bazel: ```python load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar", "pkg_deb") pkg_tar( name = "bazel-bin", data_path = "/src", directory = "/usr/bin", files = ["//src:bazel"], mode = "0755", ) pkg_tar( name = "bazel-tools", data_path = "/", directory = "/usr/share/lib/bazel/tools", files = ["//tools:package-srcs"], mode = "0644", modes = {"tools/build_defs/docker/build_test.sh": "0755"}, ) pkg_tar( name = "debian-data", 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.io", maintainer = "The Bazel Authors ", package = "bazel", version = "0.1.1", ) ``` Here, the Debian package is built from three `pkg_tar` targets: - `bazel-bin` creates a tarball with the main binary (mode `0755`) in `/usr/bin`, - `bazel-tools` create a tarball with the base workspace (mode `0644`) to `/usr/share/bazel/tools` ; the `modes` attribute let us specifies executable files, - `debian-data` creates a gzip-compressed tarball that merge the three previous tarballs. `debian-data` is then used for the data content of the debian archive created by `pkg_deb`. ## Future work - Support more format, especially `pkg_zip`. - Maybe a bit more integration with the `docker_build` rule. ## pkg_tar ```python pkg_tar(name, extension, data_path, directory, files, mode, modes, deps, symlinks) ``` Creates a tar file from a list of inputs.
Attributes
name Name, required

A unique name for this rule.

extension String, default to 'tar'

The extension for the resulting tarball. The output file will be 'name.extension'. This extension also decide on the compression: if set to tar.gz or tgz then gzip compression will be used and if set to tar.bz2 or tar.bzip2 then bzip2 compression will be used.

strip_prefix String, optional

Root path of the files.

The directory structure from the files is preserved inside the tarball but a prefix path determined by strip_prefix is removed from the directory structure. This path can be absolute from the workspace root if starting with a / or relative to the rule's directory. A relative path may start with "./" (or be ".") but cannot use ".." to go up level(s). By default, the data_path attribute is unused and all files are supposed to have no prefix. A data_path of "" (the empty string) means the same as the default.

package_dir String, optional

Target directory.

The directory in which to expand the specified files, defaulting to '/'. Only makes sense accompanying files.

files List of files, optional

File to add to the layer.

A list of files that should be included in the docker image.

mode String, default to 0555

Set the mode of files added by the files attribute.

modes Dictionary, default to '{}'

A string dictionary to change default mode of specific files from files. Each key should be a path to a file before appending the prefix directory and the corresponding value the octal permission of to apply to the file.

modes = { "tools/py/2to3.sh": "0755 ... },

deps List of labels, optional

Tar files to extract and include in this tar package.

A list of tarball labels to merge into the output tarball.

symlinks Dictionary, optional

Symlinks to create in the output tarball.

symlinks = { "/path/to/link": "/path/to/target", ... },

### pkg_deb ```python pkg_deb(name, data, package, architecture, maintainer, preinst, postinst, prerm, postrm, version, version_file, description, description_file, built_using, built_using_file, priority, section, homepage, depends, suggests, enhances, predepends, recommends) ``` Create a debian package. See http://www.debian.org/doc/debian-policy/ch-controlfields.html for more details on this.
Attributes
name Name, required

A unique name for this rule.

data File, required

A tar file that contains the data for the debian package (basically the list of files that will be installed by this package).

package String, required

The name of the package.

architecture String, default to 'all'

The architecture that this package target.

See http://www.debian.org/ports/.

maintainer String, required

The maintainer of the package.

preinst, postinst, prerm and postrm Files, optional

Respectively, the pre-install, post-install, pre-remove and post-remove scripts for the package.

See http://www.debian.org/doc/debian-policy/ch-maintainerscripts.html.

version, version_file String or File, required

The package version provided either inline (with version) or from a file (with version_file).

description, description_file String or File, required

The package description provided either inline (with description) or from a file (with description_file).

built_using, built_using_file String or File, default to 'Bazel'

The tool that were used to build this package provided either inline (with built_using) or from a file (with built_using_file).

priority String, default to 'optional'

The priority of the package.

See http://www.debian.org/doc/debian-policy/ch-archive.html#s-priorities.

section String, default to 'contrib/devel'

The section of the package.

See http://www.debian.org/doc/debian-policy/ch-archive.html#s-subsections.

homepage String, optional

The homepage of the project.

depends, suggests, enhances, predepends and recommends. String list, optional

The list of dependencies in the project.

See http://www.debian.org/doc/debian-policy/ch-relationships.html#s-binarydeps.