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', |
Brian Silverman | d54068e | 2015-10-14 17:56:05 -0400 | [diff] [blame] | 9 | 'cd "${BASH_SOURCE[@]}.runfiles"', |
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 | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 34 | collect_data = True, |
| 35 | collect_default = True, |
| 36 | ), |
| 37 | files = set([ctx.outputs.executable]), |
| 38 | ) |
| 39 | |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame^] | 40 | def _aos_downloader_dir_impl(ctx): |
| 41 | return struct( |
| 42 | downloader_dir = ctx.attr.dir, |
| 43 | downloader_srcs = ctx.files.srcs |
| 44 | ) |
| 45 | |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 46 | '''Creates a binary which downloads code to a robot. |
| 47 | |
| 48 | Running this with `bazel run` will actually download everything. |
| 49 | |
Brian Silverman | d254040 | 2015-11-28 18:35:00 -0500 | [diff] [blame] | 50 | This also generates a start_list.txt file with the names of binaries to start. |
| 51 | |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 52 | Attrs: |
| 53 | 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^] | 54 | dirs: A list of aos_downloader_dirs to download too. |
Brian Silverman | d254040 | 2015-11-28 18:35:00 -0500 | [diff] [blame] | 55 | 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] | 56 | default_target: The default host to download to. If not specified, defaults to |
| 57 | roboRIO-971.local. |
| 58 | ''' |
| 59 | aos_downloader = rule( |
| 60 | implementation = _aos_downloader_impl, |
| 61 | attrs = { |
| 62 | '_downloader': attr.label( |
| 63 | executable = True, |
| 64 | cfg = HOST_CFG, |
Brian Silverman | c206573 | 2015-11-28 22:55:30 +0000 | [diff] [blame] | 65 | default = Label('//aos/downloader'), |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 66 | ), |
Austin Schuh | 1eceeb9 | 2015-11-08 21:16:06 -0800 | [diff] [blame] | 67 | 'start_srcs': attr.label_list( |
| 68 | mandatory = True, |
| 69 | allow_files = True, |
| 70 | ), |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 71 | 'srcs': attr.label_list( |
| 72 | mandatory = True, |
| 73 | allow_files = True, |
| 74 | ), |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame^] | 75 | 'dirs': attr.label_list( |
| 76 | mandatory = False, |
| 77 | providers = [ |
| 78 | 'downloader_dir', |
| 79 | 'downloader_srcs', |
| 80 | ] |
| 81 | ), |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 82 | 'default_target': attr.string( |
Austin Schuh | b7c39ff | 2016-02-27 14:45:58 -0800 | [diff] [blame] | 83 | default = 'roboRIO-971-frc.local', |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 84 | ), |
| 85 | }, |
| 86 | executable = True, |
Austin Schuh | 1eceeb9 | 2015-11-08 21:16:06 -0800 | [diff] [blame] | 87 | outputs = { |
| 88 | '_startlist': '%{name}.start_list.dir/start_list.txt', |
| 89 | }, |
Brian Silverman | af485d6 | 2015-10-12 00:37:14 -0400 | [diff] [blame] | 90 | ) |
Comran Morshed | 3896733 | 2016-04-23 19:26:48 -0700 | [diff] [blame^] | 91 | |
| 92 | '''Downloads files to a specific directory. |
| 93 | |
| 94 | This rule does nothing by itself. Use it by adding to the dirs attribute of an |
| 95 | aos_downloader rule. |
| 96 | |
| 97 | Attrs: |
| 98 | srcs: The files to download. They all go in the same directory. |
| 99 | dir: The directory (relative to the standard download directory) to put all |
| 100 | the files in. |
| 101 | ''' |
| 102 | aos_downloader_dir = rule( |
| 103 | implementation = _aos_downloader_dir_impl, |
| 104 | attrs = { |
| 105 | 'srcs': attr.label_list( |
| 106 | mandatory = True, |
| 107 | allow_files = True, |
| 108 | ), |
| 109 | 'dir': attr.string( |
| 110 | mandatory = True, |
| 111 | ), |
| 112 | }, |
| 113 | ) |