blob: 9eb7e32e05513ffee178f431184bc0451d184ebb [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]
Austin Schuh2c4ee252021-10-17 23:15:39 -07003 target_files = []
4
5 # downloader looks for : in the inputs and uses the part after the : as
6 # the directory to copy to.
7 for d in ctx.attr.dirs:
8 target_files += [src.short_path + ":" + d.downloader_dir for src in d.downloader_srcs]
9
Philipp Schrader5dd9eda2020-11-08 10:16:35 -080010 ctx.actions.write(
Alex Perrycb7da4b2019-08-28 19:35:56 -070011 output = ctx.outputs.executable,
Philipp Schrader5dd9eda2020-11-08 10:16:35 -080012 is_executable = True,
Alex Perrycb7da4b2019-08-28 19:35:56 -070013 content = "\n".join([
14 "#!/bin/bash",
15 "set -e",
16 'cd "${BASH_SOURCE[0]}.runfiles/%s"' % ctx.workspace_name,
Austin Schuh2c4ee252021-10-17 23:15:39 -070017 ] + [
18 'exec %s --target "$@" --type %s %s %s' % (
Alex Perrycb7da4b2019-08-28 19:35:56 -070019 ctx.executable._downloader.short_path,
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080020 ctx.attr.target_type,
Alex Perrycb7da4b2019-08-28 19:35:56 -070021 " ".join([src.short_path for src in all_files]),
Austin Schuh2c4ee252021-10-17 23:15:39 -070022 " ".join(target_files),
Alex Perrycb7da4b2019-08-28 19:35:56 -070023 ),
24 ]),
25 )
26
Philipp Schrader5dd9eda2020-11-08 10:16:35 -080027 ctx.actions.write(
Alex Perrycb7da4b2019-08-28 19:35:56 -070028 output = ctx.outputs._startlist,
29 content = "\n".join([f.basename for f in ctx.files.start_srcs]) + "\n",
30 )
31
32 to_download = [ctx.outputs._startlist]
33 to_download += all_files
34 for d in ctx.attr.dirs:
35 to_download += d.downloader_srcs
36
37 return struct(
38 runfiles = ctx.runfiles(
39 files = to_download + ctx.files._downloader,
40 transitive_files = ctx.attr._downloader.default_runfiles.files,
41 collect_data = True,
42 collect_default = True,
43 ),
44 files = depset([ctx.outputs.executable]),
45 )
46
47def _aos_downloader_dir_impl(ctx):
48 return struct(
49 downloader_dir = ctx.attr.dir,
50 downloader_srcs = ctx.files.srcs,
51 )
52
53"""Creates a binary which downloads code to a robot.
54
55Running this with `bazel run` will actually download everything.
56
57This also generates a start_list.txt file with the names of binaries to start.
58
59Attrs:
60 srcs: The files to download. They currently all get shoved into one folder.
61 dirs: A list of aos_downloader_dirs to download too.
62 start_srcs: Like srcs, except they also get put into start_list.txt.
Alex Perrycb7da4b2019-08-28 19:35:56 -070063"""
64
65aos_downloader = rule(
66 attrs = {
67 "_downloader": attr.label(
68 executable = True,
69 cfg = "host",
70 default = Label("//frc971/downloader"),
71 ),
72 "start_srcs": attr.label_list(
73 mandatory = True,
74 allow_files = True,
75 ),
76 "srcs": attr.label_list(
77 mandatory = True,
78 allow_files = True,
79 ),
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080080 "target_type": attr.string(
81 default = "roborio",
82 ),
Alex Perrycb7da4b2019-08-28 19:35:56 -070083 "dirs": attr.label_list(
84 mandatory = False,
85 providers = [
86 "downloader_dir",
87 "downloader_srcs",
88 ],
89 ),
Alex Perrycb7da4b2019-08-28 19:35:56 -070090 },
91 executable = True,
92 outputs = {
93 "_startlist": "%{name}.start_list.dir/start_list.txt",
94 },
95 implementation = _aos_downloader_impl,
96)
97
98"""Downloads files to a specific directory.
99
100This rule does nothing by itself. Use it by adding to the dirs attribute of an
101aos_downloader rule.
102
103Attrs:
104 srcs: The files to download. They all go in the same directory.
105 dir: The directory (relative to the standard download directory) to put all
106 the files in.
107"""
108
109aos_downloader_dir = rule(
110 attrs = {
111 "srcs": attr.label_list(
112 mandatory = True,
113 allow_files = True,
114 ),
115 "dir": attr.string(
116 mandatory = True,
117 ),
118 },
119 implementation = _aos_downloader_dir_impl,
120)