• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
7 
8 #include "base/command_line.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "gn/commands.h"
11 #include "gn/err.h"
12 #include "gn/location.h"
13 #include "gn/standard_out.h"
14 #include "gn/switches.h"
15 #include "util/build_config.h"
16 #include "util/msg_loop.h"
17 #include "util/sys_info.h"
18 
19 #include "last_commit_position.h"
20 
21 namespace {
22 
GetArgs(const base::CommandLine & cmdline)23 std::vector<std::string> GetArgs(const base::CommandLine& cmdline) {
24   base::CommandLine::StringVector in_args = cmdline.GetArgs();
25 #if defined(OS_WIN)
26   std::vector<std::string> out_args;
27   for (const auto& arg : in_args)
28     out_args.push_back(base::UTF16ToUTF8(arg));
29   return out_args;
30 #else
31   return in_args;
32 #endif
33 }
34 
35 }  // namespace
36 
main(int argc,char ** argv)37 int main(int argc, char** argv) {
38 #if defined(OS_WIN)
39   base::CommandLine::set_slash_is_not_a_switch();
40 #endif
41   base::CommandLine::Init(argc, argv);
42 
43   const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
44   std::vector<std::string> args = GetArgs(cmdline);
45 
46   std::string command;
47   if (cmdline.HasSwitch("help") || cmdline.HasSwitch("h")) {
48     // Make "-h" and "--help" default to help command.
49     command = commands::kHelp;
50   } else if (cmdline.HasSwitch(switches::kVersion)) {
51     // Make "--version" print the version and exit.
52     OutputString(std::string(LAST_COMMIT_POSITION) + "\n");
53     exit(0);
54   } else if (args.empty()) {
55     // No command, print error and exit.
56     Err(Location(), "No command specified.",
57         "Most commonly you want \"gn gen <out_dir>\" to make a build dir.\n"
58         "Or try \"gn help\" for more commands.")
59         .PrintToStdout();
60     return 1;
61   } else {
62     command = args[0];
63     args.erase(args.begin());
64   }
65 
66   if (!commands::CommandSwitches::Init(cmdline))
67     return 1;
68 
69   const commands::CommandInfoMap& command_map = commands::GetCommands();
70   commands::CommandInfoMap::const_iterator found_command =
71       command_map.find(command);
72 
73   int retval;
74   if (found_command != command_map.end()) {
75     MsgLoop msg_loop;
76     retval = found_command->second.runner(args);
77   } else {
78     Err(Location(), "Command \"" + command + "\" unknown.").PrintToStdout();
79     OutputString(
80         "Available commands (type \"gn help <command>\" for more details):\n");
81     for (const auto& cmd : commands::GetCommands())
82       PrintShortHelp(cmd.second.help_short);
83 
84     retval = 1;
85   }
86 
87   exit(retval);  // Don't free memory, it can be really slow!
88 }
89