From 8123c44c9781f03f7ebc4b19cc7b557e1282f64e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= Date: Tue, 1 Dec 2020 14:18:15 -0800 Subject: [PATCH] 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 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: https://github.com/facebook/react-native/pull/30449 Changelog: [Internal] - Moved codegen invocation out of Podfile and into FBReactNativeSpec Pod Differential Revision: D25138896 fbshipit-source-id: c756ea2f98189735343b7769bc5022379371906e --- .../FBReactNativeSpec.podspec | 16 +++++++++ package.json | 1 + packages/rn-tester/Podfile | 10 ------ packages/rn-tester/Podfile.lock | 18 +++++----- scripts/generate_native_modules_specs.rb | 34 +++++++++++++++++++ scripts/react_native_pods.rb | 18 ---------- template/ios/Podfile | 4 --- 7 files changed, 60 insertions(+), 41 deletions(-) create mode 100644 scripts/generate_native_modules_specs.rb diff --git a/Libraries/FBReactNativeSpec/FBReactNativeSpec.podspec b/Libraries/FBReactNativeSpec/FBReactNativeSpec.podspec index de5e03607754d2..23362e196b0d1f 100644 --- a/Libraries/FBReactNativeSpec/FBReactNativeSpec.podspec +++ b/Libraries/FBReactNativeSpec/FBReactNativeSpec.podspec @@ -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' @@ -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 diff --git a/package.json b/package.json index 2a1a84cfd1d77c..ec7ac8eeec7e40 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/rn-tester/Podfile b/packages/rn-tester/Podfile index e939c4f2f0b7a8..efc5c339ad5b98 100644 --- a/packages/rn-tester/Podfile +++ b/packages/rn-tester/Podfile @@ -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 diff --git a/packages/rn-tester/Podfile.lock b/packages/rn-tester/Podfile.lock index 6ba9855fd1872b..0641db9df9fc10 100644 --- a/packages/rn-tester/Podfile.lock +++ b/packages/rn-tester/Podfile.lock @@ -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): @@ -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 @@ -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 @@ -528,6 +528,6 @@ SPEC CHECKSUMS: Yoga: 69ef0b2bba5387523f793957a9f80dbd61e89631 YogaKit: f782866e155069a2cca2517aafea43200b01fd5a -PODFILE CHECKSUM: cd671238f92c51cd349a1c778fd089994174b101 +PODFILE CHECKSUM: 3adfe268d800503789170d1862bde422ee204fe8 COCOAPODS: 1.10.0 diff --git a/scripts/generate_native_modules_specs.rb b/scripts/generate_native_modules_specs.rb new file mode 100644 index 00000000000000..c314c8b36435b1 --- /dev/null +++ b/scripts/generate_native_modules_specs.rb @@ -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() diff --git a/scripts/react_native_pods.rb b/scripts/react_native_pods.rb index 14d6166f3da6c1..a77938c0510588 100644 --- a/scripts/react_native_pods.rb +++ b/scripts/react_native_pods.rb @@ -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 diff --git a/template/ios/Podfile b/template/ios/Podfile index 04f68e5431fb93..6701249e83ac20 100644 --- a/template/ios/Podfile +++ b/template/ios/Podfile @@ -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