Skip to content

Commit

Permalink
Integrate Native Module codegen into Xcode build pipeline (facebook#3…
Browse files Browse the repository at this point in the history
…0449)

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 be executed if the generated FBReactNativeSpec{.h, -generated.mm} files are not present, or if the contents of the Libraries directory has changed. The codegen is also triggered by `pod install` via `prepare_command`.

The codegen will thus be invoked in these situations:

**RNTester:**
* When `pod install` is invoked in `packages/rn-tester/` (`prepare_command`)
* When `packages/rn-tester/RNTesterPods.xcworkspace` is built, if the output files are not present or if the input files have changed (`script_phase`).

**OSS React Native apps:**
* When `pod install` is invoked in `ios/` (`prepare_command`)
* When `ios/AwesomeProject.xcworkspace` is built, if the output files are not present or if the input files have changed (`script_phase`).

Pull Request resolved: facebook#30449

Changelog: [Internal] - Moved codegen invocation out of Podfile and into FBReactNativeSpec Pod

Differential Revision: D25138896

fbshipit-source-id: 753ef8d3514f090774e7b50b38987ed559ac7467
  • Loading branch information
hramos authored and facebook-github-bot committed Dec 1, 2020
1 parent ae6f4f3 commit 4f7ed2b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 41 deletions.
16 changes: 16 additions & 0 deletions Libraries/FBReactNativeSpec/FBReactNativeSpec.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ package = JSON.parse(File.read(File.join(__dir__, "..", "..", "package.json")))
version = package['version']

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

reactNativePath = File.join(__dir__, "..", "..")
codegenScriptPath = File.join(reactNativePath, "scripts", "generate_native_modules_specs.rb")
codegenPath = File.join(reactNativePath, codegenPathPrefix, "react-native-codegen")
codegenCommand = "CODEGEN_PATH=#{codegenPath} ruby '#{codegenScriptPath}'"

folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
folly_version = '2020.01.13.00'

Expand Down Expand Up @@ -44,4 +51,13 @@ Pod::Spec.new do |s|
s.dependency "React-Core", version
s.dependency "React-jsi", version
s.dependency "ReactCommon/turbomodule/core", version

s.prepare_command = codegenCommand
s.script_phase = {
:name => 'Generate Native Modules Code',
:input_files => [File.join(__dir__, "..")],
:output_files => [File.join(__dir__, "Libraries", "FBReactNativeSpec", "FBReactNativeSpec", "FBReactNativeSpec.h"), File.join(__dir__, "Libraries", "FBReactNativeSpec", "FBReactNativeSpec", "FBReactNativeSpec-generated.mm")],
:script => codegenCommand,
:execution_position => :before_compile
}
end
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"README.md",
"rn-get-polyfills.js",
"scripts/compose-source-maps.js",
"scripts/generate_native_modules_specs.rb",
"scripts/generate-native-modules-specs-cli.js",
"scripts/ios-configure-glog.sh",
"scripts/launchPackager.bat",
Expand Down
10 changes: 0 additions & 10 deletions packages/rn-tester/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,6 @@ def frameworks_pre_install(installer)
end
end

pre_install do |installer|
frameworks_pre_install(installer) if ENV['USE_FRAMEWORKS'] == '1'
if ENV['USE_CODEGEN'] != '0'
prefix_path = "../.."
codegen_path = "../../packages/react-native-codegen"
system("./#{codegen_path}/scripts/oss/build.sh") or raise "Could not build react-native-codegen package"
codegen_pre_install(installer, {path:prefix_path, codegen_path:codegen_path})
end
end

