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_
; }
49 // Returns the current label's toolchain as its own Label.
50 Label
GetToolchainLabel() const;
52 // Returns a copy of this label but with an empty toolchain.
53 Label
GetWithNoToolchain() const;
55 // Formats this label in a way that we can present to the user or expose to
56 // other parts of the system. SourceDirs end in slashes, but the user
57 // expects names like "//chrome/renderer:renderer_config" when printed. The
58 // toolchain is optionally included.
59 std::string
GetUserVisibleName(bool include_toolchain
) const;
61 // Like the above version, but automatically includes the toolchain if it's
62 // not the default one. Normally the user only cares about the toolchain for
63 // non-default ones, so this can make certain output more clear.
64 std::string
GetUserVisibleName(const Label
& default_toolchain
) const;
66 bool operator==(const Label
& other
) const {
67 return name_
== other
.name_
&& dir_
== other
.dir_
&&
68 toolchain_dir_
== other
.toolchain_dir_
&&
69 toolchain_name_
== other
.toolchain_name_
;
71 bool operator!=(const Label
& other
) const {
72 return !operator==(other
);
74 bool operator<(const Label
& other
) const {
75 // TODO(brettw) could be optimized to avoid an extra full string check
76 // (one for operator==, one for <).
77 if (dir_
!= other
.dir_
)
78 return dir_
< other
.dir_
;
79 if (name_
!= other
.name_
)
80 return name_
< other
.name_
;
81 if (toolchain_dir_
!= other
.toolchain_dir_
)
82 return toolchain_dir_
< other
.toolchain_dir_
;
83 return toolchain_name_
< other
.toolchain_name_
;
86 // Returns true if the toolchain dir/name of this object matches some
88 bool ToolchainsEqual(const Label
& other
) const {
89 return toolchain_dir_
== other
.toolchain_dir_
&&
90 toolchain_name_
== other
.toolchain_name_
;
97 SourceDir toolchain_dir_
;
98 std::string toolchain_name_
;
101 namespace BASE_HASH_NAMESPACE
{
103 #if defined(COMPILER_GCC)
104 template<> struct hash
<Label
> {
105 std::size_t operator()(const Label
& v
) const {
106 hash
<std::string
> stringhash
;
107 return ((stringhash(v
.dir().value()) * 131 +
108 stringhash(v
.name())) * 131 +
109 stringhash(v
.toolchain_dir().value())) * 131 +
110 stringhash(v
.toolchain_name());
113 #elif defined(COMPILER_MSVC)
114 inline size_t hash_value(const Label
& v
) {
115 return ((hash_value(v
.dir().value()) * 131 +
116 hash_value(v
.name())) * 131 +
117 hash_value(v
.toolchain_dir().value())) * 131 +
118 hash_value(v
.toolchain_name());
120 #endif // COMPILER...
122 } // namespace BASE_HASH_NAMESPACE
124 #endif // TOOLS_GN_LABEL_H_