Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 254927e

Browse files
authored
Infer --rbe based on the existence of //flutter/build/rbe (#52700)
Closes flutter/flutter#148006.
1 parent 9bf353d commit 254927e

File tree

9 files changed

+116
-32
lines changed

9 files changed

+116
-32
lines changed

tools/engine_tool/lib/src/build_utils.dart

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:io' as io show Directory;
6-
75
import 'package:engine_build_configs/engine_build_configs.dart';
8-
import 'package:path/path.dart' as p;
96

107
import 'environment.dart';
118
import 'logger.dart';
@@ -122,18 +119,12 @@ String demangleConfigName(Environment env, String name) {
122119
Future<int> runBuild(
123120
Environment environment,
124121
Build build, {
122+
required bool enableRbe,
125123
List<String> extraGnArgs = const <String>[],
126124
List<String> targets = const <String>[],
127125
}) async {
128-
// If RBE config files aren't in the tree, then disable RBE.
129-
final String rbeConfigPath = p.join(
130-
environment.engine.srcDir.path,
131-
'flutter',
132-
'build',
133-
'rbe',
134-
);
135126
final List<String> gnArgs = <String>[
136-
if (!io.Directory(rbeConfigPath).existsSync()) '--no-rbe',
127+
if (!enableRbe) '--no-rbe',
137128
...extraGnArgs,
138129
];
139130

@@ -192,16 +183,10 @@ Future<int> runGn(
192183
Environment environment,
193184
Build build, {
194185
List<String> extraGnArgs = const <String>[],
186+
required bool enableRbe,
195187
}) async {
196-
// If RBE config files aren't in the tree, then disable RBE.
197-
final String rbeConfigPath = p.join(
198-
environment.engine.srcDir.path,
199-
'flutter',
200-
'build',
201-
'rbe',
202-
);
203188
final List<String> gnArgs = <String>[
204-
if (!io.Directory(rbeConfigPath).existsSync()) '--no-rbe',
189+
if (!enableRbe) '--no-rbe',
205190
...extraGnArgs,
206191
];
207192

tools/engine_tool/lib/src/commands/build_command.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ final class BuildCommand extends CommandBase {
3030
);
3131
argParser.addFlag(
3232
rbeFlag,
33-
defaultsTo: true,
34-
help: 'RBE is enabled by default when available. Use --no-rbe to '
35-
'disable it.',
33+
defaultsTo: environment.hasRbeConfigInTree(),
34+
help: 'RBE is enabled by default when available.',
3635
);
3736
argParser.addFlag(
3837
ltoFlag,
@@ -57,6 +56,10 @@ et build //flutter/fml:fml_benchmarks # Build a specific target in `//flutter/f
5756
Future<int> run() async {
5857
final String configName = argResults![configFlag] as String;
5958
final bool useRbe = argResults![rbeFlag] as bool;
59+
if (useRbe && !environment.hasRbeConfigInTree()) {
60+
environment.logger.error('RBE was requested but no RBE config was found');
61+
return 1;
62+
}
6063
final bool useLto = argResults![ltoFlag] as bool;
6164
final String demangledName = demangleConfigName(environment, configName);
6265
final Build? build =
@@ -76,6 +79,7 @@ et build //flutter/fml:fml_benchmarks # Build a specific target in `//flutter/f
7679
environment,
7780
build,
7881
argResults!.rest,
82+
enableRbe: useRbe,
7983
);
8084
if (selectedTargets == null) {
8185
// The user typed something wrong and targetsFromCommandLine has already
@@ -93,6 +97,7 @@ et build //flutter/fml:fml_benchmarks # Build a specific target in `//flutter/f
9397
build,
9498
extraGnArgs: extraGnArgs,
9599
targets: ninjaTargets,
100+
enableRbe: useRbe,
96101
);
97102
}
98103
}

tools/engine_tool/lib/src/commands/query_command.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ final class QueryTargetsCommand extends CommandBase {
154154
help: 'Filter build targets to only include tests',
155155
negatable: false,
156156
);
157+
argParser.addFlag(
158+
rbeFlag,
159+
defaultsTo: environment.hasRbeConfigInTree(),
160+
help: 'RBE is enabled by default when available.',
161+
);
157162
}
158163

159164
/// Build configurations loaded from the engine from under ci/builders.
@@ -176,6 +181,11 @@ et query targets //flutter/fml/... # List all targets under `//flutter/fml`
176181
Future<int> run() async {
177182
final String configName = argResults![configFlag] as String;
178183
final bool testOnly = argResults![testOnlyFlag] as bool;
184+
final bool useRbe = argResults![rbeFlag] as bool;
185+
if (useRbe && !environment.hasRbeConfigInTree()) {
186+
environment.logger.error('RBE was requested but no RBE config was found');
187+
return 1;
188+
}
179189
final String demangledName = demangleConfigName(environment, configName);
180190
final Build? build =
181191
builds.where((Build build) => build.name == demangledName).firstOrNull;
@@ -189,6 +199,7 @@ et query targets //flutter/fml/... # List all targets under `//flutter/fml`
189199
build,
190200
argResults!.rest,
191201
defaultToAll: true,
202+
enableRbe: useRbe,
192203
);
193204
if (selectedTargets == null) {
194205
// The user typed something wrong and targetsFromCommandLine has already

tools/engine_tool/lib/src/commands/run_command.dart

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ final class RunCommand extends CommandBase {
3636
);
3737
argParser.addFlag(
3838
rbeFlag,
39-
defaultsTo: true,
40-
help: 'RBE is enabled by default when available. Use --no-rbe to '
41-
'disable it.',
39+
defaultsTo: environment.hasRbeConfigInTree(),
40+
help: 'RBE is enabled by default when available.',
4241
);
4342
}
4443

@@ -152,19 +151,33 @@ See `flutter run --help` for a listing
152151
}
153152

