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