Skip to content

Commit

Permalink
Add use_react_native_codegen!
Browse files Browse the repository at this point in the history
Summary:
Consolidate CocoaPods codegen scripts under a single `use_react_native_codegen!` method in `react_native_pods.rb`.

This is the first step towards making the codegen scripts library-agnostic. There are still a handful of hardcoded assumptions in place (e.g. the output directory structure, the use of a separate directory for components), but with some work one would be able to add codegen support to arbitrary CocoaPods podspecs.

The codegen script no longer takes a CODEGEN_PATH argument, and will instead attempt to use the local react-native-codegen package if available, and fallback to using the node_modules/react-native-codegen package if not.

## Usage

The `use_react_native_codegen!` method has two arguments:

- `spec`, a pod [Specification](https://www.rubydoc.info/github/CocoaPods/Core/Pod/Specification) object.
- `options`, an optional object. Supported keys:
  - `:srcs_dir`, the path to your JavaScript sources. Your native module or component specs should be located somewhere in this directory.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D25728053

fbshipit-source-id: feec587b656d5b220598ce6196ea6bb34a9580a9
  • Loading branch information
hramos authored and grabbou committed Feb 1, 2021
1 parent 0636c45 commit e5888de
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 44 deletions.
36 changes: 2 additions & 34 deletions Libraries/FBReactNativeSpec/FBReactNativeSpec.podspec
Expand Up @@ -4,44 +4,19 @@
# LICENSE file in the root directory of this source tree.

require "json"
require_relative "../../scripts/react_native_pods.rb"

package = JSON.parse(File.read(File.join(__dir__, "..", "..", "package.json")))
version = package['version']

source = { :git => 'https://github.com/facebook/react-native.git' }
codegen_path_prefix = ".."
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
codegen_path_prefix = "packages"
else
source[:tag] = "v#{version}"
end

react_native_path = File.join(__dir__, "..", "..")
srcs_dir = File.join(__dir__, "..")
codegen_script_path = File.join(react_native_path, "scripts", "generate-specs.sh")
codegen_path = File.join(react_native_path, codegen_path_prefix, "react-native-codegen")
codegen_command = "CODEGEN_PATH=#{codegen_path} sh '#{codegen_script_path}' | tee \"${SCRIPT_OUTPUT_FILE_0}\""
modules_output_dir = File.join(__dir__, "FBReactNativeSpec")
components_output_dir = File.join(react_native_path, "ReactCommon", "react", "renderer", "components", "rncore")
generated_filenames = [ "FBReactNativeSpec.h", "FBReactNativeSpec-generated.mm" ]
generated_files = generated_filenames.map { |filename| File.join(modules_output_dir, filename) }

if ENV['USE_FABRIC'] == '1'
components_generated_filenames = [
"ComponentDescriptors.h",
"EventEmitters.cpp",
"EventEmitters.h",
"Props.cpp",
"Props.h",
"RCTComponentViewHelpers.h",
"ShadowNodes.cpp",
"ShadowNodes.h"
]
generated_files = generated_files.concat(components_generated_filenames.map { |filename| File.join(components_output_dir, filename) })
end

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 @@ -72,12 +47,5 @@ Pod::Spec.new do |s|
s.dependency "React-jsi", version
s.dependency "ReactCommon/turbomodule/core", version

s.prepare_command = "mkdir -p #{modules_output_dir} #{components_output_dir} && touch #{generated_files.reduce() { |str, file| str + " " + file }}"
s.script_phase = {
:name => 'Generate Specs',
:input_files => [srcs_dir],
:output_files => ["$(DERIVED_FILE_DIR)/codegen.log"],
:script => codegen_command,
:execution_position => :before_compile
}
use_react_native_codegen! (s)
end
2 changes: 1 addition & 1 deletion packages/rn-tester/Podfile.lock
Expand Up @@ -798,7 +798,7 @@ SPEC CHECKSUMS:
CocoaLibEvent: 2fab71b8bd46dd33ddb959f7928ec5909f838e3f
DoubleConversion: cde416483dac037923206447da6e1454df403714
FBLazyVector: fe973c09b2299b5e8154186ecf1f6554b4f70987
FBReactNativeSpec: 259a715466e53b411664fdfbe166dbde93ece5b6
FBReactNativeSpec: cc83b1f9c1a764577c37a7009d002b8afb36f192
Flipper: be611d4b742d8c87fbae2ca5f44603a02539e365
Flipper-DoubleConversion: 38631e41ef4f9b12861c67d17cb5518d06badc41
Flipper-Folly: e4493b013c02d9347d5e0cb4d128680239f6c78a
Expand Down
21 changes: 13 additions & 8 deletions scripts/generate-specs.sh
Expand Up @@ -7,16 +7,10 @@
# This script collects the JavaScript spec definitions for core
# native modules and components, then uses react-native-codegen
# to generate native code.
# The script will use the local react-native-codegen package by
# default. Optionally, set the CODEGEN_PATH to point to the
# desired codegen library (e.g. when using react-native-codegen
# from npm).
#
# Usage:
# ./scripts/generate-specs.sh
#
# Examples:
# CODEGEN_PATH=.. ./scripts/generate-specs.sh

# shellcheck disable=SC2038

Expand All @@ -25,7 +19,6 @@ set -e
THIS_DIR=$(cd -P "$(dirname "$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")")" && pwd)
TEMP_DIR=$(mktemp -d /tmp/react-native-codegen-XXXXXXXX)
RN_DIR=$(cd "$THIS_DIR/.." && pwd)
CODEGEN_PATH="${CODEGEN_PATH:-$(cd "$RN_DIR/packages/react-native-codegen" && pwd)}"
YARN_BINARY="${YARN_BINARY:-$(command -v yarn)}"
USE_FABRIC="${USE_FABRIC:-0}"

Expand All @@ -48,6 +41,18 @@ main() {

SCHEMA_FILE="$TEMP_DIR/schema.json"

CODEGEN_REPO_PATH="$RN_DIR/packages/react-native-codegen"
CODEGEN_NPM_PATH="$RN_DIR/../react-native-codegen"

if [ -d "$CODEGEN_REPO_PATH" ]; then
CODEGEN_PATH=$(cd "$CODEGEN_REPO_PATH" && pwd)
elif [ -d "$CODEGEN_NPM_PATH" ]; then
CODEGEN_PATH=$(cd "$CODEGEN_NPM_PATH" && pwd)
else
echo "Error: Could not determine react-native-codegen location. Try running 'yarn install' or 'npm install' in your project root." 1>&2
exit 1
fi

if [ ! -d "$CODEGEN_PATH/lib" ]; then
describe "Building react-native-codegen package"
pushd "$CODEGEN_PATH" >/dev/null || exit
Expand All @@ -61,7 +66,7 @@ main() {

describe "Generating native code from schema (iOS)"
pushd "$RN_DIR" >/dev/null || exit
USE_FABRIC="$USE_FABRIC" "$YARN_BINARY" --silent node scripts/generate-specs-cli.js ios "$SCHEMA_FILE" "$OUTPUT_DIR"
"$YARN_BINARY" --silent node scripts/generate-specs-cli.js ios "$SCHEMA_FILE" "$OUTPUT_DIR"
popd >/dev/null || exit

mkdir -p "$COMPONENTS_DIR" "$MODULES_DIR"
Expand Down
50 changes: 49 additions & 1 deletion scripts/react_native_pods.rb
Expand Up @@ -4,7 +4,7 @@
# LICENSE file in the root directory of this source tree.

def use_react_native! (options={})
# The prefix to the react-native
# The prefix to react-native
prefix = options[:path] ||= "../node_modules/react-native"

# Include Fabric dependencies
Expand Down Expand Up @@ -144,3 +144,51 @@ def react_native_post_install(installer)

exclude_architectures(installer)
end

def use_react_native_codegen!(spec, options={})
# The path to react-native (e.g. react_native_path)
prefix = options[:path] ||= File.join(__dir__, "..")

# The path to JavaScript files
srcs_dir = options[:srcs_dir] ||= File.join(prefix, "Libraries")

# Library name (e.g. FBReactNativeSpec)
library_name = spec.name
modules_output_dir = File.join(prefix, "Libraries/#{library_name}/#{library_name}")

# Run the codegen as part of the Xcode build pipeline.
spec.script_phase = {
:name => 'Generate Specs',
:input_files => [srcs_dir],
:output_files => ["$(DERIVED_FILE_DIR)/codegen.log"],
:script => "sh '#{File.join(__dir__, "generate-specs.sh")}' | tee \"${SCRIPT_OUTPUT_FILE_0}\"",
:execution_position => :before_compile
}

# Since the generated files are not guaranteed to exist when CocoaPods is run, we need to create
# empty files to ensure the references are included in the resulting Pods Xcode project.
mkdir_command = "mkdir -p #{modules_output_dir}"
generated_filenames = [ "#{library_name}.h", "#{library_name}-generated.mm" ]
generated_files = generated_filenames.map { |filename| File.join(modules_output_dir, filename) }

if ENV['USE_FABRIC'] == '1'
# We use a different library name for components, as well as an additional set of files.
# Eventually, we want these to be part of the same library as #{library_name} above.
components_library_name = "rncore"
components_output_dir = File.join(prefix, "ReactCommon/react/renderer/components/#{components_library_name}")
mkdir_command += " #{components_output_dir}"
components_generated_filenames = [
"ComponentDescriptors.h",
"EventEmitters.cpp",
"EventEmitters.h",
"Props.cpp",
"Props.h",
"RCTComponentViewHelpers.h",
"ShadowNodes.cpp",
"ShadowNodes.h"
]
generated_files = generated_files.concat(components_generated_filenames.map { |filename| File.join(components_output_dir, filename) })
end

spec.prepare_command = "#{mkdir_command} && touch #{generated_files.reduce() { |str, file| str + " " + file }}"
end

0 comments on commit e5888de

Please sign in to comment.