X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=genbindings.py;h=0e20b2a2548f5c6095c89a2b97aa422ac0b139b4;hb=6119d62e2597737125c7e37fe5b13065b601e0e9;hp=b744da936bf0bc202d6c925b7f9c89c3428b24ff;hpb=ca0405752b09a0fb820d4be6f1e14a346614f21d;p=ldk-java diff --git a/genbindings.py b/genbindings.py index b744da93..0e20b2a2 100755 --- a/genbindings.py +++ b/genbindings.py @@ -1,31 +1,26 @@ #!/usr/bin/env python3 import sys, re -import config -from config import Language -import os +if len(sys.argv) != 7: + print("USAGE: /path/to/lightning.h /path/to/bindings/output /path/to/bindings/ /path/to/bindings/output.c debug lang") + sys.exit(1) -configuration = config.setup() +if sys.argv[5] == "false": + DEBUG = False +elif sys.argv[5] == "true": + DEBUG = True +else: + print("debug should be true or false and indicates whether to track allocations and ensure we don't leak") + sys.exit(1) -output_language = configuration.language -if output_language == Language.Java: +if sys.argv[6] == "java": from java_strings import Consts -elif output_language == Language.TypeScript: +elif sys.argv[6] == "typescript": from typescript_strings import Consts else: print("Only java or typescript can be set for lang") sys.exit(1) - -consts = Consts(configuration.debug) - -path_to_lightning_header = configuration.lightning_h_path -path_to_output_blob = configuration.output_blob_path -path_to_bindings_directory = configuration.bindings_output_directory_path -path_to_c_file = configuration.output_c_path - - - - +consts = Consts(DEBUG) from bindingstypes import * @@ -60,14 +55,6 @@ def camel_to_snake(s): lastund = True return (ret + lastchar.lower()).strip("_") - -def recursiveOpenFile(path, *args): - output_directory_path = os.path.dirname(path) - if not os.path.isdir(output_directory_path): - os.makedirs(output_directory_path) - return open(path, *args) - - unitary_enums = set() complex_enums = set() opaque_structs = set() @@ -328,7 +315,7 @@ reg_fn_regex = re.compile("([A-Za-z_0-9\* ]* \*?)([a-zA-Z_0-9]*)\((.*)\);$") clone_fns = set() constructor_fns = {} c_array_class_caches = set() -with open(path_to_lightning_header) as in_h: +with open(sys.argv[1]) as in_h: for line in in_h: reg_fn = reg_fn_regex.match(line) if reg_fn is not None: @@ -352,7 +339,7 @@ write_c("static inline struct LDKThirtyTwoBytes ThirtyTwoBytes_clone(const struc java_c_types_none_allowed = False # C structs created by cbindgen are declared in dependency order -with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as out_java: +with open(sys.argv[1]) as in_h, open(sys.argv[2], "w") as out_java: def map_type(fn_arg, print_void, ret_arr_len, is_free, holds_ref): ty_info = java_c_types(fn_arg, ret_arr_len) return map_type_with_info(ty_info, print_void, ret_arr_len, is_free, holds_ref) @@ -751,8 +738,6 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o to_hu_conv = "TODO 3", to_hu_conv_name = None, from_hu_conv = None) # its a pointer, no conv needed assert False # We should have handled every case by now. - - def map_fn(line, re_match, ret_arr_len, c_call_string): out_java.write("\t// " + line) out_java.write("\tpublic static native ") @@ -803,7 +788,7 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o out_java_struct = None if ("LDK" + struct_meth in opaque_structs or "LDK" + struct_meth in trait_structs) and not is_free: - out_java_struct = open(f"{path_to_bindings_directory}/structs/{struct_meth}.{consts.file_extension}", "a") + out_java_struct = open(f"{sys.argv[3]}/structs/{struct_meth}{consts.file_ext}", "a") if not args_known: out_java_struct.write("\t// Skipped " + re_match.group(2) + "\n") out_java_struct.close() @@ -924,7 +909,7 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o out_java_struct.close() def map_unitary_enum(struct_name, field_lines): - with recursiveOpenFile(f"{path_to_bindings_directory}/enums/{struct_name}.{consts.file_extension}", "w") as out_java_enum: + with open(f"{sys.argv[3]}/enums/{struct_name}{consts.file_ext}", "w") as out_java_enum: unitary_enums.add(struct_name) for idx, struct_line in enumerate(field_lines): if idx == 0: @@ -939,112 +924,19 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o write_c(c_out) out_java_enum.write(native_file_out) out_java.write(native_out) - + def map_complex_enum(struct_name, union_enum_items): java_hu_type = struct_name.replace("LDK", "") complex_enums.add(struct_name) - with open(sys.argv[3] + "/structs/" + java_hu_type + ".java", "w") as out_java_enum: - out_java_enum.write(consts.hu_struct_file_prefix) - out_java_enum.write("public class " + java_hu_type + " extends CommonBase {\n") - out_java_enum.write("\tprivate " + java_hu_type + "(Object _dummy, long ptr) { super(ptr); }\n") - out_java_enum.write("\t@Override @SuppressWarnings(\"deprecation\")\n") - out_java_enum.write("\tprotected void finalize() throws Throwable {\n") - out_java_enum.write("\t\tsuper.finalize();\n") - out_java_enum.write("\t\tif (ptr != 0) { bindings." + java_hu_type + "_free(ptr); }\n") - out_java_enum.write("\t}\n") - out_java_enum.write("\tstatic " + java_hu_type + " constr_from_ptr(long ptr) {\n") - out_java_enum.write("\t\tbindings." + struct_name + " raw_val = bindings." + struct_name + "_ref_from_ptr(ptr);\n") - java_hu_subclasses = "" - - tag_field_lines = union_enum_items["field_lines"] - init_meth_jty_strs = {} - for idx, struct_line in enumerate(tag_field_lines): - if idx == 0: - assert(struct_line == "typedef enum %s_Tag {" % struct_name) - elif idx == len(tag_field_lines) - 3: - assert(struct_line.endswith("_Sentinel,")) - elif idx == len(tag_field_lines) - 2: - assert(struct_line == "} %s_Tag;" % struct_name) - elif idx == len(tag_field_lines) - 1: - assert(struct_line == "") + with open(f"{sys.argv[3]}/structs/{java_hu_type}{consts.file_ext}", "w") as out_java_enum: + (out_java_addendum, out_java_enum_addendum, out_c_addendum) = consts.map_complex_enum(struct_name, union_enum_items, map_type, camel_to_snake) - out_java.write("\tpublic static class " + struct_name + " {\n") - out_java.write("\t\tprivate " + struct_name + "() {}\n") - for idx, struct_line in enumerate(tag_field_lines): - if idx != 0 and idx < len(tag_field_lines) - 3: - var_name = struct_line.strip(' ,')[len(struct_name) + 1:] - out_java.write("\t\tpublic final static class " + var_name + " extends " + struct_name + " {\n") - java_hu_subclasses = java_hu_subclasses + "\tpublic final static class " + var_name + " extends " + java_hu_type + " {\n" - out_java_enum.write("\t\tif (raw_val.getClass() == bindings." + struct_name + "." + var_name + ".class) {\n") - out_java_enum.write("\t\t\treturn new " + var_name + "(ptr, (bindings." + struct_name + "." + var_name + ")raw_val);\n") - init_meth_jty_str = "" - init_meth_params = "" - init_meth_body = "" - hu_conv_body = "" - if "LDK" + var_name in union_enum_items: - enum_var_lines = union_enum_items["LDK" + var_name] - for idx, field in enumerate(enum_var_lines): - if idx != 0 and idx < len(enum_var_lines) - 2: - field_ty = map_type(field.strip(' ;'), False, None, False, True) - out_java.write("\t\t\tpublic " + field_ty.java_ty + " " + field_ty.arg_name + ";\n") - java_hu_subclasses = java_hu_subclasses + "\t\tpublic final " + field_ty.java_hu_ty + " " + field_ty.arg_name + ";\n" - if field_ty.to_hu_conv is not None: - hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.java_ty + " " + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n" - hu_conv_body = hu_conv_body + "\t\t\t" + field_ty.to_hu_conv.replace("\n", "\n\t\t\t") + "\n" - hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = " + field_ty.to_hu_conv_name + ";\n" - else: - hu_conv_body = hu_conv_body + "\t\t\tthis." + field_ty.arg_name + " = obj." + field_ty.arg_name + ";\n" - init_meth_jty_str = init_meth_jty_str + field_ty.java_fn_ty_arg - if idx > 1: - init_meth_params = init_meth_params + ", " - init_meth_params = init_meth_params + field_ty.java_ty + " " + field_ty.arg_name - init_meth_body = init_meth_body + "this." + field_ty.arg_name + " = " + field_ty.arg_name + "; " - out_java.write("\t\t\t" + var_name + "(" + init_meth_params + ") { ") - out_java.write(init_meth_body) - out_java.write("}\n") - out_java.write("\t\t}\n") - out_java_enum.write("\t\t}\n") - java_hu_subclasses = java_hu_subclasses + "\t\tprivate " + var_name + "(long ptr, bindings." + struct_name + "." + var_name + " obj) {\n\t\t\tsuper(null, ptr);\n" - java_hu_subclasses = java_hu_subclasses + hu_conv_body - java_hu_subclasses = java_hu_subclasses + "\t\t}\n\t}\n" - init_meth_jty_strs[var_name] = init_meth_jty_str - out_java.write("\t\tstatic native void init();\n") - out_java.write("\t}\n") - out_java_enum.write("\t\tassert false; return null; // Unreachable without extending the (internal) bindings interface\n\t}\n\n") - out_java_enum.write(java_hu_subclasses) - out_java.write("\tstatic { " + struct_name + ".init(); }\n") - out_java.write("\tpublic static native " + struct_name + " " + struct_name + "_ref_from_ptr(long ptr);\n"); - - write_c(consts.c_complex_enum_pfx(struct_name, [x.strip(", ")[len(struct_name) + 1:] for x in tag_field_lines[1:-3]], init_meth_jty_strs)) - - write_c(consts.c_fn_ty_pfx + consts.c_complex_enum_pass_ty(struct_name) + " " + consts.c_fn_name_pfx + struct_name.replace("_", "_1") + "_1ref_1from_1ptr (" + consts.c_fn_args_pfx + ", " + consts.ptr_c_ty + " ptr) {\n") - write_c("\t" + struct_name + " *obj = (" + struct_name + "*)ptr;\n") - write_c("\tswitch(obj->tag) {\n") - for idx, struct_line in enumerate(tag_field_lines): - if idx != 0 and idx < len(tag_field_lines) - 3: - var_name = struct_line.strip(' ,')[len(struct_name) + 1:] - write_c("\t\tcase " + struct_name + "_" + var_name + ": {\n") - c_params = [] - if "LDK" + var_name in union_enum_items: - enum_var_lines = union_enum_items["LDK" + var_name] - for idx, field in enumerate(enum_var_lines): - if idx != 0 and idx < len(enum_var_lines) - 2: - field_map = map_type(field.strip(' ;'), False, None, False, True) - if field_map.ret_conv is not None: - write_c("\t\t\t" + field_map.ret_conv[0].replace("\n", "\n\t\t\t")) - write_c("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name) - write_c(field_map.ret_conv[1].replace("\n", "\n\t\t\t") + "\n") - c_params.append(field_map.ret_conv_name) - else: - c_params.append("obj->" + camel_to_snake(var_name) + "." + field_map.arg_name) - write_c("\t\t\treturn " + consts.c_constr_native_complex_enum(struct_name, var_name, c_params) + ";\n") - write_c("\t\t}\n") - write_c("\t\tdefault: abort();\n") - write_c("\t}\n}\n") - out_java_enum.write("}\n") + out_java_enum.write(out_java_enum_addendum) + out_java.write(out_java_addendum) + write_c(out_c_addendum) def map_trait(struct_name, field_var_lines, trait_fn_lines): - with open(f"{path_to_bindings_directory}/structs/{struct_name.replace('LDK', '')}.{consts.file_extension}", "w") as out_java_trait: + with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_trait: field_var_convs = [] for var_line in field_var_lines: if var_line.group(1) in trait_structs: @@ -1287,7 +1179,7 @@ with open(path_to_lightning_header) as in_h, open(path_to_output_blob, "w") as o def map_result(struct_name, res_ty, err_ty): result_types.add(struct_name) human_ty = struct_name.replace("LDKCResult", "Result") - with open(f"{path_to_bindings_directory}/structs/{human_ty}.{consts.file_extension}", "w") as out_java_struct: + with open(f"{sys.argv[3]}/structs/{human_ty}{consts.file_ext}", "w") as out_java_struct: out_java_struct.write(consts.hu_struct_file_prefix) out_java_struct.write("public class " + human_ty + " extends CommonBase {\n") out_java_struct.write("\tprivate " + human_ty + "(Object _dummy, long ptr) { super(ptr); }\n") @@ -1510,7 +1402,7 @@ public class bindings { """) - with recursiveOpenFile(f"{path_to_bindings_directory}/structs/CommonBase.{consts.file_extension}", "w") as out_java_struct: + with open(f"{sys.argv[3]}/structs/CommonBase{consts.file_ext}", "a") as out_java_struct: out_java_struct.write(consts.common_base) in_block_comment = False @@ -1601,7 +1493,7 @@ public class bindings { if is_opaque: opaque_structs.add(struct_name) - with open(f"{path_to_bindings_directory}/structs/{struct_name.replace('LDK', '')}.{consts.file_extension}", "w") as out_java_struct: + with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "w") as out_java_struct: out_java_struct.write(consts.hu_struct_file_prefix) out_java_struct.write("public class " + struct_name.replace("LDK","") + " extends CommonBase") if struct_name.startswith("LDKLocked"): @@ -1687,7 +1579,7 @@ public class bindings { trait_structs.add(struct_name) map_trait(struct_name, field_var_lines, trait_fn_lines) elif struct_name == "LDKTxOut": - with open(f"{path_to_bindings_directory}/structs/TxOut.{consts.file_extension}", "w") as out_java_struct: + with open(f"{sys.argv[3]}/structs/TxOut{consts.file_ext}", "w") as out_java_struct: out_java_struct.write(consts.hu_struct_file_prefix) out_java_struct.write("public class TxOut extends CommonBase{\n") out_java_struct.write("\tTxOut(java.lang.Object _dummy, long ptr) { super(ptr); }\n") @@ -1734,13 +1626,12 @@ public class bindings { out_java.write("}\n") for struct_name in opaque_structs: - with open(f"{path_to_bindings_directory}/structs/{struct_name.replace('LDK', '')}.{consts.file_extension}", "a") as out_java_struct: + with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "a") as out_java_struct: out_java_struct.write("}\n") for struct_name in trait_structs: - with open(f"{path_to_bindings_directory}/structs/{struct_name.replace('LDK', '')}.{consts.file_extension}", "a") as out_java_struct: + with open(f"{sys.argv[3]}/structs/{struct_name.replace('LDK', '')}{consts.file_ext}", "a") as out_java_struct: out_java_struct.write("}\n") -with open(path_to_c_file, "w") as out_c: +with open(sys.argv[4], "w") as out_c: out_c.write(consts.c_file_pfx) out_c.write(consts.init_str(c_array_class_caches)) out_c.write(c_file) -