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. 4 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" 10 11 namespace commands { 12 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" 18 "\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" 22 "\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" 27 "\n" 28 " --force\n" 29 " Ignores specifications of \"check_includes = false\" and checks\n" 30 " all target's files that match the target label.\n" 31 "\n" 32 " See \"gn help\" for the common command-line switches.\n" 33 "\n" 34 "Examples\n" 35 "\n" 36 " gn check out/Debug\n" 37 " Check everything.\n" 38 "\n" 39 " gn check out/Default //foo:bar\n" 40 " Check only the files in the //foo:bar target.\n" 41 "\n" 42 " gn check out/Default \"//foo/*\n" 43 " Check only the files in targets in the //foo directory tree.\n"; 44 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(); 49 return 1; 50 } 51 52 // Deliberately leaked to avoid expensive process teardown. 53 Setup* setup = new Setup(); 54 if (!setup->DoSetup(args[0], false)) 55 return 1; 56 if (!setup->Run()) 57 return 1; 58 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, 63 &targets_to_check)) 64 return 1; 65 if (targets_to_check.size() == 0) { 66 OutputString("No matching targets.\n"); 67 return 1; 68 } 69 } 70 71 const CommandLine* cmdline = CommandLine::ForCurrentProcess(); 72 bool force = cmdline->HasSwitch("force"); 73 74 if (!CheckPublicHeaders(&setup->build_settings(), 75 setup->builder()->GetAllResolvedTargets(), 76 targets_to_check, 77 force)) 78 return 1; 79 80 OutputString("Header dependency check OK\n", DECORATION_GREEN); 81 return 0; 82 } 83 84 bool CheckPublicHeaders(const BuildSettings* build_settings, 85 const std::vector<const Target*>& all_targets, 86 const std::vector<const Target*>& to_check, 87 bool force_check) { 88 ScopedTrace trace(TraceItem::TRACE_CHECK_HEADERS, "Check headers"); 89 90 scoped_refptr<HeaderChecker> header_checker( 91 new HeaderChecker(build_settings, all_targets)); 92 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++) { 96 if (i > 0) 97 OutputString("___________________\n", DECORATION_YELLOW); 98 header_errors[i].PrintToStdout(); 99 } 100 return header_errors.empty(); 101 } 102 103 } // namespace commands 104