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 "build/build_config.h"
10 #include "tools/gn/source_dir.h"
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.
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
);
29 // Makes a label with an empty toolchain.
30 Label(const SourceDir
& dir
, const base::StringPiece
& name
);
33 // Resolives a string from a build file that may be relative to the
34 // current directory into a fully qualified label. On failure returns an
35 // is_null() label and sets the error.
36 static Label
Resolve(const SourceDir
& current_dir
,
37 const Label
& current_toolchain
,
41 bool is_null() const { return dir_
.is_null(); }
43 const SourceDir
& dir() const { return dir_
; }
44 const std::string
& name() const { return name_
; }
46 const SourceDir
& toolchain_dir() const { return toolchain_dir_
; }
47 const std::string
& toolchain_name() const { return toolchain_name_
; }
48 Label
GetToolchainLabel() const;
50 // Formats this label in a way that we can present to the user or expose to
51 // other parts of the system. SourceDirs end in slashes, but the user
52 // expects names like "//chrome/renderer:renderer_config" when printed. The
53 // toolchain is optionally included.
54 std::string
GetUserVisibleName(bool include_toolchain
) const;
56 // Like the above version, but automatically includes the toolchain if it's
57 // not the default one. Normally the user only cares about the toolchain for
58 // non-default ones, so this can make certain output more clear.
59 std::string
GetUserVisibleName(const Label
& default_toolchain
) const;
61 bool operator==(const Label
& other
) const {
62 return name_
== other
.name_
&& dir_
== other
.dir_
&&
63 toolchain_dir_
== other
.toolchain_dir_
&&
64 toolchain_name_
== other
.toolchain_name_
;
66 bool operator!=(const Label
& other
) const {
67 return !operator==(other
);
69 bool operator<(const Label
& other
) const {
70 // TODO(brettw) could be optimized to avoid an extra full string check
71 // (one for operator==, one for <).
72 if (dir_
!= other
.dir_
)
73 return dir_
< other
.dir_
;
74 if (name_
!= other
.name_
)
75 return name_
< other
.name_
;
76 if (toolchain_dir_
!= other
.toolchain_dir_
)
77 return toolchain_dir_
< other
.toolchain_dir_
;
78 return toolchain_name_
< other
.toolchain_name_
;
81 // Returns true if the toolchain dir/name of this object matches some
83 bool ToolchainsEqual(const Label
& other
) const {
84 return toolchain_dir_
== other
.toolchain_dir_
&&
85 toolchain_name_
== other
.toolchain_name_
;
92 SourceDir toolchain_dir_
;
93 std::string toolchain_name_
;
96 namespace BASE_HASH_NAMESPACE
{
98 #if defined(COMPILER_GCC)
99 template<> struct hash
<Label
> {
100 std::size_t operator()(const Label
& v
) const {
101 hash
<std::string
> stringhash
;
102 return ((stringhash(v
.dir().value()) * 131 +
103 stringhash(v
.name())) * 131 +
104 stringhash(v
.toolchain_dir().value())) * 131 +
105 stringhash(v
.toolchain_name());
108 #elif defined(COMPILER_MSVC)
109 inline size_t hash_value(const Label
& v
) {
110 return ((hash_value(v
.dir().value()) * 131 +
111 hash_value(v
.name())) * 131 +
112 hash_value(v
.toolchain_dir().value())) * 131 +
113 hash_value(v
.toolchain_name());
115 #endif // COMPILER...
117 } // namespace BASE_HASH_NAMESPACE
119 #endif // TOOLS_GN_LABEL_H_