Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Add Node cross-compilation support
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisameling committed May 6, 2021
1 parent d3cb5db commit 02c3866
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 20 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ jobs:
uses: svenstaro/upload-release-action@v1-release
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ffi/node/libzkgroup.so
asset_name: libzkgroup.so
file: ffi/node/libzkgroup-x64.so
asset_name: libzkgroup-x64.so
tag: ${{ github.ref }}
overwrite: true

Expand Down Expand Up @@ -96,8 +96,8 @@ jobs:
uses: svenstaro/upload-release-action@v1-release
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ffi/node/libzkgroup.dll
asset_name: libzkgroup.dll
file: ffi/node/libzkgroup-x64.dll
asset_name: libzkgroup-x64.dll
tag: ${{ github.ref }}
overwrite: true

Expand All @@ -112,7 +112,7 @@ jobs:

- name: Install pre-requisites
run: |
rustup target add aarch64-apple-ios x86_64-apple-ios armv7-apple-ios armv7s-apple-ios
rustup target add aarch64-apple-ios x86_64-apple-ios
cd ffi/swift
make install_build_dependencies
Expand Down Expand Up @@ -141,8 +141,8 @@ jobs:
uses: svenstaro/upload-release-action@v1-release
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: ffi/node/libzkgroup.dylib
asset_name: libzkgroup.dylib
file: ffi/node/libzkgroup-x64.dylib
asset_name: libzkgroup-x64.dylib
tag: ${{ github.ref }}
overwrite: true

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}

- name: Install iOS Rust targets
run: rustup target add aarch64-apple-ios x86_64-apple-ios armv7-apple-ios armv7s-apple-ios
run: rustup target add aarch64-apple-ios x86_64-apple-ios
- name: Build zkgroup library
working-directory: ffi/swift
run: |
Expand Down
17 changes: 12 additions & 5 deletions ffi/node/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
ZKGROUP_RUST_DIR=../../rust
ZKGROUP_TARGET_DIR=../../target

NODE_ARCH := $(shell node -p "process.arch" || x64)
CARGO_BUILDFLAGS=

ifdef CARGO_TARGET
CARGO_BUILDFLAGS=--target $(CARGO_TARGET)
endif

ifeq ($(OS),Windows_NT)
DETECTED_OS := Windows
else
Expand All @@ -9,17 +16,17 @@ endif

ifeq ($(DETECTED_OS),Windows)
SOURCE_LIB = zkgroup.dll
TARGET_LIB = libzkgroup.dll
TARGET_LIB = libzkgroup-$(NODE_ARCH).dll
RUSTFLAGS = '-C link-arg=-s -C target-feature=+crt-static'
endif
ifeq ($(DETECTED_OS),Linux)
SOURCE_LIB = libzkgroup.so
TARGET_LIB = libzkgroup.so
TARGET_LIB = libzkgroup-$(NODE_ARCH).so
RUSTFLAGS='-C link-arg=-s'
endif
ifeq ($(DETECTED_OS),Darwin)
SOURCE_LIB = libzkgroup.dylib
TARGET_LIB = libzkgroup.dylib
TARGET_LIB = libzkgroup-$(NODE_ARCH).dylib
RUSTFLAGS='-C link-arg=-s'
endif

Expand All @@ -30,9 +37,9 @@ clean:
rm -r node_modules

libzkgroup: FORCE
RUSTFLAGS=$(RUSTFLAGS) cargo build --manifest-path=$(ZKGROUP_RUST_DIR)/Cargo.toml --release
RUSTFLAGS=$(RUSTFLAGS) cargo build $(CARGO_BUILDFLAGS) --manifest-path=$(ZKGROUP_RUST_DIR)/Cargo.toml --release
rm -f $(TARGET_LIB)
mv $(ZKGROUP_TARGET_DIR)/release/$(SOURCE_LIB) $(TARGET_LIB)
mv $(ZKGROUP_TARGET_DIR)/$(CARGO_TARGET)/release/$(SOURCE_LIB) $(TARGET_LIB)

test: FORCE
npm install
Expand Down
6 changes: 3 additions & 3 deletions ffi/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"dist/zkgroup",
"dist/index.d.ts",
"zkgroup/modules",
"libzkgroup.dylib",
"libzkgroup.dll",
"libzkgroup.so"
"libzkgroup-*.dylib",
"libzkgroup-*.dll",
"libzkgroup-*.so"
],
"scripts": {
"build": "tsc",
Expand Down
3 changes: 2 additions & 1 deletion ffi/node/zkgroup/internal/Native.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join, resolve } from 'path';
import { Library } from 'ffi-napi';
import FFICompatArray, { FFICompatArrayType } from './FFICompatArray';
import * as process from 'process';

type IntType = number;
type UInt32Type = number;
Expand All @@ -15,7 +16,7 @@ export const RANDOM_LENGTH = 32;
const rootPath = resolve(`${__dirname}/../../../`);

// We need to do things differently if we are in an app.asar, common in the Electron world
const libraryPath = join(rootPath.replace('app.asar', 'app.asar.unpacked'), 'libzkgroup');
const libraryPath = join(rootPath.replace('app.asar', 'app.asar.unpacked'), 'libzkgroup-' + process.arch);


interface NativeCalls {
Expand Down
2 changes: 1 addition & 1 deletion ffi/swift/BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
cargo install cbindgen
cargo install cargo-lipo
rustup target add aarch64-apple-ios x86_64-apple-ios armv7-apple-ios armv7s-apple-ios
rustup target add aarch64-apple-ios x86_64-apple-ios
```

`rustup show` should indicate that the `x86_64-apple-darwin` is the host and stable, as well as having all the targets above listed.
Expand Down
2 changes: 1 addition & 1 deletion ffi/swift/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ zkgroup.h: FORCE
cbindgen --lang c $(ZKGROUP_RUST_DIR) -o ZKGroup/libzkgroup/zkgroup.h

libzkgroup_ios.a: FORCE
cargo lipo --manifest-path=$(ZKGROUP_RUST_DIR)/Cargo.toml --release --targets=aarch64-apple-ios,x86_64-apple-ios,armv7-apple-ios,armv7s-apple-ios
cargo lipo --manifest-path=$(ZKGROUP_RUST_DIR)/Cargo.toml --release --targets=aarch64-apple-ios,x86_64-apple-ios
mv $(ZKGROUP_TARGET_DIR)/universal/release/libzkgroup.a ZKGroup/libzkgroup/libzkgroup_ios.a

install_build_dependencies:
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.41.1
1.52.0

0 comments on commit 02c3866

Please sign in to comment.