/* 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 as published by the Free Software Foundation; either version 3.0 of the License, or (at your option) any later version. 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 #include #include #include #include #include "json.hpp" #include #include #include #include #include module Crafter.Build; using namespace Crafter::Build; namespace fs = std::filesystem; std::vector mergeUnique(const std::vector& vec1, const std::vector& vec2) { std::unordered_set uniqueElements; std::vector result; for (const auto& str : vec1) { if (uniqueElements.insert(str).second) { result.push_back(str); } } for (const auto& str : vec2) { if (uniqueElements.insert(str).second) { result.push_back(str); } } return result; } Project::Project(std::string name, fs::path path, std::vector configurations) : name(name), path(path), configurations(configurations) { } void Project::Build(std::string configuration) const { for(const Configuration& config : configurations) { if(config.name == configuration){ Build(config); return; } } throw std::runtime_error("Configuration: " + configuration + " not found."); } void Project::Build(std::string configuration, fs::path outputDir) const { for(const Configuration& config : configurations) { if(config.name == configuration){ Build(config, outputDir); return; } } throw std::runtime_error("Configuration: " + configuration + " not found."); } void Project::Build(std::string configuration, fs::path outputDir, fs::path binDir) const { for(const Configuration& config : configurations) { if(config.name == configuration){ Build(config, outputDir, binDir); return; } } throw std::runtime_error("Configuration: " + configuration + " not found."); } void Project::Build(Configuration configuration) const { Build(configuration, configuration.outputDir); } void Project::Build(Configuration config, fs::path outputDir) const { Build(config, outputDir, outputDir); } void Project::Build(Configuration config, fs::path outputDir, fs::path binDir) const { if(config.standard.empty()) { config.standard = "c++26"; } if(config.march.empty()) { config.march = "native"; } if(config.type.empty()) { config.type = "executable"; } if (!fs::exists(config.buildDir)) { fs::create_directory(config.buildDir); } if (!fs::exists(outputDir)) { fs::create_directory(outputDir); } if (!fs::exists(config.buildDir)) { fs::create_directory(config.buildDir); } 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); } fs::path pcmDir; if(config.type == "library" || config.type == "shared-library"){ pcmDir = outputDir; }else{ pcmDir = buildDir; } std::string libs = " -L/usr/local/lib"; if(config.target != "x86_64-w64-mingw64" && config.target != "x86_64-w64-mingw32") { libs += " -L/usr/lib/"; } for(const std::string& path : config.lib_paths) { libs += std::format(" -L{}", path); } for(const std::string& path : config.includeDirs) { libs += std::format(" -I{}", path); } for(Shader& shader : config.shaderFiles) { shader.Compile(outputDir); } std::vector depThreads = std::vector(config.dependencies.size()); if(config.dependencies.size() > 0){ libs += std::format(" -L{}", pcmDir.generic_string()); } for(std::int_fast32_t i = 0; i < depThreads.size(); i++) { if(config.dependencies[i].path.ends_with(".git")) { 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()){ std::cout << std::format("cd {} && git clone {} && cd {} && git checkout {}", buildDir, config.dependencies[i].path, (buildDir/name).generic_string(), config.dependencies[i].commit).c_str() << std::endl; 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 { system(std::format("cd {} && git clone {}", buildDir, config.dependencies[i].path).c_str()); } } else if(config.dependencies[i].commit.empty()) { system(std::format("cd {} && git pull", (buildDir/name).generic_string()).c_str()); } config.dependencies[i].path = fs::path(config.dependencies[i].path).filename().replace_extension(); 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); }); for(const Configuration& config2 : project.configurations) { if(config2.name == config.dependencies[i].configuration){ config.libs = mergeUnique(config.libs, config2.libs); break; } } } else{ Project project = Project::LoadFromJSON(config.dependencies[i].path); libs+=std::format(" -l{}", project.name); depThreads[i] = std::thread([i, pcmDir, config, project, binDir]() { project.Build(config.dependencies[i].configuration, pcmDir, binDir); }); for(const Configuration& config2 : project.configurations) { if(config2.name == config.dependencies[i].configuration){ config.libs = mergeUnique(config.libs, config2.libs); break; } } } } std::string name = this->name; std::string clangDir; if(config.target == "wasm32-unknown-wasi" || config.target == "wasm64-unknown-wasi"){ clangDir = "${WASI_SDK_PATH}/bin/clang++ --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -Wno-unused-command-line-argument -Wl,--export-all -fno-exceptions"; if(config.type != "library") { name+=".wasm"; } } else if(config.target == "wasm32" || config.target == "wasm64") { clangDir = "${WASI_SDK_PATH}/bin/clang++ --no-standard-libraries -Wl,--no-entry -Wl,--export-all -Wno-unused-command-line-argument -fno-exceptions"; if(config.type != "library") { name+=".wasm"; } } else { clangDir = "clang++"; } std::string flags; for(const std::string& flag : config.flags) { flags+=flag; } if(config.debug) { flags+=" -g"; } for(const std::string& lib : config.libs) { libs+= std::format(" -l{}",lib); } for(std::thread& thread : depThreads){ thread.join(); } std::string march; if(config.target != "wasm32-unknown-wasi"){ march = std::format("-march={}", config.march); } std::string files; std::vector modules = Module::GetModules(clangDir, config, pcmDir, target, march, flags, files, buildDir); std::vector threads; 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 ",(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){ thread.join(); } if(config.type == "executable"){ if(config.target == "x86_64-w64-mingw64" || config.target == "x86_64-w64-mingw32") { flags += " -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread"; name += ".exe"; } std::string command = std::format("{} {} {}-O{} -o {} {} {} -fuse-ld=lld", clangDir, flags, files, config.optimizationLevel, (outputDir/name).generic_string(), target, libs); if(config.verbose) { std::cout << command << std::endl; } system(command.c_str()); } else if(config.type == "library"){ system(std::format("ar r {}.a {}", (outputDir/fs::path("lib"+name)).generic_string(), files).c_str()); } else if(config.type == "shared-library"){ system(std::format("ar r {}.so {}", (outputDir/fs::path("lib"+name)).generic_string(), files).c_str()); } for(const fs::path& additionalFile : config.additionalFiles){ if(!fs::exists(binDir/additionalFile.filename())) { fs::copy(additionalFile, binDir); } else if (fs::last_write_time(additionalFile) > fs::last_write_time(binDir/additionalFile.filename())){ fs::remove(binDir/additionalFile.filename()); fs::copy(additionalFile, binDir); } } } Project Project::LoadFromJSON(fs::path path) { if (!fs::exists(path)) { throw std::runtime_error(std::format("Project file: {} not found.", path.generic_string())); } std::ifstream f(path); nlohmann::json data = nlohmann::json::parse(f); const std::string name = data["name"].get(); std::vector configurations; nlohmann::json configs = data["configurations"]; const fs::path workingDir = path.remove_filename(); for (nlohmann::json::iterator it = configs.begin(); it != configs.end(); ++it) { configurations.emplace_back(configs, (*it), workingDir); } return Project(name, workingDir, configurations); }