Crafter.Build/implementations/Crafter.Build-Interface.cpp

188 lines
7.3 KiB
C++
Raw Permalink Normal View History

2026-04-23 01:57:25 +02:00
/*
Crafter® Build
Copyright (C) 2026 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 3.0 as published by the Free Software Foundation;
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
module Crafter.Build:Interface_impl;
import std;
import :Interface;
2026-04-27 07:04:42 +02:00
import :Platform;
2026-04-23 01:57:25 +02:00
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) {}
2026-04-27 07:04:42 +02:00
bool ModulePartition::Check(const fs::path& pcmDir, fs::file_time_type sourceFloor) {
2026-04-23 01:57:25 +02:00
if(!checked) {
checked = true;
2026-04-27 07:04:42 +02:00
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);
2026-04-23 01:57:25 +02:00
for(ModulePartition* dependency : partitionDependencies) {
2026-04-27 07:04:42 +02:00
if(dependency->Check(pcmDir, sourceFloor)) {
2026-04-23 01:57:25 +02:00
needsRecompiling = true;
return true;
}
}
for(Module* dependency : moduleDependencies) {
2026-04-27 07:04:42 +02:00
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) {
2026-04-23 01:57:25 +02:00
needsRecompiling = true;
return true;
}
}
needsRecompiling = false;
2026-04-27 07:04:42 +02:00
compiled.store(true);
2026-04-23 01:57:25 +02:00
return false;
} else {
needsRecompiling = true;
return true;
}
} else {
return needsRecompiling;
}
}
2026-04-27 07:04:42 +02:00
void ModulePartition::Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir, std::atomic<bool>& buildCancelled, std::string& buildError) {
2026-04-23 01:57:25 +02:00
for(ModulePartition* dependency : partitionDependencies) {
if(!dependency->compiled.load()) {
2026-04-27 07:04:42 +02:00
dependency->compiled.wait(false);
}
}
for(Module* dependency : moduleDependencies) {
if(!dependency->compiled.load()) {
dependency->compiled.wait(false);
2026-04-23 01:57:25 +02:00
}
}
if (buildCancelled.load(std::memory_order_relaxed)) {
compiled.store(true);
compiled.notify_all();
return;
}
std::string result = RunCommand(std::format("{} {}.cppm --precompile -o {}.pcm", clang, path.string(), (pcmDir/path.filename()).string()));
if (!result.empty()) {
2026-04-27 07:04:42 +02:00
bool expected = false;
if (buildCancelled.compare_exchange_strong(expected, true)) {
2026-04-23 01:57:25 +02:00
buildError = std::move(result);
compiled.store(true);
compiled.notify_all();
return;
}
}
compiled.store(true);
compiled.notify_all();
2026-04-27 07:04:42 +02:00
result = RunCommand(std::format("{} -Wno-unused-command-line-argument {}.pcm -c -o {}.o", clang, (pcmDir/path.filename()).string(), (buildDir/path.filename()).string()));
2026-04-23 01:57:25 +02:00
if (!result.empty()) {
2026-04-27 07:04:42 +02:00
bool expected = false;
if (buildCancelled.compare_exchange_strong(expected, true)) {
2026-04-23 01:57:25 +02:00
buildError = std::move(result);
}
}
}
Module::Module(std::string&& name, fs::path&& path) : name(std::move(name)), path(std::move(path)), compiled(false), checked(false) {}
2026-04-27 07:04:42 +02:00
bool Module::Check(const fs::path& pcmDir, fs::file_time_type sourceFloor) {
2026-04-23 01:57:25 +02:00
if(!checked) {
checked = true;
2026-04-27 07:04:42 +02:00
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)) {
2026-04-23 01:57:25 +02:00
bool depCheck = false;
for(std::unique_ptr<ModulePartition>& partition : partitions) {
2026-04-27 07:04:42 +02:00
if(partition->Check(pcmDir, sourceFloor)) {
2026-04-23 01:57:25 +02:00
depCheck = true;
}
}
if(depCheck) {
needsRecompiling = true;
return true;
} else {
needsRecompiling = false;
compiled.store(true);
return false;
}
} else {
for(std::unique_ptr<ModulePartition>& partition : partitions) {
2026-04-27 07:04:42 +02:00
partition->Check(pcmDir, sourceFloor);
2026-04-23 01:57:25 +02:00
}
needsRecompiling = true;
return true;
}
} else {
return needsRecompiling;
}
}
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;
2026-04-27 07:04:42 +02:00
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));
2026-04-23 01:57:25 +02:00
}
}
for(std::thread& thread : threads){
thread.join();
}
if (buildCancelled.load(std::memory_order_relaxed)) {
compiled.store(true);
compiled.notify_all();
return;
}
std::string result = RunCommand(std::format("{} {}.cppm --precompile -o {}.pcm", clang, path.string(), (pcmDir/path.filename()).string()));
if (!result.empty()) {
2026-04-27 07:04:42 +02:00
bool expected = false;
if (buildCancelled.compare_exchange_strong(expected, true)) {
2026-04-23 01:57:25 +02:00
buildError = std::move(result);
compiled.store(true);
compiled.notify_all();
return;
}
}
compiled.store(true);
compiled.notify_all();
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()) {
2026-04-27 07:04:42 +02:00
bool expected = false;
if (buildCancelled.compare_exchange_strong(expected, true)) {
2026-04-23 01:57:25 +02:00
buildError = std::move(result);
}
}
}
}