@@ -42,6 +42,23 @@ def get_git_ref_or_rev(dir: str) -> str:
4242 cmd_rev = "git rev-parse --short HEAD"
4343 return subprocess .check_output (shlex .split (cmd_rev ), cwd = dir , text = True ).strip ()
4444
45+ def switch_back (
46+ switch_back : bool , stash : bool , source_dir : str , old_ref : str , new_ref : str
47+ ):
48+ # Switch back to the current revision if needed and inform the user of where
49+ # the HEAD is. Must be called after checking out the previous commit on all
50+ # exit paths.
51+ if switch_back :
52+ print ("Switching back to current revision.." )
53+ if stash :
54+ subprocess .run (shlex .split ("git stash pop" ), cwd = source_dir )
55+ subprocess .run (shlex .split (f"git checkout { old_ref } " ), cwd = source_dir )
56+ else :
57+ print (
58+ f"The repository { source_dir } has been switched from { old_ref } "
59+ f"to { new_ref } . Local changes were stashed. Switch back using\n \t "
60+ f"git checkout { old_ref } \n "
61+ )
4562
4663def main ():
4764 parser = argparse .ArgumentParser (
@@ -87,22 +104,34 @@ def main():
87104 if not args .create_wrapper and len (wrapper_args ) > 0 :
88105 parser .parse_args ()
89106
90- bolt_path = f"{ args .build_dir } /bin/llvm-bolt"
91-
92- source_dir = None
93107 # find the repo directory
94- with open (f"{ args .build_dir } /CMakeCache.txt" ) as f :
95- for line in f :
96- m = re .match (r"LLVM_SOURCE_DIR:STATIC=(.*)" , line )
97- if m :
98- source_dir = m .groups ()[0 ]
99- if not source_dir :
100- sys .exit ("Source directory is not found" )
108+ source_dir = None
109+ try :
110+ CMCacheFilename = f"{ args .build_dir } /CMakeCache.txt"
111+ with open (CMCacheFilename ) as f :
112+ for line in f :
113+ m = re .match (r"LLVM_SOURCE_DIR:STATIC=(.*)" , line )
114+ if m :
115+ source_dir = m .groups ()[0 ]
116+ if not source_dir :
117+ raise Exception (f"Source directory not found: '{ CMCacheFilename } '" )
118+ except Exception as e :
119+ sys .exit (e )
120+
121+ # clean the previous llvm-bolt if it exists
122+ bolt_path = f"{ args .build_dir } /bin/llvm-bolt"
123+ if os .path .exists (bolt_path ):
124+ os .remove (bolt_path )
101125
102126 # build the current commit
127+ print ("NFC-Setup: Building current revision.." )
103128 subprocess .run (
104129 shlex .split ("cmake --build . --target llvm-bolt" ), cwd = args .build_dir
105130 )
131+
132+ if not os .path .exists (bolt_path ):
133+ sys .exit (f"Failed to build the current revision: '{ bolt_path } '" )
134+
106135 # rename llvm-bolt
107136 os .replace (bolt_path , f"{ bolt_path } .new" )
108137 # memorize the old hash for logging
@@ -133,11 +162,18 @@ def main():
133162 subprocess .run (shlex .split (f"git checkout -f { args .cmp_rev } " ), cwd = source_dir )
134163 # get the parent commit hash for logging
135164 new_ref = get_git_ref_or_rev (source_dir )
165+
136166 # build the previous commit
167+ print ("NFC-Setup: Building previous revision.." )
137168 subprocess .run (
138169 shlex .split ("cmake --build . --target llvm-bolt" ), cwd = args .build_dir
139170 )
171+
140172 # rename llvm-bolt
173+ if not os .path .exists (bolt_path ):
174+ print (f"Failed to build the previous revision: '{ bolt_path } '" )
175+ switch_back (args .switch_back , stash , source_dir , old_ref , new_ref )
176+ sys .exit (1 )
141177 os .replace (bolt_path , f"{ bolt_path } .old" )
142178
143179 # symlink llvm-bolt-wrapper
@@ -156,18 +192,12 @@ def main():
156192 # symlink llvm-bolt-wrapper
157193 os .symlink (wrapper_path , bolt_path )
158194 except Exception as e :
159- sys .exit ("Failed to create a wrapper:\n " + str (e ))
195+ print ("Failed to create a wrapper:\n " + str (e ))
196+ switch_back (args .switch_back , stash , source_dir , old_ref , new_ref )
197+ sys .exit (1 )
198+
199+ switch_back (args .switch_back , stash , source_dir , old_ref , new_ref )
160200
161- if args .switch_back :
162- if stash :
163- subprocess .run (shlex .split ("git stash pop" ), cwd = source_dir )
164- subprocess .run (shlex .split (f"git checkout { old_ref } " ), cwd = source_dir )
165- else :
166- print (
167- f"The repository { source_dir } has been switched from { old_ref } "
168- f"to { new_ref } . Local changes were stashed. Switch back using\n \t "
169- f"git checkout { old_ref } \n "
170- )
171201 print (
172202 f"Build directory { args .build_dir } is ready to run BOLT tests, e.g.\n "
173203 "\t bin/llvm-lit -sv tools/bolt/test\n or\n "
0 commit comments