@@ -10,17 +10,20 @@ using std::string;
1010using std::endl;
1111
1212// in 'cleanup' mode checker may modify the blif
13+ // if checker wrote blif, 'outFn' is the file name
1314bool do_check_blif (CStr cfn,
1415 vector<string>& badInputs,
1516 vector<string>& badOutputs,
1617 vector<uspair>& corrected,
18+ string& outFn,
1719 bool cleanup) {
1820 assert (cfn);
1921 uint16_t tr = ltrace ();
2022 auto & ls = lout ();
2123 badInputs.clear ();
2224 badOutputs.clear ();
2325 corrected.clear ();
26+ outFn.clear ();
2427 flush_out (false );
2528
2629 BLIF_file bfile (string{cfn});
@@ -127,13 +130,12 @@ bool do_check_blif(CStr cfn,
127130 std::filesystem::path base_path = full_path.filename ();
128131 std::string base = base_path.string ();
129132 // lputs9();
130- string outFn;
131133 if (cleanup) {
132134 flush_out (true );
133135 lprintf (" [PLANNER BLIF-CLEANER] : replacing file '%s' ...\n " , cfn);
134136 flush_out (true );
135137 // cannot write to the currently mem-mapped file,
136- // write to a temproray and rename later.
138+ // write to a temporary and rename later.
137139 outFn = str::concat (full_path.lexically_normal ().string (),
138140 " +BLIF_CLEANER.tmp_" , std::to_string (get_PID ()));
139141 } else {
@@ -144,9 +146,9 @@ bool do_check_blif(CStr cfn,
144146
145147 if (wr_ok.empty ()) {
146148 lprintf (" ---!! FAILED writeBlif to '%s'\n " , outFn.c_str ());
147- if (cleanup) {
149+ if (cleanup)
148150 lprintf (" [PLANNER BLIF-CLEANER] : FAILED\n " );
149- }
151+ outFn. clear ();
150152 } else {
151153 lprintf (" +++++ WRITTEN '%s'\n " , wr_ok.c_str ());
152154 if (cleanup) {
@@ -180,23 +182,88 @@ bool do_check_blif(CStr cfn,
180182
181183// 'corrected' : (lnum, removed_net)
182184bool do_cleanup_blif (CStr cfn, vector<uspair>& corrected) {
185+ using namespace fio ;
186+ using namespace std ;
183187 assert (cfn);
184188 corrected.clear ();
189+ uint16_t tr = ltrace ();
185190
186191 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 );
188195
189196 size_t cor_sz = corrected.size ();
190197 if (status and cor_sz) {
198+
191199 flush_out (true );
192200 lprintf (" [PLANNER BLIF-CLEANER] : corrected netlist '%s'\n " , cfn);
193201 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);
197212 }
198- lprintf (" -- removed dangling nets (%zu).\n " , cor_sz);
199213 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+ }
200267 }
201268
202269 return status;
@@ -270,7 +337,8 @@ bool do_check(const rsOpts& opts, bool blif_vs_csv) {
270337 if (blif_vs_csv) {
271338 vector<string> badInp, badOut;
272339 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 );
274342 } else {
275343 status = do_check_csv (cfn);
276344 }
0 commit comments