Skip to content

Commit 5abda9c

Browse files
cipolleschimeta-codesync[bot]
authored andcommitted
Add env var to compile-out legacy arch (#53975)
Summary: In this change, we are allowing users to install cocoapods with the RCT_REMOVE_LEGACY_ARCH flag enabled to compile out the legacy architecture. For it to work, we had to adjust some part of the framework that we forgot about when working on the internal side of this feature. bypass-github-export-checks ## Changelog: [iOS][Added] - Added way to set the RCT_REMOVE_LEGACY_ARCH flag from Cocoapods to compile ou the legacy arch Pull Request resolved: #53975 Test Plan: Tested locally with RNTester and HelloWorld: ``` cd packages/rn-tester RCT_REMOVE_LEGACY_ARCH=1 bundle exec pod install open RNTesterPods.xcworkspace ``` Then build and run and verify that the app keeps running. Same check with Hello World. Reviewed By: cortinico Differential Revision: D83471112 Pulled By: cipolleschi fbshipit-source-id: e5c8fe4eccb3c25510228f20e9e6da3980b0bf0d
1 parent 8cc9bbc commit 5abda9c

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

packages/react-native/Libraries/AppDelegate/RCTAppSetupUtils.mm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ void RCTAppSetupPrepareApp(UIApplication *application, BOOL turboModuleEnabled)
120120
RCTTurboModuleManager *turboModuleManager,
121121
const std::shared_ptr<facebook::react::RuntimeScheduler> &runtimeScheduler)
122122
{
123+
#ifndef RCT_REMOVE_LEGACY_ARCH
123124
// Necessary to allow NativeModules to lookup TurboModules
124125
[bridge setRCTTurboModuleRegistry:turboModuleManager];
125126

@@ -146,12 +147,18 @@ void RCTAppSetupPrepareApp(UIApplication *application, BOOL turboModuleEnabled)
146147
return std::make_unique<facebook::react::HermesExecutorFactory>(
147148
facebook::react::RCTJSIExecutorRuntimeInstaller(runtimeInstallerLambda));
148149
#endif
150+
#else
151+
// This method should not be invoked in the New Arch. So when Legacy Arch is removed, we can
152+
// safly return a nullptr.
153+
return nullptr;
154+
#endif
149155
}
150156