post_install do |installer|
flipper_post_install(installer)
end
18 changes: 9 additions & 9 deletions packages/rn-tester/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ PODS:
- Flipper-Folly (~> 2.2)
- Flipper-RSocket (~> 1.1)
- Flipper-DoubleConversion (1.1.7)
- Flipper-Folly (2.2.0):
- Flipper-Folly (2.3.0):
- boost-for-react-native
- CocoaLibEvent (~> 1.0)
- Flipper-DoubleConversion
- Flipper-Glog
- OpenSSL-Universal (= 1.0.2.19)
- OpenSSL-Universal (= 1.0.2.20)
- Flipper-Glog (0.3.6)
- Flipper-PeerTalk (0.0.4)
- Flipper-RSocket (1.1.0):
Expand Down Expand Up @@ -58,9 +58,9 @@ PODS:
- FlipperKit/Core
- FlipperKit/FlipperKitNetworkPlugin
- glog (0.3.5)
- OpenSSL-Universal (1.0.2.19):
- OpenSSL-Universal/Static (= 1.0.2.19)
- OpenSSL-Universal/Static (1.0.2.19)
- OpenSSL-Universal (1.0.2.20):
- OpenSSL-Universal/Static (= 1.0.2.20)
- OpenSSL-Universal/Static (1.0.2.20)
- RCT-Folly (2020.01.13.00):
- boost-for-react-native
- DoubleConversion
Expand Down Expand Up @@ -490,16 +490,16 @@ SPEC CHECKSUMS:
CocoaLibEvent: 2fab71b8bd46dd33ddb959f7928ec5909f838e3f
DoubleConversion: cde416483dac037923206447da6e1454df403714
FBLazyVector: fe973c09b2299b5e8154186ecf1f6554b4f70987
FBReactNativeSpec: 20a9345af9157362b51ab0258d842cb7bb347d19
FBReactNativeSpec: 5abf6676352b0b7cffaeef981408e4802bda23a2
Flipper: be611d4b742d8c87fbae2ca5f44603a02539e365
Flipper-DoubleConversion: 38631e41ef4f9b12861c67d17cb5518d06badc41
Flipper-Folly: c12092ea368353b58e992843a990a3225d4533c3
Flipper-Folly: e4493b013c02d9347d5e0cb4d128680239f6c78a
Flipper-Glog: 1dfd6abf1e922806c52ceb8701a3599a79a200a6
Flipper-PeerTalk: 116d8f857dc6ef55c7a5a75ea3ceaafe878aadc9
Flipper-RSocket: 64e7431a55835eb953b0bf984ef3b90ae9fdddd7
FlipperKit: ab353d41aea8aae2ea6daaf813e67496642f3d7d
glog: 40a13f7840415b9a77023fbcae0f1e6f43192af3
OpenSSL-Universal: 8b48cc0d10c1b2923617dfe5c178aa9ed2689355
OpenSSL-Universal: ff34003318d5e1163e9529b08470708e389ffcdd
RCT-Folly: b39288cedafe50da43317ec7d91bcc8cc0abbf33
RCTRequired: d3d4ce60e1e2282864d7560340690a3c8c646de1
RCTTypeSafety: 4da4f9f218727257c50fd3bf2683a06cdb4fede3
Expand Down Expand Up @@ -528,6 +528,6 @@ SPEC CHECKSUMS:
Yoga: 69ef0b2bba5387523f793957a9f80dbd61e89631
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

PODFILE CHECKSUM: cd671238f92c51cd349a1c778fd089994174b101
PODFILE CHECKSUM: 3adfe268d800503789170d1862bde422ee204fe8

COCOAPODS: 1.10.0
34 changes: 34 additions & 0 deletions scripts/generate_native_modules_specs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/ruby
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

require 'tmpdir'

# Handle Core Modules
def codegen_core_modules(codegenPath)
reactNativePath = File.join(__dir__, "..")
srcsDir = File.join(reactNativePath, "Libraries")
libraryName = "FBReactNativeSpec"
outputDir = File.join(srcsDir, libraryName, libraryName)

codegen(codegenPath, srcsDir, libraryName, outputDir)
end

def codegen(codegenPath, srcsDir, libraryName, outputDir)
Dir.mktmpdir do |dir|
schemaFilePath = File.join(dir, "schema-#{libraryName}.json")
combineCliPath = File.join(codegenPath, "lib", "cli", "combine", "combine-js-to-schema-cli.js")
generateSpecsCliPath = File.join(__dir__, "generate-native-modules-specs-cli.js")
system("node #{combineCliPath} #{schemaFilePath} #{srcsDir}") or raise "Could not generate Native Module schema"
system("node #{generateSpecsCliPath} ios #{schemaFilePath} #{outputDir} #{libraryName}") or raise "Could not generate code for #{libraryName}"
end
end

def main()
codegenPath = ENV["CODEGEN_PATH"] ||= File.join(reactNativePath, "..", "react-native-codegen")
codegen_core_modules(codegenPath)
end

main()
18 changes: 0 additions & 18 deletions scripts/react_native_pods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,3 @@ def flipper_post_install(installer)
end
end
end

# Pre Install processing for Native Modules
def codegen_pre_install(installer, options={})
# Path to React Native
prefix = options[:path] ||= "../node_modules/react-native"

# Path to react-native-codegen
codegen_path = options[:codegen_path] ||= "#{prefix}/../react-native-codegen"

# Handle Core Modules
Dir.mktmpdir do |dir|
native_module_spec_name = "FBReactNativeSpec"
schema_file = dir + "/schema-#{native_module_spec_name}.json"
srcs_dir = "#{prefix}/Libraries"
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"
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}"
end
end
4 changes: 0 additions & 4 deletions template/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ target 'HelloWorld' do
# Pods for testing
end

pre_install do |installer|
codegen_pre_install(installer)
end

# Enables Flipper.
#
# Note that if you have use_frameworks! enabled, Flipper will not work and
Expand Down

0 comments on commit 4f7ed2b

Please sign in to comment.