-
Notifications
You must be signed in to change notification settings - Fork 5
feat(android): Add CMake integration for polygen-generated code #137
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
Open
0xBigBoss
wants to merge
10
commits into
callstackincubator:master
Choose a base branch
from
0xBigBoss:fix/android-codegen-132
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat(android): Add CMake integration for polygen-generated code #137
0xBigBoss
wants to merge
10
commits into
callstackincubator:master
from
0xBigBoss:fix/android-codegen-132
+1,973
−2,088
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fixes the Android new architecture build failure caused by incorrect codegen configuration that generated NativePolygenSpec in com.callstack.polygen while Kotlin code expected NativeWasmSpec in com.wasm package. Changes: - Align build.gradle codegen config with package.json: - libraryName="RNPolygenSpec" (matches codegenConfig.name) - codegenJavaPackageName="com.callstack.polygen" - Update Android namespace from com.wasm to com.callstack.polygen - Rename Wasm* Kotlin files to Polygen* with correct package - Add PolygenModule implementing all NativePolygenSpec abstract methods (with stub implementations that throw UnsupportedOperationException) - Add @ReactMethod annotations to oldarch PolygenSpec for old architecture compatibility - Remove outdated C++ native build config (cpp-adapter.cpp, CMakeLists) - Update react-native.config.js with correct package import path - Add PLAN.md documenting steps for full C++ implementation including: - CMake setup with OnLoad.cpp - SoLoader.loadLibrary for JNI_OnLoad trigger - C++ TurboModule registration Note: The Android module now compiles successfully but the actual WASM runtime implementation is not yet wired up. Methods will throw UnsupportedOperationException at runtime until the C++ implementation is properly integrated via JNI. Fixes callstackincubator#132
- Rewrite CMakeLists.txt with proper React Native prefab integration - Enable externalNativeBuild and prefab in build.gradle - Add OnLoad.cpp for JNI_OnLoad registration - Add ModuleBagStub.cpp for runtime-only usage - Update PolygenPackage.kt with SoLoader and isCxxModule=true - Add cmakeListsPath to react-native.config.js - Fix missing #include <string> in w2c.h - Use GLOB_RECURSE to include utils/checksum.cpp - Link ReactAndroid::folly_runtime for fmt support
- Move react{} DSL to subprojects afterEvaluate (plugin not at root)
- Add packagingOptions to exclude React Native .so files from AAR
- Re-enable newArchEnabled for TurboModule support
- getModule now returns null when new architecture is enabled, allowing the system to use the C++ module registered via JNI_OnLoad - CMake conditionally includes codegen files only when they exist - Fixes issue where Java TurboModule stub would be used instead of C++
Wrap externalNativeBuild, CMake path, prefab, and packagingOptions inside isNewArchitectureEnabled() checks. This prevents old-arch builds from failing due to missing codegen-generated headers that only exist when the React Gradle plugin runs with new architecture enabled.
- Only call SoLoader.loadLibrary("polygen") when new arch is enabled,
since old arch builds don't produce libpolygen.so
- Make isCxxModule conditional on IS_NEW_ARCHITECTURE_ENABLED to avoid
mismatched metadata in legacy bridge registration
Verified Android C++ TurboModule works at runtime: - App launches successfully via ADB - UI visible and interactive - Polygen TurboModule loads without crashes - No UnsatisfiedLinkError or registration errors - Import Validation test works correctly
The C++ TurboModule infrastructure is working correctly: - JNI_OnLoad registers the module - libpolygen.so loads without UnsatisfiedLinkError - API is accessible from JavaScript Module loading returns "not precompiled" errors because: - iOS uses ReactNativeWebAssemblyHost.podspec to auto-link generated code - Android has no equivalent mechanism yet - ModuleBagStub.cpp returns empty bag as fallback This is expected behavior - implementing Android generated-code integration is a separate feature beyond the C++ TurboModule wiring scope.
Implements the Android equivalent of iOS's ReactNativeWebAssemblyHost.podspec mechanism for linking polygen-generated native code. Changes: - Add android-cmake.ts plugin to polygen-codegen that generates CMakeLists.txt in .polygen-out/host/ during code generation - Modify polygen library's CMakeLists.txt to detect and include generated code when available - Pass POLYGEN_APP_ROOT from Gradle to CMake to support monorepo setups - Conditionally exclude ModuleBagStub.cpp when generated loader.cpp provides getModuleBag() The generated code is now being compiled and linked, but there's a pre-existing wasm-rt API mismatch that needs to be fixed separately: wasm2c generates code calling wasm_rt_allocate_memory with 5 args, but bundled wasm-rt expects 4 args.
- Fix include paths in generated CMakeLists.txt to find Module.h in WebAssembly subdirectory and link against ReactAndroid::jsi for JSI headers - Fix duplicate symbol errors by making moduleInfo/moduleSharedInfo variable names unique per module (e.g., ExampleModuleInfo, ExampleModuleSharedInfo) - Update wasm-rt runtime to 1.0.39 API which uses 5-argument wasm_rt_allocate_memory (adds page_size parameter) - Update Memory.h constructor to match new wasm_rt_allocate_memory signature - Support wasm2c versions 1.0.36 and 1.0.39 These changes enable successful Android builds with polygen-generated code for both new architecture and old architecture configurations.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements Android CMake integration for polygen-generated native code, equivalent to iOS's
ReactNativeWebAssemblyHost.podspecauto-linking mechanism.Key Changes:
android-cmake.tsplugin - GeneratesCMakeLists.txtin.polygen-out/host/for Android buildspolygen/android/CMakeLists.txt- Detects and includes generated code from.polygen-out/host/ExampleModuleInfoinstead of genericmoduleInfo)wasm_rt_allocate_memoryAPI (5 arguments withpageSizeparameter)Memory.h- Constructor now matches new wasm-rt API signatureFeatures:
POLYGEN_APP_ROOTTest plan
simple-sha256-wasm) loads successfully on AndroidVerified output:
Closes #132