Repository navigation
Compile React Native C/C++ at -O2 on iOS to match Android #59226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chrfalch
wants to merge
2
commits into
main
Choose a base branch
from
chrfalch/ios-cxx-optimization-level
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+302
−15
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/react-native/scripts/ios-prebuild/__tests__/build-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; | ||
|
|
||
| const {getSwiftPackageBuildCommand} = require('../build'); | ||
|
|
||
| describe('getSwiftPackageBuildCommand', () => { | ||
| it('builds Release at -O2', () => { | ||
| const command = getSwiftPackageBuildCommand('Release', 'iOS', 'output'); | ||
|
|
||
| expect(command).toContain('GCC_OPTIMIZATION_LEVEL=2'); | ||
| }); | ||
|
|
||
| it('leaves the Debug optimization level to the project', () => { | ||
| const command = getSwiftPackageBuildCommand('Debug', 'iOS', 'output'); | ||
|
|
||
| expect(command).not.toContain('GCC_OPTIMIZATION_LEVEL'); | ||
| }); | ||
|
|
||
| it.each(['Debug', 'Release'])('keeps the base %s command', buildType => { | ||
| const command = getSwiftPackageBuildCommand( | ||
| buildType, | ||
| 'iOS Simulator', | ||
| 'output', | ||
| ); | ||
|
|
||
| expect(command).toContain( | ||
| 'xcodebuild -scheme React -destination "generic/platform=iOS Simulator" -derivedDataPath "output" ' + | ||
| `-configuration "${buildType}" SKIP_INSTALL=NO BUILD_LIBRARY_FOR_DISTRIBUTION=YES OTHER_SWIFT_FLAGS="-no-verify-emitted-module-interface" ` + | ||
| 'DEBUG_INFORMATION_FORMAT="dwarf-with-dsym"', | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/react-native/scripts/spm/__tests__/codegen-template-optimization-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /** | ||
| * 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'; | ||
|
|
||
| const fs = require('node:fs'); | ||
| const path = require('node:path'); | ||
|
|
||
| const TEMPLATE = fs.readFileSync( | ||
| path.resolve( | ||
| __dirname, | ||
| '..', | ||
| '..', | ||
| 'codegen', | ||
| 'templates', | ||
| 'Package.swift.spm-template', | ||
| ), | ||
| 'utf8', | ||
| ); | ||
|
|
||
| const RELEASE_O2 = '.unsafeFlags(["-O2"], .when(configuration: .release))'; | ||
|
|
||
| // Returns the text between the bracket at `openIndex` and its matching close. | ||
| function balancedBody(source, openIndex) { | ||
| const open = source[openIndex]; | ||
| const close = {'(': ')', '[': ']'}[open]; | ||
| let depth = 0; | ||
| for (let i = openIndex; i < source.length; i++) { | ||
| if (source[i] === open) { | ||
| depth++; | ||
| } else if (source[i] === close && --depth === 0) { | ||
| return source.slice(openIndex + 1, i); | ||
| } | ||
| } | ||
| throw new Error(`Unbalanced ${open} at index ${openIndex}`); | ||
| } | ||
|
|
||
| function targetBlock(name) { | ||
| const re = /\.target\(/g; | ||
| let match; | ||
| while ((match = re.exec(TEMPLATE)) != null) { | ||
| const body = balancedBody(TEMPLATE, match.index + '.target'.length); | ||
| if (body.includes(`name: "${name}"`)) { | ||
| return body; | ||
| } | ||
| } | ||
| throw new Error(`Target ${name} not found in the codegen template`); | ||
| } | ||
|
|
||
| function settings(block, key) { | ||
| const start = block.indexOf(`${key}: [`); | ||
| if (start === -1) { | ||
| return null; | ||
| } | ||
| return balancedBody(block, start + `${key}: `.length); | ||
| } | ||
|
|
||
| describe('codegen Package.swift.spm-template optimization level', () => { | ||
| describe.each(['ReactCodegen', 'ReactAppDependencyProvider'])( | ||
| '%s', | ||
| targetName => { | ||
| it.each(['cSettings', 'cxxSettings'])( | ||
| 'compiles at -O2 in Release via %s', | ||
| key => { | ||
| expect(settings(targetBlock(targetName), key)).toContain(RELEASE_O2); | ||
| }, | ||
| ); | ||
| }, | ||
| ); | ||
|
|
||
| it('leaves the headers-only ReactAppHeaders target without flags', () => { | ||
| expect(targetBlock('ReactAppHeaders')).not.toContain('unsafeFlags'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /** | ||
| * 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'; | ||
|
|
||
| const {getPlatformBuildCommand} = require('../build'); | ||
|
|
||
| describe('getPlatformBuildCommand', () => { | ||
| it('builds Release at -O2', () => { | ||
| const command = getPlatformBuildCommand( | ||
| 'ReactNativeDependencies', | ||
| 'Release', | ||
| 'iOS', | ||
| 'build', | ||
| ); | ||
|
|
||
| expect(command).toContain('GCC_OPTIMIZATION_LEVEL=2'); | ||
| }); | ||
|
|
||
| it('leaves the Debug optimization level to the project', () => { | ||
| const command = getPlatformBuildCommand( | ||
| 'ReactNativeDependencies', | ||
| 'Debug', | ||
| 'iOS', | ||
| 'build', | ||
| ); | ||
|
|
||
| expect(command).not.toContain('GCC_OPTIMIZATION_LEVEL'); | ||
| }); | ||
|
|
||
| it.each(['Debug', 'Release'])('keeps the base %s command', configuration => { | ||
| const command = getPlatformBuildCommand( | ||
| 'ReactNativeDependencies', | ||
| configuration, | ||
| 'iOS Simulator', | ||
| 'build', | ||
| ); | ||
|
|
||
| expect(command).toMatch( | ||
| new RegExp( | ||
| '^xcodebuild -scheme "ReactNativeDependencies" -destination "generic/platform=iOS Simulator" ' + | ||
| '-derivedDataPath "build" ' + | ||
| `-configuration "${configuration}" ` + | ||
| 'SKIP_INSTALL=NO\\s+BUILD_LIBRARY_FOR_DISTRIBUTION=YES\\s+DEBUG_INFORMATION_FORMAT=dwarf-with-dsym', | ||
| ), | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.