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
/build

View File

@ -49,7 +49,7 @@ TOOLS_DIRS := premix tests
# comma-separated values for the "clean" target
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 '.'
all: $(TOOLS) nimbus
@ -128,7 +128,7 @@ test-reproducibility:
# usual cleaning
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
+ $(MAKE) -C vendor/nim-nat-traversal/vendor/miniupnp/miniupnpc 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 && \
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"
ntags:
ntags -R .

View File

@ -17,3 +17,7 @@
--excessiveStackTrace:on
-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,60 +752,61 @@ LOGGING AND DEBUGGING OPTIONS:
$ord(defaultNetwork)
]
proc processArguments*(msg: var string): ConfigStatus =
## Process command line argument and update `NimbusConfiguration`.
let config = getConfiguration()
when declared(os.paramCount): # not available with `--app:lib`
proc processArguments*(msg: var string): ConfigStatus =
## Process command line argument and update `NimbusConfiguration`.
let config = getConfiguration()
# At this point `config.net.bootnodes` is likely populated with network default
# bootnodes. We want to override those if at least one custom bootnode is
# specified on the command line. We temporarily set `config.net.bootNodes`
# to empty seq, and in the end restore it if no bootnodes were spricified on
# the command line.
# TODO: This is pretty hacky and it's better to refactor it to make a clear
# distinction between default and custom bootnodes.
var tempBootNodes: seq[ENode]
swap(tempBootNodes, config.net.bootNodes)
# The same trick is done to discPort
config.net.discPort = 0
var opt = initOptParser()
var length = 0
for kind, key, value in opt.getopt():
result = Error
case kind
of cmdArgument:
discard
of cmdLongOption, cmdShortOption:
inc(length)
case key.toLowerAscii()
of "help", "h":
msg = getHelpString()
result = Success
break
of "version", "ver", "v":
msg = NimbusVersion
result = Success
break
else:
processArgument processEthArguments, key, value, msg
processArgument processRpcArguments, key, value, msg
processArgument processNetArguments, key, value, msg
processArgument processShhArguments, key, value, msg
processArgument processDebugArguments, key, value, msg
if result != Success:
msg = "Unknown option: '" & key & "'."
break
of cmdEnd:
doAssert(false) # we're never getting this kind here
if config.net.bootNodes.len == 0:
# No custom bootnodes were specified on the command line, restore to
# previous values
# At this point `config.net.bootnodes` is likely populated with network default
# bootnodes. We want to override those if at least one custom bootnode is
# specified on the command line. We temporarily set `config.net.bootNodes`
# to empty seq, and in the end restore it if no bootnodes were spricified on
# the command line.
# TODO: This is pretty hacky and it's better to refactor it to make a clear
# distinction between default and custom bootnodes.
var tempBootNodes: seq[ENode]
swap(tempBootNodes, config.net.bootNodes)
if config.net.discPort == 0:
config.net.discPort = config.net.bindPort
# The same trick is done to discPort
config.net.discPort = 0
var opt = initOptParser()
var length = 0
for kind, key, value in opt.getopt():
result = Error
case kind
of cmdArgument:
discard
of cmdLongOption, cmdShortOption:
inc(length)
case key.toLowerAscii()
of "help", "h":
msg = getHelpString()
result = Success
break
of "version", "ver", "v":
msg = NimbusVersion
result = Success
break
else:
processArgument processEthArguments, key, value, msg
processArgument processRpcArguments, key, value, msg
processArgument processNetArguments, key, value, msg
processArgument processShhArguments, key, value, msg
processArgument processDebugArguments, key, value, msg
if result != Success:
msg = "Unknown option: '" & key & "'."
break
of cmdEnd:
doAssert(false) # we're never getting this kind here
if config.net.bootNodes.len == 0:
# No custom bootnodes were specified on the command line, restore to
# previous values
swap(tempBootNodes, config.net.bootNodes)
if config.net.discPort == 0:
config.net.discPort = config.net.bindPort
proc processConfiguration*(pathname: string): ConfigStatus =
## Process configuration file `pathname` and update `NimbusConfiguration`.

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 <time.h>
#include "status_api.h"
#include "wrapper.h"
void NimMain();

View File

@ -6,8 +6,8 @@ import (
"time"
)
// #cgo LDFLAGS: /home/oskarth/git/nimbus/nimbus/libnimbus_api.so -lm
// #include "libnim.h"
// #cgo LDFLAGS: -Wl,-rpath,'$ORIGIN' -L${SRCDIR}/../build -lnimbus -lm
// #include "wrapper.h"
import "C"
// 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],
eth/[keys, rlp, p2p], eth/p2p/rlpx_protocols/[whisper_protocol],
eth/p2p/[discovery, enode, peer_pool], chronicles,
../config
../nimbus/config
type
CReceivedMessage* = object