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 29 // Makes a label with an empty toolchain. 30 Label(const SourceDir& dir, const base::StringPiece& name); 31 ~Label(); 32 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, 38 const Value& input, 39 Err* err); 40 41 bool is_null() const { return dir_.is_null(); } 42 43 const SourceDir& dir() const { return dir_; } 44 const std::string& name() const { return name_; } 45 46 const SourceDir& toolchain_dir() const { return toolchain_dir_; } 47 const std::string& toolchain_name() const { return toolchain_name_; } 48 49 // Returns the current label's toolchain as its own Label. 50 Label GetToolchainLabel() const; 51 52 // Returns a copy of this label but with an empty toolchain. 53 Label GetWithNoToolchain() const; 54 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; 60 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; 65 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_; 70 } 71 bool operator!=(const Label& other) const { 72 return !operator==(other); 73 } 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_; 84 } 85 86 // Returns true if the toolchain dir/name of this object matches some 87 // other object. 88 bool ToolchainsEqual(const Label& other) const { 89 return toolchain_dir_ == other.toolchain_dir_ && 90 toolchain_name_ == other.toolchain_name_; 91 } 92 93 private: 94 SourceDir dir_; 95 std::string name_; 96 97 SourceDir toolchain_dir_; 98 std::string toolchain_name_; 99 }; 100 101 namespace BASE_HASH_NAMESPACE { 102 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()); 111 } 112 }; 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()); 119 } 120 #endif // COMPILER... 121 122 } // namespace BASE_HASH_NAMESPACE 123 124 #endif // TOOLS_GN_LABEL_H_ 125