From 421df9ffd58092b1a2dec455a048edb6db1739de Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Sun, 19 Feb 2023 01:55:16 -0800 Subject: [PATCH] Enable NDEBUG in production builds (#36194) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/36194 This change is the iOS equivalent of D43344120 (https://github.com/facebook/react-native/commit/8486e191a170d9eae4d1d628a7539dc9e3d13ea4), but for iOS. ## Changelog: [iOS][Fixed] - Turn on NDEBUG when pods are installed for production. Reviewed By: cortinico Differential Revision: D43388881 fbshipit-source-id: 5c16d3d7b4265e4ee2f265a5f992cffee30f3887 --- .../__tests__/new_architecture-test.rb | 36 +++++++++++++++++++ scripts/cocoapods/new_architecture.rb | 14 ++++++-- scripts/react_native_pods.rb | 2 +- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/scripts/cocoapods/__tests__/new_architecture-test.rb b/scripts/cocoapods/__tests__/new_architecture-test.rb index 35394fd1ae56d7..3b92f46208d285 100644 --- a/scripts/cocoapods/__tests__/new_architecture-test.rb +++ b/scripts/cocoapods/__tests__/new_architecture-test.rb @@ -111,6 +111,42 @@ def test_modifyFlagsForNewArch_whenOnNewArch_updateFlags assert_equal(yoga_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited)") end + def test_modifyFlagsForNewArch_whenOnNewArchAndIsRelease_updateFlags + # Arrange + first_xcconfig = prepare_xcconfig("First") + second_xcconfig = prepare_xcconfig("Second") + react_core_debug_config = prepare_CXX_Flags_build_configuration("Debug") + react_core_release_config = prepare_CXX_Flags_build_configuration("Release") + yoga_debug_config = prepare_CXX_Flags_build_configuration("Debug") + yoga_release_config = prepare_CXX_Flags_build_configuration("Release") + + installer = prepare_installer_for_cpp_flags( + [ first_xcconfig, second_xcconfig ], + { + "React-Core" => [ react_core_debug_config, react_core_release_config ], + "Yoga" => [ yoga_debug_config, yoga_release_config ], + } + ) + # Act + NewArchitectureHelper.modify_flags_for_new_architecture(installer, true, is_release: true) + + # Assert + assert_equal(first_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"], "$(inherited) -DRCT_NEW_ARCH_ENABLED=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -DNDEBUG") + assert_equal(first_xcconfig.attributes["OTHER_CFLAGS"], "$(inherited) -DNDEBUG") + assert_equal(first_xcconfig.save_as_invocation, ["a/path/First.xcconfig"]) + assert_equal(second_xcconfig.attributes["OTHER_CPLUSPLUSFLAGS"], "$(inherited) -DRCT_NEW_ARCH_ENABLED=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -DNDEBUG") + assert_equal(second_xcconfig.attributes["OTHER_CFLAGS"], "$(inherited) -DNDEBUG") + 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 -DNDEBUG") + assert_equal(react_core_debug_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 -DNDEBUG") + assert_equal(react_core_release_config.build_settings["OTHER_CFLAGS"], "$(inherited) -DNDEBUG") + assert_equal(yoga_debug_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited) -DNDEBUG") + assert_equal(yoga_debug_config.build_settings["OTHER_CFLAGS"], "$(inherited) -DNDEBUG") + assert_equal(yoga_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited) -DNDEBUG") + assert_equal(yoga_release_config.build_settings["OTHER_CFLAGS"], "$(inherited) -DNDEBUG") + end + # =================================== # # Test - install Modules Dependencies # # =================================== # diff --git a/scripts/cocoapods/new_architecture.rb b/scripts/cocoapods/new_architecture.rb index 90141cd004c98e..e8c63d1618f53b 100644 --- a/scripts/cocoapods/new_architecture.rb +++ b/scripts/cocoapods/new_architecture.rb @@ -38,15 +38,17 @@ def self.set_clang_cxx_language_standard_if_needed(installer) end end - def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled) + def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled, is_release: false) unless is_new_arch_enabled return end - + ndebug_flag = (is_release ? " -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 + config_file.attributes['OTHER_CPLUSPLUSFLAGS'] = @@new_arch_cpp_flags + ndebug_flag + config_file.attributes['OTHER_CFLAGS'] = "$(inherited)" + ndebug_flag + xcconfig_path = aggregate_target.xcconfig_path(config_name) config_file.save_as(xcconfig_path) end @@ -59,6 +61,12 @@ 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| + current_flags = config.build_settings['OTHER_CPLUSPLUSFLAGS'] != nil ? config.build_settings['OTHER_CPLUSPLUSFLAGS'] : "" + config.build_settings['OTHER_CPLUSPLUSFLAGS'] = current_flags + ndebug_flag + config.build_settings['OTHER_CFLAGS'] = "$(inherited)" + ndebug_flag + end end end diff --git a/scripts/react_native_pods.rb b/scripts/react_native_pods.rb index 89c558384e2c95..cd26f9f495535c 100644 --- a/scripts/react_native_pods.rb +++ b/scripts/react_native_pods.rb @@ -218,7 +218,7 @@ def react_native_post_install(installer, react_native_path = "../node_modules/re NewArchitectureHelper.set_clang_cxx_language_standard_if_needed(installer) is_new_arch_enabled = ENV['RCT_NEW_ARCH_ENABLED'] == "1" - NewArchitectureHelper.modify_flags_for_new_architecture(installer, is_new_arch_enabled) + NewArchitectureHelper.modify_flags_for_new_architecture(installer, is_new_arch_enabled, is_release: ENV['PRODUCTION'] == "1") Pod::UI.puts "Pod install took #{Time.now.to_i - $START_TIME} [s] to run".green end