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/cli/CLI.cpp | 83 ++++++++++++++++++++++++++ src/cli/main.cc | 84 -------------------------- src/lib/IManifestBuilder.cc | 6 -- src/lib/IManifestBuilder.cpp | 6 ++ src/lib/JSONManifestBuilder.cc | 124 -------------------------------------- src/lib/JSONManifestBuilder.cpp | 124 ++++++++++++++++++++++++++++++++++++++ src/lib/TOMLManifestBuilder.cc | 128 ---------------------------------------- src/lib/TOMLManifestBuilder.cpp | 128 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 341 insertions(+), 342 deletions(-) create mode 100644 src/cli/CLI.cpp delete mode 100644 src/cli/main.cc delete mode 100644 src/lib/IManifestBuilder.cc create mode 100644 src/lib/IManifestBuilder.cpp delete mode 100644 src/lib/JSONManifestBuilder.cc create mode 100644 src/lib/JSONManifestBuilder.cpp delete mode 100644 src/lib/TOMLManifestBuilder.cc create mode 100644 src/lib/TOMLManifestBuilder.cpp (limited to 'src') diff --git a/src/cli/CLI.cpp b/src/cli/CLI.cpp new file mode 100644 index 0000000..a21c4ad --- /dev/null +++ b/src/cli/CLI.cpp @@ -0,0 +1,83 @@ + +// ============================================================= // +// NeBuild +// FILE: main.cc +// PURPOSE: Main Tool Entrypoint. +// Copyright (C) 2024-2025, Amlal El Mahrouss and NeKernel Authors, licensed under BSD-3 license. +// ============================================================= // + +#include +#include +#include +#include + +int main(int argc, char** argv) { + if (argc < 1) return EXIT_FAILURE; + + NeBuild::BuildConfig config; + + for (size_t index = 1; index < argc; ++index) { + std::string index_path = argv[index]; + + if (index_path == "-v" || index_path == "-version") { + NeBuild::Logger::info() << "NeBuild (" << NEBUILD_VERSION << ")\n"; + return EXIT_SUCCESS; + } else if (index_path == "-dry-run" || index_path == "-n") { + config.dry_run(true); + continue; + } else if (index_path == "-h" || index_path == "-help") { + NeBuild::Logger::info() << "nebuild \n"; + return EXIT_SUCCESS; + } + + auto index_cpy = index; + + std::thread job_build_thread([&index_path, &index, &index_cpy, &argc, &argv, &config]() -> void { + std::unique_ptr builder; + + constexpr auto kJsonExtension = ".json"; + + if (index_path.ends_with(kJsonExtension)) { + builder = std::make_unique(); + + /// report failed build to config. + if (!builder) { + config.has_failed(true); + return; + } + } else { + constexpr auto kTomlExtension = ".toml"; + builder = std::make_unique(); + + if (!index_path.ends_with(kTomlExtension)) { + NeBuild::Logger::info() << "error: file '" << index_path << "' is not a manifest file!" + << std::endl; + config.has_failed(true); + return; + } + } + + std::string next_path; + + if ((index_cpy + 1) < argc && argv[index_cpy + 1]) next_path = argv[index_cpy + 1]; + + if (next_path == "-build-system") { + NeBuild::Logger::info() << builder->BuildSystem() << std::endl; + std::exit(EXIT_SUCCESS); + } + + NeBuild::Logger::info() << "building manifest: " << index_path << std::endl; + + config.path(index_path); + + if (builder && !builder->BuildTarget(config)) { + config.has_failed(true); + } + }); + + job_build_thread.join(); + } + + // check for whether config is valid. if so return failure, or success. + return !config ? EXIT_FAILURE : EXIT_SUCCESS; +} diff --git a/src/cli/main.cc b/src/cli/main.cc deleted file mode 100644 index 61ab3c7..0000000 --- a/src/cli/main.cc +++ /dev/null @@ -1,84 +0,0 @@ - -// ============================================================= // -// NeBuild -// FILE: main.cc -// PURPOSE: Main Tool Entrypoint. -// Copyright (C) 2024-2025, Amlal El Mahrouss and NeKernel Authors, licensed under BSD-3 license. -// ============================================================= // - -#include -#include -#include -#include - -int main(int argc, char** argv) { - if (argc < 1) return EXIT_FAILURE; - - NeBuild::BuildConfig config; - - for (size_t index = 1; index < argc; ++index) { - std::string index_path = argv[index]; - - if (index_path == "-v" || index_path == "-version") { - NeBuild::Logger::info() << "NeBuild (" << NEBUILD_VERSION << ")\n"; - return EXIT_SUCCESS; - } else if (index_path == "-dry-run" || index_path == "-n") { - config.dry_run(true); - continue; - } else if (index_path == "-h" || index_path == "-help") { - NeBuild::Logger::info() << "usage: nebuild .\n"; - - return EXIT_SUCCESS; - } - - auto index_cpy = index; - - std::thread job_build_thread([&index_path, &index, &index_cpy, &argc, &argv, &config]() -> void { - std::unique_ptr builder; - - constexpr auto kJsonExtension = ".json"; - - if (index_path.ends_with(kJsonExtension)) { - builder = std::make_unique(); - - /// report failed build to config. - if (!builder) { - config.has_failed(true); - return; - } - } else { - constexpr auto kTomlExtension = ".toml"; - builder = std::make_unique(); - - if (!index_path.ends_with(kTomlExtension)) { - NeBuild::Logger::info() << "error: file '" << index_path << "' is not a manifest file!" - << std::endl; - config.has_failed(true); - return; - } - } - - std::string next_path; - - if ((index_cpy + 1) < argc && argv[index_cpy + 1]) next_path = argv[index_cpy + 1]; - - if (next_path == "-build-system") { - NeBuild::Logger::info() << builder->BuildSystem() << std::endl; - std::exit(EXIT_SUCCESS); - } - - NeBuild::Logger::info() << "building manifest: " << index_path << std::endl; - - config.path(index_path); - - if (builder && !builder->BuildTarget(config)) { - config.has_failed(true); - } - }); - - job_build_thread.join(); - } - - // check for whether config is valid. if so return failure, or success. - return !config ? EXIT_FAILURE : EXIT_SUCCESS; -} diff --git a/src/lib/IManifestBuilder.cc b/src/lib/IManifestBuilder.cc deleted file mode 100644 index 2a61ed0..0000000 --- a/src/lib/IManifestBuilder.cc +++ /dev/null @@ -1,6 +0,0 @@ -// ============================================================= // -// NeBuild -// Copyright (C) 2024-2025, Amlal El Mahrouss and NeKernel Authors, licensed under BSD-3 license. -// ============================================================= // - -#include diff --git a/src/lib/IManifestBuilder.cpp b/src/lib/IManifestBuilder.cpp new file mode 100644 index 0000000..2a61ed0 --- /dev/null +++ b/src/lib/IManifestBuilder.cpp @@ -0,0 +1,6 @@ +// ============================================================= // +// NeBuild +// Copyright (C) 2024-2025, Amlal El Mahrouss and NeKernel Authors, licensed under BSD-3 license. +// ============================================================= // + +#include diff --git a/src/lib/JSONManifestBuilder.cc b/src/lib/JSONManifestBuilder.cc deleted file mode 100644 index 7f52f51..0000000 --- a/src/lib/JSONManifestBuilder.cc +++ /dev/null @@ -1,124 +0,0 @@ -// ============================================================= // -// 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 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 diff --git a/src/lib/TOMLManifestBuilder.cc b/src/lib/TOMLManifestBuilder.cc deleted file mode 100644 index 0a51a4c..0000000 --- a/src/lib/TOMLManifestBuilder.cc +++ /dev/null @@ -1,128 +0,0 @@ -// ============================================================= // -// NeBuild -// PURPOSE: TOML 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 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 diff --git a/src/lib/TOMLManifestBuilder.cpp b/src/lib/TOMLManifestBuilder.cpp new file mode 100644 index 0000000..0a51a4c --- /dev/null +++ b/src/lib/TOMLManifestBuilder.cpp @@ -0,0 +1,128 @@ +// ============================================================= // +// NeBuild +// PURPOSE: TOML 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 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 -- cgit v1.2.3