154153
final bool useRbe = argResults![rbeFlag] as bool;
154+
if (useRbe && !environment.hasRbeConfigInTree()) {
155+
environment.logger.error('RBE was requested but no RBE config was found');
156+
return 1;
157+
}
155158
final List<String> extraGnArgs = <String>[
156159
if (!useRbe) '--no-rbe',
157160
];
158161

159162
// First build the host.
160-
int r = await runBuild(environment, hostBuild, extraGnArgs: extraGnArgs);
163+
int r = await runBuild(
164+
environment,
165+
hostBuild,
166+
extraGnArgs: extraGnArgs,
167+
enableRbe: useRbe,
168+
);
161169
if (r != 0) {
162170
return r;
163171
}
164172

165173
// Now build the target if it isn't the same.
166174
if (hostBuild.name != build.name) {
167-
r = await runBuild(environment, build, extraGnArgs: extraGnArgs);
175+
r = await runBuild(
176+
environment,
177+
build,
178+
extraGnArgs: extraGnArgs,
179+
enableRbe: useRbe,
180+
);
168181
if (r != 0) {
169182
return r;
170183
}

tools/engine_tool/lib/src/commands/test_command.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ final class TestCommand extends CommandBase {
3030
argParser,
3131
builds,
3232
);
33+
argParser.addFlag(
34+
rbeFlag,
35+
defaultsTo: environment.hasRbeConfigInTree(),
36+
help: 'RBE is enabled by default when available.',
37+
);
3338
}
3439

