/* 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; #include "../lib/json.hpp" #include #include module Crafter.Build:Project_impl; import :Project; import std; import :Configuration; import :Command; namespace fs = std::filesystem; namespace Crafter { Project::Project(fs::path&& path) : path(std::move(path)) { if (!fs::exists(this->path)) { throw std::runtime_error(std::format("Project file: {} not found.", this->path.generic_string())); } std::ifstream f(this->path); nlohmann::json data = nlohmann::json::parse(f); name = data["name"].get(); this->path.remove_filename(); nlohmann::json configs = data["configurations"]; nlohmann::json testJson = data["tests"]; if(data.contains("bin_dir")) { binDir = data["bin_dir"].get(); } else { binDir = "bin"; } if(data.contains("build_dir")) { buildDir = data["build_dir"].get(); } else { buildDir = "build"; } if (!fs::exists(binDir)) { fs::create_directories(binDir); } if (!fs::exists(buildDir)) { fs::create_directories(buildDir); } for (nlohmann::json::iterator it = configs.begin(); it != configs.end(); ++it) { configurations.emplace_back(configs, (*it), this->path, *this); } for (nlohmann::json::iterator it = testJson.begin(); it != testJson.end(); ++it) { tests.emplace_back(configs, (*it), this->path, *this); } } Project::Project(fs::path&& path, const std::string_view config) : path(std::move(path)) { if (!fs::exists(this->path)) { throw std::runtime_error(std::format("Project file: {} not found.", path.generic_string())); } std::ifstream f(this->path); nlohmann::json data = nlohmann::json::parse(f); name = data["name"].get(); this->path.remove_filename(); nlohmann::json configs = data["configurations"]; if(data.contains("bin_dir")) { binDir = data["bin_dir"].get(); } else { binDir = "bin"; } if(data.contains("build_dir")) { buildDir = data["build_dir"].get(); } else { buildDir = "build"; } if (!fs::exists(binDir)) { fs::create_directories(binDir); } if (!fs::exists(buildDir)) { fs::create_directories(buildDir); } for (nlohmann::json::iterator it = configs.begin(); it != configs.end(); ++it) { if(it->contains("name")) { if((*it)["name"].get() == config) { configurations.emplace_back(configs, (*it), this->path, *this); goto errorSkip; } } else { throw std::runtime_error("Invalid config json, name field is missing"); } } throw std::runtime_error(std::format("Config {} not found, requested as dependency", config)); errorSkip:; } Project::Project(std::string&& name, fs::path&& path, std::vector&& configurations) : name(std::move(name)), path(std::move(path)), configurations(std::move(configurations)) {} Project::Project(std::string&& name, fs::path&& path, std::vector&& 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)) {} std::tuple Project::Build(std::string_view configuration) { for(Configuration& config : configurations) { if(config.name == configuration){ return {config, Build(config)}; } } throw std::runtime_error(std::format("Configuration: {} not found.", configuration)); } std::tuple 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){ return {config, Build(config)}; } } throw std::runtime_error(std::format("Configuration: {} not found.", configuration)); } BuildResult Project::Build(Configuration& config) const { return Build(config, binDir/config.name, binDir/config.name, buildDir/config.name, name); } void AddLibsRecursive(std::string& libsString, std::unordered_set depLibSet, Configuration& depConfig) { for(const std::string& lib2 : depConfig.libs) { if (depLibSet.insert(lib2).second) { libsString+=std::format(" -l{}", lib2); } } for(const std::tuple, Configuration&>& dep2 : depConfig.dependencies) { std::string outputLib = std::get<0>(dep2)->name+std::get<1>(dep2).name; if (depLibSet.insert(outputLib).second) { libsString+=std::format(" -l{}", outputLib); } AddLibsRecursive(libsString, depLibSet, std::get<1>(dep2)); } } 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); } if (!fs::exists(outputDir)) { fs::create_directories(outputDir); } if (!fs::exists(buildDir)) { fs::create_directories(buildDir); } std::vector threads; for(const Shader& shader : config.shaders) { if(shader.Check(outputDir)) { threads.emplace_back(&Shader::Compile, &shader, outputDir); } } std::thread fileThread([&config, &outputDir](){ for (const fs::path& additionalFile : config.additionalFiles) { fs::path destination = outputDir / additionalFile.filename(); if (fs::is_directory(additionalFile)) { if (!fs::exists(destination)) { fs::copy(additionalFile, destination, fs::copy_options::recursive); } else if (fs::last_write_time(additionalFile) > fs::last_write_time(destination)) { fs::remove_all(destination); fs::copy(additionalFile, destination, fs::copy_options::recursive); } } else { // Handle regular file if (!fs::exists(destination)) { fs::copy(additionalFile, destination); } else if (fs::last_write_time(additionalFile) > fs::last_write_time(destination)) { fs::remove(destination); fs::copy(additionalFile, destination); } } } }); char path[PATH_MAX]; ssize_t count = readlink("/proc/self/exe", path, PATH_MAX); if (count == -1) { throw std::runtime_error("Failed to get executable path"); } path[count] = '\0'; const std::string exeDir = fs::path(path).parent_path().parent_path().string(); std::string command = "clang++"; if(!config.target.empty()) { command += std::format(" --target={}", config.target); if(config.target == "wasm32-wasi") { command += std::format(" --sysroot={}/wasi-sysroot-28.0 -fno-exceptions -fno-c++-static-destructors", exeDir); } } if(!config.march.empty()) { command += std::format(" -march={}", config.march); } if(!config.standard.empty()) { command += std::format(" -std={}", config.standard); } else { command += std::format(" -std=c++26"); } for(const Define& define : config.defines) { command += std::format(" -D {}={}", define.name, define.value); } fs::path pcmDir; if(config.type == CRAFTER_CONFIGURATION_TYPE_SHARED_LIBRARY) { command += " -fPIC -D CRAFTER_BUILD_CONFIGURATION_TYPE_SHARED_LIBRARY"; pcmDir = binDir; } else if(config.type == CRAFTER_CONFIGURATION_TYPE_LIBRARY) { command += " -D CRAFTER_BUILD_CONFIGURATION_TYPE_LIBRARY"; pcmDir = binDir; } else { command += " -D CRAFTER_BUILD_CONFIGURATION_TYPE_EXECUTABLE"; pcmDir = buildDir; } if(config.debug) { command += " -g -D CRAFTER_BUILD_CONFIGURATION_DEBUG"; } else { command += " -O3"; } if(config.target != "wasm32-wasi") { const std::string stdPcm = std::format("{}/std.pcm", exeDir); std::string gccVersion = RunCommand("g++ -dumpversion"); gccVersion.pop_back(); fs::path stdCc = fs::path(std::format("/usr/include/c++/{}/bits/std.cc", gccVersion)); if(!fs::exists(stdPcm) || fs::last_write_time(stdPcm) < fs::last_write_time(stdCc)) { std::string result = RunCommand(std::format("cp {} {}/std.cppm\nclang++ -std=c++26 -Wno-reserved-identifier -Wno-reserved-module-identifier --precompile {}/std.cppm -o {}", stdCc.string(), exeDir, exeDir, stdPcm)); if(result != "") { throw std::runtime_error(result); } } command += std::format(" -fprebuilt-module-path={} -fprebuilt-module-path={}", pcmDir.string(), exeDir); } else { const std::string stdPcm = std::format("{}/wasi-sysroot-28.0/share/libc++/v1/std.pcm", exeDir); fs::path stdCc = fs::path(std::format("{}/wasi-sysroot-28.0/share/libc++/v1/std.cppm", exeDir)); if(!fs::exists(stdPcm) || fs::last_write_time(stdPcm) < fs::last_write_time(stdCc)) { std::string result = RunCommand(std::format("clang++ -fno-exceptions --target=wasm32-wasi -nodefaultlibs --sysroot={}/wasi-sysroot-28.0 -std=c++26 -Wno-reserved-identifier -Wno-reserved-module-identifier -fno-c++-static-destructors --precompile {} -o {}", exeDir, stdCc.string(), stdPcm)); if(result != "") { throw std::runtime_error(result); } } command += std::format(" -fprebuilt-module-path={} -fprebuilt-module-path={}/wasi-sysroot-28.0/share/libc++/v1", pcmDir.string(), exeDir); const fs::path indexPath = std::format("{}/index.html", exeDir); const fs::path indexDstPath = binDir/"index.html"; if(!fs::exists(indexDstPath)) { fs::copy(indexPath, indexDstPath); } else if(fs::last_write_time(indexDstPath) < fs::last_write_time(indexPath)) { fs::remove(indexDstPath); fs::copy(indexPath, indexDstPath); } const fs::path runtimePath = std::format("{}/runtime.js", exeDir); const fs::path runtimeDstPath = binDir/"runtime.js"; if(!fs::exists(runtimeDstPath)) { fs::copy(runtimePath, runtimeDstPath); } else if(fs::last_write_time(runtimeDstPath) < fs::last_write_time(runtimePath)) { fs::remove(runtimeDstPath); fs::copy(runtimePath, runtimeDstPath); } } std::unordered_set depLibSet; std::vector depThreads = std::vector(config.dependencies.size()); std::mutex libMutex; std::string libsString; std::vector 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, &buildResult, &resultsDep](){ std::string outputLib = std::get<0>(config.dependencies[i])->name+std::get<1>(config.dependencies[i]).name; BuildResult depResult = std::get<0>(config.dependencies[i])->Build(std::get<1>(config.dependencies[i]), pcmDir, binDir, buildDir/std::get<0>(config.dependencies[i])->name/std::get<0>(config.dependencies[i])->buildDir, outputLib); libMutex.lock(); if(depResult.repack) { buildResult.repack = true; } resultsDep[i] = depResult.errors; if (depLibSet.insert(outputLib).second) { libsString+=std::format(" -l{}", outputLib); } AddLibsRecursive(libsString, depLibSet, std::get<1>(config.dependencies[i])); libMutex.unlock(); }); } for(std::thread& thread : depThreads) { thread.join(); } for(const std::string& result2 : resultsDep) { buildResult.errors += result2; } if(!buildResult.errors.empty()) { fileThread.join(); return buildResult; } std::string files; for(const fs::path& cFile: config.c_files) { files+=std::format("{}_source.o ",(buildDir/cFile.filename()).string()); if(!fs::exists((buildDir/cFile.filename()).string()+"_source.o") || fs::last_write_time(cFile.string()+".c") > fs::last_write_time((buildDir/cFile.filename()).string()+"_source.o")) { threads.emplace_back(&RunClang, std::format("clang {}.c -c -o {}_source.o", cFile.string(), (buildDir/cFile.filename()).string())); } } std::vector 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(), config.interfaces[i]->path.filename().string()); for(std::unique_ptr& part : config.interfaces[i]->partitions) { files += std::format(" {}/{}.o", buildDir.string(), part->path.filename().string()); } } std::vector resultImplementations(config.implementations.size()); 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(), 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"; } command += libsString; for(const std::string& lib : config.libs) { depLibSet.insert(lib); command += std::format(" -l{}", lib); } fileThread.join(); if(config.dependencies.size() > 0){ command += std::format(" -L{}", pcmDir.string()); } if(buildResult.repack) { if(config.type == CRAFTER_CONFIGURATION_TYPE_EXECUTABLE){ if(config.target == "wasm32-wasi") { outputName += ".wasm"; } buildResult.errors = RunClang(std::format("{}{} -o {} -fuse-ld=lld", command, files, (binDir/outputName).string())); } else if(config.type == CRAFTER_CONFIGURATION_TYPE_LIBRARY){ RunCommandIgnore(std::format("ar r {}.a {}", (binDir/fs::path(std::string("lib")+outputName)).string(), files)); } else { buildResult.errors = RunClang(std::format("{}{} -shared -o {}.so -Wl,-rpath,'$ORIGIN' -fuse-ld=lld", command, files, (binDir/(std::string("lib")+outputName)).string())); } } return buildResult; } std::vector Project::RunTests() { std::vector results; for(Test& test : tests) { results.push_back(RunTest(test)); } return results; } TestResult Project::RunTest(const std::string_view testName) { for(Test& test : tests) { if(test.config.name == testName){ return RunTest(test); } } throw std::runtime_error(std::format("Test: {} not found.", testName)); } TestResult Project::RunTest(Test& test) const { fs::path binDir = path/this->binDir/test.config.name; fs::path buildDir = path/this->buildDir/test.config.name; BuildResult buildResult = Build(test.config, binDir, binDir, buildDir, test.config.name); if(!buildResult.errors.empty()) { return {test.config.name, buildResult.errors}; } std::string lib = std::format("{}/lib{}.so", binDir.string(), test.config.name); void* handle = dlopen(lib.c_str(), RTLD_NOW); if (!handle) { throw std::runtime_error(std::format("Failed to load test library, {}, {}", lib, dlerror())); } dlerror(); typedef std::string* (*RunTestFunc)(); RunTestFunc loadedTest = (RunTestFunc) dlsym(handle, "RunTest"); const char* dlsym_error = dlerror(); if (dlsym_error) { std::string msg = std::string(dlsym_error); dlclose(handle); throw std::runtime_error(std::format("Cannot load symbol 'RunTest': {}", msg)); } std::string* testResult; try { testResult = loadedTest(); } catch(std::exception& e) { return {test.config.name, std::string(e.what())}; } if(testResult != nullptr) { TestResult result = {test.config.name, *testResult}; delete testResult; return result; } else { return {test.config.name, ""}; } return {test.config.name, ""}; } // void AddModule(std::string_view configuration, std::string_view filename); // void AddModule(Configuration& configuration, std::string_view filename); // void AddModuleSourcePair(std::string_view configuration, std::string_view filename); // void AddModuleSourcePair(Configuration& configuration, std::string_view filename); // void AddTest(std::string_view configuration, std::string_view filename, std::string_view content); // void AddTest(Configuration& configuration, std::string_view filename, std::string_view content); // void SaveToJSON(const fs::path& path) const; }