about summary refs log tree commit diff stats
path: root/wrapperhelper/main.cpp
diff options
context:
space:
mode:
authorJai A P <jai.jap.318@gmail.com>2023-03-31 18:12:28 +0530
committerGitHub <noreply@github.com>2023-03-31 14:42:28 +0200
commitba03124720ef0c6a52b4b63ab8745a78dc5f7f74 (patch)
tree3a93af738b2b2d86a489a9ae15909a9a50d6520e /wrapperhelper/main.cpp
parent27b1e28f6332c1a1bf79ed2777313f14812e09f9 (diff)
downloadbox64-ba03124720ef0c6a52b4b63ab8745a78dc5f7f74.tar.gz
box64-ba03124720ef0c6a52b4b63ab8745a78dc5f7f74.zip
Add wrapperhelper from box86 (#657)
* Add wrapperhelper (created by @wannacu, from box86)

- Changes as compared to box86 version:
- Default Guest Triple x64
- Default Host Triple arm64
Diffstat (limited to 'wrapperhelper/main.cpp')
-rw-r--r--wrapperhelper/main.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/wrapperhelper/main.cpp b/wrapperhelper/main.cpp
new file mode 100644
index 00000000..1d228b43
--- /dev/null
+++ b/wrapperhelper/main.cpp
@@ -0,0 +1,61 @@
+#include "ast.h"
+#include "utils.h"
+
+
+void dump_usage() {
+    std::string Usage = R"usage(
+        usage: command <filename> <libname> [guest_triple] [host_triple] -- <clang_flags>
+                <filename> : set the header file to be parsed
+                <libname>  : set libname required for wrapping func
+                [guest_triple]: set guest triple arm32/arm64/x86/x64, default is x86
+                [host_triple]: set host tripe arm32/arm64/x86/x64, default is arm32
+                -- : is necessary
+    )usage";
+    std::cerr << Usage << std::endl;
+}
+
+std::string parse_triple(const char* arg) {
+    if (strcmp(arg, "arm32") == 0) {
+        return TripleName[ARM32];
+    } else if (strcmp(arg, "arm64") == 0) {
+        return TripleName[ARM64];
+    } else if (strcmp(arg, "x86") == 0) {
+        return TripleName[X86];
+    } else if (strcmp(arg, "x64") == 0) {
+        return TripleName[X64];
+    } else {
+        return "";
+    }
+}
+
+using namespace clang::tooling;
+int main(int argc, const char* argv[]) {
+    if (argc < 4) {
+        dump_usage();
+        return 0;
+    }
+    std::string libname = argv[2];
+    std::string guest_triple = TripleName[X64];
+    std::string host_triple = TripleName[ARM64];
+    if (argc >= 5) {
+        guest_triple = parse_triple(argv[3]);
+    }
+    if (argc >= 6) {
+        host_triple = parse_triple(argv[4]);
+    }
+    bool has_nessary_tag = false;
+    for (int i = 0; i < argc; i++) {
+        if (strcmp(argv[i], "--") == 0) {
+            has_nessary_tag = true;
+            break;
+        }
+    }
+    if (!has_nessary_tag) {
+        std::cerr << "Please add '--' after triple arg" << std::endl;
+        return 0;
+    }
+    std::string err;
+    auto compile_db = FixedCompilationDatabase::loadFromCommandLine(argc, argv, err);
+    ClangTool Tool(*compile_db, {argv[1]});
+    return Tool.run(std::make_unique<MyFrontendActionFactory>(libname, host_triple, guest_triple).get());
+}