// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® module Crafter.Build:Implementation_impl; import std; import :Implementation; import :Interface; import :Jobs; import :Platform; namespace fs = std::filesystem; namespace Crafter { Implementation::Implementation(fs::path&& path) : path(std::move(path)) { } bool Implementation::Check(const fs::path& object, const fs::path& pcmDir, fs::file_time_type sourceFloor) const { std::string objPath = object.string(); std::string cppPath = std::format("{}.cpp", path.string()); // The depfile clang wrote beside the object lists the headers this TU // included when the object was compiled: inputs of the object as // much as the source and the BMIs it imports. if(!fs::exists(objPath) || std::max({fs::last_write_time(cppPath), sourceFloor, NewestPrerequisite(std::format("{}.d", objPath))}) >= fs::last_write_time(objPath)) { return true; } fs::file_time_type objTime = fs::last_write_time(objPath); for(ModulePartition* dependency : partitionDependencies) { if(dependency->Check(pcmDir, sourceFloor)) return true; } for(Module* dependency : moduleDependencies) { if(dependency->Check(pcmDir, sourceFloor)) return true; } for(const auto& [externalMod, externalPcmPath] : externalModuleDependencies) { std::error_code ec; fs::file_time_type pcmTime = fs::last_write_time(externalPcmPath, ec); if (!ec && pcmTime >= objTime) return true; } return false; } void Implementation::Schedule(JobGroup& jobs, std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir, const fs::path& object, std::string_view shown) const { 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); } // -MD leaves _impl.o.d beside the object for Check to read, and // -fmodule-file-deps puts the BMIs the unit imported in it with the // headers. The driver adds that for --precompile only, so a std // module rebuilt in place, for a mingw-w64 or libc++ update under // the same compiler, left the objects importing it as they were, and // the next build only relinked them. // // The job makes the directory of its object, which is one for the // source's own directory (ObjectPath): the compiler makes none. jobs.Add(std::format("Compiling {}", shown), [directory = object.parent_path(), command = std::format("{0} {1}.cpp -c -MD -MF {2}.d -fmodule-file-deps -o {2}", clang, path.string(), object.string())] { fs::create_directories(directory); return RunBuildCommand(command); }, after); } }