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