-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathbuild.py
More file actions
executable file
·1524 lines (1341 loc) · 65.6 KB
/
Copy pathbuild.py
File metadata and controls
executable file
·1524 lines (1341 loc) · 65.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2025-2026, Opsero Electronic Design Inc. All rights reserved.
#
# SPDX-License-Identifier: MIT
"""Cross-platform build runner for Opsero reference design repos.
Replaces the Makefile step-runner on hosts where GNU make is unavailable or
broken (Windows/git bash), and runs the identical flow on Linux. Reads targets
and per-design attributes straight from the repo's config/data.json.
Usage (the command names the artifact you want):
./build.sh list # all targets + attributes
./build.sh ip --target <t> # HLS IP (repos with an IP pre-stage)
./build.sh project --target <t> # Vivado project (.xpr)
./build.sh xsa --target <t> # Vivado XSA (synth+impl+export)
./build.sh standalone --target <t> # Vitis workspace + app + boot file
./build.sh petalinux --target <t> # PetaLinux image (Linux only)
./build.sh yocto --target <t> # Yocto image (Linux only)
./build.sh package --target <t> # gather built artifacts -> bootimages/*.zip
./build.sh all --target <t> # everything the target supports + package
./build.sh status --target <t> # per-stage artifact state
./build.sh clean --target <t> [--stage xsa]
python build.py <command> ... # same, without the shim
--target all loops over every target (continue-on-error, per-target summary).
Two terminals can both run './build.sh all --target all': per-target lock
files make them cooperate, exactly like the legacy concurrent 'make all'.
Each command builds what it depends on first (standalone builds the XSA if
missing) and skips stages whose outputs already exist. 'all' covers
xsa + standalone + petalinux + yocto + package as supported by the target
(this release builds both Linux flows; PetaLinux is dropped at the next
version update). On Windows the
petalinux/yocto stages are refused up front with the Linux hand-off
command; everything else, including HLS IP generation, runs on both hosts.
"""
import argparse
import json
import os
import re
import shlex
import shutil
import subprocess
import sys
import textwrap
import zipfile
from pathlib import Path
IS_WINDOWS = os.name == "nt"
# data.json group label -> device family template token
FAMILY = {"fpga": "microblaze", "z7": "zynq", "zu": "zynqMP", "versal": "versal"}
# CRITICAL WARNING message IDs to ignore as known false positives. 12-1790 is
# Vivado's evaluation-license warning -- expected when building with an
# evaluation (rather than purchased) IP/edition license, not a real problem.
IGNORE_CRIT = [re.compile(r"12-1790")]
def _tick():
"""A check mark for the 'list' table, with an ASCII fallback for consoles
whose encoding can't represent U+2713 (e.g. some Windows code pages, where
printing it would raise UnicodeEncodeError)."""
mark = "✓" # checkmark
try:
mark.encode(sys.stdout.encoding or "ascii")
return mark
except (UnicodeEncodeError, LookupError):
return "y"
class BuildError(Exception):
pass
def fail(msg):
raise BuildError(msg)
def _pid_alive(pid):
if IS_WINDOWS:
import ctypes
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
h = ctypes.windll.kernel32.OpenProcess(
PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
if not h:
# Access denied means the process exists but is protected.
ERROR_ACCESS_DENIED = 5
return ctypes.windll.kernel32.GetLastError() == ERROR_ACCESS_DENIED
ctypes.windll.kernel32.CloseHandle(h)
return True
try:
os.kill(pid, 0)
return True
except ProcessLookupError:
return False
except PermissionError:
return True
class BuildLock:
"""Per-target lock, same role as the legacy Makefile .<target>.lock files:
lets several 'build all targets' loops run concurrently without two of
them building the same target. Improvement over the legacy touch-files:
the lock records pid@host, and a lock whose process is dead (e.g. after a
crash or reboot) is reclaimed automatically instead of wedging the target
until someone deletes the file by hand."""
def __init__(self, repo_root: Path, target: str):
self.path = repo_root / f".{target}.lock"
self.acquired = False
def acquire(self):
import socket
me = f"{os.getpid()}@{socket.gethostname()}"
if self.path.exists():
try:
pid_s, host = self.path.read_text().strip().split("@", 1)
pid = int(pid_s)
except (ValueError, OSError):
pid, host = None, ""
import socket as _s
if host and host != _s.gethostname():
print(f"{self.path.name}: locked by {pid}@{host} (another "
f"machine). Skipping...")
return False
if pid and _pid_alive(pid):
print(f"{self.path.name}: locked by running pid {pid}. "
f"Skipping...")
return False
print(f"{self.path.name}: reclaiming stale lock "
f"(pid {pid} is gone).")
self.path.unlink(missing_ok=True)
self.path.write_text(me)
self.acquired = True
return True
def release(self):
if self.acquired:
self.path.unlink(missing_ok=True)
self.acquired = False
# --------------------------------------------------------------------------- #
# Repo manifest
# --------------------------------------------------------------------------- #
class Repo:
def __init__(self, root: Path):
# absolute(), not resolve(): resolve() would rewrite a subst'd short
# drive (W:) back to the long physical path, defeating the MAX_PATH fix.
self.root = root.absolute()
data_file = self.root / "config" / "data.json"
if not data_file.is_file():
fail(f"{data_file} not found -- is {root} a reference design repo?")
self.data = json.loads(data_file.read_text())
self.bd_name = self.data["bd_name"]
self.prj_name = self.data["prj_name"]
args_file = self.root / "Vitis" / "py" / "args.json"
self.vitis_args = json.loads(args_file.read_text()) if args_file.is_file() else {}
self.app_name = self.vitis_args.get("app_name", "")
self.combine_bit_elf = self.vitis_args.get("combine_bit_elf", False)
def design(self, label):
for d in self.data["designs"]:
if d["label"] == label:
return d
return None
def labels(self):
return [d["label"] for d in self.data["designs"]]
def required_vivado_version(self):
"""Parse 'set version_required "2025.2"' from Vivado/scripts/build.tcl."""
tcl = self.root / "Vivado" / "scripts" / "build.tcl"
m = re.search(r'set\s+version_required\s+"([^"]+)"', tcl.read_text())
if not m:
fail(f"could not parse version_required from {tcl}")
return m.group(1)
# --------------------------------------------------------------------------- #
# Tool discovery
# --------------------------------------------------------------------------- #
def _candidate_roots():
if IS_WINDOWS:
for drive in "CDEFG":
for name in ("Xilinx", "AMD", "AMDDesignTools"):
p = Path(f"{drive}:/{name}")
if p.is_dir():
yield p
else:
home = Path.home()
for p in (home / "Xilinx", Path("/tools/Xilinx"), Path("/opt/Xilinx"),
Path("/usr/local/Xilinx"), Path("/opt/xilinx"),
home / "AMDDesignTools", Path("/tools/AMDDesignTools"),
Path("/opt/AMDDesignTools")):
if p.is_dir():
yield p
def find_tool(tool: str, version: str):
"""Find <tool> install dir for exactly <version>. Returns Path or None.
Handles both layouts: <root>/<version>/<Tool> (2025.x default) and the
older <root>/<Tool>/<version>.
"""
exe = f"{tool.lower()}{'.bat' if IS_WINDOWS else ''}"
env_dir = os.environ.get(f"XILINX_{tool.upper()}")
candidates = []
if env_dir:
candidates.append(Path(env_dir))
for root in _candidate_roots():
candidates.append(root / version / tool)
candidates.append(root / tool / version)
for c in candidates:
if (c / "bin" / exe).is_file() and version in c.as_posix().split("/"):
return c
return None
def run_tool(cmd, cwd, extra_env=None):
"""Run a tool streaming output; return exit code."""
env = os.environ.copy()
if extra_env:
env.update(extra_env)
print(f"+ {' '.join(str(c) for c in cmd)} (cwd: {cwd})")
return subprocess.run([str(c) for c in cmd], cwd=str(cwd), env=env).returncode
# --------------------------------------------------------------------------- #
# Stages
# --------------------------------------------------------------------------- #
class Context:
"""Resolved paths and settings shared by all stages."""
def __init__(self, repo: Repo, target: str, jobs: int):
self.repo = repo
self.target = target
self.jobs = jobs
self.design = repo.design(target)
self.family = FAMILY.get(self.design["group"], self.design["group"])
self.viv_ver = repo.required_vivado_version()
self.ver_tag = self.viv_ver.replace(".", "-")
r = repo.root
self.viv_dir = r / "Vivado"
self.viv_prj = self.viv_dir / target
self.xpr = self.viv_prj / f"{target}.xpr"
self.xsa = self.viv_prj / f"{repo.bd_name}_wrapper.xsa"
self.viv_logs = self.viv_dir / "logs"
self.vit_dir = r / "Vitis"
self.vit_ws = self.vit_dir / f"{target}_workspace"
self.vit_boot = self.vit_dir / "boot" / target
if self.family == "microblaze":
name = f"{repo.bd_name}_boot.bit" if repo.combine_bit_elf else f"{repo.bd_name}.bit"
self.boot_file = self.vit_boot / name
else:
self.boot_file = self.vit_boot / "BOOT.BIN"
self.app_elf = self.vit_ws / repo.app_name / "build" / f"{repo.app_name}.elf"
self.petl_img = r / "PetaLinux" / target / "images" / "linux"
self.yocto_img = r / "Yocto" / target / "images" / "linux"
self.bootimages = r / "bootimages"
self.petl_zip = self.bootimages / f"{repo.prj_name}_{target}_petalinux-{self.ver_tag}.zip"
self.bare_zip = self.bootimages / f"{repo.prj_name}_{target}_standalone-{self.ver_tag}.zip"
self.yocto_zip = self.bootimages / f"{repo.prj_name}_{target}_yocto-{self.ver_tag}.zip"
def scan_critical_warnings(log: Path):
"""Return real CRITICAL WARNING lines (ignoring known false positives)."""
if not log.is_file():
return []
hits = []
for line in log.read_text(errors="replace").splitlines():
if "CRITICAL WARNING:" in line and not any(p.search(line) for p in IGNORE_CRIT):
hits.append(line.strip())
return hits
# Observed on Windows: Versal PLM generation (write_device_image) builds a BSP
# ~185 chars below the project dir using tools (mb-ar) that cannot handle paths
# at the 260-char MAX_PATH limit. Check before spending an hour in synth/impl.
VERSAL_PLM_PATH_TAIL = 185
def check_versal_path(ctx: Context):
if IS_WINDOWS and ctx.family == "versal":
longest = len(ctx.viv_prj.as_posix()) + VERSAL_PLM_PATH_TAIL
if longest >= 260:
fail(f"project path is too long for Versal PLM generation on Windows\n"
f" (would reach ~{longest} chars; the limit is 260, and the PLM\n"
f" build tools do not support long paths). Map the repo to a\n"
f" short drive and re-run the same command from there:\n"
f" subst W: \"{ctx.repo.root}\"\n"
f" cd /w/ (or W:\\ in Command Prompt)")
def vivado_exe(ctx: Context):
vivado = find_tool("Vivado", ctx.viv_ver)
if not vivado:
fail(f"Vivado {ctx.viv_ver} not found (required by this repo). "
f"Searched standard install roots and XILINX_VIVADO.")
return vivado / "bin" / ("vivado.bat" if IS_WINDOWS else "vivado")
# --------------------------------------------------------------------------- #
# IP generation pre-stage
# --------------------------------------------------------------------------- #
# Some repos generate IP (today: Vitis HLS cores) before the Vivado project can
# be created. The device part is always resolved per target by running a
# get_part.tcl -- never hard-coded -- so IP is built for each target's real
# part. Two layouts exist; ip_flow() classifies them as 'board' or 'cores':
# board Vivado/ip/get_part.tcl + <core>/run_hls.tcl -- outputs land in
# Vivado/ip/build/<target>/ (rpi-camera-fmc, zynqmp-hailo-ai)
# cores HLS/<core>/<core>.tcl + <core>/get_part.tcl -- each open_component
# exports IP to HLS/<core>/<target>/component_*/hls/impl/ip/
# (ethernet-fmc-max-throughput and several prod-test repos)
# A Vivado/ip directory holding only a checked-in component.xml is pre-packaged
# RTL IP: consumed directly via ip_repo_paths, nothing to generate, no flow
# reported here.
def hls_cores(hls_dir: Path):
"""Fixed-part HLS core dirs, each laid out as <core>/<core>.tcl."""
return sorted(d for d in hls_dir.iterdir()
if d.is_dir() and (d / f"{d.name}.tcl").is_file())
def ip_flow(ctx: Context):
"""Return ('board', Vivado/ip) | ('cores', HLS) | (None, None)."""
cand = ctx.viv_dir / "ip"
if (cand / "get_part.tcl").is_file():
return "board", cand
cand = ctx.repo.root / "HLS"
if cand.is_dir() and hls_cores(cand):
return "cores", cand
return None, None
def has_ip_flow(ctx: Context):
return ip_flow(ctx)[0] is not None
def hls_tools(ctx: Context):
"""vitis-run executable + env (tool bins on PATH, XILINX_VIVADO set)."""
vitis = find_tool("Vitis", ctx.viv_ver)
if not vitis:
fail(f"Vitis {ctx.viv_ver} not found (required for HLS IP generation).")
exe = vitis / "bin" / ("vitis-run.bat" if IS_WINDOWS else "vitis-run")
vivado = find_tool("Vivado", ctx.viv_ver)
env = {}
bins = [str(vitis / "bin")]
if vivado:
env["XILINX_VIVADO"] = str(vivado)
bins.append(str(vivado / "bin"))
env["PATH"] = os.pathsep.join(bins + [os.environ.get("PATH", "")])
return exe, env
def core_built(tdir: Path):
"""A target's core IP is built when its per-target dir holds an exported
component. Per-target layout (Vitis unified `open_component` flow): the
core's Tcl builds the IP for the target's real part into
HLS/<core>/<target>/component_*/hls/impl/ip/component.xml.
Artifact check, not exit codes: the .bat tool wrappers exit 0 on failure
on Windows."""
return bool(list(tdir.glob("component_*/hls/impl/ip/component.xml")))
def stage_ip(ctx: Context):
kind, ip_dir = ip_flow(ctx)
if not kind:
return "skipped (no IP generation step; bundled IP, if any, is pre-packaged)"
exe, env = hls_tools(ctx)
if kind == "cores": # per-target HLS cores, built for the target's real part
url, board = ctx.design["url"], ctx.design["boardname"]
# A design may name the specific core(s) it uses via data.json 'hls_ip'
# (a string or list) -- e.g. the aurora repos select the 7-series OR the
# UltraScale+ core per target. When absent, build every core (the eth
# repos ship a single core that all their targets use).
sel = ctx.design.get("hls_ip")
if sel is not None:
sel = [sel] if isinstance(sel, str) else list(sel)
cores = [c for c in hls_cores(ip_dir) if c.name in sel]
if not cores:
fail(f"data.json hls_ip={sel} names no HLS core under "
f"{rel_to_repo(ip_dir, ctx.repo.root)} "
f"(present: {[c.name for c in hls_cores(ip_dir)]})")
else:
cores = hls_cores(ip_dir)
results = []
for core in cores:
tdir = core / ctx.target
if core_built(tdir):
results.append(f"{core.name}: exists")
continue
# 1. Resolve the target's actual device part -> <target>/settings.tcl
# (get_part.tcl runs before any Vivado project exists).
ctx.viv_logs.mkdir(exist_ok=True)
plog = ctx.viv_logs / f"{ctx.target}_ip_part.log"
rc = run_tool([vivado_exe(ctx), "-mode", "batch", "-notrace",
"-source", "get_part.tcl",
"-log", plog.as_posix(), "-journal",
(ctx.viv_logs / f"{ctx.target}_ip_part.jou").as_posix(),
"-tclargs", url, board, ctx.target],
cwd=core)
if rc != 0 or not (tdir / "settings.tcl").is_file():
fail(f"device-part lookup for {core.name}/{ctx.target} failed "
f"(rc={rc}). See {rel_to_repo(plog, ctx.repo.root)}")
# 2. Build the HLS IP for that part into the target's own dir.
rc = run_tool([exe, "--mode", "hls", "--tcl", f"{core.name}.tcl"],
cwd=core, extra_env={**env, "IP_TARGET": ctx.target})
if not core_built(tdir):
fail(f"HLS build of {core.name} for {ctx.target} failed "
f"(rc={rc}). See logs in {rel_to_repo(tdir, ctx.repo.root)}")
results.append(f"{core.name}: built")
return "; ".join(results)
# per-target board flow: resolve the target's real device and build the HLS
# IP for it into ip/build/<target>/, so each target owns its generated IP
# (board is still needed for the device lookup).
board, url = ctx.design["boardname"], ctx.design["url"]
build_dir = ip_dir / "build" / ctx.target
done = build_dir / "ip_done.txt"
if done.is_file():
return f"skipped (IP for {ctx.target} exists)"
run_tcls = sorted(ip_dir.glob("*/run_hls.tcl"))
if not run_tcls:
fail(f"{rel_to_repo(ip_dir, ctx.repo.root)} has get_part.tcl but no "
f"<core>/run_hls.tcl")
if not (build_dir / "settings.tcl").is_file():
ctx.viv_logs.mkdir(exist_ok=True)
log = ctx.viv_logs / f"{ctx.target}_ip_part.log"
rc = run_tool([vivado_exe(ctx), "-mode", "batch", "-notrace",
"-source", "get_part.tcl",
"-log", log.as_posix(), "-journal",
(ctx.viv_logs / f"{ctx.target}_ip_part.jou").as_posix(),
"-tclargs", url, board, ctx.target],
cwd=ip_dir)
if rc != 0 or not (build_dir / "settings.tcl").is_file():
fail(f"device-part lookup for {ctx.target} failed (rc={rc}). "
f"See {rel_to_repo(log, ctx.repo.root)}")
for t in run_tcls:
rc = run_tool([exe, "--mode", "hls", "--tcl",
f"{t.parent.name}/run_hls.tcl"],
cwd=ip_dir,
extra_env={**env, "BOARD_NAME": board, "IP_TARGET": ctx.target})
if not list(build_dir.glob(f"{t.parent.name}*/*/impl/ip/component.xml")):
fail(f"HLS build of {t.parent.name} for {ctx.target} failed "
f"(rc={rc}). See logs in {rel_to_repo(build_dir, ctx.repo.root)}")
done.write_text("IP generated by build.py\n")
return f"built ({ctx.target})"
def stage_project(ctx: Context):
if ctx.xpr.is_file():
return "skipped (project exists)"
if has_ip_flow(ctx):
stage_ip(ctx)
check_versal_path(ctx)
exe = vivado_exe(ctx)
ctx.viv_logs.mkdir(exist_ok=True)
log = ctx.viv_logs / f"{ctx.target}_xpr.log"
rc = run_tool([exe, "-mode", "batch", "-notrace",
"-source", "scripts/build.tcl",
"-log", log.as_posix(), "-journal",
(ctx.viv_logs / f"{ctx.target}_xpr.jou").as_posix(),
"-tclargs", ctx.target],
cwd=ctx.viv_dir)
# build.tcl 'return's (exit 0) on its version check, so rc alone is
# not enough -- the project file must actually exist.
if rc != 0 or not ctx.xpr.is_file():
fail(f"Vivado project creation failed (rc={rc}). "
f"See {rel_to_repo(log, ctx.repo.root)}")
return "built"
def stage_xsa(ctx: Context):
if ctx.xsa.is_file():
return "skipped (XSA exists)"
check_versal_path(ctx)
stage_project(ctx)
vivado_bin = vivado_exe(ctx)
ctx.viv_logs.mkdir(exist_ok=True)
log = ctx.viv_logs / f"{ctx.target}_xsa.log"
# Some repos' xsa.tcl takes a third synth_only arg (e.g. rpi-camera-fmc).
tclargs = [ctx.target, str(ctx.jobs)]
if "synth_only" in (ctx.viv_dir / "scripts" / "xsa.tcl").read_text(
encoding="utf-8", errors="replace"):
tclargs.append("false")
rc = run_tool([vivado_bin, "-mode", "batch", "-notrace",
"-source", "scripts/xsa.tcl",
"-log", log.as_posix(), "-journal",
(ctx.viv_logs / f"{ctx.target}_xsa.jou").as_posix(),
"-tclargs"] + tclargs,
cwd=ctx.viv_dir)
if rc != 0 or not ctx.xsa.is_file():
fail(f"Vivado synthesis/implementation/XSA export failed (rc={rc}). "
f"See {rel_to_repo(log, ctx.repo.root)}")
crit = scan_critical_warnings(log)
if crit:
# The XSA was produced, so downstream stages (Vitis / PetaLinux / Yocto)
# are allowed to proceed. We flag the critical warnings loudly -- and
# carry the count into the summary -- so the user is aware, but we do
# NOT block.
print(f"\n!!! {len(crit)} CRITICAL WARNING(s) in "
f"{rel_to_repo(log, ctx.repo.root)} "
f"(the XSA was still produced -- review these):")
print("\n".join(crit))
return f"built ({len(crit)} CRITICAL WARNING(s) flagged -- see {log.name})"
return "built"
# Observed on Windows: the Vitis platform BSP build nests object files
# ~203+len(target) chars below the workspace dir (deepest: standalone BSP
# dependency files under CMakeFiles/<lib>.dir/<32-char-hash>/).
VITIS_BSP_PATH_TAIL = 203
def vitis_tools(ctx: Context):
"""Locate Vitis and build the PATH env make-boot.py needs for bootgen."""
vitis = find_tool("Vitis", ctx.viv_ver)
if not vitis:
fail(f"Vitis {ctx.viv_ver} not found (required by this repo).")
vitis_exe = vitis / "bin" / ("vitis.bat" if IS_WINDOWS else "vitis")
# make-boot.py invokes bootgen from PATH (the Makefile flow assumes a
# sourced settings64.sh) -- provide the tool bin dirs explicitly.
vivado = find_tool("Vivado", ctx.viv_ver)
tool_bins = [str(vitis / "bin")] + ([str(vivado / "bin")] if vivado else [])
tool_env = {"PATH": os.pathsep.join(tool_bins + [os.environ.get("PATH", "")])}
return vitis_exe, tool_env
def has_vitis_flow(ctx: Context):
return (ctx.vit_dir / "py").is_dir()
def stage_workspace(ctx: Context):
if not ctx.design.get("baremetal", False):
return "skipped (no baremetal app for this target)"
if not has_vitis_flow(ctx):
return ("skipped (data.json marks this target baremetal but the repo "
"ships no Vitis flow -- upstream inconsistency)")
vitis_exe, tool_env = vitis_tools(ctx)
if IS_WINDOWS:
longest = len(ctx.vit_ws.as_posix()) + VITIS_BSP_PATH_TAIL + len(ctx.target)
if longest >= 260:
fail(f"workspace path is too long for the Vitis BSP build on Windows\n"
f" (would reach ~{longest} chars; the limit is 260). Map the repo\n"
f" root to a short drive and re-run the same command from there:\n"
f" subst U: \"{ctx.repo.root}\"\n"
f" cd /u/ (or U:\\ in Command Prompt)")
# vitis.bat exits 0 even on failure, so trust artifacts, not exit codes.
xpfm = (ctx.vit_ws / f"{ctx.target}_platform" / "export"
/ f"{ctx.target}_platform" / f"{ctx.target}_platform.xpfm")
if ctx.vit_ws.is_dir() and ctx.xsa.stat().st_mtime > ctx.vit_ws.stat().st_mtime:
fail(f"Workspace {ctx.vit_ws.name} exists but is older than the XSA. "
f"Delete it and re-run (same rule as the Makefile).")
if not ctx.vit_ws.is_dir():
rc = run_tool([vitis_exe, "-s", "py/build-vitis.py",
ctx.target, "py/args.json", "../config/data.json"],
cwd=ctx.vit_dir, extra_env=tool_env)
if rc != 0 or not xpfm.is_file() or not ctx.app_elf.is_file():
# Remove the workspace we just created so the next run starts clean.
shutil.rmtree(ctx.vit_ws, ignore_errors=True)
fail(f"Vitis workspace build failed (rc={rc}; "
f"xpfm={'ok' if xpfm.is_file() else 'MISSING'}, "
f"app elf={'ok' if ctx.app_elf.is_file() else 'MISSING'}). "
f"Workspace removed; check the output above.")
return "built"
elif not xpfm.is_file() or not ctx.app_elf.is_file():
fail(f"Existing workspace {ctx.vit_ws} is incomplete (platform or app "
f"missing). Delete it and re-run.")
return "skipped (workspace exists)"
def stage_bootfile(ctx: Context):
if not ctx.design.get("baremetal", False):
return "skipped (no baremetal app for this target)"
if not has_vitis_flow(ctx):
return ("skipped (data.json marks this target baremetal but the repo "
"ships no Vitis flow -- upstream inconsistency)")
stage_workspace(ctx)
vitis_exe, tool_env = vitis_tools(ctx)
if (ctx.boot_file.is_file() and ctx.app_elf.is_file()
and ctx.boot_file.stat().st_mtime >= ctx.app_elf.stat().st_mtime):
return "skipped (boot file up to date)"
rc = run_tool([vitis_exe, "-s", "py/make-boot.py",
ctx.target, "py/args.json", "../config/data.json"],
cwd=ctx.vit_dir, extra_env=tool_env)
if rc != 0 or not ctx.boot_file.is_file():
fail(f"Vitis boot file generation failed (rc={rc}); expected {ctx.boot_file}")
return "built"
def find_petalinux_settings(ver):
home = Path.home()
roots = [home / "petalinux", home / "PetaLinux", Path("/tools/petalinux"),
Path("/opt/petalinux"), Path("/opt/pkg/petalinux"),
Path("/tools/Xilinx/PetaLinux")]
for root in roots:
for cand in (root / ver / "settings.sh", root / "settings.sh"):
if cand.is_file():
return cand
return None
def stage_petalinux(ctx: Context):
if not ctx.design.get("petalinux", False):
return "skipped (target has no PetaLinux flow)"
if IS_WINDOWS:
# Capability boundary, not a tooling gap: PetaLinux runs on native Linux only.
print(f" PetaLinux cannot run on Windows. To finish this target, copy or")
print(f" clone this checkout to a Linux machine with PetaLinux Tools "
f"{ctx.viv_ver} and run:")
print(f" ./build.sh all --target {ctx.target}")
return "BLOCKED (Linux required)"
if (ctx.petl_img / "BOOT.BIN").is_file() or (ctx.petl_img / "boot.mcs").is_file():
return "skipped (images exist)"
# Delegate to the tested Makefile flow -- make always exists on Linux.
cmd = ["make", "-C", str(ctx.repo.root / "PetaLinux"),
"petalinux", f"TARGET={ctx.target}", f"JOBS={ctx.jobs}"]
if not os.environ.get("PETALINUX"):
settings = find_petalinux_settings(ctx.viv_ver)
if not settings:
fail(f"PetaLinux {ctx.viv_ver} settings.sh not found -- install "
f"PetaLinux Tools or source settings.sh before running.")
rc = run_tool(["bash", "-c",
f'source "{settings}" >/dev/null && ' + shlex.join(cmd)],
cwd=ctx.repo.root)
else:
rc = run_tool(cmd, cwd=ctx.repo.root)
# The PetaLinux Makefile can exit 0 without producing images (e.g. bad
# environment) -- verify like every other stage.
boot_ok = (ctx.petl_img / "BOOT.BIN").is_file() or (ctx.petl_img / "boot.mcs").is_file()
if rc != 0 or not boot_ok:
fail(f"PetaLinux build failed (rc={rc}; boot artifact "
f"{'ok' if boot_ok else 'MISSING in ' + str(ctx.petl_img)}).")
return "built"
def yocto_port_cfg_dir(ctx: Context):
"""Optional per-target overlay layer (e.g. the Ethernet port-config
layers), derived from data.json 'lanes' the same way update.py used to
generate the Makefile target table. None when the repo ships no
Yocto/bsp/port-configs/ or the derived layer does not exist."""
root = ctx.repo.root / "Yocto" / "bsp" / "port-configs"
lanes = ctx.design.get("lanes")
if not root.is_dir() or not isinstance(lanes, list):
return None
length = 8 if len(lanes) > 4 else 4
name = "ports-" + "".join(str(i) if i in lanes else "-"
for i in range(length))
d = root / name
return d if d.is_dir() else None
def stage_yocto(ctx: Context):
"""Drive the Yocto / EDF flow: the engine is the four shell scripts in
Yocto/scripts/ (init-workspace, configure-build, build-image,
package-output); this stage sequences them with the same done-markers and
freshness rules the retired Yocto/Makefile used."""
if not ctx.design.get("yocto", False):
return "skipped (target has no Yocto flow)"
ydir = ctx.repo.root / "Yocto"
scripts = ydir / "scripts"
if not (scripts / "build-image.sh").is_file():
return ("skipped (data.json marks this target yocto but the repo "
"ships no Yocto flow yet)")
if IS_WINDOWS:
print(" Yocto cannot run on Windows. Build this target on a Linux "
"machine:")
print(f" ./build.sh yocto --target {ctx.target}")
return "BLOCKED (Linux required)"
work = ydir / ctx.target
img = work / "images" / "linux"
# Zynq-7000 produces a u-boot-wrapped uImage; ZynqMP/Versal a raw Image.
kernel = "uImage" if ctx.family == "zynq" else "Image"
products = [img / "BOOT.BIN", img / kernel,
img / "rootfs.tar.gz", img / "rootfs.wic.xz"]
if all(p.is_file() for p in products):
return "skipped (images exist)"
stage_xsa(ctx)
# The scripts need the Vitis environment (xsct/sdtgen) and Google's
# `repo` tool on PATH.
vitis = find_tool("Vitis", ctx.viv_ver)
if not vitis:
fail(f"Vitis {ctx.viv_ver} not found (the Yocto flow needs xsct/sdtgen).")
def sh(script, args, what):
cmd = (f'source "{vitis / "settings64.sh"}" >/dev/null && '
+ shlex.join([str(scripts / script)] + [str(a) for a in args]))
rc = run_tool(["bash", "-c", cmd], cwd=ydir)
if rc != 0:
fail(f"Yocto {what} failed (rc={rc}) for {ctx.target}.")
# 1. Manifest workspace (repo init + sync, ~5 GB on first run). The EDF
# manifest drops edf-init-build-env at the workspace root when done.
if not (work / "edf-init-build-env").is_file():
sh("init-workspace.sh",
[work, "https://github.com/Xilinx/yocto-manifests.git",
f"rel-v{ctx.viv_ver}", "default-edf.xml"], "workspace init")
# 2. Hardware handoff: the XSA is all the Yocto build consumes.
hw_xsa = work / "hw" / ctx.xsa.name
if not hw_xsa.is_file() or hw_xsa.stat().st_mtime < ctx.xsa.stat().st_mtime:
hw_xsa.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(ctx.xsa, hw_xsa)
# 3. Configure (sdtgen + gen-machineconf parse-sdt + BSP/overlay/sstate).
# Re-run when the XSA or the board conf is newer than the done-marker.
board = ctx.target.split("_")[0]
bsp = ydir / "bsp" / board
conf_append = bsp / "conf" / "local.conf.append"
done = work / "configdone.txt"
offline = ydir / "offline.txt"
sstate = ""
if offline.is_file():
sstate = offline.read_text(encoding="utf-8").splitlines()[0].strip()
deps = [p for p in (hw_xsa, conf_append) if p.is_file()]
if not done.is_file() or any(done.stat().st_mtime < p.stat().st_mtime
for p in deps):
sh("configure-build.sh",
[work, ctx.target, bsp, hw_xsa, sstate, ctx.repo.bd_name,
yocto_port_cfg_dir(ctx) or ""], "configure")
done.write_text("configured by build.py\n")
# 4. bitbake (always re-run while products are missing; it is incremental).
sh("build-image.sh", [work, "edf-linux-disk-image", ctx.jobs],
"bitbake build")
# 5. Gather the deploy outputs into images/linux/.
sh("package-output.sh", [work, img], "packaging")
missing = [p.name for p in products if not p.is_file()]
if missing:
fail(f"Yocto build completed but products are missing in "
f"{rel_to_repo(img, ctx.repo.root)}: {', '.join(missing)}")
return "built"
def _zip_tree(zip_path: Path, entries):
"""entries: list of (src_file, arcname) or (text, arcname) for readmes."""
zip_path.parent.mkdir(exist_ok=True)
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as z:
for src, arc in entries:
if isinstance(src, Path):
z.write(src, arc)
else:
z.writestr(arc, src)
def _tar_member_bytes(tgz: Path, member: str):
"""One member's bytes from a .tar.gz, or None if tarball/member is missing."""
import tarfile
if not tgz.is_file():
return None
try:
with tarfile.open(tgz, "r:gz") as t:
for name in ("./" + member, member):
try:
f = t.extractfile(name)
except KeyError:
continue
if f:
return f.read()
except (OSError, tarfile.TarError):
pass
return None
def stage_bootimage(ctx: Context):
results = []
if ctx.design.get("baremetal", False) and not has_vitis_flow(ctx):
results.append("standalone zip skipped (repo has no Vitis flow)")
elif ctx.design.get("baremetal", False):
if ctx.bare_zip.is_file():
results.append("standalone zip exists")
elif ctx.boot_file.is_file():
entries = [(p, p.relative_to(ctx.vit_boot).as_posix())
for p in sorted(ctx.vit_boot.rglob("*")) if p.is_file()]
_zip_tree(ctx.bare_zip, entries)
results.append(f"wrote {ctx.bare_zip.name}")
else:
fail(f"baremetal boot file missing: {ctx.boot_file}")
if ctx.design.get("petalinux", False):
img = ctx.petl_img
if ctx.petl_zip.is_file():
results.append("petalinux zip exists")
else:
boot_readme = "Copy these files to the boot (FAT32) partition of the SD card\n"
root_readme = "Extract contents of rootfs.tar.gz to the root partition of the SD card\n"
if ctx.family == "microblaze":
wanted = ["boot.mcs", "boot.prm", "image.elf", "system.bit"]
entries = [(img / "boot.mcs", "flash/boot.mcs"),
(img / "boot.prm", "flash/boot.prm"),
("Program the flash with this MCS file to boot from flash\n",
"flash/readme.txt"),
(img / "image.elf", "jtag/image.elf"),
(img / "system.bit", "jtag/system.bit"),
("Load these files via JTAG to boot PetaLinux from JTAG\n",
"jtag/readme.txt")]
else:
wanted = ["BOOT.BIN", "image.ub", "boot.scr", "rootfs.tar.gz"]
entries = [(img / "BOOT.BIN", "boot/BOOT.BIN"),
(img / "image.ub", "boot/image.ub"),
(img / "boot.scr", "boot/boot.scr"),
(boot_readme, "boot/readme.txt"),
(img / "rootfs.tar.gz", "root/rootfs.tar.gz"),
(root_readme, "root/readme.txt")]
missing = [w for w in wanted if not (img / w).is_file()]
if missing:
if IS_WINDOWS:
results.append(f"petalinux zip NOT gathered (artifacts missing: "
f"{', '.join(missing)}; build them on Linux)")
else:
fail(f"PetaLinux artifacts missing in "
f"{rel_to_repo(img, ctx.repo.root)}: {', '.join(missing)}")
else:
_zip_tree(ctx.petl_zip, entries)
results.append(f"wrote {ctx.petl_zip.name}")
if ctx.design.get("yocto", False):
img = ctx.yocto_img
if ctx.yocto_zip.is_file():
results.append("yocto zip exists")
else:
# The EDF Yocto wic is a full SD-card image; bmaptool uses the .bmap
# (xzcat|dd is the fallback). The wic's ESP does NOT carry BOOT.BIN on
# any family (verified 2026-07 on vck190: bootimg-efi-amd populates
# systemd-boot + kernel Image only, and the BootROM finds no boot image
# -> dead board), so BOOT.BIN must be copied onto the first FAT32 (ESP)
# partition after flashing -- ship it alongside the wic.
wanted = ["rootfs.wic.xz", "rootfs.wic.bmap"]
entries = [(img / "rootfs.wic.xz", "rootfs.wic.xz"),
(img / "rootfs.wic.bmap", "rootfs.wic.bmap")]
flash = (
"Flash rootfs.wic.xz to the SD card's RAW block device (e.g. /dev/sdX),\n"
"not a partition.\n"
"With bmaptool (faster, uses rootfs.wic.bmap):\n"
" bmaptool copy rootfs.wic.xz /dev/sdX\n"
"Without bmaptool:\n"
" xzcat rootfs.wic.xz | sudo dd of=/dev/sdX bs=4M conv=fsync\n")
wanted.append("BOOT.BIN")
entries.append((img / "BOOT.BIN", "BOOT.BIN"))
sd_readme = flash + (
"\nThen copy BOOT.BIN onto the FIRST partition (p1, FAT32/ESP):\n"
"the wic does not carry BOOT.BIN where the BootROM can read it,\n"
"so the card will not boot until BOOT.BIN is on p1, e.g.:\n"
" sudo mount /dev/sdX1 /mnt && sudo cp BOOT.BIN /mnt/ && sudo umount /mnt\n")
if ctx.family == "versal":
# The versal wic's ESP also ships an EMPTY EFI/BOOT/ (no
# systemd-boot binary), so u-boot's EFI boot manager has
# nothing to load even once BOOT.BIN is in place (verified
# 2026-07 on vck190). Ship systemd-boot from the image rootfs
# so the user can install it alongside BOOT.BIN.
efi = _tar_member_bytes(
img / "rootfs.tar.gz",
"usr/lib/systemd/boot/efi/systemd-bootaa64.efi")
if efi is not None:
entries.append((efi, "BOOTAA64.EFI"))
sd_readme += (
"\nVersal: ALSO copy BOOTAA64.EFI to EFI/BOOT/ on p1\n"
"(the wic ships an empty EFI/BOOT/, so u-boot's EFI boot\n"
"manager otherwise stops at the u-boot prompt), e.g.:\n"
" sudo mount /dev/sdX1 /mnt && sudo mkdir -p /mnt/EFI/BOOT \\\n"
" && sudo cp BOOTAA64.EFI /mnt/EFI/BOOT/ && sudo umount /mnt\n")
entries.append((sd_readme, "readme.txt"))
missing = [w for w in wanted if not (img / w).is_file()]
if missing:
if IS_WINDOWS:
results.append(f"yocto zip NOT gathered (artifacts missing: "
f"{', '.join(missing)}; build them on Linux)")
else:
fail(f"Yocto artifacts missing in "
f"{rel_to_repo(img, ctx.repo.root)}: {', '.join(missing)}")
else:
_zip_tree(ctx.yocto_zip, entries)
results.append(f"wrote {ctx.yocto_zip.name}")
return "; ".join(results) if results else "nothing to gather"
STAGE_FUNCS = {"ip": stage_ip, "project": stage_project, "xsa": stage_xsa,
"standalone": stage_bootfile,
"petalinux": stage_petalinux, "yocto": stage_yocto,
"package": stage_bootimage}
def fmt_artifact(p: Path):
if p.is_file():
import datetime
mt = datetime.datetime.fromtimestamp(p.stat().st_mtime)
return f"{mt:%Y-%m-%d %H:%M}"
if p.is_dir():
return "(directory)"
return "NOT BUILT"
def status_tree():
"""Connector glyphs for the status tree: (only, first, middle, last).
Box-drawing right angles where the console can render them; an ASCII
fallback for code pages / redirects that can't encode them (e.g. a
non-UTF-8 Windows console)."""
pretty = ("──▸", # ──▸ single output
"┬─▸", # ┬─▸ first of several
"├─▸", # ├─▸ middle
"└─▸") # └─▸ last
try:
"".join(pretty).encode(sys.stdout.encoding or "ascii")
return pretty
except (LookupError, UnicodeError):
return ("-->", "+->", "+->", "\\->")
def _wrap_path(s, width):
"""Wrap a (whitespace-free) display path to `width` columns, breaking after
a '/' separator when one fits so path segments stay intact, and hard-breaking
an over-long segment. Returns a list of one or more line chunks."""
lines = []
while len(s) > width:
cut = s.rfind("/", 0, width)
cut = cut + 1 if cut > 0 else width # keep '/' on this line, else hard break
lines.append(s[:cut])
s = s[cut:]
lines.append(s)
return lines
def print_status(ctx: Context):
print(f"=== status: {ctx.target} ({ctx.family}) ===")
# Artifacts grouped by the build subcommand that produces them, in build
# order. The command name is the first column (so it's clear which command
# makes which output); a command with several outputs branches to each with
# a tree connector.
groups = [] # list of (command, [artifact paths])
kind, ip_dir = ip_flow(ctx)
if kind == "cores":
def _ip_artifact(c):
td = c / ctx.target
hits = sorted(td.glob("component_*/hls/impl/ip/component.xml"))
return hits[0] if hits else (
td / "component_*" / "hls" / "impl" / "ip" / "component.xml")
groups.append(("ip", [_ip_artifact(c) for c in hls_cores(ip_dir)]))
elif kind == "board":
groups.append(("ip", [ip_dir / "build" / ctx.target / "ip_done.txt"]))
groups.append(("project", [ctx.xpr]))
groups.append(("xsa", [ctx.xsa]))
# Only show artifact groups the target actually supports, so an unsupported
# flow never reads as a "NOT BUILT" build that's waiting to be done.
if ctx.design.get("baremetal", False):
groups.append(("standalone", [ctx.vit_ws, ctx.boot_file]))
if ctx.design.get("petalinux", False):
if ctx.family == "microblaze":
groups.append(("petalinux", [ctx.petl_img / "boot.mcs"]))
else:
groups.append(("petalinux", [ctx.petl_img / "BOOT.BIN",
ctx.petl_img / "image.ub"]))
if ctx.design.get("yocto", False):
groups.append(("yocto", [ctx.yocto_img / "rootfs.wic.xz"]))
pkg = [] # bootimage zips -- all produced by the 'package' command
if ctx.design.get("baremetal", False):
pkg.append(ctx.bare_zip)
if ctx.design.get("petalinux", False):
pkg.append(ctx.petl_zip)
if ctx.design.get("yocto", False):
pkg.append(ctx.yocto_zip)
if pkg:
groups.append(("package", pkg))
prefix = f"{ctx.repo.prj_name}_" # redundant zip-name prefix
def disp(p):
s = rel_to_repo(p, ctx.repo.root)
# Bootimage zips are named <prj>_<target>_<flow>-<ver>.zip; the project
# name prefix is redundant in status and makes lines wrap, so elide it.
if p.name.startswith(prefix):
s = s[:-len(p.name)] + "..._" + p.name[len(prefix):]
return s
only, first, middle, last = status_tree()
w = max(len(c) for c, _ in groups)
state_w = 16
indent = 2 + w + 1 + len(only) + 1 + state_w + 2 # where the path column starts
text_w = max(shutil.get_terminal_size((80, 24)).columns - indent, 20)
for cmd, paths in groups:
n = len(paths)
for i, p in enumerate(paths):
if n == 1:
branch = only
elif i == 0:
branch = first
elif i == n - 1: