|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:io' as io; |
| 7 | + |
| 8 | +import 'package:args/args.dart'; |
| 9 | +import 'package:engine_repo_tools/engine_repo_tools.dart'; |
| 10 | +import 'package:path/path.dart' as p; |
| 11 | + |
| 12 | +void main(List<String> args) { |
| 13 | + final Engine? engine = Engine.tryFindWithin(); |
| 14 | + final ArgParser parser = ArgParser() |
| 15 | + ..addFlag( |
| 16 | + 'help', |
| 17 | + abbr: 'h', |
| 18 | + help: 'Print this usage information.', |
| 19 | + negatable: false, |
| 20 | + ) |
| 21 | + ..addOption( |
| 22 | + 'clangd', |
| 23 | + help: 'Path to clangd. Defaults to deriving the path from compile_commands.json.', |
| 24 | + ) |
| 25 | + ..addOption( |
| 26 | + 'compile-commands-dir', |
| 27 | + help: 'Path to a directory containing compile_commands.json.', |
| 28 | + defaultsTo: engine?.latestOutput()?.compileCommandsJson.parent.path, |
| 29 | + ); |
| 30 | + final ArgResults results = parser.parse(args); |
| 31 | + if (results['help'] as bool) { |
| 32 | + io.stdout.writeln(parser.usage); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + final String? compileCommandsDir = results['compile-commands-dir'] as String?; |
| 37 | + if (compileCommandsDir == null) { |
| 38 | + io.stderr.writeln('Must provide a path to compile_commands.json'); |
| 39 | + io.exitCode = 1; |
| 40 | + return; |
| 41 | + } |
| 42 | + final io.File compileCommandsFile = io.File(p.join(compileCommandsDir, 'compile_commands.json')); |
| 43 | + if (!compileCommandsFile.existsSync()) { |
| 44 | + io.stderr.writeln('No compile_commands.json found in $compileCommandsDir'); |
| 45 | + io.exitCode = 1; |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + final List<Object?> compileCommands = json.decode(compileCommandsFile.readAsStringSync()) as List<Object?>; |
| 50 | + if (compileCommands.isEmpty) { |
| 51 | + io.stderr.writeln('Unexpected: compile_commands.json is empty'); |
| 52 | + io.exitCode = 1; |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + String? clangd = results['clangd'] as String?; |
| 57 | + final Map<String, Object?> entry = compileCommands.first! as Map<String, Object?>; |
| 58 | + final String checkFile; |
| 59 | + if (entry case { |
| 60 | + 'command': final String command, |
| 61 | + 'file': final String file, |
| 62 | + }) { |
| 63 | + // Given a path like ../../flutter/foo.cc, we want to check foo.cc. |
| 64 | + checkFile = p.split(file).skip(3).join(p.separator); |
| 65 | + // On CI, the command path is different from the local path. |
| 66 | + // Find the engine root and derive the clangd path from there. |
| 67 | + if (clangd == null) { |
| 68 | + // Strip the command to find the path to the engine root. |
| 69 | + // i.e. "command": "/path/to/engine/src/... arg1 arg2 ..." |
| 70 | + // |
| 71 | + // This now looks like "../../flutter/buildtools/{platform}/{...}" |
| 72 | + final String commandPath = p.dirname(command.split(' ').first); |
| 73 | + |
| 74 | + // Find the canonical path to the command (i.e. resolve "../" and ".") |
| 75 | + // |
| 76 | + // This now looks like "/path/to/engine/src/flutter/buildtools/{platform}/{...}" |
| 77 | + final String path = p.canonicalize( |
| 78 | + p.join(compileCommandsDir, commandPath), |
| 79 | + ); |
| 80 | + |
| 81 | + // Extract which platform we're building for (e.g. linux-x64, mac-arm64, mac-x64). |
| 82 | + final String platform = RegExp( |
| 83 | + r'buildtools/([^/]+)/', |
| 84 | + ).firstMatch(path)!.group(1)!; |
| 85 | + |
| 86 | + // Find the engine root and derive the clangd path from there. |
| 87 | + final Engine compileCommandsEngineRoot = Engine.findWithin(path); |
| 88 | + clangd = p.join( |
| 89 | + // engine/src/flutter |
| 90 | + compileCommandsEngineRoot.flutterDir.path, |
| 91 | + // buildtools |
| 92 | + 'buildtools', |
| 93 | + // {platform} |
| 94 | + platform, |
| 95 | + // clangd |
| 96 | + 'clang', |
| 97 | + 'bin', |
| 98 | + 'clangd', |
| 99 | + ); |
| 100 | + } |
| 101 | + } else { |
| 102 | + io.stderr.writeln('Unexpected: compile_commands.json has an unexpected format'); |
| 103 | + io.stderr.writeln('First entry: ${const JsonEncoder.withIndent(' ').convert(entry)}'); |
| 104 | + io.exitCode = 1; |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + // Run clangd. |
| 109 | + try { |
| 110 | + final io.ProcessResult result = io.Process.runSync(clangd, <String>[ |
| 111 | + '--compile-commands-dir', |
| 112 | + compileCommandsDir, |
| 113 | + '--check=$checkFile', |
| 114 | + ]); |
| 115 | + io.stdout.write(result.stdout); |
| 116 | + io.stderr.write(result.stderr); |
| 117 | + if ((result.stderr as String).contains('Path specified by --compile-commands-dir does not exist')) { |
| 118 | + io.stdout.writeln('clangd_check failed: --compile-commands-dir does not exist'); |
| 119 | + io.exitCode = 1; |
| 120 | + } else if ((result.stderr as String).contains('Failed to resolve path')) { |
| 121 | + io.stdout.writeln('clangd_check failed: --check file does not exist'); |
| 122 | + io.exitCode = 1; |
| 123 | + } else { |
| 124 | + io.exitCode = result.exitCode; |
| 125 | + } |
| 126 | + } on io.ProcessException catch (e) { |
| 127 | + io.stderr.writeln('Failed to run clangd: $e'); |
| 128 | + io.stderr.writeln(const JsonEncoder.withIndent(' ').convert(entry)); |
| 129 | + io.exitCode = 1; |
| 130 | + } |
| 131 | +} |
0 commit comments