Skip to content

Commit 0ff4b67

Browse files
[native_toolchain_c] Broaden compiler tool discovery (#2657)
1 parent 3ec5735 commit 0ff4b67

File tree

3 files changed

+39
-39
lines changed

3 files changed

+39
-39
lines changed

pkgs/native_toolchain_c/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Made `CBuilder.run` `Logger` argument optional. It now defaults to a logger
44
printing to stdout and stderr. (Technically this is a breaking change on
55
passing `null` explicitly, but I doubt anyone is using it like that.)
6+
- Broaden compiler tool discovery
67

78
## 0.17.1
89

pkgs/native_toolchain_c/lib/src/cbuilder/compiler_resolver.dart

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ class CompilerResolver {
3939
var result = await _tryLoadCompilerFromInput();
4040

4141
// Then, try to detect on the host machine.
42-
final tool = _selectCompiler();
43-
if (tool != null) {
44-
result ??= await _tryLoadToolFromNativeToolchain(tool);
42+
for (final possibleTool in _selectPossibleCompilers()) {
43+
result ??= await _tryLoadToolFromNativeToolchain(possibleTool);
4544
}
4645

4746
if (result != null) {
@@ -57,46 +56,36 @@ class CompilerResolver {
5756
throw ToolError(errorMessage);
5857
}
5958

60-
/// Select the right compiler for cross compiling to the specified target.
61-
Tool? _selectCompiler() {
59+
/// Select possible compilers for cross compiling to the specified target.
60+
Iterable<Tool> _selectPossibleCompilers() sync* {
6261
final targetOS = codeConfig.targetOS;
6362
final targetArch = codeConfig.targetArchitecture;
6463

65-
// TODO(dacoharkes): Support falling back on other tools.
66-
if (targetArch == hostArchitecture &&
67-
targetOS == hostOS &&
68-
hostOS == OS.linux) {
69-
return clang;
70-
}
71-
if (targetOS == OS.macOS || targetOS == OS.iOS) return appleClang;
72-
if (targetOS == OS.android) return androidNdkClang;
73-
if (hostOS == OS.linux) {
74-
switch (targetArch) {
75-
case Architecture.arm:
76-
return armLinuxGnueabihfGcc;
77-
case Architecture.arm64:
78-
return aarch64LinuxGnuGcc;
79-
case Architecture.ia32:
80-
return i686LinuxGnuGcc;
81-
case Architecture.x64:
82-
return x86_64LinuxGnuGcc;
83-
case Architecture.riscv64:
84-
return riscv64LinuxGnuGcc;
85-
}
86-
}
87-
88-
if (hostOS == OS.windows) {
89-
switch (targetArch) {
90-
case Architecture.ia32:
91-
return clIA32;
92-
case Architecture.arm64:
93-
return clArm64;
94-
case Architecture.x64:
95-
return cl;
96-
}
64+
switch ((hostOS, targetOS, targetArch)) {
65+
case (_, OS.android, _):
66+
yield androidNdkClang;
67+
case (OS.macOS, OS.macOS || OS.iOS, _):
68+
yield appleClang;
69+
yield clang;
70+
case (OS.linux, OS.linux, _) when hostArchitecture == targetArch:
71+
yield clang;
72+
case (OS.linux, _, Architecture.arm):
73+
yield armLinuxGnueabihfGcc;
74+
case (OS.linux, _, Architecture.arm64):
75+
yield aarch64LinuxGnuGcc;
76+
case (OS.linux, _, Architecture.ia32):
77+
yield i686LinuxGnuGcc;
78+
case (OS.linux, _, Architecture.x64):
79+
yield x86_64LinuxGnuGcc;
80+
case (OS.linux, _, Architecture.riscv64):
81+
yield riscv64LinuxGnuGcc;
82+
case (OS.windows, _, Architecture.ia32):
83+
yield clIA32;
84+
case (OS.windows, _, Architecture.arm64):
85+
yield clArm64;
86+
case (OS.windows, _, Architecture.x64):
87+
yield cl;
9788
}
98-
99-
return null;
10089
}
10190

10291
Future<ToolInstance?> _tryLoadCompilerFromInput() async {

pkgs/native_toolchain_c/lib/src/native_toolchain/clang.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ final Tool llvmAr = Tool(
4848
wrappedResolver: clang.defaultResolver!,
4949
relativePath: Uri.file(OS.current.executableFileName('llvm-ar')),
5050
),
51+
RelativeToolResolver(
52+
toolName: 'LLVM archiver',
53+
wrappedResolver: clang.defaultResolver!,
54+
relativePath: Uri.file(OS.current.executableFileName('ar')),
55+
),
5156
PathToolResolver(
5257
toolName: 'LLVM archiver',
5358
executableName: OS.current.executableFileName('llvm-ar'),
@@ -68,6 +73,11 @@ final Tool lld = Tool(
6873
wrappedResolver: clang.defaultResolver!,
6974
relativePath: Uri.file(OS.current.executableFileName('ld.lld')),
7075
),
76+
RelativeToolResolver(
77+
toolName: 'LLD',
78+
wrappedResolver: clang.defaultResolver!,
79+
relativePath: Uri.file(OS.current.executableFileName('ld')),
80+
),
7181
PathToolResolver(
7282
toolName: 'LLD',
7383
executableName: OS.current.executableFileName('ld.lld'),

0 commit comments

Comments
 (0)