From 01d848149c22a69a62eada5fedc2406c54d95ba8 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 5 Apr 2019 09:41:18 +0200 Subject: Support for --dry-run|-n mode, perform update part --- build2/cc/compile-rule.cxx | 155 +++++++++++++---------- build2/cc/link-rule.cxx | 278 ++++++++++++++++++++++------------------- build2/cc/link-rule.hxx | 2 +- build2/cc/pkgconfig.cxx | 10 +- build2/cc/windows-manifest.cxx | 39 +++--- build2/cc/windows-rpath.cxx | 67 ++++++---- 6 files changed, 315 insertions(+), 236 deletions(-) (limited to 'build2/cc') diff --git a/build2/cc/compile-rule.cxx b/build2/cc/compile-rule.cxx index 3769b32..3050a37 100644 --- a/build2/cc/compile-rule.cxx +++ b/build2/cc/compile-rule.cxx @@ -995,7 +995,15 @@ namespace build2 // We do need to update the database timestamp, however. Failed that, // we will keep re-validating the cached data over and over again. // - if (u && dd.reading ()) + // @@ DRYRUN: note that for dry-run we would keep re-touching the + // database on every run (because u is true). So for now we suppress + // it (the file will be re-validated on the real run anyway). It feels + // like support for reusing the (partially) preprocessed output (see + // note below) should help solve this properly (i.e., we don't want + // to keep re-validating the file on every subsequent dry-run as well + // on the real run). + // + if (u && dd.reading () && !dry_run) dd.touch = true; dd.close (); @@ -4642,58 +4650,73 @@ namespace build2 if (verb >= 3) print_process (args); - try + // @@ DRYRUN: Currently we discard the (partially) preprocessed file on + // dry-run which is a waste. Even if we keep the file around (like we do + // for the error case; see above), we currently have no support for + // re-using the previously preprocessed output. However, everything + // points towards us needing this in the near future since with modules + // we may be out of date but not needing to re-preprocess the + // translation unit (i.e., one of the imported module's has BMIs + // changed). + // + if (!dry_run) { - // VC cl.exe sends diagnostics to stdout. It also prints the file name - // being compiled as the first line. So for cl.exe we redirect stdout - // to a pipe, filter that noise out, and send the rest to stderr. - // - // For other compilers redirect stdout to stderr, in case any of them - // tries to pull off something similar. For sane compilers this should - // be harmless. - // - bool filter (ctype == compiler_type::msvc); + try + { + // VC cl.exe sends diagnostics to stdout. It also prints the file + // name being compiled as the first line. So for cl.exe we redirect + // stdout to a pipe, filter that noise out, and send the rest to + // stderr. + // + // For other compilers redirect stdout to stderr, in case any of + // them tries to pull off something similar. For sane compilers this + // should be harmless. + // + bool filter (ctype == compiler_type::msvc); - process pr (cpath, - args.data (), - 0, (filter ? -1 : 2), 2, - nullptr, // CWD - env.empty () ? nullptr : env.data ()); + process pr (cpath, + args.data (), + 0, (filter ? -1 : 2), 2, + nullptr, // CWD + env.empty () ? nullptr : env.data ()); - if (filter) - { - try + if (filter) { - ifdstream is ( - move (pr.in_ofd), fdstream_mode::text, ifdstream::badbit); + try + { + ifdstream is ( + move (pr.in_ofd), fdstream_mode::text, ifdstream::badbit); - msvc_filter_cl (is, *sp); + msvc_filter_cl (is, *sp); - // If anything remains in the stream, send it all to stderr. Note - // that the eof check is important: if the stream is at eof, this - // and all subsequent writes to the diagnostics stream will fail - // (and you won't see a thing). - // - if (is.peek () != ifdstream::traits_type::eof ()) - diag_stream_lock () << is.rdbuf (); + // If anything remains in the stream, send it all to stderr. + // Note that the eof check is important: if the stream is at + // eof, this and all subsequent writes to the diagnostics stream + // will fail (and you won't see a thing). + // + if (is.peek () != ifdstream::traits_type::eof ()) + diag_stream_lock () << is.rdbuf (); - is.close (); + is.close (); + } + catch (const io_error&) {} // Assume exits with error. } - catch (const io_error&) {} // Assume exits with error. - } - run_finish (args, pr); - } - catch (const process_error& e) - { - error << "unable to execute " << args[0] << ": " << e; + run_finish (args, pr); + } + catch (const process_error& e) + { + error << "unable to execute " << args[0] << ": " << e; - if (e.child) - exit (1); + if (e.child) + exit (1); - throw failed (); + throw failed (); + } } + // Remove preprocessed file (see above). + // if (pact && verb >= 3) md.psrc.active = true; @@ -4702,11 +4725,6 @@ namespace build2 // if (mod && ctype == compiler_type::clang) { - // Remove the target file if this fails. If we don't do that, we will - // end up with a broken build that is up-to-date. - // - auto_rmfile rm (relm); - // Adjust the command line. First discard everything after -o then // build the new "tail". // @@ -4720,35 +4738,46 @@ namespace build2 if (verb >= 2) print_process (args); - try + if (!dry_run) { - process pr (cpath, - args.data (), - 0, 2, 2, - nullptr, // CWD - env.empty () ? nullptr : env.data ()); + // Remove the target file if this fails. If we don't do that, we + // will end up with a broken build that is up-to-date. + // + auto_rmfile rm (relm); - run_finish (args, pr); - } - catch (const process_error& e) - { - error << "unable to execute " << args[0] << ": " << e; + try + { + process pr (cpath, + args.data (), + 0, 2, 2, + nullptr, // CWD + env.empty () ? nullptr : env.data ()); - if (e.child) - exit (1); + run_finish (args, pr); + } + catch (const process_error& e) + { + error << "unable to execute " << args[0] << ": " << e; - throw failed (); - } + if (e.child) + exit (1); + + throw failed (); + } - rm.cancel (); + rm.cancel (); + } } timestamp now (system_clock::now ()); - depdb::check_mtime (start, md.dd, tp, now); + + if (!dry_run) + depdb::check_mtime (start, md.dd, tp, now); // Should we go to the filesystem and get the new mtime? We know the // file has been modified, so instead just use the current clock time. - // It has the advantage of having the subseconds precision. + // It has the advantage of having the subseconds precision. Plus, in + // case of dry-run, the file won't be modified. // t.mtime (now); return target_state::changed; diff --git a/build2/cc/link-rule.cxx b/build2/cc/link-rule.cxx index 8b4d3ee..7d5eb83 100644 --- a/build2/cc/link-rule.cxx +++ b/build2/cc/link-rule.cxx @@ -1687,9 +1687,7 @@ namespace build2 auto p (windows_manifest (t, rpath_timestamp != timestamp_nonexistent)); path& mf (p.first); - bool mf_cf (p.second); // Changed flag (timestamp resolution). - - timestamp mf_mt (mtime (mf)); + timestamp mf_mt (p.second); if (tsys == "mingw32") { @@ -1699,7 +1697,7 @@ namespace build2 // manifest = mf + ".o"; - if (mf_mt > mtime (manifest) || mf_cf) + if (mf_mt == timestamp_nonexistent || mf_mt > mtime (manifest)) { path of (relative (manifest)); @@ -1717,53 +1715,59 @@ namespace build2 if (verb >= 3) print_process (args); - try + if (!dry_run) { - process pr (rc, args, -1); + auto_rmfile rm (of); try { - ofdstream os (move (pr.out_fd)); + process pr (rc, args, -1); - // 1 is resource ID, 24 is RT_MANIFEST. We also need to escape - // Windows path backslashes. - // - os << "1 24 \""; - - const string& s (mf.string ()); - for (size_t i (0), j;; i = j + 1) + try { - j = s.find ('\\', i); - os.write (s.c_str () + i, - (j == string::npos ? s.size () : j) - i); + ofdstream os (move (pr.out_fd)); + + // 1 is resource ID, 24 is RT_MANIFEST. We also need to + // escape Windows path backslashes. + // + os << "1 24 \""; - if (j == string::npos) - break; + const string& s (mf.string ()); + for (size_t i (0), j;; i = j + 1) + { + j = s.find ('\\', i); + os.write (s.c_str () + i, + (j == string::npos ? s.size () : j) - i); - os.write ("\\\\", 2); - } + if (j == string::npos) + break; + + os.write ("\\\\", 2); + } - os << "\"" << endl; + os << "\"" << endl; - os.close (); + os.close (); + rm.cancel (); + } + catch (const io_error& e) + { + if (pr.wait ()) // Ignore if child failed. + fail << "unable to pipe resource file to " << args[0] + << ": " << e; + } + + run_finish (args, pr); } - catch (const io_error& e) + catch (const process_error& e) { - if (pr.wait ()) // Ignore if child failed. - fail << "unable to pipe resource file to " << args[0] - << ": " << e; - } + error << "unable to execute " << args[0] << ": " << e; - run_finish (args, pr); - } - catch (const process_error& e) - { - error << "unable to execute " << args[0] << ": " << e; + if (e.child) + exit (1); - if (e.child) - exit (1); - - throw failed (); + throw failed (); + } } update = true; // Manifest changed, force update. @@ -1773,7 +1777,7 @@ namespace build2 { manifest = move (mf); // Save for link.exe's /MANIFESTINPUT. - if (mf_mt > mt || mf_cf) + if (mf_mt == timestamp_nonexistent || mf_mt > mt) update = true; // Manifest changed, force update. } } @@ -2382,7 +2386,8 @@ namespace build2 args.push_back (nullptr); - // Cleanup old (versioned) libraries. + // Cleanup old (versioned) libraries. Let's do it even for dry-run to + // keep things simple. // if (lt.shared_library ()) { @@ -2437,20 +2442,13 @@ namespace build2 // We use relative paths to the object files which means we may end // up with different ones depending on CWD and some implementation // treat them as different archive members. So remote the file to - // be sure. Note that we ignore errors leaving it to the achiever + // be sure. Note that we ignore errors leaving it to the archiever // to complain. // if (mt != timestamp_nonexistent) try_rmfile (relt, true); } - // Remove the target file if any of the subsequent (after the linker) - // actions fail or if the linker fails but does not clean up its mess - // (like link.exe). If we don't do that, then we will end up with a - // broken build that is up-to-date. - // - auto_rmfile rm (relt); - if (verb == 1) text << (lt.static_library () ? "ar " : "ld ") << t; else if (verb == 2) @@ -2562,84 +2560,96 @@ namespace build2 if (verb > 2) print_process (args); - try - { - // VC tools (both lib.exe and link.exe) send diagnostics to stdout. - // Also, link.exe likes to print various gratuitous messages. So for - // link.exe we redirect stdout to a pipe, filter that noise out, and - // send the rest to stderr. - // - // For lib.exe (and any other insane compiler that may try to pull off - // something like this) we are going to redirect stdout to stderr. For - // sane compilers this should be harmless. - // - bool filter (tsys == "win32-msvc" && !lt.static_library ()); + // Remove the target file if any of the subsequent (after the linker) + // actions fail or if the linker fails but does not clean up its mess + // (like link.exe). If we don't do that, then we will end up with a + // broken build that is up-to-date. + // + auto_rmfile rm; - process pr (*ld, args.data (), 0, (filter ? -1 : 2)); + if (!dry_run) + { + rm = auto_rmfile (relt); - if (filter) + try { - try + // VC tools (both lib.exe and link.exe) send diagnostics to stdout. + // Also, link.exe likes to print various gratuitous messages. So for + // link.exe we redirect stdout to a pipe, filter that noise out, and + // send the rest to stderr. + // + // For lib.exe (and any other insane linker that may try to pull off + // something like this) we are going to redirect stdout to stderr. + // For sane compilers this should be harmless. + // + bool filter (tsys == "win32-msvc" && !lt.static_library ()); + + process pr (*ld, args.data (), 0, (filter ? -1 : 2)); + + if (filter) { - ifdstream is ( - move (pr.in_ofd), fdstream_mode::text, ifdstream::badbit); + try + { + ifdstream is ( + move (pr.in_ofd), fdstream_mode::text, ifdstream::badbit); - msvc_filter_link (is, t, ot); + msvc_filter_link (is, t, ot); - // If anything remains in the stream, send it all to stderr. Note - // that the eof check is important: if the stream is at eof, this - // and all subsequent writes to the diagnostics stream will fail - // (and you won't see a thing). - // - if (is.peek () != ifdstream::traits_type::eof ()) - diag_stream_lock () << is.rdbuf (); + // If anything remains in the stream, send it all to stderr. + // Note that the eof check is important: if the stream is at + // eof, this and all subsequent writes to the diagnostics stream + // will fail (and you won't see a thing). + // + if (is.peek () != ifdstream::traits_type::eof ()) + diag_stream_lock () << is.rdbuf (); - is.close (); + is.close (); + } + catch (const io_error&) {} // Assume exits with error. } - catch (const io_error&) {} // Assume exits with error. - } - run_finish (args, pr); - } - catch (const process_error& e) - { - error << "unable to execute " << args[0] << ": " << e; - - // In a multi-threaded program that fork()'ed but did not exec(), - // it is unwise to try to do any kind of cleanup (like unwinding - // the stack and running destructors). - // - if (e.child) + run_finish (args, pr); + } + catch (const process_error& e) { - rm.cancel (); + error << "unable to execute " << args[0] << ": " << e; + + // In a multi-threaded program that fork()'ed but did not exec(), it + // is unwise to try to do any kind of cleanup (like unwinding the + // stack and running destructors). + // + if (e.child) + { + rm.cancel (); #ifdef _WIN32 - trm.cancel (); + trm.cancel (); #endif - exit (1); - } + exit (1); + } - throw failed (); - } + throw failed (); + } - // VC link.exe creates an import library and .exp file for an executable - // if any of its object files export any symbols (think a unit test - // linking libus{}). And, no, there is no way to suppress it. Well, - // there is a way: create a .def file with an empty EXPORTS section, - // pass it to lib.exe to create a dummy .exp (and .lib), and then pass - // this empty .exp to link.exe. Wanna go this way? Didn't think so. - // Having no way to disable this, the next simplest thing seems to be - // just cleaning the mess up. - // - // Note also that if at some point we decide to support such "shared - // executables" (-rdynamic, etc), then it will probably have to be a - // different target type (exes{}?) since it will need a different set - // of object files (-fPIC so probably objs{}), etc. - // - if (lt.executable () && tsys == "win32-msvc") - { - path b (relt.base ()); - try_rmfile (b + ".lib", true /* ignore_errors */); - try_rmfile (b + ".exp", true /* ignore_errors */); + // VC link.exe creates an import library and .exp file for an + // executable if any of its object files export any symbols (think a + // unit test linking libus{}). And, no, there is no way to suppress + // it. Well, there is a way: create a .def file with an empty EXPORTS + // section, pass it to lib.exe to create a dummy .exp (and .lib), and + // then pass this empty .exp to link.exe. Wanna go this way? Didn't + // think so. Having no way to disable this, the next simplest thing + // seems to be just cleaning the mess up. + // + // Note also that if at some point we decide to support such "shared + // executables" (-rdynamic, etc), then it will probably have to be a + // different target type (exes{}?) since it will need a different set + // of object files (-fPIC so probably objs{}), etc. + // + if (lt.executable () && tsys == "win32-msvc") + { + path b (relt.base ()); + try_rmfile (b + ".lib", true /* ignore_errors */); + try_rmfile (b + ".exp", true /* ignore_errors */); + } } if (ranlib) @@ -2654,7 +2664,8 @@ namespace build2 if (verb >= 2) print_process (args); - run (rl, args); + if (!dry_run) + run (rl, args); } if (tclass == "windows") @@ -2676,6 +2687,9 @@ namespace build2 if (verb >= 3) text << "ln -sf " << f << ' ' << l; + if (dry_run) + return; + try { if (file_exists (l, false /* follow_symlinks */)) // The -f part. @@ -2701,29 +2715,35 @@ namespace build2 if (!so.empty ()) {ln (f->leaf (), so); f = &so;} if (!lk.empty ()) {ln (f->leaf (), lk);} } - - // Apple ar (from cctools) for some reason truncates fractional seconds - // when running on APFS (HFS has a second resolution so it's not an - // issue there). This can lead to object files being newer than the - // archive, which is naturally bad news. Filed as bug 49604334. - // - // Note that this block is not inside #ifdef __APPLE__ because we could - // be cross-compiling, theoretically. We also make sure we use Apple's - // ar (which is (un)recognized as 'generic') instead of, say, llvm-ar. - // - if (lt.static_library () && - tsys == "darwin" && - cast (rs["bin.ar.id"]) == "generic") + else if (lt.static_library ()) { - touch (tp, false /* create */, verb_never); + // Apple ar (from cctools) for some reason truncates fractional + // seconds when running on APFS (HFS has a second resolution so it's + // not an issue there). This can lead to object files being newer than + // the archive, which is naturally bad news. Filed as bug 49604334. + // + // Note that this block is not inside #ifdef __APPLE__ because we + // could be cross-compiling, theoretically. We also make sure we use + // Apple's ar (which is (un)recognized as 'generic') instead of, say, + // llvm-ar. + // + if (tsys == "darwin" && cast (rs["bin.ar.id"]) == "generic") + { + if (!dry_run) + touch (tp, false /* create */, verb_never); + } } - rm.cancel (); - dd.check_mtime (tp); + if (!dry_run) + { + rm.cancel (); + dd.check_mtime (tp); + } // Should we go to the filesystem and get the new mtime? We know the // file has been modified, so instead just use the current clock time. - // It has the advantage of having the subseconds precision. + // It has the advantage of having the subseconds precision. Plus, in + // case of dry-run, the file won't be modified. // t.mtime (system_clock::now ()); return target_state::changed; diff --git a/build2/cc/link-rule.hxx b/build2/cc/link-rule.hxx index b239dee..1a77aef 100644 --- a/build2/cc/link-rule.hxx +++ b/build2/cc/link-rule.hxx @@ -151,7 +151,7 @@ namespace build2 // Windows-specific (windows-manifest.cxx). // - pair + pair windows_manifest (const file&, bool rpath_assembly) const; // pkg-config's .pc file generation (pkgconfig.cxx). diff --git a/build2/cc/pkgconfig.cxx b/build2/cc/pkgconfig.cxx index eef1271..6f30dc9 100644 --- a/build2/cc/pkgconfig.cxx +++ b/build2/cc/pkgconfig.cxx @@ -1234,9 +1234,6 @@ namespace build2 auto* t (find_adhoc_member (l)); assert (t != nullptr); - const path& p (t->path ()); - auto_rmfile arm (p); - // By default we assume things go into install.{include, lib}. // using install::resolve_dir; @@ -1244,9 +1241,16 @@ namespace build2 dir_path idir (resolve_dir (l, cast (l["install.include"]))); dir_path ldir (resolve_dir (l, cast (l["install.lib"]))); + const path& p (t->path ()); + if (verb >= 2) text << "cat >" << p; + if (dry_run) + return; + + auto_rmfile arm (p); + try { ofdstream os (p); diff --git a/build2/cc/windows-manifest.cxx b/build2/cc/windows-manifest.cxx index 268e8c7..f890ad5 100644 --- a/build2/cc/windows-manifest.cxx +++ b/build2/cc/windows-manifest.cxx @@ -37,9 +37,9 @@ namespace build2 // Generate a Windows manifest and if necessary create/update the manifest // file corresponding to the exe{} target. Return the manifest file path - // as well as whether it was changed. + // and its timestamp if unchanged or timestamp_nonexistent otherwise. // - pair link_rule:: + pair link_rule:: windows_manifest (const file& t, bool rpath_assembly) const { tracer trace (x, "link_rule::windows_manifest"); @@ -100,13 +100,15 @@ namespace build2 // path mf (t.path () + ".manifest"); - if (exists (mf)) + timestamp mt (mtime (mf)); + + if (mt != timestamp_nonexistent) { try { - ifdstream ifs (mf); - if (ifs.read_text () == m) - return make_pair (move (mf), false); + ifdstream is (mf); + if (is.read_text () == m) + return make_pair (move (mf), mt); } catch (const io_error&) { @@ -117,18 +119,25 @@ namespace build2 if (verb >= 3) text << "cat >" << mf; - try - { - ofdstream ofs (mf); - ofs << m; - ofs.close (); - } - catch (const io_error& e) + if (!dry_run) { - fail << "unable to write to " << mf << ": " << e; + auto_rmfile rm (mf); + + try + { + ofdstream os (mf); + os << m; + os.close (); + rm.cancel (); + + } + catch (const io_error& e) + { + fail << "unable to write to " << mf << ": " << e; + } } - return make_pair (move (mf), true); + return make_pair (move (mf), timestamp_nonexistent); } } } diff --git a/build2/cc/windows-rpath.cxx b/build2/cc/windows-rpath.cxx index 46fe75b..0a19db2 100644 --- a/build2/cc/windows-rpath.cxx +++ b/build2/cc/windows-rpath.cxx @@ -290,23 +290,9 @@ namespace build2 mkdir (ad, 3); } - const char* pa (windows_manifest_arch (tcpu)); - - if (verb >= 3) - text << "cat >" << am; - - try + // Symlink or copy the DLLs. + // { - ofdstream ofs (am); - - ofs << "\n" - << "\n" - << " \n"; - const scope& as (*t.root_scope ().weak_scope ()); // Amalgamation. auto link = [&as, &ad] (const path& f, const path& l) @@ -328,15 +314,20 @@ namespace build2 // part of the same amalgamation. This way if the amalgamation is // moved as a whole, the links will remain valid. // - if (f.sub (as.out_path ())) - mksymlink (f.relative (ad), l); - else - mksymlink (f, l); + if (!dry_run) + { + if (f.sub (as.out_path ())) + mksymlink (f.relative (ad), l); + else + mksymlink (f, l); + } print ("ln -s"); } catch (const system_error& e) { + // Note: can never end up here on dry-run. + // Note that we are not guaranteed (here and below) that the // system_error exception is of the generic category. // @@ -378,7 +369,6 @@ namespace build2 } } } - }; for (const windows_dll& wd: dlls) @@ -398,13 +388,40 @@ namespace build2 path pp (*wd.pdb); link (pp, ad / pp.leaf ()); } - - ofs << " \n"; } + } + + if (verb >= 3) + text << "cat >" << am; + + if (dry_run) + return; + + auto_rmfile rm (am); + + try + { + ofdstream os (am); + + const char* pa (windows_manifest_arch (tcpu)); + + os << "\n" + << "\n" + << " \n"; + + + + for (const windows_dll& wd: dlls) + os << " \n"; - ofs << "\n"; + os << "\n"; - ofs.close (); + os.close (); + rm.cancel (); } catch (const io_error& e) { -- cgit v1.1