blob: 139401acf2c94192899cf637359164f4cafc8225 [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',
Parker Schuh5dbce222017-03-22 20:52:16 -07009 'cd "${BASH_SOURCE[0]}.runfiles/%s"' % ctx.workspace_name,
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 Silvermanae5e8682018-09-02 17:53:14 -070034 transitive_files = ctx.attr._downloader.default_runfiles.files,
Brian Silvermanaf485d62015-10-12 00:37:14 -040035 collect_data = True,
36 collect_default = True,
37 ),
Austin Schuh9d92e6b2017-10-17 01:19:38 -070038 files = depset([ctx.outputs.executable]),
Brian Silvermanaf485d62015-10-12 00:37:14 -040039 )
40
Comran Morshed38967332016-04-23 19:26:48 -070041def _aos_downloader_dir_impl(ctx):
42 return struct(
43 downloader_dir = ctx.attr.dir,
44 downloader_srcs = ctx.files.srcs
45 )
46
Brian Silvermanae5e8682018-09-02 17:53:14 -070047"""Creates a binary which downloads code to a robot.
Brian Silvermanaf485d62015-10-12 00:37:14 -040048
49Running this with `bazel run` will actually download everything.
50
Brian Silvermand2540402015-11-28 18:35:00 -050051This also generates a start_list.txt file with the names of binaries to start.
52
Brian Silvermanaf485d62015-10-12 00:37:14 -040053Attrs:
54 srcs: The files to download. They currently all get shoved into one folder.
Comran Morshed38967332016-04-23 19:26:48 -070055 dirs: A list of aos_downloader_dirs to download too.
Brian Silvermand2540402015-11-28 18:35:00 -050056 start_srcs: Like srcs, except they also get put into start_list.txt.
Brian Silvermanaf485d62015-10-12 00:37:14 -040057 default_target: The default host to download to. If not specified, defaults to
58 roboRIO-971.local.
Brian Silvermanae5e8682018-09-02 17:53:14 -070059"""
60
Brian Silvermanaf485d62015-10-12 00:37:14 -040061aos_downloader = rule(
Brian Silvermanae5e8682018-09-02 17:53:14 -070062 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 Silvermanaf485d62015-10-12 00:37:14 -040092)
Comran Morshed38967332016-04-23 19:26:48 -070093
Brian Silvermanae5e8682018-09-02 17:53:14 -070094"""Downloads files to a specific directory.
Comran Morshed38967332016-04-23 19:26:48 -070095
96This rule does nothing by itself. Use it by adding to the dirs attribute of an
97aos_downloader rule.
98
99Attrs:
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 Silvermanae5e8682018-09-02 17:53:14 -0700103"""
104
Comran Morshed38967332016-04-23 19:26:48 -0700105aos_downloader_dir = rule(
Brian Silvermanae5e8682018-09-02 17:53:14 -0700106 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 Morshed38967332016-04-23 19:26:48 -0700116)