Skip to content

Commit 72e238d

Browse files
committed
refactor: the refusals of references a plan cannot answer are mcpp::build::refuse_unresolved_references, and step13_build_graph_actions is back under 400 lines
1 parent 3a8d3d5 commit 72e238d

2 files changed

Lines changed: 56 additions & 35 deletions

File tree

‎src/build/plan.cppm‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,16 @@ struct TargetFileAnswer {
699699
TargetFileAnswer resolve_target_file(const BuildPlan& plan, std::string_view name,
700700
std::string_view actingMember);
701701

702+
// The references an action's arguments made that the plan cannot answer, as
703+
// the refusal a user reads: names no link unit has (`unresolvedTargets`), names
704+
// several members define for a package that acts for none of them
705+
// (`ambiguousTargets`, from `resolve_target_file`), and `${mcpp.artifact:}`
706+
// references no requested artifact matches (`unresolvedArtifacts`).
707+
std::expected<void, std::string> refuse_unresolved_references(
708+
const BuildPlan& plan, const std::set<std::string>& unresolvedTargets,
709+
const std::map<std::string, std::vector<std::string>>& ambiguousTargets,
710+
const std::set<std::string>& unresolvedArtifacts);
711+
702712
// The objects a package contributes to an image that links it whole: its
703713
// module units, which link unconditionally, then its implementation units, in
704714
// plan order. A shared library that carries a private copy of a graph C++
@@ -3318,6 +3328,48 @@ TargetFileAnswer resolve_target_file(const BuildPlan& plan, std::string_view nam
33183328
return answer;
33193329
}
33203330

3331+
std::expected<void, std::string> refuse_unresolved_references(
3332+
const BuildPlan& plan, const std::set<std::string>& unresolvedTargets,
3333+
const std::map<std::string, std::vector<std::string>>& ambiguousTargets,
3334+
const std::set<std::string>& unresolvedArtifacts) {
3335+
if (!unresolvedTargets.empty()) {
3336+
std::string bad, known;
3337+
for (auto const& n : unresolvedTargets) bad += (bad.empty() ? "" : ", ") + n;
3338+
for (auto const& lu : plan.linkUnits)
3339+
known += (known.empty() ? "" : ", ") + lu.targetName;
3340+
return std::unexpected(std::format(
3341+
"build.mcpp action references unknown target(s) via "
3342+
"${{mcpp.target_file:...}}: {}\n"
3343+
" targets in this build: [{}]\n"
3344+
" (a target gated by required_features is absent unless those "
3345+
"features are active)",
3346+
bad, known.empty() ? std::string("none") : known));
3347+
}
3348+
3349+
for (auto const& [n, in] : ambiguousTargets)
3350+
return std::unexpected(std::format(
3351+
"build.mcpp action references ${{mcpp.target_file:{}}}, a target of "
3352+
"each of the members {}, and its package acts for none of them.\n"
3353+
" use: select one of the members, or give the targets distinct names",
3354+
n, std::format("{}", in)));
3355+
3356+
if (!unresolvedArtifacts.empty()) {
3357+
std::string bad, known;
3358+
for (auto const& n : unresolvedArtifacts) bad += (bad.empty() ? "" : ", ") + n;
3359+
for (auto const& lu : plan.linkUnits)
3360+
if (!lu.artifactOf.empty())
3361+
known += (known.empty() ? "" : ", ") + lu.artifactOf + "/" + lu.targetName;
3362+
return std::unexpected(std::format(
3363+
"build.mcpp action references unknown artifact(s) via "
3364+
"${{mcpp.artifact:<package>/<target>}}: {}\n"
3365+
" artifacts in this build: [{}]\n"
3366+
" (an artifact exists when a dependency edge requests it with "
3367+
"`artifacts = [\"<target>\"]`)",
3368+
bad, known.empty() ? std::string("none") : known));
3369+
}
3370+
return {};
3371+
}
3372+
33213373
std::vector<std::filesystem::path>
33223374
package_link_objects(const BuildPlan& plan, std::string_view packageName) {
33233375
std::vector<std::filesystem::path> objects;

‎src/build/prepare/plan.cpp‎

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,41 +1155,10 @@ static std::expected<void, std::string> step13_build_graph_actions(PrepareState&
11551155
"there is anything to stage.\n"
11561156
" use: role = \"artifact\"", ids));
11571157
}
1158-
if (!unresolvedTargets.empty()) {
1159-
std::string bad, known;
1160-
for (auto const& n : unresolvedTargets) bad += (bad.empty() ? "" : ", ") + n;
1161-
for (auto const& lu : ctx.plan.linkUnits)
1162-
known += (known.empty() ? "" : ", ") + lu.targetName;
1163-
return std::unexpected(std::format(
1164-
"build.mcpp action references unknown target(s) via "
1165-
"${{mcpp.target_file:...}}: {}\n"
1166-
" targets in this build: [{}]\n"
1167-
" (a target gated by required_features is absent unless those "
1168-
"features are active)",
1169-
bad, known.empty() ? std::string("none") : known));
1170-
}
1171-
1172-
for (auto const& [n, in] : ambiguousTargets)
1173-
return std::unexpected(std::format(
1174-
"build.mcpp action references ${{mcpp.target_file:{}}}, a target of "
1175-
"each of the members {}, and its package acts for none of them.\n"
1176-
" use: select one of the members, or give the targets distinct names",
1177-
n, std::format("{}", in)));
1178-
1179-
if (!unresolvedArtifacts.empty()) {
1180-
std::string bad, known;
1181-
for (auto const& n : unresolvedArtifacts) bad += (bad.empty() ? "" : ", ") + n;
1182-
for (auto const& lu : ctx.plan.linkUnits)
1183-
if (!lu.artifactOf.empty())
1184-
known += (known.empty() ? "" : ", ") + lu.artifactOf + "/" + lu.targetName;
1185-
return std::unexpected(std::format(
1186-
"build.mcpp action references unknown artifact(s) via "
1187-
"${{mcpp.artifact:<package>/<target>}}: {}\n"
1188-
" artifacts in this build: [{}]\n"
1189-
" (an artifact exists when a dependency edge requests it with "
1190-
"`artifacts = [\"<target>\"]`)",
1191-
bad, known.empty() ? std::string("none") : known));
1192-
}
1158+
if (auto refused = mcpp::build::refuse_unresolved_references(
1159+
ctx.plan, unresolvedTargets, ambiguousTargets, unresolvedArtifacts);
1160+
!refused)
1161+
return std::unexpected(refused.error());
11931162

11941163
// role = "object": the outputs are LINK inputs, so attach them to the
11951164
// link units that should receive them.

0 commit comments

Comments
 (0)