v2 nearly done

This commit is contained in:
Jorijn van der Graaf 2026-04-27 07:04:42 +02:00
commit f13671b2be
24 changed files with 1467 additions and 314 deletions

View file

@ -20,30 +20,41 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
module Crafter.Build:Interface_impl;
import std;
import :Interface;
import :Command;
import :Platform;
namespace fs = std::filesystem;
namespace Crafter {
ModulePartition::ModulePartition(std::string&& name, fs::path&& path) : name(std::move(name)), path(std::move(path)), compiled(false), checked(false) {}
bool ModulePartition::Check(const fs::path& pcmDir) {
bool ModulePartition::Check(const fs::path& pcmDir, fs::file_time_type sourceFloor) {
if(!checked) {
checked = true;
if(fs::exists((pcmDir/path.filename()).generic_string()+".pcm") && fs::last_write_time(path.generic_string()+".cppm") < fs::last_write_time((pcmDir/path.filename()).generic_string()+".pcm")) {
std::string pcmPath = (pcmDir/path.filename()).generic_string()+".pcm";
std::string cppmPath = path.generic_string()+".cppm";
if(fs::exists(pcmPath) && std::max(fs::last_write_time(cppmPath), sourceFloor) < fs::last_write_time(pcmPath)) {
fs::file_time_type pcmTime = fs::last_write_time(pcmPath);
for(ModulePartition* dependency : partitionDependencies) {
if(dependency->Check(pcmDir)) {
if(dependency->Check(pcmDir, sourceFloor)) {
needsRecompiling = true;
return true;
}
}
for(Module* dependency : moduleDependencies) {
if(dependency->Check(pcmDir)) {
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;
compiled.store(CRAFTER_COMPILE_STATUS_COMPLETED);
compiled.store(true);
return false;
} else {
needsRecompiling = true;
@ -54,10 +65,15 @@ namespace Crafter {
}
}
void ModulePartition::Compile(const std::string_view clang, const fs::path& pcmDir, std::atomic<bool>& buildCancelled, std::string& buildError) {
void ModulePartition::Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir, std::atomic<bool>& buildCancelled, std::string& buildError) {
for(ModulePartition* dependency : partitionDependencies) {
if(!dependency->compiled.load()) {
dependency->compiled.wait(true);
dependency->compiled.wait(false);
}
}
for(Module* dependency : moduleDependencies) {
if(!dependency->compiled.load()) {
dependency->compiled.wait(false);
}
}
@ -70,7 +86,8 @@ namespace Crafter {
std::string result = RunCommand(std::format("{} {}.cppm --precompile -o {}.pcm", clang, path.string(), (pcmDir/path.filename()).string()));
if (!result.empty()) {
if (buildCancelled.compare_exchange_strong(false, true)) {
bool expected = false;
if (buildCancelled.compare_exchange_strong(expected, true)) {
buildError = std::move(result);
compiled.store(true);
compiled.notify_all();
@ -81,10 +98,11 @@ namespace Crafter {
compiled.store(true);
compiled.notify_all();
result = RunClang(std::format("{} -Wno-unused-command-line-argument {}.pcm -c -o {}.o", clang, (pcmDir/path.filename()).string(), (buildDir/path.filename()).string()));
result = RunCommand(std::format("{} -Wno-unused-command-line-argument {}.pcm -c -o {}.o", clang, (pcmDir/path.filename()).string(), (buildDir/path.filename()).string()));
if (!result.empty()) {
if (buildCancelled.compare_exchange_strong(false, true)) {
bool expected = false;
if (buildCancelled.compare_exchange_strong(expected, true)) {
buildError = std::move(result);
}
}
@ -92,13 +110,15 @@ namespace Crafter {
Module::Module(std::string&& name, fs::path&& path) : name(std::move(name)), path(std::move(path)), compiled(false), checked(false) {}
bool Module::Check(const fs::path& pcmDir) {
bool Module::Check(const fs::path& pcmDir, fs::file_time_type sourceFloor) {
if(!checked) {
checked = true;
if(fs::exists((pcmDir/path.filename()).generic_string()+".pcm") && fs::last_write_time(path.generic_string()+".cppm") < fs::last_write_time((pcmDir/path.filename()).generic_string()+".pcm")) {
std::string pcmPath = (pcmDir/path.filename()).generic_string()+".pcm";
std::string cppmPath = path.generic_string()+".cppm";
if(fs::exists(pcmPath) && std::max(fs::last_write_time(cppmPath), sourceFloor) < fs::last_write_time(pcmPath)) {
bool depCheck = false;
for(std::unique_ptr<ModulePartition>& partition : partitions) {
if(partition->Check(pcmDir)) {
if(partition->Check(pcmDir, sourceFloor)) {
depCheck = true;
}
}
@ -112,7 +132,7 @@ namespace Crafter {
}
} else {
for(std::unique_ptr<ModulePartition>& partition : partitions) {
partition->Check(pcmDir);
partition->Check(pcmDir, sourceFloor);
}
needsRecompiling = true;
return true;
@ -124,10 +144,10 @@ namespace Crafter {
void Module::Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir, std::atomic<bool>& buildCancelled, std::string& buildError) {
std::vector<std::thread> threads;
threads.reserve(interface.partitions.size());
for(std::unique_ptr<ModulePartition>& part : interface.partitions) {
if(part.needsRecompiling) {
threads.emplace_back(&ModulePartition::Compile, part.get(), clang, pcmDir, buildDir, buildCancelled, buildError);
threads.reserve(partitions.size());
for(std::unique_ptr<ModulePartition>& part : partitions) {
if(part->needsRecompiling) {
threads.emplace_back(&ModulePartition::Compile, part.get(), clang, pcmDir, buildDir, std::ref(buildCancelled), std::ref(buildError));
}
}
@ -144,7 +164,8 @@ namespace Crafter {
std::string result = RunCommand(std::format("{} {}.cppm --precompile -o {}.pcm", clang, path.string(), (pcmDir/path.filename()).string()));
if (!result.empty()) {
if (buildCancelled.compare_exchange_strong(false, true)) {
bool expected = false;
if (buildCancelled.compare_exchange_strong(expected, true)) {
buildError = std::move(result);
compiled.store(true);
compiled.notify_all();
@ -158,7 +179,8 @@ namespace Crafter {
result = RunCommand(std::format("{} -Wno-unused-command-line-argument {}.pcm -c -o {}.o", clang, (pcmDir/path.filename()).string(), (buildDir/path.filename()).string()));
if (!result.empty()) {
if (buildCancelled.compare_exchange_strong(false, true)) {
bool expected = false;
if (buildCancelled.compare_exchange_strong(expected, true)) {
buildError = std::move(result);
}
}