Skip to content

[native_toolchain_c] Add linking for macOS #2360

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

Merged
merged 4 commits into from
Jun 16, 2025
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
4 changes: 4 additions & 0 deletions pkgs/native_toolchain_c/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.16.4

* Support linking for MacOS.

## 0.16.3

* Support linking for Android.
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_toolchain_c/lib/src/cbuilder/clinker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CLinker extends CTool implements Linker {
required LinkOutputBuilder output,
required Logger? logger,
}) async {
const supportedTargetOSs = [OS.linux, OS.android];
const supportedTargetOSs = [OS.linux, OS.android, OS.macOS];
if (!supportedTargetOSs.contains(input.config.code.targetOS)) {
throw UnsupportedError(
'This feature is only supported when targeting '
Expand Down
105 changes: 59 additions & 46 deletions pkgs/native_toolchain_c/lib/src/cbuilder/linker_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import 'dart:io';

import 'package:code_assets/code_assets.dart';

import '../native_toolchain/tool_likeness.dart';
import '../tool/tool.dart';

Expand All @@ -14,7 +16,7 @@ import '../tool/tool.dart';
/// the [LinkerOptions.treeshake] constructor can be used.
class LinkerOptions {
/// The flags to be passed to the linker. As they depend on the linker being
/// invoked, the actual usage is via the [postSourcesFlags] method.
/// invoked, the actual usage is via the [sourceFilesToFlags] method.
final List<String> _linkerFlags;

/// Enable garbage collection of unused input sections.
Expand All @@ -27,37 +29,38 @@ class LinkerOptions {
/// See also the `ld` man page at https://linux.die.net/man/1/ld.
final Uri? linkerScript;

/// Whether to include all symbols from the sources.
/// Whether to strip debugging symbols from the binary.
final bool stripDebug;

/// The symbols to keep in the resulting binaries.
///
/// This is achieved by setting the `whole-archive` flag before passing the
/// sources, and the `no-whole-archive` flag after.
final bool _wholeArchiveSandwich;
/// If null all symbols will be kept.
final List<String>? _symbolsToKeep;

/// Create linking options manually for fine-grained control.
LinkerOptions.manual({
List<String>? flags,
bool? gcSections,
this.linkerScript,
this.stripDebug = true,
Iterable<String>? symbolsToKeep,
}) : _linkerFlags = flags ?? [],
gcSections = gcSections ?? true,
_wholeArchiveSandwich = false;
_symbolsToKeep = symbolsToKeep?.toList(growable: false);

/// Create linking options to tree-shake symbols from the input files.
///
/// The [symbols] specify the symbols which should be kept.
LinkerOptions.treeshake({
Iterable<String>? flags,
required Iterable<String>? symbols,
}) : _linkerFlags = <String>[
...flags ?? [],
'--strip-debug',
if (symbols != null) ...symbols.map((e) => '-u,$e'),
].toList(),
this.stripDebug = true,
}) : _linkerFlags = flags?.toList(growable: false) ?? [],
_symbolsToKeep = symbols?.toList(growable: false),
gcSections = true,
_wholeArchiveSandwich = symbols == null,
linkerScript = _createLinkerScript(symbols);

Iterable<String> _toLinkerSyntax(Tool linker, List<String> flagList) {
Iterable<String> _toLinkerSyntax(Tool linker, Iterable<String> flagList) {
if (linker.isClangLike) {
return flagList.map((e) => '-Wl,$e');
} else if (linker.isLdLike) {
Expand Down Expand Up @@ -85,38 +88,48 @@ class LinkerOptions {
}

extension LinkerOptionsExt on LinkerOptions {
/// The flags for the specified [linker], which are inserted _before_ the
/// sources.
///
/// This is mainly used for the whole-archive ... no-whole-archive
/// trick, which includes all symbols when linking object files.
///
/// Throws if the [linker] is not supported.
Iterable<String> preSourcesFlags(Tool linker, Iterable<String> sourceFiles) =>
_toLinkerSyntax(
linker,
sourceFiles.any((source) => source.endsWith('.a')) ||
_wholeArchiveSandwich
? ['--whole-archive']
: [],
);

/// The flags for the specified [linker], which are inserted _after_ the
/// sources.
///
/// This is mainly used for the whole-archive ... no-whole-archive
/// trick, which includes all symbols when linking object files.
///
/// Throws if the [linker] is not supported.
Iterable<String> postSourcesFlags(
Tool linker,
/// Takes [sourceFiles] and turns it into flags for the compiler driver while
/// considering the current [LinkerOptions].
Iterable<String> sourceFilesToFlags(
Tool tool,
Iterable<String> sourceFiles,
) => _toLinkerSyntax(linker, [
..._linkerFlags,
if (gcSections) '--gc-sections',
if (linkerScript != null) '--version-script=${linkerScript!.toFilePath()}',
if (sourceFiles.any((source) => source.endsWith('.a')) ||
_wholeArchiveSandwich)
'--no-whole-archive',
]);
OS targetOS,
) {
final includeAllSymbols = _symbolsToKeep == null;

switch (targetOS) {
case OS.macOS:
return [
if (!includeAllSymbols) ...sourceFiles,
..._toLinkerSyntax(tool, [
if (includeAllSymbols) ...sourceFiles.map((e) => '-force_load,$e'),
..._linkerFlags,
..._symbolsToKeep?.map((symbol) => '-u,_$symbol') ?? [],
if (stripDebug) '-S',
if (gcSections) '-dead_strip',
]),
];

case OS.android || OS.linux:
final wholeArchiveSandwich =
sourceFiles.any((source) => source.endsWith('.a')) ||
includeAllSymbols;
return [
if (wholeArchiveSandwich)
..._toLinkerSyntax(tool, ['--whole-archive']),
...sourceFiles,
..._toLinkerSyntax(tool, [
..._linkerFlags,
..._symbolsToKeep?.map((symbol) => '-u,$symbol') ?? [],
if (stripDebug) '--strip-debug',
if (gcSections) '--gc-sections',
if (linkerScript != null)
'--version-script=${linkerScript!.toFilePath()}',
if (wholeArchiveSandwich) '--no-whole-archive',
]),
];
case OS():
throw UnimplementedError();
}
}
}
18 changes: 11 additions & 7 deletions pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ class RunCBuilder {
if (dynamicLibrary != null) '-fPIC',
// Using PIC for static libraries allows them to be linked into
// any executable, but it is not necessarily the best option in
// terms of overhead. We would have to know wether the target into
// which the static library is linked is PIC, PIE or neither. Then
// we could use the same option for the static library.
// terms of overhead. We would have to know whether the target
// into which the static library is linked is PIC, PIE or neither.
// Then we could use the same option for the static library.
if (staticLibrary != null) '-fPIC',
if (executable != null) ...[
// Generate position-independent code for executables.
Expand Down Expand Up @@ -296,7 +296,6 @@ class RunCBuilder {
],
if (optimizationLevel != OptimizationLevel.unspecified)
optimizationLevel.clangFlag(),
...linkerOptions?.preSourcesFlags(toolInstance.tool, sourceFiles) ?? [],
// Support Android 15 page size by default, can be overridden by
// passing [flags].
if (codeConfig.targetOS == OS.android) '-Wl,-z,max-page-size=16384',
Expand All @@ -306,7 +305,14 @@ class RunCBuilder {
for (final include in includes) '-I${include.toFilePath()}',
for (final forcedInclude in forcedIncludes)
'-include${forcedInclude.toFilePath()}',
...sourceFiles,
if (linkerOptions != null)
...linkerOptions!.sourceFilesToFlags(
toolInstance.tool,
sourceFiles,
codeConfig.targetOS,
)
else
...sourceFiles,
if (language == Language.objectiveC) ...[
for (final framework in frameworks) ...['-framework', framework],
],
Expand All @@ -322,8 +328,6 @@ class RunCBuilder {
'-o',
outFile!.toFilePath(),
],
...linkerOptions?.postSourcesFlags(toolInstance.tool, sourceFiles) ??
[],
if (executable != null || dynamicLibrary != null) ...[
if (codeConfig.targetOS case OS.android || OS.linux)
// During bundling code assets are all placed in the same directory.
Expand Down
2 changes: 1 addition & 1 deletion pkgs/native_toolchain_c/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: native_toolchain_c
description: >-
A library to invoke the native C compiler installed on the host machine.
version: 0.16.3
version: 0.16.4
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_toolchain_c

topics:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void main() {
linkMode,
optimizationLevel: optimizationLevel,
);
await expectMachineArchitecture(libUri, target);
await expectMachineArchitecture(libUri, target, OS.android);
if (linkMode == DynamicLoadingBundled()) {
await expectPageSize(libUri, 16 * 1024);
}
Expand Down
12 changes: 11 additions & 1 deletion pkgs/native_toolchain_c/test/clinker/build_testfiles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ Future<Uri> buildTestArchive(
OS targetOS,
Architecture architecture, {
int? androidTargetNdkApi, // Must be specified iff targetOS is OS.android.
int? macOSTargetVersion, // Must be specified iff targetOS is OS.macos.
}) async {
assert((targetOS != OS.android) == (androidTargetNdkApi == null));
if (targetOS == OS.android) {
ArgumentError.checkNotNull(androidTargetNdkApi, 'androidTargetNdkApi');
}
if (targetOS == OS.macOS) {
ArgumentError.checkNotNull(macOSTargetVersion, 'macOSTargetVersion');
}

final test1Uri = packageUri.resolve('test/clinker/testfiles/linker/test1.c');
final test2Uri = packageUri.resolve('test/clinker/testfiles/linker/test2.c');
if (!await File.fromUri(test1Uri).exists() ||
Expand Down Expand Up @@ -46,6 +53,9 @@ Future<Uri> buildTestArchive(
android: androidTargetNdkApi != null
? AndroidCodeConfig(targetNdkApi: androidTargetNdkApi)
: null,
macOS: macOSTargetVersion != null
? MacOSCodeConfig(targetVersion: macOSTargetVersion)
: null,
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@ import '../helpers.dart';
import 'objects_helper.dart';

void main() {
final architectures = [
Architecture.arm,
Architecture.arm64,
Architecture.ia32,
Architecture.x64,
Architecture.riscv64,
];

const targetOS = OS.android;
final architectures = supportedArchitecturesFor(targetOS);

for (final apiLevel in [
flutterAndroidNdkVersionLowestSupported,
Expand Down
22 changes: 11 additions & 11 deletions pkgs/native_toolchain_c/test/clinker/objects_cross_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

//TODO(mosuem): Enable for windows and mac.
// TODO(mosuem): Enable for windows.
// See https://github.com/dart-lang/native/issues/1376.
@TestOn('linux')
@TestOn('linux || mac-os')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Hm, they should have made a bunch of const objects and an Or([Linux(), MacOS()]) class instead of going for strings. How do we know if this syntax is correct and we're not just skipping the test everywhere because of wrong syntax. 😄 )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was paranoid about that as well and double-checked that the test runs. :D

library;

import 'dart:io';

import 'package:code_assets/code_assets.dart';
import 'package:test/test.dart';

import '../helpers.dart';
import 'objects_helper.dart';

void main() {
if (!Platform.isLinux) {
if (!Platform.isLinux && !Platform.isMacOS) {
// Avoid needing status files on Dart SDK CI.
return;
}

final architectures = [
Architecture.arm,
Architecture.arm64,
Architecture.ia32,
Architecture.x64,
Architecture.riscv64,
]..remove(Architecture.current);
final architectures = supportedArchitecturesFor(OS.current)
..remove(Architecture.current); // See objects_test.dart for current arch.

runObjectsTests(OS.current, architectures);
runObjectsTests(
OS.current,
architectures,
macOSTargetVersion: OS.current == OS.macOS ? defaultMacOSVersion : null,
);
}
15 changes: 13 additions & 2 deletions pkgs/native_toolchain_c/test/clinker/objects_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ void runObjectsTests(
OS targetOS,
List<Architecture> architectures, {
int? androidTargetNdkApi, // Must be specified iff targetOS is OS.android.
int? macOSTargetVersion, // Must be specified iff targetOS is OS.macos.
}) {
assert((targetOS != OS.android) == (androidTargetNdkApi == null));
if (targetOS == OS.android) {
ArgumentError.checkNotNull(androidTargetNdkApi, 'androidTargetNdkApi');
}
if (targetOS == OS.macOS) {
ArgumentError.checkNotNull(macOSTargetVersion, 'macOSTargetVersion');
}

const name = 'mylibname';

for (final architecture in architectures) {
Expand All @@ -31,6 +38,7 @@ void runObjectsTests(
targetOS,
architecture,
androidTargetNdkApi: androidTargetNdkApi,
macOSTargetVersion: macOSTargetVersion,
);

final linkInputBuilder = LinkInputBuilder()
Expand All @@ -50,6 +58,9 @@ void runObjectsTests(
android: androidTargetNdkApi != null
? AndroidCodeConfig(targetNdkApi: androidTargetNdkApi)
: null,
macOS: macOSTargetVersion != null
? MacOSCodeConfig(targetVersion: macOSTargetVersion)
: null,
),
);

Expand All @@ -70,7 +81,7 @@ void runObjectsTests(
final asset = codeAssets.first;
expect(asset, isA<CodeAsset>());
expect(
await nmReadSymbols(asset),
await nmReadSymbols(asset, targetOS),
stringContainsInOrder(['my_func', 'my_other_func']),
);
});
Expand Down
13 changes: 9 additions & 4 deletions pkgs/native_toolchain_c/test/clinker/objects_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

//TODO(mosuem): Enable for windows and mac.
// TODO(mosuem): Enable for windows.
// See https://github.com/dart-lang/native/issues/1376.
@TestOn('linux')
@TestOn('linux || mac-os')
library;

import 'dart:io';

import 'package:code_assets/code_assets.dart';
import 'package:test/test.dart';

import '../helpers.dart';
import 'objects_helper.dart';

void main() {
if (!Platform.isLinux) {
if (!Platform.isLinux && !Platform.isMacOS) {
// Avoid needing status files on Dart SDK CI.
return;
}

runObjectsTests(OS.current, [Architecture.current]);
runObjectsTests(
OS.current,
[Architecture.current],
macOSTargetVersion: OS.current == OS.macOS ? defaultMacOSVersion : null,
);
}
Loading
Loading