Skip to content

[BOLT] Improve exception handling in NFC-Mode #146513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: users/paschalis-mpeis/nfc-check-remove-wrapper-logic
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 51 additions & 21 deletions bolt/utils/nfc-check-setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ def get_git_ref_or_rev(dir: str) -> str:
cmd_rev = "git rev-parse --short HEAD"
return subprocess.check_output(shlex.split(cmd_rev), cwd=dir, text=True).strip()

def switch_back(
switch_back: bool, stash: bool, source_dir: str, old_ref: str, new_ref: str
):
# Switch back to the current revision if needed and inform the user of where
# the HEAD is. Must be called after checking out the previous commit on all
# exit paths.
if switch_back:
print("Switching back to current revision..")
if stash:
subprocess.run(shlex.split("git stash pop"), cwd=source_dir)
subprocess.run(shlex.split(f"git checkout {old_ref}"), cwd=source_dir)
else:
print(
f"The repository {source_dir} has been switched from {old_ref} "
f"to {new_ref}. Local changes were stashed. Switch back using\n\t"
f"git checkout {old_ref}\n"
)

def main():
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -87,22 +104,34 @@ def main():
if not args.create_wrapper and len(wrapper_args) > 0:
parser.parse_args()

bolt_path = f"{args.build_dir}/bin/llvm-bolt"

source_dir = None
# find the repo directory
with open(f"{args.build_dir}/CMakeCache.txt") as f:
for line in f:
m = re.match(r"LLVM_SOURCE_DIR:STATIC=(.*)", line)
if m:
source_dir = m.groups()[0]
if not source_dir:
sys.exit("Source directory is not found")
source_dir = None
try:
CMCacheFilename = f"{args.build_dir}/CMakeCache.txt"
with open(CMCacheFilename) as f:
for line in f:
m = re.match(r"LLVM_SOURCE_DIR:STATIC=(.*)", line)
if m:
source_dir = m.groups()[0]
if not source_dir:
raise Exception(f"Source directory not found: '{CMCacheFilename}'")
except Exception as e:
sys.exit(e)

# clean the previous llvm-bolt if it exists
bolt_path = f"{args.build_dir}/bin/llvm-bolt"
if os.path.exists(bolt_path):
os.remove(bolt_path)

# build the current commit
print("NFC-Setup: Building current revision..")
subprocess.run(
shlex.split("cmake --build . --target llvm-bolt"), cwd=args.build_dir
)

if not os.path.exists(bolt_path):
sys.exit(f"Failed to build the current revision: '{bolt_path}'")

# rename llvm-bolt
os.replace(bolt_path, f"{bolt_path}.new")
# memorize the old hash for logging
Expand Down Expand Up @@ -133,11 +162,18 @@ def main():
subprocess.run(shlex.split(f"git checkout -f {args.cmp_rev}"), cwd=source_dir)
# get the parent commit hash for logging
new_ref = get_git_ref_or_rev(source_dir)

# build the previous commit
print("NFC-Setup: Building previous revision..")
subprocess.run(
shlex.split("cmake --build . --target llvm-bolt"), cwd=args.build_dir
)

# rename llvm-bolt
if not os.path.exists(bolt_path):
print(f"Failed to build the previous revision: '{bolt_path}'")
switch_back(args.switch_back, stash, source_dir, old_ref, new_ref)
sys.exit(1)
os.replace(bolt_path, f"{bolt_path}.old")

# symlink llvm-bolt-wrapper
Expand All @@ -156,18 +192,12 @@ def main():
# symlink llvm-bolt-wrapper
os.symlink(wrapper_path, bolt_path)
except Exception as e:
sys.exit("Failed to create a wrapper:\n" + str(e))
print("Failed to create a wrapper:\n" + str(e))
switch_back(args.switch_back, stash, source_dir, old_ref, new_ref)
sys.exit(1)

switch_back(args.switch_back, stash, source_dir, old_ref, new_ref)

if args.switch_back:
if stash:
subprocess.run(shlex.split("git stash pop"), cwd=source_dir)
subprocess.run(shlex.split(f"git checkout {old_ref}"), cwd=source_dir)
else:
print(
f"The repository {source_dir} has been switched from {old_ref} "
f"to {new_ref}. Local changes were stashed. Switch back using\n\t"
f"git checkout {old_ref}\n"
)
print(
f"Build directory {args.build_dir} is ready to run BOLT tests, e.g.\n"
"\tbin/llvm-lit -sv tools/bolt/test\nor\n"
Expand Down
Loading