Skip to content

Commit 2fb6a33

Browse files
Arkkeeperfacebook-github-bot
authored andcommitted
Mac Catalyst patches (#34026)
Summary: This PR adds a new method called **__apply_mac_catalyst_patches** to **scripts/react_native_pods.rb**. If it is enabled in the Podfile, it will apply three patches necessary for successful building not only for iOS and tvOS targets, but also for macOS using Apple's Mac Catalyst technology. These 3 patches are: - Fixing bundle signing issues by altering CODE_SIGN_IDENTITY - Explicitly setting dead code stripping flag in project.pbxproj - Modifying library search paths The details were discussed here reactwg/react-native-releases#21 (comment) ## Changelog [iOS] [Added] - Add Mac Catalyst compatibility (can be enabled in Podfile) Pull Request resolved: #34026 Test Plan: 1. Go to project settings in Xcode, to General tab. Enable "iPad" and "Mac Catalyst" checkboxes 2. Go to "Signing & Capabilities" tab, ensure that a correct bundle id and development team are set 3. Edit Podfile, uncomment **__apply_mac_catalyst_patches(installer)** line 4. Run `pod install` in ios directory 5. Get back to Xcode, select "My Mac (Mac Catalyst)" as a target device 6. Build & run Reviewed By: cipolleschi Differential Revision: D37362054 Pulled By: cortinico fbshipit-source-id: 74636f716f112289ab40968bbc8e52406c1e9579
1 parent bd12e41 commit 2fb6a33

File tree

6 files changed

+100
-15
lines changed

6 files changed

+100
-15
lines changed

packages/rn-tester/Podfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def pods(options = {}, use_flipper: false)
2121
project 'RNTesterPods.xcodeproj'
2222

2323
fabric_enabled = true
24+
2425
# Hermes is now enabled by default.
2526
# The following line will only disable Hermes if the USE_HERMES envvar is SET to a value other than 1 (e.g. USE_HERMES=0).
2627
hermes_enabled = !ENV.has_key?('USE_HERMES') || ENV['USE_HERMES'] == '1'
@@ -66,6 +67,6 @@ target 'RNTesterIntegrationTests' do
6667
end
6768

6869
post_install do |installer|
69-
react_native_post_install(installer, @prefix_path)
70+
react_native_post_install(installer, @prefix_path, :mac_catalyst_enabled => false)
7071
__apply_Xcode_12_5_M1_post_install_workaround(installer)
7172
end

scripts/cocoapods/__tests__/test_utils/InstallerMock.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,16 @@ def save_as(file_path)
138138
class TargetMock
139139
attr_reader :name
140140
attr_reader :build_configurations
141+
attr_reader :product_type
141142

142143
attr_reader :received_resolved_build_setting_parameters
143144

144-
def initialize(name, build_configurations = [])
145+
def initialize(name, build_configurations = [], product_type = nil)
145146
@name = name
146147
@build_configurations = build_configurations
148+
unless product_type.nil?
149+
@product_type = product_type
150+
end
147151
@received_resolved_build_setting_parameters = []
148152
end
149153

scripts/cocoapods/__tests__/utils-test.rb

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -266,20 +266,20 @@ def test_fixLibrarySearchPath_whenThereAreSearchPathsAndNoSwift_removesSwift5_5A
266266
# ============================== #
267267

268268
def test_fixLibrarySearchPaths_correctlySetsTheSearchPathsForAllProjects
269-
firstTarget = prepare_target("FirstTarget")
270-
secondTarget = prepare_target("SecondTarget")
271-
thirdTarget = prepare_target("ThirdTarget")
269+
first_target = prepare_target("FirstTarget")
270+
second_target = prepare_target("SecondTarget")
271+
third_target = prepare_target("ThirdTarget")
272272
user_project_mock = UserProjectMock.new("a/path", [
273273
prepare_config("Debug"),
274274
prepare_config("Release"),
275275
],
276276
:native_targets => [
277-
firstTarget,
278-
secondTarget
277+
first_target,
278+
second_target
279279
]
280280
)
281281
pods_projects_mock = PodsProjectMock.new([], {"hermes-engine" => {}}, :native_targets => [
282-
thirdTarget
282+
third_target
283283
])
284284
installer = InstallerMock.new(pods_projects_mock, [
285285
AggregatedProjectMock.new(user_project_mock)
@@ -315,6 +315,55 @@ def test_fixLibrarySearchPaths_correctlySetsTheSearchPathsForAllProjects
315315
assert_equal(pods_projects_mock.save_invocation_count, 1)
316316
end
317317

318+
# ================================= #
319+
# Test - Apply Mac Catalyst Patches #
320+
# ================================= #
321+
322+
def test_applyMacCatalystPatches_correctlyAppliesNecessaryPatches
323+
first_target = prepare_target("FirstTarget")
324+
second_target = prepare_target("SecondTarget")
325+
third_target = prepare_target("ThirdTarget", "com.apple.product-type.bundle")
326+
user_project_mock = UserProjectMock.new("a/path", [
327+
prepare_config("Debug"),
328+
prepare_config("Release"),
329+
],
330+
:native_targets => [
331+
first_target,
332+
second_target
333+
]
334+
)
335+
pods_projects_mock = PodsProjectMock.new([third_target], {"hermes-engine" => {}}, :native_targets => [])
336+
installer = InstallerMock.new(pods_projects_mock, [
337+
AggregatedProjectMock.new(user_project_mock)
338+
])
339+
340+
# Act
341+
ReactNativePodsUtils.apply_mac_catalyst_patches(installer)
342+
343+
# Assert
344+
first_target.build_configurations.each do |config|
345+
assert_nil(config.build_settings["CODE_SIGN_IDENTITY[sdk=macosx*]"])
346+
end
347+
348+
second_target.build_configurations.each do |config|
349+
assert_nil(config.build_settings["CODE_SIGN_IDENTITY[sdk=macosx*]"])
350+
end
351+
352+
third_target.build_configurations.each do |config|
353+
assert_equal(config.build_settings["CODE_SIGN_IDENTITY[sdk=macosx*]"], "-")
354+
end
355+
356+
user_project_mock.native_targets.each do |target|
357+
target.build_configurations.each do |config|
358+
assert_equal(config.build_settings["DEAD_CODE_STRIPPING"], "YES")
359+
assert_equal(config.build_settings["PRESERVE_DEAD_CODE_INITS_AND_TERMS"], "YES")
360+
assert_equal(config.build_settings["LIBRARY_SEARCH_PATHS"], ["$(SDKROOT)/usr/lib/swift", "$(SDKROOT)/System/iOSSupport/usr/lib/swift", "$(inherited)"])
361+
end
362+
end
363+
364+
assert_equal(user_project_mock.save_invocation_count, 1)
365+
end
366+
318367
# ==================================== #
319368
# Test - Set Node_Modules User Setting #
320369
# ==================================== #
@@ -357,9 +406,9 @@ def prepare_config(config_name)
357406
]})
358407
end
359408

