1 // Copyright 2014 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_PATTERN_H_ 6 #define TOOLS_GN_LABEL_PATTERN_H_ 7 8 #include <string_view> 9 10 #include "gn/label.h" 11 #include "gn/source_dir.h" 12 13 class Err; 14 class Value; 15 16 extern const char kLabelPattern_Help[]; 17 18 // A label pattern is a simple pattern that matches labels. It is used for 19 // specifying visibility and other times when multiple targets need to be 20 // referenced. 21 class LabelPattern { 22 public: 23 enum Type { 24 MATCH = 1, // Exact match for a given target. 25 DIRECTORY, // Only targets in the file in the given directory. 26 RECURSIVE_DIRECTORY // The given directory and any subdir. 27 // (also indicates "public" when dir is empty). 28 }; 29 30 LabelPattern(); 31 LabelPattern(Type type, 32 const SourceDir& dir, 33 const std::string_view& name, 34 const Label& toolchain_label); 35 LabelPattern(const LabelPattern& other); 36 ~LabelPattern(); 37 38 // Converts the given input string to a pattern. This does special stuff 39 // to treat the pattern as a label. Sets the error on failure. 40 static LabelPattern GetPattern(const SourceDir& current_dir, 41 const std::string_view& source_root, 42 const Value& value, 43 Err* err); 44 45 // Returns true if the given input string might match more than one thing. 46 static bool HasWildcard(const std::string& str); 47 48 // Returns true if this pattern matches the given label. 49 bool Matches(const Label& label) const; 50 51 // Returns true if any of the patterns in the vector match the label. 52 static bool VectorMatches(const std::vector<LabelPattern>& patterns, 53 const Label& label); 54 55 // Returns a string representation of this pattern. 56 std::string Describe() const; 57 type()58 Type type() const { return type_; } 59 dir()60 const SourceDir& dir() const { return dir_; } name()61 const std::string& name() const { return name_; } 62 toolchain()63 const Label& toolchain() const { return toolchain_; } set_toolchain(const Label & tc)64 void set_toolchain(const Label& tc) { toolchain_ = tc; } 65 66 private: 67 // If nonempty, specifies the toolchain to use. If empty, this will match 68 // all toolchains. This is independent of the match type. 69 Label toolchain_; 70 71 Type type_; 72 73 // Used when type_ == PRIVATE and PRIVATE_RECURSIVE. This specifies the 74 // directory that to which the pattern is private to. 75 SourceDir dir_; 76 77 // Empty name means match everything. Otherwise the name must match 78 // exactly. 79 std::string name_; 80 }; 81 82 #endif // TOOLS_GN_LABEL_PATTERN_H_ 83