Skip to content

Commit d8a0a9b

Browse files
hramosgrabbou
authored andcommitted
Integrate Native Module codegen into Xcode build pipeline (#30449)
Summary: Move the codegen invocation out of Podfiles and into the FBReactNativeSpec Pod itself. With this change, developers do not need to modify their existing project's Podfiles, and yet the codegen will be integrated into their projects automatically by way of the FBReactNativeSpec Pod. This is accomplished in part by injecting a script build phase into the Pods Xcode project that is generated by CocoaPods. The build phase will save the output of the codegen script to a log in the derived files directory. The codegen will be executed if the codegen log file is not present, or if the contents of the Libraries directory has changed. The codegen will thus be invoked in these situations: **RNTester:** * When `packages/rn-tester/RNTesterPods.xcworkspace` is built, if the codegen output logfile is not present or if the input files have changed. **OSS React Native apps:** * When `ios/AwesomeProject.xcworkspace` is built, if the codegen output file is not present or if the input files have changed. Normally, this should not happen, as we do not expect folks to update the contents of `node_modules/react-native/Libraries`. Pull Request resolved: #30449 Changelog: [Internal] - Moved codegen invocation out of Podfile and into FBReactNativeSpec Pod Reviewed By: fkgozali Differential Revision: D25138896 fbshipit-source-id: 4779f822459cea2c30fd544eee19a49e8d80153d
1 parent a99676e commit d8a0a9b

File tree

8 files changed

+84
-64
lines changed

8 files changed

+84
-64
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ package-lock.json
103103
/Libraries/FBReactNativeSpec/FBReactNativeSpec
104104
/packages/react-native-codegen/lib
105105
/ReactCommon/fabric/components/rncore/
106-
/schema-native-modules.json
107106
/schema-rncore.json
108107

109108
# Visual studio

Libraries/FBReactNativeSpec/FBReactNativeSpec.podspec

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@ package = JSON.parse(File.read(File.join(__dir__, "..", "..", "package.json")))
99
version = package['version']
1010

1111
source = { :git => 'https://github.com/facebook/react-native.git' }
12+
codegen_path_prefix = ".."
1213
if version == '1000.0.0'
1314
# This is an unpublished version, use the latest commit hash of the react-native repo, which we’re presumably in.
1415
source[:commit] = `git rev-parse HEAD`.strip
16+
codegen_path_prefix = "packages"
1517
else
1618
source[:tag] = "v#{version}"
1719
end
1820

21+
react_native_path = File.join(__dir__, "..", "..")
22+
srcs_dir = File.join(__dir__, "..")
23+
codegen_script_path = File.join(react_native_path, "scripts", "generate-native-modules-specs.sh")
24+
codegen_path = File.join(react_native_path, codegen_path_prefix, "react-native-codegen")
25+
generated_files = [File.join(__dir__, "FBReactNativeSpec", "FBReactNativeSpec.h"), File.join(__dir__, "FBReactNativeSpec", "FBReactNativeSpec-generated.mm")]
26+
codegen_command = "CODEGEN_PATH=#{codegen_path} sh '#{codegen_script_path}' | tee \"${SCRIPT_OUTPUT_FILE_0}\""
27+
1928
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
2029
folly_version = '2020.01.13.00'
2130

@@ -44,4 +53,13 @@ Pod::Spec.new do |s|
4453
s.dependency "React-Core", version
4554
s.dependency "React-jsi", version
4655
s.dependency "ReactCommon/turbomodule/core", version
56+
57+
s.prepare_command = "touch #{generated_files.reduce() { |str, file| str + " " + file }}"
58+
s.script_phase = {
59+
:name => 'Generate Native Modules Code',
60+
:input_files => [srcs_dir],
61+
:output_files => ["$(DERIVED_FILE_DIR)/FBReactNativeSpec-codegen.log"],
62+
:script => codegen_command,
63+
:execution_position => :before_compile
64+
}
4765
end

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"README.md",
3838
"rn-get-polyfills.js",
3939
"scripts/compose-source-maps.js",
40+
"scripts/generate-native-modules-specs.sh",
4041
"scripts/generate-native-modules-specs-cli.js",
4142
"scripts/ios-configure-glog.sh",
4243
"scripts/launchPackager.bat",

packages/rn-tester/Podfile

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,6 @@ def frameworks_pre_install(installer)
6060
end
6161
end
6262

63-
pre_install do |installer|
64-
frameworks_pre_install(installer) if ENV['USE_FRAMEWORKS'] == '1'
65-
if ENV['USE_CODEGEN'] != '0'
66-
prefix_path = "../.."
67-
codegen_path = "../../packages/react-native-codegen"
68-
system("./#{codegen_path}/scripts/oss/build.sh") or raise "Could not build react-native-codegen package"
69-
codegen_pre_install(installer, {path:prefix_path, codegen_path:codegen_path})
70-
end
71-
end
72-
7363
post_install do |installer|
7464
flipper_post_install(installer)
7565
end

packages/rn-tester/Podfile.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ PODS:
1515
- Flipper-Folly (~> 2.2)
1616
- Flipper-RSocket (~> 1.1)
1717
- Flipper-DoubleConversion (1.1.7)
18-
- Flipper-Folly (2.2.0):
18+
- Flipper-Folly (2.3.0):
1919
- boost-for-react-native
2020
- CocoaLibEvent (~> 1.0)
2121
- Flipper-DoubleConversion
2222
- Flipper-Glog
23-
- OpenSSL-Universal (= 1.0.2.19)
23+
- OpenSSL-Universal (= 1.0.2.20)
2424
- Flipper-Glog (0.3.6)
2525
- Flipper-PeerTalk (0.0.4)
2626
- Flipper-RSocket (1.1.0):
@@ -58,9 +58,9 @@ PODS:
5858
- FlipperKit/Core
5959
- FlipperKit/FlipperKitNetworkPlugin
6060
- glog (0.3.5)
61-
- OpenSSL-Universal (1.0.2.19):
62-
- OpenSSL-Universal/Static (= 1.0.2.19)
63-
- OpenSSL-Universal/Static (1.0.2.19)
61+
- OpenSSL-Universal (1.0.2.20):
62+
- OpenSSL-Universal/Static (= 1.0.2.20)
63+
- OpenSSL-Universal/Static (1.0.2.20)
6464
- RCT-Folly (2020.01.13.00):
6565
- boost-for-react-native
6666
- DoubleConversion
@@ -490,16 +490,16 @@ SPEC CHECKSUMS:
490490
CocoaLibEvent: 2fab71b8bd46dd33ddb959f7928ec5909f838e3f
491491
DoubleConversion: cde416483dac037923206447da6e1454df403714
492492
FBLazyVector: fe973c09b2299b5e8154186ecf1f6554b4f70987
493-
FBReactNativeSpec: 20a9345af9157362b51ab0258d842cb7bb347d19
493+
FBReactNativeSpec: 4b0a53603445208c324b4a23d77590c399894efc
494494
Flipper: be611d4b742d8c87fbae2ca5f44603a02539e365
495495
Flipper-DoubleConversion: 38631e41ef4f9b12861c67d17cb5518d06badc41
496-
Flipper-Folly: c12092ea368353b58e992843a990a3225d4533c3
496+
Flipper-Folly: e4493b013c02d9347d5e0cb4d128680239f6c78a
497497
Flipper-Glog: 1dfd6abf1e922806c52ceb8701a3599a79a200a6
498498
Flipper-PeerTalk: 116d8f857dc6ef55c7a5a75ea3ceaafe878aadc9
499499
Flipper-RSocket: 64e7431a55835eb953b0bf984ef3b90ae9fdddd7
500500
FlipperKit: ab353d41aea8aae2ea6daaf813e67496642f3d7d
501501
glog: 40a13f7840415b9a77023fbcae0f1e6f43192af3
502-
OpenSSL-Universal: 8b48cc0d10c1b2923617dfe5c178aa9ed2689355
502+
OpenSSL-Universal: ff34003318d5e1163e9529b08470708e389ffcdd
503503
RCT-Folly: b39288cedafe50da43317ec7d91bcc8cc0abbf33
504504
RCTRequired: d3d4ce60e1e2282864d7560340690a3c8c646de1
505505
RCTTypeSafety: 4da4f9f218727257c50fd3bf2683a06cdb4fede3
@@ -528,6 +528,6 @@ SPEC CHECKSUMS:
528528
Yoga: 69ef0b2bba5387523f793957a9f80dbd61e89631
529529
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
530530

531-
PODFILE CHECKSUM: cd671238f92c51cd349a1c778fd089994174b101
531+
PODFILE CHECKSUM: 3adfe268d800503789170d1862bde422ee204fe8
532532

533533
COCOAPODS: 1.10.0

scripts/generate-native-modules-specs.sh

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,88 @@
44
# This source code is licensed under the MIT license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
# This script collects the JavaScript spec definitions for native
8-
# modules, then uses react-native-codegen to generate native code.
9-
# The script will copy the generated code to the final location by
10-
# default. Optionally, call the script with a path to the desired
11-
# output location.
7+
# This script collects the JavaScript spec definitions for core
8+
# native modules, then uses react-native-codegen to generate
9+
# native code.
10+
# The script will use the local react-native-codegen package by
11+
# default. Optionally, set the CODEGEN_PATH to point to the
12+
# desired codegen library (e.g. when using react-native-codegen
13+
# from npm).
1214
#
1315
# Usage:
14-
# ./scripts/generate-native-modules-specs.sh [output-dir]
16+
# ./scripts/generate-native-modules-specs.sh
1517
#
1618
# Example:
17-
# ./scripts/generate-native-modules-specs.sh ./codegen-out
19+
# CODEGEN_PATH=.. ./scripts/generate-native-modules-specs.sh
1820

1921
# shellcheck disable=SC2038
2022

2123
set -e
2224

2325
THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)
26+
TEMP_DIR=$(mktemp -d /tmp/react-native-codegen-XXXXXXXX)
2427
RN_DIR=$(cd "$THIS_DIR/.." && pwd)
25-
CODEGEN_DIR=$(cd "$RN_DIR/packages/react-native-codegen" && pwd)
26-
OUTPUT_DIR="${1:-$RN_DIR/Libraries/FBReactNativeSpec/FBReactNativeSpec}"
27-
SCHEMA_FILE="$RN_DIR/schema-native-modules.json"
28+
CODEGEN_PATH="${CODEGEN_PATH:-$(cd "$RN_DIR/packages" && pwd)}"
29+
CODEGEN_DIR="$CODEGEN_PATH/react-native-codegen"
30+
2831
YARN_BINARY="${YARN_BINARY:-$(command -v yarn)}"
2932

