blob: 3043452a74746b775c17b9841de3fe26827e48d6 [file] [log] [blame]
Philipp Schrader0e19c602018-03-07 21:07:22 -08001load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
Austin Schuhf093fa52019-06-23 20:47:23 -07002load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
Philipp Schrader0e19c602018-03-07 21:07:22 -08003
4# In order to use deb packages in the build you have to follow these steps:
5#
6# 1. Create a "download_packages" build step in //debian/BUILD. List the
7# packages you care about and exclude the ones you don't care about.
8# Invoke "bazel build" on the "download_packages" target you just created.
9# Save the "_files" dictionary it prints into a .bzl file in the //debian
James Kuszmaul3ae42262019-11-08 12:33:41 -080010# folder. You will need to have the apt-rdepends package installed.
Philipp Schrader0e19c602018-03-07 21:07:22 -080011# 2. The "download_packages" steps prints the location of the deb packages
12# after it prints the "_files" dictionary. Take the deb packages from there
johnp8a384f92019-01-03 13:10:00 -080013# and upload them to http://www.frc971.org/Build-Dependencies/.
Philipp Schrader0e19c602018-03-07 21:07:22 -080014# 3. Add the newly uploaded deb packages as WORKSPACE entries using the
15# "generate_repositories_for_debs" helper. Load the "_files" dictionary
16# created earlier and the "generate_repositories_for_debs" helper and call
17# them together in the WORKSPACE file.
18# 4. Add a "generate_deb_tarball" target to //debian/BUILD. Pass in the
19# "_files" dictionary created earlier by loading it from the .bzl file.
20# 5. Invoke "bazel build" on the "generate_deb_tarball" target you just created
johnp8a384f92019-01-03 13:10:00 -080021# and upload the resulting tarball to http://www.frc971.org/Build-Dependencies.
Philipp Schrader0e19c602018-03-07 21:07:22 -080022# 6. Add a new "new_http_archive" entry to the WORKSPACE file for the tarball
23# you just uploaded.
24
25# TODO(phil): Deal with armhf packages. Right now only works for amd64.
26
Austin Schuh8e17be92019-12-24 09:32:11 -080027def download_packages(name, packages, excludes = [], force_includes = []):
28 """Downloads a set of packages as well as their dependencies.
Philipp Schrader0e19c602018-03-07 21:07:22 -080029
Austin Schuh8e17be92019-12-24 09:32:11 -080030 You can also specify excludes in case some of the dependencies are meta
31 packages.
Philipp Schrader0e19c602018-03-07 21:07:22 -080032
Austin Schuh8e17be92019-12-24 09:32:11 -080033 Use "bazel run" on these targets to download the packages and generate the
34 list to use in a .bzl file. Once you have the packages on
35 http://www.frc971.org/Build-Dependencies/ you can add them to a to
36 combine_packages rule.
37 """
38 package_list = " ".join(packages)
39 excludes_list = " ".join(["--exclude=%s" % e for e in excludes])
40 force_includes = " ".join(["--force-include=%s" % i for i in force_includes])
41 native.genrule(
42 name = name,
43 outs = ["%s_output.txt" % name],
44 tags = [
45 "local",
46 "manual",
47 ],
48 tools = [
49 "//debian:download_packages",
50 ],
51 # TODO(phil): Deal with stderr a bit better. It spews more stuff out than I
52 # would like it to.
53 cmd = "$(location //debian:download_packages) %s %s %s | tee $@ >&2" %
54 (force_includes, excludes_list, package_list),
55 )
Philipp Schrader0e19c602018-03-07 21:07:22 -080056
57def _convert_deb_to_target(deb):
Brian Silvermandc7d8052020-01-31 17:44:30 -080058 """Converts a debian package filename to a valid bazel target name."""
59 target = deb
60 target = target.replace("-", "_")
61 target = target.replace(".", "_")
62 target = target.replace(":", "_")
63 target = target.replace("+", "x")
64 target = target.replace("~", "_")
65 return "deb_%s_repo" % target
Philipp Schrader0e19c602018-03-07 21:07:22 -080066
Austin Schuh8e17be92019-12-24 09:32:11 -080067def generate_repositories_for_debs(files, base_url = "http://www.frc971.org/Build-Dependencies"):
68 """A WORKSPACE helper to add all the deb packages in the dictionary as a repo.
Philipp Schrader0e19c602018-03-07 21:07:22 -080069
Austin Schuh8e17be92019-12-24 09:32:11 -080070 The files dictionary must be one generated with the "download_packages"
71 helper above.
72 """
73 for f in files.keys():
74 name = _convert_deb_to_target(f)
75 if name not in native.existing_rules():
76 http_file(
77 name = name,
78 urls = [base_url + "/" + f],
79 sha256 = files[f],
80 downloaded_file_path = f,
81 )
Philipp Schrader0e19c602018-03-07 21:07:22 -080082
83def generate_deb_tarball(name, files):
Austin Schuh8e17be92019-12-24 09:32:11 -080084 """Takes all debs in the dictionary and generates one tarball from them.
Philipp Schrader0e19c602018-03-07 21:07:22 -080085
Austin Schuh8e17be92019-12-24 09:32:11 -080086 This can then be uploaded and used as another WORKSPACE entry.
87 """
88 deps = []
89 for f in files.keys():
90 dep = _convert_deb_to_target(f)
91 deps.append(dep)
92 if ("generate_%s_tarball" % dep) not in native.existing_rules():
93 native.genrule(
94 name = "generate_%s_tarball" % dep,
95 srcs = ["@%s//file" % dep],
96 outs = ["extracted_%s.tar" % dep],
97 cmd = "dpkg-deb --fsys-tarfile $(SRCS) > $@",
98 )
Philipp Schrader0e19c602018-03-07 21:07:22 -080099
Austin Schuh8e17be92019-12-24 09:32:11 -0800100 pkg_tar(
101 name = name,
102 extension = "tar.gz",
103 deps = ["extracted_%s.tar" % dep for dep in deps],
104 )