Home | History | Annotate | Download | only in gn
      1 // Copyright (c) 2013 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 <algorithm>
      6 #include <iostream>
      7 
      8 #include "tools/gn/args.h"
      9 #include "tools/gn/commands.h"
     10 #include "tools/gn/err.h"
     11 #include "tools/gn/functions.h"
     12 #include "tools/gn/input_conversion.h"
     13 #include "tools/gn/label_pattern.h"
     14 #include "tools/gn/setup.h"
     15 #include "tools/gn/standard_out.h"
     16 #include "tools/gn/substitution_writer.h"
     17 #include "tools/gn/variables.h"
     18 
     19 namespace commands {
     20 
     21 namespace {
     22 
     23 void PrintToplevelHelp() {
     24   OutputString("Commands (type \"gn help <command>\" for more details):\n");
     25 
     26   const commands::CommandInfoMap& command_map = commands::GetCommands();
     27   for (commands::CommandInfoMap::const_iterator i = command_map.begin();
     28        i != command_map.end(); ++i)
     29     PrintShortHelp(i->second.help_short);
     30 
     31   OutputString(
     32       "\n"
     33       "Common switches:\n");
     34   PrintShortHelp(
     35       "--args: Specifies build arguments overrides. "
     36       "See \"gn help buildargs\".");
     37   PrintShortHelp(
     38       "--[no]color: Forces colored output on or off (rather than autodetect).");
     39   PrintShortHelp(
     40       "--dotfile: Specifies an alternate .gn file. See \"gn help dotfile\".");
     41   PrintShortHelp(
     42       "--no-exec: Skips exec_script calls (for performance testing).");
     43   PrintShortHelp(
     44       "-q: Quiet mode, don't print anything on success.");
     45   PrintShortHelp(
     46       "--root: Specifies source root (overrides .gn file).");
     47   PrintShortHelp(
     48       "--time: Outputs a summary of how long everything took.");
     49   PrintShortHelp(
     50       "--tracelog: Writes a Chrome-compatible trace log to the given file.");
     51   PrintShortHelp(
     52       "-v: Verbose mode, print lots of logging.");
     53   PrintShortHelp(
     54       "--version: Print the GN binary's version and exit.");
     55 
     56   // Target declarations.
     57   OutputString("\nTarget declarations (type \"gn help <function>\" for more "
     58                "details):\n");
     59   const functions::FunctionInfoMap& function_map = functions::GetFunctions();
     60   for (functions::FunctionInfoMap::const_iterator i = function_map.begin();
     61        i != function_map.end(); ++i) {
     62     if (i->second.is_target)
     63       PrintShortHelp(i->second.help_short);
     64   }
     65 
     66   // Functions.
     67   OutputString("\nBuildfile functions (type \"gn help <function>\" for more "
     68                "details):\n");
     69   for (functions::FunctionInfoMap::const_iterator i = function_map.begin();
     70        i != function_map.end(); ++i) {
     71     if (!i->second.is_target)
     72       PrintShortHelp(i->second.help_short);
     73   }
     74 
     75   // Built-in variables.
     76   OutputString("\nBuilt-in predefined variables (type \"gn help <variable>\" "
     77                "for more details):\n");
     78   const variables::VariableInfoMap& builtin_vars =
     79       variables::GetBuiltinVariables();
     80   for (variables::VariableInfoMap::const_iterator i = builtin_vars.begin();
     81        i != builtin_vars.end(); ++i)
     82     PrintShortHelp(i->second.help_short);
     83 
     84   // Target variables.
     85   OutputString("\nVariables you set in targets (type \"gn help <variable>\" "
     86                "for more details):\n");
     87   const variables::VariableInfoMap& target_vars =
     88       variables::GetTargetVariables();
     89   for (variables::VariableInfoMap::const_iterator i = target_vars.begin();
     90        i != target_vars.end(); ++i)
     91     PrintShortHelp(i->second.help_short);
     92 
     93   OutputString("\nOther help topics:\n");
     94   PrintShortHelp("buildargs: How build arguments work.");
     95   PrintShortHelp("dotfile: Info about the toplevel .gn file.");
     96   PrintShortHelp("label_pattern: Matching more than one label.");
     97   PrintShortHelp(
     98       "input_conversion: Processing input from exec_script and read_file.");
     99   PrintShortHelp("source_expansion: Map sources to outputs for scripts.");
    100 }
    101 
    102 }  // namespace
    103 
    104 const char kHelp[] = "help";
    105 const char kHelp_HelpShort[] =
    106     "help: Does what you think.";
    107 const char kHelp_Help[] =
    108     "gn help <anything>\n"
    109     "  Yo dawg, I heard you like help on your help so I put help on the help\n"
    110     "  in the help.\n";
    111 
    112 int RunHelp(const std::vector<std::string>& args) {
    113   if (args.size() == 0) {
    114     PrintToplevelHelp();
    115     return 0;
    116   }
    117 
    118   // Check commands.
    119   const commands::CommandInfoMap& command_map = commands::GetCommands();
    120   commands::CommandInfoMap::const_iterator found_command =
    121       command_map.find(args[0]);
    122   if (found_command != command_map.end()) {
    123     PrintLongHelp(found_command->second.help);
    124     return 0;
    125   }
    126 
    127   // Check functions.
    128   const functions::FunctionInfoMap& function_map = functions::GetFunctions();
    129   functions::FunctionInfoMap::const_iterator found_function =
    130       function_map.find(args[0]);
    131   if (found_function != function_map.end()) {
    132     PrintLongHelp(found_function->second.help);
    133     return 0;
    134   }
    135 
    136   // Builtin variables.
    137   const variables::VariableInfoMap& builtin_vars =
    138       variables::GetBuiltinVariables();
    139   variables::VariableInfoMap::const_iterator found_builtin_var =
    140       builtin_vars.find(args[0]);
    141   if (found_builtin_var != builtin_vars.end()) {
    142     PrintLongHelp(found_builtin_var->second.help);
    143     return 0;
    144   }
    145 
    146   // Target variables.
    147   const variables::VariableInfoMap& target_vars =
    148       variables::GetTargetVariables();
    149   variables::VariableInfoMap::const_iterator found_target_var =
    150       target_vars.find(args[0]);
    151   if (found_target_var != target_vars.end()) {
    152     PrintLongHelp(found_target_var->second.help);
    153     return 0;
    154   }
    155 
    156   // Random other topics.
    157   if (args[0] == "buildargs") {
    158     PrintLongHelp(kBuildArgs_Help);
    159     return 0;
    160   }
    161   if (args[0] == "dotfile") {
    162     PrintLongHelp(kDotfile_Help);
    163     return 0;
    164   }
    165   if (args[0] == "input_conversion") {
    166     PrintLongHelp(kInputConversion_Help);
    167     return 0;
    168   }
    169   if (args[0] == "label_pattern") {
    170     PrintLongHelp(kLabelPattern_Help);
    171     return 0;
    172   }
    173   if (args[0] == "source_expansion") {
    174     PrintLongHelp(kSourceExpansion_Help);
    175     return 0;
    176   }
    177 
    178   // No help on this.
    179   Err(Location(), "No help on \"" + args[0] + "\".").PrintToStdout();
    180   RunHelp(std::vector<std::string>());
    181   return 1;
    182 }
    183 
    184 }  // namespace commands
    185