refactor the C and Go wrapper build system

- moved "nimbus/api" to "wrappers"
- renamed files
- replaced the build scripts with Makefile targets
- set the rpath relative to the test binary's location so it can look
  for libnimbus.so there at runtime
- libnimbus.so.0 required on Linux, apparently
- compiled all the Nimbus code with `--app:lib`, not just one file (this
  required skipping a proc in "nimbus/config.nim" because it uses an API
  that's unavailable in libraries)
- removed static linking from the Go wrapper. It doesn't make sense at a
  global level, when using a shared Nimbus library. To selectively link
  static libraries, we should probably be specifying them as *.a. I did
  build a static libnimbus.a, as a test, but it insisted on dlopen-ing a
  shared version of itself which looked too ugly to continue.
This commit is contained in:
Ștefan Talpalaru 2019-07-31 13:54:48 +02:00
parent ea06407e19
commit 1d7e14dc1b
No known key found for this signature in database
GPG Key ID: CBF7934204F1B6F9
14 changed files with 96 additions and 85 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
nimcache /nimcache
# Executables shall be put in an ignored build/ directory # Executables shall be put in an ignored build/ directory
/build /build

View File

@ -49,7 +49,7 @@ TOOLS_DIRS := premix tests
# comma-separated values for the "clean" target # comma-separated values for the "clean" target
TOOLS_CSV := $(subst $(SPACE),$(COMMA),$(TOOLS)) TOOLS_CSV := $(subst $(SPACE),$(COMMA),$(TOOLS))
.PHONY: all $(TOOLS) deps sanity-checks github-ssh build-nim update status ntags ctags nimbus testsuite test clean mrproper fetch-dlls test-libp2p-daemon nat-libs libminiupnpc.a libnatpmp.a go-checks .PHONY: all $(TOOLS) deps sanity-checks github-ssh build-nim update status ntags ctags nimbus testsuite test clean mrproper fetch-dlls test-libp2p-daemon nat-libs libminiupnpc.a libnatpmp.a go-checks libnimbus.so wrappers
# default target, because it's the first one that doesn't start with '.' # default target, because it's the first one that doesn't start with '.'
all: $(TOOLS) nimbus all: $(TOOLS) nimbus
@ -128,7 +128,7 @@ test-reproducibility:
# usual cleaning # usual cleaning
clean: clean:
rm -rf build/{nimbus,$(TOOLS_CSV),all_tests,test_rpc,*.exe} vendor/go/bin \ rm -rf build/{nimbus,$(TOOLS_CSV),all_tests,test_rpc,*.exe,*.so,*.so.0,*_wrapper_test} vendor/go/bin \
$(NIMBLE_DIR) $(NIM_BINARY) $(NIM_DIR)/nimcache nimcache $(NIMBLE_DIR) $(NIM_BINARY) $(NIM_DIR)/nimcache nimcache
+ $(MAKE) -C vendor/nim-nat-traversal/vendor/miniupnp/miniupnpc clean $(HANDLE_OUTPUT) + $(MAKE) -C vendor/nim-nat-traversal/vendor/miniupnp/miniupnpc clean $(HANDLE_OUTPUT)
+ $(MAKE) -C vendor/nim-nat-traversal/vendor/libnatpmp clean $(HANDLE_OUTPUT) + $(MAKE) -C vendor/nim-nat-traversal/vendor/libnatpmp clean $(HANDLE_OUTPUT)
@ -197,6 +197,18 @@ test-libp2p-daemon: | vendor/go/bin/p2pd deps
$(ENV_SCRIPT) nim c -r $(NIM_PARAMS) tests/testdaemon.nim && \ $(ENV_SCRIPT) nim c -r $(NIM_PARAMS) tests/testdaemon.nim && \
rm -f tests/testdaemon rm -f tests/testdaemon
libnimbus.so: | build deps nat-libs
echo -e $(BUILD_MSG) "build/$@" && \
$(ENV_SCRIPT) nim c --app:lib --noMain -d:"chronicles_sinks=textlines" --debuginfo --opt:speed --lineTrace:off $(NIM_PARAMS) -o:build/$@.0 wrappers/wrapper.nim && \
rm -f build/$@ && \
ln -s $@.0 build/$@
wrappers: | build deps nat-libs libnimbus.so go-checks
echo -e $(BUILD_MSG) "build/C_wrapper_test" && \
$(CC) wrappers/wrapper.c -Wl,-rpath,'$$ORIGIN' -Lbuild -lnimbus -lm -g -o build/C_wrapper_test
echo -e $(BUILD_MSG) "build/go_wrapper_test" && \
go build -o build/go_wrapper_test wrappers/wrapper.go
# https://bitbucket.org/nimcontrib/ntags/ - currently fails with "out of memory" # https://bitbucket.org/nimcontrib/ntags/ - currently fails with "out of memory"
ntags: ntags:
ntags -R . ntags -R .

View File

@ -17,3 +17,7 @@
--excessiveStackTrace:on --excessiveStackTrace:on
-d:metrics # enable metric collection -d:metrics # enable metric collection
# Required to make up for the compiler's inability to invalidate the C file
# (and object) cache on different command line arguments.
--forceBuild

View File

@ -1,2 +0,0 @@
status_api_test
libnimbus_api.so

View File

@ -1,7 +0,0 @@
This folder contains an experimental C API for using parts of the nimbus
code from C/go in the status console client:
https://github.com/status-im/status-console-client/
It serves mainly as a proof-of-concept for now - there are several unresolved
issues surrounding threading, inter-language communication, callbacks etc.

View File

@ -1,2 +0,0 @@
#!/bin/sh
go build --ldflags '-extldflags "-static"' -o test main.go

View File

@ -1,12 +0,0 @@
#!/bin/sh
echo "[Cleaning up caches...]"
rm -rf nimcache/
rm -f libnimbus_api.so
# ../env.sh nim c --opt:speed --lineTrace:off --verbosity:2 status_api
# gcc status_api.c ./libnimbus_api.so -lm -o xx
# debug info
../../env.sh nim c --debuginfo --opt:speed --lineTrace:off --verbosity:2 status_api
gcc status_api.c ./libnimbus_api.so -lm -g -o status_api_test

View File

@ -1,4 +0,0 @@
-app:lib
o:"libnimbus_api.so"
noMain
-d:"chronicles_sinks=textlines"

View File

@ -752,6 +752,7 @@ LOGGING AND DEBUGGING OPTIONS:
$ord(defaultNetwork) $ord(defaultNetwork)
] ]
when declared(os.paramCount): # not available with `--app:lib`
proc processArguments*(msg: var string): ConfigStatus = proc processArguments*(msg: var string): ConfigStatus =
## Process command line argument and update `NimbusConfiguration`. ## Process command line argument and update `NimbusConfiguration`.
let config = getConfiguration() let config = getConfiguration()

