summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal <amlal.elmahrouss@icloud.com>2024-10-26 14:14:05 +0200
committerAmlal <amlal.elmahrouss@icloud.com>2024-10-26 14:14:05 +0200
commit7b8238b204dd89f11e710148293f8cc2e2baac3b (patch)
treeafabc5c7d9ba04cbfe94835be0d96830ee59a90a
parentd7dcb2695ca2c69b45314cfc261c395e935d355b (diff)
IMP: Improved ManifestBuilder, do not hang when dry running.
Signed-off-by: Amlal <amlal.elmahrouss@icloud.com>
-rw-r--r--BTBKit/IManifestBuilder.h4
-rw-r--r--BTBKit/JSONManifestBuilder.h2
-rw-r--r--cli/ManifestBuilder.cc13
-rw-r--r--src/JSONManifestBuilder.cc46
4 files changed, 44 insertions, 21 deletions
diff --git a/BTBKit/IManifestBuilder.h b/BTBKit/IManifestBuilder.h
index e202a6a..dd5f4e8 100644
--- a/BTBKit/IManifestBuilder.h
+++ b/BTBKit/IManifestBuilder.h
@@ -18,12 +18,12 @@ public:
IManifestBuilder& operator=(const IManifestBuilder&) = default;
IManifestBuilder(const IManifestBuilder&) = default;
- /// @brief Builds a target.
+ /// @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) = 0;
+ virtual bool buildTarget(int arg_sz, const char* arg_val, const bool dry_run = false) = 0;
virtual const char* buildSystem() = 0;
};
diff --git a/BTBKit/JSONManifestBuilder.h b/BTBKit/JSONManifestBuilder.h
index b300a6a..38d8004 100644
--- a/BTBKit/JSONManifestBuilder.h
+++ b/BTBKit/JSONManifestBuilder.h
@@ -24,7 +24,7 @@ public:
/// @param arg_val filename path.
/// @retval true build succeeded.
/// @retval false failed to build.
- virtual bool buildTarget(int arg_sz, const char* arg_val) override;
+ virtual bool buildTarget(int arg_sz, const char* arg_val, const bool dry_run = false) override;
virtual const char* buildSystem() override;
};
diff --git a/cli/ManifestBuilder.cc b/cli/ManifestBuilder.cc
index 2c9c4ad..ca88826 100644
--- a/cli/ManifestBuilder.cc
+++ b/cli/ManifestBuilder.cc
@@ -8,6 +8,7 @@
static int kJobCount = 0;
static bool kFailed = false;
+static bool kDryRun = false;
int main(int argc, char** argv)
{
@@ -31,6 +32,11 @@ int main(int argc, char** argv)
return 0;
}
+ else if (index_path == "--dry-run")
+ {
+ kDryRun = true;
+ continue;
+ }
else if (index_path == "-h" ||
index_path == "--help")
{
@@ -51,12 +57,14 @@ int main(int argc, char** argv)
else
{
kFailed = true;
+
+ --kJobCount;
return;
}
std::cout << "btb: building: " << index_path << std::endl;
- if (builder && !builder->buildTarget(index_path.size(), index_path.c_str()))
+ if (builder && !builder->buildTarget(index_path.size(), index_path.c_str(), kDryRun))
{
kFailed = true;
}
@@ -66,6 +74,7 @@ int main(int argc, char** argv)
}
delete builder;
+ builder = nullptr;
--kJobCount;
}, index_path);
@@ -74,7 +83,7 @@ int main(int argc, char** argv)
}
// wait for completion of all jobs.
- while (kJobCount)
+ while (kJobCount > 1)
{
if (kFailed)
{
diff --git a/src/JSONManifestBuilder.cc b/src/JSONManifestBuilder.cc
index 37cefbc..ea0f46d 100644
--- a/src/JSONManifestBuilder.cc
+++ b/src/JSONManifestBuilder.cc
@@ -9,26 +9,29 @@
#include <iostream>
#include <fstream>
+using String = std::string;
using JSON = nlohmann::json;
+namespace FS = std::filesystem;
/// @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.
-bool JSONManifestBuilder::buildTarget(int arg_sz, const char* arg_val)
+bool JSONManifestBuilder::buildTarget(int arg_sz, const char* arg_val, const bool dry_run)
{
- std::string path;
+ String path;
if (arg_sz < 0)
{
+ std::cout << "btb: error: file path is empty." << std::endl;
return false;
}
else
{
path = arg_val;
- if (!std::filesystem::exists(path))
+ if (!FS::exists(path))
{
std::cout << "btb: error: file '" << path << "' does not exist." << std::endl;
return false;
@@ -46,62 +49,73 @@ bool JSONManifestBuilder::buildTarget(int arg_sz, const char* arg_val)
JSON json_obj = JSON::parse(json);
- std::string compiler = json_obj["compiler_path"].get<std::string>();
+ String compiler = json_obj["compiler_path"].get<String>();
JSON header_search_path = json_obj["headers_path"];
JSON sources_files = json_obj["sources_path"];
- std::string command = compiler + " ";
+ String command = compiler + " ";
for (auto& sources : sources_files)
{
- command += sources.get<std::string>() + " ";
+ command += sources.get<String>() + " ";
}
for (auto& headers : header_search_path)
{
- command += "-I" + headers.get<std::string>() + " ";
+ command += "-I" + headers.get<String>() + " ";
}
JSON macros_list = json_obj["cpp_macros"];
for (auto& macro : macros_list)
{
- command += "-D" + macro.get<std::string>() + " ";
+ command += "-D" + macro.get<String>() + " ";
}
JSON compiler_flags = json_obj["compiler_flags"];
for (auto& flag : compiler_flags)
{
- command += flag.get<std::string>() + " ";
+ command += flag.get<String>() + " ";
}
if (json_obj["compiler_std"].is_string())
- command += "-std=" + json_obj["compiler_std"].get<std::string>() + " ";
+ command += "-std=" + json_obj["compiler_std"].get<String>() + " ";
- command += "-o " + json_obj["output_name"].get<std::string>();
+ command += "-o " + json_obj["output_name"].get<String>();
- auto target = json_obj["output_name"].get<std::string>();
+ auto target = json_obj["output_name"].get<String>();
std::cout << "btb: output path: " << target << "\n";
std::cout << "btb: command: " << command << "\n";
- std::system(command.c_str());
+ if (dry_run)
+ {
+ return true;
+ }
+
+ auto ret_exec = std::system(command.c_str());
+
+ if (ret_exec > 0)
+ {
+ std::cout << "btb: 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") ||
- target.ends_with(".dll"))
+ if (target.ends_with(".so"))
{
std::cout << "btb: error: can't open DLL/SO, it mayn't contain an entrypoint." << std::endl;
return true;
}
else if (target.ends_with(".dll"))
{
- auto file = std::ifstream(target);
+ std::ifstream file = std::ifstream(target);
+
std::stringstream ss;
ss << file.rdbuf();