Skip to content

Commit ef0e03c

Browse files
authored
Add a simpler way to use a local Flutter engine (#225)
update_flutter_engine and build_flutter_assets now check for an engine_override file at the root of the repository to trigger the use of a local engine. Updates the README to link to instructions on using it. Since this adds more logic to build_flutter_assets, it has been rewritten in Dart. There are some obvious limitations (e.g., hard-coded name/location requirement, copying every time instead of only when the local build has changed), which can be addressed in the future if there's sufficient demand. Since it is only intended for temporary use when debugging, or testing engine changes, this may be sufficient, and is a significant improvement over managing it manually. Unlike .flutter_location_config this has no leading ., and is in the repository rather than above it. This difference is intentional, since unlike .flutter_location_config this is for temporary use, so having it show up in directory listings and git status is helpful as a reminder that it is enabled. Fixes #160
1 parent 1815194 commit ef0e03c

10 files changed

+335
-84
lines changed

LocalEngine.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Running a Locally Built Flutter Engine
2+
3+
It may be useful for debugging or development to run with a [locally built
4+
Flutter engine](https://github.com/flutter/flutter/wiki/Compiling-the-engine).
5+
To temporarily override the normal behavior of `update_flutter_engine` and
6+
`build_flutter_assets`, add a file called `engine_override` at the root of
7+
your `flutter_desktop_embedding` checkout. For instance, on macOS or Linux:
8+
```
9+
$ echo host_debug_unopt > engine_override
10+
```
11+
12+
This will cause `update_flutter_engine` to copy your local engine instead of
13+
downloading a prebuilt engine, and `build_flutter_assets` to pass the
14+
`--local-engine` flag when building assets.
15+
16+
**Important**: Your Flutter engine checkout must be in a folder called `engine`
17+
(as recommended in the [setup
18+
instructions](https://github.com/flutter/flutter/wiki/Setting-up-the-Engine-development-environment)),
19+
which must be next to your `flutter` checkout, or `update_flutter_engine` will
20+
fail.
21+
22+
## Dart Changes
23+
24+
If you change any Dart code in your engine, be sure to follow the instructions
25+
[here](https://github.com/flutter/flutter/wiki/Setting-up-the-Engine-development-environment)
26+
or
27+
[here](https://github.com/flutter/flutter/wiki/The-flutter-tool#using-a-locally-built-engine-with-the-flutter-tool)
28+
for adding a `dependency_overrides` section to your `pubspec.yaml`.
29+
30+
## Switching Back to a Prebuilt Engine
31+
32+
To stop using your local engine, just delete `engine_override` (and
33+
`dependency_overrides` if you added them to `pubspec.yaml`) and rebuild.
34+
`update_flutter_engine` will automatically re-download the correct prebuilt
35+
engine.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ with this library.
6060
Debugging of the Flutter side of a desktop application is possible, but requires
6161
[a modified workflow](Debugging.md).
6262

63+
To debug the Flutter engine, you can [use a local engine build](LocalEngine.md).
64+
6365
## Feedback and Discussion
6466

6567
For bug reports and specific feature requests, you can file GitHub issues. For

tools/build_flutter_assets

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,10 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
# This script runs the necessary Flutter commands to build the Flutter assets
18-
# than need to be packaged in an embedding application.
19-
# It should be called with one argument, which is the directory of the
20-
# Flutter application to build.
17+
# Runs build_flutter_assets.dart, using the output of flutter_location as
18+
# --flutter_root
2119

2220
readonly base_dir="$(dirname "$0")"
23-
readonly flutter_dir="$("$base_dir/flutter_location")"
24-
readonly flutter_binary="$flutter_dir/bin/flutter"
25-
26-
# To use a custom Flutter engine, uncomment the following variables, and set
27-
# engine_src_path to the path on your machine to your Flutter engine tree's
28-
# src/ directory (and build_type if your engine build is not debug).
29-
#readonly engine_src_path="/path/to/engine/src"
30-
#readonly build_type=host_debug_unopt
31-
#readonly extra_flags=(--local-engine-src-path $engine_src_path --local-engine=$build_type)
32-
33-
cd "$1"
34-
echo Running "$flutter_binary" ${extra_flags[*]} build bundle
35-
exec "$flutter_binary" ${extra_flags[*]} build bundle
21+
readonly flutter_dir="$("${base_dir}/flutter_location")"
22+
exec "${base_dir}/run_dart_tool" build_flutter_assets \
23+
--flutter_root="${flutter_dir}" "$@"

tools/build_flutter_assets.bat

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,11 @@
1111
:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
:: See the License for the specific language governing permissions and
1313
:: limitations under the License.
14-
@echo off
1514

16-
:: This script runs the necessary Flutter commands to build the Flutter assets
17-
:: than need to be packaged in an embedding application.
18-
:: It should be called with one argument, which is the directory of the
19-
:: Flutter application to build.
20-
SETLOCAL ENABLEDELAYEDEXPANSION
15+
:: Runs build_flutter_assets.dart, using the output of flutter_location.bat as
16+
:: --flutter_root
17+
@echo off
2118

2219
for /f "delims=" %%i in ('%~dp0flutter_location') do set FLUTTER_DIR=%%i
23-
set FLUTTER_BINARY=%FLUTTER_DIR%\bin\flutter
24-
25-
:: To use a custom Flutter engine, uncomment the following variables, and set
26-
:: ENGINE_SRC_PATH to the path on your machine to your Flutter engine tree's
27-
:: src\ directory (and BUILD_TYPE if your engine build is not debug).
28-
::set ENGINE_SRC_PATH=path\to\engine\src
29-
::set BUILD_TYPE=host_debug_unopt
30-
::set EXTRA_FLAGS=--local-engine-src-path %ENGINE_SRC_PATH% --local-engine=%BUILD_TYPE%
3120

32-
cd %1
33-
echo Running %FLUTTER_BINARY% %EXTRA_FLAGS% build bundle
34-
call %FLUTTER_BINARY% %EXTRA_FLAGS% build bundle
21+
call %~dp0.\run_dart_tool build_flutter_assets --flutter_root %FLUTTER_DIR% %*
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the 'License');
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an 'AS IS' BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// This script runs the necessary Flutter commands to build the Flutter assets
16+
// that need to be packaged in an embedding application.
17+
// It should be called with one argument, which is the directory of the
18+
// Flutter application to build.
19+
20+
import 'dart:io';
21+
22+
import 'package:args/args.dart';
23+
import 'package:path/path.dart' as path;
24+
25+
import '../lib/flutter_utils.dart';
26+
import '../lib/run_command.dart';
27+
28+
Future<void> main(List<String> arguments) async {
29+
final parser = new ArgParser()
30+
..addOption('flutter_root',
31+
help: 'The root of the Flutter tree to run \'flutter\' from.\n'
32+
'Defaults to a "flutter" directory next to this repository.',
33+
defaultsTo: getDefaultFlutterRoot())
34+
..addFlag('help', help: 'Prints this usage message.', negatable: false);
35+
ArgResults parsedArguments;
36+
37+
try {
38+
parsedArguments = parser.parse(arguments);
39+
} on ArgParserException {
40+
printUsage(parser);
41+
exit(1);
42+
}
43+
if (parsedArguments.rest.length != 1) {
44+
printUsage(parser);
45+
exit(1);
46+
}
47+
final flutterApplicationDir = parsedArguments.rest[0];
48+
49+
final flutterName = Platform.isWindows ? 'flutter.bat' : 'flutter';
50+
final flutterBinary =
51+
path.join(parsedArguments['flutter_root'], 'bin', flutterName);
52+
if (!File(flutterBinary).existsSync()) {
53+
print("Error: No flutter binary at '$flutterBinary'");
54+
exit(1);
55+
}
56+
57+
final buildArguments = ['build', 'bundle'];
58+
59+
// Add --local-engine if an override is specified. --local-engine-src-path
60+
// isn't provided since per
61+
// https://github.com/flutter/flutter/wiki/The-flutter-tool
62+
// it's not required if the engine directory is next to the flutter directory,
63+
// which is currently the only configuration this project supports for local
64+
// engines.
65+
final engineOverride = await getEngineOverrideBuildType();
66+
if (engineOverride != null) {
67+
buildArguments.insertAll(0, ['--local-engine', engineOverride]);
68+
}
69+
70+
await runCommand(flutterBinary, buildArguments,
71+
workingDirectory: flutterApplicationDir);
72+
}
73+
74+
/// Prints usage info for this utility.
75+
void printUsage(ArgParser argParser) {
76+
print('Usage: build_flutter_assets [options] '
77+
'<fluter application directory>\n');
78+
print(argParser.usage);
79+
}

tools/dart_tools/bin/build_jsoncpp.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import 'dart:io';
2121
import 'package:args/args.dart';
2222
import 'package:path/path.dart' as path;
2323

24-
import '../lib/runCommand.dart';
24+
import '../lib/run_command.dart';
2525

2626
Future<void> main(List<String> arguments) async {
2727
if (!Platform.isWindows) {
@@ -62,7 +62,7 @@ Future<void> buildLibrary(String buildDirectory, {bool debug}) async {
6262
arguments.add('/p:Configuration=Release');
6363
}
6464
await runCommand('vcvars64.bat 1> nul &&', arguments,
65-
workingDirectory: buildDirectory);
65+
workingDirectory: buildDirectory, runInShell: true);
6666
}
6767

6868
Future<void> copyLibraryToOutputDirectory(

tools/dart_tools/bin/fetch_jsoncpp.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import 'dart:io';
1919

20-
import '../lib/runCommand.dart';
20+
import '../lib/run_command.dart';
2121

2222
// For the fork containing V2017 support. Once
2323
// https://github.com/open-source-parsers/jsoncpp/pull/853

0 commit comments

Comments
 (0)