threaded exception handling
Some checks failed
demo.yaml / threaded exception handling (push) Failing after 0s

This commit is contained in:
Jorijn van der Graaf 2025-11-15 19:20:33 +01:00
commit c2bb9023d4
14 changed files with 237 additions and 127 deletions

View file

@ -51,40 +51,40 @@ namespace Crafter {
pclose(pipe);
}
CompileException::CompileException(std::vector<CompileError>&& errors) : errors(std::move(errors)) {
for(CompileError error : errors) {
message += std::format("File: {}:{}\nMessage: {}\nCode: {}", error.filename, error.line, error.message, error.code);
}
};
const char* CompileException::what() const noexcept {
return message.c_str();
}
// CompileException::CompileException(std::vector<CompileError>&& errors) : errors(std::move(errors)) {
// for(CompileError error : errors) {
// message += std::format("File: {}:{}\nMessage: {}\nCode: {}", error.filename, error.line, error.message, error.code);
// }
// };
// const char* CompileException::what() const noexcept {
// return message.c_str();
// }
void RunClang(const std::string_view cmd) {
std::cout << cmd << std::endl;
std::string result = RunCommand(cmd);
std::cout << result << std::endl;
// std::vector<CompileError> errors;
std::string RunClang(const std::string_view cmd) {
// std::string result = RunCommand(cmd);
// // std::vector<CompileError> errors;
// std::regex error_regex(R"((/[^:]+\.cpp):(\d+):\d+: error: (.*)\n\s*[0-9| ]*\s*(.*))");
// std::smatch match;
// // std::regex error_regex(R"((/[^:]+\.cpp):(\d+):\d+: error: (.*)\n\s*[0-9| ]*\s*(.*))");
// // std::smatch match;
// while (std::regex_search(result, match, error_regex)) {
// CompileError error;
// error.filename = match[1].str();
// error.line = std::stoi(match[2].str());
// error.message = match[3].str();
// error.code = match[4].str();
// errors.push_back(error);
// result = match.suffix().str();
// // while (std::regex_search(result, match, error_regex)) {
// // CompileError error;
// // error.filename = match[1].str();
// // error.line = std::stoi(match[2].str());
// // error.message = match[3].str();
// // error.code = match[4].str();
// // errors.push_back(error);
// // result = match.suffix().str();
// // }
// if(result != "") {
// // if(errors.size() != 0) {
// // throw CompileException(std::move(errors));
// // } else {
// throw std::runtime_error(result);
// //}
// }
if(result != "") {
// if(errors.size() != 0) {
// throw CompileException(std::move(errors));
// } else {
throw std::runtime_error(result);
//}
}
std::cout << cmd << std::endl;
return RunCommand(cmd);
}
}

View file

@ -45,17 +45,23 @@ namespace Crafter {
return true;
}
}
void Implementation::Compile(const std::string_view clang, const fs::path& buildDir) const {
void Implementation::Compile(const std::string_view clang, const fs::path& buildDir, std::string& result) const {
for(ModulePartition* dependency : partitionDependencies) {
if(!dependency->compiled.load()) {
dependency->compiled.wait(false);
dependency->compiled.wait(CRAFTER_COMPILE_STATUS_WAITING);
if(dependency->compiled.load() == CRAFTER_COMPILE_STATUS_ERROR) {
return;
}
}
}
for(Module* dependency : moduleDependencies) {
if(!dependency->compiled.load()) {
dependency->compiled.wait(false);
dependency->compiled.wait(CRAFTER_COMPILE_STATUS_WAITING);
if(dependency->compiled.load() == CRAFTER_COMPILE_STATUS_ERROR) {
return;
}
}
}
RunClang(std::format("{} {}.cpp -c -o {}_impl.o", clang, path.string(), (buildDir/path.filename()).string()));
return RunClang(std::format("{} {}.cpp -c -o {}_impl.o", clang, path.string(), (buildDir/path.filename()).string()));
}
}

View file

