Turbo Module CMAKE helper
Thekepler_add_turbo_module_library function is a CMake helper that simplifies the process of defining a Turbo Module library target for use with the React Native for Vega runtime. It handles common configuration and optimization settings required for Turbo Module libraries, streamlining the process by automatically applying the necessary settings and optimizations.
This helper function provides the following benefits:
- Simplified Configuration: Reduces boilerplate CMake code with a single function call
- Consistent Best Practices: Makes sure that all Turbo Modules follow the same configuration patterns and provides a central place to roll out common settings in the future
- Improved Application Binary Interface (ABI) Stability: Applies proper symbol visibility settings automatically
- Optimized Performance: Makes sure that performance optimizations such as link-time and size optimizations are applied
Key symbol contracts for Turbo Module DSO
All Turbo Modules on Vega platforms must adhere to two critical requirements:- Export EntryPoint function: The Turbo Module library must export a single symbol named
autoLinkVegaTurboModulesV1. This symbol serves as the entry point that the Vega runtime uses to automatically link and load your Turbo Module. - Hidden Symbol Visibility: All other symbols in the Turbo Module library should be hidden. This prevents symbol conflicts with other libraries, ensures a stable ABI, improves TurboModule loading time, and can reduce the size of the library in many cases.
kepler_add_turbo_module_library helper automatically configures these requirements for you, eliminating the need for manual symbol visibility management.
Functionality provided
Thekepler_add_turbo_module_library function provides several key features that simplify the creation of Turbo Module libraries. These features are organized into three main categories.
Shared library creation
The helper function provides support for shared libraries as follows:- Creates a shared library with the specified name using the standard CMake
add_librarycommand. - Links the
turbomoduleAPIstatic library, which provides the C++ methods to develop Turbo Modules, and the necessary platform-level functionality, to the library. - Handles the proper setup of the library target, similar to what you would do manually with
add_libraryandtarget_link_libraries.
Symbol visibility control
The helper function automatically configures symbol visibility to ensure ABI stability:- Sets default visibility to “hidden” for all symbols
- Applies a version script that explicitly exports only the
autoLinkVegaTurboModulesV1symbol - Disables symbol interposition with
-fno-semantic-interpositionand applies local binding of global references with-Bsymbolicto prevent symbol conflicts
Performance optimizations
The helper function applies several optimizations to improve the performance and size of your Turbo Module:- Enables link-time optimization (LTO) for better performance and smaller binary size
- Applies size optimizations by applying garbage collection of unused sections
Usage
To use thekepler_add_turbo_module_library function, add the following to your CMakeLists.txt.
Frequently Asked Questions
Should I migrate my existing TurboModule to use this helper function?
Should I migrate my existing TurboModule to use this helper function?
How do I migrate my existing TurboModule to use this helper function?
How do I migrate my existing TurboModule to use this helper function?
- Review your current CMakeLists.txt to understand how your TurboModule is configured
- Make sure that you have the
find_package(turbomoduleAPI CONFIG REQUIRED)line in your CMakeLists.txt - Replace your
add_librarycall and all subsequent configuration with a singlekepler_add_turbo_module_librarycall - Delete the version-script (.map file) previously used for managing the
autoLinkVegaTurboModulesV1export - Test your build to ensure everything works correctly
Before (manual configuration)
After (using the helper)
How do I handle version scripts with this helper function? Can I export additional symbols beyond autoLinkVegaTurboModulesV1?
How do I handle version scripts with this helper function? Can I export additional symbols beyond autoLinkVegaTurboModulesV1?
autoLinkVegaTurboModulesV1 symbol. If you’re already using a version script, remove your existing version script applications from your CMake configuration to avoid conflicts. If you need to export additional symbols beyond autoLinkVegaTurboModulesV1, you can apply another version script after calling the helper function, but ensure there’s no overlap between the symbols exported by each script. This approach should be used sparingly, as exporting additional symbols may compromise ABI stability.How do I debug issues with the kepler_add_turbo_module_library function?
How do I debug issues with the kepler_add_turbo_module_library function?
EXPORT_COMPILE_COMMANDS ON by default, which creates this database. You can open the database with IDEs to see the exact flags applied.How can I add additional target behavior?
How can I add additional target behavior?
Symbol inspection
To get the llvm-nm tool path from the Vega SDK, use the following:Threading
All Turbo Module methods are invoked on the JSThread. To maintain good performance, it is crucial to avoid blocking the JSThread, by using a separate non-JSThread wherever it is beneficial, such as forPromise or Callback usages.
You can use a thread utility library or even std::thread directly to create and manage a separate non-JSThread for your Turbo Module, depending on your specific requirements.
Event handling
Turbo Modules can send events from C++ to JavaScript, but the@amzn/keplerscript-turbomodule-api package currently does not include support for handling these events. To listen for events, you can use NativeEventEmitter from react-native.
Turbo Modules can signal events to JavaScript by using the built-in emit method, which accepts most Turbo Module C++ types as a payload. When on JSThread (i.e., no new thread was created for emitting events), you should use emitSync instead.
NativeEventEmitter. The following example builds on the instructions found in Implement JavaScript Layer.
Error handling
Many of the exceptions which might be encountered when running a Turbo Module are thrown into the JavaScript layer. If you have a JavaScript implementation which is wrapped over the native module, you can use atry/catch to address these errors there.

