Skip to content

Commit baa58c1

Browse files
authored
Merge pull request #688 from TheBlueMatt/2020-09-dev-docs
Add developer guideline notes for C/C++ bindings generation
2 parents 253af8d + 3fa0548 commit baa58c1

File tree

7 files changed

+40
-20
lines changed

7 files changed

+40
-20
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@ jobs:
1717
include:
1818
- toolchain: stable
1919
build-net-tokio: true
20-
build-bindings: true
2120
- toolchain: beta
2221
build-net-tokio: true
23-
build-bindings: true
2422
- toolchain: 1.39.0
2523
build-net-tokio: true
26-
build-bindings: true
2724
coverage: true
2825
- toolchain: 1.34.2
29-
build-bindings: true
3026
runs-on: ubuntu-latest
3127
steps:
3228
- name: Checkout source code
@@ -43,9 +39,6 @@ jobs:
4339
- name: Build on Rust ${{ matrix.toolchain }}
4440
if: "! matrix.build-net-tokio"
4541
run: RUSTFLAGS="-C link-dead-code" cargo build --verbose --color always -p lightning
46-
- name: Build bindings on Rust ${{ matrix.toolchain }}
47-
if: matrix.build-bindings
48-
run: RUSTFLAGS="-C link-dead-code" cargo build --verbose --color always -p lightning-c-bindings
4942
- name: Test on Rust ${{ matrix.toolchain }} with net-tokio
5043
if: matrix.build-net-tokio
5144
run: RUSTFLAGS="-C link-dead-code" cargo test --verbose --color always
@@ -127,6 +120,9 @@ jobs:
127120
apt-get -y install cargo valgrind lld git g++ clang
128121
- name: Checkout source code
129122
uses: actions/checkout@v2
123+
- name: Sanity test bindings
124+
working-directory: lightning-c-bindings
125+
run: cargo check
130126
- name: Install cbindgen
131127
run: cargo install --force cbindgen
132128
- name: Rebuild bindings, and check the sample app builds + links

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/target/
2+
c-bindings-gen/target/
3+
lightning-c-bindings/target/
4+
lightning-c-bindings/a.out
25
/hfuzz_target/
36
/net-tokio/target/
47
**/*.rs.bk

CONTRIBUTING.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ Fuzzing is heavily encouraged: you will find all related material under `fuzz/`
9999

100100
Mutation testing is work-in-progress; any contribution there would be warmly welcomed.
101101

102+
C/C++ Bindings
103+
--------------
104+
105+
You can learn more about the C/C++ bindings that are made available by reading the
106+
[C/C++ Bindings README](lightning-c-bindings/README.md). If you are not using the C/C++ bindings,
107+
you likely don't need to worry about them, and during their early experimental phase we are not
108+
requiring that pull requests keep the bindings up to date (and, thus, pass the bindings_check CI
109+
run). If you wish to ensure your PR passes the bindings generation phase, you should run the
110+
`genbindings.sh` script in the top of the directory tree to generate, build, and test C bindings on
111+
your local system.
112+
102113
Going further
103114
-------------
104115

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
members = [
44
"lightning",
55
"lightning-net-tokio",
6-
"lightning-c-bindings",
76
]
87

98
# Our tests do actual crypo and lots of work, the tradeoff for -O1 is well worth it

genbindings.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ fi
4848
# Finally, sanity-check the generated C and C++ bindings with demo apps:
4949

5050
# Naively run the C demo app:
51-
gcc -Wall -g -pthread demo.c ../target/debug/liblightning.a -ldl
51+
gcc -Wall -g -pthread demo.c target/debug/libldk.a -ldl
5252
./a.out
5353

5454
# And run the C++ demo app in valgrind to test memory model correctness and lack of leaks.
55-
g++ -std=c++11 -Wall -g -pthread demo.cpp -L../target/debug/ -llightning -ldl
55+
g++ -std=c++11 -Wall -g -pthread demo.cpp -Ltarget/debug/ -lldk -ldl
5656
if [ -x "`which valgrind`" ]; then
57-
LD_LIBRARY_PATH=../target/debug/ valgrind --error-exitcode=4 --memcheck:leak-check=full --show-leak-kinds=all ./a.out
57+
LD_LIBRARY_PATH=target/debug/ valgrind --error-exitcode=4 --memcheck:leak-check=full --show-leak-kinds=all ./a.out
5858
echo
5959
else
6060
echo "WARNING: Please install valgrind for more testing"
6161
fi
6262

6363
# Test a statically-linked C++ version, tracking the resulting binary size and runtime
6464
# across debug, LTO, and cross-language LTO builds (using the same compiler each time).
65-
clang++ -std=c++11 -Wall -pthread demo.cpp ../target/debug/liblightning.a -ldl
65+
clang++ -std=c++11 -Wall -pthread demo.cpp target/debug/libldk.a -ldl
6666
./a.out >/dev/null
6767
echo " C++ Bin size and runtime w/o optimization:"
6868
ls -lha a.out
@@ -75,19 +75,19 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" ]; then
7575
if [ -x "$(which clang-$LLVM_V)" ]; then
7676
cargo +nightly clean
7777
cargo +nightly rustc -Zbuild-std --target x86_64-unknown-linux-gnu -v -- -Zsanitizer=memory -Zsanitizer-memory-track-origins -Cforce-frame-pointers=yes
78-
mv ../target/x86_64-unknown-linux-gnu/debug/liblightning.* ../target/debug/
78+
mv target/x86_64-unknown-linux-gnu/debug/libldk.* target/debug/
7979

8080
# Sadly, std doesn't seem to compile into something that is memsan-safe as of Aug 2020,
8181
# so we'll always fail, not to mention we may be linking against git rustc LLVM which
8282
# may differ from clang-llvm, so just allow everything here to fail.
8383
set +e
8484

8585
# First the C demo app...
86-
clang-$LLVM_V -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -Wall -g -pthread demo.c ../target/debug/liblightning.a -ldl
86+
clang-$LLVM_V -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -Wall -g -pthread demo.c target/debug/libldk.a -ldl
8787
./a.out
8888

8989
# ...then the C++ demo app
90-
clang++-$LLVM_V -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -Wall -g -pthread demo.cpp ../target/debug/liblightning.a -ldl
90+
clang++-$LLVM_V -std=c++11 -fsanitize=memory -fsanitize-memory-track-origins -Wall -g -pthread demo.cpp target/debug/libldk.a -ldl
9191
./a.out >/dev/null
9292

9393
# restore exit-on-failure
@@ -153,11 +153,11 @@ if [ "$HOST_PLATFORM" = "host: x86_64-unknown-linux-gnu" -o "$HOST_PLATFORM" = "
153153
mv Cargo.toml.bk Cargo.toml
154154

155155
# First the C demo app...
156-
$CLANG -fsanitize=address -Wall -g -pthread demo.c ../target/debug/liblightning.a -ldl
156+
$CLANG -fsanitize=address -Wall -g -pthread demo.c target/debug/libldk.a -ldl
157157
ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out
158158

159159
# ...then the C++ demo app
160-
$CLANGPP -std=c++11 -fsanitize=address -Wall -g -pthread demo.cpp ../target/debug/liblightning.a -ldl
160+
$CLANGPP -std=c++11 -fsanitize=address -Wall -g -pthread demo.cpp target/debug/libldk.a -ldl
161161
ASAN_OPTIONS='detect_leaks=1 detect_invalid_pointer_pairs=1 detect_stack_use_after_return=1' ./a.out >/dev/null
162162
else
163163
echo "WARNING: Please install clang-$RUSTC_LLVM_V and clang++-$RUSTC_LLVM_V to build with address sanitizer"
@@ -168,7 +168,7 @@ fi
168168

169169
# Now build with LTO on on both C++ and rust, but without cross-language LTO:
170170
cargo rustc -v --release -- -C lto
171-
clang++ -std=c++11 -Wall -flto -O2 -pthread demo.cpp ../target/release/liblightning.a -ldl
171+
clang++ -std=c++11 -Wall -flto -O2 -pthread demo.cpp target/release/libldk.a -ldl
172172
echo "C++ Bin size and runtime with only RL (LTO) optimized:"
173173
ls -lha a.out
174174
time ./a.out > /dev/null
@@ -180,7 +180,7 @@ if [ "$HOST_PLATFORM" != "host: x86_64-apple-darwin" -a "$CLANGPP" != "" ]; then
180180
# packaging than simply shipping the rustup binaries (eg Debian should Just Work
181181
# here).
182182
cargo rustc -v --release -- -C linker-plugin-lto -C lto -C link-arg=-fuse-ld=lld
183-
$CLANGPP -Wall -std=c++11 -flto -fuse-ld=lld -O2 -pthread demo.cpp ../target/release/liblightning.a -ldl
183+
$CLANGPP -Wall -std=c++11 -flto -fuse-ld=lld -O2 -pthread demo.cpp target/release/libldk.a -ldl
184184
echo "C++ Bin size and runtime with cross-language LTO:"
185185
ls -lha a.out
186186
time ./a.out > /dev/null

lightning-c-bindings/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ Utilities to fetch the chain from Bitcoin Core REST/RPC Interfaces and feed them
99
"""
1010

1111
[lib]
12-
name = "lightning"
12+
name = "ldk"
1313
crate-type = ["staticlib"
1414
# Note that the following line is matched exactly by genbindings to turn off dylib creation
1515
,"cdylib"]
1616

1717
[dependencies]
1818
bitcoin = "0.24"
1919
lightning = { version = "0.0.11", path = "../lightning" }
20+
21+
# We eventually want to join the root workspace, but for now, the bindings generation is
22+
# a bit brittle and we don't want to hold up other developers from making changes just
23+
# because they break the bindings
24+
[workspace]

lightning-c-bindings/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ These include:
222222
**It is highly recommended that you test any code which relies on the C (or C++) bindings in
223223
valgrind, AddressSanitizer, MemorySanitizer, or other similar tools to ensure correctness.**
224224
225+
Process
226+
=======
227+
228+
`genbindings.sh` is currently a catch-all script for bindings - it generates the latest Rust/C/C++
229+
code for bindings from the rust-lightning source code, builds it, and then runs various test apps.
230+
225231
Note that after running `genbindings.sh`, if possible, the static lib in target/debug (eg
226232
target/debug/liblightning.a) will be linked with address sanitizer. In order to build against it,
227233
you will need to link with `clang` with `-fsanitize=address` with the same version of LLVM as

0 commit comments

Comments
 (0)