Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 1 | def _aos_downloader_impl(ctx): |
Austin Schuh | e791997 | 2015-11-22 21:27:37 -0800 | [diff] [blame] | 2 | all_files = ctx.files.srcs + ctx.files.start_srcs + [ctx.outputs._startlist] |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 3 | ctx.file_action( |
| 4 | output = ctx.outputs.executable, |
| 5 | executable = True, |
| 6 | content = '\n'.join([ |
| 7 | '#!/bin/bash', |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 8 | 'set -e', |
Parker Schuh | 5dbce22 | 2017-03-22 20:52:16 -0700 | [diff] [blame] | 9 | 'cd "${BASH_SOURCE[0]}.runfiles/%s"' % ctx.workspace_name, |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 10 | ] + ['%s %s --dirs %s -- %s "$@"' % ( |
| 11 | ctx.executable._downloader.short_path, |
| 12 | ' '.join([src.short_path for src in d.downloader_srcs]), |
| 13 | d.downloader_dir, |
| 14 | ctx.attr.default_target) for d in ctx.attr.dirs] + [ |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 15 | 'exec %s %s -- %s "$@"' % (ctx.executable._downloader.short_path, |
Austin Schuh | 1eceeb9 | 2015-11-08 21:16:06 -0800 | [diff] [blame] | 16 | ' '.join([src.short_path for src in all_files]), |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 17 | ctx.attr.default_target), |
| 18 | ]), |
| 19 | ) |
| 20 | |
Austin Schuh | 1eceeb9 | 2015-11-08 21:16:06 -0800 | [diff] [blame] | 21 | ctx.file_action( |
| 22 | output = ctx.outputs._startlist, |
| 23 | content = '\n'.join([f.basename for f in ctx.files.start_srcs]) + '\n', |
| 24 | ) |
| 25 | |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 26 | to_download = [ctx.outputs._startlist] |
| 27 | to_download += all_files |
| 28 | for d in ctx.attr.dirs: |
| 29 | to_download += d.downloader_srcs |
| 30 | |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 31 | return struct( |
| 32 | runfiles = ctx.runfiles( |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 33 | files = to_download + ctx.files._downloader, |
Brian Silverman | ae5e868 | 2018-09-02 17:53:14 -0700 | [diff] [blame] | 34 | transitive_files = ctx.attr._downloader.default_runfiles.files, |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 35 | collect_data = True, |
| 36 | collect_default = True, |
| 37 | ), |
Austin Schuh | 9d92e6b | 2017-10-17 01:19:38 -0700 | [diff] [blame] | 38 | files = depset([ctx.outputs.executable]), |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 39 | ) |
| 40 | |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 41 | def _aos_downloader_dir_impl(ctx): |
| 42 | return struct( |
| 43 | downloader_dir = ctx.attr.dir, |
| 44 | downloader_srcs = ctx.files.srcs |
| 45 | ) |
| 46 | |
Brian Silverman | ae5e868 | 2018-09-02 17:53:14 -0700 | [diff] [blame] | 47 | """Creates a binary which downloads code to a robot. |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 48 | |
| 49 | Running this with `bazel run` will actually download everything. |
| 50 | |
Brian Silverman | d254040 | 2015-11-28 18:35:00 -0500 | [diff] [blame] | 51 | This also generates a start_list.txt file with the names of binaries to start. |
| 52 | |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 53 | Attrs: |
| 54 | srcs: The files to download. They currently all get shoved into one folder. |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 55 | dirs: A list of aos_downloader_dirs to download too. |
Brian Silverman | d254040 | 2015-11-28 18:35:00 -0500 | [diff] [blame] | 56 | start_srcs: Like srcs, except they also get put into start_list.txt. |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 57 | default_target: The default host to download to. If not specified, defaults to |
| 58 | roboRIO-971.local. |
Brian Silverman | ae5e868 | 2018-09-02 17:53:14 -0700 | [diff] [blame] | 59 | """ |
| 60 | |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 61 | aos_downloader = rule( |
Brian Silverman | ae5e868 | 2018-09-02 17:53:14 -0700 | [diff] [blame] | 62 | attrs = { |
| 63 | "_downloader": attr.label( |
| 64 | executable = True, |
| 65 | cfg = "host", |
| 66 | default = Label("//aos/downloader"), |
| 67 | ), |
| 68 | "start_srcs": attr.label_list( |
| 69 | mandatory = True, |
| 70 | allow_files = True, |
| 71 | ), |
| 72 | "srcs": attr.label_list( |
| 73 | mandatory = True, |
| 74 | allow_files = True, |
| 75 | ), |
| 76 | "dirs": attr.label_list( |
| 77 | mandatory = False, |
| 78 | providers = [ |
| 79 | "downloader_dir", |
| 80 | "downloader_srcs", |
| 81 | ], |
| 82 | ), |
| 83 | "default_target": attr.string( |
| 84 | default = "roboRIO-971-frc.local", |
| 85 | ), |
| 86 | }, |
| 87 | executable = True, |
| 88 | outputs = { |
| 89 | "_startlist": "%{name}.start_list.dir/start_list.txt", |
| 90 | }, |
| 91 | implementation = _aos_downloader_impl, |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 92 | ) |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 93 | |
Brian Silverman | ae5e868 | 2018-09-02 17:53:14 -0700 | [diff] [blame] | 94 | """Downloads files to a specific directory. |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 95 | |
| 96 | This rule does nothing by itself. Use it by adding to the dirs attribute of an |
| 97 | aos_downloader rule. |
| 98 | |
| 99 | Attrs: |
| 100 | srcs: The files to download. They all go in the same directory. |
| 101 | dir: The directory (relative to the standard download directory) to put all |
| 102 | the files in. |
Brian Silverman | ae5e868 | 2018-09-02 17:53:14 -0700 | [diff] [blame] | 103 | """ |
| 104 | |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 105 | aos_downloader_dir = rule( |
Brian Silverman | ae5e868 | 2018-09-02 17:53:14 -0700 | [diff] [blame] | 106 | attrs = { |
| 107 | "srcs": attr.label_list( |
| 108 | mandatory = True, |
| 109 | allow_files = True, |
| 110 | ), |
| 111 | "dir": attr.string( |
| 112 | mandatory = True, |
| 113 | ), |
| 114 | }, |
| 115 | implementation = _aos_downloader_dir_impl, |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame] | 116 | ) |