Skip to content

Commit

Permalink
Fix build (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain authored May 10, 2023
1 parent 1dfcaf8 commit 72b5dd0
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 95 deletions.
55 changes: 4 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,17 @@
>DISCLAIMER: the code contained in this repo is experimental and still in WIP status.
# react-native-haskell-shelley
# cls-mobile-bridge

https://www.npmjs.com/package/@emurgo/react-native-haskell-shelley
https://www.npmjs.com/package/@emurgo/csl-mobile-bridge

## Getting started

`$ npm install @emurgo/react-native-haskell-shelley --save`
`$ npm install @emurgo/csl-mobile-bridge --save`

### Mostly automatic installation

`$ react-native link @emurgo/react-native-haskell-shelley`
`$ react-native link @emurgo/csl-mobile-bridge`

## Usage

See examples in [`App.js`](example/App.js).

## How to add new classes and functions

The process is basically as follows: we start by writting a rust wrapper of some struct method from our target rust library. Both iOS and Android require specific rust wrappers, so there are separate folders for each (`rust/ios` and `rust/android`). When this project is compiled by the host react native app, all the wrappers are transformed into a native library. In Android, java can directly interact with the rust binaries (the instructions for compiling our rust library are in `build.gradle`), while in iOS there is an additional step in which the rust library is transformed into C, with which can we easily interact with through Objective-C. This intermediate step is contained in `ios/build.sh`, where we basically use `cbindgen` to automatically generate C binaries as well as C headers (which are written in `rust/include/react_native_haskell_shelley.h`).
After writing the corresponding iOS and Android wrappers, we finally just write a simple JS library in `index.js` and define its types in `index.d.ts`.

### Android

For every new class:

- Add a new rust module named `<class_name.rs>` (snake_case) in `rust/src/android/`. Here is where we add rust wrappers of the corresponding rust struct methods from the library we want to bind. You can check other modules to see how this is done in `rust/src/android/`.
- Add a `use` declaration in `rust/src/android/mod.rs`


Now you are ready to add functions for your class/rust structure.

For every new function in the module:
- Add a rust wrapper of the form: `pub unsafe extern "C" fn Java_io_emurgo_rnhaskellshelley_Native_functionNameInCamelCase`
- Declare an equivalent java function to the target rust function in `android/src/main/java/io/emurgo/rnhaskellshelley/Native.java`. The function name must be in camelCase and
corresponds to the last part (in camelCase) of the rust wrapper signature mentioned above.

- Add the implementation of the java function that will be exposed to react native in `android/src/main/java/io/emurgo/rnhaskellshelley/HaskellShelleyModule.java`. Note that the types and signatures in `HaskellShelleyModule.java` are different from `Native.java`. In the former, we use java types while in the later we use rust types, ie., we match the signatures of the corresponding rust wrappers.

### iOS

For every new class:

- Add a new rust module named `<class_name.rs>` (snake_case) in `rust/src/ios/`.
- Add a `use` declaration in `rust/src/ios/mod.rs`

As you may have noticed, the two steps above are equivalent to those with Android. The difference is that the rust wrappers are written differently.

For every new function in the module:
- Add a rust wrapper of the form: `pub unsafe extern "C" fn function_name_in_snake_case`
- Write a iOS-native wrapper in Objective-C in `ios/HaskellShelley.m`. In contrast to Android (java), the iOS native wrappers can't directly interact with rust so we actually use a C library. This C library is automatically generated by `cargo` when the project is built.


### Additional steps in Rust

- Any new struct from `cardano_serialization_lib` that is required either as an input or output in our rust wrappers must implement the `RPtrRepresentable` trait. Add them in [`ptr_impl.rs`](rust/src/ptr_impl.rs).

### Javascript

For new classes and methods:

1. Add the javascript class signature in `index.d.ts`
2. Add the javascript class implementation in `index.js`
2 changes: 2 additions & 0 deletions ios/HaskellShelley.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@
"$(SRCROOT)/../../../React/**",
"$(SRCROOT)/../../react-native/React/**",
);
IPHONEOS_DEPLOYMENT_TARGET = 12.4;
LIBRARY_SEARCH_PATHS = "$(inherited)";
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = HaskellShelley;
Expand All @@ -326,6 +327,7 @@
"$(SRCROOT)/../../../React/**",
"$(SRCROOT)/../../react-native/React/**",
);
IPHONEOS_DEPLOYMENT_TARGET = 12.4;
LIBRARY_SEARCH_PATHS = "$(inherited)";
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = HaskellShelley;
Expand Down
15 changes: 7 additions & 8 deletions ios/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions ios/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ if [ "${HAS_CARGO_IN_PATH}" -ne "0" ]; then
source $HOME/.cargo/env
fi

