blob: 7c1c6e850e91c8a51b66b770d572831866b1fe40 [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 Schuhcf3ffc22021-10-23 23:03:38 -070017 'for T in "$@";',
Brian Silverman93b03ad2021-11-11 16:47:34 -080018 "do",
Austin Schuhcf3ffc22021-10-23 23:03:38 -070019 ' time %s --target "${T}" --type %s %s %s' % (
Alex Perrycb7da4b2019-08-28 19:35:56 -070020 ctx.executable._downloader.short_path,
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080021 ctx.attr.target_type,
Alex Perrycb7da4b2019-08-28 19:35:56 -070022 " ".join([src.short_path for src in all_files]),
Austin Schuh2c4ee252021-10-17 23:15:39 -070023 " ".join(target_files),
Alex Perrycb7da4b2019-08-28 19:35:56 -070024 ),
Brian Silverman93b03ad2021-11-11 16:47:34 -080025 "done",
Alex Perrycb7da4b2019-08-28 19:35:56 -070026 ]),
27 )
28
Philipp Schrader5dd9eda2020-11-08 10:16:35 -080029 ctx.actions.write(
Alex Perrycb7da4b2019-08-28 19:35:56 -070030 output = ctx.outputs._startlist,
31 content = "\n".join([f.basename for f in ctx.files.start_srcs]) + "\n",
32 )
33
34 to_download = [ctx.outputs._startlist]
35 to_download += all_files
36 for d in ctx.attr.dirs:
37 to_download += d.downloader_srcs
38
39 return struct(
40 runfiles = ctx.runfiles(
41 files = to_download + ctx.files._downloader,
42 transitive_files = ctx.attr._downloader.default_runfiles.files,
43 collect_data = True,
44 collect_default = True,
45 ),
46 files = depset([ctx.outputs.executable]),
47 )
48
49def _aos_downloader_dir_impl(ctx):
50 return struct(
51 downloader_dir = ctx.attr.dir,
52 downloader_srcs = ctx.files.srcs,
53 )
54
55"""Creates a binary which downloads code to a robot.
56
57Running this with `bazel run` will actually download everything.
58
59This also generates a start_list.txt file with the names of binaries to start.
60
61Attrs:
62 srcs: The files to download. They currently all get shoved into one folder.
63 dirs: A list of aos_downloader_dirs to download too.
64 start_srcs: Like srcs, except they also get put into start_list.txt.
Alex Perrycb7da4b2019-08-28 19:35:56 -070065"""
66
67aos_downloader = rule(
68 attrs = {
69 "_downloader": attr.label(
70 executable = True,
71 cfg = "host",
72 default = Label("//frc971/downloader"),
73 ),
74 "start_srcs": attr.label_list(
75 mandatory = True,
76 allow_files = True,
77 ),
78 "srcs": attr.label_list(
79 mandatory = True,
80 allow_files = True,
81 ),
James Kuszmaul2d8fa2a2020-03-01 13:51:50 -080082 "target_type": attr.string(
83 default = "roborio",
84 ),
Alex Perrycb7da4b2019-08-28 19:35:56 -070085 "dirs": attr.label_list(
86 mandatory = False,
87 providers = [
88 "downloader_dir",
89 "downloader_srcs",
90 ],
91 ),
Alex Perrycb7da4b2019-08-28 19:35:56 -070092 },
93 executable = True,
94 outputs = {
95 "_startlist": "%{name}.start_list.dir/start_list.txt",
96 },
97 implementation = _aos_downloader_impl,
98)
99
100"""Downloads files to a specific directory.
101
102This rule does nothing by itself. Use it by adding to the dirs attribute of an
103aos_downloader rule.
104
105Attrs:
106 srcs: The files to download. They all go in the same directory.
107 dir: The directory (relative to the standard download directory) to put all
108 the files in.
109"""
110
111aos_downloader_dir = rule(
112 attrs = {
113 "srcs": attr.label_list(
114 mandatory = True,
115 allow_files = True,
116 ),
117 "dir": attr.string(
118 mandatory = True,
119 ),
120 },
121 implementation = _aos_downloader_dir_impl,
122)