1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "tools/gn/substitution_writer.h"
#include "tools/gn/build_settings.h"
#include "tools/gn/escape.h"
#include "tools/gn/filesystem_utils.h"
#include "tools/gn/output_file.h"
#include "tools/gn/settings.h"
#include "tools/gn/source_file.h"
#include "tools/gn/string_utils.h"
#include "tools/gn/substitution_list.h"
#include "tools/gn/substitution_pattern.h"
#include "tools/gn/target.h"
namespace {
// Sets the given directory string to the destination, trimming any trailing
// slash from the directory (SourceDirs and OutputFiles representing
// directories will end in a trailing slash). If the directory is empty,
// it will be replaced with a ".".
void SetDirOrDotWithNoSlash(const std::string& dir, std::string* dest) {
if (!dir.empty() && dir[dir.size() - 1] == '/')
dest->assign(dir.data(), dir.size() - 1);
else
dest->assign(dir);
if (dest->empty())
dest->push_back('.');
}
} // namespace
const char kSourceExpansion_Help[] =
"How Source Expansion Works\n"
"\n"
" Source expansion is used for the action_foreach and copy target types\n"
" to map source file names to output file names or arguments.\n"
"\n"
" To perform source expansion in the outputs, GN maps every entry in the\n"
" sources to every entry in the outputs list, producing the cross\n"
" product of all combinations, expanding placeholders (see below).\n"
"\n"
" Source expansion in the args works similarly, but performing the\n"
" placeholder substitution produces a different set of arguments for\n"
" each invocation of the script.\n"
"\n"
" If no placeholders are found, the outputs or args list will be treated\n"
" as a static list of literal file names that do not depend on the\n"
" sources.\n"
"\n"
" See \"gn help copy\" and \"gn help action_foreach\" for more on how\n"
" this is applied.\n"
"\n"
"Placeholders\n"
"\n"
" {{source}}\n"
" The name of the source file including directory (*). This will\n"
" generally be used for specifying inputs to a script in the\n"
" \"args\" variable.\n"
" \"//foo/bar/baz.txt\" => \"../../foo/bar/baz.txt\"\n"
"\n"
" {{source_file_part}}\n"
" The file part of the source including the extension.\n"
" \"//foo/bar/baz.txt\" => \"baz.txt\"\n"
"\n"
" {{source_name_part}}\n"
" The filename part of the source file with no directory or\n"
" extension. This will generally be used for specifying a\n"
" transformation from a soruce file to a destination file with the\n"
" same name but different extension.\n"
" \"//foo/bar/baz.txt\" => \"baz\"\n"
"\n"
" {{source_dir}}\n"
" The directory (*) containing the source file with no\n"
" trailing slash.\n"
" \"//foo/bar/baz.txt\" => \"../../foo/bar\"\n"
"\n"
" {{source_root_relative_dir}}\n"
" The path to the source file's directory relative to the source\n"
" root, with no leading \"//\" or trailing slashes. If the path is\n"
" system-absolute, (beginning in a single slash) this will just\n"
" return the path with no trailing slash. This value will always\n"
" be the same, regardless of whether it appears in the \"outputs\"\n"
" or \"args\" section.\n"
" \"//foo/bar/baz.txt\" => \"foo/bar\"\n"
"\n"
" {{source_gen_dir}}\n"
" The generated file directory (*) corresponding to the source\n"
" file's path. This will be different than the target's generated\n"
" file directory if the source file is in a different directory\n"
" than the BUILD.gn file.\n"
" \"//foo/bar/baz.txt\" => \"gen/foo/bar\"\n"
"\n"
" {{source_out_dir}}\n"
" The object file directory (*) corresponding to the source file's\n"
" path, relative to the build directory. this us be different than\n"
" the target's out directory if the source file is in a different\n"
" directory than the build.gn file.\n"
" \"//foo/bar/baz.txt\" => \"obj/foo/bar\"\n"
"\n"
"(*) Note on directories\n"
"\n"
" Paths containing directories (except the source_root_relative_dir)\n"
" will be different depending on what context the expansion is evaluated\n"
" in. Generally it should \"just work\" but it means you can't\n"
" concatenate strings containing these values with reasonable results.\n"
"\n"
" Details: source expansions can be used in the \"outputs\" variable,\n"
" the \"args\" variable, and in calls to \"process_file_template\". The\n"
" \"args\" are passed to a script which is run from the build directory,\n"
" so these directories will relative to the build directory for the\n"
" script to find. In the other cases, the directories will be source-\n"
" absolute (begin with a \"//\") because the results of those expansions\n"
" will be handled by GN internally.\n"
"\n"
"Examples\n"
"\n"
" Non-varying outputs:\n"
" action(\"hardcoded_outputs\") {\n"
" sources = [ \"input1.idl\", \"input2.idl\" ]\n"
" outputs = [ \"$target_out_dir/output1.dat\",\n"
" \"$target_out_dir/output2.dat\" ]\n"
" }\n"
" The outputs in this case will be the two literal files given.\n"
"\n"
" Varying outputs:\n"
" action_foreach(\"varying_outputs\") {\n"
" sources = [ \"input1.idl\", \"input2.idl\" ]\n"
" outputs = [ \"{{source_gen_dir}}/{{source_name_part}}.h\",\n"
" \"{{source_gen_dir}}/{{source_name_part}}.cc\" ]\n"
" }\n"
" Performing source expansion will result in the following output names:\n"
" //out/Debug/obj/mydirectory/input1.h\n"
" //out/Debug/obj/mydirectory/input1.cc\n"
" //out/Debug/obj/mydirectory/input2.h\n"
" //out/Debug/obj/mydirectory/input2.cc\n";
// static
void SubstitutionWriter::WriteWithNinjaVariables(
const SubstitutionPattern& pattern,
const EscapeOptions& escape_options,
std::ostream& out) {
// The result needs to be quoted as if it was one string, but the $ for
// the inserted Ninja variables can't be escaped. So write to a buffer with
// no quoting, and then quote the whole thing if necessary.
EscapeOptions no_quoting(escape_options);
no_quoting.inhibit_quoting = true;
bool needs_quotes = false;
std::string result;
for (const auto& range : pattern.ranges()) {
if (range.type == SUBSTITUTION_LITERAL) {
result.append(EscapeString(range.literal, no_quoting, &needs_quotes));
} else {
result.append("${");
result.append(kSubstitutionNinjaNames[range.type]);
result.append("}");
}
}
if (needs_quotes && !escape_options.inhibit_quoting)
out << "\"" << result << "\"";
else
out << result;
}
// static
void SubstitutionWriter::GetListAsSourceFiles(
const SubstitutionList& list,
std::vector<SourceFile>* output) {
for (const auto& pattern : list.list()) {
CHECK(pattern.ranges().size() == 1 &&
pattern.ranges()[0].type == SUBSTITUTION_LITERAL)
<< "The substitution patterm \""
<< pattern.AsString()
<< "\" was expected to be a literal with no {{substitutions}}.";
const std::string& literal = pattern.ranges()[0].literal;
CHECK(literal.size() >= 1 && literal[0] == '/')
<< "The result of the pattern \""
<< pattern.AsString()
<< "\" was not an absolute path.";
output->push_back(SourceFile(literal));
}
}
// static
void SubstitutionWriter::GetListAsOutputFiles(
const Settings* settings,
const SubstitutionList& list,
std::vector<OutputFile>* output) {
std::vector<SourceFile> output_as_sources;
GetListAsSourceFiles(list, &output_as_sources);
for (const auto& file : output_as_sources)
output->push_back(OutputFile(settings->build_settings(), file));
}
// static
SourceFile SubstitutionWriter::ApplyPatternToSource(
const Settings* settings,
const SubstitutionPattern& pattern,
const SourceFile& source) {
std::string result_value = ApplyPatternToSourceAsString(
settings, pattern, source);
CHECK(!result_value.empty() && result_value[0] == '/')
<< "The result of the pattern \""
<< pattern.AsString()
<< "\" was not a path beginning in \"/\" or \"//\".";
return SourceFile(SourceFile::SWAP_IN, &result_value);
}
// static
std::string SubstitutionWriter::ApplyPatternToSourceAsString(
const Settings* settings,
const SubstitutionPattern& pattern,
const SourceFile& source) {
std::string result_value;
for (const auto& subrange : pattern.ranges()) {
if (subrange.type == SUBSTITUTION_LITERAL) {
result_value.append(subrange.literal);
} else {
result_value.append(
GetSourceSubstitution(settings, source, subrange.type,
OUTPUT_ABSOLUTE, SourceDir()));
}
}
return result_value;
}
// static
OutputFile SubstitutionWriter::ApplyPatternToSourceAsOutputFile(
const Settings* settings,
const SubstitutionPattern& pattern,
const SourceFile& source) {
SourceFile result_as_source = ApplyPatternToSource(settings, pattern, source);
return OutputFile(settings->build_settings(), result_as_source);
}
// static
void SubstitutionWriter::ApplyListToSource(
const Settings* settings,
const SubstitutionList& list,
const SourceFile& source,
std::vector<SourceFile>* output) {
for (const auto& item : list.list())
output->push_back(ApplyPatternToSource(settings, item, source));
}
// static
void SubstitutionWriter::ApplyListToSourceAsString(
const Settings* settings,
const SubstitutionList& list,
const SourceFile& source,
std::vector<std::string>* output) {
for (const auto& item : list.list())
output->push_back(ApplyPatternToSourceAsString(settings, item, source));
}
// static
void SubstitutionWriter::ApplyListToSourceAsOutputFile(
const Settings* settings,
const SubstitutionList& list,
const SourceFile& source,
std::vector<OutputFile>* output) {
for (const auto& item : list.list())
output->push_back(ApplyPatternToSourceAsOutputFile(settings, item, source));
}
// static
void SubstitutionWriter::ApplyListToSources(
const Settings* settings,
const SubstitutionList& list,
const std::vector<SourceFile>& sources,
std::vector<SourceFile>* output) {
output->clear();
for (const auto& source : sources)
ApplyListToSource(settings, list, source, output);
}
// static
void SubstitutionWriter::ApplyListToSourcesAsString(
const Settings* settings,
const SubstitutionList& list,
const std::vector<SourceFile>& sources,
std::vector<std::string>* output) {
output->clear();
for (const auto& source : sources)
ApplyListToSourceAsString(settings, list, source, output);
}
// static
void SubstitutionWriter::ApplyListToSourcesAsOutputFile(
const Settings* settings,
const SubstitutionList& list,
const std::vector<SourceFile>& sources,
std::vector<OutputFile>* output) {
output->clear();
for (const auto& source : sources)
ApplyListToSourceAsOutputFile(settings, list, source, output);
}
// static
void SubstitutionWriter::WriteNinjaVariablesForSource(
const Settings* settings,
const SourceFile& source,
const std::vector<SubstitutionType>& types,
const EscapeOptions& escape_options,
std::ostream& out) {
for (const auto& type : types) {
// Don't write SOURCE since that just maps to Ninja's $in variable, which
// is implicit in the rule.
if (type != SUBSTITUTION_SOURCE) {
out << " " << kSubstitutionNinjaNames[type] << " = ";
EscapeStringToStream(
out,
GetSourceSubstitution(settings, source, type, OUTPUT_RELATIVE,
settings->build_settings()->build_dir()),
escape_options);
out << std::endl;
}
}
}
// static
std::string SubstitutionWriter::GetSourceSubstitution(
const Settings* settings,
const SourceFile& source,
SubstitutionType type,
OutputStyle output_style,
const SourceDir& relative_to) {
std::string to_rebase;
switch (type) {
case SUBSTITUTION_SOURCE:
if (source.is_system_absolute())
return source.value();
to_rebase = source.value();
break;
case SUBSTITUTION_SOURCE_NAME_PART:
return FindFilenameNoExtension(&source.value()).as_string();
case SUBSTITUTION_SOURCE_FILE_PART:
return source.GetName();
case SUBSTITUTION_SOURCE_DIR:
if (source.is_system_absolute())
return DirectoryWithNoLastSlash(source.GetDir());
to_rebase = DirectoryWithNoLastSlash(source.GetDir());
break;
case SUBSTITUTION_SOURCE_ROOT_RELATIVE_DIR:
if (source.is_system_absolute())
return DirectoryWithNoLastSlash(source.GetDir());
return RebasePath(
DirectoryWithNoLastSlash(source.GetDir()), SourceDir("//"),
settings->build_settings()->root_path_utf8());
case SUBSTITUTION_SOURCE_GEN_DIR:
to_rebase = DirectoryWithNoLastSlash(
GetGenDirForSourceDir(settings, source.GetDir()));
break;
case SUBSTITUTION_SOURCE_OUT_DIR:
to_rebase = DirectoryWithNoLastSlash(
GetOutputDirForSourceDir(settings, source.GetDir()));
break;
default:
NOTREACHED()
<< "Unsupported substitution for this function: "
<< kSubstitutionNames[type];
return std::string();
}
// If we get here, the result is a path that should be made relative or
// absolute according to the output_style. Other cases (just file name or
// extension extraction) will have been handled via early return above.
if (output_style == OUTPUT_ABSOLUTE)
return to_rebase;
return RebasePath(to_rebase, relative_to,
settings->build_settings()->root_path_utf8());
}
// static
OutputFile SubstitutionWriter::ApplyPatternToTargetAsOutputFile(
const Target* target,
const Tool* tool,
const SubstitutionPattern& pattern) {
std::string result_value;
for (const auto& subrange : pattern.ranges()) {
if (subrange.type == SUBSTITUTION_LITERAL) {
result_value.append(subrange.literal);
} else {
std::string subst;
CHECK(GetTargetSubstitution(target, subrange.type, &subst));
result_value.append(subst);
}
}
return OutputFile(result_value);
}
// static
void SubstitutionWriter::ApplyListToTargetAsOutputFile(
const Target* target,
const Tool* tool,
const SubstitutionList& list,
std::vector<OutputFile>* output) {
for (const auto& item : list.list())
output->push_back(ApplyPatternToTargetAsOutputFile(target, tool, item));
}
// static
bool SubstitutionWriter::GetTargetSubstitution(
const Target* target,
SubstitutionType type,
std::string* result) {
switch (type) {
case SUBSTITUTION_LABEL:
// Only include the toolchain for non-default toolchains.
*result = target->label().GetUserVisibleName(
!target->settings()->is_default());
break;
case SUBSTITUTION_ROOT_GEN_DIR:
SetDirOrDotWithNoSlash(
GetToolchainGenDirAsOutputFile(target->settings()).value(),
result);
break;
case SUBSTITUTION_ROOT_OUT_DIR:
SetDirOrDotWithNoSlash(
target->settings()->toolchain_output_subdir().value(),
result);
break;
case SUBSTITUTION_TARGET_GEN_DIR:
SetDirOrDotWithNoSlash(
GetTargetGenDirAsOutputFile(target).value(),
result);
break;
case SUBSTITUTION_TARGET_OUT_DIR:
SetDirOrDotWithNoSlash(
GetTargetOutputDirAsOutputFile(target).value(),
result);
break;
case SUBSTITUTION_TARGET_OUTPUT_NAME:
*result = target->GetComputedOutputName(true);
break;
default:
return false;
}
return true;
}
// static
std::string SubstitutionWriter::GetTargetSubstitution(
const Target* target,
SubstitutionType type) {
std::string result;
GetTargetSubstitution(target, type, &result);
return result;
}
// static
OutputFile SubstitutionWriter::ApplyPatternToCompilerAsOutputFile(
const Target* target,
const SourceFile& source,
const SubstitutionPattern& pattern) {
OutputFile result;
for (const auto& subrange : pattern.ranges()) {
if (subrange.type == SUBSTITUTION_LITERAL) {
result.value().append(subrange.literal);
} else {
result.value().append(
GetCompilerSubstitution(target, source, subrange.type));
}
}
return result;
}
// static
void SubstitutionWriter::ApplyListToCompilerAsOutputFile(
const Target* target,
const SourceFile& source,
const SubstitutionList& list,
std::vector<OutputFile>* output) {
for (const auto& item : list.list())
output->push_back(ApplyPatternToCompilerAsOutputFile(target, source, item));
}
// static
std::string SubstitutionWriter::GetCompilerSubstitution(
const Target* target,
const SourceFile& source,
SubstitutionType type) {
// First try the common tool ones.
std::string result;
if (GetTargetSubstitution(target, type, &result))
return result;
// Fall-through to the source ones.
return GetSourceSubstitution(
target->settings(), source, type, OUTPUT_RELATIVE,
target->settings()->build_settings()->build_dir());
}
// static
OutputFile SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
const Target* target,
const Tool* tool,
const SubstitutionPattern& pattern) {
OutputFile result;
for (const auto& subrange : pattern.ranges()) {
if (subrange.type == SUBSTITUTION_LITERAL) {
result.value().append(subrange.literal);
} else {
result.value().append(GetLinkerSubstitution(target, tool, subrange.type));
}
}
return result;
}
// static
void SubstitutionWriter::ApplyListToLinkerAsOutputFile(
const Target* target,
const Tool* tool,
const SubstitutionList& list,
std::vector<OutputFile>* output) {
for (const auto& item : list.list())
output->push_back(ApplyPatternToLinkerAsOutputFile(target, tool, item));
}
// static
std::string SubstitutionWriter::GetLinkerSubstitution(
const Target* target,
const Tool* tool,
SubstitutionType type) {
// First try the common tool ones.
std::string result;
if (GetTargetSubstitution(target, type, &result))
return result;
// Fall-through to the linker-specific ones.
switch (type) {
case SUBSTITUTION_OUTPUT_EXTENSION:
// Use the extension provided on the target if nonempty, otherwise
// fall back on the default. Note that the target's output extension
// does not include the dot but the tool's does.
if (target->output_extension().empty())
return tool->default_output_extension();
return std::string(".") + target->output_extension();
default:
NOTREACHED();
return std::string();
}
}
|