Philipp Schrader | 0e19c60 | 2018-03-07 21:07:22 -0800 | [diff] [blame^] | 1 | load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar") |
| 2 | |
| 3 | # In order to use deb packages in the build you have to follow these steps: |
| 4 | # |
| 5 | # 1. Create a "download_packages" build step in //debian/BUILD. List the |
| 6 | # packages you care about and exclude the ones you don't care about. |
| 7 | # Invoke "bazel build" on the "download_packages" target you just created. |
| 8 | # Save the "_files" dictionary it prints into a .bzl file in the //debian |
| 9 | # folder. |
| 10 | # 2. The "download_packages" steps prints the location of the deb packages |
| 11 | # after it prints the "_files" dictionary. Take the deb packages from there |
| 12 | # and upload them to http://frc971.org/Build-Dependencies/. |
| 13 | # 3. Add the newly uploaded deb packages as WORKSPACE entries using the |
| 14 | # "generate_repositories_for_debs" helper. Load the "_files" dictionary |
| 15 | # created earlier and the "generate_repositories_for_debs" helper and call |
| 16 | # them together in the WORKSPACE file. |
| 17 | # 4. Add a "generate_deb_tarball" target to //debian/BUILD. Pass in the |
| 18 | # "_files" dictionary created earlier by loading it from the .bzl file. |
| 19 | # 5. Invoke "bazel build" on the "generate_deb_tarball" target you just created |
| 20 | # and upload the resulting tarball to http://frc971.org/Build-Dependencies. |
| 21 | # 6. Add a new "new_http_archive" entry to the WORKSPACE file for the tarball |
| 22 | # you just uploaded. |
| 23 | |
| 24 | # TODO(phil): Deal with armhf packages. Right now only works for amd64. |
| 25 | |
| 26 | def download_packages(name, packages, excludes=[]): |
| 27 | """Downloads a set of packages as well as their dependencies. |
| 28 | |
| 29 | You can also specify excludes in case some of the dependencies are meta |
| 30 | packages. |
| 31 | |
| 32 | Use "bazel run" on these targets to download the packages and generate the |
| 33 | list to use in a .bzl file. Once you have the packages on |
| 34 | http://frc971.org/Build-Dependencies/ you can add them to a to |
| 35 | combine_packages rule. |
| 36 | """ |
| 37 | package_list = " ".join(packages) |
| 38 | excludes_list = " ".join(["--exclude=%s" % e for e in excludes]) |
| 39 | native.genrule( |
| 40 | name = name, |
| 41 | outs = ["%s_output.txt" % name], |
| 42 | tags = [ |
| 43 | "local", |
| 44 | "manual", |
| 45 | ], |
| 46 | tools = [ |
| 47 | "//debian:download_packages", |
| 48 | ], |
| 49 | # TODO(phil): Deal with stderr a bit better. It spews more stuff out than I |
| 50 | # would like it to. |
| 51 | cmd = "$(location //debian:download_packages) %s %s | tee $@ >&2" % (excludes_list, package_list), |
| 52 | ) |
| 53 | |
| 54 | def _convert_deb_to_target(deb): |
| 55 | """Converts a debian package filename to a valid bazel target name.""" |
| 56 | target = deb.split('_')[0] |
| 57 | target = target.replace('-', '_') |
| 58 | target = target.replace('.', '_') |
| 59 | target = target.replace(':', '_') |
| 60 | target = target.replace('+', 'x') |
| 61 | return "deb_%s_repo" % target |
| 62 | |
| 63 | def generate_repositories_for_debs(files): |
| 64 | """A WORKSPACE helper to add all the deb packages in the dictionary as a repo. |
| 65 | |
| 66 | The files dictionary must be one generated with the "download_packages" |
| 67 | helper above. |
| 68 | """ |
| 69 | for f in files.keys(): |
| 70 | name = _convert_deb_to_target(f) |
| 71 | if name not in native.existing_rules(): |
| 72 | native.http_file( |
| 73 | name = name, |
| 74 | url = 'http://frc971.org/Build-Dependencies/%s' % f, |
| 75 | sha256 = files[f], |
| 76 | ) |
| 77 | |
| 78 | def generate_deb_tarball(name, files): |
| 79 | """Takes all debs in the dictionary and generates one tarball from them. |
| 80 | |
| 81 | This can then be uploaded and used as another WORKSPACE entry. |
| 82 | """ |
| 83 | deps = [] |
| 84 | for f in files.keys(): |
| 85 | dep = _convert_deb_to_target(f) |
| 86 | deps.append(dep) |
| 87 | if ('generate_%s_tarball' % dep) not in native.existing_rules(): |
| 88 | native.genrule( |
| 89 | name = 'generate_%s_tarball' % dep, |
| 90 | srcs = ['@%s//file' % dep], |
| 91 | outs = ['extracted_%s.tar' % dep], |
| 92 | cmd = 'dpkg-deb --fsys-tarfile $(SRCS) > $@', |
| 93 | ) |
| 94 | |
| 95 | pkg_tar( |
| 96 | name = name, |
| 97 | extension = 'tar.gz', |
| 98 | deps = ['extracted_%s.tar' % dep for dep in deps], |
| 99 | ) |