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