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 "base/at_exit.h"
      6 #include "base/command_line.h"
      7 #include "base/strings/utf_string_conversions.h"
      8 #include "tools/gn/commands.h"
      9 #include "tools/gn/err.h"
     10 #include "tools/gn/location.h"
     11 
     12 namespace {
     13 
     14 std::vector<std::string> GetArgs(const CommandLine& cmdline) {
     15   CommandLine::StringVector in_args = cmdline.GetArgs();
     16 #if defined(OS_WIN)
     17   std::vector<std::string> out_args;
     18   for (size_t i = 0; i < in_args.size(); i++)
     19     out_args.push_back(base::WideToUTF8(in_args[i]));
     20   return out_args;
     21 #else
     22   return in_args;
     23 #endif
     24 }
     25 
     26 }  // namespace
     27 
     28 int main(int argc, char** argv) {
     29   base::AtExitManager at_exit;
     30   CommandLine::Init(argc, argv);
     31 
     32   const CommandLine& cmdline = *CommandLine::ForCurrentProcess();
     33   std::vector<std::string> args = GetArgs(cmdline);
     34 
     35   std::string command;
     36   if (cmdline.HasSwitch("help")) {
     37     // Make "--help" default to help command.
     38     command = commands::kHelp;
     39   } else if (args.empty()) {
     40     command = commands::kGen;
     41   } else {
     42     command = args[0];
     43     args.erase(args.begin());
     44   }
     45 
     46   const commands::CommandInfoMap& command_map = commands::GetCommands();
     47   commands::CommandInfoMap::const_iterator found_command =
     48       command_map.find(command);
     49 
     50   int retval;
     51   if (found_command != command_map.end()) {
     52     retval = found_command->second.runner(args);
     53   } else {
     54     Err(Location(),
     55         "Command \"" + command + "\" unknown.").PrintToStdout();
     56     commands::RunHelp(std::vector<std::string>());
     57     retval = 1;
     58   }
     59 
     60   exit(retval);  // Don't free memory, it can be really slow!
     61   return retval;
     62 }
     63