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
10class Err;
11class ParseNode;
12
13// Keep kSubstitutionNames, kSubstitutionNinjaNames and the
14// IsValid*Substutition functions in sync if you change anything here.
15enum SubstitutionType {
16  SUBSTITUTION_LITERAL = 0,
17
18  // The index of the first pattern. To loop overal all patterns, go from here
19  // until NUM_TYPES.
20  SUBSTITUTION_FIRST_PATTERN,
21
22  // These map to Ninja's {in} and {out} variables.
23  SUBSTITUTION_SOURCE = SUBSTITUTION_FIRST_PATTERN,  // {{source}}
24  SUBSTITUTION_OUTPUT,  // {{output}}
25
26  // Valid for all compiler tools.
27  SUBSTITUTION_SOURCE_NAME_PART,  // {{source_name_part}}
28  SUBSTITUTION_SOURCE_FILE_PART,  // {{source_file_part}}
29  SUBSTITUTION_SOURCE_DIR,  // {{source_dir}}
30  SUBSTITUTION_SOURCE_ROOT_RELATIVE_DIR,  // {{root_relative_dir}}
31  SUBSTITUTION_SOURCE_GEN_DIR,  // {{source_gen_dir}}
32  SUBSTITUTION_SOURCE_OUT_DIR,  // {{source_out_dir}}
33
34  // Valid for all compiler and linker tools. These depend on the target and
35  // no not vary on a per-file basis.
36  SUBSTITUTION_LABEL,  // {{label}}
37  SUBSTITUTION_ROOT_GEN_DIR,  // {{root_gen_dir}}
38  SUBSTITUTION_ROOT_OUT_DIR,  // {{root_out_dir}}
39  SUBSTITUTION_TARGET_GEN_DIR,  // {{target_gen_dir}}
40  SUBSTITUTION_TARGET_OUT_DIR,  // {{target_out_dir}}
41  SUBSTITUTION_TARGET_OUTPUT_NAME,  // {{target_output_name}}
42
43  // Valid for compiler tools.
44  SUBSTITUTION_CFLAGS,  // {{cflags}}
45  SUBSTITUTION_CFLAGS_C,  // {{cflags_c}}
46  SUBSTITUTION_CFLAGS_CC,  // {{cflags_cc}}
47  SUBSTITUTION_CFLAGS_OBJC,  // {{cflags_objc}}
48  SUBSTITUTION_CFLAGS_OBJCC,  // {{cflags_objcc}}
49  SUBSTITUTION_DEFINES,  // {{defines}}
50  SUBSTITUTION_INCLUDE_DIRS,  // {{include_dirs}}
51
52  // Valid for linker tools.
53  SUBSTITUTION_LINKER_INPUTS,  // {{inputs}}
54  SUBSTITUTION_LINKER_INPUTS_NEWLINE,  // {{inputs_newline}}
55  SUBSTITUTION_LDFLAGS,  // {{ldflags}}
56  SUBSTITUTION_LIBS,  // {{libs}}
57  SUBSTITUTION_OUTPUT_EXTENSION,  // {{output_extension}}
58  SUBSTITUTION_SOLIBS,  // {{solibs}}
59
60  SUBSTITUTION_NUM_TYPES  // Must be last.
61};
62
63// An array of size SUBSTITUTION_NUM_TYPES that lists the names of the
64// substitution patterns, including the curly braces. So, for example,
65// kSubstitutionNames[SUBSTITUTION_SOURCE] == "{{source}}".
66extern const char* kSubstitutionNames[SUBSTITUTION_NUM_TYPES];
67
68// Ninja variables corresponding to each substitution. These do not include
69// the dollar sign.
70extern const char* kSubstitutionNinjaNames[SUBSTITUTION_NUM_TYPES];
71
72// A wrapper around an array if flags indicating whether a give substitution
73// type is required in some context. By convention, the LITERAL type bit is
74// not set.
75struct SubstitutionBits {
76  SubstitutionBits();
77
78  // Merges any bits set in the given "other" to this one. This object will
79  // then be the union of all bits in the two lists.
80  void MergeFrom(const SubstitutionBits& other);
81
82  // Converts the substitution type bitfield (with a true set for each required
83  // item) to a vector of the types listed. Does not include LITERAL.
84  void FillVector(std::vector<SubstitutionType>* vect) const;
85
86  bool used[SUBSTITUTION_NUM_TYPES];
87};
88
89// Returns true if the given substitution pattern references the output
90// directory. This is used to check strings that begin with a substitution to
91// verify that the produce a file in the output directory.
92bool SubstitutionIsInOutputDir(SubstitutionType type);
93
94// Returns true if the given substitution is valid for the named purpose.
95bool IsValidSourceSubstitution(SubstitutionType type);
96// Both compiler and linker tools.
97bool IsValidToolSubstutition(SubstitutionType type);
98bool IsValidCompilerSubstitution(SubstitutionType type);
99bool IsValidCompilerOutputsSubstitution(SubstitutionType type);
100bool IsValidLinkerSubstitution(SubstitutionType type);
101bool IsValidLinkerOutputsSubstitution(SubstitutionType type);
102bool IsValidCopySubstitution(SubstitutionType type);
103
104// Like the "IsValid..." version above but checks a list of types and sets a
105// an error blaming the given source if the test fails.
106bool EnsureValidSourcesSubstitutions(
107    const std::vector<SubstitutionType>& types,
108    const ParseNode* origin,
109    Err* err);
110
111#endif  // TOOLS_GN_SUBSTITUTION_TYPE_H_
112