From 26ccf519879536122ab4a91df10314aeee44413a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Thor=C3=A9n?= Date: Sun, 13 Jun 2021 20:50:10 +0800 Subject: [PATCH] Improve libwaku and C wrappers (#613) - Move stuff to Makefile - Move libwaku to root - Make examples compile for C and Go - Update README --- .gitignore | 2 -- Makefile | 18 +++++++++- waku/v2/node/libwaku/nim.cfg | 5 --- {waku/v2/node/libwaku => wrappers}/README.md | 5 +-- {waku/v2/node/libwaku => wrappers}/libwaku.h | 5 +++ .../v2/node/libwaku => wrappers}/libwaku.nim | 4 ++- .../libwaku.c => wrappers/wrapper_example.c | 2 ++ wrappers/wrapper_example.go | 35 +++++++++++++++++++ 8 files changed, 63 insertions(+), 13 deletions(-) delete mode 100644 waku/v2/node/libwaku/nim.cfg rename {waku/v2/node/libwaku => wrappers}/README.md (56%) rename {waku/v2/node/libwaku => wrappers}/libwaku.h (64%) rename {waku/v2/node/libwaku => wrappers}/libwaku.nim (91%) rename waku/v2/node/libwaku/libwaku.c => wrappers/wrapper_example.c (92%) create mode 100644 wrappers/wrapper_example.go diff --git a/.gitignore b/.gitignore index ed070c9c8..451814e44 100644 --- a/.gitignore +++ b/.gitignore @@ -34,5 +34,3 @@ rln package-lock.json node_modules/ /.update.timestamp - -libwaku diff --git a/Makefile b/Makefile index 596d77f0a..9edb9980b 100644 --- a/Makefile +++ b/Makefile @@ -29,6 +29,8 @@ DOCKER_IMAGE_NIM_PARAMS ?= -d:chronicles_colors:none -d:insecure bridge \ test \ clean \ + libwaku.so \ + wrappers \ libbacktrace ifeq ($(NIM_PARAMS),) @@ -128,7 +130,7 @@ rlnlib: ifeq ($(RLN), true) cargo build --manifest-path vendor/rln/Cargo.toml endif - + test2: | build deps installganache echo -e $(BUILD_MSG) "build/$@" && \ $(ENV_SCRIPT) nim test2 $(NIM_PARAMS) waku.nims @@ -185,3 +187,17 @@ ifneq ($(USE_LIBBACKTRACE), 0) endif endif # "variables.mk" was not included + +libwaku.so: | build deps + echo -e $(BUILD_MSG) "build/$@" && \ + $(ENV_SCRIPT) nim c --app:lib --noMain --nimcache:nimcache/libwaku $(NIM_PARAMS) -o:build/$@.0 wrappers/libwaku.nim && \ + rm -f build/$@ && \ + ln -s $@.0 build/$@ + +# libraries for dynamic linking of non-Nim objects +EXTRA_LIBS_DYNAMIC := -L"$(CURDIR)/build" -lwaku -lm +wrappers: | build deps libwaku.so + echo -e $(BUILD_MSG) "build/C_wrapper_example" && \ + $(CC) wrappers/wrapper_example.c -Wl,-rpath,'$$ORIGIN' $(EXTRA_LIBS_DYNAMIC) -g -o build/C_wrapper_example + echo -e $(BUILD_MSG) "build/go_wrapper_example" && \ + go build -ldflags "-linkmode external -extldflags '$(EXTRA_LIBS_DYNAMIC)'" -o build/go_wrapper_example wrappers/wrapper_example.go #wrappers/cfuncs.go diff --git a/waku/v2/node/libwaku/nim.cfg b/waku/v2/node/libwaku/nim.cfg deleted file mode 100644 index 29a2312bc..000000000 --- a/waku/v2/node/libwaku/nim.cfg +++ /dev/null @@ -1,5 +0,0 @@ --app:staticlib -o:"libwaku.a" -noMain -header --d:"chronicles_sinks=textlines" diff --git a/waku/v2/node/libwaku/README.md b/wrappers/README.md similarity index 56% rename from waku/v2/node/libwaku/README.md rename to wrappers/README.md index 47a4ca50d..4ecd2ff3f 100644 --- a/waku/v2/node/libwaku/README.md +++ b/wrappers/README.md @@ -4,9 +4,6 @@ Exposes a C API that can be used by other environments other than C. ## Running -Make sure you run `$PROJECTDIR/env.sh bash` first. Then: - ``` -./build.sh -./libwaku +make wrappers ``` diff --git a/waku/v2/node/libwaku/libwaku.h b/wrappers/libwaku.h similarity index 64% rename from waku/v2/node/libwaku/libwaku.h rename to wrappers/libwaku.h index f8c722481..dce2deaa3 100644 --- a/waku/v2/node/libwaku/libwaku.h +++ b/wrappers/libwaku.h @@ -1,3 +1,6 @@ +#ifndef __LIBWAKU_H__ +#define __LIBWAKU_H__ + #include #include @@ -7,3 +10,5 @@ void NimMain(); char* info(const char* wakuNode); void echo(); + +#endif //__LIBWAKU_H__ diff --git a/waku/v2/node/libwaku/libwaku.nim b/wrappers/libwaku.nim similarity index 91% rename from waku/v2/node/libwaku/libwaku.nim rename to wrappers/libwaku.nim index fc2d967f2..936233e1d 100644 --- a/waku/v2/node/libwaku/libwaku.nim +++ b/wrappers/libwaku.nim @@ -10,7 +10,7 @@ # TODO Init a node # proc info*(node: WakuNode): WakuInfo = -proc info(foo: cstring): cstring {.exportc.} = +proc info(foo: cstring): cstring {.exportc, dynlib.} = echo "info about node" echo foo return foo @@ -34,3 +34,5 @@ proc echo() {.exportc.} = # await node.start() # #main() + + # When main done stuff diff --git a/waku/v2/node/libwaku/libwaku.c b/wrappers/wrapper_example.c similarity index 92% rename from waku/v2/node/libwaku/libwaku.c rename to wrappers/wrapper_example.c index cc126e183..33bec5db3 100644 --- a/waku/v2/node/libwaku/libwaku.c +++ b/wrappers/wrapper_example.c @@ -3,6 +3,8 @@ #include "libwaku.h" +void NimMain(); + int main(int argc, char* argv[]) { char* string; NimMain(); diff --git a/wrappers/wrapper_example.go b/wrappers/wrapper_example.go new file mode 100644 index 000000000..4291e2dd0 --- /dev/null +++ b/wrappers/wrapper_example.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "runtime" +) + +/* +#include + +// Passing "-lwaku" to the Go linker through "-extldflags" is not enough. We need it in here, for some reason. +#cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lwaku +#include "libwaku.h" + +*/ +import "C" + +// Arrange that main.main runs on main thread. +func init() { + runtime.LockOSThread() +} + +func Start() { + C.NimMain() + + messageC := C.CString("Calling info") + fmt.Println("Start nim-waku") + var str = C.info(messageC) + fmt.Println("Info", str) +} + +func main() { + fmt.Println("Hi main") + Start() +}