Skip to content

Commit f8f11fc

Browse files
Riccardo Cipolleschifacebook-github-bot
authored andcommitted
Simplify the dependencies migration for new libraries
Summary: 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 Differential Revision: D39312203 fbshipit-source-id: 85998e1ed6881a353fed2da0ddd013bc3cc4b2ee
1 parent ebdb23c commit f8f11fc

File tree

6 files changed

+139
-17
lines changed

6 files changed

+139
-17
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, use_fabric: true)
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, use_fabric: false)
3026

3127
# s.dependency "..."
3228

scripts/cocoapods/__tests__/new_architecture-test.rb

Lines changed: 55 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,60 @@ 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_whenFabricEnabledAndNoSearchPathsNorCompilerFlagsArePresent_itInstallDependencies
118+
# Arrange
119+
spec = SpecMock.new
120+
121+
# Act
122+
NewArchitectureHelper.install_modules_dependencies(spec)
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+
"React-Core",
131+
"React-RCTFabric",
132+
"React-Codegen",
133+
"RCT-Folly",
134+
"RCTRequired",
135+
"RCTTypeSafety",
136+
"ReactCommon/turbomodule/core"
137+
])
138+
end
139+
140+
def test_installModulesDependencies_whenFabricEnabledAndSearchPathsAndCompilerFlagsArePresent_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+
use_fabric = false
149+
150+
# Act
151+
NewArchitectureHelper.install_modules_dependencies(spec, use_fabric: use_fabric)
152+
153+
# Assert
154+
assert_equal(spec.compiler_flags, "-Wno-nullability-completeness #{NewArchitectureHelper.folly_compiler_flags}")
155+
assert_equal(spec.pod_target_xcconfig["HEADER_SEARCH_PATHS"], "#{other_flags} \"$(PODS_ROOT)/boost\"")
156+
assert_equal(
157+
spec.dependencies,
158+
[
159+
"React-Core",
160+
"React-Codegen",
161+
"RCT-Folly",
162+
"RCTRequired",
163+
"RCTTypeSafety",
164+
"ReactCommon/turbomodule/core"
165+
])
166+
end
112167
end
113168

114169
# ================ #
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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)
19+
@dependencies.push(dependency_name)
20+
end
21+
22+
def to_hash
23+
return {
24+
"compiler_flags" => @compiler_flags,
25+
"pod_target_xcconfig" => @pod_target_xcconfig,
26+
}
27+
end
28+
end

scripts/cocoapods/new_architecture.rb

Lines changed: 34 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,34 @@ 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, use_fabric: true)
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+
if use_fabric
80+
spec.dependency "React-RCTFabric" # This is for Fabric Component
81+
end
82+
spec.dependency "React-Codegen"
83+
spec.dependency "RCT-Folly"
84+
spec.dependency "RCTRequired"
85+
spec.dependency "RCTTypeSafety"
86+
spec.dependency "ReactCommon/turbomodule/core"
87+
end
88+
89+
def self.folly_compiler_flags
90+
return @@folly_compiler_flags
91+
end
5992
end

scripts/react_native_pods.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ def use_react_native! (
131131
end
132132
end
133133

134+
# Getter to retrieve the folly flags in case contributors need to apply them manually.
135+
#
136+
# Returns: the folly compiler flags
137+
def folly_flags()
138+
return NewArchitectureHelper.folly_compiler_flags
139+
end
140+
141+
# This function can be used by library developer to prepare their modules for the New Architecture.
142+
# It passes the Folly Flags to the module, it configures the search path and installs some New Architecture specific dependencies.
143+
#
144+
# Parameters:
145+
# - spec: The spec that has to be configured with the New Architecture code
146+
# - use_fabric: Whether the module should depends on `Fabric``. Defaults to `true`
147+
def install_modules_dependencies(spec, use_fabric: true)
148+
NewArchitectureHelper.install_modules_dependencies(spec, use_fabric: use_fabric)
149+
end
150+
134151
# It returns the default flags.
135152
def get_default_flags()
136153
return ReactNativePodsUtils.get_default_flags()

0 commit comments

Comments
 (0)