| 1 |
#!/usr/bin/env python3 |
| 2 |
import argparse |
| 3 |
import csv |
| 4 |
import multiprocessing |
| 5 |
import subprocess |
| 6 |
import sys |
| 7 |
from collections import defaultdict |
| 8 |
from pathlib import Path |
| 9 |
|
| 10 |
# third party dependencies |
| 11 |
from tqdm import tqdm |
| 12 |
|
| 13 |
# spell-checker:ignore (libs) tqdm imap ; (shell/mac) xcrun ; (vars) nargs retcode csvfile |
| 14 |
|
| 15 |
BINS_PATH = Path("../src/uu") |
| 16 |
CACHE_PATH = Path("compiles_table.csv") |
| 17 |
TARGETS = [ |
| 18 |
# Linux - GNU |
| 19 |
"aarch64-unknown-linux-gnu", |
| 20 |
"i686-unknown-linux-gnu", |
| 21 |
"powerpc64-unknown-linux-gnu", |
| 22 |
"riscv64gc-unknown-linux-gnu", |
| 23 |
"x86_64-unknown-linux-gnu", |
| 24 |
# Windows |
| 25 |
"aarch64-pc-windows-msvc", |
| 26 |
"i686-pc-windows-gnu", |
| 27 |
"i686-pc-windows-msvc", |
| 28 |
"x86_64-pc-windows-gnu", |
| 29 |
"x86_64-pc-windows-msvc", |
| 30 |
# Apple |
| 31 |
"aarch64-apple-darwin", |
| 32 |
"x86_64-apple-darwin", |
| 33 |
"aarch64-apple-ios", |
| 34 |
"x86_64-apple-ios", |
| 35 |
# BSDs |
| 36 |
"x86_64-unknown-freebsd", |
| 37 |
"x86_64-unknown-netbsd", |
| 38 |
# Android |
| 39 |
"aarch64-linux-android", |
| 40 |
"x86_64-linux-android", |
| 41 |
# Solaris |
| 42 |
"x86_64-sun-solaris", |
| 43 |
# Illumos |
| 44 |
"x86_64-unknown-illumos", |
| 45 |
# WASM |
| 46 |
"wasm32-wasi", |
| 47 |
# Redox |
| 48 |
"x86_64-unknown-redox", |
| 49 |
# Fuchsia |
| 50 |
"aarch64-fuchsia", |
| 51 |
"x86_64-fuchsia", |
| 52 |
] |
| 53 |
|
| 54 |
|
| 55 |
class MissingToolchainError(Exception): |
| 56 |
"""A toolchain or SDK needed to check a target is not installed.""" |
| 57 |
|
| 58 |
|
| 59 |
class Target(str): |
| 60 |
def __new__(cls, content): |
| 61 |
obj = super().__new__(cls, content) |
| 62 |
obj.arch, obj.platform, obj.os = Target.parse(content) |
| 63 |
return obj |
| 64 |
|
| 65 |
@staticmethod |
| 66 |
def parse(s): |
| 67 |
elem = s.split("-") |
| 68 |
if len(elem) == 2: |
| 69 |
arch, platform, os = elem[0], "n/a", elem[1] |
| 70 |
else: |
| 71 |
arch, platform, os = elem[0], elem[1], "-".join(elem[2:]) |
| 72 |
if os == "ios": |
| 73 |
os = "apple IOS" |
| 74 |
if os == "darwin": |
| 75 |
os = "apple MacOS" |
| 76 |
return (arch, platform, os) |
| 77 |
|
| 78 |
@staticmethod |
| 79 |
def get_heading(): |
| 80 |
return ["OS", "ARCH"] |
| 81 |
|
| 82 |
def get_row_heading(self): |
| 83 |
return [self.os, self.arch] |
| 84 |
|
| 85 |
def requires_nightly(self): |
| 86 |
return "redox" in self |
| 87 |
|
| 88 |
# Perform the 'it-compiles' check |
| 89 |
def check(self, binary): |
| 90 |
if self.requires_nightly(): |
| 91 |
args = [ |
| 92 |
"cargo", |
| 93 |
"+nightly", |
| 94 |
"check", |
| 95 |
"-p", |
| 96 |
f"uu_{binary}", |
| 97 |
"--bin", |
| 98 |
binary, |
| 99 |
f"--target={self}", |
| 100 |
] |
| 101 |
else: |
| 102 |
args = [ |
| 103 |
"cargo", |
| 104 |
"check", |
| 105 |
"-p", |
| 106 |
f"uu_{binary}", |
| 107 |
"--bin", |
| 108 |
binary, |
| 109 |
f"--target={self}", |
| 110 |
] |
| 111 |
|
| 112 |
res = subprocess.run(args, capture_output=True, check=False) |
| 113 |
return res.returncode |
| 114 |
|
| 115 |
# Validate that the dependencies for running this target are met |
| 116 |
def is_installed(self): |
| 117 |
# check IOS sdk is installed, raise exception otherwise |
| 118 |
if "ios" in self: |
| 119 |
res = subprocess.run(["which", "xcrun"], capture_output=True, check=False) |
| 120 |
if len(res.stdout) == 0: |
| 121 |
raise MissingToolchainError( |
| 122 |
"Error: IOS sdk does not seem to be installed. Please do that manually" |
| 123 |
) |
| 124 |
if not self.requires_nightly(): |
| 125 |
# check std toolchains are installed |
| 126 |
toolchains = subprocess.run( |
| 127 |
["rustup", "target", "list"], capture_output=True, check=False |
| 128 |
) |
| 129 |
toolchains = toolchains.stdout.decode("utf-8").split("\n") |
| 130 |
if "installed" not in next(filter(lambda x: self in x, toolchains)): |
| 131 |
raise MissingToolchainError( |
| 132 |
f"Error: the {self} target is not installed. Please do that manually" |
| 133 |
) |
| 134 |
else: |
| 135 |
# check nightly toolchains are installed |
| 136 |
toolchains = subprocess.run( |
| 137 |
["rustup", "+nightly", "target", "list"], |
| 138 |
capture_output=True, |
| 139 |
check=False, |
| 140 |
) |
| 141 |
toolchains = toolchains.stdout.decode("utf-8").split("\n") |
| 142 |
if "installed" not in next(filter(lambda x: self in x, toolchains)): |
| 143 |
raise MissingToolchainError( |
| 144 |
f"Error: the {self} nightly target is not installed. Please do that manually" |
| 145 |
) |
| 146 |
return True |
| 147 |
|
| 148 |
|
| 149 |
def install_targets(): |
| 150 |
cmd = ["rustup", "target", "add"] + TARGETS |
| 151 |
print(" ".join(cmd)) |
| 152 |
ret = subprocess.run(cmd, check=False) |
| 153 |
assert ret.returncode == 0 |
| 154 |
|
| 155 |
|
| 156 |
def get_all_bins(): |
| 157 |
return sorted(path.name for path in BINS_PATH.iterdir()) |
| 158 |
|
| 159 |
|
| 160 |
def get_targets(selection): |
| 161 |
if "all" in selection: |
| 162 |
return list(map(Target, TARGETS)) |
| 163 |
else: |
| 164 |
# preserve the same order as in TARGETS |
| 165 |
return list(map(Target, filter(lambda x: x in selection, TARGETS))) |
| 166 |
|
| 167 |
|
| 168 |
def test_helper(tup): |
| 169 |
bin, target = tup |
| 170 |
retcode = target.check(bin) |
| 171 |
return (target, bin, retcode) |
| 172 |
|
| 173 |
|
| 174 |
def test_all_targets(targets, bins): |
| 175 |
pool = multiprocessing.Pool() |
| 176 |
inputs = [(b, t) for b in bins for t in targets] |
| 177 |
|
| 178 |
outputs = list(tqdm(pool.imap(test_helper, inputs), total=len(inputs))) |
| 179 |
|
| 180 |
table = defaultdict(dict) |
| 181 |
for (t, b, r) in outputs: |
| 182 |
table[t][b] = r |
| 183 |
return table |
| 184 |
|
| 185 |
|
| 186 |
def save_csv(file, table): |
| 187 |
targets = get_targets(table.keys()) # preserve order in CSV |
| 188 |
bins = list(next(iter(table.values())).keys()) |
| 189 |
with open(file, "w") as csvfile: |
| 190 |
header = ["target"] + bins |
| 191 |
writer = csv.DictWriter(csvfile, fieldnames=header) |
| 192 |
writer.writeheader() |
| 193 |
for t in targets: |
| 194 |
d = {"target": t} |
| 195 |
d.update(table[t]) |
| 196 |
writer.writerow(d) |
| 197 |
|
| 198 |
|
| 199 |
def load_csv(file): |
| 200 |
table = {} |
| 201 |
cols = [] |
| 202 |
rows = [] |
| 203 |
with open(file, "r") as csvfile: |
| 204 |
reader = csv.DictReader(csvfile) |
| 205 |
cols = list(filter(lambda x: x != "target", reader.fieldnames)) |
| 206 |
for row in reader: |
| 207 |
t = Target(row["target"]) |
| 208 |
rows += [t] |
| 209 |
del row["target"] |
| 210 |
table[t] = dict([k, int(v)] for k, v in row.items()) |
| 211 |
return (table, rows, cols) |
| 212 |
|
| 213 |
|
| 214 |
def merge_tables(old, new): |
| 215 |
from copy import deepcopy |
| 216 |
|
| 217 |
tmp = deepcopy(old) |
| 218 |
tmp.update(deepcopy(new)) |
| 219 |
return tmp |
| 220 |
|
| 221 |
|
| 222 |
def render_md(fd, table, headings: str, row_headings: Target): |
| 223 |
def print_row(lst, lens=None): |
| 224 |
lens = (lens or []) + [0] * (len(lst) - len(lens or [])) |
| 225 |
for e, lmd in zip(lst, lens): |
| 226 |
fmt = "|{}" if lmd == 0 else "|{:>%s}" % len(header[0]) |
| 227 |
fd.write(fmt.format(e)) |
| 228 |
fd.write("|\n") |
| 229 |
|
| 230 |
def cell_render(target, bin): |
| 231 |
return "y" if table[target][bin] == 0 else " " |
| 232 |
|
| 233 |
# add some 'hard' padding to specific columns |
| 234 |
lens = [ |
| 235 |
max(len(target.os) for target in row_headings) + 2, |
| 236 |
max(len(target.arch) for target in row_headings) + 2, |
| 237 |
] |
| 238 |
header = Target.get_heading() |
| 239 |
header[0] = ("{:#^%d}" % lens[0]).format(header[0]) |
| 240 |
header[1] = ("{:#^%d}" % lens[1]).format(header[1]) |
| 241 |
|
| 242 |
header += headings |
| 243 |
print_row(header) |
| 244 |
lines = ["-" * len(column) for column in header] |
| 245 |
print_row(lines) |
| 246 |
|
| 247 |
for t in row_headings: |
| 248 |
row = [cell_render(t, b) for b in headings] |
| 249 |
row = t.get_row_heading() + row |
| 250 |
print_row(row) |
| 251 |
|
| 252 |
|
| 253 |
if __name__ == "__main__": |
| 254 |
# create the top-level parser |
| 255 |
parser = argparse.ArgumentParser(prog="compiles_table.py") |
| 256 |
subparsers = parser.add_subparsers( |
| 257 |
help="sub-command to execute", required=True, dest="cmd" |
| 258 |
) |
| 259 |
# create the parser for the "check" command |
| 260 |
parser_a = subparsers.add_parser( |
| 261 |
"check", help="run cargo check on specified targets and update csv cache" |
| 262 |
) |
| 263 |
parser_a.add_argument( |
| 264 |
"targets", |
| 265 |
metavar="TARGET", |
| 266 |
type=str, |
| 267 |
nargs="+", |
| 268 |
choices=["all"] + TARGETS, |
| 269 |
help="target-triple to check, as shown by 'rustup target list'", |
| 270 |
) |
| 271 |
# create the parser for the "render" command |
| 272 |
parser_b = subparsers.add_parser("render", help="print a markdown table to stdout") |
| 273 |
parser_b.add_argument( |
| 274 |
"--equidistant", |
| 275 |
action="store_true", |
| 276 |
help="NOT IMPLEMENTED: render each column with an equal width (in plaintext)", |
| 277 |
) |
| 278 |
args = parser.parse_args() |
| 279 |
|
| 280 |
if args.cmd == "render": |
| 281 |
table, targets, bins = load_csv(CACHE_PATH) |
| 282 |
render_md(sys.stdout, table, bins, targets) |
| 283 |
|
| 284 |
if args.cmd == "check": |
| 285 |
targets = get_targets(args.targets) |
| 286 |
bins = get_all_bins() |
| 287 |
|
| 288 |
assert all(map(Target.is_installed, targets)) |
| 289 |
table = test_all_targets(targets, bins) |
| 290 |
|
| 291 |
prev_table, _, _ = load_csv(CACHE_PATH) |
| 292 |
new_table = merge_tables(prev_table, table) |
| 293 |
save_csv(CACHE_PATH, new_table) |