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_SUBSTITUTION_TYPE_H_ 6 #define TOOLS_GN_SUBSTITUTION_TYPE_H_ 7 8 #include <vector> 9 10 #include "base/containers/flat_set.h" 11 12 class Err; 13 class ParseNode; 14 15 // Each pair here represents the string representation of the substitution in GN 16 // and in Ninja. 17 struct Substitution { 18 const char* name; 19 const char* ninja_name; 20 Substitution(const Substitution&) = delete; 21 Substitution& operator=(const Substitution&) = delete; 22 }; 23 24 using SubstitutionTypes = const std::vector<const Substitution*>; 25 26 // All possible substitutions, organized into logical sets. 27 extern const std::vector<SubstitutionTypes*> AllSubstitutions; 28 29 // The set of substitutions available to all tools. 30 extern const SubstitutionTypes GeneralSubstitutions; 31 32 // Types of substitutions. 33 extern const Substitution SubstitutionLiteral; 34 35 // Valid for all tools. These depend on the target and 36 // do not vary on a per-file basis. 37 extern const Substitution SubstitutionOutput; 38 extern const Substitution SubstitutionLabel; 39 extern const Substitution SubstitutionLabelName; 40 extern const Substitution SubstitutionLabelNoToolchain; 41 extern const Substitution SubstitutionRootGenDir; 42 extern const Substitution SubstitutionRootOutDir; 43 extern const Substitution SubstitutionOutputDir; 44 extern const Substitution SubstitutionOutputExtension; 45 extern const Substitution SubstitutionTargetGenDir; 46 extern const Substitution SubstitutionTargetOutDir; 47 extern const Substitution SubstitutionTargetOutputName; 48 49 // Valid for all compiler tools. 50 extern const Substitution SubstitutionSource; 51 extern const Substitution SubstitutionSourceNamePart; 52 extern const Substitution SubstitutionSourceFilePart; 53 extern const Substitution SubstitutionSourceDir; 54 extern const Substitution SubstitutionSourceRootRelativeDir; 55 extern const Substitution SubstitutionSourceGenDir; 56 extern const Substitution SubstitutionSourceOutDir; 57 extern const Substitution SubstitutionSourceTargetRelative; 58 59 // Valid for bundle_data targets. 60 extern const Substitution SubstitutionBundleRootDir; 61 extern const Substitution SubstitutionBundleContentsDir; 62 extern const Substitution SubstitutionBundleResourcesDir; 63 extern const Substitution SubstitutionBundleExecutableDir; 64 65 // Valid for compile_xcassets tool. 66 extern const Substitution SubstitutionBundleProductType; 67 extern const Substitution SubstitutionBundlePartialInfoPlist; 68 extern const Substitution SubstitutionXcassetsCompilerFlags; 69 70 // Used only for the args of actions. 71 extern const Substitution SubstitutionRspFileName; 72 73 // A wrapper around an array if flags indicating whether a given substitution 74 // type is required in some context. By convention, the LITERAL type bit is 75 // not set. 76 struct SubstitutionBits { 77 SubstitutionBits(); 78 79 // Merges any bits set in the given "other" to this one. This object will 80 // then be the union of all bits in the two lists. 81 void MergeFrom(const SubstitutionBits& other); 82 83 // Converts the substitution type set to a vector of the types listed. Does 84 // not include SubstitutionLiteral. 85 void FillVector(std::vector<const Substitution*>* vect) const; 86 87 // This set depends on global uniqueness of pointers, and so all points in 88 // this set should be the Substitution* constants. 89 base::flat_set<const Substitution*> used; 90 }; 91 92 // Returns true if the given substitution pattern references the output 93 // directory. This is used to check strings that begin with a substitution to 94 // verify that they produce a file in the output directory. 95 bool SubstitutionIsInOutputDir(const Substitution* type); 96 97 // Returns true if the given substitution pattern references the bundle 98 // directory. This is used to check strings that begin with a substitution to 99 // verify that they produce a file in the bundle directory. 100 bool SubstitutionIsInBundleDir(const Substitution* type); 101 102 // Returns true if the given substitution is valid for the named purpose. 103 bool IsValidBundleDataSubstitution(const Substitution* type); 104 bool IsValidSourceSubstitution(const Substitution* type); 105 bool IsValidScriptArgsSubstitution(const Substitution* type); 106 107 // Both compiler and linker tools. 108 bool IsValidToolSubstitution(const Substitution* type); 109 bool IsValidCopySubstitution(const Substitution* type); 110 bool IsValidCompileXCassetsSubstitution(const Substitution* type); 111 112 // Validates that each substitution type in the vector passes the given 113 // is_valid_subst predicate. Returns true on success. On failure, fills in the 114 // error object with an appropriate message and returns false. 115 bool EnsureValidSubstitutions(const std::vector<const Substitution*>& types, 116 bool (*is_valid_subst)(const Substitution*), 117 const ParseNode* origin, 118 Err* err); 119 120 #endif // TOOLS_GN_SUBSTITUTION_TYPE_H_ 121