Skip to content

Commit a8d2685

Browse files
cipolleschifacebook-github-bot
authored andcommitted
Read the React Native version and set the new arch flag properly (#39388)
Summary: Pull Request resolved: #39388 This change prepare the infra to release and work properly in the dual release mode, making sure that the new architecture is turned on with some versions of react native. It connects the diffs in the previous changes in the stack. ## Changelog: [iOS][Changed] - Set the new arch flag based on the React Native version. Reviewed By: dmytrorykun Differential Revision: D49149212 fbshipit-source-id: 4d97cc2fbf64d217aec60c0d71de6ed1802ef793
1 parent f60b9f6 commit a8d2685

File tree

4 files changed

+65
-38
lines changed

4 files changed

+65
-38
lines changed

packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class NewArchitectureTests < Test::Unit::TestCase
1414
def teardown
1515
Pod::UI.reset()
1616
FileMock.reset()
17+
ENV["RCT_NEW_ARCH_ENABLED"] = nil
1718
end
1819

1920
# ============================= #
@@ -184,87 +185,87 @@ def test_installModulesDependencies_whenNewArchDisabledAndSearchPathsAndCompiler
184185
)
185186
end
186187

187-
# ========================== #
188-
# Test - Is New Arch Enabled #
189-
# ========================== #
188+
# =============================== #
189+
# Test - Compute New Arch Enabled #
190+
# =============================== #
190191

191-
def test_isNewArchEnabled_whenOnMainAndFlagTrue_returnTrue
192+
def test_computeNewArchEnabled_whenOnMainAndFlagTrue_returnTrue
192193
version = '1000.0.0'
193194
new_arch_enabled = true
194195

195-
isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)
196+
isEnabled = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, version)
196197

197198
assert_equal("1", isEnabled)
198199
end
199200

200-
def test_isNewArchEnabled_whenOnMainAndFlagFalse_returnFalse
201+
def test_computeNewArchEnabled_whenOnMainAndFlagFalse_returnFalse
201202
version = '1000.0.0'
202203
new_arch_enabled = false
203204

204-
isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)
205+
isEnabled = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, version)
205206

206207
assert_equal("0", isEnabled)
207208
end
208209

209-
def test_isNewArchEnabled_whenOnStableAndFlagTrue_returnTrue
210+
def test_computeNewArchEnabled_whenOnStableAndFlagTrue_returnTrue
210211
version = '0.73.0'
211212
new_arch_enabled = true
212213

213-
isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)
214+
isEnabled = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, version)
214215

215216
assert_equal("1", isEnabled)
216217
end
217218

218-
def test_isNewArchEnabled_whenOnStableAndFlagFalse_returnFalse
219+
def test_computeNewArchEnabled_whenOnStableAndFlagFalse_returnFalse
219220
version = '0.73.0'
220221
new_arch_enabled = false
221222

222-
isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)
223+
isEnabled = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, version)
223224

224225
assert_equal("0", isEnabled)
225226
end
226227

227-
def test_isNewArchEnabled_whenOn100AndFlagTrue_returnTrue
228+
def test_computeNewArchEnabled_whenOn100AndFlagTrue_returnTrue
228229
version = '1.0.0-prealpha.0'
229230
new_arch_enabled = true
230231

231-
isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)
232+
isEnabled = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, version)
232233

233234
assert_equal("1", isEnabled)
234235
end
235236

236-
def test_isNewArchEnabled_whenOn100PrealphaWithDotsAndFlagFalse_returnTrue
237+
def test_computeNewArchEnabled_whenOn100PrealphaWithDotsAndFlagFalse_returnTrue
237238
version = '1.0.0-prealpha.0'
238239
new_arch_enabled = false
239240

240-
isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)
241+
isEnabled = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, version)
241242

242243
assert_equal("1", isEnabled)
243244
end
244245

245-
def test_isNewArchEnabled_whenOn100PrealphaWithDashAndFlagFalse_returnTrue
246+
def test_computeNewArchEnabled_whenOn100PrealphaWithDashAndFlagFalse_returnTrue
246247
version = '1.0.0-prealpha-0'
247248
new_arch_enabled = false
248249

249-
isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)
250+
isEnabled = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, version)
250251

251252
assert_equal("1", isEnabled)
252253
end
253254

254-
def test_isNewArchEnabled_whenOn100PrealphaOnlyWordsAndFlagFalse_returnTrue
255+
def test_computeNewArchEnabled_whenOn100PrealphaOnlyWordsAndFlagFalse_returnTrue
255256
version = '1.0.0-prealpha0'
256257
new_arch_enabled = false
257258

258-
isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)
259+
isEnabled = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, version)
259260

260261
assert_equal("1", isEnabled)
261262
end
262263

263-
def test_isNewArchEnabled_whenOnGreaterThan100AndFlagFalse_returnTrue
264+
def test_computeNewArchEnabled_whenOnGreaterThan100AndFlagFalse_returnTrue
264265
version = '3.2.1'
265266
new_arch_enabled = false
266267

