From a65fe5cf073846ff8a4a3476bcfa0593b834bd79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Thu, 30 Nov 2023 10:06:05 +0100 Subject: [PATCH 1/6] init --- .../scripts/cocoapods/new_architecture.rb | 7 ----- .../react-native/scripts/cocoapods/utils.rb | 26 +++++++++++++++++++ .../react-native/scripts/react_native_pods.rb | 1 + 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/packages/react-native/scripts/cocoapods/new_architecture.rb b/packages/react-native/scripts/cocoapods/new_architecture.rb index 7bf30e43f604e1..5556a0b3542a9a 100644 --- a/packages/react-native/scripts/cocoapods/new_architecture.rb +++ b/packages/react-native/scripts/cocoapods/new_architecture.rb @@ -49,18 +49,11 @@ def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled) unless is_new_arch_enabled return end - ndebug_flag = " -DNDEBUG" # Add RCT_NEW_ARCH_ENABLED to Target pods xcconfig installer.aggregate_targets.each do |aggregate_target| aggregate_target.xcconfigs.each do |config_name, config_file| config_file.attributes['OTHER_CPLUSPLUSFLAGS'] = @@new_arch_cpp_flags - if config_name == "Release" - config_file.attributes['OTHER_CPLUSPLUSFLAGS'] = config_file.attributes['OTHER_CPLUSPLUSFLAGS'] + ndebug_flag - other_cflags = config_file.attributes['OTHER_CFLAGS'] != nil ? config_file.attributes['OTHER_CFLAGS'] : "$(inherited)" - config_file.attributes['OTHER_CFLAGS'] = other_cflags + ndebug_flag - end - xcconfig_path = aggregate_target.xcconfig_path(config_name) config_file.save_as(xcconfig_path) end diff --git a/packages/react-native/scripts/cocoapods/utils.rb b/packages/react-native/scripts/cocoapods/utils.rb index 11cb59a62c528f..8bec643fab783e 100644 --- a/packages/react-native/scripts/cocoapods/utils.rb +++ b/packages/react-native/scripts/cocoapods/utils.rb @@ -613,4 +613,30 @@ def self.add_search_path_to_result(result, base_path, additional_paths, include_ } return result end + + def self.add_ndebug_flag_to_pods_in_release(installer) + ndebug_flag = " -DNDEBUG" + installer.aggregate_targets.each do |aggregate_target| + aggregate_target.xcconfigs.each do |config_name, config_file| + is_release = config_name.downcase.include?("release") || config_name.downcase.include?("production") + unless is_release + next + end + + self.add_to_or_create_attribute(config_file.attributes, 'OTHER_CFLAGS', ndebug_flag); + self.add_to_or_create_attribute(config_file.attributes, 'OTHER_CPLUSPLUSFLAGS', ndebug_flag); + + xcconfig_path = aggregate_target.xcconfig_path(config_name) + config_file.save_as(xcconfig_path) + end + end + end + + def self.add_to_or_create_config_attribute_with_inheritance(attributes, attribute_name, attribute_value) + if attributes[attribute_name] == nil + attributes[attribute_name] = attribute_value + " $(inherited)" + else + attributes[attribute_name] = attributes[attribute_name] + attribute_value + end + end end diff --git a/packages/react-native/scripts/react_native_pods.rb b/packages/react-native/scripts/react_native_pods.rb index a2a6e1d23e7305..1f6591eb1e361b 100644 --- a/packages/react-native/scripts/react_native_pods.rb +++ b/packages/react-native/scripts/react_native_pods.rb @@ -280,6 +280,7 @@ def react_native_post_install( ReactNativePodsUtils.apply_ats_config(installer) ReactNativePodsUtils.updateOSDeploymentTarget(installer) ReactNativePodsUtils.set_dynamic_frameworks_flags(installer) + ReactNativePodsUtils.add_ndebug_flag_to_pods_in_release(installer) NewArchitectureHelper.set_clang_cxx_language_standard_if_needed(installer) NewArchitectureHelper.modify_flags_for_new_architecture(installer, NewArchitectureHelper.new_arch_enabled) From 3dce6f0e5e0de48162186ba317b608ebcfb6bdfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Thu, 30 Nov 2023 10:11:28 +0100 Subject: [PATCH 2/6] problematic tests --- .../__tests__/new_architecture-test.rb | 8 +- .../scripts/cocoapods/__tests__/utils-test.rb | 80 +++++++++++++++++++ .../scripts/cocoapods/new_architecture.rb | 9 --- .../react-native/scripts/cocoapods/utils.rb | 25 +++--- 4 files changed, 96 insertions(+), 26 deletions(-) diff --git a/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb b/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb index fc698ae2166940..7241a5964d2e5c 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb @@ -120,12 +120,12 @@ def test_modifyFlagsForNewArch_whenOnNewArchAndIsRelease_updateFlags assert_equal(second_xcconfig.save_as_invocation, ["a/path/Second.xcconfig"]) 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") assert_nil(react_core_debug_config.build_settings["OTHER_CFLAGS"]) - 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") - assert_equal(react_core_release_config.build_settings["OTHER_CFLAGS"], "$(inherited) -DNDEBUG") + 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") + assert_equal(react_core_release_config.build_settings["OTHER_CFLAGS"], "$(inherited)") assert_equal(yoga_debug_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited)") assert_nil(yoga_debug_config.build_settings["OTHER_CFLAGS"]) - assert_equal(yoga_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited) -DNDEBUG") - assert_equal(yoga_release_config.build_settings["OTHER_CFLAGS"], "$(inherited) -DNDEBUG") + assert_equal(yoga_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited)") + assert_equal(yoga_release_config.build_settings["OTHER_CFLAGS"], "$(inherited)") end # =================================== # diff --git a/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb b/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb index cf9988492dafb7..0216345e16d54f 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb @@ -16,6 +16,7 @@ require_relative "./test_utils/XcodeprojMock.rb" require_relative "./test_utils/XcodebuildMock.rb" require_relative "./test_utils/SpecMock.rb" +require_relative "./test_utils/InstallerMock.rb" class UtilsTests < Test::Unit::TestCase def setup @@ -1008,6 +1009,57 @@ def test_addDependencies_whenSubspecsAndHeaderSearchPathAndVersionWithAdditional assert_equal(spec.to_hash["pod_target_xcconfig"], { "HEADER_SEARCH_PATHS" => expected_search_paths}) end + + def test_add_flag_to_config_field_with_inheritance + # Arrange + first_config = BuildConfigurationMock.new("EmptyConfig") + second_config = BuildConfigurationMock.new("InitializedConfig", { + "OTHER_CPLUSPLUSFLAGS" => "INIT_FLAG" + }) + third_config = BuildConfigurationMock.new("TwiceProcessedConfig"); + test_flag = " -DTEST_FLAG=1" + + # Act + ReactNativePodsUtils.add_flag_to_config_field_with_inheritance(first_config, "OTHER_CPLUSPLUSFLAGS", test_flag) + ReactNativePodsUtils.add_flag_to_config_field_with_inheritance(second_config, "OTHER_CPLUSPLUSFLAGS", test_flag) + ReactNativePodsUtils.add_flag_to_config_field_with_inheritance(third_config, "OTHER_CPLUSPLUSFLAGS", test_flag) + ReactNativePodsUtils.add_flag_to_config_field_with_inheritance(third_config, "OTHER_CPLUSPLUSFLAGS", test_flag) + + # Assert + assert_equal("$(inherited)" + test_flag, first_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) + assert_equal("INIT_FLAG" + test_flag, second_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) + assert_equal("$(inherited)" + test_flag, third_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) + end + + def test_add_ndebug_flag_to_pods_in_release + # Arrange + xcconfig = XCConfigMock.new("Config") + default_debug_config = BuildConfigurationMock.new("Debug") + default_release_config = BuildConfigurationMock.new("Release") + custom_debug_config1 = BuildConfigurationMock.new("CustomDebug") + custom_debug_config2 = BuildConfigurationMock.new("Custom") + custom_release_config1 = BuildConfigurationMock.new("CustomRelease") + custom_release_config2 = BuildConfigurationMock.new("Production") + + installer = prepare_installer_for_cpp_flags( + [ xcconfig ], + { + "Default" => [ default_debug_config, default_release_config ], + "Custom1" => [ custom_debug_config1, custom_release_config1 ], + "Custom2" => [ custom_debug_config2, custom_release_config2 ] + } + ) + # Act + ReactNativePodsUtils.add_ndebug_flag_to_pods_in_release(installer) + + # Assert + assert_equal(nil, default_debug_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) + assert_equal("$(inherited) -DNDEBUG", default_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) + assert_equal(nil, custom_debug_config1.build_settings["OTHER_CPLUSPLUSFLAGS"]) + assert_equal("$(inherited) -DNDEBUG", custom_release_config1.build_settings["OTHER_CPLUSPLUSFLAGS"]) + assert_equal(nil, custom_debug_config2.build_settings["OTHER_CPLUSPLUSFLAGS"]) + assert_equal("$(inherited) -DNDEBUG", custom_release_config2.build_settings["OTHER_CPLUSPLUSFLAGS"]) + end end # ===== # @@ -1050,3 +1102,31 @@ def prepare_Code_Signing_build_configuration(name, param) "CODE_SIGNING_ALLOWED" => param }) end + +def prepare_pod_target_installation_results_mock(name, configs) + target = TargetMock.new(name, configs) + return TargetInstallationResultMock.new(target, target) +end + +def prepare_installer_for_cpp_flags(xcconfigs, build_configs) + xcconfigs_map = {} + xcconfigs.each do |config| + xcconfigs_map[config.name.to_s] = config + end + + pod_target_installation_results_map = {} + build_configs.each do |name, build_configs| + pod_target_installation_results_map[name.to_s] = prepare_pod_target_installation_results_mock( + name.to_s, build_configs + ) + end + + return InstallerMock.new( + PodsProjectMock.new, + [ + AggregatedProjectMock.new(:xcconfigs => xcconfigs_map, :base_path => "a/path/") + ], + :pod_target_installation_results => pod_target_installation_results_map + ) +end + diff --git a/packages/react-native/scripts/cocoapods/new_architecture.rb b/packages/react-native/scripts/cocoapods/new_architecture.rb index 5556a0b3542a9a..e9a7a51c8a4ead 100644 --- a/packages/react-native/scripts/cocoapods/new_architecture.rb +++ b/packages/react-native/scripts/cocoapods/new_architecture.rb @@ -67,15 +67,6 @@ def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled) config.build_settings['OTHER_CPLUSPLUSFLAGS'] = @@new_arch_cpp_flags end end - - target_installation_result.native_target.build_configurations.each do |config| - if config.name == "Release" - current_flags = config.build_settings['OTHER_CPLUSPLUSFLAGS'] != nil ? config.build_settings['OTHER_CPLUSPLUSFLAGS'] : "$(inherited)" - config.build_settings['OTHER_CPLUSPLUSFLAGS'] = current_flags + ndebug_flag - current_cflags = config.build_settings['OTHER_CFLAGS'] != nil ? config.build_settings['OTHER_CFLAGS'] : "$(inherited)" - config.build_settings['OTHER_CFLAGS'] = current_cflags + ndebug_flag - end - end end end diff --git a/packages/react-native/scripts/cocoapods/utils.rb b/packages/react-native/scripts/cocoapods/utils.rb index 8bec643fab783e..64c3c51a6e8f74 100644 --- a/packages/react-native/scripts/cocoapods/utils.rb +++ b/packages/react-native/scripts/cocoapods/utils.rb @@ -616,27 +616,26 @@ def self.add_search_path_to_result(result, base_path, additional_paths, include_ def self.add_ndebug_flag_to_pods_in_release(installer) ndebug_flag = " -DNDEBUG" - installer.aggregate_targets.each do |aggregate_target| - aggregate_target.xcconfigs.each do |config_name, config_file| - is_release = config_name.downcase.include?("release") || config_name.downcase.include?("production") + installer.target_installation_results.pod_target_installation_results.each do |pod_name, target_installation_result| + target_installation_result.native_target.build_configurations.each do |config| + is_release = config.name.downcase.include?("release") || config.name.downcase.include?("production") unless is_release next end - - self.add_to_or_create_attribute(config_file.attributes, 'OTHER_CFLAGS', ndebug_flag); - self.add_to_or_create_attribute(config_file.attributes, 'OTHER_CPLUSPLUSFLAGS', ndebug_flag); - - xcconfig_path = aggregate_target.xcconfig_path(config_name) - config_file.save_as(xcconfig_path) + self.add_flag_to_config_field_with_inheritance(config, 'OTHER_CFLAGS', ndebug_flag); + self.add_flag_to_config_field_with_inheritance(config, 'OTHER_CPLUSPLUSFLAGS', ndebug_flag); end end end - def self.add_to_or_create_config_attribute_with_inheritance(attributes, attribute_name, attribute_value) - if attributes[attribute_name] == nil - attributes[attribute_name] = attribute_value + " $(inherited)" + def self.add_flag_to_config_field_with_inheritance(config, field, flag) + if config.build_settings[field] == nil + config.build_settings[field] = "$(inherited)" + flag else - attributes[attribute_name] = attributes[attribute_name] + attribute_value + # add only if it's not included already + unless config.build_settings[field].include?(flag) + config.build_settings[field] = config.build_settings[field] + flag + end end end end From 45df9bd37c783c382d665a0e5dec1f60530e2b94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Thu, 30 Nov 2023 17:38:36 +0100 Subject: [PATCH 3/6] fix --- .../__tests__/new_architecture-test.rb | 6 +-- .../scripts/cocoapods/__tests__/utils-test.rb | 43 ++++++++++++++----- .../scripts/cocoapods/new_architecture.rb | 20 +++++++-- .../react-native/scripts/cocoapods/utils.rb | 30 +++++++++---- 4 files changed, 73 insertions(+), 26 deletions(-) diff --git a/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb b/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb index 7241a5964d2e5c..f8a89befe582a9 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb @@ -121,11 +121,11 @@ def test_modifyFlagsForNewArch_whenOnNewArchAndIsRelease_updateFlags 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") assert_nil(react_core_debug_config.build_settings["OTHER_CFLAGS"]) 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") - assert_equal(react_core_release_config.build_settings["OTHER_CFLAGS"], "$(inherited)") + assert_nil(react_core_release_config.build_settings["OTHER_CFLAGS"]) assert_equal(yoga_debug_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited)") assert_nil(yoga_debug_config.build_settings["OTHER_CFLAGS"]) assert_equal(yoga_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited)") - assert_equal(yoga_release_config.build_settings["OTHER_CFLAGS"], "$(inherited)") + assert_nil(yoga_release_config.build_settings["OTHER_CFLAGS"], nil) end # =================================== # @@ -142,7 +142,7 @@ def test_installModulesDependencies_whenNewArchEnabledAndNewArchAndNoSearchPaths assert_equal(spec.compiler_flags, NewArchitectureHelper.folly_compiler_flags) assert_equal(spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"], "\"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/Headers/Private/Yoga\" \"$(PODS_ROOT)/DoubleConversion\" \"$(PODS_ROOT)/fmt/include\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-graphics/React_graphics.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-graphics/React_graphics.framework/Headers/react/renderer/graphics/platform/ios\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric/React_Fabric.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric/React_Fabric.framework/Headers/react/renderer/components/view/platform/cxx\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-FabricImage/React_FabricImage.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/ReactCommon/ReactCommon.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/ReactCommon/ReactCommon.framework/Headers/react/nativemodule/core\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-NativeModulesApple/React_NativeModulesApple.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric/RCTFabric.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-utils/React_utils.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-debug/React_debug.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-ImageManager/React_ImageManager.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-rendererdebug/React_rendererdebug.framework/Headers\"") assert_equal(spec.pod_target_xcconfig["CLANG_CXX_LANGUAGE_STANDARD"], "c++20") - assert_equal(spec.pod_target_xcconfig["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") + assert_equal(spec.pod_target_xcconfig["OTHER_CPLUSPLUSFLAGS"], " -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") assert_equal( spec.dependencies, [ diff --git a/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb b/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb index 0216345e16d54f..fe63a68b5a76c3 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb @@ -1010,25 +1010,46 @@ def test_addDependencies_whenSubspecsAndHeaderSearchPathAndVersionWithAdditional "HEADER_SEARCH_PATHS" => expected_search_paths}) end - def test_add_flag_to_config_field_with_inheritance + def test_add_flag_to_map_with_inheritance_whenUsedWithBuildConfigBuildSettings # Arrange - first_config = BuildConfigurationMock.new("EmptyConfig") - second_config = BuildConfigurationMock.new("InitializedConfig", { + empty_config = BuildConfigurationMock.new("EmptyConfig") + initialized_config = BuildConfigurationMock.new("InitializedConfig", { "OTHER_CPLUSPLUSFLAGS" => "INIT_FLAG" }) - third_config = BuildConfigurationMock.new("TwiceProcessedConfig"); + twiceProcessed_config = BuildConfigurationMock.new("TwiceProcessedConfig"); test_flag = " -DTEST_FLAG=1" # Act - ReactNativePodsUtils.add_flag_to_config_field_with_inheritance(first_config, "OTHER_CPLUSPLUSFLAGS", test_flag) - ReactNativePodsUtils.add_flag_to_config_field_with_inheritance(second_config, "OTHER_CPLUSPLUSFLAGS", test_flag) - ReactNativePodsUtils.add_flag_to_config_field_with_inheritance(third_config, "OTHER_CPLUSPLUSFLAGS", test_flag) - ReactNativePodsUtils.add_flag_to_config_field_with_inheritance(third_config, "OTHER_CPLUSPLUSFLAGS", test_flag) + ReactNativePodsUtils.add_flag_to_map_with_inheritance(empty_config.build_settings, "OTHER_CPLUSPLUSFLAGS", test_flag) + ReactNativePodsUtils.add_flag_to_map_with_inheritance(initialized_config.build_settings, "OTHER_CPLUSPLUSFLAGS", test_flag) + ReactNativePodsUtils.add_flag_to_map_with_inheritance(twiceProcessed_config.build_settings, "OTHER_CPLUSPLUSFLAGS", test_flag) + ReactNativePodsUtils.add_flag_to_map_with_inheritance(twiceProcessed_config.build_settings, "OTHER_CPLUSPLUSFLAGS", test_flag) # Assert - assert_equal("$(inherited)" + test_flag, first_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) - assert_equal("INIT_FLAG" + test_flag, second_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) - assert_equal("$(inherited)" + test_flag, third_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) + assert_equal("$(inherited)" + test_flag, empty_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) + assert_equal("INIT_FLAG" + test_flag, initialized_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) + assert_equal("$(inherited)" + test_flag, twiceProcessed_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) + end + + def test_add_flag_to_map_with_inheritance_whenUsedWithXCConfigAttributes + # Arrange + empty_xcconfig = XCConfigMock.new("EmptyConfig") + initialized_xcconfig = XCConfigMock.new("InitializedConfig", attributes: { + "OTHER_CPLUSPLUSFLAGS" => "INIT_FLAG" + }) + twiceProcessed_xcconfig = XCConfigMock.new("TwiceProcessedConfig"); + test_flag = " -DTEST_FLAG=1" + + # Act + ReactNativePodsUtils.add_flag_to_map_with_inheritance(empty_xcconfig.attributes, "OTHER_CPLUSPLUSFLAGS", test_flag) + ReactNativePodsUtils.add_flag_to_map_with_inheritance(initialized_xcconfig.attributes, "OTHER_CPLUSPLUSFLAGS", test_flag) + ReactNativePodsUtils.add_flag_to_map_with_inheritance(twiceProcessed_xcconfig.attributes, "OTHER_CPLUSPLUSFLAGS", test_flag) + ReactNativePodsUtils.add_flag_to_map_with_inheritance(twiceProcessed_xcconfig.attributes, "OTHER_CPLUSPLUSFLAGS", test_flag) + + # Assert + assert_equal("$(inherited)" + test_flag, empty_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"]) + assert_equal("INIT_FLAG" + test_flag, initialized_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"]) + assert_equal("$(inherited)" + test_flag, twiceProcessed_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"]) end def test_add_ndebug_flag_to_pods_in_release diff --git a/packages/react-native/scripts/cocoapods/new_architecture.rb b/packages/react-native/scripts/cocoapods/new_architecture.rb index e9a7a51c8a4ead..5d664cf837e296 100644 --- a/packages/react-native/scripts/cocoapods/new_architecture.rb +++ b/packages/react-native/scripts/cocoapods/new_architecture.rb @@ -12,7 +12,7 @@ class NewArchitectureHelper @@folly_compiler_flags = "#{@@shared_flags} -Wno-comma -Wno-shorten-64-to-32" - @@new_arch_cpp_flags = "$(inherited) -DRCT_NEW_ARCH_ENABLED=1 #{@@shared_flags}" + @@new_arch_cpp_flags = " -DRCT_NEW_ARCH_ENABLED=1 #{@@shared_flags}" @@cplusplus_version = "c++20" @@ -52,7 +52,11 @@ def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled) # Add RCT_NEW_ARCH_ENABLED to Target pods xcconfig installer.aggregate_targets.each do |aggregate_target| aggregate_target.xcconfigs.each do |config_name, config_file| - config_file.attributes['OTHER_CPLUSPLUSFLAGS'] = @@new_arch_cpp_flags + if config_file.attributes['OTHER_CPLUSPLUSFLAGS'] == nil + config_file.attributes['OTHER_CPLUSPLUSFLAGS'] = @@new_arch_cpp_flags + else + config_file.attributes['OTHER_CPLUSPLUSFLAGS'] = config_file.attributes['OTHER_CPLUSPLUSFLAGS'] + @@new_arch_cpp_flags + end xcconfig_path = aggregate_target.xcconfig_path(config_name) config_file.save_as(xcconfig_path) @@ -64,7 +68,11 @@ def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled) # 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 if pod_name.include? 'React-Core' target_installation_result.native_target.build_configurations.each do |config| - config.build_settings['OTHER_CPLUSPLUSFLAGS'] = @@new_arch_cpp_flags + if config.build_settings['OTHER_CPLUSPLUSFLAGS'] == nil + config.build_settings['OTHER_CPLUSPLUSFLAGS'] = @@new_arch_cpp_flags + else + config.build_settings['OTHER_CPLUSPLUSFLAGS'] = config.build_settings['OTHER_CPLUSPLUSFLAGS'] + @@new_arch_cpp_flags + end end end end @@ -110,7 +118,11 @@ def self.install_modules_dependencies(spec, new_arch_enabled, folly_version) spec.dependency "glog" if new_arch_enabled - current_config["OTHER_CPLUSPLUSFLAGS"] = @@new_arch_cpp_flags + if current_config["OTHER_CPLUSPLUSFLAGS"] == nil + current_config["OTHER_CPLUSPLUSFLAGS"] = @@new_arch_cpp_flags + else + current_config["OTHER_CPLUSPLUSFLAGS"] = current_config["OTHER_CPLUSPLUSFLAGS"] + @@new_arch_cpp_flags + end end spec.dependency "React-RCTFabric" # This is for Fabric Component diff --git a/packages/react-native/scripts/cocoapods/utils.rb b/packages/react-native/scripts/cocoapods/utils.rb index 64c3c51a6e8f74..229fec6955c8a1 100644 --- a/packages/react-native/scripts/cocoapods/utils.rb +++ b/packages/react-native/scripts/cocoapods/utils.rb @@ -616,25 +616,39 @@ def self.add_search_path_to_result(result, base_path, additional_paths, include_ def self.add_ndebug_flag_to_pods_in_release(installer) ndebug_flag = " -DNDEBUG" + + installer.aggregate_targets.each do |aggregate_target| + aggregate_target.xcconfigs.each do |config_name, config_file| + is_release = config_name.downcase.include?("release") || config_name.downcase.include?("production") + unless is_release + next + end + self.add_flag_to_map_with_inheritance(config_file.attributes, 'OTHER_CPLUSPLUSFLAGS', ndebug_flag); + self.add_flag_to_map_with_inheritance(config_file.attributes, 'OTHER_CFLAGS', ndebug_flag); + + xcconfig_path = aggregate_target.xcconfig_path(config_name) + config_file.save_as(xcconfig_path) + end + end + installer.target_installation_results.pod_target_installation_results.each do |pod_name, target_installation_result| target_installation_result.native_target.build_configurations.each do |config| is_release = config.name.downcase.include?("release") || config.name.downcase.include?("production") unless is_release next end - self.add_flag_to_config_field_with_inheritance(config, 'OTHER_CFLAGS', ndebug_flag); - self.add_flag_to_config_field_with_inheritance(config, 'OTHER_CPLUSPLUSFLAGS', ndebug_flag); + self.add_flag_to_map_with_inheritance(config.build_settings, 'OTHER_CPLUSPLUSFLAGS', ndebug_flag); + self.add_flag_to_map_with_inheritance(config.build_settings, 'OTHER_CFLAGS', ndebug_flag); end end end - def self.add_flag_to_config_field_with_inheritance(config, field, flag) - if config.build_settings[field] == nil - config.build_settings[field] = "$(inherited)" + flag + def self.add_flag_to_map_with_inheritance(map, field, flag) + if map[field] == nil + map[field] = "$(inherited)" + flag else - # add only if it's not included already - unless config.build_settings[field].include?(flag) - config.build_settings[field] = config.build_settings[field] + flag + unless map[field].include?(flag) + map[field] = map[field] + flag end end end From b2ad71a0ccef6a4e81a321c744069cf32bfb3dd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Thu, 30 Nov 2023 17:40:50 +0100 Subject: [PATCH 4/6] remove extra argument --- .../scripts/cocoapods/__tests__/new_architecture-test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb b/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb index f8a89befe582a9..34732b5c91c95c 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb @@ -125,7 +125,7 @@ def test_modifyFlagsForNewArch_whenOnNewArchAndIsRelease_updateFlags assert_equal(yoga_debug_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited)") assert_nil(yoga_debug_config.build_settings["OTHER_CFLAGS"]) assert_equal(yoga_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited)") - assert_nil(yoga_release_config.build_settings["OTHER_CFLAGS"], nil) + assert_nil(yoga_release_config.build_settings["OTHER_CFLAGS"]) end # =================================== # From 84506fe20811575c3b5c9609c34a00bfb41d8c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Thu, 30 Nov 2023 18:03:48 +0100 Subject: [PATCH 5/6] fail-safe React-Core --- packages/react-native/scripts/cocoapods/new_architecture.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/react-native/scripts/cocoapods/new_architecture.rb b/packages/react-native/scripts/cocoapods/new_architecture.rb index 5d664cf837e296..f56612b3ea1fc3 100644 --- a/packages/react-native/scripts/cocoapods/new_architecture.rb +++ b/packages/react-native/scripts/cocoapods/new_architecture.rb @@ -73,6 +73,9 @@ def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled) else config.build_settings['OTHER_CPLUSPLUSFLAGS'] = config.build_settings['OTHER_CPLUSPLUSFLAGS'] + @@new_arch_cpp_flags end + unless config.build_settings['OTHER_CPLUSPLUSFLAGS'].include? '$(inherited)' + config.build_settings['OTHER_CPLUSPLUSFLAGS'] = '$(inherited) ' + config.build_settings['OTHER_CPLUSPLUSFLAGS'] + end end end end From 0a7a0fef71788214fce99ae914e04a255e0c4861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20=C5=BBelawski?= Date: Fri, 1 Dec 2023 11:11:36 +0100 Subject: [PATCH 6/6] inheritance --- .../__tests__/new_architecture-test.rb | 2 +- .../scripts/cocoapods/__tests__/utils-test.rb | 4 ++-- .../scripts/cocoapods/new_architecture.rb | 21 +++---------------- .../react-native/scripts/cocoapods/utils.rb | 3 +++ 4 files changed, 9 insertions(+), 21 deletions(-) diff --git a/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb b/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb index 34732b5c91c95c..0b5f77d9ee4667 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/new_architecture-test.rb @@ -142,7 +142,7 @@ def test_installModulesDependencies_whenNewArchEnabledAndNewArchAndNoSearchPaths assert_equal(spec.compiler_flags, NewArchitectureHelper.folly_compiler_flags) assert_equal(spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"], "\"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/Headers/Private/Yoga\" \"$(PODS_ROOT)/DoubleConversion\" \"$(PODS_ROOT)/fmt/include\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-graphics/React_graphics.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-graphics/React_graphics.framework/Headers/react/renderer/graphics/platform/ios\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric/React_Fabric.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-Fabric/React_Fabric.framework/Headers/react/renderer/components/view/platform/cxx\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-FabricImage/React_FabricImage.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/ReactCommon/ReactCommon.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/ReactCommon/ReactCommon.framework/Headers/react/nativemodule/core\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-NativeModulesApple/React_NativeModulesApple.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-RCTFabric/RCTFabric.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-utils/React_utils.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-debug/React_debug.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-ImageManager/React_ImageManager.framework/Headers\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-rendererdebug/React_rendererdebug.framework/Headers\"") assert_equal(spec.pod_target_xcconfig["CLANG_CXX_LANGUAGE_STANDARD"], "c++20") - assert_equal(spec.pod_target_xcconfig["OTHER_CPLUSPLUSFLAGS"], " -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") + assert_equal(spec.pod_target_xcconfig["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") assert_equal( spec.dependencies, [ diff --git a/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb b/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb index fe63a68b5a76c3..cc6963c73a4fc0 100644 --- a/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb +++ b/packages/react-native/scripts/cocoapods/__tests__/utils-test.rb @@ -1027,7 +1027,7 @@ def test_add_flag_to_map_with_inheritance_whenUsedWithBuildConfigBuildSettings # Assert assert_equal("$(inherited)" + test_flag, empty_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) - assert_equal("INIT_FLAG" + test_flag, initialized_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) + assert_equal("$(inherited) INIT_FLAG" + test_flag, initialized_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) assert_equal("$(inherited)" + test_flag, twiceProcessed_config.build_settings["OTHER_CPLUSPLUSFLAGS"]) end @@ -1048,7 +1048,7 @@ def test_add_flag_to_map_with_inheritance_whenUsedWithXCConfigAttributes # Assert assert_equal("$(inherited)" + test_flag, empty_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"]) - assert_equal("INIT_FLAG" + test_flag, initialized_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"]) + assert_equal("$(inherited) INIT_FLAG" + test_flag, initialized_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"]) assert_equal("$(inherited)" + test_flag, twiceProcessed_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"]) end diff --git a/packages/react-native/scripts/cocoapods/new_architecture.rb b/packages/react-native/scripts/cocoapods/new_architecture.rb index f56612b3ea1fc3..0d2aa46333d3f1 100644 --- a/packages/react-native/scripts/cocoapods/new_architecture.rb +++ b/packages/react-native/scripts/cocoapods/new_architecture.rb @@ -52,11 +52,7 @@ def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled) # Add RCT_NEW_ARCH_ENABLED to Target pods xcconfig installer.aggregate_targets.each do |aggregate_target| aggregate_target.xcconfigs.each do |config_name, config_file| - if config_file.attributes['OTHER_CPLUSPLUSFLAGS'] == nil - config_file.attributes['OTHER_CPLUSPLUSFLAGS'] = @@new_arch_cpp_flags - else - config_file.attributes['OTHER_CPLUSPLUSFLAGS'] = config_file.attributes['OTHER_CPLUSPLUSFLAGS'] + @@new_arch_cpp_flags - end + ReactNativePodsUtils.add_flag_to_map_with_inheritance(config_file.attributes, "OTHER_CPLUSPLUSFLAGS", @@new_arch_cpp_flags) xcconfig_path = aggregate_target.xcconfig_path(config_name) config_file.save_as(xcconfig_path) @@ -68,14 +64,7 @@ def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled) # 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 if pod_name.include? 'React-Core' target_installation_result.native_target.build_configurations.each do |config| - if config.build_settings['OTHER_CPLUSPLUSFLAGS'] == nil - config.build_settings['OTHER_CPLUSPLUSFLAGS'] = @@new_arch_cpp_flags - else - config.build_settings['OTHER_CPLUSPLUSFLAGS'] = config.build_settings['OTHER_CPLUSPLUSFLAGS'] + @@new_arch_cpp_flags - end - unless config.build_settings['OTHER_CPLUSPLUSFLAGS'].include? '$(inherited)' - config.build_settings['OTHER_CPLUSPLUSFLAGS'] = '$(inherited) ' + config.build_settings['OTHER_CPLUSPLUSFLAGS'] - end + ReactNativePodsUtils.add_flag_to_map_with_inheritance(config.build_settings, "OTHER_CPLUSPLUSFLAGS", @@new_arch_cpp_flags) end end end @@ -121,11 +110,7 @@ def self.install_modules_dependencies(spec, new_arch_enabled, folly_version) spec.dependency "glog" if new_arch_enabled - if current_config["OTHER_CPLUSPLUSFLAGS"] == nil - current_config["OTHER_CPLUSPLUSFLAGS"] = @@new_arch_cpp_flags - else - current_config["OTHER_CPLUSPLUSFLAGS"] = current_config["OTHER_CPLUSPLUSFLAGS"] + @@new_arch_cpp_flags - end + ReactNativePodsUtils.add_flag_to_map_with_inheritance(current_config, "OTHER_CPLUSPLUSFLAGS", @@new_arch_cpp_flags) end spec.dependency "React-RCTFabric" # This is for Fabric Component diff --git a/packages/react-native/scripts/cocoapods/utils.rb b/packages/react-native/scripts/cocoapods/utils.rb index 229fec6955c8a1..ace3848aa849d7 100644 --- a/packages/react-native/scripts/cocoapods/utils.rb +++ b/packages/react-native/scripts/cocoapods/utils.rb @@ -650,6 +650,9 @@ def self.add_flag_to_map_with_inheritance(map, field, flag) unless map[field].include?(flag) map[field] = map[field] + flag end + unless map[field].include?("$(inherited)") + map[field] = "$(inherited) " + map[field] + end end end end