1# Copyright (c) Meta Platforms, Inc. and affiliates.
2#
3# This source code is licensed under the MIT license found in the
4# LICENSE file in the root directory of this source tree.
5
6require_relative "./helpers.rb"
7
8# Utilities class for React Native Cocoapods
9class ReactNativePodsUtils
10    def self.warn_if_not_on_arm64
11        if SysctlChecker.new().call_sysctl_arm64() == 1 && !Environment.new().ruby_platform().include?('arm64')
12            Pod::UI.warn 'Do not use "pod install" from inside Rosetta2 (x86_64 emulation on arm64).'
13            Pod::UI.warn ' - Emulated x86_64 is slower than native arm64'
14            Pod::UI.warn ' - May result in mixed architectures in rubygems (eg: ffi_c.bundle files may be x86_64 with an arm64 interpreter)'
15            Pod::UI.warn 'Run "env /usr/bin/arch -arm64 /bin/bash --login" then try again.'
16        end
17    end
18
19    def self.get_default_flags
20        flags = {
21            :fabric_enabled => false,
22            :hermes_enabled => true,
23            :flipper_configuration => FlipperConfiguration.disabled
24        }
25
26        if ENV['RCT_NEW_ARCH_ENABLED'] == '1'
27            flags[:fabric_enabled] = true
28            flags[:hermes_enabled] = true
29        end
30
31        if ENV['USE_HERMES'] == '0'
32            flags[:hermes_enabled] = false
33        end
34
35        return flags
36    end
37
38    def self.has_pod(installer, name)
39        installer.pods_project.pod_group(name) != nil
40    end
41
42    def self.turn_off_resource_bundle_react_core(installer)
43        # this is needed for Xcode 14, see more details here https://github.com/facebook/react-native/issues/34673
44        # we should be able to remove this once CocoaPods catches up to it, see more details here https://github.com/CocoaPods/CocoaPods/issues/11402
45        installer.target_installation_results.pod_target_installation_results.each do |pod_name, target_installation_result|
46            if pod_name.to_s == 'React-Core'
47                target_installation_result.resource_bundle_targets.each do |resource_bundle_target|
48                    resource_bundle_target.build_configurations.each do |config|
49                        config.build_settings['CODE_SIGNING_ALLOWED'] = 'NO'
50                    end
51                end
52            end
53        end
54    end
55
56    def self.exclude_i386_architecture_while_using_hermes(installer)
57        projects = installer.aggregate_targets
58            .map{ |t| t.user_project }
59            .uniq{ |p| p.path }
60            .push(installer.pods_project)
61
62
63        # Hermes does not support `i386` architecture
64        excluded_archs_default = ReactNativePodsUtils.has_pod(installer, 'hermes-engine') ? "i386" : ""
65
66        projects.each do |project|
67            project.build_configurations.each do |config|
68                config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = excluded_archs_default
69            end
70
71            project.save()
72        end
73    end
74
75    def self.set_node_modules_user_settings(installer, react_native_path)
76        Pod::UI.puts("Setting REACT_NATIVE build settings")
77        projects = installer.aggregate_targets
78            .map{ |t| t.user_project }
79            .uniq{ |p| p.path }
80            .push(installer.pods_project)
81
82        projects.each do |project|
83            project.build_configurations.each do |config|
84                config.build_settings["REACT_NATIVE_PATH"] = File.join("${PODS_ROOT}", "..", react_native_path)
85            end
86
87            project.save()
88        end
89    end
90
91    def self.fix_library_search_paths(installer)
92        projects = installer.aggregate_targets
93            .map{ |t| t.user_project }
94            .uniq{ |p| p.path }
95            .push(installer.pods_project)
96
97        projects.each do |project|
98            project.build_configurations.each do |config|
99                ReactNativePodsUtils.fix_library_search_path(config)
100            end
101            project.native_targets.each do |target|
102                target.build_configurations.each do |config|
103                    ReactNativePodsUtils.fix_library_search_path(config)
104                end
105            end
106            project.save()
107        end
108    end
109
110    def self.fix_react_bridging_header_search_paths(installer)
111        installer.target_installation_results.pod_target_installation_results
112            .each do |pod_name, target_installation_result|
113                target_installation_result.native_target.build_configurations.each do |config|
114                    # For third party modules who have React-bridging dependency to search correct headers
115                    config.build_settings['HEADER_SEARCH_PATHS'] ||= '$(inherited) '
116                    config.build_settings['HEADER_SEARCH_PATHS'] << '"$(PODS_ROOT)/Headers/Private/ABI47_0_0React-bridging/react/bridging" '
117                    config.build_settings['HEADER_SEARCH_PATHS'] << '"$(PODS_CONFIGURATION_BUILD_DIR)/React-bridging/react_bridging.framework/Headers" '
118            end
119        end
120    end
121
122    def self.apply_mac_catalyst_patches(installer)
123        # Fix bundle signing issues
124        installer.pods_project.targets.each do |target|
125            if target.respond_to?(:product_type) and target.product_type == "com.apple.product-type.bundle"
126                target.build_configurations.each do |config|
127                    config.build_settings['CODE_SIGN_IDENTITY[sdk=macosx*]'] = '-'
128                end
129            end
130        end
131
132        installer.aggregate_targets.each do |aggregate_target|
133            aggregate_target.user_project.native_targets.each do |target|
134                target.build_configurations.each do |config|
135                    # Explicitly set dead code stripping flags
136                    config.build_settings['DEAD_CODE_STRIPPING'] = 'YES'
137                    config.build_settings['PRESERVE_DEAD_CODE_INITS_AND_TERMS'] = 'YES'
138                    # Modify library search paths
139                    config.build_settings['LIBRARY_SEARCH_PATHS'] = ['$(SDKROOT)/usr/lib/swift', '$(SDKROOT)/System/iOSSupport/usr/lib/swift', '$(inherited)']
140                end
141            end
142            aggregate_target.user_project.save()
143        end
144    end
145
146    private
147
148    def self.fix_library_search_path(config)
149        lib_search_paths = config.build_settings["LIBRARY_SEARCH_PATHS"]
150
151        if lib_search_paths == nil
152            # No search paths defined, return immediately
153            return
154        end
155
156        if lib_search_paths.include?("$(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME)") || lib_search_paths.include?("\"$(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME)\"")
157            # $(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME) causes problem with Xcode 12.5 + arm64 (Apple M1)
158            # since the libraries there are only built for x86_64 and i386.
159            lib_search_paths.delete("$(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME)")
160            lib_search_paths.delete("\"$(TOOLCHAIN_DIR)/usr/lib/swift-5.0/$(PLATFORM_NAME)\"")
161        end
162
163        if !(lib_search_paths.include?("$(SDKROOT)/usr/lib/swift") || lib_search_paths.include?("\"$(SDKROOT)/usr/lib/swift\""))
164            # however, $(SDKROOT)/usr/lib/swift is required, at least if user is not running CocoaPods 1.11
165            lib_search_paths.insert(0, "$(SDKROOT)/usr/lib/swift")
166        end
167    end
168
169
170end
171