Integrate the cli script with the build script (#51845)

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

This change refactors the build step of SwiftPM to use the cli.js script to coordinate it.

## Changelog:
[Internal] -

Reviewed By: cortinico

Differential Revision: D76046444

fbshipit-source-id: 7aa4ba55e46c2e4502687f035ba1e7bf772b3079
This commit is contained in:
Riccardo Cipolleschi
2025-06-06 06:43:33 -07:00
committed by Facebook GitHub Bot
parent 6435dec161
commit bd2855b056
2 changed files with 63 additions and 34 deletions
+20 -2
View File
@@ -8,9 +8,13 @@
* @format
*/
const {
buildSwiftPackage,
computeFrameworkPaths,
computeProductsFolder,
} = require('./ios-prebuild/build');
const {getCLIConfiguration} = require('./ios-prebuild/cli');
const {setup} = require('./ios-prebuild/setup');
const {buildSwiftPackage} = require('./ios-prebuild/swiftpackage');
const {createLogger, throwIfOnEden} = require('./ios-prebuild/utils');
const {buildXCFrameworks} = require('./ios-prebuild/xcframework');
const path = require('path');
@@ -49,9 +53,23 @@ async function main() {
await setup(root, buildFolder, currentVersion, buildType);
}
const frameworkPaths = buildSwiftPackage(root, buildFolder, buildType);
const outputFolder = path.join(buildFolder, 'output', 'spm', buildType);
// BUILD SWIFT PACKAGE
if (cli.tasks.build) {
cli.destinations.forEach(destination => {
buildSwiftPackage(
root,
buildFolder,
buildType,
destination,
outputFolder,
);
});
}
// GENERATE XCFrameworks
const productsFolder = computeProductsFolder(outputFolder);
const frameworkPaths = computeFrameworkPaths(productsFolder);
buildXCFrameworks(root, buildFolder, frameworkPaths, buildType);
// Done!
@@ -8,6 +8,10 @@
* @format
*/
/*::
import type {Destination} from './types';
*/
const {createLogger} = require('./utils');
const {execSync} = require('child_process');
const fs = require('fs');
@@ -16,38 +20,13 @@ const path = require('path');
const buildLog = createLogger('SPM');
function buildSwiftPackage(
rootFolder /*: string */,
buildFolder /*: string */,
buildType /*: 'debug' | 'release' */,
function computeProductsFolder(outputFolder /*: string */) /*: string */ {
return path.join(outputFolder, 'Build', 'Products');
}
function computeFrameworkPaths(
productsFolder /*: string */,
) /*: Array<string> */ {
const outputFolder = path.join(buildFolder, 'output', 'spm', buildType);
const buildPlatform = (platform /*:string*/) => {
const buildCommand =
`xcodebuild -scheme React -destination "generic/platform=${platform}" -derivedDataPath "${outputFolder}" ` +
`-configuration "${buildType}" SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES OTHER_SWIFT_FLAGS="-no-verify-emitted-module-interface"`;
buildLog(`Building Swift package for ${buildType}`);
buildLog(buildCommand);
execSync(buildCommand, {
cwd: rootFolder,
stdio: 'inherit',
});
};
// TODO: Extend with platforms/targets
buildPlatform('iOS simulator');
buildPlatform('iOS');
buildPlatform('macOS,variant=Mac Catalyst');
// Use glob to find all the frameworks in the output folder
const productsFolder = path.join(outputFolder, 'Build/Products');
if (!fs.existsSync(productsFolder)) {
throw new Error(
`Output folder does not exist: ${productsFolder}. Did the build fail?`,
);
}
// The frameworks are in the products folder under a platform/buildType folder and are directories ending with .framework
const frameworks = glob.sync('**/*.framework', {
cwd: productsFolder,
@@ -66,13 +45,45 @@ function buildSwiftPackage(
`No React.framework found in the output folder: ${productsFolder}`,
);
}
return frameworkPaths;
}
function buildSwiftPackage(
rootFolder /*: string */,
buildFolder /*: string */,
buildType /*: 'debug' | 'release' */,
platform /*: Destination */,
outputFolder /*: string */,
) {
const buildCommand =
`xcodebuild -scheme React -destination "generic/platform=${platform}" -derivedDataPath "${outputFolder}" ` +
`-configuration "${buildType}" SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES OTHER_SWIFT_FLAGS="-no-verify-emitted-module-interface"`;
buildLog(`Building Swift package for ${buildType}`);
buildLog(buildCommand);
execSync(buildCommand, {
cwd: rootFolder,
stdio: 'inherit',
});
// Use glob to find all the frameworks in the output folder
const productsFolder = computeProductsFolder(outputFolder);
if (!fs.existsSync(productsFolder)) {
throw new Error(
`Output folder does not exist: ${productsFolder}. Did the build fail?`,
);
}
const frameworkPaths = computeFrameworkPaths(productsFolder);
buildLog('React.frameworks:');
frameworkPaths.forEach(p => {
buildLog(' ' + p);
});
return frameworkPaths;
}
module.exports = {
buildSwiftPackage,
computeFrameworkPaths,
computeProductsFolder,
};