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

Create a pure C++ Turbo Module without wrappers #630

Closed
1 of 2 tasks
ospfranco opened this issue Sep 11, 2024 · 3 comments
Closed
1 of 2 tasks

Create a pure C++ Turbo Module without wrappers #630

ospfranco opened this issue Sep 11, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@ospfranco
Copy link

Description

I want to create a pure C++ module without ObjC/Kotlin wrappers. This library has a really simple way to load the Turbo Module.

However on the latest version it seems builder-bob has started to customize and patch the Turbo Module generation which is causing me a lot of troubles. I've tried replicating the setup of BcryptCpp but I get different errors.

First I had to remove the codegen step from pre-actions on the scheme. Related to #628. It seems the script messes with codegen as well. Then when I remove the outputDir from the codegenConfig in package.json I start getting duplicated symbols error as well as failing pod install:

~/Developer/react-native-opacity/example (main*) » yarn pods                   osp@ospm3
Found 1 module for target `OpacityExample`
link_native_modules! {:ios_packages=>[{:configurations=>[], :name=>"react-native-opacity", :path=>"../..", :podspec_path=>"/Users/osp/Developer/react-native-opacity/react-native-opacity.podspec", :script_phases=>[]}], :ios_project_root_path=>"/Users/osp/Developer/react-native-opacity/example/ios", :react_native_path=>"../node_modules/react-native"}
Auto-linking React Native module for target `OpacityExample`: react-native-opacity
Framework build type is static library
[Codegen] Adding script_phases to ReactCodegen.
[Codegen] Generating ./build/generated/ios/ReactCodegen.podspec.json
[Codegen] Analyzing /Users/osp/Developer/react-native-opacity/example/package.json
[Codegen] Searching for codegen-enabled libraries in the app.
[Codegen] The "codegenConfig" field is not defined in package.json. Assuming there is nothing to generate at the app level.
[Codegen] Searching for codegen-enabled libraries in the project dependencies.
[Codegen] Found react-native


[Codegen] >>>>> Searching for codegen-enabled libraries in react-native.config.js
[Codegen] Found react-native-opacity
[Codegen] Processing FBReactNativeSpec
[Codegen] Searching for podspec in the project dependencies.
[Codegen] Processing rncore
[Codegen] Searching for podspec in the project dependencies.
[Codegen] Processing RNOpacitySpec
[Codegen] Searching for podspec in the project dependencies.
[Codegen] Supported Apple platforms: ios for RNOpacitySpec
[Codegen] Generating Native Code for FBReactNativeSpec - ios
[Codegen] Generated artifacts: /Users/osp/Developer/react-native-opacity/example/ios/build/generated/ios
[Codegen - rncore] Skipping iOS code generation for rncore as it has been generated already.
[Codegen] Generating Native Code for RNOpacitySpec - ios
[Codegen] Generated artifacts: /Users/osp/Developer/react-native-opacity/example/ios/build/generated/ios
[Codegen] Creating component provider.
[Codegen] Generated provider in: /Users/osp/Developer/react-native-opacity/example/node_modules/react-native/React/Fabric
[Codegen] Done.
Analyzing dependencies
Downloading dependencies
ℹ Building target codegen
✔ Generated native code with codegen
/Users/osp/Developer/react-native-opacity/node_modules/react-native-builder-bob/lib/utils/patchCodegen.js:23
  let codegenAndroidPath = packageJson.codegenConfig.outputDir.android;
                                                               ^

TypeError: Cannot read properties of undefined (reading 'android')
    at patchCodegen (/Users/osp/Developer/react-native-opacity/node_modules/react-native-builder-bob/lib/utils/patchCodegen.js:23:64)
    at build (/Users/osp/Developer/react-native-opacity/node_modules/react-native-builder-bob/lib/targets/codegen.js:34:36)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async buildTarget (/Users/osp/Developer/react-native-opacity/node_modules/react-native-builder-bob/lib/index.js:421:7)

Node.js v18.20.2
Generating Pods project
Setting USE_HERMES build settings
Setting REACT_NATIVE_PATH build settings
Setting SWIFT_ACTIVE_COMPILATION_CONDITIONS build settings
[Ccache]: Removing Ccache from CC, LD, CXX & LDPLUSPLUS build settings
[SPM] Cleaning old SPM dependencies from Pods project
[SPM] Adding SPM dependencies to Pods project
[Privacy Manifest Aggregation] Appending aggregated reasons to existing PrivacyInfo.xcprivacy file.
[Privacy Manifest Aggregation] Reading .xcprivacy files to aggregate all used Required Reason APIs.
Setting CLANG_CXX_LANGUAGE_STANDARD to c++20 on /Users/osp/Developer/react-native-opacity/example/ios/OpacityExample.xcodeproj
Pod install took 8 [s] to run
Integrating client project
Pod installation complete! There are 65 dependencies from the Podfile and 65 total pods installed.

