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/filesystem_utils.h"
7 #include "tools/gn/item.h"
8 #include "tools/gn/label.h"
9 #include "tools/gn/setup.h"
10 #include "tools/gn/standard_out.h"
11 #include "tools/gn/target.h"
12
13 namespace commands {
14
CommandInfo()15 CommandInfo::CommandInfo()
16 : help_short(NULL),
17 help(NULL),
18 runner(NULL) {
19 }
20
CommandInfo(const char * in_help_short,const char * in_help,CommandRunner in_runner)21 CommandInfo::CommandInfo(const char* in_help_short,
22 const char* in_help,
23 CommandRunner in_runner)
24 : help_short(in_help_short),
25 help(in_help),
26 runner(in_runner) {
27 }
28
GetCommands()29 const CommandInfoMap& GetCommands() {
30 static CommandInfoMap info_map;
31 if (info_map.empty()) {
32 #define INSERT_COMMAND(cmd) \
33 info_map[k##cmd] = CommandInfo(k##cmd##_HelpShort, \
34 k##cmd##_Help, \
35 &Run##cmd);
36
37 INSERT_COMMAND(Args)
38 INSERT_COMMAND(Check)
39 INSERT_COMMAND(Desc)
40 INSERT_COMMAND(Gen)
41 INSERT_COMMAND(Help)
42 INSERT_COMMAND(Refs)
43
44 #undef INSERT_COMMAND
45 }
46 return info_map;
47 }
48
GetTargetForDesc(const std::vector<std::string> & args)49 const Target* GetTargetForDesc(const std::vector<std::string>& args) {
50 // Deliberately leaked to avoid expensive process teardown.
51 Setup* setup = new Setup;
52 // TODO(brettw) bug 343726: Use a temporary directory instead of this
53 // default one to avoid messing up any build that's in there.
54 if (!setup->DoSetup("//out/Default/"))
55 return NULL;
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 Label default_toolchain = setup->loader()->default_toolchain_label();
63 Value arg_value(NULL, args[0]);
64 Err err;
65 Label label = Label::Resolve(SourceDirForCurrentDirectory(
66 setup->build_settings().root_path()),
67 default_toolchain, arg_value, &err);
68 if (err.has_error()) {
69 err.PrintToStdout();
70 return NULL;
71 }
72
73 const Item* item = setup->builder()->GetItem(label);
74 if (!item) {
75 Err(Location(), "Label not found.",
76 label.GetUserVisibleName(false) + " not found.").PrintToStdout();
77 return NULL;
78 }
79
80 const Target* target = item->AsTarget();
81 if (!target) {
82 Err(Location(), "Not a target.",
83 "The \"" + label.GetUserVisibleName(false) + "\" thing\n"
84 "is not a target. Somebody should probably implement this command for "
85 "other\nitem types.");
86 return NULL;
87 }
88
89 return target;
90 }
91
92 } // namespace commands
93