Skip to content

Commit 1b93620

Browse files
Pass EXCLUDED_ARCHS from Xcode project to xcodebuild for macOS builds (flutter#176948)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> Passes `EXCLUDED_ARCHS` from Xcode project build settings to the xcodebuild command, fixing macOS builds when dependencies don't support all architectures. ### Problem Release builds for macOS create universal binaries (arm64 + x86_64) by default. This causes build failures when using native dependencies that only provide binaries for a single architecture. While developers can set `EXCLUDED_ARCHS` in their Xcode project's xcconfig files, Swift Package Manager dependencies don't respect this project-level setting. **Fixes**: flutter#176605 ## Solution Instead of adding a CLI flag, pass `EXCLUDED_ARCHS` from the Xcode project's build settings directly to xcodebuild. This ensures Swift Package Manager dependencies respect architecture exclusions set in the project configuration. **How it works:** When `macos/Runner/Configs/Release.xcconfig` contains: ```xcconfig EXCLUDED_ARCHS = x86_64 ``` The build command now includes `EXCLUDED_ARCHS=x86_64`, creating an arm64-only binary. ## Usage **1. Edit your xcconfig file** (e.g., `macos/Runner/Configs/Release.xcconfig`): ```xcconfig #include "../../Flutter/Flutter-Release.xcconfig" #include "Warnings.xcconfig" // Exclude x86_64 for dependencies that only support Apple Silicon EXCLUDED_ARCHS = x86_64 ``` **2. Build normally:** ```bash flutter build macos --release ``` **Without exclusions (default behavior):** ```bash # Creates universal binary (arm64 + x86_64) flutter build macos --release ``` ## Implementation - Modified `buildMacOS()` to retrieve `EXCLUDED_ARCHS` from Xcode project build settings - Pass `EXCLUDED_ARCHS` to xcodebuild command when set and non-empty - Removed unused `activeArch` parameter - Added inline documentation explaining the SPM compatibility fix ## Testing **Manual verification:** - ✅ Built ExecuTorch Flutter example with `EXCLUDED_ARCHS = x86_64`: arm64-only binary (518.6MB) - ✅ Built without exclusions: universal binary (existing behavior) - ✅ Verified architecture with `lipo -info`: arm64-only when excluded **Unit tests:** - ✅ Added test: "Passes EXCLUDED_ARCHS from Xcode project to xcodebuild command" - ✅ Added test: "Does not pass EXCLUDED_ARCHS when not set in Xcode project" - ✅ All 25 macOS build tests passing *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Victoria Ashworth <15619084+vashworth@users.noreply.github.com>
1 parent 3f553f6 commit 1b93620

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed

packages/flutter_tools/lib/src/commands/build_macos.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class BuildMacosCommand extends BuildSubCommand {
5757
if (!supported) {
5858
throwToolExit('"build macos" only supported on macOS hosts.');
5959
}
60+
6061
await buildMacOS(
6162
flutterProject: project,
6263
buildInfo: buildInfo,

packages/flutter_tools/lib/src/macos/build_macos.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,21 @@ Future<void> buildMacOS({
197197
HostPlatform.darwin_x64 => 'x86_64',
198198
_ => throw UnimplementedError('Unsupported platform'),
199199
};
200-
final String destination = buildInfo.isDebug
201-
? 'platform=${XcodeSdk.MacOSX.displayName},arch=$arch'
202-
: XcodeSdk.MacOSX.genericPlatform;
200+
201+
// Determine the build destination
202+
final String destination;
203+
if (buildInfo.isDebug) {
204+
// Debug builds default to current host architecture
205+
destination = 'platform=${XcodeSdk.MacOSX.displayName},arch=$arch';
206+
} else {
207+
// Release builds default to universal binary
208+
destination = XcodeSdk.MacOSX.genericPlatform;
209+
}
210+
211+
// Get EXCLUDED_ARCHS from Xcode project build settings
212+
// This allows developers to exclude specific architectures (e.g., x86_64)
213+
// when dependencies don't support them
214+
final String? excludedArches = buildSettings['EXCLUDED_ARCHS'];
203215

204216
try {
205217
result = await globals.processUtils.stream(
@@ -223,6 +235,10 @@ Future<void> buildMacOS({
223235
'COMPILER_INDEX_STORE_ENABLE=NO',
224236
if (disabledSandboxEntitlementFile != null)
225237
'CODE_SIGN_ENTITLEMENTS=${disabledSandboxEntitlementFile.path}',
238+
// Pass EXCLUDED_ARCHS from Xcode project to xcodebuild command
239+
// This fixes Swift Package Manager not respecting EXCLUDED_ARCHS from the project
240+
if (excludedArches != null && excludedArches.trim().isNotEmpty)
241+
'EXCLUDED_ARCHS=$excludedArches',
226242
...environmentVariablesAsXcodeBuildSettings(globals.platform),
227243
],
228244
trace: true,

packages/flutter_tools/test/commands.shard/hermetic/build_macos_test.dart

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ class FakeXcodeProjectInterpreterWithProfile extends FakeXcodeProjectInterpreter
4141
}
4242
}
4343

44+
class FakeXcodeProjectInterpreterWithBuildSettings extends FakeXcodeProjectInterpreter {
45+
FakeXcodeProjectInterpreterWithBuildSettings({this.overrides = const <String, String>{}});
46+
47+
final Map<String, String> overrides;
48+
49+
@override
50+
Future<XcodeProjectInfo> getInfo(String projectPath, {String? projectFilename}) async {
51+
return XcodeProjectInfo(<String>['Runner'], <String>['Debug', 'Release'], <String>[
52+
'Runner',
53+
], BufferLogger.test());
54+
}
55+
56+
@override
57+
Future<Map<String, String>> getBuildSettings(
58+
String projectPath, {
59+
XcodeProjectBuildContext? buildContext,
60+
Duration timeout = const Duration(minutes: 1),
61+
}) async {
62+
return <String, String>{...overrides, 'PRODUCT_BUNDLE_IDENTIFIER': 'com.example.test'};
63+
}
64+
}
65+
4466
final Platform macosPlatform = FakePlatform(
4567
operatingSystem: 'macos',
4668
environment: <String, String>{'FLUTTER_ROOT': '/', 'HOME': '/'},
@@ -85,6 +107,9 @@ void main() {
85107

86108
// Sets up the minimal mock project files necessary for macOS builds to succeed.
87109
void createMinimalMockProjectFiles() {
110+
fileSystem
111+
.directory(fileSystem.path.join('macos', 'Runner.xcodeproj'))
112+
.createSync(recursive: true);
88113
fileSystem
89114
.directory(fileSystem.path.join('macos', 'Runner.xcworkspace'))
90115
.createSync(recursive: true);
@@ -1010,4 +1035,92 @@ STDERR STUFF
10101035
OperatingSystemUtils: () => FakeOperatingSystemUtils(hostPlatform: HostPlatform.darwin_x64),
10111036
},
10121037
);
1038+
1039+
testUsingContext(
1040+
'Passes EXCLUDED_ARCHS from Xcode project to xcodebuild command',
1041+
() async {
1042+
createMinimalMockProjectFiles();
1043+
1044+
fakeProcessManager.addCommands(<FakeCommand>[
1045+
setUpFakeXcodeBuildHandler(
1046+
'Release',
1047+
onRun: (_) {
1048+
fileSystem.file(fileSystem.path.join('macos', 'Flutter', 'ephemeral', '.app_filename'))
1049+
..createSync(recursive: true)
1050+
..writeAsStringSync('example.app');
1051+
},
1052+
additionalCommandArguments: <String>['EXCLUDED_ARCHS=x86_64'],
1053+
),
1054+
]);
1055+
1056+
final command = BuildCommand(
1057+
androidSdk: FakeAndroidSdk(),
1058+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
1059+
fileSystem: fileSystem,
1060+
logger: logger,
1061+
osUtils: FakeOperatingSystemUtils(),
1062+
);
1063+
1064+
await createTestCommandRunner(
1065+
command,
1066+
).run(<String>['build', 'macos', '--release', '--no-pub']);
1067+
1068+
expect(fakeProcessManager, hasNoRemainingExpectations);
1069+
},
1070+
overrides: <Type, Generator>{
1071+
Platform: () => macosPlatform,
1072+
FileSystem: () => fileSystem,
1073+
ProcessManager: () => fakeProcessManager,
1074+
Pub: ThrowingPub.new,
1075+
FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
1076+
XcodeProjectInterpreter: () => FakeXcodeProjectInterpreterWithBuildSettings(
1077+
overrides: <String, String>{'EXCLUDED_ARCHS': 'x86_64'},
1078+
),
1079+
OperatingSystemUtils: () => FakeOperatingSystemUtils(hostPlatform: HostPlatform.darwin_x64),
1080+
},
1081+
);
1082+
1083+
testUsingContext(
1084+
'Does not pass EXCLUDED_ARCHS when not set in Xcode project',
1085+
() async {
1086+
createMinimalMockProjectFiles();
1087+
1088+
fakeProcessManager.addCommands(<FakeCommand>[
1089+
setUpFakeXcodeBuildHandler(
1090+
'Release',
1091+
onRun: (_) {
1092+
fileSystem.file(fileSystem.path.join('macos', 'Flutter', 'ephemeral', '.app_filename'))
1093+
..createSync(recursive: true)
1094+
..writeAsStringSync('example.app');
1095+
},
1096+
// Note: No additionalCommandArguments - EXCLUDED_ARCHS should not be passed
1097+
),
1098+
]);
1099+
1100+
final command = BuildCommand(
1101+
androidSdk: FakeAndroidSdk(),
1102+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
1103+
fileSystem: fileSystem,
1104+
logger: logger,
1105+
osUtils: FakeOperatingSystemUtils(),
1106+
);
1107+
1108+
await createTestCommandRunner(
1109+
command,
1110+
).run(<String>['build', 'macos', '--release', '--no-pub']);
1111+
1112+
expect(fakeProcessManager, hasNoRemainingExpectations);
1113+
},
1114+
overrides: <Type, Generator>{
1115+
Platform: () => macosPlatform,
1116+
FileSystem: () => fileSystem,
1117+
ProcessManager: () => fakeProcessManager,
1118+
Pub: ThrowingPub.new,
1119+
FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
1120+
XcodeProjectInterpreter: () => FakeXcodeProjectInterpreterWithBuildSettings(
1121+
// Empty overrides - no EXCLUDED_ARCHS
1122+
),
1123+
OperatingSystemUtils: () => FakeOperatingSystemUtils(hostPlatform: HostPlatform.darwin_x64),
1124+
},
1125+
);
10131126
}

0 commit comments

Comments
 (0)