about summary refs log tree commit diff stats
path: root/wrapperhelper/README.md
diff options
context:
space:
mode:
authorrajdakin <rajdakin@gmail.com>2023-03-31 17:33:40 +0200
committerGitHub <noreply@github.com>2023-03-31 17:33:40 +0200
commit4334a1985b49fcff714c7a023197d1594abeb200 (patch)
treeac28fefe938dd237e28728b498bf3a789768fc40 /wrapperhelper/README.md
parentba03124720ef0c6a52b4b63ab8745a78dc5f7f74 (diff)
downloadbox64-4334a1985b49fcff714c7a023197d1594abeb200.tar.gz
box64-4334a1985b49fcff714c7a023197d1594abeb200.zip
[WRAPPER HELPER] First pass toward a better wrapper helper (#658)
Diffstat (limited to 'wrapperhelper/README.md')
-rw-r--r--wrapperhelper/README.md108
1 files changed, 77 insertions, 31 deletions
diff --git a/wrapperhelper/README.md b/wrapperhelper/README.md
index 1fb2a7e5..cedce959 100644
--- a/wrapperhelper/README.md
+++ b/wrapperhelper/README.md
@@ -1,41 +1,87 @@
-This tool is based on liblcangtooling, by parsing the AST of the library header files, generating the required structures of the wrapping library, including structure definitions, export function signatures, callback function wrapping, etc. Of course, this cannot completely automate, only as a reference. At the same time, this tool is also quite rough, and may even have errors.
+# Wrapper helper
+
+This tool is based on libclangtooling.
+
+It parses the AST of the library header files, generating the required structures of the wrapping library, including:
+- structure definitions,
+- export function signatures,
+- callback function wrapping,
+etc. Of course, this cannot completely automate everything, it can only be used as a reference.
+
+At the same time, this tool is also quite rough, and may even have errors.
+
+## Usage:
 
-Usage:
     helper <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 x64
-            [host_triple]: set host tripe arm32/arm64/x86/x64, default is arm64
-            -- : is necessary
+            <filename>    : set the header file to be parsed
+            <libname>     : set libname required for wrapping func
+            [guest_triple]: set guest triple: can be arm32/arm64/x86/x64, default is x64
+            [host_triple] : set host triple: can be arm32/arm64/x86/x64, default is arm64
+            --            : mandatory
+            <clang_flags> : extra compiler flags
+
+### Usage example:
+
+`./helper /usr/include/jpeglib.h libjpeg x64 arm64 -- -I /usr/lib/gcc/x86_*/12.2.0/include --include /usr/lib/gcc/x86_*/12.2.0/include/stddef.h --include /usr/include/stdio.h`
 
-Usage example:
+You would see an output similar to the files `src/wrapped/wrappedlibjpeg.c` and `src/wrapped/wrappedlibjpeg_private.h`, should they exist.
 
-./helper /usr/include/jpeglib.h libjpeg x64 arm64 --
+If there are multiple header files to process, write them into a custom header file as input.
 
-You will see output similar to wrappedlibjpeg.c file and libjpeg_private.h file. If there are multiple header files to process, write them into a custom header file as input.
+### Output sample
 
-The output may like this:
+Using the command above, we get the following (trimmed) files:
+
+In `wrappedlibjpeg_private.h`:
 ```c
-// libjpeg_private.h
-GOM(jpeg_read_coefficients, pFEp)
-GOM(jpeg_write_coefficients, vFEpp)
-GOM(jpeg_copy_critical_parameters, vFEpp)
-GOM(jpeg_read_icc_profile, uFEppp)
-...
-// wrappedlibjpeg.c
-typedef void (*vFp_t)(struct jpeg_common_struct *  a0);
-typedef void (*vFpu_t)(struct jpeg_common_struct *  a0, int  a1);
-...
-typedef struct jpeg_error_mgr {
-  vFp_t error_exit;
-  vFpu_t emit_message;
-  vFp_t output_messag
-...
-EXPORT void my_jpeg_set_marker_processor(void* emu, struct jpeg_decompress_struct * cinfo, int  marker_code, void *  routine) {
-  // WARN: This function's arg has structure ptr which is special, may be need wrap it for host
-  libjpeg62_my_tmy = (libjpeg62_my_t)my_lib->priv.w.p2;
-  my->jpeg_set_marker_processor(cinfo, marker_code, findjpeg_marker_parser_methodFct(routine))
-}
+...
+GO(jpeg_quality_scaling, iFi)
+...
+GOM(jpeg_destroy, vFEp)
 ...
 ```
 
+In `wrappedlibjpeg.c`:
+```c
+...
+typedef struct jpeg_source_mgr {
+    void *next_input_byte;
+    unsigned long bytes_in_buffer;
+    vFp_t init_source;
+    iFp_t fill_input_buffer;
+    vFpI_t skip_input_data;
+    iFpi_t resync_to_restart;
+    vFp_t term_source;
+} jpeg_source_mgr, *jpeg_source_mgr_ptr;
+...
+#define GO(A) \
+static uintptr_t my_term_source_fct_##A = 0; \
+void  my_term_source_##A(struct jpeg_decompress_struct * a0) { \
+    return RunFunction(my_context, my_term_source_fct_##A, 1, a0); \
+}
+SUPER()
+#undef GO
+static void* findterm_sourceFct(void* fct) {
+    if(!fct) return fct;
+    if(GetNativeFnc((uintptr_t)fct)) return GetNativeFnc((uintptr_t)fct);
+    #define GO(A) if(my_term_source_fct_##A == (uintptr_t)fct) return my_term_source_##A;}
+    SUPER()
+    #undef GO
+    #define GO(A) if(my_term_source_fct_##A == 0) {my_term_source_fct_##A = (uintptr_t)fct;return my_term_source_##A;}
+    SUPER()
+    #undef GO
+    return NULL;
+}
+...
+EXPORT int my_jpeg_quality_scaling(void *emu, int  quality) {
+    libjpeg_my_t *my = (libjpeg_my_t*)my_lib->priv.w.p2;
+    my->jpeg_quality_scaling(quality);
+}
+...
+EXPORT void my_jpeg_destroy(void *emu, struct jpeg_common_struct * cinfo) {
+    // WARN: This function's arg has a structure ptr which is special, may need to wrap it for the host
+    libjpeg_my_t *my = (libjpeg_my_t*)my_lib->priv.w.p2;
+    my->jpeg_destroy(cinfo);
+}
+...
+```