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 12class Err; 13class 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. 18class 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 void swap(Label& other) { 87 dir_.swap(other.dir_); 88 name_.swap(other.name_); 89 toolchain_dir_.swap(other.toolchain_dir_); 90 toolchain_name_.swap(other.toolchain_name_); 91 } 92 93 // Returns true if the toolchain dir/name of this object matches some 94 // other object. 95 bool ToolchainsEqual(const Label& other) const { 96 return toolchain_dir_ == other.toolchain_dir_ && 97 toolchain_name_ == other.toolchain_name_; 98 } 99 100 private: 101 SourceDir dir_; 102 std::string name_; 103 104 SourceDir toolchain_dir_; 105 std::string toolchain_name_; 106}; 107 108namespace BASE_HASH_NAMESPACE { 109 110#if defined(COMPILER_GCC) 111template<> struct hash<Label> { 112 std::size_t operator()(const Label& v) const { 113 hash<std::string> stringhash; 114 return ((stringhash(v.dir().value()) * 131 + 115 stringhash(v.name())) * 131 + 116 stringhash(v.toolchain_dir().value())) * 131 + 117 stringhash(v.toolchain_name()); 118 } 119}; 120#elif defined(COMPILER_MSVC) 121inline size_t hash_value(const Label& v) { 122 return ((hash_value(v.dir().value()) * 131 + 123 hash_value(v.name())) * 131 + 124 hash_value(v.toolchain_dir().value())) * 131 + 125 hash_value(v.toolchain_name()); 126} 127#endif // COMPILER... 128 129} // namespace BASE_HASH_NAMESPACE 130 131inline void swap(Label& lhs, Label& rhs) { 132 lhs.swap(rhs); 133} 134 135#endif // TOOLS_GN_LABEL_H_ 136