diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2026-02-27 22:43:34 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2026-02-27 22:43:34 +0100 |
| commit | fe03952bf6dbf32509a9322e34ac1b5c9d0dbe25 (patch) | |
| tree | 04bb502e93321c08cbc1feb8ec8a9c42bf33896b /src/NeBuildKit | |
| parent | 8e27a0f610819130a91a4076eaab1ab657ab3c72 (diff) | |
chore: update NeBuild file structure.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'src/NeBuildKit')
| -rw-r--r-- | src/NeBuildKit/IManifestBuilder.cpp | 6 | ||||
| -rw-r--r-- | src/NeBuildKit/JSONManifestBuilder.cpp | 125 | ||||
| -rw-r--r-- | src/NeBuildKit/TOMLManifestBuilder.cpp | 130 |
3 files changed, 261 insertions, 0 deletions
diff --git a/src/NeBuildKit/IManifestBuilder.cpp b/src/NeBuildKit/IManifestBuilder.cpp new file mode 100644 index 0000000..2a61ed0 --- /dev/null +++ b/src/NeBuildKit/IManifestBuilder.cpp @@ -0,0 +1,6 @@ +// ============================================================= // +// NeBuild +// Copyright (C) 2024-2025, Amlal El Mahrouss and NeKernel Authors, licensed under BSD-3 license. +// ============================================================= // + +#include <NeBuildKit/IManifestBuilder.h> diff --git a/src/NeBuildKit/JSONManifestBuilder.cpp b/src/NeBuildKit/JSONManifestBuilder.cpp new file mode 100644 index 0000000..0cc9fd3 --- /dev/null +++ b/src/NeBuildKit/JSONManifestBuilder.cpp @@ -0,0 +1,125 @@ +// ============================================================= // +// NeBuild +// PURPOSE: JSON build support. +// Copyright (C) 2024-2026, Amlal El Mahrouss and NeKernel Authors, licensed under BSD-3 license. +// ============================================================= // + +#include <NeBuildKit/JSONManifestBuilder.h> +#include <json/json.h> +#include <fstream> + +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<std::string>(); !res.empty()) + NeBuild::Logger::info() << "description: " << res << std::endl; + } catch (...) {} + + std::string compiler = json_obj["compiler_path"].get<std::string>(); + + std::string command = compiler + " "; + + nlohmann::json header_search_path = json_obj["compiler_headers_path"]; + + for (auto& headers : header_search_path) { + command += "-I" + headers.get<std::string>() + " "; + } + + nlohmann::json headers_path = json_obj["headers_path"]; + + for (auto& headers : headers_path) { + command += "-I" + headers.get<std::string>() + " "; + } + + nlohmann::json sources_files = json_obj["sources_path"]; + + for (auto& sources : sources_files) { + command += sources.get<std::string>() + " "; + } + + nlohmann::json macros_list = json_obj["cpp_macros"]; + + for (auto& macro : macros_list) { + command += "-D" + macro.get<std::string>() + " "; + } + + nlohmann::json compiler_flags = json_obj["compiler_flags"]; + + for (auto& flag : compiler_flags) { + command += flag.get<std::string>() + " "; + } + + if (json_obj["compiler_std"].is_string()) + command += "-std=" + json_obj["compiler_std"].get<std::string>() + " "; + + command += "-o " + json_obj["output_name"].get<std::string>(); + + auto target = json_obj["output_name"].get<std::string>(); + + 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 diff --git a/src/NeBuildKit/TOMLManifestBuilder.cpp b/src/NeBuildKit/TOMLManifestBuilder.cpp new file mode 100644 index 0000000..cce9384 --- /dev/null +++ b/src/NeBuildKit/TOMLManifestBuilder.cpp @@ -0,0 +1,130 @@ +// ============================================================= // +// NeBuild +// PURPOSE: TOML build support. +// Copyright (C) 2024-2026, Amlal El Mahrouss and NeKernel Authors, licensed under BSD-3 license. +// ============================================================= // + +#include <NeBuildKit/TOMLManifestBuilder.h> +#include <filesystem> +#include <toml++/toml.hpp> + +namespace NeBuild { + +namespace FS = std::filesystem; + +/// =========================================================== /// +/// @brief Builds a TOML target from a TOML file. +/// @param arg_sz filename size (must be 1 or greater). +/// @param arg_val filename path (must be a valid TOML file). +/// @return bool: whether the build has succeeded or not. +/// =========================================================== /// +bool TOMLManifestBuilder::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 { + auto toml_file = toml::parse_file(path); + + try { + auto* description = toml_file["description"].as_string(); + + NeBuild::Logger::info() << "package path: " << path << std::endl; + + if (description) NeBuild::Logger::info() << "description: " << description->get() << std::endl; + } catch (...) { + // ... + } + + std::string compiler = toml_file["compiler_path"].as_string()->get(); + + std::string command = compiler + " "; + + auto header_search_path = toml_file["compiler_headers_path"].as_array(); + + if (header_search_path) { + for (auto& headers : *header_search_path) { + command += "-I" + headers.as_string()->get() + " "; + } + } + + auto headers_path = toml_file["headers_path"].as_array(); + + if (headers_path) { + for (auto& headers : *headers_path) { + command += "-I" + headers.as_string()->get() + " "; + } + } + + auto sources_files = toml_file["sources_path"].as_array(); + + if (sources_files) { + for (auto& sources : *sources_files) { + command += sources.as_string()->get() + " "; + } + } + + auto macros_list = toml_file["cpp_macros"].as_array(); + + if (macros_list) { + for (auto& macro : *macros_list) { + command += "-D" + macro.as_string()->get() + " "; + } + } + + auto compiler_flags = toml_file["compiler_flags"].as_array(); + + if (compiler_flags) { + for (auto& flag : *compiler_flags) { + command += flag.as_string()->get() + " "; + } + } + + if (!toml_file["compiler_std"].is_string()) return false; + + command += "-std=" + toml_file["compiler_std"].as_string()->get() + " "; + + if (toml_file["output_name"].as_string() == nullptr) return false; + + command += "-o " + toml_file["output_name"].as_string()->get(); + + auto target = toml_file["output_name"].as_string()->get(); + + NeBuild::Logger::info() << "output: " << 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 TOMLManifestBuilder::BuildSystem() { + return "NeBuild (toml++::toml)"; +} + +} // namespace NeBuild |
