Austin Schuh | f093fa5 | 2019-06-23 20:47:23 -0700 | [diff] [blame] | 1 | load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file") |
Austin Schuh | 8f99c82 | 2024-05-05 22:43:40 -0700 | [diff] [blame] | 2 | load("@rules_pkg//:pkg.bzl", "pkg_tar") |
James Kuszmaul | ac0912d | 2024-05-21 15:56:59 -0700 | [diff] [blame] | 3 | load("//tools/build_rules:clean_dep.bzl", "clean_dep") |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 4 | |
Stephan Pleines | 69a52cb | 2023-12-14 19:13:51 -0800 | [diff] [blame] | 5 | # In order to use deb packages in the build you have to follow these steps. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 6 | # |
Stephan Pleines | 69a52cb | 2023-12-14 19:13:51 -0800 | [diff] [blame] | 7 | # Adding new packages: |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 8 | # 1. Create a "download_packages" build step in //debian/BUILD. List the |
| 9 | # packages you care about and exclude the ones you don't care about. |
Brian Silverman | 4b90dd0 | 2022-01-08 18:16:22 -0800 | [diff] [blame] | 10 | # Invoke "bazel run" on the "download_packages" target you just created. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 11 | # Save the "_files" dictionary it prints into a .bzl file in the //debian |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 12 | # folder. You will need to have the apt-rdepends package installed. |
Brian Silverman | 4b90dd0 | 2022-01-08 18:16:22 -0800 | [diff] [blame] | 13 | # If you want to get packages for a different architecture or distribution, |
| 14 | # you can pass flags here to control those. See the --help for details. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 15 | # 2. The "download_packages" steps prints the location of the deb packages |
| 16 | # after it prints the "_files" dictionary. Take the deb packages from there |
Maxwell Henderson | 7a93a65 | 2023-06-24 15:17:09 -0700 | [diff] [blame] | 17 | # and upload them to https://software.frc971.org/Build-Dependencies/. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 18 | # 3. Add the newly uploaded deb packages as WORKSPACE entries using the |
| 19 | # "generate_repositories_for_debs" helper. Load the "_files" dictionary |
| 20 | # created earlier and the "generate_repositories_for_debs" helper and call |
| 21 | # them together in the WORKSPACE file. |
| 22 | # 4. Add a "generate_deb_tarball" target to //debian/BUILD. Pass in the |
| 23 | # "_files" dictionary created earlier by loading it from the .bzl file. |
| 24 | # 5. Invoke "bazel build" on the "generate_deb_tarball" target you just created |
Maxwell Henderson | 7a93a65 | 2023-06-24 15:17:09 -0700 | [diff] [blame] | 25 | # and upload the resulting tarball to https://software.frc971.org/Build-Dependencies. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 26 | # 6. Add a new "new_http_archive" entry to the WORKSPACE file for the tarball |
| 27 | # you just uploaded. |
Stephan Pleines | 69a52cb | 2023-12-14 19:13:51 -0800 | [diff] [blame] | 28 | # |
| 29 | # Updating existing packages: |
| 30 | # 1. Read above instructions. |
| 31 | # 2. The "download_packages" build step already exists, run it. |
| 32 | # 3. The .bzl file with the file list already exists. Update it with the |
| 33 | # output from the previous step. |
| 34 | # 4. Follow steps 2., 5., and 6. from "adding new packages". |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 35 | |
Austin Schuh | 94dbdf3 | 2024-04-11 22:51:09 -0700 | [diff] [blame] | 36 | def download_packages(name, packages, excludes = [], force_includes = [], force_excludes = [], target_compatible_with = None, release = "bookworm"): |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 37 | """Downloads a set of packages as well as their dependencies. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 38 | |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 39 | You can also specify excludes in case some of the dependencies are meta |
| 40 | packages. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 41 | |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 42 | Use "bazel run" on these targets to download the packages and generate the |
| 43 | list to use in a .bzl file. Once you have the packages on |
Maxwell Henderson | 7a93a65 | 2023-06-24 15:17:09 -0700 | [diff] [blame] | 44 | https://software.frc971.org/Build-Dependencies/ you can add them to a to |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 45 | combine_packages rule. |
Philipp Schrader | fd5489f | 2022-09-17 17:31:09 -0700 | [diff] [blame] | 46 | |
| 47 | force_includes lets you include packages that are excluded by default. The |
| 48 | dependencies of these force-included packages are also force-included. To |
| 49 | counter-act that, you can use "force_excludes". The force-excluded packages |
| 50 | are excluded even if they're pulled in as a dependency from a |
| 51 | "force_includes" package. |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 52 | """ |
| 53 | package_list = " ".join(packages) |
| 54 | excludes_list = " ".join(["--exclude=%s" % e for e in excludes]) |
| 55 | force_includes = " ".join(["--force-include=%s" % i for i in force_includes]) |
Philipp Schrader | fd5489f | 2022-09-17 17:31:09 -0700 | [diff] [blame] | 56 | force_excludes = " ".join(["--force-exclude=%s" % e for e in force_excludes]) |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 57 | native.genrule( |
Brian Silverman | 4b90dd0 | 2022-01-08 18:16:22 -0800 | [diff] [blame] | 58 | name = name + "_gen", |
| 59 | outs = ["%s.sh" % name], |
| 60 | executable = True, |
| 61 | cmd = """ |
| 62 | cat > $@ <<'END' |
| 63 | #!/bin/bash |
| 64 | |
| 65 | # --- begin runfiles.bash initialization v2 --- |
| 66 | # Copy-pasted from the Bazel Bash runfiles library v2. |
| 67 | set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash |
| 68 | source "$${RUNFILES_DIR:-/dev/null}/$$f" 2>/dev/null || \\ |
| 69 | source "$$(grep -sm1 "^$$f " "$${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \\ |
| 70 | source "$$0.runfiles/$$f" 2>/dev/null || \\ |
| 71 | source "$$(grep -sm1 "^$$f " "$$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \\ |
| 72 | source "$$(grep -sm1 "^$$f " "$$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \\ |
| 73 | { echo>&2 "ERROR: cannot find $$f"; exit 1; }; f=; set -e |
| 74 | # --- end runfiles.bash initialization v2 --- |
| 75 | |
| 76 | |
Alexei Strots | f3b7e8d | 2023-08-16 21:35:13 -0700 | [diff] [blame] | 77 | exec "$$(rlocation org_frc971/debian/download_packages)" %s %s %s %s --release=%s "$$@" |
| 78 | END""" % (force_includes, force_excludes, excludes_list, package_list, release), |
Brian Silverman | 4b90dd0 | 2022-01-08 18:16:22 -0800 | [diff] [blame] | 79 | target_compatible_with = target_compatible_with, |
| 80 | ) |
| 81 | native.sh_binary( |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 82 | name = name, |
Brian Silverman | 4b90dd0 | 2022-01-08 18:16:22 -0800 | [diff] [blame] | 83 | srcs = ["%s.sh" % name], |
| 84 | deps = [ |
| 85 | "@bazel_tools//tools/bash/runfiles", |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 86 | ], |
Brian Silverman | 4b90dd0 | 2022-01-08 18:16:22 -0800 | [diff] [blame] | 87 | data = [ |
James Kuszmaul | ac0912d | 2024-05-21 15:56:59 -0700 | [diff] [blame] | 88 | clean_dep("//debian:download_packages"), |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 89 | ], |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 90 | target_compatible_with = target_compatible_with, |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 91 | ) |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 92 | |
| 93 | def _convert_deb_to_target(deb): |
Brian Silverman | dc7d805 | 2020-01-31 17:44:30 -0800 | [diff] [blame] | 94 | """Converts a debian package filename to a valid bazel target name.""" |
| 95 | target = deb |
| 96 | target = target.replace("-", "_") |
| 97 | target = target.replace(".", "_") |
| 98 | target = target.replace(":", "_") |
| 99 | target = target.replace("+", "x") |
| 100 | target = target.replace("~", "_") |
| 101 | return "deb_%s_repo" % target |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 102 | |
Maxwell Henderson | 7a93a65 | 2023-06-24 15:17:09 -0700 | [diff] [blame] | 103 | def generate_repositories_for_debs(files, base_url = "https://software.frc971.org/Build-Dependencies"): |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 104 | """A WORKSPACE helper to add all the deb packages in the dictionary as a repo. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 105 | |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 106 | The files dictionary must be one generated with the "download_packages" |
| 107 | helper above. |
| 108 | """ |
| 109 | for f in files.keys(): |
| 110 | name = _convert_deb_to_target(f) |
| 111 | if name not in native.existing_rules(): |
| 112 | http_file( |
| 113 | name = name, |
| 114 | urls = [base_url + "/" + f], |
| 115 | sha256 = files[f], |
| 116 | downloaded_file_path = f, |
| 117 | ) |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 118 | |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 119 | def generate_deb_tarball(name, files, target_compatible_with = None): |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 120 | """Takes all debs in the dictionary and generates one tarball from them. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 121 | |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 122 | This can then be uploaded and used as another WORKSPACE entry. |
| 123 | """ |
| 124 | deps = [] |
| 125 | for f in files.keys(): |
| 126 | dep = _convert_deb_to_target(f) |
| 127 | deps.append(dep) |
| 128 | if ("generate_%s_tarball" % dep) not in native.existing_rules(): |
| 129 | native.genrule( |
| 130 | name = "generate_%s_tarball" % dep, |
| 131 | srcs = ["@%s//file" % dep], |
| 132 | outs = ["extracted_%s.tar" % dep], |
| 133 | cmd = "dpkg-deb --fsys-tarfile $(SRCS) > $@", |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 134 | target_compatible_with = target_compatible_with, |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 135 | ) |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 136 | |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 137 | pkg_tar( |
| 138 | name = name, |
| 139 | extension = "tar.gz", |
James Kuszmaul | def10fd | 2024-08-10 12:18:01 -0700 | [diff] [blame] | 140 | # TODO(james): It probably isn't ideal that we have duplicates floating |
| 141 | # around, but the warnings from it are quite noisy. |
| 142 | allow_duplicates_from_deps = True, |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 143 | deps = ["extracted_%s.tar" % dep for dep in deps], |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 144 | target_compatible_with = target_compatible_with, |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 145 | ) |