Crafter.Build/Crafter.Build-ModuleFile.cpp

233 lines
9.3 KiB
C++
Raw Normal View History

2024-12-31 20:32:00 +01:00
/*
Crafter.Build
2025-04-29 00:15:14 +02:00
Copyright (C) 2025 Catcrafts®
2024-12-31 20:32:00 +01:00
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 as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
2025-04-29 00:15:14 +02:00
This library is distributed in the hope that it will be useful,
2024-12-31 20:32:00 +01:00
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;
#include <string>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <regex>
#include <print>
#include <execution>
2024-12-31 20:32:00 +01:00
module Crafter.Build;
using namespace Crafter::Build;
namespace fs = std::filesystem;
ModulePartition::ModulePartition(const std::string& name, const fs::path& path, Module* parent, const std::string& fileContent, const fs::path& pcmDir) : name(name), path(path), parent(parent) {
{
std::regex pattern(R"(import :([a-zA-Z_\-0-9\.]*);)");
std::sregex_iterator currentMatch(fileContent.begin(), fileContent.end(), pattern);
std::sregex_iterator lastMatch;
while (currentMatch != lastMatch) {
std::smatch match = *currentMatch;
partitionDependencies.push_back(match[1]);
++currentMatch;
}
}
{
std::regex pattern(R"(import ([a-zA-Z_\-0-9\.]*);)");
std::sregex_iterator currentMatch(fileContent.begin(), fileContent.end(), pattern);
std::sregex_iterator lastMatch;
while (currentMatch != lastMatch) {
std::smatch match = *currentMatch;
moduleDependencies.push_back(match[1]);
++currentMatch;
2024-12-31 20:32:00 +01:00
}
}
if(!fs::exists((pcmDir/path.filename()).generic_string()+".pcm")) {
needsRecompiling = true;
} else if(fs::last_write_time(path.generic_string()+".cppm") > fs::last_write_time((pcmDir/path.filename()).generic_string()+".pcm")) {
needsRecompiling = true;
2024-12-31 20:32:00 +01:00
}
}
void ModulePartition::AddDependants() {
for(const std::string& dependency: partitionDependencies) {
auto pos = parent->partitions.find(dependency);
if (pos != parent->partitions.end()) {
pos->second.partitionDependants.push_back(this);
partitionDependenciesP.push_back(&pos->second);
} else {
throw std::runtime_error(std::format("Partition {}:{} not found", parent->name, dependency));
2024-12-31 20:32:00 +01:00
}
}
}
2024-12-31 20:32:00 +01:00
void ModulePartition::Check() {
if(!needsRecompiling) {
for(ModulePartition* dependency : partitionDependenciesP) {
2025-05-15 15:25:06 +02:00
if(dependency->needsRecompiling) {
2024-12-31 20:32:00 +01:00
needsRecompiling = true;
break;
2024-12-31 20:32:00 +01:00
}
}
}
}
void ModulePartition::Compile(std::string clangDir, const Configuration& config, fs::path pcmDir, std::string target, const std::string& march, const std::string& flags) {
2024-12-31 20:32:00 +01:00
if(needsRecompiling) {
2025-05-15 15:25:06 +02:00
std::string defines;
for(const Define& define : config.defines) {
defines+=define.ToString();
}
for(ModulePartition* dependency : partitionDependenciesP) {
if(dependency->needsRecompiling) {
dependency->compiled->wait(false);
2024-12-31 20:32:00 +01:00
}
}
2025-05-15 15:25:06 +02:00
std::string command = std::format("{} {} {} -std={} {}.cppm --precompile {} -fprebuilt-module-path={} -o {}.pcm {}", clangDir, defines, flags, config.standard, path.generic_string(), march, pcmDir.generic_string(), (pcmDir/path.filename()).generic_string(), target);
if(config.verbose) {
std::cout << command << std::endl;
}
2025-09-09 18:10:54 +02:00
BounceCommand(command.c_str());
}
*compiled = true;
compiled->notify_all();
}
void ModulePartition::CompileSource(std::string clangDir, const Configuration& config, fs::path pcmDir, std::string target, const std::string& march, const std::string& flags, const fs::path& buildDir) {
2025-05-15 15:25:06 +02:00
std::string defines;
for(const Define& define : config.defines) {
defines+=define.ToString();
}
std::string command = std::format("{} {} -std={} {}.pcm -fprebuilt-module-path={} -c -O{} {} {} -o {}.o {}", clangDir, defines, config.standard, (pcmDir/path.filename()).generic_string(), pcmDir.generic_string(), config.optimizationLevel, march, flags, (buildDir/path.filename()).generic_string(), target);
if(config.verbose) {
std::cout << command << std::endl;
}
2025-09-09 18:10:54 +02:00
BounceCommand(command.c_str());
}
2025-02-03 22:06:54 +01:00
Module::Module(const std::string& name, const fs::path& path, const Configuration& config, const fs::path& pcmDir, std::string& files, const fs::path& buildDir) : name(name), path(path) {
for(const fs::path& file: config.moduleFiles) {
std::ifstream t(file.generic_string()+".cppm");
std::stringstream buffer;
buffer << t.rdbuf();
std::string fileContent = buffer.str();
std::regex pattern("export module ([a-zA-Z_\\-0-9\\.]*):([a-zA-Z_\\-0-9\\.]*);");
std::smatch match;
if (std::regex_search(fileContent, match, pattern)) {
if(match[1] == name) {
partitions.insert({match[2], ModulePartition(match[2], file, this, fileContent, pcmDir)});
files += std::format("{}.o ",(buildDir/file.filename()).generic_string());
2025-02-03 22:06:54 +01:00
}
}
}
2025-04-24 19:21:09 +02:00
if(!fs::exists((pcmDir/path.filename()).generic_string()+".pcm")) {
needsRecompiling = true;
} else if(fs::last_write_time(path.generic_string()+".cppm") > fs::last_write_time((pcmDir/path.filename()).generic_string()+".pcm")) {
needsRecompiling = true;
}
2025-02-12 21:35:25 +01:00
2025-04-24 19:21:09 +02:00
for(auto& [key, val] : partitions) {
val.AddDependants();
}
2025-04-24 19:21:09 +02:00
files += std::format("{}.o ",(buildDir/path.filename()).generic_string());
}
2024-12-31 20:32:00 +01:00
void Module::Check() {
for(auto& [key, val] : partitions) {
val.Check();
}
2025-02-12 21:35:25 +01:00
2025-05-15 15:25:06 +02:00
for(auto& [key, val] : partitions) {
if(val.needsRecompiling) {
needsRecompiling = true;
2025-05-15 15:25:06 +02:00
for(auto& [key, val] : partitions) {
val.needsRecompiling = true;
}
break;
2024-12-31 20:32:00 +01:00
}
}
}
void Module::Compile(std::string clangDir, const Configuration& config, fs::path pcmDir, std::string target, const std::string& march, const std::string& flags, const fs::path& buildDir) {
2025-06-10 14:28:25 +02:00
std::string defines;
for(const Define& define : config.defines) {
defines+=define.ToString();
}
std::vector<std::thread> threads;
for(auto& [key, val] : partitions) {;
if(val.needsRecompiling) {
threads.emplace_back([&val, &clangDir, &config, &pcmDir, &target, &march, &flags](){
val.Compile(clangDir, config, pcmDir, target, march, flags);
});
}
}
for(std::thread& thread : threads){
thread.join();
}
{
2025-06-10 14:28:25 +02:00
std::string command = std::format("{} {} {} -std={} {}.cppm --precompile {} -fprebuilt-module-path={} -o {}.pcm {}", clangDir, defines, flags, config.standard, path.generic_string(), march, pcmDir.generic_string(), (pcmDir/path.filename()).generic_string(), target);
if(config.verbose) {
std::cout << command << std::endl;
}
2025-09-09 18:10:54 +02:00
BounceCommand(command.c_str());
}
std::vector<std::thread> threads2;
for(auto& [key, val] : partitions) {;
if(val.needsRecompiling) {
threads2.emplace_back([&val, &clangDir, &config, &pcmDir, &target, &march, &flags, &buildDir](){
val.CompileSource(clangDir, config, pcmDir, target, march, flags, buildDir);
});
}
}
2025-06-10 14:29:20 +02:00
threads2.emplace_back([this, &clangDir, &config, &pcmDir, &target, &march, &flags, &buildDir, &defines](){
2025-06-10 14:28:25 +02:00
std::string command = std::format("{} {} -std={} {}.pcm -fprebuilt-module-path={} -c -O{} {} {} -o {}.o {}", clangDir, defines, config.standard, (pcmDir/path.filename()).generic_string(), pcmDir.generic_string(), config.optimizationLevel, march, flags, (buildDir/path.filename()).generic_string(), target);
if(config.verbose) {
std::cout << command << std::endl;
}
2025-09-09 18:10:54 +02:00
BounceCommand(command.c_str());
});
for(std::thread& thread : threads2){
thread.join();
}
}
std::vector<Module> Module::GetModules(std::string clangDir, const Configuration& config, fs::path pcmDir, std::string target, const std::string& march, const std::string& flags, std::string& files, const fs::path& buildDir) {
std::vector<Module> modules;
for(const fs::path& file: config.moduleFiles) {
std::ifstream t(file.generic_string()+".cppm");
std::stringstream buffer;
buffer << t.rdbuf();
std::string fileContent = buffer.str();
std::regex pattern("export module ([a-zA-Z_\\-0-9\\.]*);");
std::smatch match;
if (std::regex_search(fileContent, match, pattern)) {
modules.emplace_back(match[1], file, config, pcmDir, files, buildDir);
}
}
std::for_each(std::execution::par, modules.begin(), modules.end(), [&clangDir, &config, &pcmDir, &target, &march, &flags, &buildDir](Module& modulee) {
modulee.Check();
modulee.Compile(clangDir, config, pcmDir, target, march, flags, buildDir);
});
return modules;
}