From b3a74e97f0979c68d21df1084e01dc49ffc1c4ed Mon Sep 17 00:00:00 2001 From: Eric Rozell Date: Wed, 1 May 2024 12:21:28 -0700 Subject: [PATCH] Add C++ header for default TurboModules and cross-platform reuse (#44361) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44361 In order to keep all platforms in sync (Android, iOS, Windows, etc.), it makes sense to consolidate all C++ TurboModules that we want available by default on all platforms to a shared C++ header / implementation. This moves the duplicated code from Android and iOS to such a shared module provider and updates relevant build specs. ## Changelog [Internal] Differential Revision: D56835783 --- .../Libraries/AppDelegate/RCTAppDelegate.mm | 18 +------ .../AppDelegate/React-RCTAppDelegate.podspec | 4 +- .../ReactAndroid/src/main/jni/CMakeLists.txt | 1 + .../jni/react/newarchdefaults/CMakeLists.txt | 1 + .../DefaultTurboModuleManagerDelegate.cpp | 18 +------ .../nativemodule/defaults/CMakeLists.txt | 26 ++++++++++ .../defaults/DefaultTurboModules.cpp | 33 ++++++++++++ .../defaults/DefaultTurboModules.h | 20 ++++++++ .../React-defaultsnativemodule.podspec | 50 +++++++++++++++++++ .../react-native/scripts/react_native_pods.rb | 1 + 10 files changed, 137 insertions(+), 35 deletions(-) create mode 100644 packages/react-native/ReactCommon/react/nativemodule/defaults/CMakeLists.txt create mode 100644 packages/react-native/ReactCommon/react/nativemodule/defaults/DefaultTurboModules.cpp create mode 100644 packages/react-native/ReactCommon/react/nativemodule/defaults/DefaultTurboModules.h create mode 100644 packages/react-native/ReactCommon/react/nativemodule/defaults/React-defaultsnativemodule.podspec diff --git a/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm b/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm index 81a4b5b20f28aa..323190488d1911 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm +++ b/packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm @@ -30,9 +30,7 @@ #else #import #endif -#import -#import -#import +#import @interface RCTAppDelegate () @end @@ -212,19 +210,7 @@ - (Class)getModuleClassFromName:(const char *)name - (std::shared_ptr)getTurboModule:(const std::string &)name jsInvoker:(std::shared_ptr)jsInvoker { - if (name == facebook::react::NativeReactNativeFeatureFlags::kModuleName) { - return std::make_shared(jsInvoker); - } - - if (name == facebook::react::NativeMicrotasks::kModuleName) { - return std::make_shared(jsInvoker); - } - - if (name == facebook::react::NativeDOM::kModuleName) { - return std::make_shared(jsInvoker); - } - - return nullptr; + return facebook::react::DefaultTurboModules::getTurboModule(name, jsInvoker); } - (std::shared_ptr)getTurboModule:(const std::string &)name diff --git a/packages/react-native/Libraries/AppDelegate/React-RCTAppDelegate.podspec b/packages/react-native/Libraries/AppDelegate/React-RCTAppDelegate.podspec index 4db8bfcdee76d9..9978c9e96f7b06 100644 --- a/packages/react-native/Libraries/AppDelegate/React-RCTAppDelegate.podspec +++ b/packages/react-native/Libraries/AppDelegate/React-RCTAppDelegate.podspec @@ -75,9 +75,7 @@ Pod::Spec.new do |s| s.dependency "React-CoreModules" s.dependency "React-nativeconfig" s.dependency "ReactCodegen" - s.dependency "React-domnativemodule" - s.dependency "React-featureflagsnativemodule" - s.dependency "React-microtasksnativemodule" + s.dependency "React-defaultsnativemodule" add_dependency(s, "ReactCommon", :subspec => "turbomodule/core", :additional_framework_paths => ["react/nativemodule/core"]) add_dependency(s, "React-NativeModulesApple") diff --git a/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt b/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt index 87347f9637aa11..cc19c00df2340c 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt +++ b/packages/react-native/ReactAndroid/src/main/jni/CMakeLists.txt @@ -106,6 +106,7 @@ add_react_common_subdir(react/utils) add_react_common_subdir(react/bridging) add_react_common_subdir(react/renderer/mapbuffer) add_react_common_subdir(react/nativemodule/core) +add_react_common_subdir(react/nativemodule/defaults) add_react_common_subdir(react/nativemodule/dom) add_react_common_subdir(react/nativemodule/featureflags) add_react_common_subdir(react/nativemodule/microtasks) diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/CMakeLists.txt b/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/CMakeLists.txt index d0fbbe8aba9d64..dbd585e0d3ff05 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/CMakeLists.txt +++ b/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/CMakeLists.txt @@ -21,6 +21,7 @@ target_link_libraries(react_newarchdefaults react_nativemodule_core react_codegen_rncore react_cxxreactpackage + react_nativemodule_defaults react_nativemodule_dom react_nativemodule_featureflags react_nativemodule_microtasks diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.cpp index 34a98d5483b884..e2f8771c3a7191 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/newarchdefaults/DefaultTurboModuleManagerDelegate.cpp @@ -9,9 +9,7 @@ #include -#include -#include -#include +#include namespace facebook::react { @@ -75,19 +73,7 @@ std::shared_ptr DefaultTurboModuleManagerDelegate::getTurboModule( } } - if (name == NativeReactNativeFeatureFlags::kModuleName) { - return std::make_shared(jsInvoker); - } - - if (name == NativeMicrotasks::kModuleName) { - return std::make_shared(jsInvoker); - } - - if (name == NativeDOM::kModuleName) { - return std::make_shared(jsInvoker); - } - - return nullptr; + return DefaultTurboModules::getTurboModule(name, jsInvoker); } std::shared_ptr DefaultTurboModuleManagerDelegate::getTurboModule( diff --git a/packages/react-native/ReactCommon/react/nativemodule/defaults/CMakeLists.txt b/packages/react-native/ReactCommon/react/nativemodule/defaults/CMakeLists.txt new file mode 100644 index 00000000000000..827bdbc45ad5f5 --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/defaults/CMakeLists.txt @@ -0,0 +1,26 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +cmake_minimum_required(VERSION 3.13) +set(CMAKE_VERBOSE_MAKEFILE on) + +add_compile_options( + -fexceptions + -frtti + -std=c++20 + -Wall + -Wpedantic + -DLOG_TAG=\"ReactNative\") + +file(GLOB react_nativemodule_defaults_SRC CONFIGURE_DEPENDS *.cpp) +add_library(react_nativemodule_defaults SHARED ${react_nativemodule_defaults_SRC}) + +target_include_directories(react_nativemodule_defaults PUBLIC ${REACT_COMMON_DIR}) + +target_link_libraries(react_nativemodule_defaults + react_nativemodule_dom + react_nativemodule_featureflags + react_nativemodule_microtasks +) diff --git a/packages/react-native/ReactCommon/react/nativemodule/defaults/DefaultTurboModules.cpp b/packages/react-native/ReactCommon/react/nativemodule/defaults/DefaultTurboModules.cpp new file mode 100644 index 00000000000000..1aeb61fe333ba4 --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/defaults/DefaultTurboModules.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "DefaultTurboModules.h" +#include +#include +#include + +namespace facebook::react { + +/* static */ std::shared_ptr DefaultTurboModules::getTurboModule( + const std::string& name, + const std::shared_ptr& jsInvoker) { + if (name == NativeReactNativeFeatureFlags::kModuleName) { + return std::make_shared(jsInvoker); + } + + if (name == NativeMicrotasks::kModuleName) { + return std::make_shared(jsInvoker); + } + + if (name == NativeDOM::kModuleName) { + return std::make_shared(jsInvoker); + } + + return nullptr; +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/nativemodule/defaults/DefaultTurboModules.h b/packages/react-native/ReactCommon/react/nativemodule/defaults/DefaultTurboModules.h new file mode 100644 index 00000000000000..00613fbcbf8778 --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/defaults/DefaultTurboModules.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include + +namespace facebook::react { + +struct DefaultTurboModules { + static std::shared_ptr getTurboModule( + const std::string& name, + const std::shared_ptr& jsInvoker); +}; + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/nativemodule/defaults/React-defaultsnativemodule.podspec b/packages/react-native/ReactCommon/react/nativemodule/defaults/React-defaultsnativemodule.podspec new file mode 100644 index 00000000000000..1d5f00f1c883c1 --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/defaults/React-defaultsnativemodule.podspec @@ -0,0 +1,50 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +require "json" + +package = JSON.parse(File.read(File.join(__dir__, "..", "..", "..", "..", "package.json"))) +version = package['version'] + +source = { :git => 'https://github.com/facebook/react-native.git' } +if version == '1000.0.0' + # This is an unpublished version, use the latest commit hash of the react-native repo, which we’re presumably in. + source[:commit] = `git rev-parse HEAD`.strip if system("git rev-parse --git-dir > /dev/null 2>&1") +else + source[:tag] = "v#{version}" +end + +header_search_paths = [] + +if ENV['USE_FRAMEWORKS'] + header_search_paths << "\"$(PODS_TARGET_SRCROOT)/../../..\"" # this is needed to allow the defaultsnativemodule to access its own files +end + +Pod::Spec.new do |s| + s.name = "React-defaultsnativemodule" + s.version = version + s.summary = "React Native Default native modules" + s.homepage = "https://reactnative.dev/" + s.license = package["license"] + s.author = "Meta Platforms, Inc. and its affiliates" + s.platforms = min_supported_versions + s.source = source + s.source_files = "*.{cpp,h}" + s.header_dir = "react/nativemodule/defaults" + s.pod_target_xcconfig = { "CLANG_CXX_LANGUAGE_STANDARD" => "c++20", + "HEADER_SEARCH_PATHS" => header_search_paths.join(' '), + "DEFINES_MODULE" => "YES" } + + if ENV['USE_FRAMEWORKS'] + s.module_name = "React_defaultsnativemodule" + s.header_mappings_dir = "../.." + end + + install_modules_dependencies(s) + + s.dependency "React-domnativemodule" + s.dependency "React-featureflagsnativemodule" + s.dependency "React-microtasksnativemodule" +end diff --git a/packages/react-native/scripts/react_native_pods.rb b/packages/react-native/scripts/react_native_pods.rb index 2068431e6d0088..20978c7ea8538c 100644 --- a/packages/react-native/scripts/react_native_pods.rb +++ b/packages/react-native/scripts/react_native_pods.rb @@ -123,6 +123,7 @@ def use_react_native! ( pod 'React-featureflagsnativemodule', :path => "#{prefix}/ReactCommon/react/nativemodule/featureflags" pod 'React-microtasksnativemodule', :path => "#{prefix}/ReactCommon/react/nativemodule/microtasks" pod 'React-domnativemodule', :path => "#{prefix}/ReactCommon/react/nativemodule/dom" + pod 'React-defaultsnativemodule', :path => "#{prefix}/ReactCommon/react/nativemodule/defaults" pod 'React-Mapbuffer', :path => "#{prefix}/ReactCommon" pod 'React-jserrorhandler', :path => "#{prefix}/ReactCommon/jserrorhandler" pod 'React-nativeconfig', :path => "#{prefix}/ReactCommon"