Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure FFI interface from C++ <-> Dart is using static libraries #34

Open
Super-Genius opened this issue Jul 20, 2023 · 6 comments
Open
Assignees

Comments

@Super-Genius
Copy link
Contributor

The Original code assumed we were using dynamic/shared libraries/DLLs.

We are not, so the FFI code needs to be updated to use static libraries generated in ThirdParty.

https://docs.flutter.dev/platform-integration/ios/c-interop#dynamic-vs-static-linking

@henriqueaklein
Copy link

@Super-Genius I did some searching on the repo and found that the "genius_api_ffi.dart" is not referenced anywhere. I've debugged the code and as far as I could test the debugger never landed on this source file:

class NativeLibrary {
  /// Holds the symbol lookup function.
  final ffi.Pointer<T> Function<T extends ffi.NativeType>(String symbolName)
      _lookup;

  /// The symbols are looked up in [dynamicLibrary].
  NativeLibrary(ffi.DynamicLibrary dynamicLibrary)
      : _lookup = dynamicLibrary.lookup;

Is this the code you were talking about?
Also, what are the main libraries I need to load? I've checked them on the Release folder of the thirdparty, but I couldn't quite track it down what lib (.so or .a) the symbols (TWDataCreateWithData and etc) from wallet-core are.

Do you have any pointers on that?

@henriqueaklein
Copy link

In order to test anything I'm trying to compile wallet-core. It caused an error due to rust nightly usage, since some things got "broken" on latest nightly builds. I temporarily changed the "install-rust-dependencies" script to a fixed nightly release:

  rustup toolchain install nightly-2023-05-11
  rustup default nightly-2023-05-11

@Super-Genius Right now I'm having some issues on CMakeLists, since it's requesting the boost directory. I infer that I'm supposed to point to the build/Android (or Linux or iOS)/Release/boost, correct?

Also, after some research I don't think wallet-core was ever linked to GeniusWallet. After I'm able to create the libraries from wallet-core I'm gonna try to embed them on a wrapper on the application (without an extra plugin). The current build.gradle compiles the "native.cpp" source and creates an "libnative.so", which then its linked by the dart:ffi abstraction layer. My end goal is to replace this "native.cpp" test for a "TrustWalletWrapper.cpp" which will have basically "extern "C" and static or singletons for the wallet-core static libraries I'll link once I have them compiled. What do you think?

@Super-Genius
Copy link
Contributor Author

Super-Genius commented Jul 27, 2023

Yes boost library linking should follow SuperGenius CMakeLists.txt default location for thirdparty and Find_Package() locating boost automatically. CMakeLists.Txt has a function that looks for a define for THIRDPARTY_DIR and if not defined sets to ../../thirdparty relative directory and that is used to define directories for header files and library builds with the target OS defines

The WalletCore and GeniusSDK were never built and linked in using plugins. So, yes. This is the right direction for you to proceed

@henriqueaklein
Copy link

I paused this task to compile the wallet-core on Android so I'd have something to test it. So far I've only changed CMakeLists and build.gradle to forward wallet-core library to flutter. I was able to load a function pointer (the wallet-core already exports C like symbols). I'll check if this approach is the best one, since it takes a lot of memory (had to increase emulator's heap to 2GB I think) and also I read that there's another way to link it statically with JNI

@vampiebar
Copy link

vampiebar commented Aug 21, 2023 via email

@henriqueaklein
Copy link

Trust wallet core is being loaded as a static library, along with its dependencies (which aren't "whole-archived"). The procedure to insert extra libraries is pretty straightforward from now on, although for it to be added on all OSs one needs to change a couple of different places (cmakelists, runner xcode projects)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants