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