diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2021-12-16 09:24:06 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2021-12-16 09:24:06 +0200 |
commit | 5c3744e914d72916d30c9b4cb4804227d6aae736 (patch) | |
tree | 356f4a6a14f5c33772b9094416f37aa39cbd26ba | |
parent | fb9f2206a3a9b860480d2e9967561b47c1e86351 (diff) |
Pass context to (meta-)operation hooks
-rw-r--r-- | build2/b.cxx | 127 | ||||
-rw-r--r-- | libbuild2/config/operation.cxx | 8 | ||||
-rw-r--r-- | libbuild2/dist/operation.cxx | 18 | ||||
-rw-r--r-- | libbuild2/install/operation.cxx | 9 | ||||
-rw-r--r-- | libbuild2/module.cxx | 10 | ||||
-rw-r--r-- | libbuild2/operation.cxx | 2 | ||||
-rw-r--r-- | libbuild2/operation.hxx | 15 | ||||
-rw-r--r-- | libbuild2/test/operation.cxx | 9 |
8 files changed, 104 insertions, 94 deletions
diff --git a/build2/b.cxx b/build2/b.cxx index 91d59a5..bb09675 100644 --- a/build2/b.cxx +++ b/build2/b.cxx @@ -816,18 +816,18 @@ main (int argc, char* argv[]) // the global scope being setup. We reset it for every meta-operation (see // below). // - unique_ptr<context> ctx; - auto new_context = [&ctx, &sched, &mutexes, &fcache, &cmd_vars] + unique_ptr<context> pctx; + auto new_context = [&pctx, &sched, &mutexes, &fcache, &cmd_vars] { - ctx = nullptr; // Free first. - ctx.reset (new context (sched, - mutexes, - fcache, - ops.match_only (), - ops.no_external_modules (), - ops.dry_run (), - !ops.serial_stop () /* keep_going */, - cmd_vars)); + pctx = nullptr; // Free first. + pctx.reset (new context (sched, + mutexes, + fcache, + ops.match_only (), + ops.no_external_modules (), + ops.dry_run (), + !ops.serial_stop () /* keep_going */, + cmd_vars)); }; new_context (); @@ -840,7 +840,7 @@ main (int argc, char* argv[]) istringstream is (args); is.exceptions (istringstream::failbit | istringstream::badbit); - parser p (*ctx); + parser p (*pctx); bspec = p.parse_buildspec (is, path_name ("<buildspec>")); } catch (const io_error&) @@ -918,6 +918,8 @@ main (int argc, char* argv[]) dirty = false; } + context& ctx (*pctx); + const path p ("<buildspec>"); const location l (p, 0, 0); //@@ TODO @@ -932,25 +934,25 @@ main (int argc, char* argv[]) values& mparams (lifted == nullptr ? mit->params : lifted->params); string mname (lifted == nullptr ? mit->name : lifted->name); - ctx->current_mname = mname; // Set early. + ctx.current_mname = mname; // Set early. if (!mname.empty ()) { - if (meta_operation_id m = ctx->meta_operation_table.find (mname)) + if (meta_operation_id m = ctx.meta_operation_table.find (mname)) { // Can modify params, opspec, change meta-operation name. // - if (auto f = ctx->meta_operation_table[m].process) - mname = ctx->current_mname = f ( - *ctx, mparams, opspecs, lifted != nullptr, l); + if (auto f = ctx.meta_operation_table[m].process) + mname = ctx.current_mname = f ( + ctx, mparams, opspecs, lifted != nullptr, l); } } // Expose early so can be used during bootstrap (with the same // limitations as for pre-processing). // - scope& gs (ctx->global_scope.rw ()); - gs.assign (ctx->var_build_meta_operation) = mname; + scope& gs (ctx.global_scope.rw ()); + gs.assign (ctx.var_build_meta_operation) = mname; for (auto oit (opspecs.begin ()); oit != opspecs.end (); ++oit) { @@ -961,7 +963,7 @@ main (int argc, char* argv[]) const values& oparams (lifted == nullptr ? os.params : values ()); const string& oname (lifted == nullptr ? os.name : empty_string); - ctx->current_oname = oname; // Set early. + ctx.current_oname = oname; // Set early. if (lifted != nullptr) lifted = nullptr; // Clear for the next iteration. @@ -985,7 +987,7 @@ main (int argc, char* argv[]) &oname, &mname, &os, &mit, &lifted, &skip, &l, &trace] () { - meta_operation_id m (ctx->meta_operation_table.find (oname)); + meta_operation_id m (ctx.meta_operation_table.find (oname)); if (m != 0) { @@ -1144,7 +1146,7 @@ main (int argc, char* argv[]) // Handle a forwarded configuration. Note that if we've changed // out_root then we also have to remap out_base. // - out_root = bootstrap_fwd (*ctx, src_root, altn); + out_root = bootstrap_fwd (ctx, src_root, altn); if (src_root != out_root) { out_base = out_root / out_base.leaf (src_root); @@ -1189,7 +1191,7 @@ main (int argc, char* argv[]) // use to the bootstrap files (other than src-root.build, which, // BTW, doesn't need to exist if src_root == out_root). // - scope& rs (*create_root (*ctx, out_root, src_root)->second.front ()); + scope& rs (*create_root (ctx, out_root, src_root)->second.front ()); bool bstrapped (bootstrapped (rs)); @@ -1225,8 +1227,8 @@ main (int argc, char* argv[]) << (forwarded ? "forwarded " : "specified ") << src_root; - ctx->new_src_root = src_root; - ctx->old_src_root = move (p); + ctx.new_src_root = src_root; + ctx.old_src_root = move (p); p = src_root; } } @@ -1269,7 +1271,7 @@ main (int argc, char* argv[]) // command line and import). // if (forwarded) - rs.assign (ctx->var_forwarded) = true; + rs.assign (ctx.var_forwarded) = true; // Sync local variable that are used below with actual values. // @@ -1321,8 +1323,8 @@ main (int argc, char* argv[]) // all be known. We store the combined action id in uint8_t; // see <operation> for details. // - assert (ctx->operation_table.size () <= 128); - assert (ctx->meta_operation_table.size () <= 128); + assert (ctx.operation_table.size () <= 128); + assert (ctx.meta_operation_table.size () <= 128); // Since we now know all the names of meta-operations and // operations, "lift" names that we assumed (from buildspec syntax) @@ -1339,7 +1341,7 @@ main (int argc, char* argv[]) if (!mname.empty ()) { - m = ctx->meta_operation_table.find (mname); + m = ctx.meta_operation_table.find (mname); if (m == 0) fail (l) << "unknown meta-operation " << mname; @@ -1347,7 +1349,7 @@ main (int argc, char* argv[]) if (!oname.empty ()) { - o = ctx->operation_table.find (oname); + o = ctx.operation_table.find (oname); if (o == 0) fail (l) << "unknown operation " << oname; @@ -1370,7 +1372,7 @@ main (int argc, char* argv[]) if (mif == nullptr) fail (l) << "target " << tn << " does not support meta-" - << "operation " << ctx->meta_operation_table[m].name; + << "operation " << ctx.meta_operation_table[m].name; } // // Otherwise, check that all the targets in a meta-operation @@ -1383,7 +1385,7 @@ main (int argc, char* argv[]) if (mi == nullptr) fail (l) << "target " << tn << " does not support meta-" - << "operation " << ctx->meta_operation_table[mid].name; + << "operation " << ctx.meta_operation_table[mid].name; if (mi != mif) fail (l) << "different implementations of meta-operation " @@ -1406,12 +1408,12 @@ main (int argc, char* argv[]) << ", id " << static_cast<uint16_t> (mid);}); if (mif->meta_operation_pre != nullptr) - mif->meta_operation_pre (mparams, l); + mif->meta_operation_pre (ctx, mparams, l); else if (!mparams.empty ()) fail (l) << "unexpected parameters for meta-operation " << mif->name; - ctx->current_meta_operation (*mif); + ctx.current_meta_operation (*mif); dirty = true; } @@ -1427,7 +1429,7 @@ main (int argc, char* argv[]) if (r == nullptr) fail (l) << "target " << tn << " does not support " - << "operation " << ctx->operation_table[o]; + << "operation " << ctx.operation_table[o]; return r; }; @@ -1445,7 +1447,7 @@ main (int argc, char* argv[]) // Allow the meta-operation to translate the operation. // if (mif->operation_pre != nullptr) - oid = mif->operation_pre (mparams, oif->id); + oid = mif->operation_pre (ctx, mparams, oif->id); else // Otherwise translate default to update. oid = (oif->id == default_id ? update_id : oif->id); @@ -1468,9 +1470,9 @@ main (int argc, char* argv[]) // Handle pre/post operations. // - if (oif->pre != nullptr) + if (auto po = oif->pre_operation) { - if ((orig_pre_oid = oif->pre (oparams, mid, l)) != 0) + if ((orig_pre_oid = po (ctx, oparams, mid, l)) != 0) { assert (orig_pre_oid != default_id); pre_oif = lookup (orig_pre_oid); @@ -1478,12 +1480,11 @@ main (int argc, char* argv[]) } } else if (!oparams.empty ()) - fail (l) << "unexpected parameters for operation " - << oif->name; + fail (l) << "unexpected parameters for operation " << oif->name; - if (oif->post != nullptr) + if (auto po = oif->post_operation) { - if ((orig_post_oid = oif->post (oparams, mid)) != 0) + if ((orig_post_oid = po (ctx, oparams, mid)) != 0) { assert (orig_post_oid != default_id); post_oif = lookup (orig_post_oid); @@ -1504,7 +1505,7 @@ main (int argc, char* argv[]) if (r == nullptr) fail (l) << "target " << tn << " does not support " - << "operation " << ctx->operation_table[o]; + << "operation " << ctx.operation_table[o]; if (r != i) fail (l) << "different implementations of operation " @@ -1590,9 +1591,9 @@ main (int argc, char* argv[]) // So we split it into two passes. // { - auto& sm (ctx->scopes.rw ()); + auto& sm (ctx.scopes.rw ()); - for (const variable_override& o: ctx->var_overrides) + for (const variable_override& o: ctx.var_overrides) { if (o.ovr.visibility != variable_visibility::global) continue; @@ -1614,7 +1615,7 @@ main (int argc, char* argv[]) v = o.val; } - for (const variable_override& o: ctx->var_overrides) + for (const variable_override& o: ctx.var_overrides) { // Ours is either project (%foo) or scope (/foo). // @@ -1669,7 +1670,7 @@ main (int argc, char* argv[]) // building before we know how to for all the targets in this // operation batch. // - const scope& bs (ctx->scopes.find_out (ts.out_base)); + const scope& bs (ctx.scopes.find_out (ts.out_base)); // Find the target type and extract the extension. // @@ -1711,7 +1712,7 @@ main (int argc, char* argv[]) } // target if (dump_load) - dump (*ctx); + dump (ctx); // Finally, match the rules and perform the operation. // @@ -1721,9 +1722,9 @@ main (int argc, char* argv[]) << ", id " << static_cast<uint16_t> (pre_oid);}); if (mif->operation_pre != nullptr) - mif->operation_pre (mparams, pre_oid); // Cannot be translated. + mif->operation_pre (ctx, mparams, pre_oid); // Can't be translated. - ctx->current_operation (*pre_oif, oif); + ctx.current_operation (*pre_oif, oif); action a (mid, pre_oid, oid); @@ -1735,14 +1736,14 @@ main (int argc, char* argv[]) mif->match (mparams, a, tgs, diag, true /* progress */); if (dump_match) - dump (*ctx, a); + dump (ctx, a); - if (mif->execute != nullptr && !ctx->match_only) + if (mif->execute != nullptr && !ctx.match_only) mif->execute (mparams, a, tgs, diag, true /* progress */); } if (mif->operation_post != nullptr) - mif->operation_post (mparams, pre_oid); + mif->operation_post (ctx, mparams, pre_oid); l5 ([&]{trace << "end pre-operation batch " << pre_oif->name << ", id " << static_cast<uint16_t> (pre_oid);}); @@ -1750,7 +1751,7 @@ main (int argc, char* argv[]) tgs.reset (); } - ctx->current_operation (*oif, outer_oif); + ctx.current_operation (*oif, outer_oif); action a (mid, oid, oif->outer_id); @@ -1762,9 +1763,9 @@ main (int argc, char* argv[]) mif->match (mparams, a, tgs, diag, true /* progress */); if (dump_match) - dump (*ctx, a); + dump (ctx, a); - if (mif->execute != nullptr && !ctx->match_only) + if (mif->execute != nullptr && !ctx.match_only) mif->execute (mparams, a, tgs, diag, true /* progress */); } @@ -1776,9 +1777,9 @@ main (int argc, char* argv[]) << ", id " << static_cast<uint16_t> (post_oid);}); if (mif->operation_pre != nullptr) - mif->operation_pre (mparams, post_oid); // Cannot be translated. + mif->operation_pre (ctx, mparams, post_oid); // Can't be translated. - ctx->current_operation (*post_oif, oif); + ctx.current_operation (*post_oif, oif); action a (mid, post_oid, oid); @@ -1790,21 +1791,21 @@ main (int argc, char* argv[]) mif->match (mparams, a, tgs, diag, true /* progress */); if (dump_match) - dump (*ctx, a); + dump (ctx, a); - if (mif->execute != nullptr && !ctx->match_only) + if (mif->execute != nullptr && !ctx.match_only) mif->execute (mparams, a, tgs, diag, true /* progress */); } if (mif->operation_post != nullptr) - mif->operation_post (mparams, post_oid); + mif->operation_post (ctx, mparams, post_oid); l5 ([&]{trace << "end post-operation batch " << post_oif->name << ", id " << static_cast<uint16_t> (post_oid);}); } if (mif->operation_post != nullptr) - mif->operation_post (mparams, oid); + mif->operation_post (ctx, mparams, oid); l5 ([&]{trace << "end operation batch " << oif->name << ", id " << static_cast<uint16_t> (oid);}); @@ -1813,7 +1814,7 @@ main (int argc, char* argv[]) if (mid != 0) { if (mif->meta_operation_post != nullptr) - mif->meta_operation_post (mparams); + mif->meta_operation_post (ctx, mparams); l5 ([&]{trace << "end meta-operation batch " << mif->name << ", id " << static_cast<uint16_t> (mid);}); diff --git a/libbuild2/config/operation.cxx b/libbuild2/config/operation.cxx index 5883d8c..8ceb4d4 100644 --- a/libbuild2/config/operation.cxx +++ b/libbuild2/config/operation.cxx @@ -806,7 +806,7 @@ namespace build2 operation_id (*pre) (const values&, meta_operation_id, const location&); static operation_id - configure_operation_pre (const values&, operation_id o) + configure_operation_pre (context&, const values&, operation_id o) { // Don't translate default to update. In our case unspecified // means configure everything. @@ -845,7 +845,7 @@ namespace build2 } static void - configure_pre (const values& params, const location& l) + configure_pre (context&, const values& params, const location& l) { forward (params, "configure", l); // Validate. } @@ -1152,13 +1152,13 @@ namespace build2 } static void - disfigure_pre (const values& params, const location& l) + disfigure_pre (context&, const values& params, const location& l) { forward (params, "disfigure", l); // Validate. } static operation_id - disfigure_operation_pre (const values&, operation_id o) + disfigure_operation_pre (context&, const values&, operation_id o) { // Don't translate default to update. In our case unspecified // means disfigure everything. diff --git a/libbuild2/dist/operation.cxx b/libbuild2/dist/operation.cxx index a799cfc..f3db8ad 100644 --- a/libbuild2/dist/operation.cxx +++ b/libbuild2/dist/operation.cxx @@ -56,7 +56,7 @@ namespace build2 const path& arc, const dir_path& dir, const string& ext); static operation_id - dist_operation_pre (const values&, operation_id o) + dist_operation_pre (context&, const values&, operation_id o) { if (o != default_id) fail << "explicit operation specified for dist meta-operation"; @@ -267,9 +267,9 @@ namespace build2 // Use standard (perform) match. // - if (oif->pre != nullptr) + if (auto pp = oif->pre_operation) { - if (operation_id pid = oif->pre (params, dist_id, loc)) + if (operation_id pid = pp (ctx, params, dist_id, loc)) { const operation_info* poif (ops[pid]); ctx.current_operation (*poif, oif, false /* diag_noise */); @@ -286,9 +286,9 @@ namespace build2 1 /* diag (failures only) */, false /* progress */); - if (oif->post != nullptr) + if (auto po = oif->post_operation) { - if (operation_id pid = oif->post (params, dist_id)) + if (operation_id pid = po (ctx, params, dist_id)) { const operation_info* poif (ops[pid]); ctx.current_operation (*poif, oif, false /* diag_noise */); @@ -411,7 +411,7 @@ namespace build2 // { if (mo_perform.meta_operation_pre != nullptr) - mo_perform.meta_operation_pre (params, loc); + mo_perform.meta_operation_pre (ctx, params, loc); // This is a hack since according to the rules we need to completely // reset the state. We could have done that (i.e., saved target @@ -427,7 +427,7 @@ namespace build2 ctx.current_on = on + 1; if (mo_perform.operation_pre != nullptr) - mo_perform.operation_pre (params, update_id); + mo_perform.operation_pre (ctx, params, update_id); ctx.current_operation (op_update, nullptr, false /* diag_noise */); @@ -442,10 +442,10 @@ namespace build2 prog /* progress */); if (mo_perform.operation_post != nullptr) - mo_perform.operation_post (params, update_id); + mo_perform.operation_post (ctx, params, update_id); if (mo_perform.meta_operation_post != nullptr) - mo_perform.meta_operation_post (params); + mo_perform.meta_operation_post (ctx, params); } } else diff --git a/libbuild2/install/operation.cxx b/libbuild2/install/operation.cxx index 54d5b9a..52e8c94 100644 --- a/libbuild2/install/operation.cxx +++ b/libbuild2/install/operation.cxx @@ -13,7 +13,10 @@ namespace build2 namespace install { static operation_id - install_pre (const values& params, meta_operation_id mo, const location& l) + install_pre (context&, + const values& params, + meta_operation_id mo, + const location& l) { if (!params.empty ()) fail (l) << "unexpected parameters for operation install"; @@ -82,8 +85,8 @@ namespace build2 op_update.name_done, op_update.mode, op_update.concurrency, - op_update.pre, - op_update.post, + op_update.pre_operation, + op_update.post_operation, op_update.adhoc_match, op_update.adhoc_apply }; diff --git a/libbuild2/module.cxx b/libbuild2/module.cxx index 9756860..3f4f1d0 100644 --- a/libbuild2/module.cxx +++ b/libbuild2/module.cxx @@ -91,8 +91,8 @@ namespace build2 // We use the same context for building any nested modules that might be // required while building modules. // - ctx.module_context = ctx.module_context_storage->get (); - ctx.module_context->module_context = ctx.module_context; + context& mctx (*(ctx.module_context = ctx.module_context_storage->get ())); + mctx.module_context = &mctx; // Setup the context to perform update. In a sense we have a long-running // perform meta-operation batch (indefinite, in fact, since we never call @@ -104,12 +104,12 @@ namespace build2 // recipes) we will see the old state. // if (mo_perform.meta_operation_pre != nullptr) - mo_perform.meta_operation_pre ({} /* parameters */, loc); + mo_perform.meta_operation_pre (mctx, {} /* parameters */, loc); - ctx.module_context->current_meta_operation (mo_perform); + mctx.current_meta_operation (mo_perform); if (mo_perform.operation_pre != nullptr) - mo_perform.operation_pre ({} /* parameters */, update_id); + mo_perform.operation_pre (mctx, {} /* parameters */, update_id); } // Note: also used by ad hoc recipes thus not static. diff --git a/libbuild2/operation.cxx b/libbuild2/operation.cxx index f1fc83c..8ef1a13 100644 --- a/libbuild2/operation.cxx +++ b/libbuild2/operation.cxx @@ -483,7 +483,7 @@ namespace build2 // info // static operation_id - info_operation_pre (const values&, operation_id o) + info_operation_pre (context&, const values&, operation_id o) { if (o != default_id) fail << "explicit operation specified for meta-operation info"; diff --git a/libbuild2/operation.hxx b/libbuild2/operation.hxx index d80a01c..2f88e88 100644 --- a/libbuild2/operation.hxx +++ b/libbuild2/operation.hxx @@ -82,8 +82,8 @@ namespace build2 // then default_id is used. If, however, operation_pre() is NULL, // then default_id is translated to update_id. // - void (*meta_operation_pre) (const values&, const location&); - operation_id (*operation_pre) (const values&, operation_id); + void (*meta_operation_pre) (context&, const values&, const location&); + operation_id (*operation_pre) (context&, const values&, operation_id); // Meta-operation-specific logic to load the buildfile, search and match // the targets, and execute the action on the targets. @@ -121,8 +121,8 @@ namespace build2 // End of operation and meta-operation batches. // - void (*operation_post) (const values&, operation_id); - void (*meta_operation_post) (const values&); + void (*operation_post) (context&, const values&, operation_id); + void (*meta_operation_post) (context&, const values&); // Optional prerequisite exclusion override callback. See include() for // details. Note that it's not called for include_type::normal; @@ -225,8 +225,11 @@ namespace build2 // as pre/post operations for this operation. Can be NULL if unused. // The returned operation_id shall not be default_id. // - operation_id (*pre) (const values&, meta_operation_id, const location&); - operation_id (*post) (const values&, meta_operation_id); + operation_id (*pre_operation) ( + context&, const values&, meta_operation_id, const location&); + + operation_id (*post_operation) ( + context&, const values&, meta_operation_id); // Operation-specific ad hoc rule callbacks. Essentially, if not NULL, // then every ad hoc rule match and apply call for this operation is diff --git a/libbuild2/test/operation.cxx b/libbuild2/test/operation.cxx index 0a65bed..841abb5 100644 --- a/libbuild2/test/operation.cxx +++ b/libbuild2/test/operation.cxx @@ -17,7 +17,10 @@ namespace build2 namespace test { static operation_id - test_pre (const values& params, meta_operation_id mo, const location& l) + test_pre (context&, + const values& params, + meta_operation_id mo, + const location& l) { if (!params.empty ()) fail (l) << "unexpected parameters for operation test"; @@ -85,8 +88,8 @@ namespace build2 op_update.name_done, op_update.mode, op_update.concurrency, - op_update.pre, - op_update.post, + op_update.pre_operation, + op_update.post_operation, op_update.adhoc_match, op_update.adhoc_apply }; |