360-
def prepare_target(name)
361-
return TargetMock.new(name, [
362-
prepare_config("Debug"),
363-
prepare_config("Release")
364-
])
409+
def prepare_target(name, product_type = nil)
410+
return TargetMock.new(name, [
411+
prepare_config("Debug"),
412+
prepare_config("Release")
413+
], product_type)
365414
end

scripts/cocoapods/utils.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,30 @@ def self.fix_library_search_paths(installer)
9393
end
9494
end
9595

96+
def self.apply_mac_catalyst_patches(installer)
97+
# Fix bundle signing issues
98+
installer.pods_project.targets.each do |target|
99+
if target.respond_to?(:product_type) and target.product_type == "com.apple.product-type.bundle"
100+
target.build_configurations.each do |config|
101+
config.build_settings['CODE_SIGN_IDENTITY[sdk=macosx*]'] = '-'
102+
end
103+
end
104+
end
105+
106+
installer.aggregate_targets.each do |aggregate_target|
107+
aggregate_target.user_project.native_targets.each do |target|
108+
target.build_configurations.each do |config|
109+
# Explicitly set dead code stripping flags
110+
config.build_settings['DEAD_CODE_STRIPPING'] = 'YES'
111+
config.build_settings['PRESERVE_DEAD_CODE_INITS_AND_TERMS'] = 'YES'
112+
# Modify library search paths
113+
config.build_settings['LIBRARY_SEARCH_PATHS'] = ['$(SDKROOT)/usr/lib/swift', '$(SDKROOT)/System/iOSSupport/usr/lib/swift', '$(inherited)']
114+
end
115+
end
116+
aggregate_target.user_project.save()
117+
end
118+
end
119+
96120
private
97121

98122
def self.fix_library_search_path(config)

scripts/react_native_pods.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ def use_flipper!(versions = {}, configurations: ['Debug'])
141141
use_flipper_pods(versions, :configurations => configurations)
142142
end
143143

144-
def react_native_post_install(installer, react_native_path = "../node_modules/react-native")
144+
def react_native_post_install(installer, react_native_path = "../node_modules/react-native", mac_catalyst_enabled: false)
145+
ReactNativePodsUtils.apply_mac_catalyst_patches(installer) if mac_catalyst_enabled
146+
145147
if ReactNativePodsUtils.has_pod(installer, 'Flipper')
146148
flipper_post_install(installer)
147149
end

template/ios/Podfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ target 'HelloWorld' do
3232
end
3333

3434
post_install do |installer|
35-
react_native_post_install(installer)
35+
react_native_post_install(
36+
installer,
37+
# Set `mac_catalyst_enabled` to `true` in order to apply patches
38+
# necessary for Mac Catalyst builds
39+
:mac_catalyst_enabled => false
40+
)
3641
__apply_Xcode_12_5_M1_post_install_workaround(installer)
3742
end
3843
end

0 commit comments

Comments
 (0)