1 // Copyright 2019 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 #include "gn/c_substitution_type.h"
6
7 #include <stddef.h>
8 #include <stdlib.h>
9
10 #include "gn/err.h"
11
12 const SubstitutionTypes CSubstitutions = {
13 &CSubstitutionAsmFlags,
14 &CSubstitutionCFlags,
15 &CSubstitutionCFlagsC,
16 &CSubstitutionCFlagsCc,
17 &CSubstitutionCFlagsObjC,
18 &CSubstitutionCFlagsObjCc,
19 &CSubstitutionDefines,
20 &CSubstitutionFrameworkDirs,
21 &CSubstitutionIncludeDirs,
22 &CSubstitutionModuleDeps,
23 &CSubstitutionModuleDepsNoSelf,
24 &CSubstitutionSwiftModules,
25
26 &CSubstitutionLinkerInputs,
27 &CSubstitutionLinkerInputsNewline,
28 &CSubstitutionLdFlags,
29 &CSubstitutionLibs,
30 &CSubstitutionSoLibs,
31 &CSubstitutionFrameworks,
32 &CSubstitutionRlibs,
33
34 &CSubstitutionArFlags,
35
36 &CSubstitutionSwiftModuleName,
37 &CSubstitutionSwiftBridgeHeader,
38 &CSubstitutionSwiftModuleDirs,
39 &CSubstitutionSwiftFlags,
40 };
41
42 // Valid for compiler tools.
43 const Substitution CSubstitutionAsmFlags = {"{{asmflags}}", "asmflags"};
44 const Substitution CSubstitutionCFlags = {"{{cflags}}", "cflags"};
45 const Substitution CSubstitutionCFlagsC = {"{{cflags_c}}", "cflags_c"};
46 const Substitution CSubstitutionCFlagsCc = {"{{cflags_cc}}", "cflags_cc"};
47 const Substitution CSubstitutionCFlagsObjC = {"{{cflags_objc}}", "cflags_objc"};
48 const Substitution CSubstitutionCFlagsObjCc = {"{{cflags_objcc}}",
49 "cflags_objcc"};
50 const Substitution CSubstitutionDefines = {"{{defines}}", "defines"};
51 const Substitution CSubstitutionFrameworkDirs = {"{{framework_dirs}}",
52 "framework_dirs"};
53 const Substitution CSubstitutionIncludeDirs = {"{{include_dirs}}",
54 "include_dirs"};
55 const Substitution CSubstitutionModuleDeps = {"{{module_deps}}", "module_deps"};
56 const Substitution CSubstitutionModuleDepsNoSelf = {"{{module_deps_no_self}}",
57 "module_deps_no_self"};
58
59 // Valid for linker tools.
60 const Substitution CSubstitutionLinkerInputs = {"{{inputs}}", "in"};
61 const Substitution CSubstitutionLinkerInputsNewline = {"{{inputs_newline}}",
62 "in_newline"};
63 const Substitution CSubstitutionLdFlags = {"{{ldflags}}", "ldflags"};
64 const Substitution CSubstitutionLibs = {"{{libs}}", "libs"};
65 const Substitution CSubstitutionSoLibs = {"{{solibs}}", "solibs"};
66 const Substitution CSubstitutionRlibs = {"{{rlibs}}", "rlibs"};
67 const Substitution CSubstitutionFrameworks = {"{{frameworks}}", "frameworks"};
68 const Substitution CSubstitutionSwiftModules = {"{{swiftmodules}}",
69 "swiftmodules"};
70
71 // Valid for alink only.
72 const Substitution CSubstitutionArFlags = {"{{arflags}}", "arflags"};
73
74 // Valid for swift only.
75 const Substitution CSubstitutionSwiftModuleName = {"{{module_name}}",
76 "module_name"};
77 const Substitution CSubstitutionSwiftBridgeHeader = {"{{bridge_header}}",
78 "bridge_header"};
79 const Substitution CSubstitutionSwiftModuleDirs = {"{{module_dirs}}",
80 "module_dirs"};
81 const Substitution CSubstitutionSwiftFlags = {"{{swiftflags}}", "swiftflags"};
82
IsValidCompilerSubstitution(const Substitution * type)83 bool IsValidCompilerSubstitution(const Substitution* type) {
84 return IsValidToolSubstitution(type) || IsValidSourceSubstitution(type) ||
85 type == &SubstitutionSource || type == &CSubstitutionAsmFlags ||
86 type == &CSubstitutionCFlags || type == &CSubstitutionCFlagsC ||
87 type == &CSubstitutionCFlagsCc || type == &CSubstitutionCFlagsObjC ||
88 type == &CSubstitutionCFlagsObjCc || type == &CSubstitutionDefines ||
89 type == &CSubstitutionFrameworkDirs ||
90 type == &CSubstitutionIncludeDirs ||
91 type == &CSubstitutionModuleDeps ||
92 type == &CSubstitutionModuleDepsNoSelf;
93 }
94
IsValidCompilerScriptArgsSubstitution(const Substitution * type)95 bool IsValidCompilerScriptArgsSubstitution(const Substitution* type) {
96 return type == &CSubstitutionAsmFlags || type == &CSubstitutionCFlags ||
97 type == &CSubstitutionCFlagsC || type == &CSubstitutionCFlagsCc ||
98 type == &CSubstitutionCFlagsObjC ||
99 type == &CSubstitutionCFlagsObjCc || type == &CSubstitutionDefines ||
100 type == &CSubstitutionFrameworkDirs ||
101 type == &CSubstitutionIncludeDirs ||
102 type == &CSubstitutionSwiftModuleName ||
103 type == &CSubstitutionSwiftBridgeHeader ||
104 type == &CSubstitutionSwiftModuleDirs ||
105 type == &CSubstitutionSwiftFlags;
106 }
107
IsValidCompilerOutputsSubstitution(const Substitution * type)108 bool IsValidCompilerOutputsSubstitution(const Substitution* type) {
109 // All tool types except "output" (which would be infinitely recursive).
110 return (IsValidToolSubstitution(type) && type != &SubstitutionOutput) ||
111 IsValidSourceSubstitution(type);
112 }
113
IsValidSwiftCompilerSubstitution(const Substitution * type)114 bool IsValidSwiftCompilerSubstitution(const Substitution* type) {
115 return IsValidToolSubstitution(type) ||
116 type == &CSubstitutionSwiftModuleName ||
117 type == &CSubstitutionLinkerInputs ||
118 type == &CSubstitutionIncludeDirs ||
119 type == &CSubstitutionSwiftBridgeHeader ||
120 type == &CSubstitutionSwiftModuleDirs ||
121 type == &CSubstitutionSwiftFlags || type == &CSubstitutionDefines;
122 }
123
IsValidSwiftCompilerOutputsSubstitution(const Substitution * type)124 bool IsValidSwiftCompilerOutputsSubstitution(const Substitution* type) {
125 return (IsValidSwiftCompilerSubstitution(type) &&
126 type != &SubstitutionOutput) ||
127 IsValidSourceSubstitution(type);
128 }
129
IsValidLinkerSubstitution(const Substitution * type)130 bool IsValidLinkerSubstitution(const Substitution* type) {
131 return IsValidToolSubstitution(type) || type == &SubstitutionOutputDir ||
132 type == &SubstitutionOutputExtension ||
133 type == &CSubstitutionLinkerInputs ||
134 type == &CSubstitutionLinkerInputsNewline ||
135 type == &CSubstitutionLdFlags || type == &CSubstitutionLibs ||
136 type == &CSubstitutionSoLibs || type == &CSubstitutionFrameworks ||
137 type == &CSubstitutionRlibs || type == &CSubstitutionSwiftModules;
138 }
139
IsValidLinkerOutputsSubstitution(const Substitution * type)140 bool IsValidLinkerOutputsSubstitution(const Substitution* type) {
141 // All valid compiler outputs plus the output extension.
142 return IsValidCompilerOutputsSubstitution(type) ||
143 type == &SubstitutionOutputDir || type == &SubstitutionOutputExtension;
144 }
145
IsValidALinkSubstitution(const Substitution * type)146 bool IsValidALinkSubstitution(const Substitution* type) {
147 return IsValidToolSubstitution(type) ||
148 type == &SubstitutionOutputDir ||
149 type == &SubstitutionOutputExtension ||
150 type == &CSubstitutionLinkerInputs ||
151 type == &CSubstitutionLinkerInputsNewline ||
152 type == &CSubstitutionArFlags;
153 }
154