267-
isEnabled = NewArchitectureHelper.is_new_arch_enabled(new_arch_enabled, version)
268+
isEnabled = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, version)
268269

269270
assert_equal("1", isEnabled)
270271
end
@@ -296,6 +297,28 @@ def test_extractReactNativeVersion_whenFileExists_returnTheRightVersion()
296297
assert_equal("1.0.0-prealpha.0", version)
297298
end
298299

300+
# =============================== #
301+
# Test - New Architecture Enabled #
302+
# =============================== #
303+
def test_newArchEnabled_whenRCTNewArchEnabledIsSetTo1_returnTrue
304+
ENV["RCT_NEW_ARCH_ENABLED"] = "1"
305+
is_enabled = NewArchitectureHelper.new_arch_enabled
306+
assert_true(is_enabled)
307+
end
308+
309+
def test_newArchEnabled_whenRCTNewArchEnabledIsSetTo0_returnFalse
310+
ENV["RCT_NEW_ARCH_ENABLED"] = "0"
311+
is_enabled = NewArchitectureHelper.new_arch_enabled
312+
assert_false(is_enabled)
313+
end
314+
315+
def test_newArchEnabled_whenRCTNewArchEnabledIsNotSet_returnFalse
316+
ENV["RCT_NEW_ARCH_ENABLED"] = nil
317+
is_enabled = NewArchitectureHelper.new_arch_enabled
318+
assert_false(is_enabled)
319+
end
320+
321+
299322
end
300323

