diff options
Diffstat (limited to 'dev')
| -rw-r--r-- | dev/BuildKit/Defines.h | 30 | ||||
| -rw-r--r-- | dev/BuildKit/IManifestBuilder.h | 31 | ||||
| -rw-r--r-- | dev/BuildKit/Imports.h | 19 | ||||
| -rw-r--r-- | dev/BuildKit/JSONManifestBuilder.h | 33 | ||||
| -rw-r--r-- | dev/BuildKit/TOMLManifestBuilder.h | 33 | ||||
| -rw-r--r-- | dev/cli/Tool.cc | 76 | ||||
| -rw-r--r-- | dev/src/IManifestBuilder.cc | 6 | ||||
| -rw-r--r-- | dev/src/JSONManifestBuilder.cc | 155 |
8 files changed, 383 insertions, 0 deletions
diff --git a/dev/BuildKit/Defines.h b/dev/BuildKit/Defines.h new file mode 100644 index 0000000..d408058 --- /dev/null +++ b/dev/BuildKit/Defines.h @@ -0,0 +1,30 @@ +// ============================================================= // +// nebuild +// Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. +// ============================================================= // + +#pragma once + +#include <BuildKit/Imports.h> + +#define LIKELY(ARG) ((ARG) ? assert(false) : ((void) 0)) +#define UNLIKELY(ARG) LIKELY(!(ARG)) + +#define LIBNEBUILD_VERSION "v0.0.1-libNeBuild" + +#define LIBNEBUILD_VERSION_BCD 0x0001 + +#define LIBNEBUILD_VERSION_MAJOR 1 +#define LIBNEBUILD_VERSION_MINOR 1 +#define LIBNEBUILD_VERSION_PATCH 0 + +#define LIBNEBUILD_UNUSED(X) ((void) X) + +namespace NeBuild::Logger { +/// @brief replacement for std::cout for NeBuild logging. +inline std::ostream& info() noexcept { + auto& out = std::cout; + out << rang::fg::red << "nebuild: " << rang::style::reset; + return out; +} +} // namespace NeBuild::Logger diff --git a/dev/BuildKit/IManifestBuilder.h b/dev/BuildKit/IManifestBuilder.h new file mode 100644 index 0000000..c1d45d0 --- /dev/null +++ b/dev/BuildKit/IManifestBuilder.h @@ -0,0 +1,31 @@ +// ============================================================= // +// nebuild +// Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. +// ============================================================= // + +#pragma once + +#include <BuildKit/Defines.h> + +#define NEBUILD_MANIFEST_BUILDER : public NeBuild::IManifestBuilder + +namespace NeBuild { +/// @brief Builder interface class. +/// @note This class is meant to be used as an interface. +class IManifestBuilder { + public: + IManifestBuilder() = default; + virtual ~IManifestBuilder() = default; + + IManifestBuilder& operator=(const IManifestBuilder&) = default; + IManifestBuilder(const IManifestBuilder&) = default; + + /// @brief Builds a target using the implemented laguage. + /// @param arg_sz filename size + /// @param arg_val filename path. + /// @retval true succeeded. + /// @retval false failed. + virtual bool BuildTarget(const std::string& arg, const bool dry_run = false) = 0; + virtual const char* BuildSystem() = 0; +}; +} // namespace NeBuild
\ No newline at end of file diff --git a/dev/BuildKit/Imports.h b/dev/BuildKit/Imports.h new file mode 100644 index 0000000..c542737 --- /dev/null +++ b/dev/BuildKit/Imports.h @@ -0,0 +1,19 @@ +// ============================================================= // +// nebuild +// Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. +// ============================================================= // + +#ifndef NEBUILD_INCLUDES_H +#define NEBUILD_INCLUDES_H + +#include <rang/rang.h> +#include <cassert> +#include <cstddef> +#include <cstdio> +#include <fstream> +#include <iostream> +#include <sstream> +#include <string> +#include <thread> + +#endif // NEBUILD_INCLUDES_H diff --git a/dev/BuildKit/JSONManifestBuilder.h b/dev/BuildKit/JSONManifestBuilder.h new file mode 100644 index 0000000..63a67b7 --- /dev/null +++ b/dev/BuildKit/JSONManifestBuilder.h @@ -0,0 +1,33 @@ +// ============================================================= // +// nebuild +// Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. +// ============================================================= // + +#pragma once + +#include <BuildKit/IManifestBuilder.h> +#include <json/json.h> + +/// @file JSONManifestBuilder.h +/// @brief JSON manifest builder header file. + +namespace NeBuild { +/// @brief JSON builder +class JSONManifestBuilder final NEBUILD_MANIFEST_BUILDER { + public: + JSONManifestBuilder() = default; + ~JSONManifestBuilder() override = default; + + JSONManifestBuilder& operator=(const JSONManifestBuilder&) = default; + JSONManifestBuilder(const JSONManifestBuilder&) = default; + + public: + /// @brief Builds a JSON target. + /// @param arg_sz filename size + /// @param arg_val filename path. + /// @retval true build succeeded. + /// @retval false failed to build. + bool BuildTarget(const std::string& arg_val, const bool dry_run = false) override; + const char* BuildSystem() override; +}; +} // namespace NeBuild
\ No newline at end of file diff --git a/dev/BuildKit/TOMLManifestBuilder.h b/dev/BuildKit/TOMLManifestBuilder.h new file mode 100644 index 0000000..10a8200 --- /dev/null +++ b/dev/BuildKit/TOMLManifestBuilder.h @@ -0,0 +1,33 @@ +// ============================================================= // +// nebuild +// Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. +// ============================================================= // + +#pragma once + +#include <BuildKit/IManifestBuilder.h> +#include <toml++/toml.hpp> + +/// @file TOMLManifestBuilder.h +/// @brief TOML manifest builder header file. + +namespace NeBuild { +/// @brief TOML builder +class TOMLManifestBuilder final NEBUILD_MANIFEST_BUILDER { + public: + TOMLManifestBuilder() = default; + ~TOMLManifestBuilder() override = default; + + TOMLManifestBuilder& operator=(const TOMLManifestBuilder&) = default; + TOMLManifestBuilder(const TOMLManifestBuilder&) = default; + + public: + /// @brief Builds a TOML target. + /// @param arg_sz filename size + /// @param arg_val filename path. + /// @retval true build succeeded. + /// @retval false failed to build. + bool BuildTarget(int arg_sz, const char* arg_val, const bool dry_run = false) override; + const char* BuildSystem() override; +}; +} // namespace NeBuild
\ No newline at end of file diff --git a/dev/cli/Tool.cc b/dev/cli/Tool.cc new file mode 100644 index 0000000..1d49c9f --- /dev/null +++ b/dev/cli/Tool.cc @@ -0,0 +1,76 @@ + +// ============================================================= // +// nebuild +// Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. +// ============================================================= // + +#include <BuildKit/JSONManifestBuilder.h> + +static bool kFailed = false; +static bool kDryRun = false; + +int main(int argc, char** argv) { + if (argc <= 1) return EXIT_FAILURE; + + for (size_t index = 1; index < argc; ++index) { + std::string index_path = argv[index]; + + if (index_path == "-v" || index_path == "--version") { + NeBuild::Logger::info() << "Brought to you by Amlal El Mahrouss for NeKernel.org.\n"; + NeBuild::Logger::info() << "© 2024-2025 Amlal El Mahrouss, all rights reserved.\n"; + + NeBuild::Logger::info() + << "Bugs, issues? Check out: https://github.com/nekernel-org/nebuild/issues\n"; + + return EXIT_SUCCESS; + } else if (index_path == "--dry-run") { + kDryRun = true; + continue; + } else if (index_path == "-h" || index_path == "--help") { + NeBuild::Logger::info() << "Usage: nebuild <file>\n"; + + return EXIT_SUCCESS; + } + + if (index_path.starts_with("-")) { + NeBuild::Logger::info() << "error: unknown option '" << index_path << "'\n"; + + return EXIT_FAILURE; + } + + std::thread job_build_thread( + [](std::string index_path) -> void { + NeBuild::IManifestBuilder* builder = nullptr; + + const auto kJsonExtension = ".json"; + + if (index_path.ends_with(kJsonExtension)) { + builder = new NeBuild::JSONManifestBuilder(); + + if (!builder) { + kFailed = true; + return; + } + } else { + NeBuild::Logger::info() + << "error: file '" << index_path << "' is not a JSON file!" << std::endl; + kFailed = true; + return; + } + + NeBuild::Logger::info() << "building manifest: " << index_path << std::endl; + + if (builder && !builder->BuildTarget(index_path, kDryRun)) { + kFailed = true; + } + + delete builder; + builder = nullptr; + }, + index_path); + + job_build_thread.join(); + } + + return kFailed ? EXIT_FAILURE : EXIT_SUCCESS; +} diff --git a/dev/src/IManifestBuilder.cc b/dev/src/IManifestBuilder.cc new file mode 100644 index 0000000..1397dd4 --- /dev/null +++ b/dev/src/IManifestBuilder.cc @@ -0,0 +1,6 @@ +// ============================================================= // +// nebuild +// Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. +// ============================================================= // + +#include <BuildKit/IManifestBuilder.h> diff --git a/dev/src/JSONManifestBuilder.cc b/dev/src/JSONManifestBuilder.cc new file mode 100644 index 0000000..33d7f62 --- /dev/null +++ b/dev/src/JSONManifestBuilder.cc @@ -0,0 +1,155 @@ +// ============================================================= // +// nebuild +// Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. +// ============================================================= // + +#include <BuildKit/JSONManifestBuilder.h> + +using JSON = nlohmann::json; + +namespace FS = std::filesystem; + +using namespace NeBuild; + +/// @brief Builds a JSON target from a JSON file. +/// @param arg_sz filename size (must be 1 or greater). +/// @param arg_val filename path (must be a valid JSON file). +/// @retval true building has succeeded. +/// @retval false fail to build, see error message. +bool JSONManifestBuilder::BuildTarget(const std::string& argv_val, const bool dry_run) { + std::string path; + + if (argv_val.empty()) { + NeBuild::Logger::info() << "nebuild: error: file path is empty" << std::endl; + return false; + } else { + path = argv_val; + + if (!FS::exists(path)) { + NeBuild::Logger::info() << "nebuild: error: file '" << path << "' does not exist" + << std::endl; + return false; + } + } + + try { + std::ifstream json(path); + + if (!json.good()) { + NeBuild::Logger::info() << "nebuild: error: file '" << path << "' is not a valid JSON" + << std::endl; + return false; + } + + JSON json_obj = JSON::parse(json); + + std::string compiler = json_obj["compiler_path"].get<std::string>(); + + JSON header_search_path = json_obj["headers_path"]; + JSON sources_files = json_obj["sources_path"]; + + std::string command = compiler + " "; + + for (auto& sources : sources_files) { + command += sources.get<std::string>() + " "; + } + + for (auto& headers : header_search_path) { + command += "-I" + headers.get<std::string>() + " "; + } + + JSON macros_list = json_obj["cpp_macros"]; + + for (auto& macro : macros_list) { + command += "-D" + macro.get<std::string>() + " "; + } + + 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"; + NeBuild::Logger::info() << "command: " << command << "\n"; + + try { + if (json_obj["dry_run"].get<bool>()) return true; + } catch (...) { + } + + if (dry_run) { + return true; + } + + 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; + return false; + } + + try { + if (json_obj["run_after_build"].get<bool>()) { + if (target.ends_with(".so")) { + NeBuild::Logger::info() + << "error: can't open dynamic library, it mayn't have an entrypoint" << std::endl; + + return true; + } else if (target.ends_with(".dylib") || target.ends_with(".dll")) { + std::ifstream file = std::ifstream(target); + std::stringstream ss; + + ss << file.rdbuf(); + + if (ss.str()[0] == 'O' && ss.str()[1] == 'p' && ss.str()[2] == 'e' && ss.str()[3] == 'n') + NeBuild::Logger::info() + << "error: can't open PEF dynamic library, it mayn't contain an entrypoint" + << std::endl; + else if (ss.str()[0] == 'n' && ss.str()[1] == 'e' && ss.str()[2] == 'p' && + ss.str()[3] == 'O') + NeBuild::Logger::info() + << "error: can't open FEP dynamic library, it mayn't contain an entrypoint" + << std::endl; + else if (ss.str()[0] == 'M' && ss.str()[1] == 'Z') + NeBuild::Logger::info() + << "error: can't open MZ dynamic library, it mayn't contain an entrypoint" + << std::endl; + else if (ss.str()[0] == 0x7F && ss.str()[1] == 'E') { + NeBuild::Logger::info() + << "error: can't open ELF dynamic library, it mayn't contain an entrypoint" + << std::endl; + } + + return true; + } + +#if defined(NEBUILD_WINDOWS) + std::system((".\\" + target).c_str()); +#else + std::system(("./" + target).c_str()); +#endif + } + } catch (...) { + return true; + } + } catch (std::runtime_error& err) { + NeBuild::Logger::info() << "error: " << err.what() << std::endl; + + return false; + } + + return true; +} + +const char* JSONManifestBuilder::BuildSystem() { + return "NeBuild (JSON)"; +} |
