forked from ROCm/TheRock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_sources.py
More file actions
executable file
·606 lines (548 loc) · 18.8 KB
/
fetch_sources.py
File metadata and controls
executable file
·606 lines (548 loc) · 18.8 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
#!/usr/bin/env python
# Fetches sources from a specified branch/set of projects.
# This script is available for users, but it is primarily the mechanism
# the CI uses to get to a clean state.
#
# Stage-aware fetching:
# Use --stage <stage_name> to fetch only submodules needed for a build stage.
# This uses BUILD_TOPOLOGY.toml to determine which submodules are required.
#
# Legacy flag-based fetching:
# Use --include-* flags to control which project groups to fetch.
# This is the original behavior and is still supported.
import argparse
import hashlib
from pathlib import Path
import platform
import shlex
import shutil
import subprocess
import sys
from typing import List
import os
THIS_SCRIPT_DIR = Path(__file__).resolve().parent
THEROCK_DIR = THIS_SCRIPT_DIR.parent
PATCHES_DIR = THEROCK_DIR / "patches"
TOPOLOGY_PATH = THEROCK_DIR / "BUILD_TOPOLOGY.toml"
ALWAYS_SUBMODULE_PATHS = [
"base/rocm-kpack",
]
def is_windows() -> bool:
return platform.system() == "Windows"
def log(*args, **kwargs):
print(*args, **kwargs)
sys.stdout.flush()
def run_command(args: list[str | Path], cwd: Path, env: dict[str, str] | None = None):
args = [str(arg) for arg in args]
log(f"++ Exec [{cwd}]$ {shlex.join(args)}")
sys.stdout.flush()
full_env = {**os.environ, **(env or {})}
subprocess.check_call(args, cwd=str(cwd), env=full_env, stdin=subprocess.DEVNULL)
def get_projects_from_topology(stage: str) -> List[str]:
"""Get submodule names for a build stage from BUILD_TOPOLOGY.toml."""
from _therock_utils.build_topology import BuildTopology
if not TOPOLOGY_PATH.exists():
raise FileNotFoundError(f"BUILD_TOPOLOGY.toml not found at {TOPOLOGY_PATH}")
topology = BuildTopology(str(TOPOLOGY_PATH))
current_platform = platform.system().lower()
submodules = topology.get_submodules_for_stage(stage, platform=current_platform)
return [s.name for s in submodules]
def get_available_stages() -> List[str]:
"""Get list of available build stages from BUILD_TOPOLOGY.toml."""
from _therock_utils.build_topology import BuildTopology
if not TOPOLOGY_PATH.exists():
return []
topology = BuildTopology(str(TOPOLOGY_PATH))
return [s.name for s in topology.get_build_stages()]
def parse_nested_submodules(input):
"""Parse nested submodules string like 'iree:flatcc,something' into ("iree", ["flatcc", "something"])."""
project, nested = input.split(":", 1)
nested_list = [n.strip() for n in nested.split(",")] if nested else []
return (project, nested_list)
def get_enabled_projects(args) -> List[str]:
"""Get list of submodule names to fetch.
If --stage is provided, uses BUILD_TOPOLOGY.toml to determine submodules.
Otherwise, uses the legacy --include-* flags.
"""
# Stage-aware mode: use topology
if args.stage:
projects = get_projects_from_topology(args.stage)
log(f"Stage '{args.stage}' requires submodules: {projects}")
return projects
# Legacy flag-based mode
projects = []
if args.include_system_projects:
projects.extend(args.system_projects)
if args.include_compilers:
projects.extend(args.compiler_projects)
if args.include_debug_tools:
projects.extend(args.debug_tools)
if args.include_rocm_libraries:
projects.extend(["rocm-libraries"])
if args.include_rocm_systems:
projects.extend(["rocm-systems"])
if args.include_ml_frameworks:
projects.extend(args.ml_framework_projects)
if args.include_rocm_media:
projects.extend(args.rocm_media_projects)
if args.include_iree_libs:
projects.extend(args.iree_libs_projects)
if args.include_math_libraries:
projects.extend(args.math_library_projects)
return projects
def fetch_nested_submodules(args, projects):
"""Fetch nested submodules for projects specified in --nested-submodules."""
update_args = []
if args.depth:
update_args += ["--depth", str(args.depth)]
if args.progress:
update_args += ["--progress"]
if args.jobs:
update_args += ["--jobs", str(args.jobs)]
if args.remote:
update_args += ["--remote"]
for parent, nested_submodules in dict(args.nested_submodules).items():
if len(nested_submodules) == 0:
continue
# Skip if parent project wasn't fetched
if parent not in projects:
continue
# Fetch the nested submodules
parent_dir = THEROCK_DIR / get_submodule_path(parent)
nested_submodule_paths = [
get_submodule_path(nested_submodule, cwd=parent_dir)
for nested_submodule in nested_submodules
]
run_command(
["git", "submodule", "update", "--init"]
+ update_args
+ ["--"]
+ nested_submodule_paths,
cwd=parent_dir,
)
def run(args):
projects = get_enabled_projects(args)
submodule_paths = ALWAYS_SUBMODULE_PATHS + [
get_submodule_path(project) for project in projects
]
# TODO(scotttodd): Check for git lfs?
update_args = []
if args.depth:
update_args += ["--depth", str(args.depth)]
if args.progress:
update_args += ["--progress"]
if args.jobs:
update_args += ["--jobs", str(args.jobs)]
if args.remote:
update_args += ["--remote"]
if args.update_submodules:
run_command(
["git", "submodule", "update", "--init"]
+ update_args
+ ["--"]
+ submodule_paths,
cwd=THEROCK_DIR,
)
if args.dvc_projects:
pull_large_files(args.dvc_projects, projects)
# Fetch nested submodules
if args.update_submodules:
fetch_nested_submodules(args, projects)
# Because we allow local patches, if a submodule is in a patched state,
# we manually set it to skip-worktree since recording the commit is
# then meaningless. Here on each fetch, we reset the flag so that if
# patches are aged out, the tree is restored to normal.
submodule_paths = [get_submodule_path(name) for name in projects]
run_command(
["git", "update-index", "--no-skip-worktree", "--"] + submodule_paths,
cwd=THEROCK_DIR,
)
# Remove any stale .smrev files.
remove_smrev_files(args, projects)
if args.apply_patches:
apply_patches(args, projects)
def pull_large_files(dvc_projects, projects):
if not dvc_projects:
print("No DVC projects specified, skipping large file pull.")
return
dvc_missing = shutil.which("dvc") is None
if dvc_missing:
if is_windows():
print("Could not find `dvc` on PATH so large files could not be fetched")
print("Visit https://dvc.org/doc/install for installation instructions.")
sys.exit(1)
else:
print("`dvc` not found, skipping large file pull on Linux.")
return
for project in dvc_projects:
if not project in projects:
continue
submodule_path = get_submodule_path(project)
project_dir = THEROCK_DIR / submodule_path
dvc_config_file = project_dir / ".dvc" / "config"
if dvc_config_file.exists():
print(f"dvc detected in {project_dir}, running dvc pull")
run_command(["dvc", "pull"], cwd=project_dir)
else:
log(f"WARNING: dvc config not found in {project_dir}, when expected.")
def remove_smrev_files(args, projects):
for project in projects:
submodule_path = get_submodule_path(project)
project_dir = THEROCK_DIR / submodule_path
project_revision_file = project_dir.with_name(f".{project_dir.name}.smrev")
if project_revision_file.exists():
print(f"Remove stale project revision file: {project_revision_file}")
project_revision_file.unlink()
def apply_patches(args, projects):
if not args.patch_tag:
log("Not patching (no --patch-tag specified)")
patch_version_dir: Path = PATCHES_DIR / args.patch_tag
if not patch_version_dir.exists():
log(f"ERROR: Patch directory {patch_version_dir} does not exist")
for patch_project_dir in patch_version_dir.iterdir():
log(f"* Processing project patch directory {patch_project_dir}:")
# Check that project patch directory was included
if not patch_project_dir.name in projects:
log(
f"* Project patch directory {patch_project_dir.name} was not included. Skipping."
)
continue
submodule_path = get_submodule_path(patch_project_dir.name)
submodule_url = get_submodule_url(patch_project_dir.name)
submodule_revision = get_submodule_revision(submodule_path)
project_dir = THEROCK_DIR / submodule_path
project_revision_file = project_dir.with_name(f".{project_dir.name}.smrev")
if not project_dir.exists():
log(f"WARNING: Source directory {project_dir} does not exist. Skipping.")
continue
patch_files = list(patch_project_dir.glob("*.patch"))
patch_files.sort()
log(f"Applying {len(patch_files)} patches")
run_command(
[
"git",
"-c",
"user.name=therockbot",
"-c",
"user.email=therockbot@amd.com",
"am",
"--whitespace=nowarn",
]
+ patch_files,
cwd=project_dir,
env={
"GIT_COMMITTER_DATE": "Thu, 1 Jan 2099 00:00:00 +0000",
},
)
# Since it is in a patched state, make it invisible to changes.
run_command(
["git", "update-index", "--skip-worktree", "--", submodule_path],
cwd=THEROCK_DIR,
)
# Generate the .smrev patch state file.
# This file consists of two lines: The git origin and a summary of the
# state of the source tree that was checked out. This can be consumed
# by individual build steps in lieu of heuristics for asking git. If
# the tree is in a patched state, the commit hashes of HEAD may be
# different from checkout-to-checkout, but the .smrev file will have
# stable contents so long as the submodule pin and contents of the
# hashes are the same.
# Note that this does not track the dirty state of the tree. If full
# fidelity hashes of the tree state are needed for development/dirty
# trees, then another mechanism must be used.
patches_hash = hashlib.sha1()
for patch_file in patch_files:
patch_contents = Path(patch_file).read_bytes()
patches_hash.update(patch_contents)
patches_digest = patches_hash.digest().hex()
project_revision_file.write_text(
f"{submodule_url}\n{submodule_revision}+PATCHED:{patches_digest}\n"
)
# Gets the the relative path to a submodule given its name.
# Raises an exception on failure.
def get_submodule_path(name: str, cwd=THEROCK_DIR) -> str:
relpath = (
subprocess.check_output(
[
"git",
"config",
"--file",
".gitmodules",
"--get",
f"submodule.{name}.path",
],
cwd=cwd,
)
.decode()
.strip()
)
return relpath
# Gets the the relative path to a submodule given its name.
# Raises an exception on failure.
def get_submodule_url(name: str) -> str:
relpath = (
subprocess.check_output(
[
"git",
"config",
"--file",
".gitmodules",
"--get",
f"submodule.{name}.url",
],
cwd=str(THEROCK_DIR),
)
.decode()
.strip()
)
return relpath
def get_submodule_revision(submodule_path: str) -> str:
# Generates a line like:
# 160000 5e2093d23f7d34c372a788a6f2b7df8bc1c97947 0 compiler/amd-llvm
ls_line = (
subprocess.check_output(
["git", "ls-files", "--stage", submodule_path], cwd=str(THEROCK_DIR)
)
.decode()
.strip()
)
return ls_line.split()[1]
def main(argv):
parser = argparse.ArgumentParser(
prog="fetch_sources",
description="Fetch sources for TheRock build. Use --stage for stage-aware "
"fetching or --include-* flags for legacy mode.",
)
# Stage-aware fetching (preferred for CI)
available_stages = get_available_stages()
parser.add_argument(
"--stage",
type=str,
choices=available_stages if available_stages else None,
help=f"Build stage to fetch sources for. Uses BUILD_TOPOLOGY.toml. "
f"Available: {', '.join(available_stages) if available_stages else 'none'}",
)
parser.add_argument(
"--list-stages",
action="store_true",
help="List available build stages and their submodules, then exit",
)
# Legacy options
parser.add_argument(
"--patch-tag",
type=str,
default="amd-mainline",
help="Patch tag to apply to sources after sync",
)
parser.add_argument(
"--update-submodules",
default=True,
action=argparse.BooleanOptionalAction,
help="Updates submodules",
)
parser.add_argument(
"--remote",
default=False,
action=argparse.BooleanOptionalAction,
help="Updates submodules from remote vs current",
)
parser.add_argument(
"--apply-patches",
default=True,
action=argparse.BooleanOptionalAction,
help="Apply patches",
)
parser.add_argument(
"--depth", type=int, help="Git depth when updating submodules", default=None
)
parser.add_argument(
"--progress",
default=False,
action="store_true",
help="Git progress displayed when updating submodules",
)
parser.add_argument(
"--jobs",
type=int,
help="Number of jobs to use for updating submodules",
default=None,
)
parser.add_argument(
"--nested-submodules",
nargs="+",
type=parse_nested_submodules,
default=[("iree", ["third_party/flatcc", "third_party/benchmark"])],
help="Specify which nested submodules to fetch (e.g., project1:nested_in_project1_1,nested_in_project1_2 project2:nested_in_project2)",
)
parser.add_argument(
"--include-system-projects",
default=True,
action=argparse.BooleanOptionalAction,
help="Include systems projects",
)
parser.add_argument(
"--include-compilers",
default=True,
action=argparse.BooleanOptionalAction,
help="Include compilers",
)
parser.add_argument(
"--include-debug-tools",
default=True,
action=argparse.BooleanOptionalAction,
help="Include ROCm debugging tools",
)
parser.add_argument(
"--include-rocm-libraries",
default=True,
action=argparse.BooleanOptionalAction,
help="Include supported rocm-libraries projects",
)
parser.add_argument(
"--include-rocm-systems",
default=True,
action=argparse.BooleanOptionalAction,
help="Include supported rocm-systems projects",
)
parser.add_argument(
"--include-ml-frameworks",
default=True,
action=argparse.BooleanOptionalAction,
help="Include machine learning frameworks that are part of ROCM",
)
parser.add_argument(
"--include-rocm-media",
default=True,
action=argparse.BooleanOptionalAction,
help="Include media projects that are part of ROCM",
)
parser.add_argument(
"--include-iree-libs",
default=True,
action=argparse.BooleanOptionalAction,
help="Include IREE and related libraries",
)
parser.add_argument(
"--include-math-libraries",
default=True,
action=argparse.BooleanOptionalAction,
help="Include math libraries that are part of ROCM",
)
parser.add_argument(
"--system-projects",
nargs="+",
type=str,
default=[
"half",
"rccl",
"rccl-tests",
"rocm-cmake",
"rocprof-trace-decoder",
],
)
parser.add_argument(
"--compiler-projects",
nargs="+",
type=str,
default=[
"HIPIFY",
"llvm-project",
"spirv-llvm-translator",
],
)
parser.add_argument(
"--ml-framework-projects",
nargs="+",
type=str,
default=[],
)
parser.add_argument(
"--rocm-media-projects",
nargs="+",
type=str,
default=(
[]
if is_windows()
else [
# Linux only projects.
"amd-mesa",
"rocdecode",
"rocjpeg",
]
),
)
parser.add_argument(
"--iree-libs-projects",
nargs="+",
type=str,
default=[
"iree",
"fusilli",
],
)
parser.add_argument(
# projects that use DVC to manage large files
"--dvc-projects",
nargs="+",
type=str,
default=(
[
"rocm-libraries",
"rocm-systems",
]
if is_windows()
else [
"rocm-libraries",
]
),
)
parser.add_argument(
"--debug-tools",
nargs="+",
type=str,
default=(
[]
if is_windows()
else [
# Linux only projects.
"amd-dbgapi",
"rocr-debug-agent",
"rocgdb",
]
),
)
parser.add_argument(
"--math-library-projects",
nargs="+",
type=str,
default=(
[]
if is_windows()
else [
# Linux only projects.
"libhipcxx",
]
),
)
args = parser.parse_args(argv)
# Handle --list-stages
if args.list_stages:
from _therock_utils.build_topology import BuildTopology
if not TOPOLOGY_PATH.exists():
print(f"BUILD_TOPOLOGY.toml not found at {TOPOLOGY_PATH}")
sys.exit(1)
topology = BuildTopology(str(TOPOLOGY_PATH))
print("Available build stages and their submodules:\n")
for stage in topology.get_build_stages():
submodules = topology.get_submodules_for_stage(stage.name)
submodule_names = [s.name for s in submodules]
print(f" {stage.name} ({stage.type}):")
print(f" {stage.description}")
print(
f" Submodules: {', '.join(submodule_names) if submodule_names else '(none)'}"
)
print()
sys.exit(0)
run(args)
if __name__ == "__main__":
main(sys.argv[1:])