From d665536abf405941107d7f8fd02fda70285fa4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?book=E8=AF=97=E6=84=8F?= Date: Fri, 22 Sep 2023 21:14:18 +0800 Subject: [PATCH] Squashed 'cargokit/' changes from 62bb5c9..7a5bba8 7a5bba8 fix: setup flutter environment for Xcode iOS builds (#30) 7467b9a fix: build_pod.sh should setup flutter environment (#29) 0f0359e fix: install targets for correct toolchain (#26) git-subtree-dir: cargokit git-subtree-split: 7a5bba8eefbfe7e567f754477fd172e7a06b153f --- build_pod.sh | 14 +++++- build_tool/lib/src/builder.dart | 6 +-- build_tool/lib/src/rustup.dart | 79 ++++++++++++++++++++++++++------- 3 files changed, 80 insertions(+), 19 deletions(-) diff --git a/build_pod.sh b/build_pod.sh index 75041e91..481071ee 100755 --- a/build_pod.sh +++ b/build_pod.sh @@ -37,10 +37,22 @@ export CARGOKIT_TOOL_TEMP_DIR=$TARGET_TEMP_DIR/build_tool # Directory inside root project. Not necessarily the top level directory of root project. export CARGOKIT_ROOT_PROJECT_DIR=$SRCROOT +FLUTTER_EXPORT_BUILD_ENVIRONMENT=( + "$PODS_ROOT/../Flutter/ephemeral/flutter_export_environment.sh" # macOS + "$PODS_ROOT/../Flutter/flutter_export_environment.sh" # iOS +) + +for path in "${FLUTTER_EXPORT_BUILD_ENVIRONMENT[@]}" +do + if [[ -f "$path" ]]; then + source "$path" + fi +done + "$BASEDIR/run_build_tool.sh" build-pod "$@" # Make a symlink from built framework to phony file, which will be used as input to # build script. This should force rebuild (podspec currently doesn't support alwaysOutOfDate # attribute on custom build phase) -ln -Fs "${BUILT_PRODUCTS_DIR}/${EXECUTABLE_PATH}" "${BUILT_PRODUCTS_DIR}/cargokit_phony" +ln -Fs "$OBJROOT/XCBuildData/build.db" "${BUILT_PRODUCTS_DIR}/cargokit_phony" ln -Fs "${BUILT_PRODUCTS_DIR}/${EXECUTABLE_PATH}" "${BUILT_PRODUCTS_DIR}/cargokit_phony_out" diff --git a/build_tool/lib/src/builder.dart b/build_tool/lib/src/builder.dart index 9393f632..570a5375 100644 --- a/build_tool/lib/src/builder.dart +++ b/build_tool/lib/src/builder.dart @@ -116,14 +116,14 @@ class RustBuilder { Rustup rustup, ) { final toolchain = _toolchain; - if (!rustup.installedToolchains.any((i) => i.startsWith('$toolchain-'))) { + if (rustup.installedTargets(toolchain) == null) { rustup.installToolchain(toolchain); } if (toolchain == 'nightly') { rustup.installRustSrcForNightly(); } - if (!rustup.installedTargets.contains(target.rust)) { - rustup.installTarget(target.rust); + if (!rustup.installedTargets(toolchain)!.contains(target.rust)) { + rustup.installTarget(target.rust, toolchain: toolchain); } } diff --git a/build_tool/lib/src/rustup.dart b/build_tool/lib/src/rustup.dart index 6828614f..6cd331af 100644 --- a/build_tool/lib/src/rustup.dart +++ b/build_tool/lib/src/rustup.dart @@ -1,41 +1,90 @@ import 'dart:io'; +import 'package:collection/collection.dart'; import 'package:path/path.dart' as path; import 'util.dart'; -class Rustup { - final List installedTargets; - final List installedToolchains; +class _Toolchain { + _Toolchain( + this.name, + this.targets, + ); - Rustup() - : installedTargets = _getInstalledTargets(), - installedToolchains = _getInstalledToolchains(); + final String name; + final List targets; +} - void installTarget(String target) { - log.info("Installing Rust target: $target"); - runCommand("rustup", ['target', 'add', target]); - installedTargets.add(target); +class Rustup { + List? installedTargets(String toolchain) { + final targets = _installedTargets(toolchain); + return targets != null ? List.unmodifiable(targets) : null; } void installToolchain(String toolchain) { log.info("Installing Rust toolchain: $toolchain"); runCommand("rustup", ['toolchain', 'install', toolchain]); - installedToolchains.add(toolchain); + _installedToolchains + .add(_Toolchain(toolchain, _getInstalledTargets(toolchain))); } - static List _getInstalledToolchains() { + void installTarget( + String target, { + required String toolchain, + }) { + log.info("Installing Rust target: $target"); + runCommand("rustup", [ + 'target', + 'add', + '--toolchain', + toolchain, + target, + ]); + _installedTargets(toolchain)?.add(target); + } + + final List<_Toolchain> _installedToolchains; + + Rustup() : _installedToolchains = _getInstalledToolchains(); + + List? _installedTargets(String toolchain) => _installedToolchains + .firstWhereOrNull( + (e) => e.name == toolchain || e.name.startsWith('$toolchain-')) + ?.targets; + + static List<_Toolchain> _getInstalledToolchains() { + String extractToolchainName(String line) { + // ignore (default) after toolchain name + final parts = line.split(' '); + return parts[0]; + } + final res = runCommand("rustup", ['toolchain', 'list']); final lines = res.stdout .toString() .split('\n') .where((e) => e.isNotEmpty) + .map(extractToolchainName) + .toList(growable: true); + + return lines + .map( + (name) => _Toolchain( + name, + _getInstalledTargets(name), + ), + ) .toList(growable: true); - return lines; } - static List _getInstalledTargets() { - final res = runCommand("rustup", ['target', 'list', '--installed']); + static List _getInstalledTargets(String toolchain) { + final res = runCommand("rustup", [ + 'target', + 'list', + '--toolchain', + toolchain, + '--installed', + ]); final lines = res.stdout .toString() .split('\n')