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 "tools/gn/commands.h"
6 #include "tools/gn/item.h"
7 #include "tools/gn/label.h"
8 #include "tools/gn/setup.h"
9 #include "tools/gn/standard_out.h"
10 #include "tools/gn/target.h"
11
12 namespace commands {
13
CommandInfo()14 CommandInfo::CommandInfo()
15 : help_short(NULL),
16 help(NULL),
17 runner(NULL) {
18 }
19
CommandInfo(const char * in_help_short,const char * in_help,CommandRunner in_runner)20 CommandInfo::CommandInfo(const char* in_help_short,
21 const char* in_help,
22 CommandRunner in_runner)
23 : help_short(in_help_short),
24 help(in_help),
25 runner(in_runner) {
26 }
27
GetCommands()28 const CommandInfoMap& GetCommands() {
29 static CommandInfoMap info_map;
30 if (info_map.empty()) {
31 #define INSERT_COMMAND(cmd) \
32 info_map[k##cmd] = CommandInfo(k##cmd##_HelpShort, \
33 k##cmd##_Help, \
34 &Run##cmd);
35
36 INSERT_COMMAND(Args)
37 INSERT_COMMAND(Desc)
38 INSERT_COMMAND(Gen)
39 INSERT_COMMAND(Gyp)
40 INSERT_COMMAND(Help)
41 INSERT_COMMAND(Refs)
42
43 #undef INSERT_COMMAND
44 }
45 return info_map;
46 }
47
GetTargetForDesc(const std::vector<std::string> & args)48 const Target* GetTargetForDesc(const std::vector<std::string>& args) {
49 // Deliberately leaked to avoid expensive process teardown.
50 Setup* setup = new Setup;
51 if (!setup->DoSetup())
52 return NULL;
53
54 // TODO(brettw): set the output dir to be a sandbox one to avoid polluting
55 // the real output dir with files written by the build scripts.
56
57 // Do the actual load. This will also write out the target ninja files.
58 if (!setup->Run())
59 return NULL;
60
61 // Need to resolve the label after we know the default toolchain.
62 // TODO(brettw) find the current directory and resolve the input label
63 // relative to that.
64 Label default_toolchain = setup->loader()->default_toolchain_label();
65 Value arg_value(NULL, args[0]);
66 Err err;
67 Label label =
68 Label::Resolve(SourceDir("//"), default_toolchain, arg_value, &err);
69 if (err.has_error()) {
70 err.PrintToStdout();
71 return NULL;
72 }
73
74 const Item* item = setup->builder()->GetItem(label);
75 if (!item) {
76 Err(Location(), "Label not found.",
77 label.GetUserVisibleName(false) + " not found.").PrintToStdout();
78 return NULL;
79 }
80
81 const Target* target = item->AsTarget();
82 if (!target) {
83 Err(Location(), "Not a target.",
84 "The \"" + label.GetUserVisibleName(false) + "\" thing\n"
85 "is not a target. Somebody should probably implement this command for "
86 "other\nitem types.");
87 return NULL;
88 }
89
90 return target;
91 }
92
93 } // namespace commands
94