conditional compiling and multithreading
Some checks failed
Main / build (push) Has been cancelled

This commit is contained in:
Jorijn van der Graaf 2025-05-03 01:20:31 +02:00
commit 52436399e8
9 changed files with 386 additions and 156 deletions

View file

@ -28,6 +28,7 @@ module;
#include <filesystem>
#include <thread>
#include <glslang/SPIRV/GlslangToSpv.h>
#include <regex>
module Crafter.Build;
using namespace Crafter::Build;
namespace fs = std::filesystem;
@ -90,8 +91,16 @@ void Project::Build(Configuration config, fs::path outputDir, fs::path binDir) c
}
if (!fs::exists(outputDir)) {
fs::create_directory(outputDir);
} else{
for (const auto& entry : fs::directory_iterator(outputDir)) {
fs::remove_all(entry);
}
}
std::string buildDir = config.buildDir/fs::path(config.name);
if (!fs::exists(buildDir)) {
fs::create_directory(buildDir);
}
std::string target;
if(!config.target.empty()){
target = std::format("-target {}", config.target);
@ -100,7 +109,7 @@ void Project::Build(Configuration config, fs::path outputDir, fs::path binDir) c
if(config.type == "library" || config.type == "shared-library"){
pcmDir = outputDir;
}else{
pcmDir = config.buildDir;
pcmDir = buildDir;
}
@ -130,19 +139,19 @@ void Project::Build(Configuration config, fs::path outputDir, fs::path binDir) c
for(std::int_fast32_t i = 0; i < depThreads.size(); i++) {
if(config.dependencies[i].path.ends_with(".git")) {
std::string branch;
if(!config.dependencies[i].branch.empty()) {
fs::path name = fs::path(config.dependencies[i].path).filename();
name.replace_extension();
branch = std::format(" && cd {} && git switch {}", name.generic_string(), config.dependencies[i].branch);
} else if(!config.dependencies[i].commit.empty()){
fs::path name = fs::path(config.dependencies[i].path).filename();
name.replace_extension();
branch = std::format(" && cd {} && git checkout {}", name.generic_string(), config.dependencies[i].commit);
fs::path name = fs::path(config.dependencies[i].path).filename();
name.replace_extension();
if(!fs::exists(buildDir/name)) {
if(!config.dependencies[i].branch.empty()) {
system(std::format("cd {} && git clone {} && cd {} && git switch {}", buildDir, config.dependencies[i].path, (buildDir/name).generic_string(), config.dependencies[i].branch).c_str());
} else if(!config.dependencies[i].commit.empty()){
system(std::format("cd {} && git clone {} && cd {} && git checkout {}", buildDir, config.dependencies[i].path, (buildDir/name).generic_string(), config.dependencies[i].commit).c_str());
}
} else if(config.dependencies[i].commit.empty()) {
system(std::format("cd {} && git pull", (buildDir/name).generic_string()).c_str());
}
system(std::format("cd {} && git clone {}{}", config.buildDir, config.dependencies[i].path, branch).c_str());
config.dependencies[i].path = fs::path(config.dependencies[i].path).filename().replace_extension();
Project project = Project::LoadFromJSON(fs::path(config.buildDir)/config.dependencies[i].path/"project.json");
Project project = Project::LoadFromJSON(fs::path(buildDir)/config.dependencies[i].path/"project.json");
libs+=std::format(" -l{}", project.name);
depThreads[i] = std::thread([i, pcmDir, config, project, binDir]() {
project.Build(config.dependencies[i].configuration, pcmDir, binDir);
@ -154,7 +163,6 @@ void Project::Build(Configuration config, fs::path outputDir, fs::path binDir) c
project.Build(config.dependencies[i].configuration, pcmDir, binDir);
});
}
}
std::string name = this->name;
@ -171,7 +179,7 @@ void Project::Build(Configuration config, fs::path outputDir, fs::path binDir) c
name+=".wasm";
}
} else {
clangDir = "clang++ -Wno-unused-command-line-argument";
clangDir = "clang++";
}
std::string flags;
@ -191,45 +199,25 @@ void Project::Build(Configuration config, fs::path outputDir, fs::path binDir) c
thread.join();
}
std::string files;
//std::vector<std::thread> moduleThreads(config.moduleFiles.size());
for(std::uint_fast32_t i = 0; i < config.moduleFiles.size(); i++) {
//moduleThreads[i] = std::thread([i, &config, pcmDir, target, clangDir](){
ModuleFile::CompileModuleFile(config.moduleFiles[i], clangDir, config, pcmDir, target);
//});
files+=std::format("{}.o ",(config.buildDir/config.moduleFiles[i].filename()).generic_string());
}
// for(std::thread& thread : moduleThreads){
// thread.join();
// }
std::string march;
if(config.target != "wasm32-unknown-wasi"){
march = std::format("-march={}", config.march);
}
std::string files;
std::vector<Module> modules = Module::GetModules(clangDir, config, pcmDir, target, march, flags, files, buildDir);
std::vector<std::thread> threads;
for(std::uint_fast32_t i = 0; i < config.sourceFiles.size(); i++) {
files+=std::format("{}_source.o ",(config.buildDir/config.sourceFiles[i].filename()).generic_string());
//if(!fs::exists((config.buildDir/config.sourceFiles[i].filename()).generic_string()+"_source.o") || fs::last_write_time(config.sourceFiles[i].generic_string()+".cpp") > fs::last_write_time((config.buildDir/config.sourceFiles[i].filename()).generic_string()+"_source.o")) {
threads.emplace_back([i, &config, pcmDir, target, clangDir, flags, march](){
std::string command = std::format("{} -std={} {}.cpp -fprebuilt-module-path={} -c -O{} {} {} -o {}_source.o {}", clangDir, config.standard, config.sourceFiles[i].generic_string(), pcmDir.generic_string(), config.optimizationLevel, march, flags, (config.buildDir/config.sourceFiles[i].filename()).generic_string(), target);
if(config.verbose) {
std::cout << command << std::endl;
}
system(command.c_str());
});
//}
}
Source::GetSourceFiles(clangDir, config, pcmDir, target, march, flags, threads, modules, files, buildDir);
for(std::uint_fast32_t i = 0; i < config.c_files.size(); i++) {
files+=std::format("{}_source.o ",(config.buildDir/config.c_files[i].filename()).generic_string());
//if(!fs::exists((config.buildDir/config.sourceFiles[i].filename()).generic_string()+"_source.o") || fs::last_write_time(config.sourceFiles[i].generic_string()+".cpp") > fs::last_write_time((config.buildDir/config.sourceFiles[i].filename()).generic_string()+"_source.o")) {
threads.emplace_back([i, &config, pcmDir, target, clangDir, flags, march](){
system(std::format("clang {}.c -c -O{} {} {} -o {}_source.o {}", config.c_files[i].generic_string(), config.optimizationLevel, march, flags, (config.buildDir/config.c_files[i].filename()).generic_string(), target).c_str());
files+=std::format("{}_source.o ",(buildDir/config.c_files[i].filename()).generic_string());
if(!fs::exists((buildDir/config.sourceFiles[i].filename()).generic_string()+"_source.o") || fs::last_write_time(config.sourceFiles[i].generic_string()+".c") > fs::last_write_time((config.buildDir/config.sourceFiles[i].filename()).generic_string()+"_source.o")) {
threads.emplace_back([i, &config, pcmDir, target, clangDir, flags, march, &buildDir](){
system(std::format("clang {}.c -c -O{} {} {} -o {}_source.o {}", config.c_files[i].generic_string(), config.optimizationLevel, march, flags, (buildDir/config.c_files[i].filename()).generic_string(), target).c_str());
});
//}
}
}
for(std::thread& thread : threads){
@ -237,7 +225,6 @@ void Project::Build(Configuration config, fs::path outputDir, fs::path binDir) c
}
if(config.type == "executable"){
//std::cout << std::format("{} {} {}-O{} -o {} {} {} -fuse-ld=lld", clangDir, flags, files, config.optimizationLevel, (outputDir/name).generic_string(), target, libs).c_str() << std::endl;
if(config.target == "x86_64-w64-mingw64" || config.target == "x86_64-w64-mingw32") {
flags += " -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread";
name += ".exe";