blob: 2914388fe8dc5e9c7adb1621d36c3764f448b078 [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
Austin Schuha5778ae2020-10-21 21:15:35 -070013# and upload them to https://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
Austin Schuha5778ae2020-10-21 21:15:35 -070021# and upload the resulting tarball to https://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
Philipp Schraderdada1072020-11-24 11:34:46 -080027def download_packages(name, packages, excludes = [], force_includes = [], target_compatible_with = None):
Austin Schuh8e17be92019-12-24 09:32:11 -080028 """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
Austin Schuha5778ae2020-10-21 21:15:35 -070035 https://www.frc971.org/Build-Dependencies/ you can add them to a to
Austin Schuh8e17be92019-12-24 09:32:11 -080036 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),
Philipp Schraderdada1072020-11-24 11:34:46 -080055 target_compatible_with = target_compatible_with,
Austin Schuh8e17be92019-12-24 09:32:11 -080056 )
Philipp Schrader0e19c602018-03-07 21:07:22 -080057
58def _convert_deb_to_target(deb):
Brian Silvermandc7d8052020-01-31 17:44:30 -080059 """Converts a debian package filename to a valid bazel target name."""
60 target = deb
61 target = target.replace("-", "_")
62 target = target.replace(".", "_")
63 target = target.replace(":", "_")
64 target = target.replace("+", "x")
65 target = target.replace("~", "_")
66 return "deb_%s_repo" % target
Philipp Schrader0e19c602018-03-07 21:07:22 -080067
Austin Schuha5778ae2020-10-21 21:15:35 -070068def generate_repositories_for_debs(files, base_url = "https://www.frc971.org/Build-Dependencies"):
Austin Schuh8e17be92019-12-24 09:32:11 -080069 """A WORKSPACE helper to add all the deb packages in the dictionary as a repo.
Philipp Schrader0e19c602018-03-07 21:07:22 -080070
Austin Schuh8e17be92019-12-24 09:32:11 -080071 The files dictionary must be one generated with the "download_packages"
72 helper above.
73 """
74 for f in files.keys():
75 name = _convert_deb_to_target(f)
76 if name not in native.existing_rules():
77 http_file(
78 name = name,
79 urls = [base_url + "/" + f],
80 sha256 = files[f],
81 downloaded_file_path = f,
82 )
Philipp Schrader0e19c602018-03-07 21:07:22 -080083
Philipp Schraderdada1072020-11-24 11:34:46 -080084def generate_deb_tarball(name, files, target_compatible_with = None):
Austin Schuh8e17be92019-12-24 09:32:11 -080085 """Takes all debs in the dictionary and generates one tarball from them.
Philipp Schrader0e19c602018-03-07 21:07:22 -080086
Austin Schuh8e17be92019-12-24 09:32:11 -080087 This can then be uploaded and used as another WORKSPACE entry.
88 """
89 deps = []
90 for f in files.keys():
91 dep = _convert_deb_to_target(f)
92 deps.append(dep)
93 if ("generate_%s_tarball" % dep) not in native.existing_rules():
94 native.genrule(
95 name = "generate_%s_tarball" % dep,
96 srcs = ["@%s//file" % dep],
97 outs = ["extracted_%s.tar" % dep],
98 cmd = "dpkg-deb --fsys-tarfile $(SRCS) > $@",
Philipp Schraderdada1072020-11-24 11:34:46 -080099 target_compatible_with = target_compatible_with,
Austin Schuh8e17be92019-12-24 09:32:11 -0800100 )
Philipp Schrader0e19c602018-03-07 21:07:22 -0800101
Austin Schuh8e17be92019-12-24 09:32:11 -0800102 pkg_tar(
103 name = name,
104 extension = "tar.gz",
105 deps = ["extracted_%s.tar" % dep for dep in deps],
Philipp Schraderdada1072020-11-24 11:34:46 -0800106 target_compatible_with = target_compatible_with,
Austin Schuh8e17be92019-12-24 09:32:11 -0800107 )