@ -24,7 +24,7 @@ import :Command;
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) {}
ModulePartition::ModulePartition(std::string&& name, fs::path&& path) : name(std::move(name)), path(std::move(path)), compiled(CRAFTER_COMPILE_STATUS_WAITING), checked(false) {}
bool ModulePartition::Check(const fs::path& pcmDir) {
if(!checked) {
@ -43,7 +43,7 @@ namespace Crafter {
}
}
needsRecompiling = false;
compiled.store(true);
compiled.store(CRAFTER_COMPILE_STATUS_COMPLETED);
return false;
} else {
needsRecompiling = true;
@ -54,24 +54,34 @@ namespace Crafter {
}
}
void ModulePartition::Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir) {
void ModulePartition::Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir, std::string& result) {
for(ModulePartition* dependency : partitionDependencies) {
if(!dependency->compiled.load()) {
//std::cout << std::format("{} is waiting on {} {}", name, dependency->name, dependency->needsRecompiling) << std::endl;
dependency->compiled.wait(false);
dependency->compiled.wait(CRAFTER_COMPILE_STATUS_WAITING);
if(dependency->compiled.load() == CRAFTER_COMPILE_STATUS_ERROR) {
compiled.store(CRAFTER_COMPILE_STATUS_ERROR);
compiled.notify_all();
return;
}
}
}
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()));
compiled.store(true);
compiled.notify_all();
result += RunClang(std::format("{} {}.cppm --precompile -o {}.pcm", clang, path.string(), (pcmDir/path.filename()).string()));
result += RunClang(std::format("{} {}.pcm -c -o {}.o", clang, (pcmDir/path.filename()).string(), (buildDir/path.filename()).string()));
if(result.empty()) {
compiled.store(CRAFTER_COMPILE_STATUS_COMPLETED);
compiled.notify_all();
} else {
compiled.store(CRAFTER_COMPILE_STATUS_ERROR);
compiled.notify_all();
}
}
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) {}
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(CRAFTER_COMPILE_STATUS_WAITING), checked(false) {}
Module::Module(std::string&& name, fs::path&& path) : name(std::move(name)), path(std::move(path)), compiled(false), checked(false) {}
Module::Module(std::string&& name, fs::path&& path) : name(std::move(name)), path(std::move(path)), compiled(CRAFTER_COMPILE_STATUS_WAITING), checked(false) {}
Module::Module(fs::path&& path) : path(std::move(path)), compiled(false), checked(false) {
Module::Module(fs::path&& path) : path(std::move(path)), compiled(CRAFTER_COMPILE_STATUS_WAITING), checked(false) {
std::ifstream t(this->path);
std::stringstream buffer;
buffer << t.rdbuf();
@ -101,7 +111,7 @@ namespace Crafter {
return true;
} else {
needsRecompiling = false;
compiled.store(true);
compiled.store(CRAFTER_COMPILE_STATUS_COMPLETED);
return false;
}
} else {
@ -115,14 +125,13 @@ namespace Crafter {
return needsRecompiling;
}
}
void Module::Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir) {
void Module::Compile(const std::string_view clang, const fs::path& pcmDir, const fs::path& buildDir, std::string& result) {
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);
});
std::vector<std::string> results(partitions.size());
for(std::uint_fast32_t i = 0; i < partitions.size(); i++) {
if( partitions[i]->needsRecompiling) {
threads.emplace_back(&ModulePartition::Compile, partitions[i].get(), clang, pcmDir, buildDir, std::ref(results[i]));
}
}
@ -130,10 +139,23 @@ namespace Crafter {
thread.join();
}
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()));
for(const std::string& result2 : results) {
result += result2;
}
compiled.store(true);
compiled.notify_all();
if(result.empty()) {
result += RunClang(std::format("{} {}.cppm --precompile -o {}.pcm", clang, path.string(), (pcmDir/path.filename()).string()));
result += RunClang(std::format("{} {}.pcm -c -o {}.o", clang, (pcmDir/path.filename()).string(), (buildDir/path.filename()).string()));
if(result.empty()) {
compiled.store(CRAFTER_COMPILE_STATUS_COMPLETED);
compiled.notify_all();
} else {
compiled.store(CRAFTER_COMPILE_STATUS_ERROR);
compiled.notify_all();
}
} else {
compiled.store(CRAFTER_COMPILE_STATUS_ERROR);
compiled.notify_all();
}
}
}

View file

