Skip to content

Commit 0a4ade7

Browse files
authored
Merge pull request #147 from TheBlueMatt/main
Various C# build fixes
2 parents 239d70e + 0cfcfc4 commit 0a4ade7

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

.github/workflows/build.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,14 @@ jobs:
195195
cd ldk-c-bindings
196196
export LDK_C_BINDINGS_EXTRA_TARGETS=x86_64-pc-windows-gnu
197197
export LDK_C_BINDINGS_EXTRA_TARGET_CCS=`pwd`/deterministic-build-wrappers/clang-x86_64-windows
198+
export LDK_C_BINDINGS_EXTRA_TARGET_LINK_LTO=true
198199
./genbindings.sh ../rust-lightning true
199200
- name: Remove checked-in source to ensure its correctly checked-in
200201
run: rm c_sharp/src/org/ldk/enums/*.cs c_sharp/src/org/ldk/impl/*.cs c_sharp/src/org/ldk/structs/*.cs
201202
- name: Build Windows C# Bindings
202203
run: |
203204
export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(git describe --tag HEAD)"
204-
LDK_TARGET=x86_64-pc-windows-gnu ./genbindings.sh ./ldk-c-bindings/ c_sharp false false
205+
LDK_TARGET=x86_64-pc-windows-gnu LDK_TARGET_CPU=sandybridge ./genbindings.sh ./ldk-c-bindings/ c_sharp false false
205206
- name: Build Linux C# Bindings
206207
run: |
207208
export LDK_GARBAGECOLLECTED_GIT_OVERRIDE="$(git describe --tag HEAD)"

csharp_strings.py

+23-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import sys
44

55
class Target(Enum):
6-
CSHARP = 1,
6+
WINDOWS = 1,
7+
LINUX = 2,
8+
PTHREAD = 3,
79

810
def first_to_lower(string: str) -> str:
911
first = string[0]
@@ -188,7 +190,25 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
188190

189191
self.c_file_pfx = self.c_file_pfx + "#include <stdio.h>\n#define DEBUG_PRINT(...) fprintf(stderr, __VA_ARGS__)\n"
190192

191-
if not DEBUG or sys.platform == "darwin":
193+
if self.target == Target.WINDOWS:
194+
self.c_file_pfx = self.c_file_pfx + """#include <heapapi.h>
195+
static HANDLE process_heap = NULL;
196+
static inline void* init_heap() {
197+
if (UNLIKELY(process_heap == NULL)) {
198+
// Assume pointer writes wont tear, which is true where we need it.
199+
process_heap = GetProcessHeap();
200+
}
201+
}
202+
static inline void* MALLOC(size_t a, const char* _) {
203+
init_heap();
204+
return HeapAlloc(process_heap, HEAP_ZERO_MEMORY, a);
205+
}
206+
#define do_MALLOC(a, b, _c) MALLOC(a, b)
207+
#define FREE(p) if ((uint64_t)(p) > 4096) { init_heap(); HeapFree(process_heap, 0, p); }
208+
#define CHECK_ACCESS(p)
209+
#define CHECK_INNER_FIELD_ACCESS_OR_NULL(v)
210+
"""
211+
elif not DEBUG or self.target != Target.LINUX:
192212
self.c_file_pfx = self.c_file_pfx + """#define do_MALLOC(a, _b, _c) malloc(a)
193213
#define MALLOC(a, _) malloc(a)
194214
#define FREE(p) if ((uint64_t)(p) > 4096) { free(p); }
@@ -214,7 +234,7 @@ def __init__(self, DEBUG: bool, target: Target, outdir: str, **kwargs):
214234
}
215235
"""
216236

217-
if sys.platform != "darwin":
237+
if self.target == Target.LINUX:
218238
self.c_file_pfx += """
219239
// Running a leak check across all the allocations and frees of the JDK is a mess,
220240
// so instead we implement our own naive leak checker here, relying on the -wrap

genbindings.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,17 @@
2828
target = typescript_strings.Target.NODEJS
2929
if len(sys.argv) == 8 and sys.argv[7] == 'browser':
3030
target = typescript_strings.Target.BROWSER
31-
elif sys.argv[6] == "c_sharp":
31+
elif sys.argv[6].startswith("c_sharp"):
3232
import csharp_strings
3333
from csharp_strings import Consts
34-
target = csharp_strings.Target.CSHARP
34+
if sys.argv[6] == "c_sharp-win":
35+
target = csharp_strings.Target.WINDOWS
36+
elif sys.argv[6] == "c_sharp-darwin":
37+
target = csharp_strings.Target.PTHREAD
38+
elif sys.argv[6] == "c_sharp-linux":
39+
target = csharp_strings.Target.LINUX
40+
else:
41+
assert False
3542
elif sys.argv[6] == "python":
3643
import python_strings
3744
from python_strings import Consts

genbindings.sh

+20-19
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ if [ "$LDK_TARGET_CPU" = "" ]; then
7575
fi
7676

7777
COMMON_COMPILE="$CC -std=c11 -Wall -Wextra -Wno-unused-parameter -Wno-ignored-qualifiers -Wno-unused-function -Wno-nullability-completeness -Wno-pointer-sign -Wdate-time -ffile-prefix-map=$(pwd)="
78-
[ "$IS_MAC" = "true" -a "$2" != "wasm" ] && COMMON_COMPILE="$COMMON_COMPILE --target=$TARGET_STRING -mcpu=$LDK_TARGET_CPU"
78+
COMMON_CC=""
79+
[[ "$TARGET_STRING" != "x86"* ]] && COMMON_CC="$COMMON_CC --target=$TARGET_STRING -mcpu=$LDK_TARGET_CPU"
80+
[[ "$TARGET_STRING" = "x86"* ]] && COMMON_CC="$COMMON_CC --target=$TARGET_STRING -march=$LDK_TARGET_CPU -mtune=$LDK_TARGET_CPU"
81+
[ "$IS_MAC" = "true" -a "$MACOS_SDK" != "" ] && COMMON_COMPILE="$COMMON_COMPILE -isysroot $MACOS_SDK"
7982

8083
DEBUG_ARG="$3"
8184
if [ "$3" = "leaks" ]; then
@@ -106,7 +109,10 @@ if [ "$2" = "c_sharp" ]; then
106109
echo "Creating C# bindings..."
107110
mkdir -p c_sharp/src/org/ldk/{enums,structs,impl}
108111
rm -f c_sharp/src/org/ldk/{enums,structs,impl}/*.cs
109-
./genbindings.py "./lightning.h" c_sharp/src/org/ldk/impl c_sharp/src/org/ldk c_sharp/ $DEBUG_ARG c_sharp $4 $TARGET_STRING
112+
GEN_PLAT="c_sharp-linux"
113+
[ "$IS_WIN" = "true" ] && GEN_PLAT="c_sharp-win"
114+
[ "$IS_MAC" = "true" ] && GEN_PLAT="c_sharp-darwin"
115+
./genbindings.py "./lightning.h" c_sharp/src/org/ldk/impl c_sharp/src/org/ldk c_sharp/ $DEBUG_ARG $GEN_PLAT $4 $TARGET_STRING
110116
rm -f c_sharp/bindings.c
111117
if [ "$3" = "true" ]; then
112118
echo "#define LDK_DEBUG_BUILD" > c_sharp/bindings.c
@@ -119,11 +125,6 @@ if [ "$2" = "c_sharp" ]; then
119125
cat header.c >> c_sharp/bindings.c
120126
cat c_sharp/bindings.c.body >> c_sharp/bindings.c
121127

122-
IS_MAC=false
123-
[ "$($CC --version | grep apple-darwin)" != "" ] && IS_MAC=true
124-
IS_APPLE_CLANG=false
125-
[ "$($CC --version | grep "Apple clang version")" != "" ] && IS_APPLE_CLANG=true
126-
127128
if is_gnu_sed; then
128129
sed -i "s/<version>.*<\/version>/<version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" c_sharp/packaging_artifacts/org.ldk.nuspec
129130
sed -i "s/<version>.*<\/version>/<version>${LDK_GARBAGECOLLECTED_GIT_OVERRIDE:1:100}<\/version>/g" c_sharp/packaging_artifacts/package/services/metadata/core-properties/ldk.psmdcp
@@ -143,14 +144,16 @@ if [ "$2" = "c_sharp" ]; then
143144
fi
144145

145146
echo "Building C# bindings..."
146-
COMPILE="$COMMON_COMPILE -pthread -fPIC"
147-
LINK="-shared"
147+
COMPILE="$COMMON_COMPILE $COMMON_CC -pthread -fPIC"
148+
LINK="$COMMON_CC -shared"
148149
[ "$IS_WIN" = "false" ] && LINK="$LINK -ldl"
149150
[ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined"
150151
[ "$IS_MAC" = "true" ] && COMPILE="$COMPILE -mmacosx-version-min=10.9"
151152
[ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && LINK="$LINK -fuse-ld=lld"
152153
[ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && echo "WARNING: Need at least upstream clang 13!"
153154
[ "$IS_MAC" = "false" -a "$3" != "false" ] && LINK="$LINK -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,malloc -Wl,-wrap,free"
155+
[ "$IS_WIN" = "true" ] && LINK="$LINK -lbcrypt -lntdll -static-libgcc"
156+
[ "$IS_WIN" = "true" ] && COMPILE="$COMPILE -I/usr/x86_64-w64-mingw32/sys-root/mingw/include/ -I/usr/x86_64-w64-mingw32/include/"
154157

155158
if [ "$3" = "true" ]; then
156159
$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
@@ -159,8 +162,6 @@ if [ "$2" = "c_sharp" ]; then
159162
[ "$IS_APPLE_CLANG" = "false" ] && COMPILE="$COMPILE -flto"
160163
[ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined"
161164
[ "$IS_WIN" = "false" ] && LINK="$LINK -Wl,--lto-O3"
162-
[ "$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"
163-
[ "$IS_WIN" = "true" ] && COMPILE="$COMPILE --target=x86_64-pc-windows-gnu"
164165
LDK_LIB="$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a
165166
if [ "$IS_MAC" = "false" -a "$IS_WIN" = "false" -a "$4" = "false" ]; then
166167
LINK="$LINK -Wl,--version-script=c_sharp/libcode.version -Wl,--build-id=0x0000000000000000"
@@ -170,7 +171,7 @@ if [ "$2" = "c_sharp" ]; then
170171
# so we have to build with faketime.
171172
faketime -f "2021-01-01 00:00:00" $COMPILE -o bindings.o -c -O3 -I"$1"/lightning-c-bindings/include/ c_sharp/bindings.c
172173
faketime -f "2021-01-01 00:00:00" $COMPILE $LINK -o libldkcsharp_release$LDK_TARGET_SUFFIX.so -O3 bindings.o $LDK_LIB -lm
173-
[ "$IS_APPLE_CLANG" != "true" ] && llvm-strip libldkcsharp_release$LDK_TARGET_SUFFIX.so
174+
[ "$IS_APPLE_CLANG" != "true" ] && llvm-strip -R .llvmbc -R .llvmcmd libldkcsharp_release$LDK_TARGET_SUFFIX.so
174175

175176
if [ "$LDK_JAR_TARGET" = "true" ]; then
176177
# Copy resulting native binary for inclusion in release nuget zip
@@ -210,8 +211,8 @@ elif [ "$2" = "python" ]; then
210211
[ "$($CC --version | grep "Apple clang version")" != "" ] && IS_APPLE_CLANG=true
211212

212213
echo "Building Python bindings..."
213-
COMPILE="$COMMON_COMPILE -Isrc/main/jni -pthread -fPIC"
214-
LINK="-ldl -shared"
214+
COMPILE="$COMMON_COMPILE $COMMON_CC -Isrc/main/jni -pthread -fPIC"
215+
LINK="$COMMON_CC -ldl -shared"
215216
[ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined"
216217
[ "$IS_MAC" = "true" ] && COMPILE="$COMPILE -mmacosx-version-min=10.9"
217218
[ "$IS_MAC" = "true" -a "$IS_APPLE_CLANG" = "false" ] && LINK="$LINK -fuse-ld=lld"
@@ -224,7 +225,7 @@ elif [ "$2" = "python" ]; then
224225
else
225226
$COMPILE -o bindings.o -c -flto -O3 -I"$1"/lightning-c-bindings/include/ $2 c_sharp/bindings.c
226227
$COMPILE $LINK -o liblightningpython_release$LDK_TARGET_SUFFIX.so -Wl,--version-script=python/libcode.version -flto -O3 -Wl,--lto-O3 -Wl,-O3 -I"$1"/lightning-c-bindings/include/ $2 bindings.o "$1"/lightning-c-bindings/target/$LDK_TARGET/release/libldk.a -lm
227-
[ "$IS_APPLE_CLANG" != "true" ] && llvm-strip liblightningpython_release$LDK_TARGET_SUFFIX.so
228+
[ "$IS_APPLE_CLANG" != "true" ] && llvm-strip -R .llvmbc -R .llvmcmd liblightningpython_release$LDK_TARGET_SUFFIX.so
228229
fi
229230
elif [ "$2" = "wasm" ]; then
230231
echo "Creating TS bindings..."
@@ -250,7 +251,7 @@ elif [ "$2" = "wasm" ]; then
250251
cat ts/bindings.c.body >> ts/bindings.c
251252

252253
echo "Building TS bindings..."
253-
COMPILE="$COMMON_COMPILE -flto -Wl,--no-entry -nostdlib --target=wasm32-wasi -Wl,-z -Wl,stack-size=$((8*1024*1024)) -Wl,--initial-memory=$((16*1024*1024)) -Wl,--max-memory=$((1024*1024*1024)) -Wl,--global-base=4096"
254+
COMPILE="$COMMON_COMPILE $COMMON_CC -flto -Wl,--no-entry -nostdlib --target=wasm32-wasi -Wl,-z -Wl,stack-size=$((8*1024*1024)) -Wl,--initial-memory=$((16*1024*1024)) -Wl,--max-memory=$((1024*1024*1024)) -Wl,--global-base=4096"
254255
# We only need malloc and assert/abort, but for now just use WASI for those:
255256
EXTRA_LINK=/usr/lib/wasm32-wasi/libc.a
256257
[ "$3" != "false" ] && COMPILE="$COMPILE -Wl,-wrap,calloc -Wl,-wrap,realloc -Wl,-wrap,reallocarray -Wl,-wrap,malloc -Wl,-wrap,aligned_alloc -Wl,-wrap,free"
@@ -317,8 +318,8 @@ else
317318
rm src/main/java/org/ldk/enums/*.class src/main/java/org/ldk/impl/bindings*.class
318319

319320
echo "Building Java bindings..."
320-
COMPILE="$COMMON_COMPILE -Isrc/main/jni -pthread -fPIC"
321-
LINK="-shared"
321+
COMPILE="$COMMON_COMPILE $COMMON_CC -Isrc/main/jni -pthread -fPIC"
322+
LINK="$COMMON_CC -shared"
322323
[ "$IS_WIN" = "false" ] && LINK="$LINK -ldl"
323324
[ "$IS_MAC" = "false" ] && LINK="$LINK -Wl,--no-undefined"
324325
[ "$IS_MAC" = "true" ] && COMPILE="$COMPILE -mmacosx-version-min=10.9"
@@ -340,7 +341,7 @@ else
340341

341342
$COMPILE -o bindings.o -c -O3 -I"$1"/lightning-c-bindings/include/ $2 src/main/jni/bindings.c
342343
$COMPILE $LINK -o liblightningjni_release$LDK_TARGET_SUFFIX.so -O3 $2 bindings.o $LDK_LIB -lm
343-
[ "$IS_APPLE_CLANG" != "true" ] && llvm-strip liblightningjni_release$LDK_TARGET_SUFFIX.so
344+
[ "$IS_APPLE_CLANG" != "true" ] && llvm-strip -R .llvmbc -R .llvmcmd liblightningjni_release$LDK_TARGET_SUFFIX.so
344345

345346
if [ "$IS_MAC" = "false" -a "$4" = "false" ]; then
346347
GLIBC_SYMBS="$(objdump -T liblightningjni_release$LDK_TARGET_SUFFIX.so | grep GLIBC_ | grep -v "GLIBC_2\.\(2\|3\)\(\.\|)\)" | grep -v "GLIBC_2.\(3\.4\|14\|17\|18\|25\|28\|29\|32\|33\|34\|\))" || echo)"

0 commit comments

Comments
 (0)