1515// module the rebuild started at, when the importers that followed are many,
1616// and once a build directory: the developer who chose the shape is not told
1717// on every edit.
18+ //
19+ // A SECOND ADVICE: WHEN A BUILD FAILS AT A STEP AN ENTRY ACTED ON (SPEC-019
20+ // §9). Most projects are right with every entry on; the few that are not need
21+ // to learn, from the failure itself, how to find out whether an entry is the
22+ // cause, without it being said on every failure. It reads the step that
23+ // failed, not the entries that ran: a source built as two units that failed
24+ // (R1), a unit that imports a BMI kept with the lines of an earlier source
25+ // that failed (R2), a link that failed where either is in the graph (R3). Any
26+ // other failure says nothing (R4). The text is made of the register's own key,
27+ // environment name and `check` value: there is no second spelling of a switch.
1828
1929export module mcpp.buildopt.advice;
2030
2131import std;
32+ import mcpp.buildopt.registry;
2233
2334export namespace mcpp ::buildopt::advice {
2435
@@ -53,6 +64,46 @@ std::string line(const Cascade& c);
5364// yet; saying it is recorded there (`buildopt-advice.list`).
5465bool first_time (const std::filesystem::path& dir, std::string_view module );
5566
67+ // WHAT THE PLAN HOLDS THAT A FAILURE READS (SPEC-019 §9), kept beside
68+ // build.ninja (`buildopt-applied.tsv`) because the fast path, which has no
69+ // plan, reports a failure as well.
70+ struct Fragment {
71+ std::string written; // the source as it is written
72+ std::vector<std::string> units; // the files it is built as
73+ };
74+ struct Importer {
75+ std::string source; // a unit that imports a module
76+ std::vector<std::string> imports; // the modules it imports
77+ };
78+ struct Applied {
79+ std::vector<Fragment> fragments;
80+ std::vector<Importer> importers;
81+ std::string fragmentCheck; // the `private_fragment` value that checks, for this compiler
82+ };
83+ inline constexpr std::string_view kAppliedFile = " buildopt-applied.tsv" ;
84+ // Written when it differs from what is there.
85+ void write_applied (const std::filesystem::path& dir, const Applied& applied);
86+ Applied read_applied (const std::filesystem::path& dir);
87+
88+ // The step that failed: the first input of each failed step (the source, for a
89+ // compile), and whether the linker said a link failed.
90+ struct Failed {
91+ std::vector<std::string> sources;
92+ bool linked = false ;
93+ };
94+ // The outputs each `FAILED:` line of ninja's output names.
95+ std::vector<std::vector<std::string>> failed_outputs (std::string_view ninjaOutput);
96+ // Whether the output says a link failed, in either linker's words.
97+ bool linker_failed (std::string_view ninjaOutput);
98+
99+ struct Tip {
100+ std::string domain;
101+ std::string text;
102+ };
103+ // The tips for a failure. `kept` names the BMIs the build directory keeps with
104+ // the lines of an earlier source (`bmi-kept.list`).
105+ std::vector<Tip> suspects (const Applied& applied, const std::vector<std::string>& kept, const Failed& failed);
106+
56107} // namespace mcpp::buildopt::advice
57108
58109namespace mcpp ::buildopt::advice {
@@ -109,6 +160,29 @@ std::string line(const Cascade& c) {
109160 c.source , c.module , c.importers , c.importers );
110161}
111162
163+ namespace {
164+
165+ // A source as a step names it: relative to its package's root, or absolute.
166+ // The plan's units are absolute, so one names the other when it is the tail.
167+ bool names (std::string_view planned, std::string_view stated) {
168+ if (stated.empty ()) return false ;
169+ if (planned == stated) return true ;
170+ return planned.size () > stated.size () && planned.ends_with (stated)
171+ && planned[planned.size () - stated.size () - 1 ] == ' /' ;
172+ }
173+
174+ std::string bmi_stem_of (std::string module ) {
175+ std::ranges::replace (module , ' :' , ' -' );
176+ return module ;
177+ }
178+
179+ // The invocation that checks one entry, and the statement that keeps it.
180+ std::string how_to_check (const registry::Entry& e, std::string_view check) {
181+ return std::format (" {}={} mcpp build ({} = \" {}\" keeps it)" , e.env , check, e.key , check);
182+ }
183+
184+ } // namespace
185+
112186bool first_time (const std::filesystem::path& dir, std::string_view module ) {
113187 const auto file = dir / " buildopt-advice.list" ;
114188 {
@@ -121,4 +195,136 @@ bool first_time(const std::filesystem::path& dir, std::string_view module) {
121195 return true ;
122196}
123197
198+ void write_applied (const std::filesystem::path& dir, const Applied& applied) {
199+ std::string text;
200+ if (!applied.fragments .empty ()) text += std::format (" check\t {}\n " , applied.fragmentCheck );
201+ for (auto const & f : applied.fragments ) {
202+ text += std::format (" fragment\t {}" , f.written );
203+ for (auto const & u : f.units ) text += " \t " + u;
204+ text += " \n " ;
205+ }
206+ for (auto const & i : applied.importers ) {
207+ text += std::format (" imports\t {}" , i.source );
208+ for (auto const & m : i.imports ) text += " \t " + m;
209+ text += " \n " ;
210+ }
211+ const auto file = dir / kAppliedFile ;
212+ std::string have;
213+ {
214+ std::ifstream in (file, std::ios::binary);
215+ have.assign (std::istreambuf_iterator<char >(in), {});
216+ }
217+ if (have == text) return ;
218+ std::ofstream (file, std::ios::binary | std::ios::trunc) << text;
219+ }
220+
221+ Applied read_applied (const std::filesystem::path& dir) {
222+ Applied a;
223+ std::ifstream in (dir / kAppliedFile , std::ios::binary);
224+ for (std::string line; std::getline (in, line);) {
225+ std::vector<std::string> cells;
226+ for (std::size_t at = 0 ;;) {
227+ const auto tab = line.find (' \t ' , at);
228+ cells.push_back (line.substr (at, tab == std::string::npos ? tab : tab - at));
229+ if (tab == std::string::npos) break ;
230+ at = tab + 1 ;
231+ }
232+ if (cells.size () < 2 ) continue ;
233+ if (cells[0 ] == " check" ) a.fragmentCheck = cells[1 ];
234+ else if (cells[0 ] == " fragment" ) a.fragments .push_back ({cells[1 ], {cells.begin () + 2 , cells.end ()}});
235+ else if (cells[0 ] == " imports" ) a.importers .push_back ({cells[1 ], {cells.begin () + 2 , cells.end ()}});
236+ }
237+ return a;
238+ }
239+
240+ std::vector<std::vector<std::string>> failed_outputs (std::string_view output) {
241+ std::vector<std::vector<std::string>> steps;
242+ while (!output.empty ()) {
243+ const auto end = output.find (' \n ' );
244+ auto line = output.substr (0 , end);
245+ output = end == std::string_view::npos ? std::string_view{} : output.substr (end + 1 );
246+ while (!line.empty () && (line.front () == ' ' || line.front () == ' \t ' )) line.remove_prefix (1 );
247+ if (!line.starts_with (" FAILED:" )) continue ;
248+ line.remove_prefix (7 );
249+ if (line.find (" [code=" ) != std::string_view::npos) line = line.substr (line.find (' ]' ) + 1 );
250+ std::vector<std::string> outputs;
251+ for (std::size_t at = 0 ; at < line.size ();) {
252+ const auto next = line.find (' ' , at);
253+ const auto word = line.substr (at, next == std::string_view::npos ? next : next - at);
254+ if (!word.empty ()) outputs.emplace_back (word);
255+ if (next == std::string_view::npos) break ;
256+ at = next + 1 ;
257+ }
258+ if (!outputs.empty ()) steps.push_back (std::move (outputs));
259+ }
260+ return steps;
261+ }
262+
263+ bool linker_failed (std::string_view output) {
264+ for (std::string_view words : {" undefined reference to" , " undefined symbol" , " multiple definition of" ,
265+ " duplicate symbol" , " ld returned" , " ld.lld: error" , " lld-link: error" })
266+ if (output.find (words) != std::string_view::npos) return true ;
267+ return false ;
268+ }
269+
270+ std::vector<Tip> suspects (const Applied& applied, const std::vector<std::string>& kept, const Failed& failed) {
271+ std::vector<Tip> tips;
272+ const auto * fragment = registry::find (registry::kPrivateFragment );
273+ const auto * cutoff = registry::find (registry::kGcmLocations );
274+ const std::string_view off = registry::kStandardEnv ;
275+
276+ // R1: the step that failed compiled a source built as units of its own.
277+ for (auto const & f : applied.fragments ) {
278+ const bool hit = std::ranges::any_of (f.units , [&](const std::string& unit) {
279+ return std::ranges::any_of (failed.sources , [&](const std::string& s) { return names (unit, s); });
280+ });
281+ if (!hit || !fragment) continue ;
282+ tips.push_back ({" buildopt/private-fragment" , std::format (
283+ " {} was built as two units (private-fragment), and one of them failed.\n "
284+ " To check whether that is the cause: {}\n "
285+ " If it builds, report the file; {}=off turns every optimization off for one run." ,
286+ f.written , how_to_check (*fragment, applied.fragmentCheck .empty () ? fragment->check : applied.fragmentCheck ),
287+ off)});
288+ break ;
289+ }
290+
291+ // R2: the step that failed compiled a unit that imports a BMI kept with the
292+ // lines of an earlier source.
293+ std::vector<std::string> keptStems;
294+ for (auto const & k : kept) keptStems.push_back (std::filesystem::path (k).stem ().string ());
295+ std::set<std::string> modules;
296+ for (auto const & i : applied.importers ) {
297+ if (!std::ranges::any_of (failed.sources , [&](const std::string& s) { return names (i.source , s); })) continue ;
298+ for (auto const & m : i.imports )
299+ if (std::ranges::contains (keptStems, bmi_stem_of (m))) modules.insert (m);
300+ }
301+ if (!modules.empty () && cutoff) {
302+ std::string list;
303+ std::size_t shown = 0 ;
304+ for (auto const & m : modules) {
305+ if (shown++ == 3 ) { list += " , ..." ; break ; }
306+ list += (list.empty () ? " " : " , " ) + m;
307+ }
308+ tips.push_back ({" buildopt/bmi-cutoff" , std::format (
309+ " the BMI of module {} was kept although its source changed (bmi_cutoff), and a unit that imports it failed.\n "
310+ " To check whether that is the cause: {}\n "
311+ " If it builds, report the module; {}=off turns every optimization off for one run." ,
312+ list, how_to_check (*cutoff, cutoff->check ), off)});
313+ }
314+
315+ // R3: a link failed, and the graph holds something either entry acted on.
316+ if (tips.empty () && failed.linked ) {
317+ if (!applied.fragments .empty () && fragment)
318+ tips.push_back ({" buildopt/private-fragment" , std::format (
319+ " the link failed, and {} source(s) of this build are built as two units (private-fragment); the link cannot say whether that is the cause.\n "
320+ " To check: {}" , applied.fragments .size (),
321+ how_to_check (*fragment, applied.fragmentCheck .empty () ? fragment->check : applied.fragmentCheck ))});
322+ if (!kept.empty () && cutoff)
323+ tips.push_back ({" buildopt/bmi-cutoff" , std::format (
324+ " the link failed, and {} BMI(s) of this build are kept with the lines of an earlier source (bmi_cutoff); the link cannot say whether that is the cause.\n "
325+ " To check: {}" , kept.size (), how_to_check (*cutoff, cutoff->check ))});
326+ }
327+ return tips;
328+ }
329+
124330} // namespace mcpp::buildopt::advice
0 commit comments