@ -31,28 +31,28 @@ namespace Crafter {
Project::Project(std::string&& name, fs::path&& path, std::vector<Configuration>&& configurations) : name(std::move(name)), path(std::move(path)), configurations(std::move(configurations)) {}
Project::Project(std::string&& name, fs::path&& path, std::vector<Configuration>&& configurations, fs::path&& binDir, fs::path&& buildDir) : name(std::move(name)), path(std::move(path)), configurations(std::move(configurations)), binDir(std::move(binDir)), buildDir(std::move(buildDir)) {}
Configuration& Project::Build(std::string_view configuration) {
std::tuple<Configuration&, BuildResult> Project::Build(std::string_view configuration) {
for(Configuration& config : configurations) {
if(config.name == configuration){
Build(config);
return config;
return {config, Build(config)};
}
}
throw std::runtime_error(std::format("Configuration: {} not found.", configuration));
}
Configuration& Project::Build(std::string_view configuration, const fs::path& binDir, const fs::path& outputDir, const fs::path& buildDir, std::string outputName) {
std::tuple<Configuration&, BuildResult> Project::Build(std::string_view configuration, const fs::path& binDir, const fs::path& outputDir, const fs::path& buildDir, std::string outputName) {
for(Configuration& config : configurations) {
if(config.name == configuration){
Build(config, binDir, binDir, outputDir, outputName);
return config;
return {config, Build(config)};
}
}
throw std::runtime_error(std::format("Configuration: {} not found.", configuration));
}
void Project::Build(Configuration& config) const {
Build(config, binDir/config.name, binDir/config.name, buildDir/config.name, name);
BuildResult Project::Build(Configuration& config) const {
return Build(config, binDir/config.name, binDir/config.name, buildDir/config.name, name);
}
void Project::Build(Configuration& config, const fs::path& binDir, const fs::path& outputDir, const fs::path& buildDir, std::string outputName) const {
BuildResult Project::Build(Configuration& config, const fs::path& binDir, const fs::path& outputDir, const fs::path& buildDir, std::string outputName) const {
BuildResult buildResult;
if (!fs::exists(binDir)) {
fs::create_directories(binDir);
}
@ -75,7 +75,6 @@ namespace Crafter {
std::thread fileThread([&config, &outputDir](){
for (const fs::path& additionalFile : config.additionalFiles) {
fs::path destination = outputDir / additionalFile.filename();
std::cout << destination << std::endl;
if (fs::is_directory(additionalFile)) {
if (!fs::exists(destination)) {
@ -190,9 +189,10 @@ namespace Crafter {
std::vector<std::thread> depThreads = std::vector<std::thread>(config.dependencies.size());
std::mutex libMutex;
std::string libsString;
std::vector<std::string> resultsDep(config.dependencies.size());
for(std::uint_fast32_t i = 0; i < depThreads.size(); i++) {
depThreads[i] = std::thread([i, &config, &libMutex, &depLibSet, &buildDir, &pcmDir, &libsString, &binDir, this](){
depThreads[i] = std::thread([i, &config, &libMutex, &depLibSet, &buildDir, &pcmDir, &libsString, &binDir, this, &buildResult, &resultsDep](){
if(config.dependencies[i].path.ends_with(".git")) {
fs::path name = fs::path(config.dependencies[i].path).filename();
name.replace_extension();
@ -216,8 +216,13 @@ namespace Crafter {
for(Configuration& depConfig : project.configurations) {
if(depConfig.name == config.dependencies[i].configuration){
fs::path depBuildDir = fs::path(config.dependencies[i].path).parent_path()/project.buildDir/depConfig.name;
project.Build(depConfig, pcmDir, binDir, depBuildDir, project.name);
BuildResult depResult = project.Build(depConfig, pcmDir, binDir, depBuildDir, project.name);
libMutex.lock();
if(depResult.repack) {
buildResult.repack = true;
}
resultsDep[i] = depResult.errors;
if (depLibSet.insert(project.name).second) {
libsString+=std::format(" -l{}", project.name);
}
@ -231,6 +236,7 @@ namespace Crafter {
libsString+=std::format(" -l{}", project.name);
}
}
libMutex.unlock();
return;
}
@ -243,31 +249,55 @@ namespace Crafter {
thread.join();
}
for(const std::string& result2 : resultsDep) {
buildResult.errors += result2;
}
if(!buildResult.errors.empty()) {
fileThread.join();
return buildResult;
}
std::string files;
bool repack = false;
for(std::unique_ptr<Module>& modulee : config.interfaces) {
if(modulee->Check(pcmDir)) {
threads.emplace_back(&Module::Compile, modulee.get(), command, pcmDir, buildDir);
repack = true;
std::vector<std::string> resultInterfaces(config.interfaces.size());
for(uint_fast32_t i = 0; i < config.interfaces.size(); i++) {
if(config.interfaces[i]->Check(pcmDir)) {
threads.emplace_back(&Module::Compile, config.interfaces[i].get(), command, pcmDir, buildDir, std::ref(resultInterfaces[i]));
buildResult.repack = true;
}
files += std::format(" {}/{}.o", buildDir.string(), modulee->path.filename().string());
for(std::unique_ptr<ModulePartition>& part : modulee->partitions) {
files += std::format(" {}/{}.o", buildDir.string(), config.interfaces[i]->path.filename().string());
for(std::unique_ptr<ModulePartition>& part : config.interfaces[i]->partitions) {
files += std::format(" {}/{}.o", buildDir.string(), part->path.filename().string());
}
}
std::vector<std::string> resultImplementations(config.implementations.size());
for(const Implementation& implementation : config.implementations) {
if(implementation.Check(buildDir, pcmDir)) {
repack = true;
threads.emplace_back(&Implementation::Compile, &implementation, command, buildDir);
for(uint_fast32_t i = 0; i < config.implementations.size(); i++) {
if(config.implementations[i].Check(buildDir, pcmDir)) {
buildResult.repack = true;
threads.emplace_back(&Implementation::Compile, &config.implementations[i], command, buildDir, std::ref(resultImplementations[i]));
}
files += std::format(" {}/{}_impl.o", buildDir.string(), implementation.path.filename().string());
files += std::format(" {}/{}_impl.o", buildDir.string(), config.implementations[i].path.filename().string());
}
for(std::thread& thread : threads) {
thread.join();
}
for(const std::string& result2 : resultInterfaces) {
buildResult.errors += result2;
}
for(const std::string& result2 : resultImplementations) {
buildResult.errors += result2;
}
if(!buildResult.errors.empty()) {
fileThread.join();
return buildResult;
}
if(config.target != "wasm32-wasi") {
command += " -L/usr/local/lib";
}
@ -285,19 +315,21 @@ namespace Crafter {
command += std::format(" -L{}", pcmDir.string());
}
if(repack) {
if(buildResult.repack) {
if(config.type == CRAFTER_CONFIGURATION_TYPE_EXECUTABLE){
if(config.target == "wasm32-wasi") {
outputName += ".wasm";
}
RunClang(std::format("{}{} -o {} -fuse-ld=lld", command, files, (binDir/outputName).string()));
buildResult.errors = RunClang(std::format("{}{} -o {} -fuse-ld=lld", command, files, (binDir/outputName).string()));
} else if(config.type == CRAFTER_CONFIGURATION_TYPE_LIBRARY){
std::cout << std::format("ar r {}.a {}", (binDir/fs::path(std::string("lib")+outputName)).string(), files) << std::endl;
RunCommandIgnore(std::format("ar r {}.a {}", (binDir/fs::path(std::string("lib")+outputName)).string(), files));
} else {
RunClang(std::format("{}{} -shared -o {}.so -Wl,-rpath,'$ORIGIN' -fuse-ld=lld", command, files, (binDir/(std::string("lib")+outputName)).string()));
buildResult.errors = RunClang(std::format("{}{} -shared -o {}.so -Wl,-rpath,'$ORIGIN' -fuse-ld=lld", command, files, (binDir/(std::string("lib")+outputName)).string()));
}
}
return buildResult;
}
Project Project::LoadFromJSON(const fs::path& path) {

View file

@ -69,18 +69,25 @@ int main(int argc, char* argv[]) {
Project project = Project::LoadFromJSON(projectPath);
if(command == "build") {
Configuration& config = project.Build(argument);
if(run){
std::string binDir = std::format("{}/{}", project.binDir.string(), config.name);
if(config.debug) {
system(std::format("cd {} && ./{}", (fs::path(projectPath).parent_path()/binDir).string(), project.name).c_str());
//system(std::format("cd {} && lldb -o run {}", (fs::path(projectPath).parent_path()/binDir).string(), project.name).c_str());
} else {
system(std::format("cd {} && ./{}", (fs::path(projectPath).parent_path()/binDir).string(), project.name).c_str());
std::tuple<Configuration&, BuildResult> config = project.Build(argument);
std::cout << "amogus" << std::endl;
if(std::get<1>(config).errors.empty()) {
if(run){
std::string binDir = std::format("{}/{}", project.binDir.string(), std::get<0>(config).name);
if(std::get<0>(config).debug) {
system(std::format("cd {} && ./{}", (fs::path(projectPath).parent_path()/binDir).string(), project.name).c_str());
//system(std::format("cd {} && lldb -o run {}", (fs::path(projectPath).parent_path()/binDir).string(), project.name).c_str());
} else {
system(std::format("cd {} && ./{}", (fs::path(projectPath).parent_path()/binDir).string(), project.name).c_str());
}
}
return 0;
} else {
std::cout << std::get<1>(config).errors << std::endl;
return 1;
}
} else if(command == "test") {
bool anyFailed = false;
if(argument.empty()) {
std::vector<TestResult> results = project.RunTests();
for(const TestResult& result : results) {
@ -88,6 +95,7 @@ int main(int argc, char* argv[]) {
std::cout << std::format("✅ {}", result.name) << std::endl;
} else {
std::cout << std::format("❌ {}\t{}", result.name, result.message) << std::endl;
anyFailed = true;
}
}
} else {
@ -96,8 +104,11 @@ int main(int argc, char* argv[]) {
std::cout << std::format("✅ {}", result.name) << std::endl;
} else {
std::cout << std::format("❌ {}\t{}", result.name, result.message) << std::endl;
anyFailed = true;
}
}
return anyFailed ? 1 : 0;
} else {
std::println("Unkown command: {}", command);
}