Skip to content

Commit bf61d22

Browse files
nathanchanceojeda
authored andcommitted
tc_build: Introduce a common GitSourceManager
We will be adding Rust support in the future, so pull out common git repository management code from LLVMSourceManager that can be shared with a RustSourceManager. Signed-off-by: Nathan Chancellor <[email protected]>
1 parent 611cc49 commit bf61d22

File tree

2 files changed

+72
-57
lines changed

2 files changed

+72
-57
lines changed

tc_build/llvm.py

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python3
22

3-
import contextlib
43
import os
54
from pathlib import Path
65
import platform
@@ -10,6 +9,7 @@
109
import time
1110

1211
from tc_build.builder import Builder
12+
from tc_build.source import GitSourceManager
1313
import tc_build.utils
1414

1515
LLVM_VER_FOR_RUNTIMES = 20
@@ -572,10 +572,13 @@ class LLVMSlimInstrumentedBuilder(LLVMInstrumentedBuilder, LLVMSlimBuilder):
572572
pass
573573

574574

575-
class LLVMSourceManager:
575+
class LLVMSourceManager(GitSourceManager):
576576

577577
def __init__(self, repo):
578-
self.repo = repo
578+
super().__init__(repo)
579+
580+
self._pretty_name = 'LLVM'
581+
self._repo_url = 'https://github.com/llvm/llvm-project.git'
579582

580583
def default_projects(self):
581584
return ['clang', 'compiler-rt', 'lld', 'polly']
@@ -591,57 +594,3 @@ def default_targets(self):
591594
targets.append('LoongArch')
592595

593596
return targets
594-
595-
def download(self, ref, shallow=False):
596-
if self.repo.exists():
597-
return
598-
599-
tc_build.utils.print_header('Downloading LLVM')
600-
601-
git_clone = ['git', 'clone']
602-
if shallow:
603-
git_clone.append('--depth=1')
604-
if ref != 'main':
605-
git_clone.append('--no-single-branch')
606-
git_clone += ['https://github.com/llvm/llvm-project', self.repo]
607-
608-
subprocess.run(git_clone, check=True)
609-
610-
self.git(['checkout', ref])
611-
612-
def git(self, cmd, capture_output=False):
613-
return subprocess.run(['git', *cmd],
614-
capture_output=capture_output,
615-
check=True,
616-
cwd=self.repo,
617-
text=True)
618-
619-
def git_capture(self, cmd):
620-
return self.git(cmd, capture_output=True).stdout.strip()
621-
622-
def is_shallow(self):
623-
git_dir = self.git_capture(['rev-parse', '--git-dir'])
624-
return Path(git_dir, 'shallow').exists()
625-
626-
def ref_exists(self, ref):
627-
try:
628-
self.git(['show-branch', ref])
629-
except subprocess.CalledProcessError:
630-
return False
631-
return True
632-
633-
def update(self, ref):
634-
tc_build.utils.print_header('Updating LLVM')
635-
636-
self.git(['fetch', 'origin'])
637-
638-
if self.is_shallow() and not self.ref_exists(ref):
639-
raise RuntimeError(f"Repo is shallow and supplied ref ('{ref}') does not exist!")
640-
641-
self.git(['checkout', ref])
642-
643-
local_ref = None
644-
with contextlib.suppress(subprocess.CalledProcessError):
645-
local_ref = self.git_capture(['symbolic-ref', '-q', 'HEAD'])
646-
if local_ref and local_ref.startswith('refs/heads/'):
647-
self.git(['pull', '--rebase', 'origin', local_ref.replace('refs/heads/', '')])

tc_build/source.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python3
22

3+
import contextlib
34
import hashlib
5+
from pathlib import Path
46
import re
57
import subprocess
68

@@ -86,3 +88,67 @@ class SourceManager:
8688
def __init__(self, location=None):
8789
self.location = location
8890
self.tarball = Tarball()
91+
92+
93+
class GitSourceManager:
94+
95+
def __init__(self, repo):
96+
self.repo = repo
97+
98+
# Will be set by derived classes but used here
99+
self._pretty_name = ''
100+
self._repo_url = ''
101+
102+
def download(self, ref, shallow=False):
103+
if self.repo.exists():
104+
return
105+
106+
tc_build.utils.print_header(f"Downloading {self._pretty_name}")
107+
108+
git_clone = ['git', 'clone']
109+
if shallow:
110+
git_clone.append('--depth=1')
111+
if ref != 'main':
112+
git_clone.append('--no-single-branch')
113+
git_clone += [self._repo_url, self.repo]
114+
115+
subprocess.run(git_clone, check=True)
116+
117+
self.git(['checkout', ref])
118+
119+
def git(self, cmd, capture_output=False):
120+
return subprocess.run(['git', *cmd],
121+
capture_output=capture_output,
122+
check=True,
123+
cwd=self.repo,
124+
text=True)
125+
126+
def git_capture(self, cmd):
127+
return self.git(cmd, capture_output=True).stdout.strip()
128+
129+
def is_shallow(self):
130+
git_dir = self.git_capture(['rev-parse', '--git-dir'])
131+
return Path(git_dir, 'shallow').exists()
132+
133+
def ref_exists(self, ref):
134+
try:
135+
self.git(['show-branch', ref])
136+
except subprocess.CalledProcessError:
137+
return False
138+
return True
139+
140+
def update(self, ref):
141+
tc_build.utils.print_header(f"Updating {self._pretty_name}")
142+
143+
self.git(['fetch', 'origin'])
144+
145+
if self.is_shallow() and not self.ref_exists(ref):
146+
raise RuntimeError(f"Repo is shallow and supplied ref ('{ref}') does not exist!")
147+
148+
self.git(['checkout', ref])
149+
150+
local_ref = None
151+
with contextlib.suppress(subprocess.CalledProcessError):
152+
local_ref = self.git_capture(['symbolic-ref', '-q', 'HEAD'])
153+
if local_ref and local_ref.startswith('refs/heads/'):
154+
self.git(['pull', '--rebase', 'origin', local_ref.replace('refs/heads/', '')])

0 commit comments

Comments
 (0)