151157
std::unique_ptr<facebook::react::JSExecutorFactory> RCTAppSetupJsExecutorFactoryForOldArch(
152158
RCTBridge *bridge,
153159
const std::shared_ptr<facebook::react::RuntimeScheduler> &runtimeScheduler)
154160
{
161+
#ifndef RCT_REMOVE_LEGACY_ARCH
155162
auto runtimeInstallerLambda = [bridge, runtimeScheduler](facebook::jsi::Runtime &runtime) {
156163
if (!bridge) {
157164
return;
@@ -164,4 +171,9 @@ void RCTAppSetupPrepareApp(UIApplication *application, BOOL turboModuleEnabled)
164171
return std::make_unique<facebook::react::HermesExecutorFactory>(
165172
facebook::react::RCTJSIExecutorRuntimeInstaller(runtimeInstallerLambda));
166173
#endif
174+
#else
175+
// This method should not be invoked in the New Arch. So when Legacy Arch is removed, we can
176+
// safly return a nullptr.
177+
return nullptr;
178+
#endif
167179
}

packages/react-native/Libraries/AppDelegate/RCTReactNativeFactory.mm

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,12 @@ - (RCTRootViewFactory *)createRCTRootViewFactory
261261

262262
configuration.sourceURLForBridge = ^NSURL *_Nullable(RCTBridge *_Nonnull bridge)
263263
{
264+
#ifndef RCT_REMOVE_LEGACY_ARCH
264265
return [weakSelf.delegate sourceURLForBridge:bridge];
266+
#else
267+
// When the Legacy Arch is removed, the Delegate does not have a sourceURLForBridge method
268+
return [weakSelf.delegate bundleURL];
269+
#endif
265270
};
266271

267272
if ([self.delegate respondsToSelector:@selector(extraModulesForBridge:)]) {
@@ -275,13 +280,24 @@ - (RCTRootViewFactory *)createRCTRootViewFactory
275280
configuration.extraLazyModuleClassesForBridge =
276281
^NSDictionary<NSString *, Class> *_Nonnull(RCTBridge *_Nonnull bridge)
277282
{
283+
#ifndef RCT_REMOVE_LEGACY_ARCH
278284
return [weakSelf.delegate extraLazyModuleClassesForBridge:bridge];
285+
#else
286+
// When the Legacy Arch is removed, the Delegate does not have a extraLazyModuleClassesForBridge method
287+
return @{};
288+
#endif
279289
};
280290
}
281291

282292
if ([self.delegate respondsToSelector:@selector(bridge:didNotFindModule:)]) {
283293
configuration.bridgeDidNotFindModule = ^BOOL(RCTBridge *_Nonnull bridge, NSString *_Nonnull moduleName) {
294+
#ifndef RCT_REMOVE_LEGACY_ARCH
284295
return [weakSelf.delegate bridge:bridge didNotFindModule:moduleName];
296+
#else
297+
// When the Legacy Arch is removed, the Delegate does not have a bridge:didNotFindModule method
298+
// We return NO, because if we have invoked this method is unlikely that the module will be actually registered
299+
return NO;
300+
#endif
285301
};
286302
}
287303

@@ -290,13 +306,30 @@ - (RCTRootViewFactory *)createRCTRootViewFactory
290306
^(RCTBridge *_Nonnull bridge,
291307
RCTSourceLoadProgressBlock _Nonnull onProgress,
292308
RCTSourceLoadBlock _Nonnull loadCallback) {
309+
#ifndef RCT_REMOVE_LEGACY_ARCH
293310
[weakSelf.delegate loadSourceForBridge:bridge onProgress:onProgress onComplete:loadCallback];
311+
#else
312+
// When the Legacy Arch is removed, the Delegate does not have a
313+
// loadSourceForBridge:onProgress:onComplete: method
314+
// We then call the loadBundleAtURL:onProgress:onComplete: instead
315+
[weakSelf.delegate loadBundleAtURL:self.bundleURL onProgress:onProgress onComplete:loadCallback];
316+
#endif
294317
};
295318
}
296319

297320
if ([self.delegate respondsToSelector:@selector(loadSourceForBridge:withBlock:)]) {
298321
configuration.loadSourceForBridge = ^(RCTBridge *_Nonnull bridge, RCTSourceLoadBlock _Nonnull loadCallback) {
322+
#ifndef RCT_REMOVE_LEGACY_ARCH
299323
[weakSelf.delegate loadSourceForBridge:bridge withBlock:loadCallback];
324+
#else
325+
// When the Legacy Arch is removed, the Delegate does not have a
326+
// loadSourceForBridge:withBlock: method
327+
// We then call the loadBundleAtURL:onProgress:onComplete: instead
328+
[weakSelf.delegate loadBundleAtURL:self.bundleURL
329+
onProgress:^(RCTLoadingProgress *progressData) {
330+
}
331+
onComplete:loadCallback];
332+
#endif
300333
};
301334
}
302335

packages/react-native/scripts/cocoapods/utils.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ def self.remove_flag_for_key(config, flag, key)
521521

522522
if current_setting.include?(flag)
523523
current_setting.slice! flag
524+
current_setting.strip!
524525
end
525526

526527
config.build_settings[key] = current_setting

packages/react-native/scripts/react_native_pods.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,12 @@ def react_native_post_install(
518518
ReactNativePodsUtils.set_build_setting(installer, build_setting: "REACT_NATIVE_PATH", value: File.join("${PODS_ROOT}", "..", react_native_path))
519519
ReactNativePodsUtils.set_build_setting(installer, build_setting: "SWIFT_ACTIVE_COMPILATION_CONDITIONS", value: ['$(inherited)', 'DEBUG'], config_name: "Debug")
520520

521+
if (ENV['RCT_REMOVE_LEGACY_ARCH'] == '1')
522+
ReactNativePodsUtils.add_compiler_flag_to_project(installer, "-DRCT_REMOVE_LEGACY_ARCH=1")
523+
else
524+
ReactNativePodsUtils.remove_compiler_flag_from_project(installer, "-DRCT_REMOVE_LEGACY_ARCH=1")
525+
end
526+
521527
ReactNativePodsUtils.set_ccache_compiler_and_linker_build_settings(installer, react_native_path, ccache_enabled)
522528
ReactNativePodsUtils.updateOSDeploymentTarget(installer)
523529
ReactNativePodsUtils.set_dynamic_frameworks_flags(installer)

0 commit comments

Comments
 (0)