Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/react-native/scripts/ios-prebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async function main() {
buildType,
destination,
outputFolder,
cli.mergeable,
);
});
}
Expand Down
19 changes: 10 additions & 9 deletions packages/react-native/scripts/ios-prebuild/__docs__/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 40 additions & 0 deletions packages/react-native/scripts/ios-prebuild/__tests__/build-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
41 changes: 41 additions & 0 deletions packages/react-native/scripts/ios-prebuild/__tests__/cli-test.js
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
16 changes: 13 additions & 3 deletions packages/react-native/scripts/ios-prebuild/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions packages/react-native/scripts/ios-prebuild/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/**
Expand All @@ -86,6 +91,7 @@ async function getCLIConfiguration() /*: Promise<?{
destinations: ReadonlyArray<Destination>,
identity: ?string,
requireHermes: boolean,
mergeable: boolean,
}> */ {
// Run input parsing
const argv = await cli.argv;
Expand Down Expand Up @@ -131,6 +137,7 @@ async function getCLIConfiguration() /*: Promise<?{
destinations: resolvedPlatforms,
identity: argv.identity,
requireHermes: argv.requireHermes ?? false,
mergeable: argv.mergeable ?? false,
};
}

Expand Down
23 changes: 12 additions & 11 deletions scripts/releases/ios-prebuild/__docs__/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ If no options are passed, the script executes all the steps in this order:

**Options:**

| Option | Short | Description |
| ------------------ | ----: | ------------------------------------------------------- |
| `--setup` | `-s` | Download and setup dependencies |
| `--build` | `-b` | Build dependencies for target platforms |
| `--compose` | `-c` | Compose XCFrameworks from built artifacts |
| `--swiftpackage` | `-w` | Generate `Package.swift` file |
| `--platforms` | `-p` | Target platforms (ios, macos, catalyst, tvos, visionos) |
| `--configurations` | `-g` | Build configurations (Debug, Release) |
| `--dependencies` | `-d` | Specific dependencies to process |
| `--clean` | — | Clean build folder before building |
| `--identity` | `-i` | Signing identity for frameworks |
| Option | Short | Description |
| ------------------ | ----: | --------------------------------------------------------------------------------------------------- |
| `--setup` | `-s` | Download and setup dependencies |
| `--build` | `-b` | Build dependencies for target platforms |
| `--compose` | `-c` | Compose XCFrameworks from built artifacts |
| `--swiftpackage` | `-w` | Generate `Package.swift` file |
| `--platforms` | `-p` | Target platforms (ios, macos, catalyst, tvos, visionos) |
| `--configurations` | `-g` | Build configurations (Debug, Release) |
| `--dependencies` | `-d` | Specific dependencies to process |
| `--clean` | — | Clean build folder before building |
| `--identity` | `-i` | Signing identity for frameworks |
| `--mergeable` | — | Build Release frameworks that Xcode can merge into the app executable. Release only, off by default |

## Integrating in your project with Cocoapods

Expand Down
41 changes: 41 additions & 0 deletions scripts/releases/ios-prebuild/__tests__/build-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,45 @@ describe('getPlatformBuildCommand', () => {
),
);
});

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');
});
});
});
41 changes: 41 additions & 0 deletions scripts/releases/ios-prebuild/__tests__/cli-test.js
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
18 changes: 15 additions & 3 deletions scripts/releases/ios-prebuild/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async function buildDepenencies(
destinations /*: ReadonlyArray<Destination> */,
rootFolder /*: string */,
buildFolder /*: string */,
mergeable /*: boolean */,
) {
console.log('✅ Building dependencies...');

Expand All @@ -37,6 +38,7 @@ async function buildDepenencies(
destination,
rootFolder,
buildFolder,
mergeable,
),
),
);
Expand All @@ -51,23 +53,31 @@ async function buildPlatform(
destination /*: Destination */,
rootFolder /*: string */,
buildFolder /*: string */,
mergeable /*: boolean */,
) {
console.log(`Building ${destination}...`);
const command = getPlatformBuildCommand(
scheme,
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}" ` +
Expand All @@ -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 = {
Expand Down
Loading
Loading