blob: 00858043d11e7944ca9caf6e741b9fa90eb9c5f9 [file] [log] [blame]
Brian Silvermanaf485d62015-10-12 00:37:14 -04001def _aos_downloader_impl(ctx):
Austin Schuhe7919972015-11-22 21:27:37 -08002 all_files = ctx.files.srcs + ctx.files.start_srcs + [ctx.outputs._startlist]
Brian Silvermanaf485d62015-10-12 00:37:14 -04003 ctx.file_action(
4 output = ctx.outputs.executable,
5 executable = True,
6 content = '\n'.join([
7 '#!/bin/bash',
Comran Morshed38967332016-04-23 19:26:48 -07008 'set -e',
Brian Silvermand54068e2015-10-14 17:56:05 -04009 'cd "${BASH_SOURCE[@]}.runfiles"',
Comran Morshed38967332016-04-23 19:26:48 -070010 ] + ['%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 Silvermanaf485d62015-10-12 00:37:14 -040015 'exec %s %s -- %s "$@"' % (ctx.executable._downloader.short_path,
Austin Schuh1eceeb92015-11-08 21:16:06 -080016 ' '.join([src.short_path for src in all_files]),
Brian Silvermanaf485d62015-10-12 00:37:14 -040017 ctx.attr.default_target),
18 ]),
19 )
20
Austin Schuh1eceeb92015-11-08 21:16:06 -080021 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 Morshed38967332016-04-23 19:26:48 -070026 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 Silvermanaf485d62015-10-12 00:37:14 -040031 return struct(
32 runfiles = ctx.runfiles(
Comran Morshed38967332016-04-23 19:26:48 -070033 files = to_download + ctx.files._downloader,
Brian Silvermanaf485d62015-10-12 00:37:14 -040034 collect_data = True,
35 collect_default = True,
36 ),
37 files = set([ctx.outputs.executable]),
38 )
39
Comran Morshed38967332016-04-23 19:26:48 -070040def _aos_downloader_dir_impl(ctx):
41 return struct(
42 downloader_dir = ctx.attr.dir,
43 downloader_srcs = ctx.files.srcs
44 )
45
Brian Silvermanaf485d62015-10-12 00:37:14 -040046'''Creates a binary which downloads code to a robot.
47
48Running this with `bazel run` will actually download everything.
49
Brian Silvermand2540402015-11-28 18:35:00 -050050This also generates a start_list.txt file with the names of binaries to start.
51
Brian Silvermanaf485d62015-10-12 00:37:14 -040052Attrs:
53 srcs: The files to download. They currently all get shoved into one folder.
Comran Morshed38967332016-04-23 19:26:48 -070054 dirs: A list of aos_downloader_dirs to download too.
Brian Silvermand2540402015-11-28 18:35:00 -050055 start_srcs: Like srcs, except they also get put into start_list.txt.
Brian Silvermanaf485d62015-10-12 00:37:14 -040056 default_target: The default host to download to. If not specified, defaults to
57 roboRIO-971.local.
58'''
59aos_downloader = rule(
60 implementation = _aos_downloader_impl,
61 attrs = {
62 '_downloader': attr.label(
63 executable = True,
64 cfg = HOST_CFG,
Brian Silvermanc2065732015-11-28 22:55:30 +000065 default = Label('//aos/downloader'),
Brian Silvermanaf485d62015-10-12 00:37:14 -040066 ),
Austin Schuh1eceeb92015-11-08 21:16:06 -080067 'start_srcs': attr.label_list(
68 mandatory = True,
69 allow_files = True,
70 ),
Brian Silvermanaf485d62015-10-12 00:37:14 -040071 'srcs': attr.label_list(
72 mandatory = True,
73 allow_files = True,
74 ),
Comran Morshed38967332016-04-23 19:26:48 -070075 'dirs': attr.label_list(
76 mandatory = False,
77 providers = [
78 'downloader_dir',
79 'downloader_srcs',
80 ]
81 ),
Brian Silvermanaf485d62015-10-12 00:37:14 -040082 'default_target': attr.string(
Austin Schuhb7c39ff2016-02-27 14:45:58 -080083 default = 'roboRIO-971-frc.local',
Brian Silvermanaf485d62015-10-12 00:37:14 -040084 ),
85 },
86 executable = True,
Austin Schuh1eceeb92015-11-08 21:16:06 -080087 outputs = {
88 '_startlist': '%{name}.start_list.dir/start_list.txt',
89 },
Brian Silvermanaf485d62015-10-12 00:37:14 -040090)
Comran Morshed38967332016-04-23 19:26:48 -070091
92'''Downloads files to a specific directory.
93
94This rule does nothing by itself. Use it by adding to the dirs attribute of an
95aos_downloader rule.
96
97Attrs:
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'''
102aos_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)