@@ -42,6 +42,23 @@ def get_git_ref_or_rev(dir: str) -> str:
42
42
cmd_rev = "git rev-parse --short HEAD"
43
43
return subprocess .check_output (shlex .split (cmd_rev ), cwd = dir , text = True ).strip ()
44
44
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
+ )
45
62
46
63
def main ():
47
64
parser = argparse .ArgumentParser (
@@ -87,22 +104,34 @@ def main():
87
104
if not args .create_wrapper and len (wrapper_args ) > 0 :
88
105
parser .parse_args ()
89
106
90
- bolt_path = f"{ args .build_dir } /bin/llvm-bolt"
91
-
92
- source_dir = None
93
107
# 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 )
101
125
102
126
# build the current commit
127
+ print ("NFC-Setup: Building current revision.." )
103
128
subprocess .run (
104
129
shlex .split ("cmake --build . --target llvm-bolt" ), cwd = args .build_dir
105
130
)
131
+
132
+ if not os .path .exists (bolt_path ):
133
+ sys .exit (f"Failed to build the current revision: '{ bolt_path } '" )
134
+
106
135
# rename llvm-bolt
107
136
os .replace (bolt_path , f"{ bolt_path } .new" )
108
137
# memorize the old hash for logging
@@ -133,11 +162,18 @@ def main():
133
162
subprocess .run (shlex .split (f"git checkout -f { args .cmp_rev } " ), cwd = source_dir )
134
163
# get the parent commit hash for logging
135
164
new_ref = get_git_ref_or_rev (source_dir )
165
+
136
166
# build the previous commit
167
+ print ("NFC-Setup: Building previous revision.." )
137
168
subprocess .run (
138
169
shlex .split ("cmake --build . --target llvm-bolt" ), cwd = args .build_dir
139
170
)
171
+
140
172
# 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 )
141
177
os .replace (bolt_path , f"{ bolt_path } .old" )
142
178
143
179
# symlink llvm-bolt-wrapper
@@ -156,18 +192,12 @@ def main():
156
192
# symlink llvm-bolt-wrapper
157
193
os .symlink (wrapper_path , bolt_path )
158
194
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 )
160
200
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
- )
171
201
print (
172
202
f"Build directory { args .build_dir } is ready to run BOLT tests, e.g.\n "
173
203
"\t bin/llvm-lit -sv tools/bolt/test\n or\n "
0 commit comments