OLD | NEW |
| (Empty) |
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 #ifndef TOOLS_GN_LABEL_H_ | |
6 #define TOOLS_GN_LABEL_H_ | |
7 | |
8 #include "base/containers/hash_tables.h" | |
9 #include "build/build_config.h" | |
10 #include "tools/gn/source_dir.h" | |
11 | |
12 class Err; | |
13 class Value; | |
14 | |
15 // A label represents the name of a target or some other named thing in | |
16 // the source path. The label is always absolute and always includes a name | |
17 // part, so it starts with a slash, and has one colon. | |
18 class Label { | |
19 public: | |
20 Label(); | |
21 | |
22 // Makes a label given an already-separate out path and name. | |
23 // See also Resolve(). | |
24 Label(const SourceDir& dir, | |
25 const base::StringPiece& name, | |
26 const SourceDir& toolchain_dir, | |
27 const base::StringPiece& toolchain_name); | |
28 ~Label(); | |
29 | |
30 // Resolives a string from a build file that may be relative to the | |
31 // current directory into a fully qualified label. On failure returns an | |
32 // is_null() label and sets the error. | |
33 static Label Resolve(const SourceDir& current_dir, | |
34 const Label& current_toolchain, | |
35 const Value& input, | |
36 Err* err); | |
37 | |
38 bool is_null() const { return dir_.is_null(); } | |
39 | |
40 const SourceDir& dir() const { return dir_; } | |
41 const std::string& name() const { return name_; } | |
42 | |
43 const SourceDir& toolchain_dir() const { return toolchain_dir_; } | |
44 const std::string& toolchain_name() const { return toolchain_name_; } | |
45 Label GetToolchainLabel() const; | |
46 | |
47 // Formats this label in a way that we can present to the user or expose to | |
48 // other parts of the system. SourceDirs end in slashes, but the user | |
49 // expects names like "//chrome/renderer:renderer_config" when printed. The | |
50 // toolchain is optionally included. | |
51 std::string GetUserVisibleName(bool include_toolchain) const; | |
52 | |
53 // Like the above version, but automatically includes the toolchain if it's | |
54 // not the default one. Normally the user only cares about the toolchain for | |
55 // non-default ones, so this can make certain output more clear. | |
56 std::string GetUserVisibleName(const Label& default_toolchain) const; | |
57 | |
58 bool operator==(const Label& other) const { | |
59 return name_ == other.name_ && dir_ == other.dir_ && | |
60 toolchain_dir_ == other.toolchain_dir_ && | |
61 toolchain_name_ == other.toolchain_name_; | |
62 } | |
63 bool operator!=(const Label& other) const { | |
64 return !operator==(other); | |
65 } | |
66 bool operator<(const Label& other) const { | |
67 // TODO(brettw) could be optimized to avoid an extra full string check | |
68 // (one for operator==, one for <). | |
69 if (dir_ != other.dir_) | |
70 return dir_ < other.dir_; | |
71 if (name_ != other.name_) | |
72 return name_ < other.name_; | |
73 if (toolchain_dir_ != other.toolchain_dir_) | |
74 return toolchain_dir_ < other.toolchain_dir_; | |
75 return toolchain_name_ < other.toolchain_name_; | |
76 } | |
77 | |
78 // Returns true if the toolchain dir/name of this object matches some | |
79 // other object. | |
80 bool ToolchainsEqual(const Label& other) const { | |
81 return toolchain_dir_ == other.toolchain_dir_ && | |
82 toolchain_name_ == other.toolchain_name_; | |
83 } | |
84 | |
85 private: | |
86 SourceDir dir_; | |
87 std::string name_; | |
88 | |
89 SourceDir toolchain_dir_; | |
90 std::string toolchain_name_; | |
91 }; | |
92 | |
93 namespace BASE_HASH_NAMESPACE { | |
94 | |
95 #if defined(COMPILER_GCC) | |
96 template<> struct hash<Label> { | |
97 std::size_t operator()(const Label& v) const { | |
98 hash<std::string> stringhash; | |
99 return ((stringhash(v.dir().value()) * 131 + | |
100 stringhash(v.name())) * 131 + | |
101 stringhash(v.toolchain_dir().value())) * 131 + | |
102 stringhash(v.toolchain_name()); | |
103 } | |
104 }; | |
105 #elif defined(COMPILER_MSVC) | |
106 inline size_t hash_value(const Label& v) { | |
107 return ((hash_value(v.dir().value()) * 131 + | |
108 hash_value(v.name())) * 131 + | |
109 hash_value(v.toolchain_dir().value())) * 131 + | |
110 hash_value(v.toolchain_name()); | |
111 } | |
112 #endif // COMPILER... | |
113 | |
114 } // namespace BASE_HASH_NAMESPACE | |
115 | |
116 #endif // TOOLS_GN_LABEL_H_ | |
OLD | NEW |