summaryrefslogtreecommitdiffstats
path: root/tools/gn/command_check.cc
blob: 584803e2ef55b2851028be728b76ae89d4a96753 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "tools/gn/commands.h"
#include "tools/gn/header_checker.h"
#include "tools/gn/setup.h"
#include "tools/gn/standard_out.h"
#include "tools/gn/trace.h"

namespace commands {

const char kCheck[] = "check";
const char kCheck_HelpShort[] =
    "check: Check header dependencies.";
const char kCheck_Help[] =
    "gn check <out_dir> [<target label>] [--force]\n"
    "\n"
    "  \"gn check\" is the same thing as \"gn gen\" with the \"--check\" flag\n"
    "  except that this command does not write out any build files. It's\n"
    "  intended to be an easy way to manually trigger include file checking.\n"
    "\n"
    "  The <label_pattern> can take exact labels or patterns that match more\n"
    "  than one (although not general regular expressions). If specified,\n"
    "  only those matching targets will be checked.\n"
    "  See \"gn help label_pattern\" for details.\n"
    "\n"
    "  --force\n"
    "      Ignores specifications of \"check_includes = false\" and checks\n"
    "      all target's files that match the target label.\n"
    "\n"
    "  See \"gn help\" for the common command-line switches.\n"
    "\n"
    "Examples\n"
    "\n"
    "  gn check out/Debug\n"
    "      Check everything.\n"
    "\n"
    "  gn check out/Default //foo:bar\n"
    "      Check only the files in the //foo:bar target.\n"
    "\n"
    "  gn check out/Default \"//foo/*\n"
    "      Check only the files in targets in the //foo directory tree.\n";

int RunCheck(const std::vector<std::string>& args) {
  if (args.size() != 1 && args.size() != 2) {
    Err(Location(), "You're holding it wrong.",
        "Usage: \"gn check <out_dir> [<target_label>]\"").PrintToStdout();
    return 1;
  }

  // Deliberately leaked to avoid expensive process teardown.
  Setup* setup = new Setup();
  if (!setup->DoSetup(args[0], false))
    return 1;
  if (!setup->Run())
    return 1;

  std::vector<const Target*> targets_to_check;
  if (args.size() == 2) {
    // Compute the target to check (empty means everything).
    if (!ResolveTargetsFromCommandLinePattern(setup, args[1], false,
                                              &targets_to_check))
      return 1;
    if (targets_to_check.size() == 0) {
      OutputString("No matching targets.\n");
      return 1;
    }
  }

  const CommandLine* cmdline = CommandLine::ForCurrentProcess();
  bool force = cmdline->HasSwitch("force");

  if (!CheckPublicHeaders(&setup->build_settings(),
                          setup->builder()->GetAllResolvedTargets(),
                          targets_to_check,
                          force))
    return 1;

  OutputString("Header dependency check OK\n", DECORATION_GREEN);
  return 0;
}

bool CheckPublicHeaders(const BuildSettings* build_settings,
                        const std::vector<const Target*>& all_targets,
                        const std::vector<const Target*>& to_check,
                        bool force_check) {
  ScopedTrace trace(TraceItem::TRACE_CHECK_HEADERS, "Check headers");

  scoped_refptr<HeaderChecker> header_checker(
      new HeaderChecker(build_settings, all_targets));

  std::vector<Err> header_errors;
  header_checker->Run(to_check, force_check, &header_errors);
  for (size_t i = 0; i < header_errors.size(); i++) {
    if (i > 0)
      OutputString("___________________\n", DECORATION_YELLOW);
    header_errors[i].PrintToStdout();
  }
  return header_errors.empty();
}

}  // namespace commands