From 3b3d502ccfc163ecb8531fb465032e831980b8af Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Tue, 11 Mar 2025 05:05:49 -0700 Subject: [PATCH] Automate setting the `RCTNewArchEnabled` flag (#49927) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49927 This change automates updating App's Info.plist with the new `RCTNewArchEnabled` boolean entry. The value depends on how the pod install is set up. In this way, we maintain the previous UX to enable/disable the New Arch. ## Context The RCT_NEW_ARCH_ENABLE flag is a compile time flag we used for almost two years to configure the iOS apps and to determine whether the app should build with the New Arch or not. However, given that we are looking into prebuilding React Native, we have to get rid of all the compilation flags, because they would require us to prebuild a combinatorial number of artifacts for react native. For example: - New Arch / Hermes - Old Arch / Hermes - New Arch / JSC - Old Arch / JSC - ... ## Backward compatibility We are going to keep adding the RCT_NEW_ARCH_ENABLED flag in all the dependencies, through the cocoapods inrastructure, so libraries, which are not prebuilt, will be build for the right architecture by the app itself. ## Changelog: [iOS][Added] - Reviewed By: cortinico Differential Revision: D70888212 fbshipit-source-id: 541a818e02ccb96c822de29b0714646d1e69a5a9 --- .../scripts/cocoapods/new_architecture.rb | 37 +++++++++++++++++++ .../react-native/scripts/react_native_pods.rb | 1 + 2 files changed, 38 insertions(+) diff --git a/packages/react-native/scripts/cocoapods/new_architecture.rb b/packages/react-native/scripts/cocoapods/new_architecture.rb index 6354cd5a984..97c91d14c28 100644 --- a/packages/react-native/scripts/cocoapods/new_architecture.rb +++ b/packages/react-native/scripts/cocoapods/new_architecture.rb @@ -9,6 +9,7 @@ require_relative "./utils.rb" require_relative "./helpers.rb" require_relative "./jsengine.rb" + class NewArchitectureHelper @@NewArchWarningEmitted = false # Used not to spam warnings to the user. @@ -159,4 +160,40 @@ class NewArchitectureHelper def self.new_arch_enabled return ENV["RCT_NEW_ARCH_ENABLED"] == '0' ? false : true end + + def self.set_RCTNewArchEnabled_in_info_plist(installer, new_arch_enabled) + projectPaths = installer.aggregate_targets + .map{ |t| t.user_project } + .uniq{ |p| p.path } + .map{ |p| p.path } + + excluded_info_plist = ["/Pods", "Tests", "metainternal", ".bundle"] + projectPaths.each do |projectPath| + projectFolderPath = File.dirname(projectPath) + infoPlistFiles = `find #{projectFolderPath} -name "Info.plist"` + infoPlistFiles = infoPlistFiles.split("\n").map { |f| f.strip } + + infoPlistFiles.each do |infoPlistFile| + # If infoPlistFile contains Pods or tests, skip it + should_skip = false + excluded_info_plist.each do |excluded| + if infoPlistFile.include? excluded + should_skip = true + end + end + next if should_skip + + # Read the file as a plist + info_plist = Xcodeproj::Plist.read_from_path(infoPlistFile) + # Check if it contains the RCTNewArchEnabled key + if info_plist["RCTNewArchEnabled"] and info_plist["RCTNewArchEnabled"] == new_arch_enabled + next + end + + # Add the key and value to the plist + info_plist["RCTNewArchEnabled"] = new_arch_enabled ? true : false + Xcodeproj::Plist.write_to_path(info_plist, infoPlistFile) + end + end + end end diff --git a/packages/react-native/scripts/react_native_pods.rb b/packages/react-native/scripts/react_native_pods.rb index 3b47cf5187e..c221e0c5726 100644 --- a/packages/react-native/scripts/react_native_pods.rb +++ b/packages/react-native/scripts/react_native_pods.rb @@ -451,6 +451,7 @@ def react_native_post_install( NewArchitectureHelper.set_clang_cxx_language_standard_if_needed(installer) NewArchitectureHelper.modify_flags_for_new_architecture(installer, NewArchitectureHelper.new_arch_enabled) + NewArchitectureHelper.set_RCTNewArchEnabled_in_info_plist(installer, NewArchitectureHelper.new_arch_enabled) if ENV['USE_HERMES'] == '0' && ENV['USE_THIRD_PARTY_JSC'] != '1' print_jsc_removal_message()