/* Crafter.Build Copyright (C) 2024 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 "json.hpp" #include module Crafter.Build; using namespace Crafter::Build; namespace fs = std::filesystem; Configuration::Configuration(std::string name, std::string standard, std::vector sourceFiles, std::vector moduleFiles, std::string optimizationLevel, std::string buildDir, std::string outputDir, std::string type, std::string target, std::string march, std::vector dependencies, std::vector additionalFiles, std::vector flags, bool debug, std::vector libs, std::vector lib_paths, std::vector c_files, std::vector shaderFiles, std::vector includeDirs, bool verbose): name(name), standard(standard), sourceFiles(sourceFiles), moduleFiles(moduleFiles), optimizationLevel(optimizationLevel), buildDir(buildDir), outputDir(outputDir), type(type), target(target), march(march), dependencies(dependencies), additionalFiles(additionalFiles), flags(flags), debug(debug), libs(libs), lib_paths(lib_paths), c_files(c_files), shaderFiles(shaderFiles), includeDirs(includeDirs), verbose(verbose) { } Configuration::Configuration(const nlohmann::json& configs, const nlohmann::json& config, fs::path workingDir) { name = config["name"].get(); for (auto& [key, val] : config.items()) { if(key == "standard"){ standard = val.get(); } else if(key == "target") { target = val.get(); } else if(key == "debug") { debug = val.get(); } else if(key == "verbose") { verbose = val.get(); } else if(key == "type") { type = val.get(); } else if(key == "march") { march = val.get(); } else if(key == "source_files") { const std::vector tempSourceFiles = val.get>(); sourceFiles = std::vector(tempSourceFiles.size()); for(std::int_fast32_t i = 0; i < sourceFiles.size(); i++){ const std::filesystem::path filePath (tempSourceFiles[i]); const std::filesystem::path fullFilePath = workingDir / filePath; sourceFiles[i] = fullFilePath.generic_string(); } } else if(key == "c_files") { const std::vector tempSourceFiles = val.get>(); c_files = std::vector(tempSourceFiles.size()); for(std::int_fast32_t i = 0; i < c_files.size(); i++){ const std::filesystem::path filePath (tempSourceFiles[i]); const std::filesystem::path fullFilePath = workingDir / filePath; c_files[i] = fullFilePath.generic_string(); } } else if(key == "flags") { flags = val.get>(); } else if(key == "lib_paths") { lib_paths = val.get>(); } else if(key == "include_dirs") { includeDirs = val.get>(); } else if(key == "libs") { libs = val.get>(); } else if(key == "module_files") { const std::vector tempModuleFiles = val.get>(); moduleFiles = std::vector(tempModuleFiles.size()); for(std::int_fast32_t i = 0; i < moduleFiles.size(); i++){ const std::filesystem::path filePath (tempModuleFiles[i]); const std::filesystem::path fullFilePath = workingDir / filePath; moduleFiles[i] = fullFilePath.generic_string(); } } else if(key == "shaders") { for (auto it : val) { const std::filesystem::path filePath (it["path"].get()); const std::filesystem::path fullFilePath = workingDir / filePath; shaderFiles.emplace_back(fullFilePath, it["entrypoint"].get(), static_cast(it["type"].get())); } } else if(key == "additional_files") { const std::vector tempAdditionalFiles = val.get>(); additionalFiles = std::vector(tempAdditionalFiles.size()); for(std::int_fast32_t i = 0; i < additionalFiles.size(); i++){ const std::filesystem::path filePath (tempAdditionalFiles[i]); const std::filesystem::path fullFilePath = workingDir / filePath; additionalFiles[i] = fullFilePath.generic_string(); } } else if(key == "optimization_level") { optimizationLevel = val.get(); } else if(key == "build_dir") { const std::string tempBuildDir = val.get(); const std::filesystem::path buildPath (tempBuildDir); const std::filesystem::path fullBuildPath = workingDir / buildPath; buildDir = fullBuildPath.generic_string(); } else if(key == "output_dir") { const std::string tempOutputDir = val.get(); const std::filesystem::path outputPath (tempOutputDir); const std::filesystem::path fullOutputPath = workingDir / outputPath; outputDir = fullOutputPath.generic_string(); } else if(key == "dependencies") { for (auto it : val) { std::string commit; std::string branch; if(it.contains("commit")){ commit = it["commit"].get(); } if(it.contains("branch")){ branch = it["branch"].get(); } dependencies.emplace_back(it["path"].get(), it["configuration"].get(), commit, branch); } } else if(key != "extends") { additionalProperties.insert({key, val}); } } if(config.contains("extends")) { const std::vector extends = config["extends"].get>(); for(const std::string& extendName : extends) { for (auto it : configs) { if(it["name"].get() == extendName) { Configuration extendData = Configuration(configs, it, workingDir); if(!extendData.standard.empty() && standard.empty()){ standard = extendData.standard; } if(!extendData.sourceFiles.empty()){ sourceFiles.insert(sourceFiles.end(), extendData.sourceFiles.begin(), extendData.sourceFiles.end()); } if(!extendData.moduleFiles.empty()){ moduleFiles.insert(moduleFiles.end(), extendData.moduleFiles.begin(), extendData.moduleFiles.end()); } if(!extendData.additionalFiles.empty()){ additionalFiles.insert(additionalFiles.end(), extendData.additionalFiles.begin(), extendData.additionalFiles.end()); } if(!extendData.optimizationLevel.empty() && optimizationLevel.empty()){ optimizationLevel = extendData.optimizationLevel; } if(!extendData.dependencies.empty()){ dependencies.insert(dependencies.end(), extendData.dependencies.begin(), extendData.dependencies.end()); } if(!extendData.buildDir.empty() && buildDir.empty()) { buildDir = extendData.buildDir; } if(!extendData.outputDir.empty() && outputDir.empty()) { outputDir = extendData.outputDir; } if(!extendData.target.empty() && target.empty()) { target = extendData.target; } if(!extendData.type.empty() && type.empty()) { type = extendData.type; } if(!extendData.march.empty() && march.empty()) { march = extendData.march; } if(!extendData.lib_paths.empty()){ lib_paths.insert(lib_paths.end(), extendData.lib_paths.begin(), extendData.lib_paths.end()); } if(!extendData.c_files.empty()){ c_files.insert(c_files.end(), extendData.c_files.begin(), extendData.c_files.end()); } if(!extendData.flags.empty()){ flags.insert(flags.end(), extendData.flags.begin(), extendData.flags.end()); } if(extendData.debug){ debug = true; } if(extendData.verbose){ verbose = true; } if(!extendData.libs.empty()){ libs.insert(libs.end(), extendData.libs.begin(), extendData.libs.end()); } if(!extendData.shaderFiles.empty()){ shaderFiles.insert(shaderFiles.end(), extendData.shaderFiles.begin(), extendData.shaderFiles.end()); } if(!extendData.includeDirs.empty()){ includeDirs.insert(includeDirs.end(), extendData.includeDirs.begin(), extendData.includeDirs.end()); } break; } } } } }