From e1d5089d237a34c4977cdf5dfa65c9f58b351d11 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 19 Feb 2026 07:49:30 +0100 Subject: chore: build: project infra tweaks. Signed-off-by: Amlal El Mahrouss --- src/lib/JSONManifestBuilder.cpp | 124 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/lib/JSONManifestBuilder.cpp (limited to 'src/lib/JSONManifestBuilder.cpp') diff --git a/src/lib/JSONManifestBuilder.cpp b/src/lib/JSONManifestBuilder.cpp new file mode 100644 index 0000000..7f52f51 --- /dev/null +++ b/src/lib/JSONManifestBuilder.cpp @@ -0,0 +1,124 @@ +// ============================================================= // +// NeBuild +// PURPOSE: JSON support. +// Copyright (C) 2024-2025, Amlal El Mahrouss and NeKernel Authors, licensed under BSD-3 license. +// ============================================================= // + +#include +#include +#include + +namespace NeBuild { +namespace FS = std::filesystem; + +/// =========================================================== /// +/// @brief Builds a nlohmann::json target from a nlohmann::json file. +/// @param arg_sz filename size (must be 1 or greater). +/// @param arg_val filename path (must be a valid nlohmann::json file). +/// @return bool: whether the build has succeeded or not. +/// =========================================================== /// +bool JSONManifestBuilder::BuildTarget(BuildConfig& config) { + std::string path; + + if (config.path_.empty()) { + NeBuild::Logger::info() << "error: file path is empty" << std::endl; + return false; + } else { + path = config.path_; + + if (!FS::exists(path)) { + NeBuild::Logger::info() << "error: file '" << path << "' does not exist" + << std::endl; + return false; + } + } + + try { + std::ifstream json(path); + + if (!json.good()) { + NeBuild::Logger::info() << "error: file '" << path + << "' is not a valid nlohmann::json" << std::endl; + return false; + } + + nlohmann::json json_obj = nlohmann::json::parse(json); + + try { + nlohmann::json description = json_obj["description"]; + + NeBuild::Logger::info() << "package path: " << path << std::endl; + + if (auto res = description.get(); !res.empty()) + NeBuild::Logger::info() << "description: " << res << std::endl; + } catch (...) { + + } + + std::string compiler = json_obj["compiler_path"].get(); + + std::string command = compiler + " "; + + nlohmann::json header_search_path = json_obj["compiler_headers_path"]; + + for (auto& headers : header_search_path) { + command += "-I" + headers.get() + " "; + } + + nlohmann::json headers_path = json_obj["headers_path"]; + + for (auto& headers : headers_path) { + command += "-I" + headers.get() + " "; + } + + nlohmann::json sources_files = json_obj["sources_path"]; + + for (auto& sources : sources_files) { + command += sources.get() + " "; + } + + nlohmann::json macros_list = json_obj["cpp_macros"]; + + for (auto& macro : macros_list) { + command += "-D" + macro.get() + " "; + } + + nlohmann::json compiler_flags = json_obj["compiler_flags"]; + + for (auto& flag : compiler_flags) { + command += flag.get() + " "; + } + + if (json_obj["compiler_std"].is_string()) + command += "-std=" + json_obj["compiler_std"].get() + " "; + + command += "-o " + json_obj["output_name"].get(); + + auto target = json_obj["output_name"].get(); + + NeBuild::Logger::info() << "output path: " << target << "\n"; + + auto ret_exec = std::system(command.c_str()); + + if (ret_exec > 0) { + NeBuild::Logger::info() << "error: exit with message: " << std::strerror(ret_exec) << "" + << std::endl; + config.has_failed_ = true; + return false; + } + } catch (const std::exception& err) { + NeBuild::Logger::info() << "error: exit with message: " << err.what() << "" << std::endl; + config.has_failed_ = true; + return false; + } + + return true; +} + +/// =========================================================== /// +/// @brief Returns the build system name. +/// =========================================================== /// +const std::string_view JSONManifestBuilder::BuildSystem() { + return "NeBuild (nlohmann::json)"; +} +} // namespace NeBuild -- cgit v1.2.3