21
wrappers/README.md Normal file
View File

@ -0,0 +1,21 @@
This folder contains an experimental C wrapper for using parts of the Nimbus
code from C/Go in the Status console client:
https://github.com/status-im/status-console-client/
It serves mainly as a proof-of-concept for now - there are several unresolved
issues surrounding threading, inter-language communication, callbacks etc.
To build the wrappers and the test programs, run from the top level directory:
```bash
make wrappers
```
Now you can run the test programs:
```bash
build/C_wrapper_test
build/go_wrapper_test
```

View File

@ -4,7 +4,7 @@
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include "status_api.h" #include "wrapper.h"
void NimMain(); void NimMain();

View File

@ -6,8 +6,8 @@ import (
"time" "time"
) )
// #cgo LDFLAGS: /home/oskarth/git/nimbus/nimbus/libnimbus_api.so -lm // #cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus -lm
// #include "libnim.h" // #include "wrapper.h"
import "C" import "C"
// Arrange that main.main runs on main thread. // Arrange that main.main runs on main thread.

View File

@ -12,7 +12,7 @@ import
nimcrypto/[bcmode, hmac, rijndael, pbkdf2, sha2, sysrand, utils, keccak, hash], nimcrypto/[bcmode, hmac, rijndael, pbkdf2, sha2, sysrand, utils, keccak, hash],
eth/[keys, rlp, p2p], eth/p2p/rlpx_protocols/[whisper_protocol], eth/[keys, rlp, p2p], eth/p2p/rlpx_protocols/[whisper_protocol],
eth/p2p/[discovery, enode, peer_pool], chronicles, eth/p2p/[discovery, enode, peer_pool], chronicles,
../config ../nimbus/config
type type
CReceivedMessage* = object CReceivedMessage* = object