From: Matt Corallo Date: Wed, 26 Aug 2020 16:36:54 +0000 (-0400) Subject: New genbindings X-Git-Tag: v0.0.1~167 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=75dd8a14a70c7aaacd4c2f0f6b99e0793640874b;p=ldk-java New genbindings --- diff --git a/genbindings.py b/genbindings.py index 851bbdb6..a867f558 100755 --- a/genbindings.py +++ b/genbindings.py @@ -120,8 +120,8 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java, open(sys.arg if ret_conv is not None: ret_conv_pfx, ret_conv_sfx = ret_conv - out_java.write(" " + re_match.group(2).replace('_', '') + "(") - out_c.write(" JNICALL " + re_match.group(2).replace('_', '') + "(JNIEnv * _env, jclass _b") + out_java.write(" " + re_match.group(2) + "(") + out_c.write(" JNICALL " + re_match.group(2).replace('_', '_1') + "(JNIEnv * _env, jclass _b") arg_names = [] for idx, arg in enumerate(re_match.group(3).split(',')): @@ -160,7 +160,7 @@ with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java, open(sys.arg public class bindings { static { - System.loadLibrary(\"lightning\"); + System.loadLibrary(\"lightningjni\"); } """) @@ -170,21 +170,34 @@ public class bindings { in_block_comment = False in_block_enum = False - in_block_struct = False + cur_block_struct = None in_block_union = False fn_ptr_regex = re.compile("^extern const ([A-Za-z_0-9\* ]*) \(\*(.*)\)\((.*)\);$") fn_ret_arr_regex = re.compile("(.*) \(\*(.*)\((.*)\)\)\[([0-9]*)\];$") reg_fn_regex = re.compile("([A-Za-z_0-9\* ]* \*?)([a-zA-Z_0-9]*)\((.*)\);$") + const_val_regex = re.compile("^extern const ([A-Za-z_0-9]*) ([A-Za-z_0-9]*);$") for line in in_h: if in_block_comment: #out_java.write("\t" + line) if line.endswith("*/\n"): in_block_comment = False - elif in_block_struct: + elif cur_block_struct is not None: + cur_block_struct = cur_block_struct + line if line.startswith("} "): - in_block_struct = False + field_lines = [] + struct_lines = cur_block_struct.split("\n") + for idx, struct_line in enumerate(struct_lines): + if struct_line.strip().startswith("/*"): + in_block_comment = True + if in_block_comment: + if struct_line.endswith("*/"): + in_block_comment = False + else: + field_lines.append(struct_line) + #out_java.write("".join(field_lines) + "\n") + cur_block_struct = None elif in_block_union: if line.startswith("} "): in_block_union = False @@ -195,6 +208,7 @@ public class bindings { fn_ptr = fn_ptr_regex.match(line) fn_ret_arr = fn_ret_arr_regex.match(line) reg_fn = reg_fn_regex.match(line) + const_val = const_val_regex.match(line) if line.startswith("#include <"): pass @@ -205,7 +219,7 @@ public class bindings { elif line.startswith("typedef enum "): in_block_enum = True elif line.startswith("typedef struct "): - in_block_struct = True + cur_block_struct = line elif line.startswith("typedef union "): in_block_union = True elif line.startswith("typedef "): @@ -216,6 +230,9 @@ public class bindings { map_fn(fn_ret_arr, fn_ret_arr.group(4)) elif reg_fn is not None: map_fn(reg_fn, None) + elif const_val_regex is not None: + # TODO Map const variables + pass else: assert(line == "\n") diff --git a/genbindings.sh b/genbindings.sh index 9ddaa5b0..16f6ef98 100755 --- a/genbindings.sh +++ b/genbindings.sh @@ -7,4 +7,5 @@ fi set -e ./genbindings.py "$1/lightning-c-bindings/include/lightning.h" src/main/java/org/ldk/bindings.java src/main/jni/bindings.c javac -h src/main/jni src/main/java/org/ldk/bindings.java -gcc -shared -fPIC -Wno-pointer-sign -Wall -Isrc/main/jni -I"$1/lightning-c-bindings/include/" $2 src/main/jni/bindings.c "$1"/target/debug/liblightning.a +rm src/main/java/org/ldk/bindings.class +gcc -o liblightningjni.so -shared -fPIC -Wno-pointer-sign -Wall -Isrc/main/jni -I"$1/lightning-c-bindings/include/" $2 src/main/jni/bindings.c "$1"/target/debug/liblightning.a