Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 1 | load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar") |
Austin Schuh | f093fa5 | 2019-06-23 20:47:23 -0700 | [diff] [blame] | 2 | load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file") |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 3 | |
| 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. |
Brian Silverman | 4b90dd0 | 2022-01-08 18:16:22 -0800 | [diff] [blame^] | 8 | # Invoke "bazel run" on the "download_packages" target you just created. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 9 | # 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] | 10 | # folder. You will need to have the apt-rdepends package installed. |
Brian Silverman | 4b90dd0 | 2022-01-08 18:16:22 -0800 | [diff] [blame^] | 11 | # If you want to get packages for a different architecture or distribution, |
| 12 | # 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] | 13 | # 2. The "download_packages" steps prints the location of the deb packages |
| 14 | # after it prints the "_files" dictionary. Take the deb packages from there |
Austin Schuh | a5778ae | 2020-10-21 21:15:35 -0700 | [diff] [blame] | 15 | # and upload them to https://www.frc971.org/Build-Dependencies/. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 16 | # 3. Add the newly uploaded deb packages as WORKSPACE entries using the |
| 17 | # "generate_repositories_for_debs" helper. Load the "_files" dictionary |
| 18 | # created earlier and the "generate_repositories_for_debs" helper and call |
| 19 | # them together in the WORKSPACE file. |
| 20 | # 4. Add a "generate_deb_tarball" target to //debian/BUILD. Pass in the |
| 21 | # "_files" dictionary created earlier by loading it from the .bzl file. |
| 22 | # 5. Invoke "bazel build" on the "generate_deb_tarball" target you just created |
Austin Schuh | a5778ae | 2020-10-21 21:15:35 -0700 | [diff] [blame] | 23 | # and upload the resulting tarball to https://www.frc971.org/Build-Dependencies. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 24 | # 6. Add a new "new_http_archive" entry to the WORKSPACE file for the tarball |
| 25 | # you just uploaded. |
| 26 | |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 27 | def download_packages(name, packages, excludes = [], force_includes = [], target_compatible_with = None): |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 28 | """Downloads a set of packages as well as their dependencies. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 29 | |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 30 | You can also specify excludes in case some of the dependencies are meta |
| 31 | packages. |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 32 | |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 33 | 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 Schuh | a5778ae | 2020-10-21 21:15:35 -0700 | [diff] [blame] | 35 | https://www.frc971.org/Build-Dependencies/ you can add them to a to |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 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( |
Brian Silverman | 4b90dd0 | 2022-01-08 18:16:22 -0800 | [diff] [blame^] | 42 | name = name + "_gen", |
| 43 | outs = ["%s.sh" % name], |
| 44 | executable = True, |
| 45 | cmd = """ |
| 46 | cat > $@ <<'END' |
| 47 | #!/bin/bash |
| 48 | |
| 49 | # --- begin runfiles.bash initialization v2 --- |
| 50 | # Copy-pasted from the Bazel Bash runfiles library v2. |
| 51 | set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash |
| 52 | source "$${RUNFILES_DIR:-/dev/null}/$$f" 2>/dev/null || \\ |
| 53 | source "$$(grep -sm1 "^$$f " "$${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \\ |
| 54 | source "$$0.runfiles/$$f" 2>/dev/null || \\ |
| 55 | source "$$(grep -sm1 "^$$f " "$$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \\ |
| 56 | source "$$(grep -sm1 "^$$f " "$$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \\ |
| 57 | { echo>&2 "ERROR: cannot find $$f"; exit 1; }; f=; set -e |
| 58 | # --- end runfiles.bash initialization v2 --- |
| 59 | |
| 60 | |
| 61 | exec "$$(rlocation org_frc971/debian/download_packages)" %s %s %s "$$@" |
| 62 | END""" % (force_includes, excludes_list, package_list), |
| 63 | target_compatible_with = target_compatible_with, |
| 64 | ) |
| 65 | native.sh_binary( |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 66 | name = name, |
Brian Silverman | 4b90dd0 | 2022-01-08 18:16:22 -0800 | [diff] [blame^] | 67 | srcs = ["%s.sh" % name], |
| 68 | deps = [ |
| 69 | "@bazel_tools//tools/bash/runfiles", |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 70 | ], |
Brian Silverman | 4b90dd0 | 2022-01-08 18:16:22 -0800 | [diff] [blame^] | 71 | data = [ |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 72 | "//debian:download_packages", |
| 73 | ], |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 74 | target_compatible_with = target_compatible_with, |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 75 | ) |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 76 | |
| 77 | def _convert_deb_to_target(deb): |
Brian Silverman | dc7d805 | 2020-01-31 17:44:30 -0800 | [diff] [blame] | 78 | """Converts a debian package filename to a valid bazel target name.""" |
| 79 | target = deb |
| 80 | target = target.replace("-", "_") |
| 81 | target = target.replace(".", "_") |
| 82 | target = target.replace(":", "_") |
| 83 | target = target.replace("+", "x") |
| 84 | target = target.replace("~", "_") |
| 85 | return "deb_%s_repo" % target |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 86 | |
Austin Schuh | a5778ae | 2020-10-21 21:15:35 -0700 | [diff] [blame] | 87 | def generate_repositories_for_debs(files, base_url = "https://www.frc971.org/Build-Dependencies"): |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 88 | """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] | 89 | |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 90 | The files dictionary must be one generated with the "download_packages" |
| 91 | helper above. |
| 92 | """ |
| 93 | for f in files.keys(): |
| 94 | name = _convert_deb_to_target(f) |
| 95 | if name not in native.existing_rules(): |
| 96 | http_file( |
| 97 | name = name, |
| 98 | urls = [base_url + "/" + f], |
| 99 | sha256 = files[f], |
| 100 | downloaded_file_path = f, |
| 101 | ) |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 102 | |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 103 | def generate_deb_tarball(name, files, target_compatible_with = None): |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 104 | """Takes all debs in the dictionary and generates one tarball from them. |
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 | This can then be uploaded and used as another WORKSPACE entry. |
| 107 | """ |
| 108 | deps = [] |
| 109 | for f in files.keys(): |
| 110 | dep = _convert_deb_to_target(f) |
| 111 | deps.append(dep) |
| 112 | if ("generate_%s_tarball" % dep) not in native.existing_rules(): |
| 113 | native.genrule( |
| 114 | name = "generate_%s_tarball" % dep, |
| 115 | srcs = ["@%s//file" % dep], |
| 116 | outs = ["extracted_%s.tar" % dep], |
| 117 | cmd = "dpkg-deb --fsys-tarfile $(SRCS) > $@", |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 118 | target_compatible_with = target_compatible_with, |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 119 | ) |
Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame] | 120 | |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 121 | pkg_tar( |
| 122 | name = name, |
| 123 | extension = "tar.gz", |
| 124 | deps = ["extracted_%s.tar" % dep for dep in deps], |
Philipp Schrader | dada107 | 2020-11-24 11:34:46 -0800 | [diff] [blame] | 125 | target_compatible_with = target_compatible_with, |
Austin Schuh | 8e17be9 | 2019-12-24 09:32:11 -0800 | [diff] [blame] | 126 | ) |