Skip to content
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

Fix clinker some more #1400

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
24 changes: 17 additions & 7 deletions pkgs/native_toolchain_c/lib/src/cbuilder/linker_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import '../native_toolchain/apple_clang.dart';
import '../native_toolchain/clang.dart';
import '../native_toolchain/gcc.dart';
import '../tool/tool.dart';
import '../tool/tool_instance.dart';

/// Options to pass to the linker.
///
/// These can be manually set via the [LinkerOptions.manual] constructor.
/// Alternatively, if the goal of the linking is to treeshake unused symbols,
/// the [LinkerOptions.treeshake] constructor can be used.
class LinkerOptions {
static const defaultLibraries = ['gcc', 'c', 'm', 'gcc_s'];

/// The flags to be passed to the linker. As they depend on the linker being
/// invoked, the actual usage is via the [postSourcesFlags] method.
final List<String> _linkerFlags;
Expand Down Expand Up @@ -50,9 +53,14 @@ class LinkerOptions {
LinkerOptions.treeshake({
Iterable<String>? flags,
required Iterable<String>? symbols,
Iterable<String> libraries = defaultLibraries,
}) : _linkerFlags = <String>[
...flags ?? [],
'--strip-debug',
...[
'--as-needed',
...libraries.expand((e) => ['-l$e']),
],
if (symbols != null) ...symbols.expand((e) => ['-u', e]),
].toList(),
gcSections = true,
Expand Down Expand Up @@ -115,17 +123,19 @@ extension LinkerOptionsExt on LinkerOptions {
/// trick, which includes all symbols when linking object files.
///
/// Throws if the [linker] is not supported.
Iterable<String> postSourcesFlags(
Tool linker,
Future<Iterable<String>> postSourcesFlags(
ToolInstance linker,
Iterable<String> sourceFiles,
) =>
_toLinkerSyntax(linker, [
) async =>
_toLinkerSyntax(linker.tool, [
if (sourceFiles.any((source) => source.endsWith('.a')) ||
_wholeArchiveSandwich)
'--no-whole-archive',
..._linkerFlags,
...(await linker.tool.libraryPaths?.call() ?? [])
.expand((e) => ['-L$e']),
if (gcSections) '--gc-sections',
if (linkerScript != null)
'--version-script=${linkerScript!.toFilePath()}',
if (sourceFiles.any((source) => source.endsWith('.a')) ||
_wholeArchiveSandwich)
'--no-whole-archive',
]);
}
6 changes: 3 additions & 3 deletions pkgs/native_toolchain_c/lib/src/cbuilder/run_cbuilder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,14 @@ class RunCBuilder {
'-l',
cppLinkStdLib ?? defaultCppLinkStdLib[config.targetOS]!
],
...linkerOptions?.preSourcesFlags(toolInstance.tool, sourceFiles) ?? [],
...flags,
...linkerOptions?.preSourcesFlags(toolInstance.tool, sourceFiles) ?? [],
for (final MapEntry(key: name, :value) in defines.entries)
if (value == null) '-D$name' else '-D$name=$value',
for (final include in includes) '-I${include.toFilePath()}',
...sourceFiles,
...await linkerOptions?.postSourcesFlags(toolInstance, sourceFiles) ??
[],
if (language == Language.objectiveC) ...[
for (final framework in frameworks) ...[
'-framework',
Expand All @@ -303,8 +305,6 @@ class RunCBuilder {
'-o',
outFile!.toFilePath(),
],
...linkerOptions?.postSourcesFlags(toolInstance.tool, sourceFiles) ??
[],
],
logger: logger,
captureOutput: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ class _AndroidNdkResolver implements ToolResolver {
final installLocationResolver = PathVersionResolver(
wrappedResolver: ToolResolvers([
RelativeToolResolver(
toolName: 'Android NDK',
tool: Tool(name: 'Android NDK'),
wrappedResolver: PathToolResolver(
toolName: 'ndk-build',
tool: Tool(name: 'ndk-build'),
executableName: Platform.isWindows ? 'ndk-build.cmd' : 'ndk-build',
),
relativePath: Uri(path: ''),
),
InstallLocationResolver(
toolName: 'Android NDK',
tool: Tool(name: 'Android NDK'),
paths: [
if (Platform.isLinux) ...[
'\$HOME/Android/Sdk/ndk/*/',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final Tool appleClang = Tool(
cliArguments: ['--version'],
keepIf: ({required String stdout}) => stdout.contains('Apple clang'),
wrappedResolver: PathToolResolver(
toolName: 'Apple Clang',
tool: Tool(name: 'Apple Clang'),
executableName: 'clang',
),
),
Expand All @@ -27,7 +27,7 @@ final Tool appleAr = Tool(
name: 'Apple archiver',
defaultResolver: ToolResolvers([
RelativeToolResolver(
toolName: 'Apple archiver',
tool: Tool(name: 'Apple archiver'),
wrappedResolver: appleClang.defaultResolver!,
relativePath: Uri.file('ar'),
),
Expand All @@ -39,7 +39,7 @@ final Tool appleLd = Tool(
name: 'Apple linker',
defaultResolver: ToolResolvers([
RelativeToolResolver(
toolName: 'Apple linker',
tool: Tool(name: 'Apple linker'),
wrappedResolver: appleClang.defaultResolver!,
relativePath: Uri.file('ld'),
),
Expand All @@ -53,7 +53,7 @@ final Tool otool = Tool(
name: 'otool',
defaultResolver: CliVersionResolver(
wrappedResolver: PathToolResolver(
toolName: 'otool',
tool: Tool(name: 'otool'),
executableName: 'otool',
),
),
Expand Down
6 changes: 3 additions & 3 deletions pkgs/native_toolchain_c/lib/src/native_toolchain/clang.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final Tool clang = Tool(
cliArguments: ['--version'],
keepIf: ({required String stdout}) => !stdout.contains('Apple clang'),
wrappedResolver: PathToolResolver(
toolName: 'Clang',
tool: Tool(name: 'Clang'),
executableName: 'clang',
),
),
Expand All @@ -30,7 +30,7 @@ final Tool llvmAr = Tool(
defaultResolver: CliVersionResolver(
wrappedResolver: ToolResolvers([
RelativeToolResolver(
toolName: 'LLVM archiver',
tool: Tool(name: 'LLVM archiver'),
wrappedResolver: clang.defaultResolver!,
relativePath: Uri.file('llvm-ar'),
),
Expand All @@ -46,7 +46,7 @@ final Tool lld = Tool(
defaultResolver: CliVersionResolver(
wrappedResolver: ToolResolvers([
RelativeToolResolver(
toolName: 'LLD',
tool: Tool(name: 'LLD'),
wrappedResolver: clang.defaultResolver!,
relativePath: Uri.file('ld.lld'),
),
Expand Down
23 changes: 20 additions & 3 deletions pkgs/native_toolchain_c/lib/src/native_toolchain/gcc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// 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:logging/logging.dart';
import 'package:native_assets_cli/native_assets_cli.dart';

import '../tool/tool.dart';
import '../tool/tool_resolver.dart';
import '../utils/run_process.dart';

/// The GNU Compiler Collection for [Architecture.current].
///
Expand Down Expand Up @@ -77,7 +79,7 @@ Tool _gcc(String prefix) => Tool(
name: gcc.name,
defaultResolver: CliVersionResolver(
wrappedResolver: PathToolResolver(
toolName: gcc.name,
tool: gcc,
executableName: '$prefix-gcc',
),
),
Expand All @@ -88,7 +90,7 @@ Tool _gnuArchiver(String prefix) {
return Tool(
name: gnuArchiver.name,
defaultResolver: RelativeToolResolver(
toolName: gnuArchiver.name,
tool: gnuArchiver,
wrappedResolver: gcc.defaultResolver!,
relativePath: Uri.file('$prefix-gcc-ar'),
),
Expand All @@ -100,9 +102,24 @@ Tool _gnuLinker(String prefix) {
return Tool(
name: gnuLinker.name,
defaultResolver: RelativeToolResolver(
toolName: gnuLinker.name,
tool: Tool(
name: gnuLinker.name,
libraryPaths: () => parseSearchDirectoriesFor(Uri.file('$prefix-gcc')),
),
wrappedResolver: gcc.defaultResolver!,
relativePath: Uri.file('$prefix-ld'),
),
);
}

Future<List<String>> parseSearchDirectoriesFor(Uri uri) async {
final stdout = (await runProcess(
executable: uri,
arguments: ['-print-search-dirs'],
logger: Logger(''),
))
.stdout;
const pattern = 'libraries: =';
final start = stdout.indexOf(pattern);
return stdout.substring(start + pattern.length).split(':');
}
20 changes: 10 additions & 10 deletions pkgs/native_toolchain_c/lib/src/native_toolchain/msvc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ final Tool vswhere = Tool(
wrappedResolver: ToolResolvers(
[
PathToolResolver(
toolName: 'Visual Studio Locator',
tool: Tool(name: 'Visual Studio Locator'),
executableName: 'vswhere.exe',
),
InstallLocationResolver(
toolName: 'Visual Studio Locator',
tool: Tool(name: 'Visual Studio Locator'),
paths: [
'C:/Program Files \\(x86\\)/Microsoft Visual Studio/Installer/vswhere.exe',
'C:/Program Files/Microsoft Visual Studio/Installer/vswhere.exe',
Expand All @@ -52,7 +52,7 @@ final Tool msvc = Tool(
name: 'MSVC',
defaultResolver: PathVersionResolver(
wrappedResolver: RelativeToolResolver(
toolName: 'MSVC',
tool: Tool(name: 'MSVC'),
wrappedResolver: visualStudio.defaultResolver!,
relativePath: Uri(path: './VC/Tools/MSVC/*/'),
),
Expand Down Expand Up @@ -80,7 +80,7 @@ Tool vcvars(ToolInstance toolInstance) {
return Tool(
name: fileName,
defaultResolver: InstallLocationResolver(
toolName: fileName,
tool: Tool(name: fileName),
paths: [
Glob.quote(batchScript.toFilePath().replaceAll('\\', '/')),
],
Expand All @@ -91,7 +91,7 @@ Tool vcvars(ToolInstance toolInstance) {
final Tool vcvars64 = Tool(
name: 'vcvars64.bat',
defaultResolver: RelativeToolResolver(
toolName: 'vcvars64.bat',
tool: Tool(name: 'vcvars64.bat'),
wrappedResolver: visualStudio.defaultResolver!,
relativePath: Uri(path: './VC/Auxiliary/Build/vcvars64.bat'),
),
Expand All @@ -100,7 +100,7 @@ final Tool vcvars64 = Tool(
final Tool vcvars32 = Tool(
name: 'vcvars32.bat',
defaultResolver: RelativeToolResolver(
toolName: 'vcvars32.bat',
tool: Tool(name: 'vcvars32.bat'),
wrappedResolver: visualStudio.defaultResolver!,
relativePath: Uri(path: './VC/Auxiliary/Build/vcvars32.bat'),
),
Expand All @@ -114,7 +114,7 @@ final Tool vcvarsarm64 = Tool(
// emulation on windows-arm64.
name: 'vcvarsamd64_arm64.bat',
defaultResolver: RelativeToolResolver(
toolName: 'vcvarsamd64_arm64.bat',
tool: Tool(name: 'vcvarsamd64_arm64.bat'),
wrappedResolver: visualStudio.defaultResolver!,
relativePath: Uri(path: './VC/Auxiliary/Build/vcvarsamd64_arm64.bat'),
),
Expand All @@ -123,7 +123,7 @@ final Tool vcvarsarm64 = Tool(
final Tool vcvarsall = Tool(
name: 'vcvarsall.bat',
defaultResolver: RelativeToolResolver(
toolName: 'vcvars32.bat',
tool: Tool(name: 'vcvars32.bat'),
wrappedResolver: visualStudio.defaultResolver!,
relativePath: Uri(path: './VC/Auxiliary/Build/vcvarsall.bat'),
),
Expand All @@ -132,7 +132,7 @@ final Tool vcvarsall = Tool(
final Tool vsDevCmd = Tool(
name: 'VsDevCmd.bat',
defaultResolver: RelativeToolResolver(
toolName: 'VsDevCmd.bat',
tool: Tool(name: 'VsDevCmd.bat'),
wrappedResolver: visualStudio.defaultResolver!,
relativePath: Uri(path: './Common7/Tools/VsDevCmd.bat'),
),
Expand Down Expand Up @@ -246,7 +246,7 @@ Tool _msvcTool({
final hostArchName = _msvcArchNames[hostArchitecture]!;
final targetArchName = _msvcArchNames[targetArchitecture]!;
ToolResolver resolver = RelativeToolResolver(
toolName: executableName,
tool: Tool(name: executableName),
wrappedResolver: msvc.defaultResolver!,
relativePath: Uri(
path: 'bin/Host$hostArchName/$targetArchName/$executableName',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final Tool xcrun = Tool(
name: 'xcrun',
defaultResolver: CliVersionResolver(
wrappedResolver: PathToolResolver(
toolName: 'xcrun',
tool: Tool(name: 'xcrun'),
executableName: 'xcrun',
),
),
Expand Down
3 changes: 3 additions & 0 deletions pkgs/native_toolchain_c/lib/src/tool/tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ class Tool {

ToolResolver? defaultResolver;

final Future<Iterable<String>> Function()? libraryPaths;

Tool({
required this.name,
this.defaultResolver,
this.libraryPaths,
});

@override
Expand Down
Loading
Loading