Skip to content

Commit 1a0e174

Browse files
tjzelfacebook-github-bot
authored andcommitted
Add NDEBUG flag for Release builds for both architectures (#41715)
Summary: Currently React Native defines `NDEBUG` flag for all pods in Fabric only. This is useful for other libraries, like Reanimated, because they have no easy way of defining their compilation flags (at least none that I know of). Therefore defining `NDEBUG` for both architectures would be beneficial. ## Changelog: Pick one each for the category and type tags: [IOS] [CHANGED] - Add `NDEBUG` flag for Release builds for both architectures Pull Request resolved: #41715 Test Plan: Run ruby test suite. ## Notes For the time being I just copied `prepare_pod_target_installation_results_mock` and `def prepare_installer_for_cpp_flags` to `utils-test.rb` since I wasn't sure how to handle the installer mock. Reviewed By: cortinico Differential Revision: D51708382 Pulled By: cipolleschi fbshipit-source-id: ff206f8fc151934dbae89aacd1bc69c57b4f28ee
1 parent dbf0984 commit 1a0e174

File tree

5 files changed

+152
-24
lines changed

5 files changed

+152
-24
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ def test_modifyFlagsForNewArch_whenOnNewArchAndIsRelease_updateFlags
120120
assert_equal(second_xcconfig.save_as_invocation, ["a/path/Second.xcconfig"])
121121
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 -DFOLLY_CFG_NO_COROUTINES=1 -DFOLLY_HAVE_CLOCK_GETTIME=1")
122122
assert_nil(react_core_debug_config.build_settings["OTHER_CFLAGS"])
123-
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 -DFOLLY_CFG_NO_COROUTINES=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DNDEBUG")
124-
assert_equal(react_core_release_config.build_settings["OTHER_CFLAGS"], "$(inherited) -DNDEBUG")
123+
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 -DFOLLY_CFG_NO_COROUTINES=1 -DFOLLY_HAVE_CLOCK_GETTIME=1")
124+
assert_nil(react_core_release_config.build_settings["OTHER_CFLAGS"])
125125
assert_equal(yoga_debug_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited)")
126126
assert_nil(yoga_debug_config.build_settings["OTHER_CFLAGS"])
127-
assert_equal(yoga_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited) -DNDEBUG")
128-
assert_equal(yoga_release_config.build_settings["OTHER_CFLAGS"], "$(inherited) -DNDEBUG")
127+
assert_equal(yoga_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited)")
128+
assert_nil(yoga_release_config.build_settings["OTHER_CFLAGS"])
129129
end
130130

131131
# =================================== #

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
require_relative "./test_utils/XcodeprojMock.rb"
1717
require_relative "./test_utils/XcodebuildMock.rb"
1818
require_relative "./test_utils/SpecMock.rb"
19+
require_relative "./test_utils/InstallerMock.rb"
1920

2021
class UtilsTests < Test::Unit::TestCase
2122
def setup
@@ -1008,6 +1009,78 @@ def test_addDependencies_whenSubspecsAndHeaderSearchPathAndVersionWithAdditional
10081009
assert_equal(spec.to_hash["pod_target_xcconfig"], {
10091010
"HEADER_SEARCH_PATHS" => expected_search_paths})
10101011
end
1012+
1013+
def test_add_flag_to_map_with_inheritance_whenUsedWithBuildConfigBuildSettings
1014+
# Arrange
1015+
empty_config = BuildConfigurationMock.new("EmptyConfig")
1016+
initialized_config = BuildConfigurationMock.new("InitializedConfig", {
1017+
"OTHER_CPLUSPLUSFLAGS" => "INIT_FLAG"
1018+
})
1019+
twiceProcessed_config = BuildConfigurationMock.new("TwiceProcessedConfig");
1020+
test_flag = " -DTEST_FLAG=1"
1021+
1022+
# Act
1023+
ReactNativePodsUtils.add_flag_to_map_with_inheritance(empty_config.build_settings, "OTHER_CPLUSPLUSFLAGS", test_flag)
1024+
ReactNativePodsUtils.add_flag_to_map_with_inheritance(initialized_config.build_settings, "OTHER_CPLUSPLUSFLAGS", test_flag)
1025+
ReactNativePodsUtils.add_flag_to_map_with_inheritance(twiceProcessed_config.build_settings, "OTHER_CPLUSPLUSFLAGS", test_flag)
1026+
ReactNativePodsUtils.add_flag_to_map_with_inheritance(twiceProcessed_config.build_settings, "OTHER_CPLUSPLUSFLAGS", test_flag)
1027+
1028+
# Assert
1029+
assert_equal("$(inherited)" + test_flag, empty_config.build_settings["OTHER_CPLUSPLUSFLAGS"])
1030+
assert_equal("$(inherited) INIT_FLAG" + test_flag, initialized_config.build_settings["OTHER_CPLUSPLUSFLAGS"])
1031+
assert_equal("$(inherited)" + test_flag, twiceProcessed_config.build_settings["OTHER_CPLUSPLUSFLAGS"])
1032+
end
1033+
1034+
def test_add_flag_to_map_with_inheritance_whenUsedWithXCConfigAttributes
1035+
# Arrange
1036+
empty_xcconfig = XCConfigMock.new("EmptyConfig")
1037+
initialized_xcconfig = XCConfigMock.new("InitializedConfig", attributes: {
1038+
"OTHER_CPLUSPLUSFLAGS" => "INIT_FLAG"
1039+
})
1040+
twiceProcessed_xcconfig = XCConfigMock.new("TwiceProcessedConfig");
1041+
test_flag = " -DTEST_FLAG=1"
1042+
1043+
# Act
1044+
ReactNativePodsUtils.add_flag_to_map_with_inheritance(empty_xcconfig.attributes, "OTHER_CPLUSPLUSFLAGS", test_flag)
1045+
ReactNativePodsUtils.add_flag_to_map_with_inheritance(initialized_xcconfig.attributes, "OTHER_CPLUSPLUSFLAGS", test_flag)
1046+
ReactNativePodsUtils.add_flag_to_map_with_inheritance(twiceProcessed_xcconfig.attributes, "OTHER_CPLUSPLUSFLAGS", test_flag)
1047+
ReactNativePodsUtils.add_flag_to_map_with_inheritance(twiceProcessed_xcconfig.attributes, "OTHER_CPLUSPLUSFLAGS", test_flag)
1048+
1049+
# Assert
1050+
assert_equal("$(inherited)" + test_flag, empty_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"])
1051+
assert_equal("$(inherited) INIT_FLAG" + test_flag, initialized_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"])
1052+
assert_equal("$(inherited)" + test_flag, twiceProcessed_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"])
1053+
end
1054+
1055+
def test_add_ndebug_flag_to_pods_in_release
1056+
# Arrange
1057+
xcconfig = XCConfigMock.new("Config")
1058+
default_debug_config = BuildConfigurationMock.new("Debug")
1059+
default_release_config = BuildConfigurationMock.new("Release")
1060+
custom_debug_config1 = BuildConfigurationMock.new("CustomDebug")
1061+
custom_debug_config2 = BuildConfigurationMock.new("Custom")
1062+
custom_release_config1 = BuildConfigurationMock.new("CustomRelease")
1063+
custom_release_config2 = BuildConfigurationMock.new("Production")
1064+
1065+
installer = prepare_installer_for_cpp_flags(
1066+
[ xcconfig ],
1067+
{
1068+
"Default" => [ default_debug_config, default_release_config ],
1069+
"Custom1" => [ custom_debug_config1, custom_release_config1 ],
1070+
"Custom2" => [ custom_debug_config2, custom_release_config2 ]
1071+
}
1072+
)
1073+
# Act
1074+
ReactNativePodsUtils.add_ndebug_flag_to_pods_in_release(installer)
1075+
1076+
# Assert
1077+
assert_equal(nil, default_debug_config.build_settings["OTHER_CPLUSPLUSFLAGS"])
1078+
assert_equal("$(inherited) -DNDEBUG", default_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"])
1079+
assert_equal(nil, custom_debug_config1.build_settings["OTHER_CPLUSPLUSFLAGS"])
1080+
assert_equal("$(inherited) -DNDEBUG", custom_release_config1.build_settings["OTHER_CPLUSPLUSFLAGS"])
1081+
assert_equal(nil, custom_debug_config2.build_settings["OTHER_CPLUSPLUSFLAGS"])
1082+
assert_equal("$(inherited) -DNDEBUG", custom_release_config2.build_settings["OTHER_CPLUSPLUSFLAGS"])
1083+
end
10111084
end
10121085

10131086
# ===== #
@@ -1050,3 +1123,31 @@ def prepare_Code_Signing_build_configuration(name, param)
10501123
"CODE_SIGNING_ALLOWED" => param
10511124
})
10521125
end
1126+
1127+
def prepare_pod_target_installation_results_mock(name, configs)
1128+
target = TargetMock.new(name, configs)
1129+
return TargetInstallationResultMock.new(target, target)
1130+
end
1131+
1132+
def prepare_installer_for_cpp_flags(xcconfigs, build_configs)
1133+
xcconfigs_map = {}
1134+
xcconfigs.each do |config|
1135+
xcconfigs_map[config.name.to_s] = config
1136+
end
1137+
1138+
pod_target_installation_results_map = {}
1139+
build_configs.each do |name, build_configs|
1140+
pod_target_installation_results_map[name.to_s] = prepare_pod_target_installation_results_mock(
1141+
name.to_s, build_configs
1142+
)
1143+
end
1144+
1145+
return InstallerMock.new(
1146+
PodsProjectMock.new,
1147+
[
1148+
AggregatedProjectMock.new(:xcconfigs => xcconfigs_map, :base_path => "a/path/")
1149+
],
1150+
:pod_target_installation_results => pod_target_installation_results_map
1151+
)
1152+
end
1153+

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class NewArchitectureHelper
1212

1313
@@folly_compiler_flags = "#{@@shared_flags} -Wno-comma -Wno-shorten-64-to-32"
1414

15-
@@new_arch_cpp_flags = "$(inherited) -DRCT_NEW_ARCH_ENABLED=1 #{@@shared_flags}"
15+
@@new_arch_cpp_flags = " -DRCT_NEW_ARCH_ENABLED=1 #{@@shared_flags}"
1616

1717
@@cplusplus_version = "c++20"
1818

@@ -49,17 +49,10 @@ def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled)
4949
unless is_new_arch_enabled
5050
return
5151
end
52-
ndebug_flag = " -DNDEBUG"
5352
# Add RCT_NEW_ARCH_ENABLED to Target pods xcconfig
5453
installer.aggregate_targets.each do |aggregate_target|
5554
aggregate_target.xcconfigs.each do |config_name, config_file|
56-
config_file.attributes['OTHER_CPLUSPLUSFLAGS'] = @@new_arch_cpp_flags
57-
58-
if config_name == "Release"
59-
config_file.attributes['OTHER_CPLUSPLUSFLAGS'] = config_file.attributes['OTHER_CPLUSPLUSFLAGS'] + ndebug_flag
60-
other_cflags = config_file.attributes['OTHER_CFLAGS'] != nil ? config_file.attributes['OTHER_CFLAGS'] : "$(inherited)"
61-
config_file.attributes['OTHER_CFLAGS'] = other_cflags + ndebug_flag
62-
end
55+
ReactNativePodsUtils.add_flag_to_map_with_inheritance(config_file.attributes, "OTHER_CPLUSPLUSFLAGS", @@new_arch_cpp_flags)
6356

6457
xcconfig_path = aggregate_target.xcconfig_path(config_name)
6558
config_file.save_as(xcconfig_path)
@@ -71,16 +64,7 @@ def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled)
7164
# The React-Core pod may have a suffix added by Cocoapods, so we test whether 'React-Core' is a substring, and do not require exact match
7265
if pod_name.include? 'React-Core'
7366
target_installation_result.native_target.build_configurations.each do |config|
74-
config.build_settings['OTHER_CPLUSPLUSFLAGS'] = @@new_arch_cpp_flags
75-
end
76-
end
77-
78-
target_installation_result.native_target.build_configurations.each do |config|
79-
if config.name == "Release"
80-
current_flags = config.build_settings['OTHER_CPLUSPLUSFLAGS'] != nil ? config.build_settings['OTHER_CPLUSPLUSFLAGS'] : "$(inherited)"
81-
config.build_settings['OTHER_CPLUSPLUSFLAGS'] = current_flags + ndebug_flag
82-
current_cflags = config.build_settings['OTHER_CFLAGS'] != nil ? config.build_settings['OTHER_CFLAGS'] : "$(inherited)"
83-
config.build_settings['OTHER_CFLAGS'] = current_cflags + ndebug_flag
67+
ReactNativePodsUtils.add_flag_to_map_with_inheritance(config.build_settings, "OTHER_CPLUSPLUSFLAGS", @@new_arch_cpp_flags)
8468
end
8569
end
8670
end
@@ -126,7 +110,7 @@ def self.install_modules_dependencies(spec, new_arch_enabled, folly_version)
126110
spec.dependency "glog"
127111

128112
if new_arch_enabled
129-
current_config["OTHER_CPLUSPLUSFLAGS"] = @@new_arch_cpp_flags
113+
ReactNativePodsUtils.add_flag_to_map_with_inheritance(current_config, "OTHER_CPLUSPLUSFLAGS", @@new_arch_cpp_flags)
130114
end
131115

132116
spec.dependency "React-RCTFabric" # This is for Fabric Component

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,4 +613,46 @@ def self.add_search_path_to_result(result, base_path, additional_paths, include_
613613
}
614614
return result
615615
end
616+
617+
def self.add_ndebug_flag_to_pods_in_release(installer)
618+
ndebug_flag = " -DNDEBUG"
619+
620+
installer.aggregate_targets.each do |aggregate_target|
621+
aggregate_target.xcconfigs.each do |config_name, config_file|
622+
is_release = config_name.downcase.include?("release") || config_name.downcase.include?("production")
623+
unless is_release
624+
next
625+
end
626+
self.add_flag_to_map_with_inheritance(config_file.attributes, 'OTHER_CPLUSPLUSFLAGS', ndebug_flag);
627+
self.add_flag_to_map_with_inheritance(config_file.attributes, 'OTHER_CFLAGS', ndebug_flag);
628+
629+
xcconfig_path = aggregate_target.xcconfig_path(config_name)
630+
config_file.save_as(xcconfig_path)
631+
end
632+
end
633+
634+
installer.target_installation_results.pod_target_installation_results.each do |pod_name, target_installation_result|
635+
target_installation_result.native_target.build_configurations.each do |config|
636+
is_release = config.name.downcase.include?("release") || config.name.downcase.include?("production")
637+
unless is_release
638+
next
639+
end
640+
self.add_flag_to_map_with_inheritance(config.build_settings, 'OTHER_CPLUSPLUSFLAGS', ndebug_flag);
641+
self.add_flag_to_map_with_inheritance(config.build_settings, 'OTHER_CFLAGS', ndebug_flag);
642+
end
643+
end
644+
end
645+
646+
def self.add_flag_to_map_with_inheritance(map, field, flag)
647+
if map[field] == nil
648+
map[field] = "$(inherited)" + flag
649+
else
650+
unless map[field].include?(flag)
651+
map[field] = map[field] + flag
652+
end
653+
unless map[field].include?("$(inherited)")
654+
map[field] = "$(inherited) " + map[field]
655+
end
656+
end
657+
end
616658
end

packages/react-native/scripts/react_native_pods.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ def react_native_post_install(
280280
ReactNativePodsUtils.apply_ats_config(installer)
281281
ReactNativePodsUtils.updateOSDeploymentTarget(installer)
282282
ReactNativePodsUtils.set_dynamic_frameworks_flags(installer)
283+
ReactNativePodsUtils.add_ndebug_flag_to_pods_in_release(installer)
283284

284285
NewArchitectureHelper.set_clang_cxx_language_standard_if_needed(installer)
285286
NewArchitectureHelper.modify_flags_for_new_architecture(installer, NewArchitectureHelper.new_arch_enabled)

0 commit comments

Comments
 (0)