Revert of Eliminate components_unittests' dependence on chrome resources. (patchset...
[chromium-blink-merge.git] / tools / gn / gn_main.cc
blob50d55e72a7239542a924eaf8c878d0b9f211f2e8
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.
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 #include "tools/gn/standard_out.h"
12 #include "tools/gn/switches.h"
14 // Only the GN-generated build makes this header for now.
15 // TODO(brettw) consider adding this if we need it in GYP.
16 #if defined(GN_BUILD)
17 #include "tools/gn/last_commit_position.h"
18 #else
19 #define LAST_COMMIT_POSITION "UNKNOWN"
20 #endif
22 namespace {
24 std::vector<std::string> GetArgs(const base::CommandLine& cmdline) {
25 base::CommandLine::StringVector in_args = cmdline.GetArgs();
26 #if defined(OS_WIN)
27 std::vector<std::string> out_args;
28 for (const auto& arg : in_args)
29 out_args.push_back(base::WideToUTF8(arg));
30 return out_args;
31 #else
32 return in_args;
33 #endif
36 } // namespace
38 int main(int argc, char** argv) {
39 base::AtExitManager at_exit;
40 #if defined(OS_WIN)
41 base::CommandLine::set_slash_is_not_a_switch();
42 #endif
43 base::CommandLine::Init(argc, argv);
45 const base::CommandLine& cmdline = *base::CommandLine::ForCurrentProcess();
46 std::vector<std::string> args = GetArgs(cmdline);
48 std::string command;
49 if (cmdline.HasSwitch("help") || cmdline.HasSwitch("h")) {
50 // Make "-h" and "--help" default to help command.
51 command = commands::kHelp;
52 } else if (cmdline.HasSwitch(switches::kVersion)) {
53 // Make "--version" print the version and exit.
54 OutputString(std::string(LAST_COMMIT_POSITION) + "\n");
55 exit(0);
56 } else if (args.empty()) {
57 // No command, print error and exit.
58 Err(Location(), "No command specified.",
59 "Most commonly you want \"gn gen <out_dir>\" to make a build dir.\n"
60 "Or try \"gn help\" for more commands.").PrintToStdout();
61 return 1;
62 } else {
63 command = args[0];
64 args.erase(args.begin());
67 const commands::CommandInfoMap& command_map = commands::GetCommands();
68 commands::CommandInfoMap::const_iterator found_command =
69 command_map.find(command);
71 int retval;
72 if (found_command != command_map.end()) {
73 retval = found_command->second.runner(args);
74 } else {
75 Err(Location(),
76 "Command \"" + command + "\" unknown.").PrintToStdout();
77 commands::RunHelp(std::vector<std::string>());
78 retval = 1;
81 exit(retval); // Don't free memory, it can be really slow!