Skip to content

Commit 82e9c6a

Browse files
Riccardo Cipolleschifacebook-github-bot
authored andcommitted
Simplify the dependencies migration for new libraries (#34619)
Summary: Pull Request resolved: #34619 When it comes to migrate existing libraries, users needs to update their .podspec files with a bunch [of changes](https://reactnative.dev/docs/0.69/new-architecture-library-ios#add-folly-and-other-dependencies). This diff groups those changes in a single function, so that contributors can just invoke that function to prepare their dependencies. ## Changelog [iOS][Changed] - Add function to simplify podspecs Reviewed By: cortinico Differential Revision: D39312203 fbshipit-source-id: ed631839e07d472a1fdcba33310f9b1d94fe2fd7
1 parent 54e50ea commit 82e9c6a

File tree

6 files changed

+141
-21
lines changed

6 files changed

+141
-21
lines changed

packages/rn-tester/NativeComponentExample/MyNativeView.podspec

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ require "json"
77

88
package = JSON.parse(File.read(File.join(__dir__, "../" "package.json")))
99

10-
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
11-
folly_version = '2021.07.22.00'
1210
boost_version = '1.76.0'
1311
boost_compiler_flags = '-Wno-documentation'
1412

@@ -20,23 +18,18 @@ Pod::Spec.new do |s|
2018
s.homepage = "https://github.com/sota000/my-native-view.git"
2119
s.license = "MIT"
2220
s.platforms = { :ios => "12.4", :tvos => "12.4" }
23-
s.compiler_flags = folly_compiler_flags + ' ' + boost_compiler_flags + ' -Wno-nullability-completeness'
21+
s.compiler_flags = boost_compiler_flags + ' -Wno-nullability-completeness'
2422
s.author = "Facebook, Inc. and its affiliates"
2523
s.source = { :git => "https://github.com/facebook/my-native-view.git", :tag => "#{s.version}" }
2624
s.pod_target_xcconfig = {
27-
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/RCT-Folly\" \"$(PODS_ROOT)/boost\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-Codegen/React_Codegen.framework/Headers\"",
25+
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/RCT-Folly\" \"$(PODS_ROOT)/boost\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-Codegen/React_Codegen.framework/Headers\"",
2826
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
2927
}
3028

3129
s.source_files = "ios/**/*.{h,m,mm,cpp}"
3230
s.requires_arc = true
3331

34-
s.dependency "React"
35-
s.dependency "React-RCTFabric"
36-
s.dependency "React-Codegen"
37-
s.dependency "RCTRequired"
38-
s.dependency "RCTTypeSafety"
39-
s.dependency "ReactCommon/turbomodule/core"
32+
install_modules_dependencies(s)
4033

4134
# Enable codegen for this library
4235
use_react_native_codegen!(s, {

packages/rn-tester/NativeModuleExample/ScreenshotManager.podspec

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ require "json"
77

88
package = JSON.parse(File.read(File.join(__dir__, "../package.json")))
99

10-
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
11-
folly_version = '2021.07.22.00'
12-
1310
Pod::Spec.new do |s|
1411
s.name = "ScreenshotManager"
1512
s.version = package["version"]
@@ -18,15 +15,14 @@ Pod::Spec.new do |s|
1815
s.homepage = "https://github.com/facebook/react-native.git"
1916
s.license = "MIT"
2017
s.platforms = { :ios => "12.4", :tvos => "12.4" }
21-
s.compiler_flags = folly_compiler_flags + ' -Wno-nullability-completeness'
18+
s.compiler_flags = '-Wno-nullability-completeness'
2219
s.author = "Facebook, Inc. and its affiliates"
2320
s.source = { :git => "https://github.com/facebook/react-native.git", :tag => "#{s.version}" }
2421

2522
s.source_files = "**/*.{h,m,mm,swift}"
2623
s.requires_arc = true
2724

28-
s.dependency "React-Core"
29-
s.dependency "RCT-Folly", folly_version
25+
install_modules_dependencies(s)
3026

3127
# s.dependency "..."
3228

scripts/cocoapods/__tests__/new_architecture-test.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require_relative "../new_architecture.rb"
88
require_relative "./test_utils/InstallerMock.rb"
99
require_relative "./test_utils/PodMock.rb"
10+
require_relative "./test_utils/SpecMock.rb"
1011

1112
class NewArchitectureTests < Test::Unit::TestCase
1213
def teardown
@@ -109,6 +110,56 @@ def test_modifyFlagsForNewArch_whenOnNewArch_updateFlags
109110
assert_equal(yoga_debug_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited)")
110111
assert_equal(yoga_release_config.build_settings["OTHER_CPLUSPLUSFLAGS"], "$(inherited)")
111112
end
113+
114+
# =================================== #
115+
# Test - install Modules Dependencies #
116+
# =================================== #
117+
def test_installModulesDependencies_whenNewArchEnabledAndNewArchAndNoSearchPathsNorCompilerFlagsArePresent_itInstallDependencies
118+
# Arrange
119+
spec = SpecMock.new
120+
121+
# Act
122+
NewArchitectureHelper.install_modules_dependencies(spec, true, '2021.07.22.00')
123+
124+
# Assert
125+
assert_equal(spec.compiler_flags, NewArchitectureHelper.folly_compiler_flags)
126+
assert_equal(spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"], "\"$(PODS_ROOT)/boost\"")
127+
assert_equal(
128+
spec.dependencies,
129+
[
130+
{ :dependency_name => "React-Core" },
131+
{ :dependency_name => "RCT-Folly", "version"=>"2021.07.22.00" },
132+
{ :dependency_name => "React-RCTFabric" },
133+
{ :dependency_name => "React-Codegen" },
134+
{ :dependency_name => "RCTRequired" },
135+
{ :dependency_name => "RCTTypeSafety" },
136+
{ :dependency_name => "ReactCommon/turbomodule/core" }
137+
])
138+
end
139+
140+
def test_installModulesDependencies_whenNewArchDisabledAndSearchPathsAndCompilerFlagsArePresent_itInstallDependenciesAndPreserveOtherSettings
141+
# Arrange
142+
spec = SpecMock.new
143+
spec.compiler_flags = '-Wno-nullability-completeness'
144+
other_flags = "\"$(PODS_ROOT)/RCT-Folly\" \"$(PODS_ROOT)/boost\" \"${PODS_CONFIGURATION_BUILD_DIR}/React-Codegen/React_Codegen.framework/Headers\""
145+
spec.pod_target_xcconfig = {
146+
"HEADER_SEARCH_PATHS" => other_flags
147+
}
148+
149+
# Act
150+
NewArchitectureHelper.install_modules_dependencies(spec, false, '2021.07.22.00')
151+
152+
# Assert
153+
assert_equal(spec.compiler_flags, "-Wno-nullability-completeness #{NewArchitectureHelper.folly_compiler_flags}")
154+
assert_equal(spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"], "#{other_flags} \"$(PODS_ROOT)/boost\"")
155+
assert_equal(
156+
spec.dependencies,
157+
[
158+
{ :dependency_name => "React-Core" },
159+
{ :dependency_name => "RCT-Folly", "version"=>"2021.07.22.00" },
160+
]
161+
)
162+
end
112163
end
113164

114165
# ================ #
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
6+
class SpecMock
7+
8+
attr_accessor :compiler_flags
9+
attr_accessor :pod_target_xcconfig
10+
attr_reader :dependencies
11+
12+
def initialize
13+
@compiler_flags = ""
14+
@pod_target_xcconfig = Hash.new
15+
@dependencies = []
16+
end
17+
18+
def dependency(dependency_name, version = nil)
19+
toPush = {"dependency_name": dependency_name}
20+
toPush["version"] = version if version
21+
@dependencies.push(toPush)
22+
end
23+
24+
def to_hash
25+
return {
26+
"compiler_flags" => @compiler_flags,
27+
"pod_target_xcconfig" => @pod_target_xcconfig,
28+
}
29+
end
30+
end

scripts/cocoapods/new_architecture.rb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
# LICENSE file in the root directory of this source tree.
55

66
class NewArchitectureHelper
7+
@@shared_flags = "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1"
78

8-
@@new_arch_cpp_flags = '$(inherited) -DRCT_NEW_ARCH_ENABLED=1 -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1'
9+
@@folly_compiler_flags = "#{@@shared_flags} -Wno-comma -Wno-shorten-64-to-32"
10+
11+
@@new_arch_cpp_flags = "$(inherited) -DRCT_NEW_ARCH_ENABLED=1 #{@@shared_flags}"
912

1013
def self.set_clang_cxx_language_standard_if_needed(installer)
1114
language_standard = nil
@@ -56,4 +59,36 @@ def self.modify_flags_for_new_architecture(installer, is_new_arch_enabled)
5659
end
5760
end
5861
end
62+
63+
def self.install_modules_dependencies(spec, new_arch_enabled, folly_version)
64+
# Pod::Specification does not have getters so, we have to read
65+
# the existing values from a hash representation of the object.
66+
hash = spec.to_hash
67+
68+
compiler_flags = hash["compiler_flags"] ? hash["compiler_flags"] : ""
69+
current_config = hash["pod_target_xcconfig"] != nil ? hash["pod_target_xcconfig"] : {}
70+
current_headers = current_config["HEADER_SEARCH_PATHS"] != nil ? current_config["HEADER_SEARCH_PATHS"] : ""
71+
72+
boost_search_path = "\"$(PODS_ROOT)/boost\""
73+
74+
spec.compiler_flags = compiler_flags.empty? ? @@folly_compiler_flags : "#{compiler_flags} #{@@folly_compiler_flags}"
75+
current_config["HEADER_SEARCH_PATHS"] = current_headers.empty? ? boost_search_path : "#{current_headers} #{boost_search_path}"
76+
spec.pod_target_xcconfig = current_config
77+
78+
spec.dependency "React-Core"
79+
spec.dependency "RCT-Folly", '2021.07.22.00'
80+
81+
if new_arch_enabled
82+
spec.dependency "React-RCTFabric" # This is for Fabric Component
83+
spec.dependency "React-Codegen"
84+
85+
spec.dependency "RCTRequired"
86+
spec.dependency "RCTTypeSafety"
87+
spec.dependency "ReactCommon/turbomodule/core"
88+
end
89+
end
90+
91+
def self.folly_compiler_flags
92+
return @@folly_compiler_flags
93+
end
5994
end

scripts/react_native_pods.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
$CODEGEN_OUTPUT_DIR = 'build/generated/ios'
2020
$CODEGEN_COMPONENT_DIR = 'react/renderer/components'
2121
$CODEGEN_MODULE_DIR = '.'
22+
$FOLLY_VERSION = '2021.07.22.00'
2223

2324
$START_TIME = Time.now.to_i
2425

@@ -49,9 +50,6 @@ def use_react_native! (
4950

5051
prefix = path
5152

52-
# The version of folly that must be used
53-
folly_version = '2021.07.22.00'
54-
5553
ReactNativePodsUtils.warn_if_not_on_arm64()
5654

5755
# The Pods which should be included in all projects
@@ -100,7 +98,7 @@ def use_react_native! (
10098
:fabric_enabled => fabric_enabled,
10199
:codegen_output_dir => $CODEGEN_OUTPUT_DIR,
102100
:package_json_file => File.join(__dir__, "..", "package.json"),
103-
:folly_version => folly_version
101+
:folly_version => $FOLLY_VERSION
104102
)
105103

106104
pod 'React-Codegen', :path => $CODEGEN_OUTPUT_DIR, :modular_headers => true
@@ -131,6 +129,23 @@ def use_react_native! (
131129
end
132130
end
133131

132+
# Getter to retrieve the folly flags in case contributors need to apply them manually.
133+
#
134+
# Returns: the folly compiler flags
135+
def folly_flags()
136+
return NewArchitectureHelper.folly_compiler_flags
137+
end
138+
139+
# This function can be used by library developer to prepare their modules for the New Architecture.
140+
# It passes the Folly Flags to the module, it configures the search path and installs some New Architecture specific dependencies.
141+
#
142+
# Parameters:
143+
# - spec: The spec that has to be configured with the New Architecture code
144+
# - new_arch_enabled: Whether the module should install dependencies for the new architecture
145+
def install_modules_dependencies(spec, new_arch_enabled: ENV['RCT_NEW_ARCH_ENABLED'] == "1")
146+
NewArchitectureHelper.install_modules_dependencies(spec, new_arch_enabled, $FOLLY_VERSION)
147+
end
148+
134149
# It returns the default flags.
135150
def get_default_flags()
136151
return ReactNativePodsUtils.get_default_flags()

0 commit comments

Comments
 (0)