From 11dce6c2fa7dc39cd9e40c0fedda3280aa2757ad Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 30 Nov 2017 14:50:05 +0200 Subject: Implement module sidebuilds cleanup using scope operation callbacks --- build2/cc/init.cxx | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'build2/cc/init.cxx') 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 +#include #include #include +#include #include #include #include +#include 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 ("config.cc.reprocess", true); v.insert ("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; } -- cgit v1.1