From 602a5b177636ed55e950b239eedfc0d3217b97e1 Mon Sep 17 00:00:00 2001 From: Amlal Date: Sun, 8 Sep 2024 21:27:22 +0200 Subject: [src/ManifestBuilder.cxx] Almost detect ELF executables. [cli/BuilderJSON.cxx] Improve code readiblity. Signed-off-by: Amlal --- src/ManifestBuilder.cxx | 145 ++++++++++++++++++++++++++++++++++++++++++++++++ src/build.cxx | 140 ---------------------------------------------- 2 files changed, 145 insertions(+), 140 deletions(-) create mode 100644 src/ManifestBuilder.cxx delete mode 100644 src/build.cxx (limited to 'src') diff --git a/src/ManifestBuilder.cxx b/src/ManifestBuilder.cxx new file mode 100644 index 0000000..70f3475 --- /dev/null +++ b/src/ManifestBuilder.cxx @@ -0,0 +1,145 @@ +// +// main.cpp +// btb +// +// Created by Amlal on 6/20/24. +// + +#include + +#include + +#include +#include + +#include +#include +#include + +using json = nlohmann::json; + +/// @brief Builds a JSON target. +/// @param arg_sz filename size +/// @param arg_val filename path. +/// @retval true succeeded. +/// @retval false failed. +bool ManifestBuilder::buildJson(int arg_sz, const char* arg_val) +{ + std::string path; + + if (arg_sz < 0) + { + return false; + } + else + { + path = arg_val; + } + + try + { + std::ifstream json_obj(path); + + if (!json_obj.good()) + { + return false; + } + + json buildme = json::parse(json_obj); + + std::string compiler = buildme["compiler_path"].get(); + + json headerSearchPath = buildme["headers_path"]; + json sourceFiles = buildme["sources_path"]; + + std::string cmdLine = compiler + " "; + + for (auto& sources : sourceFiles) + { + cmdLine += sources.get() + " "; + } + + for (auto& headers : headerSearchPath) + { + cmdLine += "-I" + headers.get() + " "; + } + + json macrosList = buildme["cpp_macros"]; + + for (auto& macro : macrosList) + { + cmdLine += "-D" + macro.get() + " "; + } + + json compilerFlags = buildme["compiler_flags"]; + + for (auto& flag : compilerFlags) + { + cmdLine += flag.get() + " "; + } + + if (buildme["compiler_std"].is_string()) + cmdLine += "-std=" + buildme["compiler_std"].get() + " "; + + cmdLine += "-o " + buildme["output_name"].get(); + + std::system(cmdLine.c_str()); + + try + { + if (buildme["run_after_build"].get()) + { + auto target = buildme["output_name"].get(); + + if (target.ends_with(".so") || + target.ends_with(".dll")) + { + 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::stringstream ss; + ss << file.rdbuf(); + + if (ss.str()[0] == 'J' && + ss.str()[1] == 'o' && + ss.str()[2] == 'y' && + ss.str()[3] == '!') + std::cout << "btb: error: can't open Joy! DLL, it maynt't contain an entrypoint." << std::endl; + else if (ss.str()[0] == '!' && + ss.str()[1] == 'y' && + ss.str()[2] == 'o' && + ss.str()[3] == 'J') + std::cout << "btb: error: can't open !yoJ DLL, it maynt't contain an entrypoint." << std::endl; + else if (ss.str()[0] == 'M' && + ss.str()[1] == 'Z') + std::cout << "btb: error: can't open MZ DLL, it maynt't contain an entrypoint." << std::endl; + else if (ss.str()[0] == 0x7F && + ss.str()[1] == 'E') + { + std::cout << "btb: error: can't open ELF DLL, it maynt't contain an entrypoint." << std::endl; + } + + return true; + } + + std::system(("./" + target).c_str()); + } + } + catch (...) + { + // ignore error... + } + } + catch (std::runtime_error& err) + { + std::cout << "btb: error: " << err.what() << std::endl; + perror("btb"); + + return false; + } + + return true; +} diff --git a/src/build.cxx b/src/build.cxx deleted file mode 100644 index b0e759f..0000000 --- a/src/build.cxx +++ /dev/null @@ -1,140 +0,0 @@ -// -// main.cpp -// btb -// -// Created by Amlal on 6/20/24. -// - -#include - -#include - -#include -#include - -#include -#include -#include - -using json = nlohmann::json; - -/// @brief Builds a JSON target. -/// @param arg_sz filename size -/// @param arg_val filename path. -/// @retval true succeeded. -/// @retval false failed. -bool ManifestBuilder::buildJson(int arg_sz, const char* arg_val) -{ - std::string path; - - if (arg_sz < 0) - { - return false; - } - else - { - path = arg_val; - } - - try - { - std::ifstream json_obj(path); - - if (!json_obj.good()) - { - return false; - } - - json buildme = json::parse(json_obj); - - std::string compiler = buildme["compiler_path"].get(); - - json headerSearchPath = buildme["headers_path"]; - json sourceFiles = buildme["sources_path"]; - - std::string cmdLine = compiler + " "; - - for (auto& sources : sourceFiles) - { - cmdLine += sources.get() + " "; - } - - for (auto& headers : headerSearchPath) - { - cmdLine += "-I" + headers.get() + " "; - } - - json macrosList = buildme["cpp_macros"]; - - for (auto& macro : macrosList) - { - cmdLine += "-D" + macro.get() + " "; - } - - json compilerFlags = buildme["compiler_flags"]; - - for (auto& flag : compilerFlags) - { - cmdLine += flag.get() + " "; - } - - if (buildme["compiler_std"].is_string()) - cmdLine += "-std=" + buildme["compiler_std"].get() + " "; - - cmdLine += "-o " + buildme["output_name"].get(); - - std::system(cmdLine.c_str()); - - try - { - if (buildme["run_after_build"].get()) - { - auto target = buildme["output_name"].get(); - - if (target.ends_with(".so") || - target.ends_with(".dll")) - { - 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::stringstream ss; - ss << file.rdbuf(); - - if (ss.str()[0] == 'J' && - ss.str()[1] == 'o' && - ss.str()[2] == 'y' && - ss.str()[3] == '!') - std::cout << "btb: error: can't open PEF DLL, 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') - std::cout << "btb: error: can't open FEP DLL, it mayn't contain an entrypoint." << std::endl; - else if (ss.str()[0] == 'M' && - ss.str()[1] == 'Z') - std::cout << "btb: error: can't open MZ DLL, it mayn't contain an entrypoint." << std::endl; - - return true; - } - - std::system(("./" + target).c_str()); - } - } - catch (...) - { - // ignore... - } - } - catch (std::runtime_error& err) - { - std::cout << "btb: error: " << err.what() << std::endl; - perror("buildme"); - - return false; - } - - return true; -} -- cgit v1.2.3