Files
kappa/src/rebuild/rebuild.cpp
T
huntedbytheirs 7e93db2d07 feat: portability, correctness, and quality improvements for v0.2
Portability (Linux distro-agnostic):
- Remove hardcoded Clang compiler enforcement; GCC now builds
- Add find_package(Threads REQUIRED) for older glibc
- Add cmake install() target
- FHS 3.0 default root: /kappa -> /usr/local/kappa
- fs::path operator/ for all init/bootloader paths (fixes prefix fragility)
- Multi-distro zoneinfo search (FHS, NixOS, Guix, alt)
- Portable tar extraction (drop GNU-only --no-same-permissions)
- Runit enable/disable commands now prefix-aware
- --root CLI flag before/after subcommand, lazy directory creation
- Shebang constants de-duplicated to types.hpp

Correctness (race conditions, UB, corruption):
- Fix CWD race in scheduler: per-child chdir() instead of process-global
- Fix UB const_cast in exec_cmd/exec_capture: mutable argv buffers
- Fix non-atomic installed DB writes: tmp+rename pattern
- Fix read_file() no longer calls exit(1), throws instead
- Fix silent catch(...) parse errors now print diagnostics
- Fix rebuild false positives with config_hash change detection
- Fix s6 disable_cmd copy-paste bug (was identical to enable)
- Fix runit enable_cmd incomplete, disable_cmd wrong target
- Fix dinit env vars: functional env-file + companion .env

Quality:
- Add -Wall -Wextra -Wpedantic to CMake, fix 2 pre-existing warnings
- Move parse_int from error.hpp to parse_util.hpp
- Fix hash verification guard checks all three hash types
- Check patch return code in fetch.cpp
- Add explicit system_dir creation in ensure_directories()
- Add resolve to needs_dirs for build_registry() consistency
- Update stale /kappa path references in examples
- Remove inaccurate -Werror claim in CONTRIBUTING.md
- Add build-gcc/ and agent dirs to .gitignore
- Suppress clang-tidy portability-avoid-pragma-once
- Fix .gitignore /kappa pattern (was matching include/kappa/)
- Delete stale vcpkg_installed/ directory

54/54 tests pass. Builds on Clang and GCC with 0 warnings.
2026-07-31 08:28:49 -04:00

133 lines
4.1 KiB
C++

#include "kappa/rebuild/rebuild.hpp"
#include "kappa/install/install.hpp"
#include "kappa/service/types.hpp"
#include <iostream>
#include <set>
namespace kappa::rebuild {
ChangeSet compute_changes(const dsl::SystemConfig& cfg) {
ChangeSet cs;
auto installed = install::read_installed();
std::set<std::string> installed_names;
for (auto& e : installed) { installed_names.insert(e.name); }
std::set<std::string> config_names;
for (auto& p : cfg.packages) { config_names.insert(p.name); }
for (auto& p : cfg.packages) {
if (!installed_names.contains(p.name)) {
cs.added.push_back(p.name);
continue;
}
bool found = false;
for (auto& e : installed) {
if (e.name != p.name) { continue; }
found = true;
bool changed = false;
if (!p.version.empty() && p.version != e.version) {
changed = true;
}
if (!changed && (!p.features.empty() || !p.config.empty())) {
if (e.config_hash.empty()) {
changed = true;
} else {
auto ch = install::compute_config_hash(p.features, p.config);
changed = (ch != e.config_hash);
}
}
if (changed) { cs.changed.push_back(p.name); }
break;
}
if (!found) { cs.added.push_back(p.name); }
}
for (auto& e : installed) {
if (!config_names.contains(e.name)) {
cs.removed.push_back(e.name);
}
}
for (auto& e : installed) {
// Compare the installed version (which stores the identifier string
// for virtual packages like init/kernel/bootloader) against the
// config value. These virtual entries have their identity in the
// version field, not the hash field.
if (e.name == "kernel" && e.version != cfg.boot.kernel) {
cs.kernel_changed = true;
}
if (e.name == "init" && e.version != cfg.boot.init) {
cs.init_changed = true;
}
if (e.name == "bootloader" && e.version != cfg.boot.bootloader) {
cs.bootloader_changed = true;
}
}
for (auto& svc : cfg.services) {
if (svc.enable) { cs.services_changed = true; break; }
}
return cs;
}
InitImpact compute_init_impact(const dsl::SystemConfig& cfg,
const resolve::Registry& registry) {
InitImpact impact;
auto init_system = cfg.boot.init;
// Only compute impact if an init system is actually configured
if (init_system.empty()) { return impact; }
auto is = kappa::service::parse_init_system(init_system);
if (is == kappa::service::InitSystem::Unknown) {
std::cerr << "warning: unknown init system '" << init_system
<< "' — cannot compute init impact\n";
return impact;
}
// For each package in the config that has a matching registry entry...
for (auto& pref : cfg.packages) {
auto rit = registry.find(pref.name);
if (rit == registry.end()) { continue; }
auto& pkg = rit->second;
// No services — nothing to do
if (pkg.services.empty()) {
impact.skipped.push_back(pkg.name);
continue;
}
// Check if build scripts reference ${enabledinit}
bool uses_enabledinit = false;
auto check_phase = [&](const dsl::Phase& phase) {
for (auto& cmd : phase.commands) {
if (cmd.find("${enabledinit}") != std::string::npos) {
uses_enabledinit = true;
return;
}
}
};
check_phase(pkg.prepare);
if (!uses_enabledinit) check_phase(pkg.build);
if (!uses_enabledinit) check_phase(pkg.check);
if (!uses_enabledinit) check_phase(pkg.install);
if (uses_enabledinit) {
impact.service_rebuild.push_back(pkg.name);
} else {
impact.service_only.push_back(pkg.name);
}
}
return impact;
}
} // namespace kappa::rebuild