if [[ -n "${DEVELOPER_SDK_DIR:-}" ]] && [[ "$MAC_OS_VERSION" != "11" ]]; then
# Assume we're in Xcode, which means we're probably cross-compiling.
# In this case, we need to add an extra library search path for build scripts and proc-macros,
# which run on the host instead of the target.
# (macOS Big Sur does not have linkable libraries in /usr/lib/.)
export LIBRARY_PATH="${DEVELOPER_SDK_DIR}/MacOSX.sdk/usr/lib:${LIBRARY_PATH:-}"
fi

if [ -z "${PODS_TARGET_SRCROOT}" ]; then
ROOT_DIR="${SRCROOT}/../rust"
else
Expand All @@ -33,10 +25,14 @@ cd "${ROOT_DIR}"
if [[ "$TARGET_DEVICE_PLATFORM_NAME" == "iphonesimulator" ]] && [[ "$MAC_CURRENT_ARCH" == "arm64" ]]; then
# If we're building for the arm simulator on an M1 Mac, we need to use the x86_64-apple-ios-sim target.
# Otherwise, lipo will compile for arm64 iphone that can't run on the simulator.
ACTUAL_SDK_PATH=$(xcrun --sdk iphonesimulator --show-sdk-path)
export LIBRARY_PATH="${ACTUAL_SDK_PATH}/usr/lib:${LIBRARY_PATH:-}"
cargo lipo --targets="aarch64-apple-ios-sim"
LIPO_BIN_TARGET_DIR="aarch64-apple-ios-sim"
else
cargo lipo --xcode-integ
ACTUAL_SDK_PATH=$(xcrun --sdk iphoneos --show-sdk-path)
export LIBRARY_PATH="${ACTUAL_SDK_PATH}/usr/lib:${LIBRARY_PATH:-}"
cargo lipo --xcode-intem
fi

mkdir -p "${CONFIGURATION_BUILD_DIR}"
Expand Down
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@emurgo/react-native-haskell-shelley",
"title": "React Native Haskell Shelley",
"version": "5.0.0",
"name": "@emurgo/csl-mobile-bridge",
"title": "CSL mobile bridge",
"version": "5.1.0",
"description": "React-native bindings for Emurgo's cardano-serialization-lib (Cardano haskell Shelley)",
"main": "index.js",
"files": [
Expand All @@ -26,8 +26,8 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/Emurgo/react-native-haskell-shelley.git",
"baseUrl": "https://github.com/Emurgo/react-native-haskell-shelley"
"url": "git+https://github.com/Emurgo/csl-mobile-bridge.git",
"baseUrl": "https://github.com/Emurgo/csl-mobile-bridge"
},
"keywords": [
"react-native"
Expand All @@ -39,16 +39,16 @@
"licenseFilename": "LICENSE",
"readmeFilename": "README.md",
"peerDependencies": {
"react": "^17.0.2",
"react-native": ">=0.67.5 <1.0.x"
"react": ">=17.0.2",
"react-native": ">=0.67.5 <0.72.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.14.1",
"@typescript-eslint/parser": "^4.14.1",
"eslint": "7.8.1",
"flowgen": "1.13.0",
"react": "^17.0.2",
"react-native": "^0.67.5",
"flowgen": "^1.21.0",
"react": "^18.0.0",
"react-native": "^0.71.6",
"typescript": "^4.1.3"
},
"dependencies": {
Expand Down
9 changes: 4 additions & 5 deletions react-native-haskell-shelley.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ Pod::Spec.new do |s|
s.version = package["version"]
s.summary = package["description"]
s.description = <<-DESC
react-native-haskell-shelley
Cardano serialization lib bridge
DESC
s.homepage = "https://github.com/Emurgo/react-native-haskell-shelley"
# brief license entry:
s.homepage = "https://github.com/Emurgo/csl-mobile-bridge"
s.license = "MIT"
# optional - use expanded license entry instead:
# s.license = { :type => "MIT", :file => "LICENSE" }
s.authors = { "emurgo" => "[email protected]" }
s.platforms = { :ios => "11.0" }
s.source = { :git => "https://github.com/Emurgo/react-native-haskell-shelley.git", :tag => "#{s.version}" }
s.platforms = { :ios => "12.4" }
s.source = { :git => "https://github.com/Emurgo/csl-mobile-bridge.git", :tag => "#{s.version}" }

s.source_files = "ios/**/*.{h,c,m,swift,sh}"
s.requires_arc = true
Expand Down
9 changes: 0 additions & 9 deletions react-native.config.js

This file was deleted.

6 changes: 3 additions & 3 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "react-native-haskell-shelley"
version = "5.0.0"
authors = ["EMURGO R&D"]
name = "csl-mobile-bridge"
version = "5.1.0"
authors = ["EMURGO"]
edition = "2018"

[build-dependencies]
Expand Down

0 comments on commit 72b5dd0

Please sign in to comment.