33+
cleanup () {
34+
set +e
35+
rm -rf "$TEMP_DIR"
36+
set -e
37+
}
38+
3039
describe () {
3140
printf "\\n\\n>>>>> %s\\n\\n\\n" "$1"
3241
}
3342

3443
step_build_codegen () {
35-
describe "Building react-native-codegen package"
36-
pushd "$CODEGEN_DIR" >/dev/null || exit
37-
"$YARN_BINARY"
38-
"$YARN_BINARY" build
39-
popd >/dev/null || exit
44+
if [ ! -d "$CODEGEN_DIR/lib" ]; then
45+
describe "Building react-native-codegen package"
46+
pushd "$CODEGEN_DIR" >/dev/null || exit
47+
"$YARN_BINARY"
48+
"$YARN_BINARY" build
49+
popd >/dev/null || exit
50+
fi
4051
}
4152

42-
step_gen_schema () {
53+
run_codegen () {
54+
SRCS_DIR=$1
55+
LIBRARY_NAME=$2
56+
OUTPUT_DIR=$3
57+
58+
SCHEMA_FILE="$TEMP_DIR/schema-$LIBRARY_NAME.json"
59+
60+
if [ ! -d "$CODEGEN_DIR/lib" ]; then
61+
describe "Building react-native-codegen package"
62+
pushd "$CODEGEN_DIR" >/dev/null || exit
63+
"$YARN_BINARY"
64+
"$YARN_BINARY" build
65+
popd >/dev/null || exit
66+
fi
67+
4368
describe "Generating schema from flow types"
44-
SRCS_DIR=$(cd "$RN_DIR/Libraries" && pwd)
4569
"$YARN_BINARY" node "$CODEGEN_DIR/lib/cli/combine/combine-js-to-schema-cli.js" "$SCHEMA_FILE" "$SRCS_DIR"
46-
}
4770

48-
step_gen_specs () {
4971
describe "Generating native code from schema (iOS)"
5072
pushd "$RN_DIR" >/dev/null || exit
5173
"$YARN_BINARY" --silent node scripts/generate-native-modules-specs-cli.js ios "$SCHEMA_FILE" "$OUTPUT_DIR"
5274
popd >/dev/null || exit
5375
}
5476

55-
step_build_codegen
56-
step_gen_schema
57-
step_gen_specs
77+
# Handle Core Modules
78+
run_codegen_core_modules () {
79+
LIBRARY_NAME="FBReactNativeSpec"
80+
SRCS_DIR=$(cd "$RN_DIR/Libraries" && pwd)
81+
OUTPUT_DIR="$SRCS_DIR/$LIBRARY_NAME/$LIBRARY_NAME"
82+
83+
run_codegen "$SRCS_DIR" "$LIBRARY_NAME" "$OUTPUT_DIR"
84+
}
85+
86+
main() {
87+
run_codegen_core_modules
88+
}
89+
90+
trap cleanup EXIT
91+
main "$@"

scripts/react_native_pods.rb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,3 @@ def flipper_post_install(installer)
108108
end
109109
end
110110
end
111-
112-
# Pre Install processing for Native Modules
113-
def codegen_pre_install(installer, options={})
114-
# Path to React Native
115-
prefix = options[:path] ||= "../node_modules/react-native"
116-
117-
# Path to react-native-codegen
118-
codegen_path = options[:codegen_path] ||= "#{prefix}/../react-native-codegen"
119-
120-
# Handle Core Modules
121-
Dir.mktmpdir do |dir|
122-
native_module_spec_name = "FBReactNativeSpec"
123-
schema_file = dir + "/schema-#{native_module_spec_name}.json"
124-
srcs_dir = "#{prefix}/Libraries"
125-
schema_generated = system("node #{codegen_path}/lib/cli/combine/combine-js-to-schema-cli.js #{schema_file} #{srcs_dir}") or raise "Could not generate Native Module schema"
126-
specs_generated = system("node #{prefix}/scripts/generate-native-modules-specs-cli.js ios #{schema_file} #{srcs_dir}/#{native_module_spec_name}/#{native_module_spec_name}") or raise "Could not generate code for #{native_module_spec_name}"
127-
end
128-
end

template/ios/Podfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ target 'HelloWorld' do
1313
# Pods for testing
1414
end
1515

16-
pre_install do |installer|
17-
codegen_pre_install(installer)
18-
end
19-
2016
# Enables Flipper.
2117
#
2218
# Note that if you have use_frameworks! enabled, Flipper will not work and

0 commit comments

Comments
 (0)