Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | def _aos_downloader_impl(ctx): |
| 2 | all_files = ctx.files.srcs + ctx.files.start_srcs + [ctx.outputs._startlist] |
Austin Schuh | 2c4ee25 | 2021-10-17 23:15:39 -0700 | [diff] [blame] | 3 | target_files = [] |
| 4 | |
| 5 | # downloader looks for : in the inputs and uses the part after the : as |
| 6 | # the directory to copy to. |
| 7 | for d in ctx.attr.dirs: |
| 8 | target_files += [src.short_path + ":" + d.downloader_dir for src in d.downloader_srcs] |
| 9 | |
Philipp Schrader | 5dd9eda | 2020-11-08 10:16:35 -0800 | [diff] [blame] | 10 | ctx.actions.write( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | output = ctx.outputs.executable, |
Philipp Schrader | 5dd9eda | 2020-11-08 10:16:35 -0800 | [diff] [blame] | 12 | is_executable = True, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | content = "\n".join([ |
| 14 | "#!/bin/bash", |
| 15 | "set -e", |
| 16 | 'cd "${BASH_SOURCE[0]}.runfiles/%s"' % ctx.workspace_name, |
Austin Schuh | cf3ffc2 | 2021-10-23 23:03:38 -0700 | [diff] [blame] | 17 | 'for T in "$@";', |
Brian Silverman | 93b03ad | 2021-11-11 16:47:34 -0800 | [diff] [blame] | 18 | "do", |
Austin Schuh | cf3ffc2 | 2021-10-23 23:03:38 -0700 | [diff] [blame] | 19 | ' time %s --target "${T}" --type %s %s %s' % ( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | ctx.executable._downloader.short_path, |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 21 | ctx.attr.target_type, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 22 | " ".join([src.short_path for src in all_files]), |
Austin Schuh | 2c4ee25 | 2021-10-17 23:15:39 -0700 | [diff] [blame] | 23 | " ".join(target_files), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 24 | ), |
Brian Silverman | 93b03ad | 2021-11-11 16:47:34 -0800 | [diff] [blame] | 25 | "done", |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 26 | ]), |
| 27 | ) |
| 28 | |
Philipp Schrader | 5dd9eda | 2020-11-08 10:16:35 -0800 | [diff] [blame] | 29 | ctx.actions.write( |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | output = ctx.outputs._startlist, |
| 31 | content = "\n".join([f.basename for f in ctx.files.start_srcs]) + "\n", |
| 32 | ) |
| 33 | |
| 34 | to_download = [ctx.outputs._startlist] |
| 35 | to_download += all_files |
| 36 | for d in ctx.attr.dirs: |
| 37 | to_download += d.downloader_srcs |
| 38 | |
| 39 | return struct( |
| 40 | runfiles = ctx.runfiles( |
| 41 | files = to_download + ctx.files._downloader, |
| 42 | transitive_files = ctx.attr._downloader.default_runfiles.files, |
| 43 | collect_data = True, |
| 44 | collect_default = True, |
| 45 | ), |
| 46 | files = depset([ctx.outputs.executable]), |
| 47 | ) |
| 48 | |
| 49 | def _aos_downloader_dir_impl(ctx): |
| 50 | return struct( |
| 51 | downloader_dir = ctx.attr.dir, |
| 52 | downloader_srcs = ctx.files.srcs, |
| 53 | ) |
| 54 | |
| 55 | """Creates a binary which downloads code to a robot. |
| 56 | |
| 57 | Running this with `bazel run` will actually download everything. |
| 58 | |
| 59 | This also generates a start_list.txt file with the names of binaries to start. |
| 60 | |
| 61 | Attrs: |
| 62 | srcs: The files to download. They currently all get shoved into one folder. |
| 63 | dirs: A list of aos_downloader_dirs to download too. |
| 64 | start_srcs: Like srcs, except they also get put into start_list.txt. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 65 | """ |
| 66 | |
| 67 | aos_downloader = rule( |
| 68 | attrs = { |
| 69 | "_downloader": attr.label( |
| 70 | executable = True, |
| 71 | cfg = "host", |
| 72 | default = Label("//frc971/downloader"), |
| 73 | ), |
| 74 | "start_srcs": attr.label_list( |
| 75 | mandatory = True, |
| 76 | allow_files = True, |
| 77 | ), |
| 78 | "srcs": attr.label_list( |
| 79 | mandatory = True, |
| 80 | allow_files = True, |
| 81 | ), |
James Kuszmaul | 2d8fa2a | 2020-03-01 13:51:50 -0800 | [diff] [blame] | 82 | "target_type": attr.string( |
| 83 | default = "roborio", |
| 84 | ), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 85 | "dirs": attr.label_list( |
| 86 | mandatory = False, |
| 87 | providers = [ |
| 88 | "downloader_dir", |
| 89 | "downloader_srcs", |
| 90 | ], |
| 91 | ), |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | }, |
| 93 | executable = True, |
| 94 | outputs = { |
| 95 | "_startlist": "%{name}.start_list.dir/start_list.txt", |
| 96 | }, |
| 97 | implementation = _aos_downloader_impl, |
| 98 | ) |
| 99 | |
| 100 | """Downloads files to a specific directory. |
| 101 | |
| 102 | This rule does nothing by itself. Use it by adding to the dirs attribute of an |
| 103 | aos_downloader rule. |
| 104 | |
| 105 | Attrs: |
| 106 | srcs: The files to download. They all go in the same directory. |
| 107 | dir: The directory (relative to the standard download directory) to put all |
| 108 | the files in. |
| 109 | """ |
| 110 | |
| 111 | aos_downloader_dir = rule( |
| 112 | attrs = { |
| 113 | "srcs": attr.label_list( |
| 114 | mandatory = True, |
| 115 | allow_files = True, |
| 116 | ), |
| 117 | "dir": attr.string( |
| 118 | mandatory = True, |
| 119 | ), |
| 120 | }, |
| 121 | implementation = _aos_downloader_dir_impl, |
| 122 | ) |