Skip to content

Commit

Permalink
chore: android support (#2554)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos authored May 22, 2024
1 parent 58aa5e6 commit 1e2aa57
Show file tree
Hide file tree
Showing 85 changed files with 158,195 additions and 65 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
/metrics/waku-sim-all-nodes-grafana-dashboard.json

*.log
package-lock.json
package.json
/package-lock.json
/package.json
node_modules/
/.update.timestamp

Expand Down
78 changes: 74 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ GIT_SUBMODULE_UPDATE := git submodule update --init --recursive

else # "variables.mk" was included. Business as usual until the end of this file.

ifeq ($(OS),Windows_NT) # is Windows_NT on XP, 2000, 7, Vista, 10...
detected_OS := Windows
else
detected_OS := $(strip $(shell uname))
endif

##########
## Main ##
Expand Down Expand Up @@ -131,7 +136,7 @@ clean: | clean-libbacktrace
##################
## RLN ##
##################
.PHONY: librln
.PHONY: librln shouldUseRLNV2

LIBRLN_BUILDDIR := $(CURDIR)/vendor/zerokit
ifeq ($(RLN_V2),true)
Expand All @@ -150,13 +155,13 @@ $(LIBRLN_FILE):
echo -e $(BUILD_MSG) "$@" && \
./scripts/build_rln.sh $(LIBRLN_BUILDDIR) $(LIBRLN_VERSION) $(LIBRLN_FILE)


librln: | $(LIBRLN_FILE)
$(eval NIM_PARAMS += --passL:$(LIBRLN_FILE) --passL:-lm)
shouldUseRLNV2:
ifeq ($(RLN_V2),true)
$(eval NIM_PARAMS += -d:rln_v2)
endif

librln: | $(LIBRLN_FILE) shouldUseRLNV2
$(eval NIM_PARAMS += --passL:$(LIBRLN_FILE) --passL:-lm)

clean-librln:
cargo clean --manifest-path vendor/zerokit/rln/Cargo.toml
Expand Down Expand Up @@ -294,6 +299,71 @@ else
$(ENV_SCRIPT) nim libwakuDynamic $(NIM_PARAMS) waku.nims
endif

#####################
## Mobile Bindings ##
#####################
.PHONY: libwaku-android \
libwaku-android-precheck \
libwaku-android-arm64 \
libwaku-android-amd64 \
libwaku-android-x86 \
libwaku-android-arm \
rebuild-nat-libs \
build-libwaku-for-android-arch

ANDROID_TARGET ?= 30
ifeq ($(detected_OS),Darwin)
ANDROID_TOOLCHAIN_DIR := $(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/darwin-x86_64
else
ANDROID_TOOLCHAIN_DIR := $(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/linux-x86_64
endif

rebuild-nat-libs: | clean-cross nat-libs

libwaku-android-precheck: shouldUseRLNV2
ifndef ANDROID_NDK_HOME
$(error ANDROID_NDK_HOME is not set)
endif

build-libwaku-for-android-arch:
$(MAKE) rebuild-nat-libs CC=$(ANDROID_TOOLCHAIN_DIR)/bin/$(ANDROID_COMPILER) && \
./scripts/build_rln_android.sh $(CURDIR)/build $(LIBRLN_BUILDDIR) $(LIBRLN_VERSION) $(CROSS_TARGET) $(ABIDIR) && \
CPU=$(CPU) ABIDIR=$(ABIDIR) ANDROID_ARCH=$(ANDROID_ARCH) ANDROID_COMPILER=$(ANDROID_COMPILER) ANDROID_TOOLCHAIN_DIR=$(ANDROID_TOOLCHAIN_DIR) $(ENV_SCRIPT) nim libWakuAndroid $(NIM_PARAMS) waku.nims

libwaku-android-arm64: ANDROID_ARCH=aarch64-linux-android
libwaku-android-arm64: CPU=arm64
libwaku-android-arm64: ABIDIR=arm64-v8a
libwaku-android-arm64: | libwaku-android-precheck build deps
$(MAKE) build-libwaku-for-android-arch ANDROID_ARCH=$(ANDROID_ARCH) CROSS_TARGET=$(ANDROID_ARCH) CPU=$(CPU) ABIDIR=$(ABIDIR) ANDROID_COMPILER=$(ANDROID_ARCH)$(ANDROID_TARGET)-clang

libwaku-android-amd64: ANDROID_ARCH=x86_64-linux-android
libwaku-android-amd64: CPU=amd64
libwaku-android-amd64: ABIDIR=x86_64
libwaku-android-amd64: | libwaku-android-precheck build deps
$(MAKE) build-libwaku-for-android-arch ANDROID_ARCH=$(ANDROID_ARCH) CROSS_TARGET=$(ANDROID_ARCH) CPU=$(CPU) ABIDIR=$(ABIDIR) ANDROID_COMPILER=$(ANDROID_ARCH)$(ANDROID_TARGET)-clang

libwaku-android-x86: ANDROID_ARCH=i686-linux-android
libwaku-android-x86: CPU=i386
libwaku-android-x86: ABIDIR=x86
libwaku-android-x86: | libwaku-android-precheck build deps
$(MAKE) build-libwaku-for-android-arch ANDROID_ARCH=$(ANDROID_ARCH) CROSS_TARGET=$(ANDROID_ARCH) CPU=$(CPU) ABIDIR=$(ABIDIR) ANDROID_COMPILER=$(ANDROID_ARCH)$(ANDROID_TARGET)-clang

libwaku-android-arm: ANDROID_ARCH=armv7a-linux-androideabi
libwaku-android-arm: CPU=arm
libwaku-android-arm: ABIDIR=armeabi-v7a
libwaku-android-arm: | libwaku-android-precheck build deps
# cross-rs target architecture name does not match the one used in android
$(MAKE) build-libwaku-for-android-arch ANDROID_ARCH=$(ANDROID_ARCH) CROSS_TARGET=armv7-linux-androideabi CPU=$(CPU) ABIDIR=$(ABIDIR) ANDROID_COMPILER=$(ANDROID_ARCH)$(ANDROID_TARGET)-clang

libwaku-android:
$(MAKE) libwaku-android-amd64
$(MAKE) libwaku-android-arm64
$(MAKE) libwaku-android-x86
# This target is disabled because on recent versions of cross-rs complain with the following error
# relocation R_ARM_THM_ALU_PREL_11_0 cannot be used against symbol 'stack_init_trampoline_return'; recompile with -fPIC
# It's likely this architecture is not used so we might just not support it.
# $(MAKE) libwaku-android-arm

cwaku_example: | build libwaku
echo -e $(BUILD_MSG) "build/$@" && \
cc -o "build/$@" \
Expand Down
19 changes: 16 additions & 3 deletions config.nims
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ elif defined(macosx) and defined(arm64):
switch("passC", "-mcpu=apple-m1")
switch("passL", "-mcpu=apple-m1")
else:
switch("passC", "-march=native")
switch("passL", "-march=native")
if not defined(android):
switch("passC", "-march=native")
switch("passL", "-march=native")
if defined(windows):
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65782
# ("-fno-asynchronous-unwind-tables" breaks Nim's exception raising, sometimes)
Expand All @@ -72,7 +73,7 @@ switch("define", "withoutPCRE")

# the default open files limit is too low on macOS (512), breaking the
# "--debugger:native" build. It can be increased with `ulimit -n 1024`.
if not defined(macosx):
if not defined(macosx) and not defined(android):
# add debugging symbols and original files and line numbers
--debugger:native
if not (defined(windows) and defined(i386)) and not defined(disable_libbacktrace):
Expand All @@ -92,3 +93,15 @@ switch("warning", "ObservableStores:off")
# Too many false positives for "Warning: method has lock level <unknown>, but another method has 0 [LockLevel]"
switch("warning", "LockLevel:off")

if defined(android):
var clang = getEnv("ANDROID_COMPILER")
var ndk_home = getEnv("ANDROID_TOOLCHAIN_DIR")
var sysroot = ndk_home & "/sysroot"
var cincludes = sysroot & "/usr/include/" & getEnv("ANDROID_ARCH")

switch("clang.path", ndk_home & "/bin")
switch("clang.exe", clang)
switch("clang.linkerexe", clang)
switch("passC", "--sysroot=" & sysRoot)
switch("passL", "--sysroot=" & sysRoot)
switch("cincludes", sysRoot & "/usr/include/")
1 change: 1 addition & 0 deletions examples/cbindings/waku_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ void handle_user_input() {
// End of UI program logic

int main(int argc, char** argv) {
waku_setup();

struct ConfigNode cfgNode;
// default values
Expand Down
1 change: 1 addition & 0 deletions examples/cpp/waku.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ auto cify(F&& f) {
}

int main(int argc, char** argv) {
waku_setup();

struct ConfigNode cfgNode;
// default values
Expand Down
6 changes: 6 additions & 0 deletions examples/golang/waku.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ type WakuNode struct {
ctx unsafe.Pointer
}

func WakuSetup() {
C.waku_setup()
}

func WakuNew(config WakuConfig) (*WakuNode, error) {
jsonConfig, err := json.Marshal(config)
if err != nil {
Expand Down Expand Up @@ -443,6 +447,8 @@ func (self *WakuNode) WakuListenAddresses() (string, error) {
}

func main() {
WakuSetup()

config := WakuConfig{
Host: "0.0.0.0",
Port: 30304,
Expand Down
2 changes: 2 additions & 0 deletions examples/mobile/.bundle/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BUNDLE_PATH: "vendor/bundle"
BUNDLE_FORCE_RUBY_PLATFORM: 1
4 changes: 4 additions & 0 deletions examples/mobile/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: '@react-native',
};
74 changes: 74 additions & 0 deletions examples/mobile/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# OSX
#
.DS_Store

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
**/.xcode.env.local

# Android/IntelliJ
#
build/
.idea
.gradle
local.properties
*.iml
*.hprof
.cxx/
*.keystore
!debug.keystore

# node.js
#
node_modules/
npm-debug.log
yarn-error.log

# fastlane
#
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
# screenshots whenever they are needed.
# For more information about the recommended setup visit:
# https://docs.fastlane.tools/best-practices/source-control/

**/fastlane/report.xml
**/fastlane/Preview.html
**/fastlane/screenshots
**/fastlane/test_output

# Bundle artifact
*.jsbundle

# Ruby / CocoaPods
**/Pods/
/vendor/bundle/

# Temporary files created by Metro to check the health of the file watcher
.metro-health-check*

# testing
/coverage

# Yarn
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
7 changes: 7 additions & 0 deletions examples/mobile/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
arrowParens: 'avoid',
bracketSameLine: true,
bracketSpacing: false,
singleQuote: true,
trailingComma: 'all',
};
1 change: 1 addition & 0 deletions examples/mobile/.watchmanconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Loading

0 comments on commit 1e2aa57

Please sign in to comment.