diff --git a/packages/react-native/scripts/ios-prebuild.js b/packages/react-native/scripts/ios-prebuild.js index 78631f7d6b9..3160df83255 100644 --- a/packages/react-native/scripts/ios-prebuild.js +++ b/packages/react-native/scripts/ios-prebuild.js @@ -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! diff --git a/packages/react-native/scripts/ios-prebuild/swiftpackage.js b/packages/react-native/scripts/ios-prebuild/build.js similarity index 62% rename from packages/react-native/scripts/ios-prebuild/swiftpackage.js rename to packages/react-native/scripts/ios-prebuild/build.js index b085e1ab049..e0454e658b4 100644 --- a/packages/react-native/scripts/ios-prebuild/swiftpackage.js +++ b/packages/react-native/scripts/ios-prebuild/build.js @@ -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 */ { - 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, };