blob: 97d6355f69e885ae41896dd7567589146820da9e [file] [log] [blame]
Philipp Schrader9670a2a2023-01-11 01:20:16 -08001load("@rules_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
Stephan Pleines69a52cb2023-12-14 19:13:51 -08004# In order to use deb packages in the build you have to follow these steps.
Philipp Schrader0e19c602018-03-07 21:07:22 -08005#
Stephan Pleines69a52cb2023-12-14 19:13:51 -08006# Adding new packages:
Philipp Schrader0e19c602018-03-07 21:07:22 -08007# 1. Create a "download_packages" build step in //debian/BUILD. List the
8# packages you care about and exclude the ones you don't care about.
Brian Silverman4b90dd02022-01-08 18:16:22 -08009# Invoke "bazel run" on the "download_packages" target you just created.
Philipp Schrader0e19c602018-03-07 21:07:22 -080010# Save the "_files" dictionary it prints into a .bzl file in the //debian
James Kuszmaul3ae42262019-11-08 12:33:41 -080011# folder. You will need to have the apt-rdepends package installed.
Brian Silverman4b90dd02022-01-08 18:16:22 -080012# If you want to get packages for a different architecture or distribution,
13# you can pass flags here to control those. See the --help for details.
Philipp Schrader0e19c602018-03-07 21:07:22 -080014# 2. The "download_packages" steps prints the location of the deb packages
15# after it prints the "_files" dictionary. Take the deb packages from there
Maxwell Henderson7a93a652023-06-24 15:17:09 -070016# and upload them to https://software.frc971.org/Build-Dependencies/.
Philipp Schrader0e19c602018-03-07 21:07:22 -080017# 3. Add the newly uploaded deb packages as WORKSPACE entries using the
18# "generate_repositories_for_debs" helper. Load the "_files" dictionary
19# created earlier and the "generate_repositories_for_debs" helper and call
20# them together in the WORKSPACE file.
21# 4. Add a "generate_deb_tarball" target to //debian/BUILD. Pass in the
22# "_files" dictionary created earlier by loading it from the .bzl file.
23# 5. Invoke "bazel build" on the "generate_deb_tarball" target you just created
Maxwell Henderson7a93a652023-06-24 15:17:09 -070024# and upload the resulting tarball to https://software.frc971.org/Build-Dependencies.
Philipp Schrader0e19c602018-03-07 21:07:22 -080025# 6. Add a new "new_http_archive" entry to the WORKSPACE file for the tarball
26# you just uploaded.
Stephan Pleines69a52cb2023-12-14 19:13:51 -080027#
28# Updating existing packages:
29# 1. Read above instructions.
30# 2. The "download_packages" build step already exists, run it.
31# 3. The .bzl file with the file list already exists. Update it with the
32# output from the previous step.
33# 4. Follow steps 2., 5., and 6. from "adding new packages".
Philipp Schrader0e19c602018-03-07 21:07:22 -080034
Austin Schuh94dbdf32024-04-11 22:51:09 -070035def download_packages(name, packages, excludes = [], force_includes = [], force_excludes = [], target_compatible_with = None, release = "bookworm"):
Austin Schuh8e17be92019-12-24 09:32:11 -080036 """Downloads a set of packages as well as their dependencies.
Philipp Schrader0e19c602018-03-07 21:07:22 -080037
Austin Schuh8e17be92019-12-24 09:32:11 -080038 You can also specify excludes in case some of the dependencies are meta
39 packages.
Philipp Schrader0e19c602018-03-07 21:07:22 -080040
Austin Schuh8e17be92019-12-24 09:32:11 -080041 Use "bazel run" on these targets to download the packages and generate the
42 list to use in a .bzl file. Once you have the packages on
Maxwell Henderson7a93a652023-06-24 15:17:09 -070043 https://software.frc971.org/Build-Dependencies/ you can add them to a to
Austin Schuh8e17be92019-12-24 09:32:11 -080044 combine_packages rule.
Philipp Schraderfd5489f2022-09-17 17:31:09 -070045
46 force_includes lets you include packages that are excluded by default. The
47 dependencies of these force-included packages are also force-included. To
48 counter-act that, you can use "force_excludes". The force-excluded packages
49 are excluded even if they're pulled in as a dependency from a
50 "force_includes" package.
Austin Schuh8e17be92019-12-24 09:32:11 -080051 """
52 package_list = " ".join(packages)
53 excludes_list = " ".join(["--exclude=%s" % e for e in excludes])
54 force_includes = " ".join(["--force-include=%s" % i for i in force_includes])
Philipp Schraderfd5489f2022-09-17 17:31:09 -070055 force_excludes = " ".join(["--force-exclude=%s" % e for e in force_excludes])
Austin Schuh8e17be92019-12-24 09:32:11 -080056 native.genrule(
Brian Silverman4b90dd02022-01-08 18:16:22 -080057 name = name + "_gen",
58 outs = ["%s.sh" % name],
59 executable = True,
60 cmd = """
61cat > $@ <<'END'
62#!/bin/bash
63
64# --- begin runfiles.bash initialization v2 ---
65# Copy-pasted from the Bazel Bash runfiles library v2.
66set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
67source "$${RUNFILES_DIR:-/dev/null}/$$f" 2>/dev/null || \\
68 source "$$(grep -sm1 "^$$f " "$${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \\
69 source "$$0.runfiles/$$f" 2>/dev/null || \\
70 source "$$(grep -sm1 "^$$f " "$$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \\
71 source "$$(grep -sm1 "^$$f " "$$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \\
72 { echo>&2 "ERROR: cannot find $$f"; exit 1; }; f=; set -e
73# --- end runfiles.bash initialization v2 ---
74
75
Alexei Strotsf3b7e8d2023-08-16 21:35:13 -070076exec "$$(rlocation org_frc971/debian/download_packages)" %s %s %s %s --release=%s "$$@"
77END""" % (force_includes, force_excludes, excludes_list, package_list, release),
Brian Silverman4b90dd02022-01-08 18:16:22 -080078 target_compatible_with = target_compatible_with,
79 )
80 native.sh_binary(
Austin Schuh8e17be92019-12-24 09:32:11 -080081 name = name,
Brian Silverman4b90dd02022-01-08 18:16:22 -080082 srcs = ["%s.sh" % name],
83 deps = [
84 "@bazel_tools//tools/bash/runfiles",
Austin Schuh8e17be92019-12-24 09:32:11 -080085 ],
Brian Silverman4b90dd02022-01-08 18:16:22 -080086 data = [
Austin Schuh8e17be92019-12-24 09:32:11 -080087 "//debian:download_packages",
88 ],
Philipp Schraderdada1072020-11-24 11:34:46 -080089 target_compatible_with = target_compatible_with,
Austin Schuh8e17be92019-12-24 09:32:11 -080090 )
Philipp Schrader0e19c602018-03-07 21:07:22 -080091
92def _convert_deb_to_target(deb):
Brian Silvermandc7d8052020-01-31 17:44:30 -080093 """Converts a debian package filename to a valid bazel target name."""
94 target = deb
95 target = target.replace("-", "_")
96 target = target.replace(".", "_")
97 target = target.replace(":", "_")
98 target = target.replace("+", "x")
99 target = target.replace("~", "_")
100 return "deb_%s_repo" % target
Philipp Schrader0e19c602018-03-07 21:07:22 -0800101
Maxwell Henderson7a93a652023-06-24 15:17:09 -0700102def generate_repositories_for_debs(files, base_url = "https://software.frc971.org/Build-Dependencies"):
Austin Schuh8e17be92019-12-24 09:32:11 -0800103 """A WORKSPACE helper to add all the deb packages in the dictionary as a repo.
Philipp Schrader0e19c602018-03-07 21:07:22 -0800104
Austin Schuh8e17be92019-12-24 09:32:11 -0800105 The files dictionary must be one generated with the "download_packages"
106 helper above.
107 """
108 for f in files.keys():
109 name = _convert_deb_to_target(f)
110 if name not in native.existing_rules():
111 http_file(
112 name = name,
113 urls = [base_url + "/" + f],
114 sha256 = files[f],
115 downloaded_file_path = f,
116 )
Philipp Schrader0e19c602018-03-07 21:07:22 -0800117
Philipp Schraderdada1072020-11-24 11:34:46 -0800118def generate_deb_tarball(name, files, target_compatible_with = None):
Austin Schuh8e17be92019-12-24 09:32:11 -0800119 """Takes all debs in the dictionary and generates one tarball from them.
Philipp Schrader0e19c602018-03-07 21:07:22 -0800120
Austin Schuh8e17be92019-12-24 09:32:11 -0800121 This can then be uploaded and used as another WORKSPACE entry.
122 """
123 deps = []
124 for f in files.keys():
125 dep = _convert_deb_to_target(f)
126 deps.append(dep)
127 if ("generate_%s_tarball" % dep) not in native.existing_rules():
128 native.genrule(
129 name = "generate_%s_tarball" % dep,
130 srcs = ["@%s//file" % dep],
131 outs = ["extracted_%s.tar" % dep],
132 cmd = "dpkg-deb --fsys-tarfile $(SRCS) > $@",
Philipp Schraderdada1072020-11-24 11:34:46 -0800133 target_compatible_with = target_compatible_with,
Austin Schuh8e17be92019-12-24 09:32:11 -0800134 )
Philipp Schrader0e19c602018-03-07 21:07:22 -0800135
Austin Schuh8e17be92019-12-24 09:32:11 -0800136 pkg_tar(
137 name = name,
138 extension = "tar.gz",
139 deps = ["extracted_%s.tar" % dep for dep in deps],
Philipp Schraderdada1072020-11-24 11:34:46 -0800140 target_compatible_with = target_compatible_with,
Austin Schuh8e17be92019-12-24 09:32:11 -0800141 )