From f9a609008279b5563bfc04d509a58e1f2ab403d6 Mon Sep 17 00:00:00 2001 From: KyleWong Date: Sat, 16 Feb 2019 07:56:49 +0800 Subject: [PATCH] Refactor local engine logic (#27765) * Refactor for iOS. * For android_xxx_unopt_arm64&ios_xxx_unopt_arm, remove the _arm64/_arm suffix. dart generated in host_xxx_unopt/host_xxx has an arch of x86_64 which supports arm64/arm. * Remove suffixes for various archs. --- packages/flutter_tools/bin/xcode_backend.sh | 11 ++++- .../lib/src/base/user_messages.dart | 4 +- .../flutter_tools/lib/src/ios/xcodeproj.dart | 6 ++- .../src/runner/flutter_command_runner.dart | 45 ++++++++++++------- .../runner/flutter_command_runner_test.dart | 23 +++++++++- 5 files changed, 65 insertions(+), 24 deletions(-) diff --git a/packages/flutter_tools/bin/xcode_backend.sh b/packages/flutter_tools/bin/xcode_backend.sh index 0537672b23..1bf2310435 100755 --- a/packages/flutter_tools/bin/xcode_backend.sh +++ b/packages/flutter_tools/bin/xcode_backend.sh @@ -96,10 +96,15 @@ BuildApp() { RunCommand rm -rf -- "${derived_dir}/App.framework" + local flutter_engine_flag="" local local_engine_flag="" local flutter_framework="${framework_path}/Flutter.framework" local flutter_podspec="${framework_path}/Flutter.podspec" + if [[ -n "$FLUTTER_ENGINE" ]]; then + flutter_engine_flag="--local-engine-src-path=${FLUTTER_ENGINE}" + fi + if [[ -n "$LOCAL_ENGINE" ]]; then if [[ $(echo "$LOCAL_ENGINE" | tr "[:upper:]" "[:lower:]") != *"$build_mode"* ]]; then EchoError "========================================================================" @@ -114,8 +119,8 @@ BuildApp() { exit -1 fi local_engine_flag="--local-engine=${LOCAL_ENGINE}" - flutter_framework="${LOCAL_ENGINE}/Flutter.framework" - flutter_podspec="${LOCAL_ENGINE}/Flutter.podspec" + flutter_framework="${FLUTTER_ENGINE}/out/${LOCAL_ENGINE}/Flutter.framework" + flutter_podspec="${FLUTTER_ENGINE}/out/${LOCAL_ENGINE}/Flutter.podspec" fi if [[ -e "${project_path}/.ios" ]]; then @@ -168,6 +173,7 @@ BuildApp() { --target="${target_path}" \ --${build_mode} \ --ios-arch="${archs}" \ + ${flutter_engine_flag} \ ${local_engine_flag} \ ${track_widget_creation_flag} @@ -244,6 +250,7 @@ BuildApp() { --depfile="${build_dir}/snapshot_blob.bin.d" \ --asset-dir="${derived_dir}/App.framework/flutter_assets" \ ${precompilation_flag} \ + ${flutter_engine_flag} \ ${local_engine_flag} \ ${track_widget_creation_flag} diff --git a/packages/flutter_tools/lib/src/base/user_messages.dart b/packages/flutter_tools/lib/src/base/user_messages.dart index acd1b805c2..f7ceef6ea4 100644 --- a/packages/flutter_tools/lib/src/base/user_messages.dart +++ b/packages/flutter_tools/lib/src/base/user_messages.dart @@ -243,8 +243,8 @@ class UserMessages { 'Warning: this bug report contains local paths, device identifiers, and log snippets.'; String get runnerNoRecordTo => 'record-to location not specified'; String get runnerNoReplayFrom => 'replay-from location not specified'; - String runnerNoEngineBuildDir(String enginePackageName, String engineEnvVar) => - 'Unable to detect local Flutter engine build directory.\n' + String runnerNoEngineSrcDir(String enginePackageName, String engineEnvVar) => + 'Unable to detect local Flutter engine src directory.\n' 'Either specify a dependency_override for the $enginePackageName package in your pubspec.yaml and ' 'ensure --package-root is set if necessary, or set the \$$engineEnvVar environment variable, or ' 'use --local-engine-src-path to specify the path to the root of your flutter/engine repository.'; diff --git a/packages/flutter_tools/lib/src/ios/xcodeproj.dart b/packages/flutter_tools/lib/src/ios/xcodeproj.dart index 5d7d460846..0a32c7fdd2 100644 --- a/packages/flutter_tools/lib/src/ios/xcodeproj.dart +++ b/packages/flutter_tools/lib/src/ios/xcodeproj.dart @@ -74,7 +74,9 @@ Future updateGeneratedXcodeProperties({ if (artifacts is LocalEngineArtifacts) { final LocalEngineArtifacts localEngineArtifacts = artifacts; - localsBuffer.writeln('LOCAL_ENGINE=${localEngineArtifacts.engineOutPath}'); + final String engineOutPath = localEngineArtifacts.engineOutPath; + localsBuffer.writeln('FLUTTER_ENGINE=${fs.path.dirname(fs.path.dirname(engineOutPath))}'); + localsBuffer.writeln('LOCAL_ENGINE=${fs.path.basename(engineOutPath)}'); // Tell Xcode not to build universal binaries for local engines, which are // single-architecture. @@ -82,7 +84,7 @@ Future updateGeneratedXcodeProperties({ // NOTE: this assumes that local engine binary paths are consistent with // the conventions uses in the engine: 32-bit iOS engines are built to // paths ending in _arm, 64-bit builds are not. - final String arch = localEngineArtifacts.engineOutPath.endsWith('_arm') ? 'armv7' : 'arm64'; + final String arch = engineOutPath.endsWith('_arm') ? 'armv7' : 'arm64'; localsBuffer.writeln('ARCHS=$arch'); } diff --git a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart index 04d654e894..df4e5e2a81 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart @@ -409,25 +409,28 @@ class FlutterCommandRunner extends CommandRunner { if (engineSourcePath == null && globalResults['local-engine'] != null) { try { - final Uri engineUri = PackageMap(PackageMap.globalPackagesPath).map[kFlutterEnginePackageName]; - if (engineUri != null) { - engineSourcePath = fs.path.dirname(fs.path.dirname(fs.path.dirname(fs.path.dirname(engineUri.path)))); - final bool dirExists = fs.isDirectorySync(fs.path.join(engineSourcePath, 'out')); - if (engineSourcePath == '/' || engineSourcePath.isEmpty || !dirExists) + Uri engineUri = PackageMap(PackageMap.globalPackagesPath).map[kFlutterEnginePackageName]; + // Skip if sky_engine is the self-contained one. + if (fs.path.join(Cache.flutterRoot, 'bin', 'cache', 'pkg', kFlutterEnginePackageName, 'lib') + fs.path.separator == engineUri?.path) { + engineUri = null; + } + // If sky_engine is specified and the engineSourcePath not set, try to determine the engineSourcePath by sky_engine setting. + // A typical engineUri looks like: file://flutter-engine-local-path/src/out/host_debug_unopt/gen/dart-pkg/sky_engine/lib/ + if (engineUri?.path != null) { + engineSourcePath = fs.directory(engineUri.path)?.parent?.parent?.parent?.parent?.parent?.parent?.path; + if (engineSourcePath != null && (engineSourcePath == fs.path.dirname(engineSourcePath) || engineSourcePath.isEmpty)) { engineSourcePath = null; + throwToolExit(userMessages.runnerNoEngineSrcDir(kFlutterEnginePackageName, kFlutterEngineEnvironmentVariableName), + exitCode: 2); + } } } on FileSystemException { engineSourcePath = null; } on FormatException { engineSourcePath = null; } - - engineSourcePath ??= _tryEnginePath(fs.path.join(Cache.flutterRoot, '../engine/src')); - - if (engineSourcePath == null) { - throwToolExit(userMessages.runnerNoEngineBuildDir(kFlutterEnginePackageName, kFlutterEngineEnvironmentVariableName), - exitCode: 2); - } + // If engineSourcePath is still not set, try to determine it by flutter root. + engineSourcePath ??= _tryEnginePath(fs.path.join(fs.directory(Cache.flutterRoot).parent.path, 'engine', 'src')); } if (engineSourcePath != null && _tryEnginePath(engineSourcePath) == null) { @@ -438,6 +441,19 @@ class FlutterCommandRunner extends CommandRunner { return engineSourcePath; } + String _getHostEngineBasename(String localEngineBasename) { + // Determine the host engine directory associated with the local engine: + // Strip '_sim_' since there are no host simulator builds. + String tmpBasename = localEngineBasename.replaceFirst('_sim_', '_'); + tmpBasename = tmpBasename.substring(tmpBasename.indexOf('_') + 1); + // Strip suffix for various archs. + final List suffixes = ['_arm', '_arm64', '_x86', '_x64']; + for (String suffix in suffixes) { + tmpBasename = tmpBasename.replaceFirst(RegExp('$suffix\$'), ''); + } + return 'host_' + tmpBasename; + } + EngineBuildPaths _findEngineBuildPath(ArgResults globalResults, String enginePath) { String localEngine; if (globalResults['local-engine'] != null) { @@ -451,11 +467,8 @@ class FlutterCommandRunner extends CommandRunner { throwToolExit(userMessages.runnerNoEngineBuild(engineBuildPath), exitCode: 2); } - // Determine the host engine directory associated with the local engine: - // * strip '_sim_' since there are no host simulator builds. - // * replace the target platform with host. final String basename = fs.path.basename(engineBuildPath); - final String hostBasename = 'host_' + basename.replaceFirst('_sim_', '_').substring(basename.indexOf('_') + 1); + final String hostBasename = _getHostEngineBasename(basename); final String engineHostBuildPath = fs.path.normalize(fs.path.join(fs.path.dirname(engineBuildPath), hostBasename)); if (!fs.isDirectorySync(engineHostBuildPath)) { throwToolExit(userMessages.runnerNoEngineBuild(engineHostBuildPath), exitCode: 2); diff --git a/packages/flutter_tools/test/runner/flutter_command_runner_test.dart b/packages/flutter_tools/test/runner/flutter_command_runner_test.dart index 563d480fd2..953e0fe3cf 100644 --- a/packages/flutter_tools/test/runner/flutter_command_runner_test.dart +++ b/packages/flutter_tools/test/runner/flutter_command_runner_test.dart @@ -21,6 +21,7 @@ import 'utils.dart'; const String _kFlutterRoot = '/flutter/flutter'; const String _kEngineRoot = '/flutter/engine'; +const String _kArbitraryEngineRoot = '/arbitrary/engine'; const String _kProjectRoot = '/project'; const String _kDotPackages = '.packages'; @@ -68,8 +69,26 @@ void main() { Platform: () => platform, }, initializeFlutterRoot: false); - testUsingContext('works if --local-engine is specified', () async { - fs.file(_kDotPackages).writeAsStringSync('sky_engine:file://$_kFlutterRoot/bin/cache/pkg/sky_engine/lib/'); + testUsingContext('works if --local-engine is specified and --local-engine-src-path is determined by sky_engine', () async { + fs.directory('$_kArbitraryEngineRoot/src/out/ios_debug/gen/dart-pkg/sky_engine/lib/').createSync(recursive: true); + fs.directory('$_kArbitraryEngineRoot/src/out/host_debug').createSync(recursive: true); + fs.file(_kDotPackages).writeAsStringSync('sky_engine:file://$_kArbitraryEngineRoot/src/out/ios_debug/gen/dart-pkg/sky_engine/lib/'); + await runner.run(['dummy', '--local-engine=ios_debug']); + }, overrides: { + FileSystem: () => fs, + Platform: () => platform, + }, initializeFlutterRoot: false); + + testUsingContext('works if --local-engine is specified and --local-engine-src-path is specified', () async { + fs.directory('$_kArbitraryEngineRoot/src/out/ios_debug').createSync(recursive: true); + fs.directory('$_kArbitraryEngineRoot/src/out/host_debug').createSync(recursive: true); + await runner.run(['dummy', '--local-engine-src-path=$_kArbitraryEngineRoot/src', '--local-engine=ios_debug']); + }, overrides: { + FileSystem: () => fs, + Platform: () => platform, + }, initializeFlutterRoot: false); + + testUsingContext('works if --local-engine is specified and --local-engine-src-path is determined by flutter root', () async { fs.directory('$_kEngineRoot/src/out/ios_debug').createSync(recursive: true); fs.directory('$_kEngineRoot/src/out/host_debug').createSync(recursive: true); await runner.run(['dummy', '--local-engine=ios_debug']);