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 std::string_view name, 34 const Label& toolchain_label); 35 LabelPattern(const LabelPattern& other); 36 ~LabelPattern(); 37 38 LabelPattern& operator=(const LabelPattern&) = default; 39 40 // Converts the given input string to a pattern. This does special stuff 41 // to treat the pattern as a label. Sets the error on failure. 42 static LabelPattern GetPattern(const SourceDir& current_dir, 43 std::string_view source_root, 44 const Value& value, 45 Err* err); 46 47 // Returns true if the given input string might match more than one thing. 48 static bool HasWildcard(const std::string& str); 49 50 // Returns true if this pattern matches the given label. 51 bool Matches(const Label& label) const; 52 53 // Returns true if any of the patterns in the vector match the label. 54 static bool VectorMatches(const std::vector<LabelPattern>& patterns, 55 const Label& label); 56 57 // Returns a string representation of this pattern. 58 std::string Describe() const; 59 type()60 Type type() const { return type_; } 61 dir()62 const SourceDir& dir() const { return dir_; } name()63 const std::string& name() const { return name_; } 64 toolchain()65 const Label& toolchain() const { return toolchain_; } set_toolchain(const Label & tc)66 void set_toolchain(const Label& tc) { toolchain_ = tc; } 67 68 private: 69 // If nonempty, specifies the toolchain to use. If empty, this will match 70 // all toolchains. This is independent of the match type. 71 Label toolchain_; 72 73 Type type_; 74 75 // Used when type_ == PRIVATE and PRIVATE_RECURSIVE. This specifies the 76 // directory that to which the pattern is private to. 77 SourceDir dir_; 78 79 // Empty name means match everything. Otherwise the name must match 80 // exactly. 81 std::string name_; 82 }; 83 84 #endif // TOOLS_GN_LABEL_PATTERN_H_ 85