This commit is contained in:
parent
cf08a52f42
commit
52436399e8
9 changed files with 386 additions and 156 deletions
|
|
@ -25,108 +25,194 @@ module;
|
|||
#include <iostream>
|
||||
#include <regex>
|
||||
#include <print>
|
||||
#include <execution>
|
||||
module Crafter.Build;
|
||||
using namespace Crafter::Build;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
|
||||
ModuleFile* ModuleFile::CompileModuleFile(fs::path path, std::string clangDir, const Configuration& config, fs::path pcmDir, std::string target) {
|
||||
if(fs::exists(path.generic_string()+".cppm")) {
|
||||
allFilesMutex.lock();
|
||||
if(!allFiles.contains(path)) {
|
||||
return new ModuleFile(path, clangDir, config, pcmDir, target);
|
||||
} else{
|
||||
allFilesMutex.unlock();
|
||||
return allFiles[path];
|
||||
}
|
||||
} else{
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
ModuleFile::ModuleFile(fs::path path, std::string clangDir, const Configuration& config, fs::path pcmDir, std::string target): path(path), recompiled(false) {
|
||||
fileMutex.lock();
|
||||
allFiles.insert({path,this});
|
||||
allFilesMutex.unlock();
|
||||
needsRecompiling = !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::ifstream t(path.generic_string()+".cppm");
|
||||
std::stringstream buffer;
|
||||
buffer << t.rdbuf();
|
||||
std::string fileContent = buffer.str();
|
||||
|
||||
std::regex rgx("import (.*);");
|
||||
std::smatch matches;
|
||||
std::regex_search(fileContent, matches, rgx);
|
||||
|
||||
std::string::const_iterator searchStart( fileContent.cbegin() );
|
||||
while ( regex_search( searchStart, fileContent.cend(), matches, rgx ) )
|
||||
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::string matchString = matches[1].str();
|
||||
if(matchString[0] == ':'){
|
||||
std::regex rgx2("export module ([a-zA-Z_\\-0-9\\.]*).*;");
|
||||
std::smatch matches2;
|
||||
std::regex_search(fileContent, matches2, rgx2);
|
||||
matchString.erase(0, 1);
|
||||
matchString = matches2[1].str()+"-"+matchString;
|
||||
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;
|
||||
}
|
||||
fs::path importPath = path.remove_filename()/fs::path(matchString);
|
||||
ModuleFile* file = CompileModuleFile(importPath,clangDir, config, pcmDir, target);
|
||||
if(file){
|
||||
file->fileMutex.lock();
|
||||
|
||||
if(file->needsRecompiling){
|
||||
needsRecompiling = true;
|
||||
}
|
||||
|
||||
file->fileMutex.unlock();
|
||||
dependencies.push_back(file);
|
||||
}
|
||||
searchStart = matches.suffix().first;
|
||||
}
|
||||
fileMutex.unlock();
|
||||
Compile(clangDir, config, pcmDir, target);
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
if(!fs::exists((pcmDir/path.filename()).generic_string()+".pcm")) {
|
||||
needsRecompiling = true;
|
||||
needsRecompilingDependency = true;
|
||||
} else if(fs::last_write_time(path.generic_string()+".cppm") > fs::last_write_time((pcmDir/path.filename()).generic_string()+".pcm")) {
|
||||
needsRecompiling = true;
|
||||
needsRecompilingDependency = true;
|
||||
}
|
||||
}
|
||||
|
||||
void ModuleFile::Compile(std::string clangDir, const Configuration& config, fs::path pcmDir, std::string target) {
|
||||
if(needsRecompiling) {
|
||||
fileMutex.lock();
|
||||
if(!recompiled){
|
||||
for(ModuleFile* dep : dependencies){
|
||||
dep->Compile(clangDir, config, pcmDir, target);
|
||||
}
|
||||
|
||||
std::string flags;
|
||||
for(const std::string& flag : config.flags) {
|
||||
flags+=flag;
|
||||
}
|
||||
|
||||
if(config.debug) {
|
||||
flags+=" -g";
|
||||
}
|
||||
|
||||
std::string march;
|
||||
if(config.target != "wasm32-unknown-wasi"){
|
||||
march = std::format("-march={}", config.march);
|
||||
}
|
||||
|
||||
std::string command = std::format("{} {} -std={} {}.cppm --precompile {} -fprebuilt-module-path={} -o {}.pcm {}", clangDir, flags, config.standard, path.generic_string(), march, pcmDir.generic_string(), (pcmDir/path.filename()).generic_string(), target);
|
||||
if(config.verbose) {
|
||||
std::cout << command << std::endl;
|
||||
}
|
||||
system(command.c_str());
|
||||
|
||||
|
||||
|
||||
recompiled = true;
|
||||
fileMutex.unlock();
|
||||
|
||||
command = std::format("{} -std={} {}.pcm -fprebuilt-module-path={} -c -O{} {} {} -o {}.o {}", clangDir, config.standard, (pcmDir/path.filename()).generic_string(), pcmDir.generic_string(), config.optimizationLevel, march, flags, (config.buildDir/path.filename()).generic_string(), target);
|
||||
if(config.verbose) {
|
||||
std::cout << command << std::endl;
|
||||
}
|
||||
system(command.c_str());
|
||||
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 {
|
||||
fileMutex.unlock();
|
||||
throw std::runtime_error(std::format("Partition {}:{} not found", parent->name, dependency));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ModulePartition::Check() {
|
||||
if(!needsRecompiling) {
|
||||
for(ModulePartition* dependency : partitionDependenciesP) {
|
||||
if(dependency->needsRecompilingDependency) {
|
||||
needsRecompiling = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ModulePartition::Compile(std::string clangDir, const Configuration& config, fs::path pcmDir, std::string target, const std::string& march, const std::string& flags) {
|
||||
if(needsRecompiling) {
|
||||
for(ModulePartition* dependency : partitionDependenciesP) {
|
||||
if(dependency->needsRecompiling) {
|
||||
dependency->compiled->wait(false);
|
||||
}
|
||||
}
|
||||
std::string command = std::format("{} {} -std={} {}.cppm --precompile {} -fprebuilt-module-path={} -o {}.pcm {}", clangDir, flags, config.standard, path.generic_string(), march, pcmDir.generic_string(), (pcmDir/path.filename()).generic_string(), target);
|
||||
if(config.verbose) {
|
||||
std::cout << command << std::endl;
|
||||
}
|
||||
system(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) {
|
||||
std::string command = std::format("{} -std={} {}.pcm -fprebuilt-module-path={} -c -O{} {} {} -o {}.o {}", clangDir, 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;
|
||||
}
|
||||
system(command.c_str());
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
for(auto& [key, val] : partitions) {
|
||||
val.AddDependants();
|
||||
}
|
||||
|
||||
files += std::format("{}.o ",(buildDir/path.filename()).generic_string());
|
||||
}
|
||||
|
||||
void Module::Check() {
|
||||
for(auto& [key, val] : partitions) {
|
||||
val.Check();
|
||||
}
|
||||
|
||||
for(auto& [key, val] : partitions) {;
|
||||
if(val.needsRecompilingDependency) {
|
||||
needsRecompiling = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
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();
|
||||
}
|
||||
|
||||
{
|
||||
std::string command = std::format("{} {} -std={} {}.cppm --precompile {} -fprebuilt-module-path={} -o {}.pcm {}", clangDir, flags, config.standard, path.generic_string(), march, pcmDir.generic_string(), (pcmDir/path.filename()).generic_string(), target);
|
||||
if(config.verbose) {
|
||||
std::cout << command << std::endl;
|
||||
}
|
||||
system(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);
|
||||
});
|
||||
}
|
||||
}
|
||||
threads2.emplace_back([this, &clangDir, &config, &pcmDir, &target, &march, &flags, &buildDir](){
|
||||
std::string command = std::format("{} -std={} {}.pcm -fprebuilt-module-path={} -c -O{} {} {} -o {}.o {}", clangDir, 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;
|
||||
}
|
||||
system(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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue