Unverified Commit 8b0243f4 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Teach Linux to use local engine (#31631)

parent 829bdeb4
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:io'; // ignore: dart_io_import.
import 'package:path/path.dart' as path; // ignore: package_path_import.
/// Executes the required flutter tasks for a linux build.
Future<void> main(List<String> arguments) async {
final String targetPlatform = arguments[0];
final String buildMode = arguments[1];
final String projectDirectory = Platform.environment['PROJECT_DIR'];
final bool verbose = Platform.environment['VERBOSE_SCRIPT_LOGGING'] != null;
final bool trackWidgetCreation = Platform.environment['TRACK_WIDGET_CREATION'] != null;
final String flutterTarget = Platform.environment['FLUTTER_TARGET'] ?? 'lib/main.dart';
final String flutterEngine = Platform.environment['FLUTTER_ENGINE'];
final String localEngine = Platform.environment['LOCAL_ENGINE'];
final String flutterRoot = Platform.environment['FLUTTER_ROOT'];
Directory.current = projectDirectory;
if (localEngine != null && !localEngine.contains(buildMode)) {
stderr.write('''
ERROR: Requested build with Flutter local engine at '$localEngine'
This engine is not compatible with FLUTTER_BUILD_MODE: '$buildMode'.
You can fix this by updating the LOCAL_ENGINE environment variable, or
by running:
flutter build linux --local-engine=host_$buildMode
or
flutter build linux --local-engine=host_${buildMode}_unopt
========================================================================
''');
exit(1);
}
String cacheDirectory;
switch (targetPlatform) {
case 'linux-x64':
cacheDirectory = 'linux/flutter';
break;
default:
stderr.write('Unsupported target platform $targetPlatform');
exit(1);
}
final String flutterExecutable = path.join(
flutterRoot, 'bin', Platform.isWindows ? 'flutter.bat' : 'flutter');
final ProcessResult unpackResult = await Process.run(
flutterExecutable,
<String>[
'--suppress-analytics',
if (verbose) '--verbose',
'unpack',
'--target-platform=$targetPlatform',
'--cache-dir=$cacheDirectory',
if (flutterEngine != null) '--local-engine-src-path=$flutterEngine',
if (localEngine != null) '--local-engine=$localEngine',
]);
if (unpackResult.exitCode != 0) {
stderr.write(unpackResult.stderr);
exit(1);
}
final ProcessResult buildResult = await Process.run(
flutterExecutable,
<String>[
'--suppress-analytics',
if (verbose) '--verbose',
'build',
'bundle',
'--target=$flutterTarget',
'--target-platform=$targetPlatform',
if (trackWidgetCreation) '--track-widget-creation',
if (flutterEngine != null) '--local-engine-src-path=$flutterEngine',
if (localEngine != null) '--local-engine=$localEngine',
]);
if (buildResult.exitCode != 0) {
stderr.write(buildResult.stderr);
exit(1);
}
exit(0);
}
#!/usr/bin/env bash
# Copyright 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
readonly flutter_bin_dir="${FLUTTER_ROOT}/bin"
readonly dart_bin_dir="${flutter_bin_dir}/cache/dart-sdk/bin"
exec "${dart_bin_dir}/dart" "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.dart" "${@:1}"
......@@ -37,6 +37,7 @@ import 'src/commands/screenshot.dart';
import 'src/commands/shell_completion.dart';
import 'src/commands/test.dart';
import 'src/commands/train.dart';
import 'src/commands/unpack.dart';
import 'src/commands/update_packages.dart';
import 'src/commands/upgrade.dart';
import 'src/commands/version.dart';
......@@ -82,6 +83,7 @@ Future<void> main(List<String> args) async {
ShellCompletionCommand(),
TestCommand(verboseHelp: verboseHelp),
TrainingCommand(),
UnpackCommand(),
UpdatePackagesCommand(hidden: !verboseHelp),
UpgradeCommand(),
VersionCommand(),
......
This diff is collapsed.
......@@ -2,7 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import '../artifacts.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/process_manager.dart';
......@@ -13,21 +15,32 @@ import '../globals.dart';
import '../project.dart';
/// Builds the Linux project through the Makefile.
Future<void> buildLinux(LinuxProject linuxProject, BuildInfo buildInfo) async {
/// Cache flutter root in linux directory.
linuxProject.editableHostAppDirectory.childFile('.generated_flutter_root')
Future<void> buildLinux(LinuxProject linuxProject, BuildInfo buildInfo, {String target = 'lib/main.dart'}) async {
final String buildFlag = buildInfo?.isDebug == true ? 'debug' : 'release';
final StringBuffer buffer = StringBuffer('''
# Generated code do not commit.
export FLUTTER_ROOT=${Cache.flutterRoot}
export BUILD=$buildFlag
export TRACK_WIDGET_CREATION=${buildInfo?.trackWidgetCreation == true}
export FLUTTER_TARGET=$target
export PROJECT_DIR=${linuxProject.project.directory.path}
''');
if (artifacts is LocalEngineArtifacts) {
final LocalEngineArtifacts localEngineArtifacts = artifacts;
final String engineOutPath = localEngineArtifacts.engineOutPath;
buffer.writeln('export FLUTTER_ENGINE=${fs.path.dirname(fs.path.dirname(engineOutPath))}');
buffer.writeln('export LOCAL_ENGINE=${fs.path.basename(engineOutPath)}');
}
/// Cache flutter configuration files in the linux directory.
linuxProject.cacheDirectory.childFile('generated_config')
..createSync(recursive: true)
..writeAsStringSync(Cache.flutterRoot);
..writeAsStringSync(buffer.toString());
final String buildFlag = buildInfo?.isDebug == true ? 'debug' : 'release';
final String bundleFlags = buildInfo?.trackWidgetCreation == true ? '--track-widget-creation' : '';
final Process process = await processManager.start(<String>[
'make',
'-C',
linuxProject.editableHostAppDirectory.path,
'BUILD=$buildFlag',
'FLUTTER_ROOT=${Cache.flutterRoot}',
'FLUTTER_BUNDLE_FLAGS=$bundleFlags',
], runInShell: true);
final Status status = logger.startProgress(
'Building Linux application...',
......
......@@ -599,6 +599,8 @@ class LinuxProject {
Directory get editableHostAppDirectory => project.directory.childDirectory('linux');
Directory get cacheDirectory => editableHostAppDirectory.childDirectory('flutter');
bool existsSync() => editableHostAppDirectory.existsSync();
/// The Linux project makefile.
......
......@@ -5,7 +5,7 @@ author: Flutter Authors <flutter-dev@googlegroups.com>
environment:
# The pub client defaults to an <2.0.0 sdk constraint which we need to explicitly overwrite.
sdk: ">=2.2.0 <3.0.0"
sdk: ">=2.2.2 <3.0.0"
dependencies:
# To update these, use "flutter update-packages --force-upgrade".
......
......@@ -21,7 +21,6 @@ import '../src/mocks.dart';
void main() {
Cache.disableLocking();
final MockProcessManager mockProcessManager = MockProcessManager();
final MemoryFileSystem memoryFilesystem = MemoryFileSystem();
final MockProcess mockProcess = MockProcess();
final MockPlatform linuxPlatform = MockPlatform();
final MockPlatform notLinuxPlatform = MockPlatform();
......@@ -46,7 +45,7 @@ void main() {
), throwsA(isInstanceOf<ToolExit>()));
}, overrides: <Type, Generator>{
Platform: () => linuxPlatform,
FileSystem: () => memoryFilesystem,
FileSystem: () => MemoryFileSystem(),
});
testUsingContext('Linux build fails on non-linux platform', () async {
......@@ -61,22 +60,20 @@ void main() {
), throwsA(isInstanceOf<ToolExit>()));
}, overrides: <Type, Generator>{
Platform: () => notLinuxPlatform,
FileSystem: () => memoryFilesystem,
FileSystem: () => MemoryFileSystem(),
});
testUsingContext('Linux build invokes make', () async {
testUsingContext('Linux build invokes make and writes temporary files', () async {
final BuildCommand command = BuildCommand();
applyMocksToCommand(command);
fs.file('linux/build.sh').createSync(recursive: true);
fs.file('pubspec.yaml').createSync();
fs.file('.packages').createSync();
when(mockProcessManager.start(<String>[
'make',
'-C',
'/linux',
'BUILD=release',
'FLUTTER_ROOT=/',
'FLUTTER_BUNDLE_FLAGS=',
], runInShell: true)).thenAnswer((Invocation invocation) async {
return mockProcess;
});
......@@ -84,8 +81,9 @@ void main() {
await createTestCommandRunner(command).run(
const <String>['build', 'linux']
);
expect(fs.file('linux/flutter/generated_config').existsSync(), true);
}, overrides: <Type, Generator>{
FileSystem: () => memoryFilesystem,
FileSystem: () => MemoryFileSystem(),
ProcessManager: () => mockProcessManager,
Platform: () => linuxPlatform,
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment