From 8a4f4acd97461df35e3e74d56a321ccc2b2299c9 Mon Sep 17 00:00:00 2001 From: Christian Falch Date: Fri, 27 Nov 2026 12:45:37 +0100 Subject: [PATCH] Add --mergeable option to the iOS prebuild scripts The iOS prebuild scripts cannot build React and ReactNativeDependencies as mergeable frameworks, which Xcode can merge into an app executable. --mergeable adds MERGEABLE_LIBRARY (for dynamic libraries only) to the Release xcodebuild command in both scripts. Debug builds and builds without the option are unchanged. --- packages/react-native/scripts/ios-prebuild.js | 1 + .../scripts/ios-prebuild/__docs__/README.md | 19 +++++---- .../ios-prebuild/__tests__/build-test.js | 40 ++++++++++++++++++ .../ios-prebuild/__tests__/cli-test.js | 41 +++++++++++++++++++ .../scripts/ios-prebuild/build.js | 16 ++++++-- .../react-native/scripts/ios-prebuild/cli.js | 7 ++++ .../releases/ios-prebuild/__docs__/README.md | 23 ++++++----- .../ios-prebuild/__tests__/build-test.js | 41 +++++++++++++++++++ .../ios-prebuild/__tests__/cli-test.js | 41 +++++++++++++++++++ scripts/releases/ios-prebuild/build.js | 18 ++++++-- scripts/releases/ios-prebuild/cli.js | 7 ++++ scripts/releases/prepare-ios-prebuilds.js | 1 + 12 files changed, 229 insertions(+), 26 deletions(-) create mode 100644 packages/react-native/scripts/ios-prebuild/__tests__/cli-test.js create mode 100644 scripts/releases/ios-prebuild/__tests__/cli-test.js diff --git a/packages/react-native/scripts/ios-prebuild.js b/packages/react-native/scripts/ios-prebuild.js index 30096fb909e9..4cb0fdcb39f9 100644 --- a/packages/react-native/scripts/ios-prebuild.js +++ b/packages/react-native/scripts/ios-prebuild.js @@ -63,6 +63,7 @@ async function main() { buildType, destination, outputFolder, + cli.mergeable, ); }); } diff --git a/packages/react-native/scripts/ios-prebuild/__docs__/README.md b/packages/react-native/scripts/ios-prebuild/__docs__/README.md index 0e1431f876ba..6fe135775439 100644 --- a/packages/react-native/scripts/ios-prebuild/__docs__/README.md +++ b/packages/react-native/scripts/ios-prebuild/__docs__/README.md @@ -37,15 +37,16 @@ If no options are passed, the script executes all the steps in this order: ### Options -| Option | Alias | Type | Default | Description | -| ------------- | ----- | ------- | ------------------------------------------ | ------------------------------------------------------------------- | -| `--setup` | `-s` | boolean | - | Download and setup dependencies | -| `--build` | `-b` | boolean | - | Build dependencies/platforms | -| `--compose` | `-c` | boolean | - | Compose XCFramework from built dependencies | -| `--platforms` | `-p` | array | `['ios', 'ios-simulator', 'mac-catalyst']` | Specify one or more platforms to build for | -| `--flavor` | `-f` | string | `Debug` | Specify the flavor to build: `Debug` or `Release` | -| `--identity` | `-i` | string | - | Specify the code signing identity to use for signing the frameworks | -| `--help` | - | boolean | - | Show help information | +| Option | Alias | Type | Default | Description | +| ------------- | ----- | ------- | ------------------------------------------ | --------------------------------------------------------------------------------------------------- | +| `--setup` | `-s` | boolean | - | Download and setup dependencies | +| `--build` | `-b` | boolean | - | Build dependencies/platforms | +| `--compose` | `-c` | boolean | - | Compose XCFramework from built dependencies | +| `--platforms` | `-p` | array | `['ios', 'ios-simulator', 'mac-catalyst']` | Specify one or more platforms to build for | +| `--flavor` | `-f` | string | `Debug` | Specify the flavor to build: `Debug` or `Release` | +| `--identity` | `-i` | string | - | Specify the code signing identity to use for signing the frameworks | +| `--mergeable` | - | boolean | - | Build Release frameworks that Xcode can merge into the app executable. Release only, off by default | +| `--help` | - | boolean | - | Show help information | ### Output Structure diff --git a/packages/react-native/scripts/ios-prebuild/__tests__/build-test.js b/packages/react-native/scripts/ios-prebuild/__tests__/build-test.js index b27a2a9cb39a..d0e1330c03af 100644 --- a/packages/react-native/scripts/ios-prebuild/__tests__/build-test.js +++ b/packages/react-native/scripts/ios-prebuild/__tests__/build-test.js @@ -38,4 +38,44 @@ describe('getSwiftPackageBuildCommand', () => { 'DEBUG_INFORMATION_FORMAT="dwarf-with-dsym"', ); }); + + describe('mergeable', () => { + const releaseCommand = + 'xcodebuild -scheme React -destination "generic/platform=iOS" -derivedDataPath "output" ' + + '-configuration "Release" SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES OTHER_SWIFT_FLAGS="-no-verify-emitted-module-interface" ' + + 'DEBUG_INFORMATION_FORMAT="dwarf-with-dsym" GCC_OPTIMIZATION_LEVEL=2'; + + it('makes Release dynamic frameworks mergeable', () => { + const command = getSwiftPackageBuildCommand( + 'Release', + 'iOS', + 'output', + true, + ); + + expect(command).toBe( + `${releaseCommand} 'MERGEABLE_LIBRARY=$(MERGEABLE_LIBRARY_$(MACH_O_TYPE))' MERGEABLE_LIBRARY_mh_dylib=YES`, + ); + }); + + it('leaves the Release command unchanged when off', () => { + const command = getSwiftPackageBuildCommand( + 'Release', + 'iOS', + 'output', + false, + ); + + expect(command).toBe(releaseCommand); + }); + + it('leaves the Debug command unchanged', () => { + expect(getSwiftPackageBuildCommand('Debug', 'iOS', 'output', true)).toBe( + getSwiftPackageBuildCommand('Debug', 'iOS', 'output', false), + ); + expect( + getSwiftPackageBuildCommand('Debug', 'iOS', 'output', true), + ).not.toContain('MERGEABLE_LIBRARY'); + }); + }); }); diff --git a/packages/react-native/scripts/ios-prebuild/__tests__/cli-test.js b/packages/react-native/scripts/ios-prebuild/__tests__/cli-test.js new file mode 100644 index 000000000000..8865c8d3b5d6 --- /dev/null +++ b/packages/react-native/scripts/ios-prebuild/__tests__/cli-test.js @@ -0,0 +1,41 @@ +/** + * 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 + * @noflow + */ + +'use strict'; + +describe('getCLIConfiguration', () => { + const originalArgv = process.argv; + + afterEach(() => { + process.argv = originalArgv; + }); + + // yargs reads process.argv when it is first required. + const parse = args => { + process.argv = ['node', 'ios-prebuild.js', ...args]; + let getCLIConfiguration; + jest.isolateModules(() => { + ({getCLIConfiguration} = require('../cli')); + }); + return getCLIConfiguration(); + }; + + it('reads --mergeable', async () => { + expect(await parse(['--flavor', 'Release', '--mergeable'])).toMatchObject({ + mergeable: true, + }); + }); + + it('leaves mergeable off by default', async () => { + expect(await parse(['--flavor', 'Release'])).toMatchObject({ + mergeable: false, + }); + }); +}); diff --git a/packages/react-native/scripts/ios-prebuild/build.js b/packages/react-native/scripts/ios-prebuild/build.js index 73a723689cb4..caaf18e7126e 100644 --- a/packages/react-native/scripts/ios-prebuild/build.js +++ b/packages/react-native/scripts/ios-prebuild/build.js @@ -56,21 +56,29 @@ function computeFrameworkPaths( return frameworkPaths; } +// MERGEABLE_LIBRARY=YES would also reach static library targets such as glog +// and break their link, so enable it only where MACH_O_TYPE is mh_dylib. +const MERGEABLE_SETTINGS = + "'MERGEABLE_LIBRARY=$(MERGEABLE_LIBRARY_$(MACH_O_TYPE))' MERGEABLE_LIBRARY_mh_dylib=YES"; + function getSwiftPackageBuildCommand( buildType /*: BuildFlavor */, platform /*: Destination */, outputFolder /*: string */, + mergeable /*: boolean */, ) /*: string */ { const command = `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" ` + `DEBUG_INFORMATION_FORMAT="dwarf-with-dsym"`; + if (buildType !== 'Release') { + return command; + } // Xcode defaults Release to -Os. Android builds React Native at -O2 in // Release. A command-line setting applies to every configuration, so add it // only to Release builds. - return buildType === 'Release' - ? `${command} GCC_OPTIMIZATION_LEVEL=2` - : command; + const releaseCommand = `${command} GCC_OPTIMIZATION_LEVEL=2`; + return mergeable ? `${releaseCommand} ${MERGEABLE_SETTINGS}` : releaseCommand; } function buildSwiftPackage( @@ -79,11 +87,13 @@ function buildSwiftPackage( buildType /*: BuildFlavor */, platform /*: Destination */, outputFolder /*: string */, + mergeable /*: boolean */, ) { const buildCommand = getSwiftPackageBuildCommand( buildType, platform, outputFolder, + mergeable, ); buildLog(`Building Swift package for ${buildType}`); buildLog(buildCommand); diff --git a/packages/react-native/scripts/ios-prebuild/cli.js b/packages/react-native/scripts/ios-prebuild/cli.js index 53c326439d22..49182ad35da8 100644 --- a/packages/react-native/scripts/ios-prebuild/cli.js +++ b/packages/react-native/scripts/ios-prebuild/cli.js @@ -71,6 +71,11 @@ const cli = yargs describe: 'Require Hermes headers when composing the ReactNativeHeaders XCFramework.', }) + .option('mergeable', { + type: 'boolean', + describe: + 'Build Release frameworks as mergeable libraries, which Xcode can merge into the app executable.', + }) .help(); /** @@ -86,6 +91,7 @@ async function getCLIConfiguration() /*: Promise, identity: ?string, requireHermes: boolean, + mergeable: boolean, }> */ { // Run input parsing const argv = await cli.argv; @@ -131,6 +137,7 @@ async function getCLIConfiguration() /*: Promise { ), ); }); + + describe('mergeable', () => { + const releaseCommand = (mergeable /*: boolean */) => + getPlatformBuildCommand( + 'ReactNativeDependencies', + 'Release', + 'iOS', + 'build', + mergeable, + ); + + it('makes Release dynamic frameworks mergeable', () => { + expect(releaseCommand(true)).toBe( + `${releaseCommand(false)} 'MERGEABLE_LIBRARY=$(MERGEABLE_LIBRARY_$(MACH_O_TYPE))' MERGEABLE_LIBRARY_mh_dylib=YES`, + ); + }); + + it('leaves the Release command unchanged when off', () => { + // The spaces come from the line continuations in the command template. + expect(releaseCommand(false)).toBe( + 'xcodebuild -scheme "ReactNativeDependencies" -destination "generic/platform=iOS" ' + + '-derivedDataPath "build" -configuration "Release" ' + + 'SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES DEBUG_INFORMATION_FORMAT=dwarf-with-dsym ' + + 'GCC_OPTIMIZATION_LEVEL=2', + ); + }); + + it('leaves the Debug command unchanged', () => { + const debugCommand = (mergeable /*: boolean */) => + getPlatformBuildCommand( + 'ReactNativeDependencies', + 'Debug', + 'iOS', + 'build', + mergeable, + ); + + expect(debugCommand(true)).toBe(debugCommand(false)); + expect(debugCommand(true)).not.toContain('MERGEABLE_LIBRARY'); + }); + }); }); diff --git a/scripts/releases/ios-prebuild/__tests__/cli-test.js b/scripts/releases/ios-prebuild/__tests__/cli-test.js new file mode 100644 index 000000000000..ef4323417d02 --- /dev/null +++ b/scripts/releases/ios-prebuild/__tests__/cli-test.js @@ -0,0 +1,41 @@ +/** + * 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 + * @noflow + */ + +'use strict'; + +describe('getCLIConfiguration', () => { + const originalArgv = process.argv; + + afterEach(() => { + process.argv = originalArgv; + }); + + // yargs reads process.argv when it is first required. + const parse = args => { + process.argv = ['node', 'prepare-ios-prebuilds.js', ...args]; + let getCLIConfiguration; + jest.isolateModules(() => { + ({getCLIConfiguration} = require('../cli')); + }); + return getCLIConfiguration(); + }; + + it('reads --mergeable', async () => { + expect( + await parse(['--configuration', 'Release', '--mergeable']), + ).toMatchObject({mergeable: true}); + }); + + it('leaves mergeable off by default', async () => { + expect(await parse(['--configuration', 'Release'])).toMatchObject({ + mergeable: false, + }); + }); +}); diff --git a/scripts/releases/ios-prebuild/build.js b/scripts/releases/ios-prebuild/build.js index 5296e3872424..dad01f29f471 100644 --- a/scripts/releases/ios-prebuild/build.js +++ b/scripts/releases/ios-prebuild/build.js @@ -26,6 +26,7 @@ async function buildDepenencies( destinations /*: ReadonlyArray */, rootFolder /*: string */, buildFolder /*: string */, + mergeable /*: boolean */, ) { console.log('✅ Building dependencies...'); @@ -37,6 +38,7 @@ async function buildDepenencies( destination, rootFolder, buildFolder, + mergeable, ), ), ); @@ -51,6 +53,7 @@ async function buildPlatform( destination /*: Destination */, rootFolder /*: string */, buildFolder /*: string */, + mergeable /*: boolean */, ) { console.log(`Building ${destination}...`); const command = getPlatformBuildCommand( @@ -58,16 +61,23 @@ async function buildPlatform( configuration, destination, buildFolder, + mergeable, ); execSync(command, {cwd: rootFolder, stdio: 'inherit'}); } +// MERGEABLE_LIBRARY=YES would also reach static library targets such as glog +// and break their link, so enable it only where MACH_O_TYPE is mh_dylib. +const MERGEABLE_SETTINGS = + "'MERGEABLE_LIBRARY=$(MERGEABLE_LIBRARY_$(MACH_O_TYPE))' MERGEABLE_LIBRARY_mh_dylib=YES"; + function getPlatformBuildCommand( scheme /*: string */, configuration /*: string */, destination /*: Destination */, buildFolder /*: string */, + mergeable /*: boolean */, ) /*: string */ { const command = `xcodebuild -scheme "${scheme}" -destination "generic/platform=${destination}" ` + @@ -76,12 +86,14 @@ function getPlatformBuildCommand( 'SKIP_INSTALL=NO \ BUILD_LIBRARY_FOR_DISTRIBUTION=YES \ DEBUG_INFORMATION_FORMAT=dwarf-with-dsym'; + if (configuration !== 'Release') { + return command; + } // Xcode defaults Release to -Os. Android builds these dependencies at -O2 in // Release. A command-line setting applies to every configuration, so add it // only to Release builds. - return configuration === 'Release' - ? `${command} GCC_OPTIMIZATION_LEVEL=2` - : command; + const releaseCommand = `${command} GCC_OPTIMIZATION_LEVEL=2`; + return mergeable ? `${releaseCommand} ${MERGEABLE_SETTINGS}` : releaseCommand; } module.exports = { diff --git a/scripts/releases/ios-prebuild/cli.js b/scripts/releases/ios-prebuild/cli.js index e90069a736d9..5adeef0fdbc5 100644 --- a/scripts/releases/ios-prebuild/cli.js +++ b/scripts/releases/ios-prebuild/cli.js @@ -80,6 +80,11 @@ const cli = yargs describe: 'Specify the code signing identity to use for signing the frameworks.', }) + .option('mergeable', { + type: 'boolean', + describe: + 'Build Release frameworks as mergeable libraries, which Xcode can merge into the app executable.', + }) .help(); /** @@ -96,6 +101,7 @@ async function getCLIConfiguration() /*: Promise, configuration: string, identity: ?string, + mergeable: boolean, }> */ { // Run input parsing const argv = await cli.argv; @@ -150,6 +156,7 @@ async function getCLIConfiguration() /*: Promise