// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® module Crafter.Build:Interface_impl; import std; import :Interface; import :Jobs; import :Platform; namespace fs = std::filesystem; namespace { // The job that makes an interface unit's object from its BMI, once the // jobs in `after` have finished. The command is put together here, as in // the functions below, so the job owns the one it runs. void AddObjectJob(Crafter::JobGroup& jobs, std::string_view clang, const fs::path& path, const fs::path& pcmDir, const fs::path& buildDir, std::span after) { jobs.Add(std::format("Compiling {}.cppm", path.filename().string()), [command = std::format("{} -Wno-unused-command-line-argument {}.pcm -c -o {}.o", clang, (pcmDir/path.filename()).string(), (buildDir/path.filename()).string())] { return Crafter::RunBuildCommand(command); }, after); } // The two compiler runs of a stale interface unit, as jobs of `jobs`: its // BMI once the jobs in `after` have finished, then its object from that // BMI. Returns the first, the one the units importing this one come // after: the object is the link's alone to wait for. Crafter::JobGroup::Job AddInterfaceJobs(Crafter::JobGroup& jobs, std::string_view clang, const fs::path& path, const fs::path& pcmDir, const fs::path& buildDir, std::span after) { // -MD records every header the preamble pulled in, next to the BMI as // .pcm.d, so the next Check can see an edit to one of them. const Crafter::JobGroup::Job precompile = jobs.Add(std::format("Precompiling {}.cppm", path.filename().string()), [command = std::format("{0} {1}.cppm --precompile -MD -MF {2}.pcm.d -o {2}.pcm", clang, path.string(), (pcmDir/path.filename()).string())] { return Crafter::RunBuildCommand(command); }, after); const std::array precompiled = {precompile}; AddObjectJob(jobs, clang, path, pcmDir, buildDir, precompiled); return precompile; } // For a unit whose BMI is up to date: the object job alone, when the // object is missing or older than the BMI it is made from. The two are // written by two commands, and a build can end between them: by an error // elsewhere, which keeps the second from starting, by the second failing, // or by being interrupted. An object written in the same tick of the // file system's clock as its BMI is taken to be the later of the two, // which it is whenever the command that writes it ran. void AddObjectJobIfStale(Crafter::JobGroup& jobs, std::string_view clang, const fs::path& path, const fs::path& pcmDir, const fs::path& buildDir) { std::error_code ec; const fs::file_time_type pcmTime = fs::last_write_time(std::format("{}.pcm", (pcmDir/path.filename()).string()), ec); // No BMI to make it from; Check does not call such a unit up to date. if (ec) return; const fs::file_time_type objectTime = fs::last_write_time(std::format("{}.o", (buildDir/path.filename()).string()), ec); if (!ec && objectTime >= pcmTime) return; AddObjectJob(jobs, clang, path, pcmDir, buildDir, {}); } } namespace Crafter { ModulePartition::ModulePartition(std::string&& name, fs::path&& path) : name(std::move(name)), path(std::move(path)) {} bool ModulePartition::Check(const fs::path& pcmDir, fs::file_time_type sourceFloor) { if(!checked) { checked = true; std::string pcmPath = std::format("{}.pcm", (pcmDir/path.filename()).generic_string()); std::string cppmPath = std::format("{}.cppm", path.generic_string()); // NewestPrerequisite covers the headers this partition #includes — // they are inputs to the BMI just as much as the .cppm is, and the // .cppm's own mtime says nothing about them. It only ever bounds // *this* artifact, so it goes in the comparison below and not into // the `sourceFloor` handed to the recursive Checks: each of those // reads its own depfile. if(fs::exists(pcmPath) && std::max({fs::last_write_time(cppmPath), sourceFloor, NewestPrerequisite(std::format("{}.d", pcmPath))}) < fs::last_write_time(pcmPath)) { fs::file_time_type pcmTime = fs::last_write_time(pcmPath); for(ModulePartition* dependency : partitionDependencies) { if(dependency->Check(pcmDir, sourceFloor)) { needsRecompiling = true; return true; } } for(Module* dependency : moduleDependencies) { if(dependency->Check(pcmDir, sourceFloor)) { needsRecompiling = true; return true; } } for(const auto& [externalMod, externalPcmPath] : externalModuleDependencies) { std::error_code ec; fs::file_time_type t = fs::last_write_time(externalPcmPath, ec); if (!ec && t >= pcmTime) { needsRecompiling = true; return true; } } needsRecompiling = false; return false; } else { needsRecompiling = true; return true; } } else { return needsRecompiling; } } std::optional ModulePartition::Schedule(JobGroup& jobs, std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir) { if (scheduled) return precompileJob; if (needsRecompiling) { std::vector after; for (ModulePartition* dependency : partitionDependencies) { if (const std::optional job = dependency->Schedule(jobs, clang, pcmDir, buildDir)) after.push_back(*job); } for (Module* dependency : moduleDependencies) { if (const std::optional job = dependency->Schedule(jobs, clang, pcmDir, buildDir)) after.push_back(*job); } precompileJob = AddInterfaceJobs(jobs, clang, path, pcmDir, buildDir, after); } else { AddObjectJobIfStale(jobs, clang, path, pcmDir, buildDir); } scheduled = true; return precompileJob; } Module::Module(std::string&& name, fs::path&& path) : name(std::move(name)), path(std::move(path)) {} bool Module::Check(const fs::path& pcmDir, fs::file_time_type sourceFloor) { if(!checked) { checked = true; std::string pcmPath = std::format("{}.pcm", (pcmDir/path.filename()).generic_string()); std::string cppmPath = std::format("{}.cppm", path.generic_string()); // See ModulePartition::Check — the depfile clang wrote beside the BMI // is the only record of which headers this interface unit included. if(fs::exists(pcmPath) && std::max({fs::last_write_time(cppmPath), sourceFloor, NewestPrerequisite(std::format("{}.d", pcmPath))}) < fs::last_write_time(pcmPath)) { fs::file_time_type pcmTime = fs::last_write_time(pcmPath); // Every partition gets Check()ed even once one is known stale: // Schedule() drives partition rebuilds off their own // needsRecompiling flags, so short-circuiting here would leave // the later ones unevaluated and silently unbuilt. bool depCheck = false; for(std::unique_ptr& partition : partitions) { if(partition->Check(pcmDir, sourceFloor)) { depCheck = true; } } // Modules this interface unit imports directly. Local ones share // our pcmDir and are resolved recursively; external ones are // compared by PCM mtime, their owning Configuration having // already finished building by the time we run. for(Module* dependency : moduleDependencies) { if(dependency->Check(pcmDir, sourceFloor)) { depCheck = true; } } if(!depCheck) { for(const auto& [externalMod, externalPcmPath] : externalModuleDependencies) { std::error_code ec; fs::file_time_type t = fs::last_write_time(externalPcmPath, ec); if (!ec && t >= pcmTime) { depCheck = true; break; } } } if(depCheck) { needsRecompiling = true; return true; } else { needsRecompiling = false; return false; } } else { // Already known stale, but the dependencies still need // evaluating: Schedule() below reads their needsRecompiling, // and an unchecked local module would never get built. for(std::unique_ptr& partition : partitions) { partition->Check(pcmDir, sourceFloor); } for(Module* dependency : moduleDependencies) { dependency->Check(pcmDir, sourceFloor); } needsRecompiling = true; return true; } } else { return needsRecompiling; } } std::optional Module::Schedule(JobGroup& jobs, std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir) { if (scheduled) return precompileJob; std::vector after; // All of the module's partitions, not the ones the primary interface // names: Build() records no edge for its `import :Part;` lines (see // Module::moduleDependencies). And of a module that is up to date as // well: this is where its partitions are scheduled, and one of them // may be without its object. for (const std::unique_ptr& partition : partitions) { if (const std::optional job = partition->Schedule(jobs, clang, pcmDir, buildDir)) after.push_back(*job); } if (needsRecompiling) { // A sibling module in the same Configuration that this interface // unit imports has to have its PCM on disk before we precompile // against it. for (Module* dependency : moduleDependencies) { if (const std::optional job = dependency->Schedule(jobs, clang, pcmDir, buildDir)) after.push_back(*job); } precompileJob = AddInterfaceJobs(jobs, clang, path, pcmDir, buildDir, after); } else { AddObjectJobIfStale(jobs, clang, path, pcmDir, buildDir); } scheduled = true; return precompileJob; } }