Skip to content

Commit 71da212

Browse files
Riccardo Cipolleschifacebook-github-bot
authored andcommitted
Move New Architecture setup to new_architecture.rb file (#33990)
Summary: Pull Request resolved: #33990 This diff moves the setting of some CPP flags from the main React native pods file to a dedicated file. It also introduces some tests and it improves the Test Mocks we have ## Changelog [iOS][Changed] - Move the `modify_flags_for_new_architecture` method to separate ruby file Reviewed By: cortinico Differential Revision: D37040927 fbshipit-source-id: 037ddaf123d01f3a2fd622b8a0cd10535da70b92
1 parent 5854b11 commit 71da212

5 files changed

Lines changed: 210 additions & 58 deletions

File tree

‎packages/rn-tester/Podfile‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require_relative'../../scripts/react_native_pods'
2-
require_relative'../../scripts/cocoapods/new_architecture'
32

43
source'https://cdn.cocoapods.org/'
54
platform:ios,'12.4'
@@ -29,7 +28,7 @@ def pods(options = {}, use_flipper: false)
2928
# Custom fabric component is only supported when using codegen discovery.
3029
pod'MyNativeView',:path=>"NativeComponentExample"
3130
end
32-
31+
3332
use_react_native!(
3433
path: @prefix_path,
3534
fabric_enabled: fabric_enabled,
@@ -67,5 +66,4 @@ end
6766
post_installdo |installer|
6867
react_native_post_install(installer,@prefix_path)
6968
__apply_Xcode_12_5_M1_post_install_workaround(installer)
70-
set_clang_cxx_language_standard_if_needed(installer)
7169
end

‎scripts/cocoapods/__tests__/new_architecture-test.rb‎

Lines changed: 113 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@
99
require_relative"./test_utils/PodMock.rb"
1010

1111
classNewArchitectureTests < Test::Unit::TestCase
12-
defsetup
13-
File.enable_testing_mode!
14-
end
15-
1612
defteardown
1713
Pod::UI.reset()
1814
end
1915

16+
# ============================= #
17+
# Test - Set Clang Cxx Lang Std #
18+
# ============================= #
2019

2120
deftest_setClangCxxLanguageStandardIfNeeded_whenReactCoreIsPresent
2221
installer=prepare_mocked_installer_with_react_core
23-
set_clang_cxx_language_standard_if_needed(installer)
22+
NewArchitectureHelper.set_clang_cxx_language_standard_if_needed(installer)
2423

2524
assert_equal(installer.aggregate_targets[0].user_project.build_configurations[0].build_settings["CLANG_CXX_LANGUAGE_STANDARD"],"c++17")
2625
assert_equal(installer.aggregate_targets[1].user_project.build_configurations[0].build_settings["CLANG_CXX_LANGUAGE_STANDARD"],"c++17")
@@ -30,7 +29,7 @@ def test_setClangCxxLanguageStandardIfNeeded_whenReactCoreIsPresent
3029

3130
deftest_setClangCxxLanguageStandardIfNeeded_whenReactCoreIsNotPresent
3231
installer=prepare_mocked_installer_without_react_core
33-
set_clang_cxx_language_standard_if_needed(installer)
32+
NewArchitectureHelper.set_clang_cxx_language_standard_if_needed(installer)
3433

3534
assert_equal(installer.aggregate_targets[0].user_project.build_configurations[0].build_settings["CLANG_CXX_LANGUAGE_STANDARD"],nil)
3635
assert_equal(installer.aggregate_targets[1].user_project.build_configurations[0].build_settings["CLANG_CXX_LANGUAGE_STANDARD"],nil)
@@ -40,15 +39,81 @@ def test_setClangCxxLanguageStandardIfNeeded_whenReactCoreIsNotPresent
4039

4140
deftest_setClangCxxLanguageStandardIfNeeded_whenThereAreDifferentValuesForLanguageStandard_takesTheFirstValue
4241
installer=prepare_mocked_installer_with_react_core_and_different_language_standards
43-
set_clang_cxx_language_standard_if_needed(installer)
42+
NewArchitectureHelper.set_clang_cxx_language_standard_if_needed(installer)
4443

4544
assert_equal(installer.aggregate_targets[0].user_project.build_configurations[0].build_settings["CLANG_CXX_LANGUAGE_STANDARD"],"c++17")
4645
assert_equal(installer.aggregate_targets[1].user_project.build_configurations[0].build_settings["CLANG_CXX_LANGUAGE_STANDARD"],"c++17")
4746
assert_equal(installer.pods_project.targets[1].received_resolved_build_setting_parameters,[ReceivedCommonResolvedBuildSettings.new("CLANG_CXX_LANGUAGE_STANDARD",true)])
4847
assert_equal(Pod::UI.collected_messages,["Setting CLANG_CXX_LANGUAGE_STANDARD to c++17 on /test/path.xcproj","Setting CLANG_CXX_LANGUAGE_STANDARD to c++17 on /test/path2.xcproj"])
4948
end
49+
50+
# =================== #
51+
# Test - Modify Flags #
52+
# =================== #
53+
deftest_modifyFlagsForNewArch_whenOnOldArch_doNothing
54+
# Arrange
55+
first_xcconfig=prepare_xcconfig("First")
56+
second_xcconfig=prepare_xcconfig("Second")
57+
react_core_debug_config=prepare_CXX_Flags_build_configuration("Debug")
58+
react_core_release_config=prepare_CXX_Flags_build_configuration("Release")
59+
yoga_debug_config=prepare_CXX_Flags_build_configuration("Debug")
60+
yoga_release_config=prepare_CXX_Flags_build_configuration("Release")
61+
62+
installer=prepare_installer_for_cpp_flags(
63+
[first_xcconfig,second_xcconfig],
64+
{
65+
"React-Core"=>[react_core_debug_config,react_core_release_config],
66+
"Yoga"=>[yoga_debug_config,yoga_release_config],
67+
}
68+
)
69+
# Act
70+
NewArchitectureHelper.modify_flags_for_new_architecture(installer,false)
71+
72+
# Assert
73+
assert_equal(first_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"],"$(inherited)")
74+
assert_equal(first_xcconfig.save_as_invocation,[])
75+
assert_equal(second_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"],"$(inherited)")
76+
assert_equal(second_xcconfig.save_as_invocation,[])
77+
assert_equal(react_core_debug_config.build_settings["OTHER_CPLUSPLUSFLAGS"],"$(inherited)")
78+
assert_equal(react_core_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"],"$(inherited)")
79+
assert_equal(yoga_debug_config.build_settings["OTHER_CPLUSPLUSFLAGS"],"$(inherited)")
80+
assert_equal(yoga_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"],"$(inherited)")
81+
end
82+
83+
deftest_modifyFlagsForNewArch_whenOnNewArch_updateFlags
84+
# Arrange
85+
first_xcconfig=prepare_xcconfig("First")
86+
second_xcconfig=prepare_xcconfig("Second")
87+
react_core_debug_config=prepare_CXX_Flags_build_configuration("Debug")
88+
react_core_release_config=prepare_CXX_Flags_build_configuration("Release")
89+
yoga_debug_config=prepare_CXX_Flags_build_configuration("Debug")
90+
yoga_release_config=prepare_CXX_Flags_build_configuration("Release")
91+
92+
installer=prepare_installer_for_cpp_flags(
93+
[first_xcconfig,second_xcconfig],
94+
{
95+
"React-Core"=>[react_core_debug_config,react_core_release_config],
96+
"Yoga"=>[yoga_debug_config,yoga_release_config],
97+
}
98+
)
99+
# Act
100+
NewArchitectureHelper.modify_flags_for_new_architecture(installer,true)
101+
102+
# Assert
103+
assert_equal(first_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"],"$(inherited) -DRCT_NEW_ARCH_ENABLED=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1")
104+
assert_equal(first_xcconfig.save_as_invocation,["a/path/First.xcconfig"])
105+
assert_equal(second_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"],"$(inherited) -DRCT_NEW_ARCH_ENABLED=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1")
106+
assert_equal(second_xcconfig.save_as_invocation,["a/path/Second.xcconfig"])
107+
assert_equal(react_core_debug_config.build_settings["OTHER_CPLUSPLUSFLAGS"],"$(inherited) -DRCT_NEW_ARCH_ENABLED=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1")
108+
assert_equal(react_core_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"],"$(inherited) -DRCT_NEW_ARCH_ENABLED=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1")
109+
assert_equal(yoga_debug_config.build_settings["OTHER_CPLUSPLUSFLAGS"],"$(inherited)")
110+
assert_equal(yoga_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"],"$(inherited)")
111+
end
50112
end
51113

114+
# ================ #
115+
# Test - Utilities #
116+
# ================ #
52117
defprepare_mocked_installer_with_react_core
53118
returnInstallerMock.new(
54119
PodsProjectMock.new([
@@ -131,3 +196,44 @@ def prepare_mocked_installer_without_react_core
131196
]
132197
)
133198
end
199+
200+
defprepare_xcconfig(name)
201+
returnXCConfigMock.new(name,:attributes=>{"OTHER_CPLUSPLUSFLAGS"=>"$(inherited)"})
202+
end
203+
204+
defprepare_CXX_Flags_build_configuration(name)
205+
returnBuildConfigurationMock.new(name,{
206+
"OTHER_CPLUSPLUSFLAGS"=>"$(inherited)"
207+
})
208+
end
209+
210+
defprepare_pod_target_installation_results_mock(name,configs)
211+
returnPodTargetInstallationResultsMock.new(
212+
:name=>name,
213+
:native_target=>TargetMock.new(name,configs)
214+
)
215+
end
216+
217+
defprepare_installer_for_cpp_flags(xcconfigs,build_configs)
218+
xcconfigs_map={}
219+
xcconfigs.eachdo |config|
220+
xcconfigs_map[config.name.to_s]=config
221+
end
222+
223+
pod_target_installation_results_map={}
224+
build_configs.eachdo |name,build_configs|
225+
pod_target_installation_results_map[name.to_s]=prepare_pod_target_installation_results_mock(
226+
name.to_s,build_configs
227+
)
228+
end
229+
230+
returnInstallerMock.new(
231+
PodsProjectMock.new,
232+
[
233+
AggregatedProjectMock.new(:xcconfigs=>xcconfigs_map,:base_path=>"a/path/")
234+
],
235+
:target_installation_results=>TargetInstallationResultsMock.new(
236+
:pod_target_installation_results=>pod_target_installation_results_map
237+
)
238+
)
239+
end

‎scripts/cocoapods/__tests__/test_utils/InstallerMock.rb‎

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@
3939
classInstallerMock
4040
attr_reader:pods_project
4141
attr_reader:aggregate_targets
42+
attr_reader:target_installation_results
4243

43-
definitialize(pods_project=PodsProjectMock.new,aggregate_targets=[AggregatedProjectMock.new])
44+
definitialize(pods_project=PodsProjectMock.new,aggregate_targets=[AggregatedProjectMock.new],target_installation_results: [])
4445
@pods_project=pods_project
4546
@aggregate_targets=aggregate_targets
47+
@target_installation_results=target_installation_results
4648
end
4749

4850
deftarget_with_name(name)
@@ -80,9 +82,18 @@ def save()
8082

8183
classAggregatedProjectMock
8284
attr_reader:user_project
85+
attr_reader:xcconfigs
8386

84-
definitialize(user_project=UserProjectMock.new)
87+
@base_path
88+
89+
definitialize(user_project=UserProjectMock.new,xcconfigs: {},base_path: "")
8590
@user_project=user_project
91+
@xcconfigs=xcconfigs
92+
@base_path=base_path
93+
end
94+
95+
defxcconfig_path(config_name)
96+
returnFile.join(@base_path,"#{config_name}.xcconfig")
8697
end
8798
end
8899

@@ -106,6 +117,22 @@ def save()
106117
end
107118
end
108119

120+
classXCConfigMock
121+
attr_reader:name
122+
attr_accessor:attributes
123+
attr_reader:save_as_invocation
124+
125+
definitialize(name,attributes: {})
126+
@name=name
127+
@attributes=attributes
128+
@save_as_invocation=[]
129+
end
130+
131+
defsave_as(file_path)
132+
@save_as_invocation.push(file_path)
133+
end
134+
end
135+
109136
ReceivedCommonResolvedBuildSettings=Struct.new(:key,:resolve_against_xcconfig)
110137

111138
classTargetMock
@@ -136,3 +163,21 @@ def initialize(name, build_settings = {})
136163
@build_settings=build_settings
137164
end
138165
end
166+
167+
classTargetInstallationResultsMock
168+
attr_reader:pod_target_installation_results
169+
170+
definitialize(pod_target_installation_results: {})
171+
@pod_target_installation_results=pod_target_installation_results
172+
end
173+
end
174+
175+
classPodTargetInstallationResultsMock
176+
attr_reader:name
177+
attr_reader:native_target
178+
179+
definitialize(name: "",native_target: TargetMock.new())
180+
@name=name
181+
@native_target=native_target
182+
end
183+
end

‎scripts/cocoapods/new_architecture.rb‎

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,57 @@
33
# This source code is licensed under the MIT license found in the
44
# LICENSE file in the root directory of this source tree.
55

6-
defset_clang_cxx_language_standard_if_needed(installer)
7-
language_standard=nil
6+
classNewArchitectureHelper
87

9-
installer.pods_project.targets.eachdo |target|
10-
iftarget.name == 'React-Core'
11-
language_standard=target.resolved_build_setting("CLANG_CXX_LANGUAGE_STANDARD",resolve_against_xcconfig: true).values[0]
8+
@@new_arch_cpp_flags='$(inherited) -DRCT_NEW_ARCH_ENABLED=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1'
9+
10+
defself.set_clang_cxx_language_standard_if_needed(installer)
11+
language_standard=nil
12+
13+
installer.pods_project.targets.eachdo |target|
14+
iftarget.name == 'React-Core'
15+
language_standard=target.resolved_build_setting("CLANG_CXX_LANGUAGE_STANDARD",resolve_against_xcconfig: true).values[0]
16+
end
1217
end
13-
end
1418

15-
unlesslanguage_standard.nil?
16-
projects=installer.aggregate_targets
17-
.map{ |t| t.user_project}
18-
.uniq{ |p| p.path}
19+
unlesslanguage_standard.nil?
20+
projects=installer.aggregate_targets
21+
.map{ |t| t.user_project}
22+
.uniq{ |p| p.path}
1923

20-
projects.eachdo |project|
21-
Pod::UI.puts("Setting CLANG_CXX_LANGUAGE_STANDARD to #{language_standard} on #{project.path}")
24+
projects.eachdo |project|
25+
Pod::UI.puts("Setting CLANG_CXX_LANGUAGE_STANDARD to #{language_standard} on #{project.path}")
2226

23-
project.build_configurations.eachdo |config|
24-
config.build_settings["CLANG_CXX_LANGUAGE_STANDARD"]=language_standard
27+
project.build_configurations.eachdo |config|
28+
config.build_settings["CLANG_CXX_LANGUAGE_STANDARD"]=language_standard
29+
end
30+
31+
project.save()
2532
end
33+
end
34+
end
2635

27-
project.save()
36+
defself.modify_flags_for_new_architecture(installer,is_new_arch_enabled)
37+
unlessis_new_arch_enabled
38+
return
39+
end
40+
41+
# Add RCT_NEW_ARCH_ENABLED to Target pods xcconfig
42+
installer.aggregate_targets.eachdo |aggregate_target|
43+
aggregate_target.xcconfigs.eachdo |config_name,config_file|
44+
config_file.attributes['OTHER_CPLUSPLUSFLAGS']=@@new_arch_cpp_flags
45+
xcconfig_path=aggregate_target.xcconfig_path(config_name)
46+
config_file.save_as(xcconfig_path)
47+
end
48+
end
49+
50+
# Add RCT_NEW_ARCH_ENABLED to generated pod target projects
51+
installer.target_installation_results.pod_target_installation_results.eachdo |pod_name,target_installation_result|
52+
ifpod_name == 'React-Core'
53+
target_installation_result.native_target.build_configurations.eachdo |config|
54+
config.build_settings['OTHER_CPLUSPLUSFLAGS']=@@new_arch_cpp_flags
55+
end
56+
end
2857
end
2958
end
3059
end

‎scripts/react_native_pods.rb‎

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
require_relative'./cocoapods/fabric.rb'
1212
require_relative'./cocoapods/codegen.rb'
1313
require_relative'./cocoapods/utils.rb'
14+
require_relative'./cocoapods/new_architecture.rb'
1415

1516
$CODEGEN_OUTPUT_DIR ='build/generated/ios'
1617
$CODEGEN_COMPONENT_DIR ='react/renderer/components'
1718
$CODEGEN_MODULE_DIR ='.'
1819
$REACT_CODEGEN_PODSPEC_GENERATED =false
1920
$REACT_CODEGEN_DISCOVERY_DONE =false
20-
DEFAULT_OTHER_CPLUSPLUSFLAGS='$(inherited)'
21-
NEW_ARCH_OTHER_CPLUSPLUSFLAGS='$(inherited) -DRCT_NEW_ARCH_ENABLED=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1'
2221

2322
$START_TIME =Time.now.to_i
2423

@@ -148,38 +147,13 @@ def react_native_post_install(installer, react_native_path = "../node_modules/re
148147

149148
ReactNativePodsUtils.exclude_i386_architecture_while_using_hermes(installer)
150149
ReactNativePodsUtils.fix_library_search_paths(installer)
151-
152-
cpp_flags=DEFAULT_OTHER_CPLUSPLUSFLAGS
153-
ifENV['RCT_NEW_ARCH_ENABLED'] == '1'
154-
cpp_flags=NEW_ARCH_OTHER_CPLUSPLUSFLAGS
155-
end
156-
modify_flags_for_new_architecture(installer,cpp_flags)
157-
158150
ReactNativePodsUtils.set_node_modules_user_settings(installer,react_native_path)
159151

160-
puts"Pod install took #{Time.now.to_i - $START_TIME} [s] to run"
161-
end
162-
163-
defmodify_flags_for_new_architecture(installer,cpp_flags)
164-
# Add RCT_NEW_ARCH_ENABLED to Target pods xcconfig
165-
installer.aggregate_targets.eachdo |aggregate_target|
166-
aggregate_target.xcconfigs.eachdo |config_name,config_file|
167-
config_file.attributes['OTHER_CPLUSPLUSFLAGS']=cpp_flags
168-
xcconfig_path=aggregate_target.xcconfig_path(config_name)
169-
Pod::UI.putsxcconfig_path
170-
config_file.save_as(xcconfig_path)
171-
end
172-
end
152+
NewArchitectureHelper.set_clang_cxx_language_standard_if_needed(installer)
153+
is_new_arch_enabled=ENV['RCT_NEW_ARCH_ENABLED'] == '1'
154+
NewArchitectureHelper.modify_flags_for_new_architecture(installer,is_new_arch_enabled)
173155

174-
# Add RCT_NEW_ARCH_ENABLED to generated pod target projects
175-
installer.target_installation_results.pod_target_installation_results
176-
.eachdo |pod_name,target_installation_result|
177-
ifpod_name == 'React-Core'
178-
target_installation_result.native_target.build_configurations.eachdo |config|
179-
config.build_settings['OTHER_CPLUSPLUSFLAGS']=cpp_flags
180-
end
181-
end
182-
end
156+
Pod::UI.puts"Pod install took #{Time.now.to_i - $START_TIME} [s] to run".green
183157
end
184158

185159
defget_react_codegen_spec(options={})

0 commit comments

Comments
 (0)