1 // Copyright 2014 The Chromium 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.
5 #include "tools/gn/commands.h"
6 #include "tools/gn/header_checker.h"
7 #include "tools/gn/setup.h"
8 #include "tools/gn/standard_out.h"
9 #include "tools/gn/trace.h"
13 const char kCheck
[] = "check";
14 const char kCheck_HelpShort
[] =
15 "check: Check header dependencies.";
16 const char kCheck_Help
[] =
17 "gn check <out_dir> [<target label>] [--force]\n"
19 " \"gn check\" is the same thing as \"gn gen\" with the \"--check\" flag\n"
20 " except that this command does not write out any build files. It's\n"
21 " intended to be an easy way to manually trigger include file checking.\n"
23 " The <label_pattern> can take exact labels or patterns that match more\n"
24 " than one (although not general regular expressions). If specified,\n"
25 " only those matching targets will be checked.\n"
26 " See \"gn help label_pattern\" for details.\n"
29 " Ignores specifications of \"check_includes = false\" and checks\n"
30 " all target's files that match the target label.\n"
32 " See \"gn help\" for the common command-line switches.\n"
36 " gn check out/Debug\n"
37 " Check everything.\n"
39 " gn check out/Default //foo:bar\n"
40 " Check only the files in the //foo:bar target.\n"
42 " gn check out/Default \"//foo/*\n"
43 " Check only the files in targets in the //foo directory tree.\n";
45 int RunCheck(const std::vector
<std::string
>& args
) {
46 if (args
.size() != 1 && args
.size() != 2) {
47 Err(Location(), "You're holding it wrong.",
48 "Usage: \"gn check <out_dir> [<target_label>]\"").PrintToStdout();
52 // Deliberately leaked to avoid expensive process teardown.
53 Setup
* setup
= new Setup();
54 if (!setup
->DoSetup(args
[0], false))
59 std::vector
<const Target
*> targets_to_check
;
60 if (args
.size() == 2) {
61 // Compute the target to check (empty means everything).
62 if (!ResolveTargetsFromCommandLinePattern(setup
, args
[1], false,
65 if (targets_to_check
.size() == 0) {
66 OutputString("No matching targets.\n");
71 const CommandLine
* cmdline
= CommandLine::ForCurrentProcess();
72 bool force
= cmdline
->HasSwitch("force");
74 if (!CheckPublicHeaders(&setup
->build_settings(),
75 setup
->builder()->GetAllResolvedTargets(),
80 OutputString("Header dependency check OK\n", DECORATION_GREEN
);
84 bool CheckPublicHeaders(const BuildSettings
* build_settings
,
85 const std::vector
<const Target
*>& all_targets
,
86 const std::vector
<const Target
*>& to_check
,
88 ScopedTrace
trace(TraceItem::TRACE_CHECK_HEADERS
, "Check headers");
90 scoped_refptr
<HeaderChecker
> header_checker(
91 new HeaderChecker(build_settings
, all_targets
));
93 std::vector
<Err
> header_errors
;
94 header_checker
->Run(to_check
, force_check
, &header_errors
);
95 for (size_t i
= 0; i
< header_errors
.size(); i
++) {
97 OutputString("___________________\n", DECORATION_YELLOW
);
98 header_errors
[i
].PrintToStdout();
100 return header_errors
.empty();
103 } // namespace commands