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

Commit e667d37

Browse files
committed
Allow engine variant to be passed into linter script
1 parent d5cadd2 commit e667d37

File tree

4 files changed

+61
-22
lines changed

4 files changed

+61
-22
lines changed

ci/lint.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ cd "$SCRIPT_DIR"
4141
"$DART" \
4242
--disable-dart-dev \
4343
"$SRC_DIR/flutter/tools/clang_tidy/bin/main.dart" \
44-
--compile-commands="$COMPILE_COMMANDS" \
4544
--repo="$SRC_DIR/flutter" \
45+
--src-dir="$SRC_DIR" \
4646
"$@"

tools/clang_tidy/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# clang_tidy
22

3-
This is a Dart program/library that runs clang_tidy over modified files. It
4-
takes two mandatory arguments that point at a compile_commands.json command
5-
and the root of the Flutter engine repo:
3+
This is a Dart program/library that runs clang_tidy over modified files in the Flutter engine repo.
4+
5+
By default the linter runs on the repo files changed contained in `src/out/host_debug/compile_commands.json` command.
6+
To check files other than in `host_debug` use `--target-variant android_debug_unopt`,
7+
`--target-variant ios_debug_sim_unopt`, etc.
8+
9+
Alternatively, use `--compile-commands` to specify a path to a `compile_commands.json` file.
610

711
```
12+
$ bin/main.dart --target-variant <engine-variant> --repo <path-to-repo>
813
$ bin/main.dart --compile-commands <compile_commands.json-path> --repo <path-to-repo>
14+
$ bin/main.dart --help
915
```

tools/clang_tidy/lib/src/options.dart

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import 'dart:io' as io show Directory, File, Platform, stderr;
66

77
import 'package:args/args.dart';
8+
import 'package:path/path.dart' as path;
9+
10+
// Path to root of the flutter/engine repository containing this script.
11+
final String _engineRoot = path.dirname(path.dirname(path.dirname(path.dirname(path.fromUri(io.Platform.script)))));
812

