diff --git a/.github/workflows/native_toolchain_c.yaml b/.github/workflows/native_toolchain_c.yaml index bae4096539..6721ec1281 100644 --- a/.github/workflows/native_toolchain_c.yaml +++ b/.github/workflows/native_toolchain_c.yaml @@ -18,6 +18,44 @@ on: - cron: "0 0 * * 0" # weekly jobs: + dart-swiftly-clang: + strategy: + matrix: + os: [ubuntu] + sdk: [dev, stable] + package: [native_toolchain_c] + + runs-on: ${{ matrix.os }}-latest + + defaults: + run: + working-directory: pkgs/${{ matrix.package }} + + steps: + - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 + + - uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c + with: + sdk: ${{ matrix.sdk }} + + - name: Install swiftly dependencies + run: sudo apt-get update && sudo apt-get -y install libcurl4-openssl-dev + + - name: Install swiftly + run: | + curl -O https://download.swift.org/swiftly/linux/swiftly-$(uname -m).tar.gz + tar zxf swiftly-$(uname -m).tar.gz + ./swiftly init --quiet-shell-followup -y + echo "$SWIFTLY_BIN_DIR" >> $GITHUB_PATH + + - name: Install the latest Swift toolchain + run: swiftly install latest + + - run: echo "$SWIFTLY_BIN_DIR" >> $GITHUB_PATH + + - run: clang --version + + - run: dart test dart-sdk-clang: strategy: matrix: diff --git a/pkgs/hooks_runner/lib/src/build_runner/build_planner.dart b/pkgs/hooks_runner/lib/src/build_runner/build_planner.dart index f9ebbb505a..35afe8e42e 100644 --- a/pkgs/hooks_runner/lib/src/build_runner/build_planner.dart +++ b/pkgs/hooks_runner/lib/src/build_runner/build_planner.dart @@ -332,7 +332,8 @@ class PackageGraph { /// compilation. This enum holds static information about these hooks. enum Hook { link('link'), - build('build'); + build('build') + ; final String _scriptName; diff --git a/pkgs/native_toolchain_c/CHANGELOG.md b/pkgs/native_toolchain_c/CHANGELOG.md index 79a5affa38..779e1df681 100644 --- a/pkgs/native_toolchain_c/CHANGELOG.md +++ b/pkgs/native_toolchain_c/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.17.7 + +- Fixed resolution of C compiler and tools on macOS when `swiftly` is installed. +- Broaden compiler tool discovery on macOS. + ## 0.17.6 - On Android, use the NDK's `libc++.a` linker script when `cppLinkStdLib` is diff --git a/pkgs/native_toolchain_c/lib/src/native_toolchain/apple_clang.dart b/pkgs/native_toolchain_c/lib/src/native_toolchain/apple_clang.dart index 9c7eb4c99e..fd59529f04 100644 --- a/pkgs/native_toolchain_c/lib/src/native_toolchain/apple_clang.dart +++ b/pkgs/native_toolchain_c/lib/src/native_toolchain/apple_clang.dart @@ -2,6 +2,8 @@ // 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 '../tool/tool.dart'; import '../tool/tool_resolver.dart'; @@ -13,7 +15,8 @@ final Tool appleClang = Tool( defaultResolver: CliVersionResolver( wrappedResolver: CliFilter( cliArguments: ['--version'], - keepIf: ({required String stdout}) => stdout.contains('Apple clang'), + keepIf: ({required String stdout, required String stderr}) => + stdout.contains('Apple clang'), wrappedResolver: PathToolResolver( toolName: 'Apple Clang', executableName: 'clang', @@ -31,6 +34,14 @@ final Tool appleAr = Tool( wrappedResolver: appleClang.defaultResolver!, relativePath: Uri.file('ar'), ), + PathFilter( + toolName: 'Apple archiver', + wrappedResolver: PathToolResolver( + toolName: 'Apple archiver', + executableName: OS.current.executableFileName('ar'), + ), + keepIf: ({required uri}) => uri == Uri.file('/usr/bin/ar'), + ), ]), ); @@ -43,6 +54,15 @@ final Tool appleLd = Tool( wrappedResolver: appleClang.defaultResolver!, relativePath: Uri.file('ld'), ), + CliFilter( + wrappedResolver: PathToolResolver( + toolName: 'Apple linker', + executableName: OS.current.executableFileName('ld'), + ), + cliArguments: ['-v'], + keepIf: ({required String stdout, required String stderr}) => + stdout.contains('Apple TAPI') || stderr.contains('Apple TAPI'), + ), ]), ); diff --git a/pkgs/native_toolchain_c/lib/src/native_toolchain/clang.dart b/pkgs/native_toolchain_c/lib/src/native_toolchain/clang.dart index 41c47f28f1..8556d4f8d5 100644 --- a/pkgs/native_toolchain_c/lib/src/native_toolchain/clang.dart +++ b/pkgs/native_toolchain_c/lib/src/native_toolchain/clang.dart @@ -16,7 +16,8 @@ final Tool clang = Tool( defaultResolver: CliVersionResolver( wrappedResolver: CliFilter( cliArguments: ['--version'], - keepIf: ({required String stdout}) => !stdout.contains('Apple clang'), + keepIf: ({required String stdout, required String stderr}) => + !stdout.contains('Apple clang'), wrappedResolver: ToolResolvers([ PathToolResolver( toolName: 'Clang', diff --git a/pkgs/native_toolchain_c/lib/src/native_toolchain/recognizer.dart b/pkgs/native_toolchain_c/lib/src/native_toolchain/recognizer.dart index 73a0fb689a..5b98942acc 100644 --- a/pkgs/native_toolchain_c/lib/src/native_toolchain/recognizer.dart +++ b/pkgs/native_toolchain_c/lib/src/native_toolchain/recognizer.dart @@ -27,7 +27,7 @@ class CompilerRecognizer implements ToolResolver { if (filePath.contains('-gcc')) { tool = gcc; } else if (filePath.endsWith(os.executableFileName('clang'))) { - final stdout = await CliFilter.executeCli( + final (:stdout, :stderr) = await CliFilter.executeCli( uri, arguments: ['--version'], logger: logger, diff --git a/pkgs/native_toolchain_c/lib/src/tool/tool_resolver.dart b/pkgs/native_toolchain_c/lib/src/tool/tool_resolver.dart index 8221b55f5d..55aad9078b 100644 --- a/pkgs/native_toolchain_c/lib/src/tool/tool_resolver.dart +++ b/pkgs/native_toolchain_c/lib/src/tool/tool_resolver.dart @@ -82,8 +82,9 @@ class PathToolResolver extends ToolResolver { if (process.exitCode == 0) { final file = File(LineSplitter.split(process.stdout).first); final uri = File(await file.resolveSymbolicLinks()).uri; - if (uri.pathSegments.last case 'llvm' || 'lld') { + if (uri.pathSegments.last case 'llvm' || 'lld' || 'swiftly') { // https://github.com/dart-lang/native/issues/136 + // https://github.com/dart-lang/native/issues/2792 return file.uri; } return uri; @@ -373,10 +374,47 @@ class RelativeToolResolver implements ToolResolver { } } +class PathFilter implements ToolResolver { + final String toolName; + final ToolResolver wrappedResolver; + final bool Function({required Uri uri}) keepIf; + + PathFilter({ + required this.toolName, + required this.wrappedResolver, + required this.keepIf, + }); + + @override + Future> resolve(ToolResolvingContext context) async { + final logger = context.logger; + final otherToolInstances = await wrappedResolver.resolve(context); + + logger?.finer( + 'Checking if one of $toolName resolved as $otherToolInstances is ' + 'matches filter', + ); + + final result = otherToolInstances + .where((instance) => keepIf(uri: instance.uri)) + .toList(); + + if (result.isNotEmpty) { + logger?.fine('Found $result.'); + } else { + logger?.finer( + 'Found no $toolName with the specified absolute path ' + '$otherToolInstances.', + ); + } + return result; + } +} + class CliFilter implements ToolResolver { final ToolResolver wrappedResolver; final List cliArguments; - final bool Function({required String stdout}) keepIf; + final bool Function({required String stdout, required String stderr}) keepIf; CliFilter({ required this.wrappedResolver, @@ -399,12 +437,12 @@ class CliFilter implements ToolResolver { }) async { if (toolInstance.version != null) return toolInstance; logger?.finer('Checking if $toolInstance satisfies CLI filter.'); - final stdout = await executeCli( + final (:stdout, :stderr) = await executeCli( toolInstance.uri, arguments: cliArguments, logger: logger, ); - final doKeep = keepIf(stdout: stdout); + final doKeep = keepIf(stdout: stdout, stderr: stderr); if (doKeep) { logger?.fine('$toolInstance satisfies CLI filter.'); return toolInstance; @@ -413,7 +451,7 @@ class CliFilter implements ToolResolver { return null; } - static Future executeCli( + static Future<({String stdout, String stderr})> executeCli( Uri executable, { required List arguments, int expectedExitCode = 0, @@ -426,6 +464,6 @@ class CliFilter implements ToolResolver { ); final exitCode = process.exitCode; assert(exitCode == expectedExitCode); - return process.stdout; + return (stdout: process.stdout, stderr: process.stderr); } } diff --git a/pkgs/native_toolchain_c/pubspec.yaml b/pkgs/native_toolchain_c/pubspec.yaml index 1984dd0b30..7e643a180a 100644 --- a/pkgs/native_toolchain_c/pubspec.yaml +++ b/pkgs/native_toolchain_c/pubspec.yaml @@ -1,7 +1,7 @@ name: native_toolchain_c description: >- A library to invoke the native C compiler installed on the host machine. -version: 0.17.6 +version: 0.17.7 repository: https://github.com/dart-lang/native/tree/main/pkgs/native_toolchain_c topics: