Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git
commit e260980016948431ec706d659cd97c866a78c409
parent 76d6d15dcdb40773af1a5c94b6d88bdb0d9f5aaf
Author: sentomk <sentomk@proton.me>
Date:   Tue, 17 Nov 2026 22:07:48 +0800

feat: invalidate build cache on toolchain or flag changes

Diffstat:
Mkumo_builder.cpp | 148+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 148 insertions(+), 0 deletions(-)

diff --git a/kumo_builder.cpp b/kumo_builder.cpp @@ -984,6 +984,99 @@ static ExitStatus run_status(const Cmd& cmd) { /// @return `true` if the command succeeded. static bool run(const Cmd& cmd) { return run_status(cmd).ok(); } +/// @brief Runs a command and captures its stdout and stderr (empty on failure). +static std::string capture(const Cmd& cmd) { + if (cmd.empty()) return {}; + std::fflush(stdout); + +#if defined(_WIN32) + SECURITY_ATTRIBUTES sa = {sizeof(sa), nullptr, TRUE}; + HANDLE read_pipe = nullptr; + HANDLE write_pipe = nullptr; + if (!CreatePipe(&read_pipe, &write_pipe, &sa, 0)) return {}; + SetHandleInformation(read_pipe, HANDLE_FLAG_INHERIT, 0); + + std::string line; + for (size_t i = 0; i < cmd.size(); ++i) { + if (i > 0) line += ' '; + line += quote_arg(cmd[i]); + } + STARTUPINFOA startup = {}; + startup.cb = sizeof(startup); + startup.dwFlags = STARTF_USESTDHANDLES; + startup.hStdInput = GetStdHandle(STD_INPUT_HANDLE); + startup.hStdOutput = write_pipe; + startup.hStdError = write_pipe; + PROCESS_INFORMATION info = {}; + if (!CreateProcessA(nullptr, line.data(), nullptr, nullptr, TRUE, 0, nullptr, + nullptr, &startup, &info)) { + CloseHandle(read_pipe); + CloseHandle(write_pipe); + return {}; + } + CloseHandle(write_pipe); + CloseHandle(info.hThread); + std::string out; + char buf[512]; + DWORD n = 0; + while (ReadFile(read_pipe, buf, sizeof(buf), &n, nullptr) && n > 0) { + out.append(buf, n); + } + CloseHandle(read_pipe); + WaitForSingleObject(info.hProcess, INFINITE); + DWORD code = 1; + GetExitCodeProcess(info.hProcess, &code); + CloseHandle(info.hProcess); + return code == 0 ? out : std::string{}; +#else + int fds[2]; + if (pipe(fds) != 0) return {}; + + posix_spawn_file_actions_t actions; + posix_spawn_file_actions_init(&actions); + posix_spawn_file_actions_addclose(&actions, fds[0]); + posix_spawn_file_actions_adddup2(&actions, fds[1], STDOUT_FILENO); + posix_spawn_file_actions_adddup2(&actions, fds[1], STDERR_FILENO); + posix_spawn_file_actions_addclose(&actions, fds[1]); + + std::vector<char*> argv; + for (const std::string& arg : cmd) { + argv.push_back(const_cast<char*>(arg.c_str())); + } + argv.push_back(nullptr); + + pid_t pid = 0; + int err = + posix_spawnp(&pid, argv[0], &actions, nullptr, argv.data(), environ); + posix_spawn_file_actions_destroy(&actions); + close(fds[1]); + if (err != 0) { + close(fds[0]); + return {}; + } + + std::string out; + char buf[512]; + for (;;) { + ssize_t n = read(fds[0], buf, sizeof(buf)); + if (n > 0) { + out.append(buf, static_cast<size_t>(n)); + } else if (n == 0) { + break; + } else if (errno != EINTR) { + break; + } + } + close(fds[0]); + + int raw = 0; + while (waitpid(pid, &raw, 0) < 0 && errno == EINTR) { + } + if (!WIFEXITED(raw) || WEXITSTATUS(raw) != 0) return {}; + return out; +#endif +} + /// @brief Says in words how a child ended, to follow the name of the program: /// `exited with status 3` or `was killed by signal 11 (Segmentation /// fault)`. @@ -1101,6 +1194,59 @@ static bool needs_compile(const fs::path& obj, const fs::path& dep, return false; } +static void append_list(std::ostringstream& ss, const char* key, + const Cmd& list) { + ss << key << "=" << list.size() << "\n"; + for (const std::string& item : list) { + ss << " " << item << "\n"; + } +} + +/// @brief Computes a fingerprint of the active compiler and build flags. +static std::string toolchain_fingerprint(const Config& cfg) { + const std::string version = capture({cfg.compiler, "--version"}); + if (version.empty()) { + print_error("cannot run compiler '" + cfg.compiler + "'"); + std::exit(EXIT_FAILURE); + } + std::ostringstream ss; + ss << "compiler=" << cfg.compiler << "\n" + << "version:\n" + << version << "\n"; + append_list(ss, "flags", cfg.flags); + append_list(ss, "objcxx_flags", cfg.objcxx_flags); + append_list(ss, "link", cfg.link_flags); + return ss.str(); +} + +/// @brief Clears stale build outputs if the compiler or flags changed. +static void check_toolchain_cache(const Config& cfg) { + const fs::path stamp = cfg.out_dir / ".toolchain"; + const std::string current = toolchain_fingerprint(cfg); + + if (fs::exists(stamp)) { + std::ifstream in(stamp, std::ios::binary); + std::ostringstream old; + old << in.rdbuf(); + if (old.str() == current) return; + log_step(Style::Cyan, "rebuild", + "toolchain or flags changed, clearing " + + cfg.out_dir.generic_string() + "/"); + fs::remove_all(cfg.out_dir); + } else if (fs::exists(cfg.out_dir)) { + // Existing build directory without a stamp: clear once so objects from + // an unknown compiler cannot linger. + log_step( + Style::Cyan, "rebuild", + "no toolchain stamp, clearing " + cfg.out_dir.generic_string() + "/"); + fs::remove_all(cfg.out_dir); + } + + fs::create_directories(cfg.out_dir); + std::ofstream out(stamp, std::ios::binary); + out << current; +} + /// @} /// @name compile_commands.json @@ -1491,6 +1637,8 @@ int main(int argc, char** argv) { "' (key 'tests') does not exist"); } + check_toolchain_cache(cfg); + // The compile flags come from the builder and from kumo.toml, so an object // file older than either is stale. fs::file_time_type flags_time =