913
/// A class for organizing the options to the Engine linter, and the files
1014
/// that it operates on.
@@ -49,12 +53,13 @@ class Options {
4953
/// Builds an [Options] instance with an [ArgResults] instance.
5054
factory Options._fromArgResults(
5155
ArgResults options, {
56+
required io.File buildCommandsPath,
5257
StringSink? errSink,
5358
}) {
5459
return Options(
5560
help: options['help'] as bool,
5661
verbose: options['verbose'] as bool,
57-
buildCommandsPath: io.File(options['compile-commands'] as String),
62+
buildCommandsPath: buildCommandsPath,
5863
repoPath: io.Directory(options['repo'] as String),
5964
checksArg: options.wasParsed('checks') ? options['checks'] as String : '',
6065
lintAll: io.Platform.environment['FLUTTER_LINT_ALL'] != null ||
@@ -70,7 +75,17 @@ class Options {
7075
StringSink? errSink,
7176
}) {
7277
final ArgResults argResults = _argParser.parse(arguments);
73-
final String? message = _checkArguments(argResults);
78+
79+
String? buildCommandsPath = argResults['compile-commands'] as String?;
80+
// path/to/engine/src/out/variant/compile_commands.json
81+
buildCommandsPath ??= path.join(
82+
argResults['src-dir'] as String,
83+
'out',
84+
argResults['target-variant'] as String,
85+
'compile_commands.json',
86+
);
87+
final io.File buildCommands = io.File(buildCommandsPath);
88+
final String? message = _checkArguments(argResults, buildCommands);
7489
if (message != null) {
7590
return Options._error(message, errSink: errSink);
7691
}
@@ -79,13 +94,15 @@ class Options {
7994
}
8095
return Options._fromArgResults(
8196
argResults,
97+
buildCommandsPath: buildCommands,
8298
errSink: errSink,
8399
);
84100
}
85101

86102
static final ArgParser _argParser = ArgParser()
87103
..addFlag(
88104
'help',
105+
abbr: 'h',
89106
help: 'Print help.',
90107
)
91108
..addFlag(
@@ -112,6 +129,20 @@ class Options {
112129
help: 'Use the given path as the source of compile_commands.json. This '
113130
'file is created by running tools/gn',
114131
)
132+
..addOption(
133+
'target-variant',
134+
aliases: <String>['variant'],
135+
help: 'The engine variant directory containing compile_commands.json '
136+
'created by running tools/gn. Ignored if --compile-commands is also passed.',
137+
valueHelp: 'host_debug|android_debug_unopt|ios_debug|ios_debug_sim_unopt',
138+
defaultsTo: 'host_debug',
139+
)
140+
..addOption(
141+
'src-dir',
142+
help: 'Path to the engine src directory. Ignored if --compile-commands is also passed.',
143+
valueHelp: 'path/to/engine/src',
144+
defaultsTo: path.dirname(_engineRoot),
145+
)
115146
..addOption(
116147
'checks',
117148
help: 'Perform the given checks on the code. Defaults to the empty '
@@ -156,26 +187,21 @@ class Options {
156187
_errSink.writeln(message);
157188
}
158189
_errSink.writeln(
159-
'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch]',
190+
'Usage: bin/main.dart [--help] [--lint-all] [--fix] [--verbose] [--diff-branch] [--target-variant variant] [--src-dir path/to/engine/src]',
160191
);
161192
_errSink.writeln(_argParser.usage);
162193
}
163194

164195
/// Command line argument validation.
165-
static String? _checkArguments(ArgResults argResults) {
196+
static String? _checkArguments(ArgResults argResults, io.File buildCommandsPath) {
166197
if (argResults.wasParsed('help')) {
167198
return null;
168199
}
169200

170-
if (!argResults.wasParsed('compile-commands')) {
171-
return 'ERROR: The --compile-commands argument is required.';
172-
}
173-
174201
if (!argResults.wasParsed('repo')) {
175202
return 'ERROR: The --repo argument is required.';
176203
}
177204

178-
final io.File buildCommandsPath = io.File(argResults['compile-commands'] as String);
179205
if (!buildCommandsPath.existsSync()) {
180206
return "ERROR: Build commands path ${buildCommandsPath.absolute.path} doesn't exist.";
181207
}

tools/clang_tidy/test/clang_tidy_test.dart

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:process_runner/process_runner.dart';
1212
Future<int> main(List<String> args) async {
1313
if (args.length < 2) {
1414
io.stderr.writeln(
15-
'Usage: clang_tidy_test.dart [build commands] [repo root]',
15+
'Usage: clang_tidy_test.dart [path/to/compile_commands.json] [repo root]',
1616
);
1717
return 1;
1818
}
@@ -37,11 +37,14 @@ Future<int> main(List<String> args) async {
3737
expect(errBuffer.toString(), contains('Usage: '));
3838
});
3939

40-
test('Error when --compile-commands is missing', () async {
40+
test('Error when --repo is missing', () async {
4141
final StringBuffer outBuffer = StringBuffer();
4242
final StringBuffer errBuffer = StringBuffer();
4343
final ClangTidy clangTidy = ClangTidy.fromCommandLine(
44-
<String>[],
44+
<String>[
45+
'--compile-commands',
46+
'/unused',
47+
],
4548
outSink: outBuffer,
4649
errSink: errBuffer,
4750
);
@@ -51,16 +54,18 @@ Future<int> main(List<String> args) async {
5154
expect(clangTidy.options.help, isFalse);
5255
expect(result, equals(1));
5356
expect(errBuffer.toString(), contains(
54-
'ERROR: The --compile-commands argument is required.',
57+
'ERROR: The --repo argument is required.',
5558
));
5659
});
5760

58-
test('Error when --repo is missing', () async {
61+
test('Error when --compile-commands path does not exist', () async {
5962
final StringBuffer outBuffer = StringBuffer();
6063
final StringBuffer errBuffer = StringBuffer();
6164
final ClangTidy clangTidy = ClangTidy.fromCommandLine(
6265
<String>[
6366
'--compile-commands',
67+
'/does/not/exist',
68+
'--repo',
6469
'/unused',
6570
],
6671
outSink: outBuffer,
@@ -72,17 +77,19 @@ Future<int> main(List<String> args) async {
7277
expect(clangTidy.options.help, isFalse);
7378
expect(result, equals(1));
7479
expect(errBuffer.toString(), contains(
75-
'ERROR: The --repo argument is required.',
80+
"ERROR: Build commands path /does/not/exist doesn't exist.",
7681
));
7782
});
7883

79-
test('Error when --compile-commands path does not exist', () async {
84+
test('Error when --src-dir path does not exist, uses target variant in path', () async {
8085
final StringBuffer outBuffer = StringBuffer();
8186
final StringBuffer errBuffer = StringBuffer();
8287
final ClangTidy clangTidy = ClangTidy.fromCommandLine(
8388
<String>[
84-
'--compile-commands',
89+
'--src-dir',
8590
'/does/not/exist',
91+
'--target-variant',
92+
'ios_debug_unopt',
8693
'--repo',
8794
'/unused',
8895
],
@@ -95,7 +102,7 @@ Future<int> main(List<String> args) async {
95102
expect(clangTidy.options.help, isFalse);
96103
expect(result, equals(1));
97104
expect(errBuffer.toString(), contains(
98-
"ERROR: Build commands path /does/not/exist doesn't exist.",
105+
"ERROR: Build commands path /does/not/exist/out/ios_debug_unopt/compile_commands.json doesn't exist.",
99106
));
100107
});
101108

0 commit comments

Comments
 (0)