aboutsummaryrefslogtreecommitdiff
path: root/build2/cc/init.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-11-30 14:50:05 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-11-30 14:50:05 +0200
commit11dce6c2fa7dc39cd9e40c0fedda3280aa2757ad (patch)
tree7ae301e2e7df6f2679faf9d78622d58ae99c7fd2 /build2/cc/init.cxx
parent879b5f52cb86f24352f4ed245fcce5f1ab885f97 (diff)
Implement module sidebuilds cleanup using scope operation callbacks
Diffstat (limited to 'build2/cc/init.cxx')
-rw-r--r--build2/cc/init.cxx46
1 files changed, 46 insertions, 0 deletions
diff --git a/build2/cc/init.cxx b/build2/cc/init.cxx
index 87a7b12..3108d6d 100644
--- a/build2/cc/init.cxx
+++ b/build2/cc/init.cxx
@@ -4,13 +4,16 @@
#include <build2/cc/init.hxx>
+#include <build2/file.hxx>
#include <build2/scope.hxx>
#include <build2/context.hxx>
+#include <build2/filesystem.hxx>
#include <build2/diagnostics.hxx>
#include <build2/config/utility.hxx>
#include <build2/cc/target.hxx>
+#include <build2/cc/utility.hxx>
using namespace std;
using namespace butl;
@@ -19,6 +22,39 @@ namespace build2
{
namespace cc
{
+ // Scope operation callback that cleans up module sidebuilds.
+ //
+ static target_state
+ clean_module_sidebuilds (action, const scope& rs, const dir&)
+ {
+ dir_path d (rs.out_path () / modules_sidebuild_dir);
+
+ if (exists (d))
+ {
+ if (build2::rmdir_r (d))
+ {
+ // Clean up cc/ if it became empty.
+ //
+ d = rs.out_path () / module_dir;
+ if (empty (d))
+ {
+ rmdir (d);
+
+ // And build/ if it also became empty (e.g., in case of a build
+ // with a transient configuration).
+ //
+ d = rs.out_path () / build_dir;
+ if (empty (d))
+ rmdir (d);
+ }
+
+ return target_state::changed;
+ }
+ }
+
+ return target_state::unchanged;
+ }
+
bool
core_vars_init (scope& rs,
scope&,
@@ -88,6 +124,16 @@ namespace build2
v.insert<bool> ("config.cc.reprocess", true);
v.insert<bool> ("cc.reprocess");
+ // Register scope operation callback.
+ //
+ // It feels natural to do clean up sidebuilds as a post operation but
+ // that prevents the (otherwise-empty) out root directory to be cleaned
+ // up (via the standard fsdir{} chain).
+ //
+ rs.operation_callbacks.emplace (
+ perform_clean_id,
+ scope::operation_callback {&clean_module_sidebuilds, nullptr /*post*/});
+
return true;
}