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