summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-05-29 10:56:20 +0200
committerGitHub <noreply@github.com>2025-05-29 10:56:20 +0200
commit50c2c9773d5660b1497a1132ffa96a7ab10d9c45 (patch)
tree9e38258948019249d64b90a27707ad3ed36bb186 /src
parentdab3762388278d1efadec1040a8f5c5ebd286b24 (diff)
parent61fc36d7fb3d147b577c1d25a72e22e45f6c6c83 (diff)
Merge pull request #2 from nekernel-org/dev
0.0.2
Diffstat (limited to 'src')
-rw-r--r--src/IManifestBuilder.cc2
-rw-r--r--src/JSONManifestBuilder.cc286
2 files changed, 130 insertions, 158 deletions
diff --git a/src/IManifestBuilder.cc b/src/IManifestBuilder.cc
index e3cfc6b..b3f4de8 100644
--- a/src/IManifestBuilder.cc
+++ b/src/IManifestBuilder.cc
@@ -3,4 +3,4 @@
// Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
// ============================================================= //
-#include <IManifestBuilder.h>
+#include <BTBKit/IManifestBuilder.h>
diff --git a/src/JSONManifestBuilder.cc b/src/JSONManifestBuilder.cc
index d268724..46a8518 100644
--- a/src/JSONManifestBuilder.cc
+++ b/src/JSONManifestBuilder.cc
@@ -3,11 +3,10 @@
// Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
// ============================================================= //
-#include <JSONManifestBuilder.h>
-#include <initializer_list>
+#include <BTBKit/JSONManifestBuilder.h>
using String = std::string;
-using JSON = nlohmann::json;
+using JSON = nlohmann::json;
namespace FS = std::filesystem;
using namespace BTB;
@@ -17,164 +16,137 @@ using namespace BTB;
/// @param arg_val filename path (must be a valid JSON file).
/// @retval true succeeded building.
/// @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)
- {
- BTB::Logger::info() << "btb: error: file path is empty" << std::endl;
- return false;
- }
- else
- {
- path = arg_val;
-
- if (!FS::exists(path))
- {
- BTB::Logger::info() << "btb: error: file '" << path << "' does not exist" << std::endl;
- return false;
- }
- }
-
- try
- {
- std::ifstream json(path);
-
- if (!json.good())
- {
- BTB::Logger::info() << "btb: error: file '" << path << "' is not a valid JSON" << std::endl;
- return false;
- }
-
- JSON json_obj = JSON::parse(json);
-
- String compiler = json_obj["compiler_path"].get<String>();
-
- JSON header_search_path = json_obj["headers_path"];
- JSON sources_files = json_obj["sources_path"];
-
- String command = compiler + " ";
-
- for (auto& sources : sources_files)
- {
- command += sources.get<String>() + " ";
- }
-
- for (auto& headers : header_search_path)
- {
- command += "-I" + headers.get<String>() + " ";
- }
-
- JSON macros_list = json_obj["cpp_macros"];
-
- for (auto& macro : macros_list)
- {
- command += "-D" + macro.get<String>() + " ";
- }
-
- JSON compiler_flags = json_obj["compiler_flags"];
-
- for (auto& flag : compiler_flags)
- {
- command += flag.get<String>() + " ";
- }
-
- if (json_obj["compiler_std"].is_string())
- command += "-std=" + json_obj["compiler_std"].get<String>() + " ";
-
- command += "-o " + json_obj["output_name"].get<String>();
-
- auto target = json_obj["output_name"].get<String>();
-
- BTB::Logger::info() << "output path: " << target << "\n";
- BTB::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)
- {
- BTB::Logger::info() << "error: exec exit with code: " << ret_exec << "" << std::endl;
- return false;
- }
-
- try
- {
- if (json_obj["run_after_build"].get<bool>())
- {
- if (target.ends_with(".so"))
- {
- BTB::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] == 'J' &&
- ss.str()[1] == 'o' &&
- ss.str()[2] == 'y' &&
- ss.str()[3] == '!')
- 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')
- 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')
- 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')
- {
- BTB::Logger::info() << "error: can't open ELF dynamic library, it mayn't contain an entrypoint" << std::endl;
- }
-
- return true;
- }
+bool JSONManifestBuilder::buildTarget(int arg_sz, const char* arg_val, const bool dry_run) {
+ String path;
+
+ if (arg_sz < 0) {
+ BTB::Logger::info() << "btb: error: file path is empty" << std::endl;
+ return false;
+ } else {
+ path = arg_val;
+
+ if (!FS::exists(path)) {
+ BTB::Logger::info() << "btb: error: file '" << path << "' does not exist" << std::endl;
+ return false;
+ }
+ }
+
+ try {
+ std::ifstream json(path);
+
+ if (!json.good()) {
+ BTB::Logger::info() << "btb: error: file '" << path << "' is not a valid JSON" << std::endl;
+ return false;
+ }
+
+ JSON json_obj = JSON::parse(json);
+
+ String compiler = json_obj["compiler_path"].get<String>();
+
+ JSON header_search_path = json_obj["headers_path"];
+ JSON sources_files = json_obj["sources_path"];
+
+ String command = compiler + " ";
+
+ for (auto& sources : sources_files) {
+ command += sources.get<String>() + " ";
+ }
+
+ for (auto& headers : header_search_path) {
+ command += "-I" + headers.get<String>() + " ";
+ }
+
+ JSON macros_list = json_obj["cpp_macros"];
+
+ for (auto& macro : macros_list) {
+ command += "-D" + macro.get<String>() + " ";
+ }
+
+ JSON compiler_flags = json_obj["compiler_flags"];
+
+ for (auto& flag : compiler_flags) {
+ command += flag.get<String>() + " ";
+ }
+
+ if (json_obj["compiler_std"].is_string())
+ command += "-std=" + json_obj["compiler_std"].get<String>() + " ";
+
+ command += "-o " + json_obj["output_name"].get<String>();
+
+ auto target = json_obj["output_name"].get<String>();
+
+ BTB::Logger::info() << "output path: " << target << "\n";
+ BTB::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) {
+ BTB::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")) {
+ BTB::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] == 'J' && ss.str()[1] == 'o' && ss.str()[2] == 'y' && ss.str()[3] == '!')
+ 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')
+ 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')
+ 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') {
+ BTB::Logger::info()
+ << "error: can't open ELF dynamic library, it mayn't contain an entrypoint"
+ << std::endl;
+ }
+
+ return true;
+ }
#if defined(BTB_WINDOWS)
- std::system((".\\" + target).c_str());
+ std::system((".\\" + target).c_str());
#else
- std::system(("./" + target).c_str());
+ std::system(("./" + target).c_str());
#endif
- }
- }
- catch (...)
- {
- return true;
- }
- }
- catch (std::runtime_error& err)
- {
- BTB::Logger::info() << "error: " << err.what() << std::endl;
-
- return false;
- }
-
- return true;
+ }
+ } catch (...) {
+ return true;
+ }
+ } catch (std::runtime_error& err) {
+ BTB::Logger::info() << "error: " << err.what() << std::endl;
+
+ return false;
+ }
+
+ return true;
}
-const char* JSONManifestBuilder::buildSystem()
-{
- return "json";
+const char* JSONManifestBuilder::buildSystem() {
+ return "json";
}