Add ReactNativeDependencies.podspec and replace script. (#49812)

Summary:
There are two environment variables that is related to ReactNativeDependencies:
- `RCT_USE_DEP_PREBUILD `: If set to 1, ReactNativeDependencies will be built from source.
- `RCT_DEPS_VERSION`: If set to 1, it will override the version of ReactNativeDependencies to be used.
bypass-github-export-checks
## Changelog:

[INTERNAL] - Introduced functions to configure ReactNativeDependencies in Cocoapods

Pull Request resolved: https://github.com/facebook/react-native/pull/49812

Test Plan:
 Run Rn-Tester and  verify that it works as expected both building deps from source and using prebuilt tarballs
 Add third-party library (react-native-reanimated) and perform the same tests to verify that it works with the changed podspec and utilities

Reviewed By: javache

Differential Revision: D70968672

Pulled By: cipolleschi

fbshipit-source-id: bb93e763bd71cec7314565b5a751b226735b404e
This commit is contained in:
Christian Falch
2025-03-13 08:40:14 -07:00
committed by Facebook GitHub Bot
parent e876e4926f
commit 6bde3ce715
3 changed files with 187 additions and 0 deletions
@@ -0,0 +1,75 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
require "json"
begin
react_native_path = File.dirname(Pod::Executable.execute_command('node', ['-p',
'require.resolve(
"react-native",
{paths: [process.argv[1]]},
)', __dir__]).strip
)
rescue => e
# Fallback to the parent directory if the above command fails (e.g when building locally in OOT Platform)
react_native_path = File.join(__dir__, "..", "..")
end
# package.json
package = JSON.parse(File.read(File.join(react_native_path, "package.json")))
version = package['version']
source = ReactNativeDependenciesUtils.podspec_source_download_prebuild_release_tarball()
Pod::Spec.new do |spec|
spec.name = 'ReactNativeDependencies'
spec.version = version
spec.summary = 'React Native Dependencies'
spec.description = 'ReactNativeDependencies is a podspec that contains all the third-party dependencies of React Native.'
spec.homepage = 'https://github.com/facebook/react-native'
spec.license = package['license']
spec.authors = 'meta'
spec.platforms = min_supported_versions
spec.user_target_xcconfig = {
'WARNING_CFLAGS' => '-Wno-comma -Wno-shorten-64-to-32',
}
spec.source = source
spec.preserve_paths = '**/*.*'
spec.vendored_frameworks = 'framework/packages/react-native/third-party/ReactNativeDependencies.xcframework'
spec.header_mappings_dir = 'Headers'
spec.source_files = 'Headers/**/*.{h,hpp}'
spec.prepare_command = <<-CMD
mkdir -p Headers
rsync -a react-native/third-party/ReactNativeDependencies.xcframework/ios-arm64/ReactNativeDependencies.framework/Headers/ Headers
mkdir -p framework/packages/react-native
rsync -a --remove-source-files react-native/ framework/packages/react-native/
find react-native/ -type d -empty -delete
CMD
script_phase = {
:name => "[RNDeps] Replace React Native Dependencies for the right configuration, if needed",
:execution_position => :before_compile,
:script => <<-EOS
. "$REACT_NATIVE_PATH/scripts/xcode/with-environment.sh"
CONFIG="Release"
if echo $GCC_PREPROCESSOR_DEFINITIONS | grep -q "DEBUG=1"; then
CONFIG="Debug"
fi
"$NODE_BINARY" "$REACT_NATIVE_PATH/third-party-podspecs/replace_dependencies_version.js" -c "$CONFIG" -r "#{version}" -p "$PODS_ROOT"
EOS
}
# :always_out_of_date is only available in CocoaPods 1.13.0 and later
if Gem::Version.new(Pod::VERSION) >= Gem::Version.new('1.13.0')
# always run the script without warning
script_phase[:always_out_of_date] = "1"
end
spec.script_phase = script_phase
end
@@ -0,0 +1,107 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const {execSync} = require('child_process');
const fs = require('fs');
const yargs = require('yargs');
const LAST_BUILD_FILENAME = 'ReactNativeDependencies/.last_build_configuration';
function validateBuildConfiguration(configuration) {
if (!['Debug', 'Release'].includes(configuration)) {
throw new Error(`Invalid configuration ${configuration}`);
}
}
function validateVersion(version) {
if (version == null || version === '') {
throw new Error('Version cannot be empty');
}
}
function shouldReplaceRnDepsConfiguration(configuration) {
const fileExists = fs.existsSync(LAST_BUILD_FILENAME);
if (fileExists) {
console.log(`Found ${LAST_BUILD_FILENAME} file`);
const oldConfiguration = fs.readFileSync(LAST_BUILD_FILENAME).toString();
if (oldConfiguration === configuration) {
console.log(
'Same config of the previous build. No need to replace RNDeps',
);
return false;
}
}
// Assumption: if there is no stored last build, we assume that it was build for debug.
if (!fileExists && configuration === 'Debug') {
console.log(
'No previous build detected, but Debug Configuration. No need to replace RNDeps',
);
return false;
}
return true;
}
function replaceRNDepsConfiguration(configuration, version, podsRoot) {
const tarballURLPath = `${podsRoot}/ReactNativeDependencies-artifacts/rndeps-ios-${version.toLowerCase()}-${configuration.toLowerCase()}.tar.gz`;
const finalLocation = 'ReactNativeDependencies/framework';
console.log('Preparing the final location', finalLocation);
fs.rmSync(finalLocation, {force: true, recursive: true});
fs.mkdirSync(finalLocation, {recursive: true});
console.log('Extracting the tarball', tarballURLPath);
execSync(`tar -xf ${tarballURLPath} -C ${finalLocation}`);
}
function updateLastBuildConfiguration(configuration) {
console.log(`Updating ${LAST_BUILD_FILENAME} with ${configuration}`);
fs.writeFileSync(LAST_BUILD_FILENAME, configuration);
}
function main(configuration, version, podsRoot) {
validateBuildConfiguration(configuration);
validateVersion(version);
if (!shouldReplaceRnDepsConfiguration(configuration)) {
return;
}
replaceRNDepsConfiguration(configuration, version, podsRoot);
updateLastBuildConfiguration(configuration);
console.log('Done replacing React Native Dependencies');
}
// This script is executed in the Pods folder, which is usually not synched to Github, so it should be ok
const argv = yargs
.option('c', {
alias: 'configuration',
description:
'Configuration to use to download the right React Native Dependencies version. Allowed values are "Debug" and "Release".',
})
.option('r', {
alias: 'reactNativeVersion',
description:
'The Version of React Native associated with the React Native Dependencies tarball.',
})
.option('p', {
alias: 'podsRoot',
description: 'The path to the Pods root folder',
})
.usage('Usage: $0 -c Debug -r <version> -p <path/to/react-native>').argv;
const configuration = argv.configuration;
const version = argv.reactNativeVersion;
const podsRoot = argv.podsRoot;
main(configuration, version, podsRoot);
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict/>
</plist>