With the following package.json config:

  "codegenConfig": {
    "name": "RNOpacitySpec",
    "type": "modules",
    "jsSrcsDir": "src"
  },
  "create-react-native-library": {
    "type": "module-new",
    "languages": "cpp",
    "version": "0.41.1"
  }

Some times I can get it to work. After clearing the build and derived data folder. But other times I don't.

Not necessarily that I disagree with the direction builder-bob is trying to help package creation. But the scripts that modify/patch the codegen process should be robust enough

Packages

  • create-react-native-library
  • react-native-builder-bob

Selected options

C++ Turbo Module with Vanilla app

Link to repro

Will create one later

Environment

info Fetching system and libraries information...
System:
  OS: macOS 15.0
  CPU: (11) arm64 Apple M3 Pro
  Memory: 1.52 GB / 36.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.20.2
    path: ~/.local/share/mise/installs/node/18/bin/node
  Yarn:
    version: 3.6.1
    path: ~/.local/share/mise/installs/node/18/bin/yarn
  npm:
    version: 10.5.0
    path: ~/.local/share/mise/installs/node/18/bin/npm
  Watchman:
    version: 2024.06.24.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.15.2
    path: /Users/osp/.local/share/mise/installs/ruby/3/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.0
      - iOS 18.0
      - macOS 15.0
      - tvOS 18.0
      - visionOS 2.0
      - watchOS 11.0
  Android SDK:
    API Levels:
      - "33"
      - "34"
    Build Tools:
      - 30.0.3
      - 33.0.0
      - 33.0.1
      - 34.0.0
      - 35.0.0
    System Images:
      - android-33 | Google Play ARM 64 v8a
    Android NDK: 26.1.10909125
IDEs:
  Android Studio: 2024.1 AI-241.18034.62.2412.12266719
  Xcode:
    version: 16.0/16A242
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.11
    path: /usr/bin/javac
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.75.2
    wanted: 0.75.2
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: Not found
  newArchEnabled: Not found
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found
@ospfranco ospfranco added the bug Something isn't working label Sep 11, 2024
@atlj
Copy link
Collaborator

atlj commented Sep 13, 2024

Hey, @ospfranco let me try to address all the problems the best I can.

But the scripts that modify/patch the codegen process should be robust enough

I definitely agree with this and apologize that I didn't do enough testing with all cases. I've mostly tested the happy cases, and will be more careful about this in the future.

TypeError: Cannot read properties of undefined (reading 'android') at patchCodegen (/Users/osp/Developer/react-native-opacity/node_modules/react-native-builder-bob/lib/utils/patchCodegen.js:23:64)

This is coming from the patchCodegen script that we have that's patching a known issue in codegen itself. Looks like we should display better error messages and instruct you to either add that key or remove the codegen target from the target list.

First I had to remove the codegen step from pre-actions on the scheme

We are invoking codegen manually in that step since it's not done automatically for us anymore. The reason is, we are shifting towards shipping the codegen specs with the library. You can take a look at this guide from React Native new architecture working group if you're not familiar with the concept. It's sad to see this causes some issues but I'm not sure whether #628 is a common case since you've stated you don't have npx, node, or npm on your path.

I want to create a pure C++ module without ObjC/Kotlin wrappers.

Could you move this part into a discussion/feature request? I'm actually working on adding this exact template, you can find an early example here. I've been waiting for react-native-community/cli#2387 to get merged so we won't need additional files on Android. So I can tell that this is in the works. With that being said, this is not our biggest priority right now. We are focused on enhancing the CLI experience, reducing the number of steps, and increasing robustness as you stated.

@stephane-r
Copy link

Hi guys !

I am very interested in your discussion. I am also trying to create a cpp module wrappers or JNI. I've check your early example, but, it work at this time ?

@ospfranco
Copy link
Author

Thanks @atlj!

  • Regarding android codegen path, it seems it is working for me now. As you mentioned this is an upstream change, but I had some other issue it seems. Now everything is generated and working correctly.
  • The issue with npx is an problem with my setup (mise) and how Xcode detects global installations of tools. I wonder how nvm seems to be working seamlessly but it appears there is no way to make it work with my setup. You can forget about this, I removed the script anyways.
  • Yeah, I figured one needs a react-native.config.js to make a pure C++ turbo module work on Android. Looking forward to see a pure C++ template without wrappers.

Going to close this for now as none of the items are actionable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants