@@ -10,17 +10,20 @@ using std::string;
10
10
using std::endl;
11
11
12
12
// in 'cleanup' mode checker may modify the blif
13
+ // if checker wrote blif, 'outFn' is the file name
13
14
bool do_check_blif (CStr cfn,
14
15
vector<string>& badInputs,
15
16
vector<string>& badOutputs,
16
17
vector<uspair>& corrected,
18
+ string& outFn,
17
19
bool cleanup) {
18
20
assert (cfn);
19
21
uint16_t tr = ltrace ();
20
22
auto & ls = lout ();
21
23
badInputs.clear ();
22
24
badOutputs.clear ();
23
25
corrected.clear ();
26
+ outFn.clear ();
24
27
flush_out (false );
25
28
26
29
BLIF_file bfile (string{cfn});
@@ -127,13 +130,12 @@ bool do_check_blif(CStr cfn,
127
130
std::filesystem::path base_path = full_path.filename ();
128
131
std::string base = base_path.string ();
129
132
// lputs9();
130
- string outFn;
131
133
if (cleanup) {
132
134
flush_out (true );
133
135
lprintf (" [PLANNER BLIF-CLEANER] : replacing file '%s' ...\n " , cfn);
134
136
flush_out (true );
135
137
// cannot write to the currently mem-mapped file,
136
- // write to a temproray and rename later.
138
+ // write to a temporary and rename later.
137
139
outFn = str::concat (full_path.lexically_normal ().string (),
138
140
" +BLIF_CLEANER.tmp_" , std::to_string (get_PID ()));
139
141
} else {
@@ -144,9 +146,9 @@ bool do_check_blif(CStr cfn,
144
146
145
147
if (wr_ok.empty ()) {
146
148
lprintf (" ---!! FAILED writeBlif to '%s'\n " , outFn.c_str ());
147
- if (cleanup) {
149
+ if (cleanup)
148
150
lprintf (" [PLANNER BLIF-CLEANER] : FAILED\n " );
149
- }
151
+ outFn. clear ();
150
152
} else {
151
153
lprintf (" +++++ WRITTEN '%s'\n " , wr_ok.c_str ());
152
154
if (cleanup) {
@@ -180,23 +182,88 @@ bool do_check_blif(CStr cfn,
180
182
181
183
// 'corrected' : (lnum, removed_net)
182
184
bool do_cleanup_blif (CStr cfn, vector<uspair>& corrected) {
185
+ using namespace fio ;
186
+ using namespace std ;
183
187
assert (cfn);
184
188
corrected.clear ();
189
+ uint16_t tr = ltrace ();
185
190
186
191
vector<string> badInp, badOut;
187
- bool status = do_check_blif (cfn, badInp, badOut, corrected, true );
192
+ string outFn;
193
+
194
+ bool status = do_check_blif (cfn, badInp, badOut, corrected, outFn, true );
188
195
189
196
size_t cor_sz = corrected.size ();
190
197
if (status and cor_sz) {
198
+
191
199
flush_out (true );
192
200
lprintf (" [PLANNER BLIF-CLEANER] : corrected netlist '%s'\n " , cfn);
193
201
lprintf (" -- removed dangling nets (%zu):\n " , cor_sz);
194
- for (size_t i = 0 ; i < cor_sz; i++) {
195
- const uspair& cor = corrected[i];
196
- lprintf (" line %u net %s\n " , cor.first , cor.second .c_str ());
202
+ if (tr >= 3 ) {
203
+ for (size_t i = 0 ; i < cor_sz; i++) {
204
+ const uspair& cor = corrected[i];
205
+ lprintf (" line %u net %s\n " , cor.first , cor.second .c_str ());
206
+ if (tr < 4 and i > 40 and cor_sz > 50 ) {
207
+ lputs (" ..." );
208
+ break ;
209
+ }
210
+ }
211
+ lprintf (" -- removed dangling nets (%zu).\n " , cor_sz);
197
212
}
198
- lprintf (" -- removed dangling nets (%zu).\n " , cor_sz);
199
213
flush_out (true );
214
+
215
+ bool outFn_exists = Fio::nonEmptyFileExists (outFn);
216
+ bool cfn_exists = Fio::nonEmptyFileExists (cfn);
217
+ // assert(cfn_exists);
218
+ if (outFn_exists and cfn_exists) {
219
+
220
+ bool error1 = false , error2 = false ;
221
+
222
+ // -- 1. add prefix 'orig_' to the original BLIF
223
+ {
224
+ string newName = str::concat (" orig_" , cfn);
225
+ filesystem::path newPath{newName};
226
+ filesystem::path oldPath{cfn};
227
+ error_code ec;
228
+ filesystem::rename (oldPath, newPath, ec);
229
+ if (ec) {
230
+ error1 = true ;
231
+ lout () << endl
232
+ << " BLIF-CLEANER: [Error] renaming original BLIF to "
233
+ << newPath << endl
234
+ << " ERROR: " << ec.message () << ' \n ' << endl;
235
+ }
236
+ else if (tr >= 3 ) {
237
+ lprintf (" [PLANNER BLIF-CLEANER] : original BLIF saved as '%s'\n " ,
238
+ newName.c_str ());
239
+ }
240
+ }
241
+
242
+ // -- 2. rename 'outFn' to 'cfn'
243
+ if (not error1) {
244
+ filesystem::path oldPath{outFn};
245
+ filesystem::path newPath{cfn};
246
+ error_code ec;
247
+ filesystem::rename (oldPath, newPath, ec);
248
+ if (ec) {
249
+ error2 = true ;
250
+ lout () << endl
251
+ << " BLIF-CLEANER: [Error] renaming temporary BLIF to "
252
+ << newPath << endl
253
+ << " ERROR: " << ec.message () << ' \n ' << endl;
254
+ }
255
+ else if (tr >= 3 ) {
256
+ string newName = newPath.lexically_normal ().string ();
257
+ lprintf (" [PLANNER BLIF-CLEANER] : corrected BLIF written : %s\n " ,
258
+ newName.c_str ());
259
+ }
260
+ }
261
+
262
+ if (error1 or error2) {
263
+ status = false ;
264
+ lputs (" [PLANNER BLIF-CLEANER] : FAILED due to filesystem errors\n " );
265
+ }
266
+ }
200
267
}
201
268
202
269
return status;
@@ -270,7 +337,8 @@ bool do_check(const rsOpts& opts, bool blif_vs_csv) {
270
337
if (blif_vs_csv) {
271
338
vector<string> badInp, badOut;
272
339
vector<uspair> corrected;
273
- status = do_check_blif (cfn, badInp, badOut, corrected, false );
340
+ string outFn;
341
+ status = do_check_blif (cfn, badInp, badOut, corrected, outFn, false );
274
342
} else {
275
343
status = do_check_csv (cfn);
276
344
}
0 commit comments