diff --git a/packages/react-native/scripts/cocoapods/utils.rb b/packages/react-native/scripts/cocoapods/utils.rb index 0e388d6a7ef5..6376d45e6c34 100644 --- a/packages/react-native/scripts/cocoapods/utils.rb +++ b/packages/react-native/scripts/cocoapods/utils.rb @@ -480,6 +480,57 @@ def self.remove_compiler_flag_from_project(installer, flag, configuration: nil) end end + def self.add_swift_active_compilation_condition_to_project(installer, condition) + projects = self.extract_projects(installer) + + projects.each do |project| + project.build_configurations.each do |config| + self.add_swift_condition_in_config(config, condition) + end + project.save() + end + end + + def self.remove_swift_active_compilation_condition_from_project(installer, condition) + projects = self.extract_projects(installer) + + projects.each do |project| + project.build_configurations.each do |config| + self.remove_swift_condition_in_config(config, condition) + end + project.save() + end + end + + def self.add_swift_condition_in_config(config, condition) + key = "SWIFT_ACTIVE_COMPILATION_CONDITIONS" + current = config.build_settings[key] + + # Normalise to array form so we can dedupe and preserve $(inherited) + # alongside any pre-existing conditions (e.g. DEBUG). + list = case current + when nil then ["$(inherited)"] + when Array then current.dup + when String then current.split(/\s+/).reject(&:empty?) + end + + list << "$(inherited)" unless list.include?("$(inherited)") + list << condition unless list.include?(condition) + + config.build_settings[key] = list + end + + def self.remove_swift_condition_in_config(config, condition) + key = "SWIFT_ACTIVE_COMPILATION_CONDITIONS" + current = config.build_settings[key] + return if current.nil? + + list = current.kind_of?(Array) ? current.dup : current.split(/\s+/).reject(&:empty?) + list.delete(condition) + + config.build_settings[key] = list + end + def self.add_compiler_flag_to_pods(installer, flag, configuration: nil) 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| diff --git a/packages/react-native/scripts/react_native_pods.rb b/packages/react-native/scripts/react_native_pods.rb index 3944c19d1a4e..ce53222ceb81 100644 --- a/packages/react-native/scripts/react_native_pods.rb +++ b/packages/react-native/scripts/react_native_pods.rb @@ -556,8 +556,10 @@ def react_native_post_install( if (ENV['RCT_REMOVE_LEGACY_ARCH'] == '1') ReactNativePodsUtils.add_compiler_flag_to_project(installer, "-DRCT_REMOVE_LEGACY_ARCH=1") + ReactNativePodsUtils.add_swift_active_compilation_condition_to_project(installer, "RCT_REMOVE_LEGACY_ARCH") else ReactNativePodsUtils.remove_compiler_flag_from_project(installer, "-DRCT_REMOVE_LEGACY_ARCH=1") + ReactNativePodsUtils.remove_swift_active_compilation_condition_from_project(installer, "RCT_REMOVE_LEGACY_ARCH") end ReactNativePodsUtils.set_ccache_compiler_and_linker_build_settings(installer, react_native_path, ccache_enabled)