diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-04-23 08:22:28 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-04-23 08:22:28 +0200 |
| commit | 72f132b8fb9f2a47b0f723f4dc4eaa4fdb0c45bd (patch) | |
| tree | 8e1fd9656a6650129c96f11ff7453f4a56b27517 | |
| parent | 892f9abd9bde257acef9a9d8b17707a01100cd25 (diff) | |
dev: Improve makefile, add BTB_POSIX, and BTB_WINDOWS macro.
- Refactor library code for better maintenance.
- Improve code quality by using well known macros for status
code.
- Don't let the CLI pass when '-' is being passed as argument
and is not recognized.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | cli/CommandLine.cc | 30 | ||||
| -rw-r--r-- | examples/example_01/example.cc | 4 | ||||
| -rwxr-xr-x | format.sh | 10 | ||||
| -rw-r--r-- | lib/IManifestBuilder.h | 35 | ||||
| -rw-r--r-- | lib/Includes.h | 5 | ||||
| -rw-r--r-- | lib/JSONManifestBuilder.h | 35 | ||||
| -rw-r--r-- | lib/Macros.h | 13 | ||||
| -rw-r--r-- | makefile | 19 | ||||
| -rw-r--r-- | posix.json | 1 | ||||
| -rw-r--r-- | src/JSONManifestBuilder.cc | 42 | ||||
| -rw-r--r-- | win64.json | 1 |
11 files changed, 119 insertions, 76 deletions
diff --git a/cli/CommandLine.cc b/cli/CommandLine.cc index 8c93898..5c522e7 100644 --- a/cli/CommandLine.cc +++ b/cli/CommandLine.cc @@ -7,13 +7,13 @@ #include <JSONManifestBuilder.h> #include <Includes.h> -static bool kFailed = false; +static bool kFailed = false; static bool kDryRun = false; int main(int argc, char** argv) { if (argc <= 1) - return 1; + return EXIT_FAILURE; for (size_t index = 1; index < argc; ++index) { @@ -22,10 +22,10 @@ int main(int argc, char** argv) if (index_path == "-v" || index_path == "--version") { - logger::info() << "Brought to you by Amlal El Mahrouss for the NeKernel project.\n"; - logger::info() << "© 2024-2025 Amlal El Mahrouss, all rights reserved.\n"; + BTB::Logger::info() << "Brought to you by Amlal El Mahrouss for the NeKernel project.\n"; + BTB::Logger::info() << "© 2024-2025 Amlal El Mahrouss, all rights reserved.\n"; - logger::info() << "Bugs, Issues?, check out: https://github.com/amlel-el-mahrouss/btb/issues\n"; + BTB::Logger::info() << "Bugs, issues? Check out: https://github.com/nekernel-org/btb/issues\n"; return EXIT_SUCCESS; } @@ -37,19 +37,26 @@ int main(int argc, char** argv) else if (index_path == "-h" || index_path == "--help") { - logger::info() << "Usage: btb <file>\n"; + BTB::Logger::info() << "Usage: btb <file>\n"; return EXIT_SUCCESS; } + if (index_path.starts_with("-")) + { + BTB::Logger::info() << "error: unknown option '" << index_path << "'\n"; + + return EXIT_FAILURE; + } + std::thread job_build_thread([](std::string index_path) -> void { - IManifestBuilder* builder = nullptr; + BTB::IManifestBuilder* builder = nullptr; const auto kJsonExtension = ".json"; if (index_path.ends_with(kJsonExtension)) { - builder = new JSONManifestBuilder(); + builder = new BTB::JSONManifestBuilder(); if (!builder) { @@ -59,12 +66,12 @@ int main(int argc, char** argv) } else { - logger::info() << "error: file '" << index_path << "' does not end with .json!" << std::endl; + BTB::Logger::info() << "error: file '" << index_path << "' is not a JSON file!" << std::endl; kFailed = true; return; } - logger::info() << "building: " << index_path << std::endl; + BTB::Logger::info() << "building manifest: " << index_path << std::endl; if (builder && !builder->buildTarget(index_path.size(), index_path.c_str(), kDryRun)) { @@ -73,7 +80,8 @@ int main(int argc, char** argv) delete builder; builder = nullptr; - }, index_path); + }, + index_path); job_build_thread.join(); } diff --git a/examples/example_01/example.cc b/examples/example_01/example.cc index dfef2e4..aa2d8b0 100644 --- a/examples/example_01/example.cc +++ b/examples/example_01/example.cc @@ -3,6 +3,6 @@ int main(int argc, char** argv) { - std::cout << "hello, world!\n"; - return 0; + std::cout << "hello, world!\n"; + return 0; } diff --git a/format.sh b/format.sh new file mode 100755 index 0000000..f36943c --- /dev/null +++ b/format.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +THIS_PATH="$(realpath "$0")" +THIS_DIR="$(dirname "$THIS_PATH")" + +FILE_LIST="$(find "$THIS_DIR" | grep -E ".*(\.cc|\.c|\.h|\.inl)$")" + +echo -e "Files found to format = \n\"\"\"\n$FILE_LIST\n\"\"\"" + +clang-format --verbose -i --style=file $FILE_LIST diff --git a/lib/IManifestBuilder.h b/lib/IManifestBuilder.h index 2aa45db..a48511c 100644 --- a/lib/IManifestBuilder.h +++ b/lib/IManifestBuilder.h @@ -8,23 +8,26 @@ #include <Macros.h> #include <Includes.h> -/// @brief Builder interface class. -/// @note This class is meant to be used as an interface. -class IManifestBuilder +namespace BTB { -public: - explicit IManifestBuilder() = default; - virtual ~IManifestBuilder() = default; + /// @brief Builder interface class. + /// @note This class is meant to be used as an interface. + class IManifestBuilder + { + public: + explicit IManifestBuilder() = default; + virtual ~IManifestBuilder() = default; - IManifestBuilder& operator=(const IManifestBuilder&) = default; - IManifestBuilder(const 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(int arg_sz, const char* arg_val, const bool dry_run = false) = 0; + /// @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(int arg_sz, const char* arg_val, const bool dry_run = false) = 0; - virtual const char* buildSystem() = 0; -}; + virtual const char* buildSystem() = 0; + }; +} // namespace BTB
\ No newline at end of file diff --git a/lib/Includes.h b/lib/Includes.h index 070f80b..3f035f3 100644 --- a/lib/Includes.h +++ b/lib/Includes.h @@ -1,3 +1,8 @@ +// ============================================================= // +// btb +// Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. +// ============================================================= // + #ifndef BTB_INCLUDES_H #define BTB_INCLUDES_H diff --git a/lib/JSONManifestBuilder.h b/lib/JSONManifestBuilder.h index ae2d482..fef5baf 100644 --- a/lib/JSONManifestBuilder.h +++ b/lib/JSONManifestBuilder.h @@ -8,23 +8,26 @@ #include <IManifestBuilder.h> #include <json.h> -/// @brief JSON builder -class JSONManifestBuilder final : public IManifestBuilder +namespace BTB { -public: - explicit JSONManifestBuilder() = default; - virtual ~JSONManifestBuilder() override = default; + /// @brief JSON builder + class JSONManifestBuilder final : public IManifestBuilder + { + public: + explicit JSONManifestBuilder() = default; + virtual ~JSONManifestBuilder() override = default; - JSONManifestBuilder& operator=(const JSONManifestBuilder&) = default; - JSONManifestBuilder(const JSONManifestBuilder&) = 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. - virtual bool buildTarget(int arg_sz, const char* arg_val, const bool dry_run = false) override; + 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. + virtual bool buildTarget(int arg_sz, const char* arg_val, const bool dry_run = false) override; - virtual const char* buildSystem() override; -}; + virtual const char* buildSystem() override; + }; +} // namespace BTB
\ No newline at end of file diff --git a/lib/Macros.h b/lib/Macros.h index 85fb45b..35a44a1 100644 --- a/lib/Macros.h +++ b/lib/Macros.h @@ -12,25 +12,26 @@ extern "C" #include <rang.h> -#define LIKELY(ARG) (ARG) ? assert(false) : ((void)0) +#define LIKELY(ARG) (ARG) ? assert(false) : ((void)0) #define UNLIKELY(ARG) LIKELY(!(ARG)) -#define LIBBTB_VERSION "1.2.0" +#define LIBBTB_VERSION "1.1.0" -#define LIBBTB_VERSION_BCD 0x0120 +#define LIBBTB_VERSION_BCD 0x0110 #define LIBBTB_VERSION_MAJOR 1 -#define LIBBTB_VERSION_MINOR 0 +#define LIBBTB_VERSION_MINOR 1 #define LIBBTB_VERSION_PATCH 0 #define LIBBTB_UNUSED(X) ((void)X) -namespace logger +namespace BTB::Logger { + /// @brief replacement for std::cout for BTB logging. inline std::ostream& info() noexcept { auto& out = std::cout; out << rang::fg::red << "btb: " << rang::style::reset; return out; } -} +} // namespace BTB::Logger @@ -1,14 +1,23 @@ +SUDO=sudo +GCC=g++ +GCC_MINGW=x86_64-w64-mingw32-g++ +CXXFLAGS=-I./lib -I./vendor +CXXSTD= -std=c++20 +SRC=$(wildcard cli/*.cc) $(wildcard src/*.cc) +OUT=btb +CP=cp .PHONY: build-btb build-btb: - sudo g++ -I./lib -I./vendor $(wildcard cli/*.cc) $(wildcard src/*.cc) -std=c++20 -o btb - sudo cp btb /usr/local/bin + $(SUDO) $(GCC) $(CXXFLAGS) $(SRC) $(CXXSTD) -o $(OUT) + $(SUDO) $(CP) $(OUT) /usr/local/bin .PHONY: build-btb-windows build-btb-windows: - x86_64-w64-mingw32-g++.exe -I./lib -I./vendor $(wildcard cli/*.cc) $(wildcard src/*.cc) -std=c++20 -o btb.exe + $(GCC_MINGW) $(CXXFLAGS) $(SRC) -o $(OUT).exe .PHONY: help help: - @echo "=> build-btb-windows" - @echo "=> build-btb" + @echo "=> help: Show this help message." + @echo "=> build-btb-windows: Build BTB for Windows." + @echo "=> build-btb: Build BTB for POSIX." @@ -5,5 +5,6 @@ "sources_path": ["src/*.cc", "cli/*.cc"], "output_name": "btb", "compiler_flags": ["-fPIC"], + "cpp_macros": ["BTB_POSIX"], "run_after_build": false } diff --git a/src/JSONManifestBuilder.cc b/src/JSONManifestBuilder.cc index a7b5938..d268724 100644 --- a/src/JSONManifestBuilder.cc +++ b/src/JSONManifestBuilder.cc @@ -7,21 +7,23 @@ #include <initializer_list> using String = std::string; -using JSON = nlohmann::json; +using JSON = nlohmann::json; namespace FS = std::filesystem; +using namespace BTB; + /// @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 succeeded building. -/// @retval false failed to build. +/// @retval false fail to build, see error message. bool JSONManifestBuilder::buildTarget(int arg_sz, const char* arg_val, const bool dry_run) { String path; if (arg_sz < 0) { - logger::info() << "btb: error: file path is empty" << std::endl; + BTB::Logger::info() << "btb: error: file path is empty" << std::endl; return false; } else @@ -30,7 +32,7 @@ bool JSONManifestBuilder::buildTarget(int arg_sz, const char* arg_val, const boo if (!FS::exists(path)) { - logger::info() << "btb: error: file '" << path << "' does not exist" << std::endl; + BTB::Logger::info() << "btb: error: file '" << path << "' does not exist" << std::endl; return false; } } @@ -41,7 +43,7 @@ bool JSONManifestBuilder::buildTarget(int arg_sz, const char* arg_val, const boo if (!json.good()) { - logger::info() << "btb: error: file '" << path << "' is not a valid JSON" << std::endl; + BTB::Logger::info() << "btb: error: file '" << path << "' is not a valid JSON" << std::endl; return false; } @@ -85,8 +87,8 @@ bool JSONManifestBuilder::buildTarget(int arg_sz, const char* arg_val, const boo auto target = json_obj["output_name"].get<String>(); - logger::info() << "output path: " << target << "\n"; - logger::info() << "command: " << command << "\n"; + BTB::Logger::info() << "output path: " << target << "\n"; + BTB::Logger::info() << "command: " << command << "\n"; try { @@ -95,7 +97,6 @@ bool JSONManifestBuilder::buildTarget(int arg_sz, const char* arg_val, const boo } catch (...) { - } if (dry_run) @@ -107,7 +108,7 @@ bool JSONManifestBuilder::buildTarget(int arg_sz, const char* arg_val, const boo if (ret_exec > 0) { - logger::info() << "error: exec exit with code: " << ret_exec << "" << std::endl; + BTB::Logger::info() << "error: exec exit with code: " << ret_exec << "" << std::endl; return false; } @@ -117,39 +118,41 @@ bool JSONManifestBuilder::buildTarget(int arg_sz, const char* arg_val, const boo { if (target.ends_with(".so")) { - logger::info() << "error: can't open dynamic library, it may not have an entrypoint" << std::endl; + BTB::Logger::info() << "error: can't open dynamic library, it mayn't have an entrypoint" << std::endl; + return true; } - else if (target.ends_with(".dll")) + else if (target.ends_with(".dylib") || + target.ends_with(".dll")) { - std::ifstream file = std::ifstream(target); - + std::ifstream file = std::ifstream(target); std::stringstream ss; + ss << file.rdbuf(); if (ss.str()[0] == 'J' && ss.str()[1] == 'o' && ss.str()[2] == 'y' && ss.str()[3] == '!') - logger::info() << "error: can't open Joy! dynamic library, it maynt't contain an entrypoint" << std::endl; + BTB::Logger::info() << "error: can't open PEF dynamic library, it mayn't contain an entrypoint" << std::endl; else if (ss.str()[0] == '!' && ss.str()[1] == 'y' && ss.str()[2] == 'o' && ss.str()[3] == 'J') - logger::info() << "error: can't open !yoJ dynamic library, it maynt't contain an entrypoint" << std::endl; + BTB::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') - logger::info() << "error: can't open MZ dynamic library, it maynt't contain an entrypoint" << std::endl; + BTB::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') { - logger::info() << "error: can't open ELF dynamic library, it maynt't contain an entrypoint" << std::endl; + BTB::Logger::info() << "error: can't open ELF dynamic library, it mayn't contain an entrypoint" << std::endl; } return true; } -#ifdef _WIN32 +#if defined(BTB_WINDOWS) std::system((".\\" + target).c_str()); #else std::system(("./" + target).c_str()); @@ -163,8 +166,7 @@ bool JSONManifestBuilder::buildTarget(int arg_sz, const char* arg_val, const boo } catch (std::runtime_error& err) { - logger::info() << "error: " << err.what() << std::endl; - perror("btb"); + BTB::Logger::info() << "error: " << err.what() << std::endl; return false; } @@ -5,5 +5,6 @@ "sources_path": ["src/*.cc", "cli/*.cc"], "output_name": "btb.exe", "compiler_flags": ["-fPIC"], + "cpp_macros": ["BTB_WINDOWS"], "run_after_build": false } |
