#!/usr/bin/env python3
-import sys, re
+import sys, re, subprocess
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")
consts = Consts(DEBUG, target=target)
+local_git_version = subprocess.check_output(["git", "describe", '--tag', '--dirty']).decode("utf-8").strip()
+
from bindingstypes import *
c_file = ""
write_c("\treturn tuple->" + e + ";\n")
write_c("}\n")
- out_java.write(consts.bindings_header)
+ out_java.write(consts.bindings_header.replace('<git_version_ldk_garbagecollected>', local_git_version))
with open(f"{sys.argv[3]}/structs/CommonBase{consts.file_ext}", "w") as out_java_struct:
out_java_struct.write(consts.common_base)
out_java_struct.write("}\n")
with open(sys.argv[4], "w") as out_c:
- out_c.write(consts.c_file_pfx)
+ out_c.write(consts.c_file_pfx.replace('<git_version_ldk_garbagecollected>', local_git_version))
out_c.write(consts.init_str())
out_c.write(c_file)
with open(f"{sys.argv[3]}/structs/UtilMethods{consts.file_ext}", "a") as util:
System.loadLibrary(\"lightningjni\");
init(java.lang.Enum.class, VecOrSliceDef.class);
init_class_cache();
+ if (!get_lib_version_string().equals(get_ldk_java_bindings_version()))
+ throw new IllegalArgumentException("Compiled LDK library and LDK class failes do not match");
+ // Fetching the LDK versions from C also checks that the header and binaries match
+ get_ldk_c_bindings_version();
+ get_ldk_version();
}
static native void init(java.lang.Class c, java.lang.Class slicedef);
static native void init_class_cache();
+ static native String get_lib_version_string();
+
+ public static String get_ldk_java_bindings_version() {
+ return "<git_version_ldk_garbagecollected>";
+ }
+ public static native String get_ldk_c_bindings_version();
+ public static native String get_ldk_version();
public static native boolean deref_bool(long ptr);
public static native long deref_long(long ptr);
"""
self.c_file_pfx = """#include \"org_ldk_impl_bindings.h\"
-#include <rust_types.h>
#include <lightning.h>
#include <string.h>
#include <stdatomic.h>
// Assert a is true or do nothing
#define CHECK(a) DO_ASSERT(a)
+void __attribute__((constructor)) debug_log_version() {
+ if (check_get_ldk_version() == NULL)
+ DEBUG_PRINT("LDK version did not match the header we built against\\n");
+ if (check_get_ldk_bindings_version() == NULL)
+ DEBUG_PRINT("LDK C Bindings version did not match the header we built against\\n");
+ DEBUG_PRINT("Loaded LDK-Java Bindings with LDK %s and LDK-C-Bindings %s\\n", check_get_ldk_version(), check_get_ldk_bindings_version());
+}
+
// Running a leak check across all the allocations and frees of the JDK is a mess,
// so instead we implement our own naive leak checker here, relying on the -wrap
// linker option to wrap malloc/calloc/realloc/free, tracking everyhing allocated
};
return res;
}
+
+JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1lib_1version_1string(JNIEnv *env, jclass _c) {
+ return str_ref_to_java(env, "<git_version_ldk_garbagecollected>", strlen("<git_version_ldk_garbagecollected>"));
+}
+JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1ldk_1c_1bindings_1version(JNIEnv *env, jclass _c) {
+ return str_ref_to_java(env, check_get_ldk_bindings_version(), strlen(check_get_ldk_bindings_version()));
+}
+JNIEXPORT jstring JNICALL Java_org_ldk_impl_bindings_get_1ldk_1version(JNIEnv *env, jclass _c) {
+ return str_ref_to_java(env, check_get_ldk_version(), strlen(check_get_ldk_version()));
+}
"""
self.hu_struct_file_prefix = """package org.ldk.structs;