3540
/// List of compatible builds.
@@ -48,6 +53,11 @@ et test //flutter/fml:fml_benchmarks # Run a single test target in `//flutter/f
4853
@override
4954
Future<int> run() async {
5055
final String configName = argResults![configFlag] as String;
56+
final bool useRbe = argResults![rbeFlag] as bool;
57+
if (useRbe && !environment.hasRbeConfigInTree()) {
58+
environment.logger.error('RBE was requested but no RBE config was found');
59+
return 1;
60+
}
5161
final String demangledName = demangleConfigName(environment, configName);
5262
final Build? build =
5363
builds.where((Build build) => build.name == demangledName).firstOrNull;
@@ -61,6 +71,7 @@ et test //flutter/fml:fml_benchmarks # Run a single test target in `//flutter/f
6171
build,
6272
argResults!.rest,
6373
defaultToAll: true,
74+
enableRbe: useRbe,
6475
);
6576
if (selectedTargets == null) {
6677
// The user typed something wrong and targetsFromCommandLine has already
@@ -90,8 +101,12 @@ et test //flutter/fml:fml_benchmarks # Run a single test target in `//flutter/f
90101
.toList();
91102
// TODO(johnmccutchan): runBuild manipulates buildTargets and adds some
92103
// targets listed in Build. Fix this.
93-
final int buildExitCode =
94-
await runBuild(environment, build, targets: buildTargets);
104+
final int buildExitCode = await runBuild(
105+
environment,
106+
build,
107+
targets: buildTargets,
108+
enableRbe: useRbe,
109+
);
95110
if (buildExitCode != 0) {
96111
return buildExitCode;
97112
}

tools/engine_tool/lib/src/environment.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// found in the LICENSE file.
44

55
import 'dart:ffi' as ffi show Abi;
6+
import 'dart:io' as io show Directory;
67

78
import 'package:engine_repo_tools/engine_repo_tools.dart';
9+
import 'package:path/path.dart' as p;
810
import 'package:platform/platform.dart';
911
import 'package:process_runner/process_runner.dart';
1012

@@ -44,4 +46,21 @@ final class Environment {
4446

4547
/// Facility for commands to run subprocesses.
4648
final ProcessRunner processRunner;
49+
50+
/// Whether it appears that the current environment supports remote builds.
51+
///
52+
/// This is a heuristic based on the presence of certain directories in the
53+
/// engine repo; it is not a guarantee that remote builds will work (due to
54+
/// authentication, network, or other issues).
55+
///
56+
/// **Note**: This calls does synchronous I/O.
57+
bool hasRbeConfigInTree() {
58+
final String rbeConfigPath = p.join(
59+
engine.srcDir.path,
60+
'flutter',
61+
'build',
62+
'rbe',
63+
);
64+
return io.Directory(rbeConfigPath).existsSync();
65+
}
4766
}

tools/engine_tool/lib/src/gn_utils.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ Future<List<BuildTarget>?> targetsFromCommandLine(
222222
Build build,
223223
List<String> commandLineTargets, {
224224
bool defaultToAll = false,
225+
required bool enableRbe,
225226
}) async {
226227
// If there are no targets specified on the command line, then delegate to
227228
// the default targets specified in the Build object unless directed
@@ -240,7 +241,7 @@ Future<List<BuildTarget>?> targetsFromCommandLine(
240241
environment.logger.status(
241242
'Build output directory at ${buildDir.path} not found. Running GN.',
242243
);
243-
final int gnResult = await runGn(environment, build);
244+
final int gnResult = await runGn(environment, build, enableRbe: enableRbe);
244245
if (gnResult != 0 || !buildDir.existsSync()) {
245246
environment.logger.error(
246247
'The specified build did not produce the expected build '

tools/engine_tool/test/build_command_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:engine_build_configs/engine_build_configs.dart';
99
import 'package:engine_tool/src/build_utils.dart';
1010
import 'package:engine_tool/src/commands/command_runner.dart';
1111
import 'package:engine_tool/src/environment.dart';
12+
import 'package:engine_tool/src/logger.dart';
1213
import 'package:litetest/litetest.dart';
1314
import 'package:path/path.dart' as path;
1415
import 'package:platform/platform.dart';
@@ -176,6 +177,33 @@ void main() {
176177
}
177178
});
178179

180+
test('build command fails when rbe is enabled but not supported', () async {
181+
final TestEnvironment testEnv = TestEnvironment.withTestEngine(
182+
cannedProcesses: cannedProcesses,
183+
// Intentionally omit withRbe: true.
184+
// That means the //flutter/build/rbe directory will not be created.
185+
);
186+
try {
187+
final ToolCommandRunner runner = ToolCommandRunner(
188+
environment: testEnv.environment,
189+
configs: configs,
190+
);
191+
final int result = await runner.run(<String>[
192+
'build',
193+
'--config',
194+
'ci/android_debug_rbe_arm64',
195+
'--rbe',
196+
]);
197+
expect(result, equals(1));
198+
expect(
199+
testEnv.testLogs.map((LogRecord r) => r.message).join(),
200+
contains('RBE was requested but no RBE config was found'),
201+
);
202+
} finally {
203+
testEnv.cleanup();
204+
}
205+
});
206+
179207
test('build command does not run rbe when disabled', () async {
180208
final TestEnvironment testEnv = TestEnvironment.withTestEngine(
181209
withRbe: true,

tools/engine_tool/test/gn_utils_test.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ void main() {
9191
(Build build) => build.name == 'linux/host_debug',
9292
).firstOrNull;
9393
final List<BuildTarget>? selectedTargets = await targetsFromCommandLine(
94-
env, build!, <String>[],
94+
env,
95+
build!,
96+
<String>[],
97+
enableRbe: false,
9598
);
9699
expect(selectedTargets, isNotNull);
97100
expect(selectedTargets, isEmpty);
@@ -111,7 +114,11 @@ void main() {
111114
(Build build) => build.name == 'linux/host_debug',
112115
).firstOrNull;
113116
final List<BuildTarget>? selectedTargets = await targetsFromCommandLine(
114-
env, build!, <String>[], defaultToAll: true,
117+
env,
118+
build!,
119+
<String>[],
120+
defaultToAll: true,
121+
enableRbe: false,
115122
);
116123
expect(selectedTargets, isNotNull);
117124
expect(selectedTargets, isNotEmpty);

0 commit comments

Comments
 (0)