Skip to content

[WIP] Experiment #2351

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
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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.3

* Support linking for Android.

## 0.16.2

* Bump the SDK constraint to at least the one from `package:hooks` to fix
Expand Down
8 changes: 5 additions & 3 deletions pkgs/native_toolchain_c/lib/src/cbuilder/clinker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ class CLinker extends CTool implements Linker {
required LinkOutputBuilder output,
required Logger? logger,
}) async {
if (OS.current != OS.linux || input.config.code.targetOS != OS.linux) {
const supportedTargetOSs = [OS.linux, OS.android];
if (!supportedTargetOSs.contains(input.config.code.targetOS)) {
throw UnsupportedError(
'Currently, only linux is supported for this '
'feature. See also https://github.com/dart-lang/native/issues/1376',
'This feature is only supported when targeting '
'${supportedTargetOSs.join(', ')}. '
'See also https://github.com/dart-lang/native/issues/1376',
);
}
final outDir = input.outputDirectory;
Expand Down
88 changes: 0 additions & 88 deletions pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:logging/logging.dart';

import '../native_toolchain/android_ndk.dart';
import '../native_toolchain/apple_clang.dart';
import '../native_toolchain/clang.dart';
import '../native_toolchain/gcc.dart';
import '../native_toolchain/msvc.dart';
import '../native_toolchain/recognizer.dart';
Expand Down Expand Up @@ -63,11 +62,6 @@ class CompilerResolver {
final targetArch = codeConfig.targetArchitecture;

// TODO(dacoharkes): Support falling back on other tools.
if (targetArch == hostArchitecture &&
targetOS == hostOS &&
hostOS == OS.linux) {
return clang;
}
if (targetOS == OS.macOS || targetOS == OS.iOS) return appleClang;
if (targetOS == OS.android) return androidNdkClang;
if (hostOS == OS.linux) {
Expand Down Expand Up @@ -151,11 +145,6 @@ class CompilerResolver {
final targetArchitecture = codeConfig.targetArchitecture;

// TODO(dacoharkes): Support falling back on other tools.
if (targetArchitecture == hostArchitecture &&
targetOS == hostOS &&
hostOS == OS.linux) {
return llvmAr;
}
if (targetOS == OS.macOS || targetOS == OS.iOS) return appleAr;
if (targetOS == OS.android) return androidNdkLlvmAr;
if (hostOS == OS.linux) {
Expand Down Expand Up @@ -240,81 +229,4 @@ class CompilerResolver {
],
);
}

Future<ToolInstance> resolveLinker() async {
final targetOS = codeConfig.targetOS;
final targetArchitecture = codeConfig.targetArchitecture;
// First, check if the launcher provided a direct path to the compiler.
var result = await _tryLoadLinkerFromInput();

// Then, try to detect on the host machine.
final tool = _selectLinker();
if (tool != null) {
result ??= await _tryLoadToolFromNativeToolchain(tool);
}

if (result != null) {
return result;
}

final errorMessage =
"No linker configured on host '${hostOS}_$hostArchitecture' with "
"target '${targetOS}_$targetArchitecture'.";
logger?.severe(errorMessage);
throw ToolError(errorMessage);
}

Future<ToolInstance?> _tryLoadLinkerFromInput() async {
final inputLdUri = codeConfig.cCompiler?.linker;
if (inputLdUri != null) {
assert(await File.fromUri(inputLdUri).exists());
logger?.finer(
'Using linker ${inputLdUri.toFilePath()} '
'from cCompiler.ld.',
);
final tools = await LinkerRecognizer(inputLdUri).resolve(logger: logger);
return tools.first;
}
logger?.finer('No linker set in cCompiler.ld.');
return null;
}

/// Select the right compiler for cross compiling to the specified target.
Tool? _selectLinker() {
final targetOS = codeConfig.targetOS;
final targetArchitecture = codeConfig.targetArchitecture;

if (targetOS == OS.macOS || targetOS == OS.iOS) return appleLd;
if (targetOS == OS.android) return androidNdkLld;
if (hostOS == OS.linux) {
if (Architecture.current == targetArchitecture) {
return lld;
}
switch (targetArchitecture) {
case Architecture.arm:
return armLinuxGnueabihfLd;
case Architecture.arm64:
return aarch64LinuxGnuLd;
case Architecture.ia32:
return i686LinuxGnuLd;
case Architecture.x64:
return x86_64LinuxGnuLd;
case Architecture.riscv64:
return riscv64LinuxGnuLd;
}
}

if (hostOS == OS.windows) {
switch (targetArchitecture) {
case Architecture.ia32:
return linkIA32;
case Architecture.arm64:
return linkArm64;
case Architecture.x64:
return msvcLink;
}
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class LinkerOptions {
}) : _linkerFlags = <String>[
...flags ?? [],
'--strip-debug',
if (symbols != null) ...symbols.expand((e) => ['-u', e]),
if (symbols != null) ...symbols.map((e) => '-u,$e'),
].toList(),
gcSections = true,
_wholeArchiveSandwich = symbols == null,
Expand Down
11 changes: 2 additions & 9 deletions pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ class RunCBuilder {

Future<Uri> archiver() async => (await _resolver.resolveArchiver()).uri;

Future<ToolInstance> linker() async => await _resolver.resolveLinker();

Future<Uri> iosSdk(IOSSdk iosSdk, {required Logger? logger}) async {
if (iosSdk == IOSSdk.iPhoneOS) {
return (await iPhoneOSSdk.defaultResolver!.resolve(
Expand All @@ -119,9 +117,7 @@ class RunCBuilder {
compiler.uri.resolve('../sysroot/');

Future<void> run() async {
final toolInstance_ = linkerOptions != null
? await linker()
: await compiler();
final toolInstance_ = await compiler();
final tool = toolInstance_.tool;
if (tool.isClangLike || tool.isLdLike) {
await runClangLike(tool: toolInstance_);
Expand Down Expand Up @@ -333,10 +329,7 @@ class RunCBuilder {
// During bundling code assets are all placed in the same directory.
// Setting this rpath allows the binary to find other code assets
// it is linked against.
if (linkerOptions != null)
'-rpath=\$ORIGIN'
else
'-Wl,-rpath=\$ORIGIN',
'-Wl,-rpath=\$ORIGIN',
for (final directory in libraryDirectories)
'-L${directory.toFilePath()}',
for (final library in libraries) '-l$library',
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.2
version: 0.16.3
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 @@ -7,7 +7,6 @@ import 'dart:io';
import 'package:code_assets/code_assets.dart';
import 'package:hooks/hooks.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';
import 'package:native_toolchain_c/src/utils/run_process.dart';
import 'package:test/test.dart';

import '../helpers.dart';
Expand All @@ -21,20 +20,6 @@ void main() {
Architecture.riscv64,
];

const objdumpFileFormat = {
Architecture.arm: 'elf32-littlearm',
Architecture.arm64: 'elf64-littleaarch64',
Architecture.ia32: 'elf32-i386',
Architecture.x64: 'elf64-x86-64',
Architecture.riscv64: 'elf64-littleriscv',
};

/// From https://docs.flutter.dev/reference/supported-platforms.
const flutterAndroidNdkVersionLowestSupported = 21;

/// From https://docs.flutter.dev/reference/supported-platforms.
const flutterAndroidNdkVersionHighestSupported = 34;

const optimizationLevels = OptimizationLevel.values;
var selectOptimizationLevel = 0;

Expand All @@ -58,21 +43,7 @@ void main() {
linkMode,
optimizationLevel: optimizationLevel,
);
if (Platform.isLinux) {
final machine = await readelfMachine(libUri.path);
expect(machine, contains(readElfMachine[target]));
} else if (Platform.isMacOS) {
final result = await runProcess(
executable: Uri.file('objdump'),
arguments: ['-T', libUri.path],
logger: logger,
);
expect(result.exitCode, 0);
final machine = result.stdout
.split('\n')
.firstWhere((e) => e.contains('file format'));
expect(machine, contains(objdumpFileFormat[target]));
}
await expectMachineArchitecture(libUri, target);
if (linkMode == DynamicLoadingBundled()) {
await expectPageSize(libUri, 16 * 1024);
}
Expand Down
14 changes: 9 additions & 5 deletions pkgs/native_toolchain_c/test/clinker/build_testfiles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import '../helpers.dart';
Future<Uri> buildTestArchive(
Uri tempUri,
Uri tempUri2,
OS os,
Architecture architecture,
) async {
OS targetOS,
Architecture architecture, {
int? androidTargetNdkApi, // Must be specified iff targetOS is OS.android.
}) async {
assert((targetOS != OS.android) == (androidTargetNdkApi == null));
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 All @@ -27,7 +29,6 @@ Future<Uri> buildTestArchive(
final logMessages = <String>[];
final logger = createCapturingLogger(logMessages);

assert(os == OS.linux); // Setup code input for other OSes.
final buildInputBuilder = BuildInputBuilder()
..setupShared(
packageName: name,
Expand All @@ -38,10 +39,13 @@ Future<Uri> buildTestArchive(
..config.setupBuild(linkingEnabled: false)
..addExtension(
CodeAssetExtension(
targetOS: os,
targetOS: targetOS,
targetArchitecture: architecture,
linkModePreference: LinkModePreference.dynamic,
cCompiler: cCompiler,
android: androidTargetNdkApi != null
? AndroidCodeConfig(targetNdkApi: androidTargetNdkApi)
: null,
),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// 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.

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

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;

for (final apiLevel in [
flutterAndroidNdkVersionLowestSupported,
flutterAndroidNdkVersionHighestSupported,
]) {
group('Android API$apiLevel', () {
runObjectsTests(targetOS, architectures, androidTargetNdkApi: apiLevel);
});
}
}
32 changes: 32 additions & 0 deletions pkgs/native_toolchain_c/test/clinker/objects_cross_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// 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.
// See https://github.com/dart-lang/native/issues/1376.
@TestOn('linux')
library;

import 'dart:io';

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

import 'objects_helper.dart';

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

final architectures = [
Architecture.arm,
Architecture.arm64,
Architecture.ia32,
Architecture.x64,
Architecture.riscv64,
]..remove(Architecture.current);

runObjectsTests(OS.current, architectures);
}
Loading
Loading