One user-space repair task per eligible HWE debug-diag / compile-deploy base ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content filter. Each ships a public spec with a check-to-basis table, at least two defects with single-fix fixtures, a symptom-masking fixture and an alternative correct implementation. Claude re-checked all 320: structure, the HWE quality items and trace6 Docker calibration receipts against current digests (320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120 ad-hoc family names; the next batch maps them to the canonical list. The batch's 1,256-line progress log stays in its factory report and the 36-local branch batch15-hwe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
296 lines
9.7 KiB
C++
296 lines
9.7 KiB
C++
#include "planner.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <map>
|
|
#include <set>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
namespace buildplan {
|
|
namespace {
|
|
|
|
struct Validation {
|
|
bool bad_reference = false;
|
|
bool cycle = false;
|
|
std::vector<std::string> order;
|
|
};
|
|
|
|
const Target *find_target(const Input &input, const std::string &name) {
|
|
auto position = input.targets.find(name);
|
|
if (position == input.targets.end()) {
|
|
return nullptr;
|
|
}
|
|
return &position->second;
|
|
}
|
|
|
|
const Profile *find_profile(const Input &input, const std::string &name) {
|
|
auto position = input.profiles.find(name);
|
|
if (position == input.profiles.end()) {
|
|
return nullptr;
|
|
}
|
|
return &position->second;
|
|
}
|
|
|
|
bool valid_edge(const Target &owner, const Target &dependency) {
|
|
if (dependency.kind != TargetKind::Static) {
|
|
return false;
|
|
}
|
|
if (dependency.profile != owner.profile) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool references_exist(const Input &input) {
|
|
for (const auto &entry : input.targets) {
|
|
const Target &target = entry.second;
|
|
if (find_profile(input, target.profile) == nullptr) {
|
|
return false;
|
|
}
|
|
for (const std::string &name : target.dependencies) {
|
|
const Target *dependency = find_target(input, name);
|
|
if (dependency == nullptr || !valid_edge(target, *dependency)) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
Validation validate(const Input &input) {
|
|
Validation outcome;
|
|
if (!references_exist(input)) {
|
|
outcome.bad_reference = true;
|
|
return outcome;
|
|
}
|
|
std::map<std::string, int> unresolved;
|
|
std::map<std::string, std::vector<std::string>> reverse_edges;
|
|
for (const auto &entry : input.targets) {
|
|
unresolved[entry.first] = static_cast<int>(entry.second.dependencies.size());
|
|
for (const std::string &dependency : entry.second.dependencies) {
|
|
reverse_edges[dependency].push_back(entry.first);
|
|
}
|
|
}
|
|
std::set<std::string> ready;
|
|
for (const auto &entry : unresolved) {
|
|
if (entry.second == 0) {
|
|
ready.insert(entry.first);
|
|
}
|
|
}
|
|
while (!ready.empty()) {
|
|
std::string next = *ready.begin();
|
|
ready.erase(ready.begin());
|
|
outcome.order.push_back(next);
|
|
for (const std::string &dependent : reverse_edges[next]) {
|
|
int &left = unresolved[dependent];
|
|
--left;
|
|
if (left == 0) {
|
|
ready.insert(dependent);
|
|
}
|
|
}
|
|
}
|
|
if (outcome.order.size() != input.targets.size()) {
|
|
outcome.cycle = true;
|
|
outcome.order.clear();
|
|
}
|
|
return outcome;
|
|
}
|
|
|
|
// An executable's thread requirement comes from every reachable static input.
|
|
// The memo is reset for each plan; it does not carry requirements between jobs.
|
|
bool closure_threads(const Input &input, const std::string &name,
|
|
std::map<std::string, bool> *memo) {
|
|
auto known = memo->find(name);
|
|
if (known != memo->end()) {
|
|
return known->second;
|
|
}
|
|
const Target &target = input.targets.at(name);
|
|
bool needed = target.needs_threads;
|
|
for (const std::string &dependency : target.dependencies) {
|
|
if (closure_threads(input, dependency, memo)) {
|
|
needed = true;
|
|
}
|
|
}
|
|
memo->emplace(name, needed);
|
|
return needed;
|
|
}
|
|
|
|
void collect_archive(const Input &input, const std::string &name,
|
|
std::set<std::string> *seen,
|
|
std::vector<std::string> *archives) {
|
|
if (!seen->insert(name).second) {
|
|
return;
|
|
}
|
|
archives->push_back(archive_path(name));
|
|
const Target &target = input.targets.at(name);
|
|
std::vector<std::string> sorted = target.dependencies;
|
|
std::sort(sorted.begin(), sorted.end());
|
|
for (const std::string &dependency : sorted) {
|
|
collect_archive(input, dependency, seen, archives);
|
|
}
|
|
}
|
|
|
|
std::vector<std::string> closure_archives(const Input &input,
|
|
const Target &target) {
|
|
std::vector<std::string> archives;
|
|
std::set<std::string> seen;
|
|
std::vector<std::string> direct = target.dependencies;
|
|
std::sort(direct.begin(), direct.end());
|
|
for (const std::string &dependency : direct) {
|
|
collect_archive(input, dependency, &seen, &archives);
|
|
}
|
|
return archives;
|
|
}
|
|
|
|
std::vector<std::string> compile_command(const Target &target,
|
|
const Profile &profile,
|
|
const std::string &source,
|
|
const std::string &output) {
|
|
std::vector<std::string> argv;
|
|
argv.push_back("gcc");
|
|
argv.push_back("--sysroot=" + profile.sysroot);
|
|
argv.push_back("-c");
|
|
argv.push_back(source);
|
|
argv.push_back("-o");
|
|
argv.push_back(output);
|
|
if (target.needs_threads) {
|
|
argv.push_back("-pthread");
|
|
}
|
|
for (const std::string &path : target.include_dirs) {
|
|
argv.push_back("-I" + profile.sysroot + path);
|
|
}
|
|
for (const std::string &flag : target.compile_flags) {
|
|
argv.push_back("-" + flag);
|
|
}
|
|
for (const std::string &definition : target.defines) {
|
|
argv.push_back("-D" + definition);
|
|
}
|
|
return argv;
|
|
}
|
|
|
|
std::vector<std::string> archive_command(const Target &target,
|
|
const std::vector<std::string> &objects) {
|
|
std::vector<std::string> argv;
|
|
argv.push_back("ar");
|
|
argv.push_back("rcs");
|
|
argv.push_back(archive_path(target.name));
|
|
argv.insert(argv.end(), objects.begin(), objects.end());
|
|
return argv;
|
|
}
|
|
|
|
std::vector<std::string> link_prefix(const Profile &profile,
|
|
const std::vector<std::string> &objects) {
|
|
std::vector<std::string> argv;
|
|
argv.push_back("gcc");
|
|
argv.push_back("--sysroot=" + profile.sysroot);
|
|
argv.insert(argv.end(), objects.begin(), objects.end());
|
|
return argv;
|
|
}
|
|
|
|
void append_output(std::vector<std::string> *argv, const Target &target) {
|
|
argv->push_back("-o");
|
|
argv->push_back(executable_path(target.output_name.empty() ?
|
|
target.name : target.output_name));
|
|
}
|
|
|
|
void append_thread_options(std::vector<std::string> *argv,
|
|
const Profile &profile, bool needs_threads,
|
|
const Input &input) {
|
|
if (!needs_threads) {
|
|
return;
|
|
}
|
|
argv->push_back("-pthread");
|
|
if (profile.mode == ThreadMode::Separate) {
|
|
// Initial worksite symptom: this path remains on the first profile.
|
|
const Profile &default_profile = input.profiles.begin()->second;
|
|
argv->push_back("-L" + default_profile.sysroot + "/usr/lib");
|
|
argv->push_back("-lthreadshim");
|
|
}
|
|
}
|
|
|
|
std::vector<std::string> link_command(const Input &input,
|
|
const Target &target,
|
|
const Profile &profile,
|
|
const std::vector<std::string> &objects,
|
|
std::map<std::string, bool> *memo) {
|
|
std::vector<std::string> argv = link_prefix(profile, objects);
|
|
std::vector<std::string> archives = closure_archives(input, target);
|
|
argv.insert(argv.end(), archives.begin(), archives.end());
|
|
append_output(&argv, target);
|
|
for (const std::string &library : target.runtime_libs) {
|
|
argv.push_back("-L" + profile.sysroot + "/usr/lib");
|
|
argv.push_back("-l" + library);
|
|
}
|
|
for (const std::string &flag : target.link_flags) {
|
|
argv.push_back("-" + flag);
|
|
}
|
|
// Initial worksite symptom: a final target checks only its own source.
|
|
bool thread_dependency = closure_threads(input, target.name, memo);
|
|
append_thread_options(&argv, profile, thread_dependency, input);
|
|
return argv;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::string object_path(const std::string &name) {
|
|
return "out/" + name + ".o";
|
|
}
|
|
|
|
std::string archive_path(const std::string &name) {
|
|
return "out/" + name + ".a";
|
|
}
|
|
|
|
std::string executable_path(const std::string &name) {
|
|
return "out/" + name;
|
|
}
|
|
|
|
std::string format_command(const std::vector<std::string> &argv) {
|
|
std::string result;
|
|
for (const std::string &token : argv) {
|
|
if (!result.empty()) {
|
|
result.push_back(' ');
|
|
}
|
|
result += token;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
Result make_plan(const Input &input) {
|
|
Result result;
|
|
Validation check = validate(input);
|
|
if (check.bad_reference) {
|
|
result.error = "REFERENCE";
|
|
return result;
|
|
}
|
|
if (check.cycle) {
|
|
result.error = "CYCLE";
|
|
return result;
|
|
}
|
|
std::map<std::string, bool> memo;
|
|
for (const std::string &name : check.order) {
|
|
const Target &target = input.targets.at(name);
|
|
const Profile &profile = input.profiles.at(target.profile);
|
|
std::vector<std::string> objects;
|
|
objects.push_back(object_path(target.name));
|
|
result.commands.push_back(compile_command(target, profile,
|
|
target.source, objects.back()));
|
|
for (std::size_t i = 0; i < target.extra_sources.size(); ++i) {
|
|
std::string output = "out/" + target.name + "_" +
|
|
std::to_string(i + 1) + ".o";
|
|
objects.push_back(output);
|
|
result.commands.push_back(compile_command(target, profile,
|
|
target.extra_sources[i], objects.back()));
|
|
}
|
|
if (target.kind == TargetKind::Static) {
|
|
result.commands.push_back(archive_command(target, objects));
|
|
} else {
|
|
result.commands.push_back(link_command(input, target, profile,
|
|
objects, &memo));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
} // namespace buildplan
|