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 #ifndef TOOLS_GN_LABEL_H_
6 #define TOOLS_GN_LABEL_H_
8 #include "base/containers/hash_tables.h"
9 #include "tools/gn/source_dir.h"
14 // A label represents the name of a target or some other named thing in
15 // the source path. The label is always absolute and always includes a name
16 // part, so it starts with a slash, and has one colon.
21 // Makes a label given an already-separated out path and name.
22 // See also Resolve().
23 Label(const SourceDir
& dir
,
24 const base::StringPiece
& name
,
25 const SourceDir
& toolchain_dir
,
26 const base::StringPiece
& toolchain_name
);
28 // Makes a label with an empty toolchain.
29 Label(const SourceDir
& dir
, const base::StringPiece
& name
);
32 // Resolives a string from a build file that may be relative to the
33 // current directory into a fully qualified label. On failure returns an
34 // is_null() label and sets the error.
35 static Label
Resolve(const SourceDir
& current_dir
,
36 const Label
& current_toolchain
,
40 bool is_null() const { return dir_
.is_null(); }
42 const SourceDir
& dir() const { return dir_
; }
43 const std::string
& name() const { return name_
; }
45 const SourceDir
& toolchain_dir() const { return toolchain_dir_
; }
46 const std::string
& toolchain_name() const { return toolchain_name_
; }
48 // Returns the current label's toolchain as its own Label.
49 Label
GetToolchainLabel() const;
51 // Returns a copy of this label but with an empty toolchain.
52 Label
GetWithNoToolchain() const;
54 // Formats this label in a way that we can present to the user or expose to
55 // other parts of the system. SourceDirs end in slashes, but the user
56 // expects names like "//chrome/renderer:renderer_config" when printed. The
57 // toolchain is optionally included.
58 std::string
GetUserVisibleName(bool include_toolchain
) const;
60 // Like the above version, but automatically includes the toolchain if it's
61 // not the default one. Normally the user only cares about the toolchain for
62 // non-default ones, so this can make certain output more clear.
63 std::string
GetUserVisibleName(const Label
& default_toolchain
) const;
65 bool operator==(const Label
& other
) const {
66 return name_
== other
.name_
&& dir_
== other
.dir_
&&
67 toolchain_dir_
== other
.toolchain_dir_
&&
68 toolchain_name_
== other
.toolchain_name_
;
70 bool operator!=(const Label
& other
) const {
71 return !operator==(other
);
73 bool operator<(const Label
& other
) const {
74 // TODO(brettw) could be optimized to avoid an extra full string check
75 // (one for operator==, one for <).
76 if (dir_
!= other
.dir_
)
77 return dir_
< other
.dir_
;
78 if (name_
!= other
.name_
)
79 return name_
< other
.name_
;
80 if (toolchain_dir_
!= other
.toolchain_dir_
)
81 return toolchain_dir_
< other
.toolchain_dir_
;
82 return toolchain_name_
< other
.toolchain_name_
;
85 void swap(Label
& other
) {
86 dir_
.swap(other
.dir_
);
87 name_
.swap(other
.name_
);
88 toolchain_dir_
.swap(other
.toolchain_dir_
);
89 toolchain_name_
.swap(other
.toolchain_name_
);
92 // Returns true if the toolchain dir/name of this object matches some
94 bool ToolchainsEqual(const Label
& other
) const {
95 return toolchain_dir_
== other
.toolchain_dir_
&&
96 toolchain_name_
== other
.toolchain_name_
;
103 SourceDir toolchain_dir_
;
104 std::string toolchain_name_
;
107 namespace BASE_HASH_NAMESPACE
{
109 template<> struct hash
<Label
> {
110 std::size_t operator()(const Label
& v
) const {
111 hash
<std::string
> stringhash
;
112 return ((stringhash(v
.dir().value()) * 131 +
113 stringhash(v
.name())) * 131 +
114 stringhash(v
.toolchain_dir().value())) * 131 +
115 stringhash(v
.toolchain_name());
119 } // namespace BASE_HASH_NAMESPACE
121 inline void swap(Label
& lhs
, Label
& rhs
) {
125 #endif // TOOLS_GN_LABEL_H_