Skip to content

Commit

Permalink
Update utils.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen authored Dec 27, 2023
1 parent 7c680b2 commit e79ead9
Showing 1 changed file with 47 additions and 66 deletions.
113 changes: 47 additions & 66 deletions packages/react-native/scripts/cocoapods/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def self.get_default_flags
flags = {
:fabric_enabled => false,
:hermes_enabled => true,
:flipper_configuration => FlipperConfiguration.disabled
}

if ENV['RCT_NEW_ARCH_ENABLED'] == '1'
Expand Down Expand Up @@ -60,6 +61,31 @@ def self.turn_off_resource_bundle_react_core(installer)
end
end

def self.exclude_i386_architecture_while_using_hermes(installer)
is_using_hermes = self.has_pod(installer, 'hermes-engine')

if is_using_hermes
key = "EXCLUDED_ARCHS[sdk=iphonesimulator*]"

projects = self.extract_projects(installer)

projects.each do |project|
project.build_configurations.each do |config|
current_setting = config.build_settings[key] || ""

excluded_archs_includes_I386 = current_setting.include?("i386")

if !excluded_archs_includes_I386
# Hermes does not support 'i386' architecture
config.build_settings[key] = "#{current_setting} i386".strip
end
end

project.save()
end
end
end

def self.set_use_hermes_build_setting(installer, hermes_enabled)
Pod::UI.puts("Setting USE_HERMES build settings")
projects = self.extract_projects(installer)
Expand Down Expand Up @@ -92,10 +118,14 @@ def self.set_ccache_compiler_and_linker_build_settings(installer, react_native_p
ccache_path = `command -v ccache`.strip
ccache_available = !ccache_path.empty?

message_prefix = ccache_available ? "⚡️".yellow + "Ccache detected (found at %s): " % [ccache_path.italic] : "Unable to locate ccache: "
message_prefix = "[Ccache]"

if ccache_available
Pod::UI.puts("#{message_prefix}: Ccache found at #{ccache_path}")
end

if ccache_available and ccache_enabled
Pod::UI.puts(message_prefix + "Setting CC, LD, CXX & LDPLUSPLUS build settings")
Pod::UI.puts("#{message_prefix}: Setting CC, LD, CXX & LDPLUSPLUS build settings")
# Using scripts wrapping the ccache executable, to allow injection of configurations
ccache_clang_sh = File.join("$(REACT_NATIVE_PATH)", 'scripts', 'xcode', 'ccache-clang.sh')
ccache_clangpp_sh = File.join("$(REACT_NATIVE_PATH)", 'scripts', 'xcode', 'ccache-clang++.sh')
Expand All @@ -112,9 +142,9 @@ def self.set_ccache_compiler_and_linker_build_settings(installer, react_native_p
project.save()
end
elsif ccache_available and !ccache_enabled
Pod::UI.puts(message_prefix + "Pass %s to %s in your Podfile or set environment variable %s to increase the speed of subsequent builds" % [":ccache_enabled => true".italic, "react_native_post_install".italic, "USE_CCACHE=1".italic])
Pod::UI.puts("#{message_prefix}: Pass #{":ccache_enabled => true".italic} to #{"react_native_post_install".italic} in your Podfile or set environment variable #{"USE_CCACHE=1".italic} to increase the speed of subsequent builds")
elsif !ccache_available and ccache_enabled
Pod::UI.warn(message_prefix + "Install ccache or ensure your neither passing %s nor setting environment variable %s" % [":ccache_enabled => true", "USE_CCACHE=1"])
Pod::UI.warn("#{message_prefix}: Install ccache or ensure your neither passing ':ccache_enabled => true' nor setting environment variable 'USE_CCACHE=1'")
end
end

Expand Down Expand Up @@ -171,14 +201,23 @@ def self.apply_xcode_15_patch(installer, xcodebuild_manager: Xcodebuild)
if self.is_using_xcode15_or_greater(:xcodebuild_manager => xcodebuild_manager)
self.add_value_to_setting_if_missing(config, other_ld_flags_key, xcode15_compatibility_flags)
else
self.remove_value_from_setting_if_present(config, other_ld_flags_key, xcode15_compatibility_flags)
self.remove_value_to_setting_if_present(config, other_ld_flags_key, xcode15_compatibility_flags)
end
end
project.save()
end

end

def self.apply_flags_for_fabric(installer, fabric_enabled: false)
fabric_flag = "-DRN_FABRIC_ENABLED"
if fabric_enabled
self.add_compiler_flag_to_project(installer, fabric_flag)
else
self.remove_compiler_flag_from_project(installer, fabric_flag)
end
end

private

def self.add_build_settings_to_pod(installer, settings_name, settings_value, target_pod_name, configuration)
Expand Down Expand Up @@ -341,22 +380,6 @@ def self.updateOSDeploymentTarget(installer)
end
end

def self.set_dynamic_frameworks_flags(installer)
installer.target_installation_results.pod_target_installation_results.each do |pod_name, target_installation_result|

# Set "RCT_DYNAMIC_FRAMEWORKS=1" if pod are installed with USE_FRAMEWORKS=dynamic
# This helps with backward compatibility.
if pod_name == 'React-RCTFabric' && ENV['USE_FRAMEWORKS'] == 'dynamic'
Pod::UI.puts "Setting -DRCT_DYNAMIC_FRAMEWORKS=1 to React-RCTFabric".green
rct_dynamic_framework_flag = " -DRCT_DYNAMIC_FRAMEWORKS=1"
target_installation_result.native_target.build_configurations.each do |config|
prev_build_settings = config.build_settings['OTHER_CPLUSPLUSFLAGS'] != nil ? config.build_settings['OTHER_CPLUSPLUSFLAGS'] : "$(inherithed)"
config.build_settings['OTHER_CPLUSPLUSFLAGS'] = prev_build_settings + rct_dynamic_framework_flag
end
end
end
end

# ========= #
# Utilities #
# ========= #
Expand Down Expand Up @@ -387,7 +410,7 @@ def self.add_value_to_setting_if_missing(config, setting_name, value)
end
end

def self.remove_value_from_setting_if_present(config, setting_name, value)
def self.remove_value_to_setting_if_present(config, setting_name, value)
old_config = config.build_settings[setting_name]
if old_config.is_a?(Array)
old_config = old_config.join(" ")
Expand Down Expand Up @@ -573,8 +596,7 @@ def self.update_ats_in_plist(plistPaths, parent)
"NSAppTransportSecurity" => ats_configs
}
else
plist["NSAppTransportSecurity"] ||= {}
plist["NSAppTransportSecurity"] = plist["NSAppTransportSecurity"].merge(ats_configs)
plist["NSAppTransportSecurity"] = ats_configs
end
Xcodeproj::Plist.write_to_path(plist, fullPlistPath)
end
Expand Down Expand Up @@ -632,6 +654,7 @@ def self.react_native_pods
"fmt",
"glog",
"hermes-engine",
"libevent",
"React-hermes",
]
end
Expand All @@ -646,46 +669,4 @@ 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_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_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_map_with_inheritance(map, field, flag)
if map[field] == nil
map[field] = "$(inherited)" + flag
else
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

0 comments on commit e79ead9

Please sign in to comment.