Crafter.Build/implementations/Crafter.Build-Module.cpp

139 lines
5.8 KiB
C++
Raw Normal View History

2025-10-31 16:50:47 +01:00
/*
Crafter® Build
Copyright (C) 2025 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:Module_impl;
import std;
import :Module;
import :Command;
namespace fs = std::filesystem;
namespace Crafter {
2025-11-01 11:53:09 +01:00
ModulePartition::ModulePartition(std::string&& name, fs::path&& path) : name(std::move(name)), path(std::move(path)), compiled(false), checked(false) {}
2025-10-31 16:50:47 +01:00
bool ModulePartition::Check(const fs::path& pcmDir) {
if(!checked) {
2025-11-01 11:53:09 +01:00
checked = true;
2025-10-31 16:50:47 +01:00
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")) {
for(ModulePartition* dependency : partitionDependencies) {
if(dependency->Check(pcmDir)) {
needsRecompiling = true;
return true;
}
}
for(Module* dependency : moduleDependencies) {
if(dependency->Check(pcmDir)) {
needsRecompiling = true;
return true;
}
}
needsRecompiling = false;
compiled.store(true);
return false;
} else {
needsRecompiling = true;
return true;
}
} else {
return needsRecompiling;
}
}
void ModulePartition::Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir) {
for(ModulePartition* dependency : partitionDependencies) {
if(!dependency->compiled.load()) {
2025-11-01 11:53:09 +01:00
//std::cout << std::format("{} is waiting on {} {}", name, dependency->name, dependency->needsRecompiling) << std::endl;
2025-10-31 16:50:47 +01:00
dependency->compiled.wait(false);
}
}
2025-11-01 06:50:41 +01:00
RunClang(std::format("{} {}.cppm --precompile -o {}.pcm", clang, path.string(), (pcmDir/path.filename()).string()));
RunClang(std::format("{} {}.pcm -c -o {}.o", clang, (pcmDir/path.filename()).string(), (buildDir/path.filename()).string()));
2025-10-31 16:50:47 +01:00
compiled.store(true);
compiled.notify_all();
}
2025-11-01 11:53:09 +01:00
Module::Module(std::string&& name, fs::path&& path, std::vector<std::unique_ptr<ModulePartition>>&& partitions) : name(std::move(name)), path(std::move(path)), partitions(std::move(partitions)), compiled(false), checked(false) {}
2025-10-31 16:50:47 +01:00
2025-11-01 11:53:09 +01:00
Module::Module(std::string&& name, fs::path&& path) : name(std::move(name)), path(std::move(path)), compiled(false), checked(false) {}
2025-10-31 16:50:47 +01:00
2025-11-01 11:53:09 +01:00
Module::Module(fs::path&& path) : path(std::move(path)), compiled(false), checked(false) {
2025-10-31 16:50:47 +01:00
std::ifstream t(this->path);
std::stringstream buffer;
buffer << t.rdbuf();
std::string fileContent = buffer.str();
fileContent = std::regex_replace(fileContent, std::regex(R"(//[^\n]*)"), "");
fileContent = std::regex_replace(fileContent, std::regex(R"(/\*.*?\*/)"), "");
std::smatch match;
if (std::regex_search(fileContent, match, std::regex(R"(export module ([a-zA-Z0-9_.-]+);)"))) {
name = match[1].str();
} else {
throw std::runtime_error(std::format("No module declaration found in {}", this->path.string()));
}
}
bool Module::Check(const fs::path& pcmDir) {
if(!checked) {
2025-11-01 11:53:09 +01:00
checked = true;
2025-10-31 16:50:47 +01:00
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")) {
2025-11-01 11:53:09 +01:00
bool depCheck = false;
2025-10-31 16:50:47 +01:00
for(std::unique_ptr<ModulePartition>& partition : partitions) {
if(partition->Check(pcmDir)) {
2025-11-01 11:53:09 +01:00
depCheck = true;
2025-10-31 16:50:47 +01:00
}
}
2025-11-01 11:53:09 +01:00
if(depCheck) {
needsRecompiling = true;
return true;
} else {
needsRecompiling = false;
compiled.store(true);
return false;
}
2025-10-31 16:50:47 +01:00
} else {
for(std::unique_ptr<ModulePartition>& partition : partitions) {
partition->Check(pcmDir);
}
needsRecompiling = true;
return true;
}
} else {
return needsRecompiling;
}
}
void Module::Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir) {
std::vector<std::thread> threads;
for(std::unique_ptr<ModulePartition>& partition : partitions) {
if(partition->needsRecompiling) {
threads.emplace_back([&partition, clang, &pcmDir, &buildDir](){
partition->Compile(clang, pcmDir, buildDir);
});
}
}
for(std::thread& thread : threads){
thread.join();
}
2025-11-01 06:50:41 +01:00
RunClang(std::format("{} {}.cppm --precompile -o {}.pcm", clang, path.string(), (pcmDir/path.filename()).string()));
RunClang(std::format("{} {}.pcm -c -o {}.o", clang, (pcmDir/path.filename()).string(), (buildDir/path.filename()).string()));
2025-10-31 16:50:47 +01:00
compiled.store(true);
compiled.notify_all();
}
}