import sys
class Target(Enum):
- CSHARP = 1,
+ WINDOWS = 1,
+ LINUX = 2,
+ PTHREAD = 3,
def first_to_lower(string: str) -> str:
first = string[0]
self.c_file_pfx = self.c_file_pfx + "#include <stdio.h>\n#define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)\n"
- if not DEBUG or sys.platform == "darwin":
+ if self.target == Target.WINDOWS:
+ self.c_file_pfx = self.c_file_pfx + """#include <heapapi.h>
+static HANDLE process_heap = NULL;
+static inline void* init_heap() {
+ if (UNLIKELY(process_heap == NULL)) {
+ // Assume pointer writes wont tear, which is true where we need it.
+ process_heap = GetProcessHeap();
+ }
+}
+static inline void* MALLOC(size_t a, const char* _) {
+ init_heap();
+ return HeapAlloc(process_heap, HEAP_ZERO_MEMORY, a);
+}
+#define do_MALLOC(a, b, _c) MALLOC(a, b)
+#define FREE(p) if ((uint64_t)(p) > 4096) { init_heap(); HeapFree(process_heap, 0, p); }
+#define CHECK_ACCESS(p)
+#define CHECK_INNER_FIELD_ACCESS_OR_NULL(v)
+"""
+ elif not DEBUG or self.target != Target.LINUX:
self.c_file_pfx = self.c_file_pfx + """#define do_MALLOC(a, _b, _c) malloc(a)
#define MALLOC(a, _) malloc(a)
#define FREE(p) if ((uint64_t)(p) > 4096) { free(p); }
}
"""
- if sys.platform != "darwin":
+ if self.target == Target.LINUX:
self.c_file_pfx += """
// 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
target = typescript_strings.Target.NODEJS
if len(sys.argv) == 8 and sys.argv[7] == 'browser':
target = typescript_strings.Target.BROWSER
-elif sys.argv[6] == "c_sharp":
+elif sys.argv[6].startswith("c_sharp"):
import csharp_strings
from csharp_strings import Consts
- target = csharp_strings.Target.CSHARP
+ if sys.argv[6] == "c_sharp-win":
+ target = csharp_strings.Target.WINDOWS
+ elif sys.argv[6] == "c_sharp-darwin":
+ target = csharp_strings.Target.PTHREAD
+ elif sys.argv[6] == "c_sharp-linux":
+ target = csharp_strings.Target.LINUX
+ else:
+ assert False
elif sys.argv[6] == "python":
import python_strings
from python_strings import Consts
echo "Creating C# bindings..."
mkdir -p c_sharp/src/org/ldk/{enums,structs,impl}
rm -f c_sharp/src/org/ldk/{enums,structs,impl}/*.cs
- ./genbindings.py "./lightning.h" c_sharp/src/org/ldk/impl c_sharp/src/org/ldk c_sharp/ $DEBUG_ARG c_sharp $4 $TARGET_STRING
+ GEN_PLAT="c_sharp-linux"
+ [ "$IS_WIN" = "true" ] && GEN_PLAT="c_sharp-win"
+ [ "$IS_MAC" = "true" ] && GEN_PLAT="c_sharp-darwin"
+ ./genbindings.py "./lightning.h" c_sharp/src/org/ldk/impl c_sharp/src/org/ldk c_sharp/ $DEBUG_ARG $GEN_PLAT $4 $TARGET_STRING
rm -f c_sharp/bindings.c
if [ "$3" = "true" ]; then
echo "#define LDK_DEBUG_BUILD" > c_sharp/bindings.c
[ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && LINK="$LINK -fuse-ld=lld"
[ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && echo "WARNING: Need at least upstream clang 13!"
[ "$IS_MAC" = "false" -a "$3" != "false" ] && LINK="$LINK -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,malloc -Wl,-wrap,free"
+ [ "$IS_WIN" = "true" ] && LINK="$LINK --target=x86_64-pc-windows-gnu -lbcrypt -lntdll -static-libgcc"
+ [ "$IS_WIN" = "true" ] && COMPILE="$COMPILE --target=x86_64-pc-windows-gnu -I/usr/x86_64-w64-mingw32/sys-root/mingw/include/ -I/usr/x86_64-w64-mingw32/include/"
if [ "$3" = "true" ]; then
$COMPILE $LINK -o libldkcsharp_debug$LDK_TARGET_SUFFIX.so -g -fsanitize=address -shared-libasan -rdynamic -I"$1"/lightning-c-bindings/include/ c_sharp/bindings.c "$1"/lightning-c-bindings/target/$LDK_TARGET/debug/libldk.a -lm
[ "$IS_APPLE_CLANG" = "false" ] && COMPILE="$COMPILE -flto"
[ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined"
[ "$IS_WIN" = "false" ] && LINK="$LINK -Wl,--lto-O3"
- [ "$IS_WIN" = "true" ] && LINK="$LINK --target=x86_64-pc-windows-gnu -L/usr/lib/gcc/x86_64-w64-mingw32/12-win32/ -lbcrypt -lntdll -static-libgcc"
- [ "$IS_WIN" = "true" ] && COMPILE="$COMPILE --target=x86_64-pc-windows-gnu"
LDK_LIB="$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
if [ "$IS_MAC" = "false" -a "$IS_WIN" = "false" -a "$4" = "false" ]; then
LINK="$LINK -Wl,--version-script=c_sharp/libcode.version -Wl,--build-id=0x0000000000000000"