301324
# ================ #

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled)
7474
# Set "RCT_DYNAMIC_FRAMEWORKS=1" if pod are installed with USE_FRAMEWORKS=dynamic
7575
# This helps with backward compatibility.
7676
if pod_name == 'React-RCTFabric' && ENV['USE_FRAMEWORKS'] == 'dynamic'
77+
Pod::UI.puts "Setting -DRCT_DYNAMIC_FRAMEWORKS=1 to React-RCTFabric".green
7778
rct_dynamic_framework_flag = " -DRCT_DYNAMIC_FRAMEWORKS=1"
7879
target_installation_result.native_target.build_configurations.each do |config|
7980
prev_build_settings = config.build_settings['OTHER_CPLUSPLUSFLAGS'] != nil ? config.build_settings['OTHER_CPLUSPLUSFLAGS'] : "$(inherithed)"
@@ -170,7 +171,7 @@ def self.extract_react_native_version(react_native_path, file_manager: File, jso
170171
return package["version"]
171172
end
172173

173-
def self.is_new_arch_enabled(new_arch_enabled, react_native_version)
174+
def self.compute_new_arch_enabled(new_arch_enabled, react_native_version)
174175
# Regex that identify a version with the syntax `<major>.<minor>.<patch>[-<prerelease>[.-]k]
175176
# where
176177
# - major is a number
@@ -192,4 +193,8 @@ def self.is_new_arch_enabled(new_arch_enabled, react_native_version)
192193
end
193194
return new_arch_enabled ? "1" : "0"
194195
end
196+
197+
def self.new_arch_enabled
198+
return ENV["RCT_NEW_ARCH_ENABLED"] == "1"
199+
end
195200
end

packages/react-native/scripts/react_native_pods.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def prepare_react_native_project!
7272
def use_react_native! (
7373
path: "../node_modules/react-native",
7474
fabric_enabled: false,
75-
new_arch_enabled: ENV['RCT_NEW_ARCH_ENABLED'] == '1',
75+
new_arch_enabled: NewArchitectureHelper.new_arch_enabled,
7676
production: false, # deprecated
7777
hermes_enabled: ENV['USE_HERMES'] && ENV['USE_HERMES'] == '0' ? false : true,
7878
flipper_configuration: FlipperConfiguration.disabled,
@@ -93,8 +93,11 @@ def use_react_native! (
9393

9494
# We are relying on this flag also in third parties libraries to proper install dependencies.
9595
# Better to rely and enable this environment flag if the new architecture is turned on using flags.
96-
ENV['RCT_NEW_ARCH_ENABLED'] = new_arch_enabled ? "1" : "0"
97-
fabric_enabled = fabric_enabled || new_arch_enabled
96+
relative_path_from_current = Pod::Config.instance.installation_root.relative_path_from(Pathname.pwd)
97+
react_native_version = NewArchitectureHelper.extract_react_native_version(File.join(relative_path_from_current, path))
98+
ENV['RCT_NEW_ARCH_ENABLED'] = NewArchitectureHelper.compute_new_arch_enabled(new_arch_enabled, react_native_version)
99+
100+
fabric_enabled = fabric_enabled || NewArchitectureHelper.new_arch_enabled
98101
ENV['RCT_FABRIC_ENABLED'] = fabric_enabled ? "1" : "0"
99102
ENV['USE_HERMES'] = hermes_enabled ? "1" : "0"
100103

@@ -104,7 +107,7 @@ def use_react_native! (
104107

105108
# The Pods which should be included in all projects
106109
pod 'FBLazyVector', :path => "#{prefix}/Libraries/FBLazyVector"
107-
pod 'FBReactNativeSpec', :path => "#{prefix}/React/FBReactNativeSpec" if !new_arch_enabled
110+
pod 'FBReactNativeSpec', :path => "#{prefix}/React/FBReactNativeSpec" if !NewArchitectureHelper.new_arch_enabled
108111
pod 'RCTRequired', :path => "#{prefix}/Libraries/RCTRequired"
109112
pod 'RCTTypeSafety', :path => "#{prefix}/Libraries/TypeSafety", :modular_headers => true
110113
pod 'React', :path => "#{prefix}/"
@@ -156,7 +159,7 @@ def use_react_native! (
156159
run_codegen!(
157160
app_path,
158161
config_file_dir,
159-
:new_arch_enabled => new_arch_enabled,
162+
:new_arch_enabled => NewArchitectureHelper.new_arch_enabled,
160163
:disable_codegen => ENV['DISABLE_CODEGEN'] == '1',
161164
:react_native_path => prefix,
162165
:fabric_enabled => fabric_enabled,
@@ -172,14 +175,14 @@ def use_react_native! (
172175
# If the New Arch is turned off, we will use the Old Renderer, though.
173176
# RNTester always installed Fabric, this change is required to make the template work.
174177
setup_fabric!(:react_native_path => prefix)
175-
checkAndGenerateEmptyThirdPartyProvider!(prefix, new_arch_enabled)
178+
checkAndGenerateEmptyThirdPartyProvider!(prefix, NewArchitectureHelper.new_arch_enabled)
176179

177180
if !fabric_enabled
178181
relative_installation_root = Pod::Config.instance.installation_root.relative_path_from(Pathname.pwd)
179182
build_codegen!(prefix, relative_installation_root)
180183
end
181184

182-
if new_arch_enabled
185+
if NewArchitectureHelper.new_arch_enabled
183186
setup_bridgeless!(:react_native_path => prefix, :use_hermes => hermes_enabled)
184187
end
185188

@@ -212,7 +215,7 @@ def folly_flags()
212215
# Parameters:
213216
# - spec: The spec that has to be configured with the New Architecture code
214217
# - new_arch_enabled: Whether the module should install dependencies for the new architecture
215-
def install_modules_dependencies(spec, new_arch_enabled: ENV['RCT_NEW_ARCH_ENABLED'] == "1")
218+
def install_modules_dependencies(spec, new_arch_enabled: NewArchitectureHelper.new_arch_enabled)
216219
NewArchitectureHelper.install_modules_dependencies(spec, new_arch_enabled, $FOLLY_VERSION)
217220
end
218221

@@ -271,8 +274,7 @@ def react_native_post_install(
271274
ReactNativePodsUtils.apply_ats_config(installer)
272275

273276
NewArchitectureHelper.set_clang_cxx_language_standard_if_needed(installer)
274-
is_new_arch_enabled = ENV['RCT_NEW_ARCH_ENABLED'] == "1"
275-
NewArchitectureHelper.modify_flags_for_new_architecture(installer, is_new_arch_enabled)
277+
NewArchitectureHelper.modify_flags_for_new_architecture(installer, NewArchitectureHelper.new_arch_enabled)
276278

277279
Pod::UI.puts "Pod install took #{Time.now.to_i - $START_TIME} [s] to run".green
278280
end
@@ -281,7 +283,7 @@ def react_native_post_install(
281283
# We need to keep this while we continue to support the old architecture.
282284
# =====================
283285
def use_react_native_codegen!(spec, options={})
284-
return if ENV['RCT_NEW_ARCH_ENABLED'] == "1"
286+
return if NewArchitectureHelper.new_arch_enabled
285287
# TODO: Once the new codegen approach is ready for use, we should output a warning here to let folks know to migrate.
286288

287289
# The prefix to react-native

packages/rn-tester/Podfile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ def pods(target_name, options = {}, use_flipper: $shouldUseFlipper)
4444
hermes_enabled = !ENV.has_key?('USE_HERMES') || ENV['USE_HERMES'] == '1'
4545
puts "Configuring #{target_name} with Fabric #{fabric_enabled ? "enabled" : "disabled"}.#{hermes_enabled ? " Using Hermes engine." : ""}"
4646

47-
if ENV['RCT_NEW_ARCH_ENABLED'] == '1'
48-
# Custom fabric component is only supported when using codegen discovery.
49-
pod 'MyNativeView', :path => "NativeComponentExample"
50-
end
51-
5247
use_react_native!(
5348
path: @prefix_path,
5449
fabric_enabled: fabric_enabled,
@@ -68,7 +63,9 @@ def pods(target_name, options = {}, use_flipper: $shouldUseFlipper)
6863

6964
# RNTester native modules and components
7065
pod 'ScreenshotManager', :path => "NativeModuleExample"
66+
7167
if ENV['RCT_NEW_ARCH_ENABLED'] == '1'
68+
pod 'MyNativeView', :path => "NativeComponentExample"
7269
pod 'NativeCxxModuleExample', :path => "NativeCxxModuleExample"
7370
end
7471
end

0 commit comments

Comments
 (0)