Add ICU message format support
[chromium-blink-merge.git] / tools / gn / function_toolchain.cc
blob2fe5f53a23c5135918262ed2b0a7cea3f9fa7378
1 // Copyright (c) 2013 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.
5 #include <algorithm>
6 #include <limits>
8 #include "tools/gn/err.h"
9 #include "tools/gn/functions.h"
10 #include "tools/gn/parse_tree.h"
11 #include "tools/gn/scheduler.h"
12 #include "tools/gn/scope.h"
13 #include "tools/gn/settings.h"
14 #include "tools/gn/tool.h"
15 #include "tools/gn/toolchain.h"
16 #include "tools/gn/value_extractors.h"
17 #include "tools/gn/variables.h"
19 namespace functions {
21 namespace {
23 // This is jsut a unique value to take the address of to use as the key for
24 // the toolchain property on a scope.
25 const int kToolchainPropertyKey = 0;
27 bool ReadBool(Scope* scope,
28 const char* var,
29 Tool* tool,
30 void (Tool::*set)(bool),
31 Err* err) {
32 const Value* v = scope->GetValue(var, true);
33 if (!v)
34 return true; // Not present is fine.
35 if (!v->VerifyTypeIs(Value::BOOLEAN, err))
36 return false;
38 (tool->*set)(v->boolean_value());
39 return true;
42 // Reads the given string from the scope (if present) and puts the result into
43 // dest. If the value is not a string, sets the error and returns false.
44 bool ReadString(Scope* scope,
45 const char* var,
46 Tool* tool,
47 void (Tool::*set)(const std::string&),
48 Err* err) {
49 const Value* v = scope->GetValue(var, true);
50 if (!v)
51 return true; // Not present is fine.
52 if (!v->VerifyTypeIs(Value::STRING, err))
53 return false;
55 (tool->*set)(v->string_value());
56 return true;
59 // Calls the given validate function on each type in the list. On failure,
60 // sets the error, blame the value, and return false.
61 bool ValidateSubstitutionList(const std::vector<SubstitutionType>& list,
62 bool (*validate)(SubstitutionType),
63 const Value* origin,
64 Err* err) {
65 for (const auto& cur_type : list) {
66 if (!validate(cur_type)) {
67 *err = Err(*origin, "Pattern not valid here.",
68 "You used the pattern " + std::string(kSubstitutionNames[cur_type]) +
69 " which is not valid\nfor this variable.");
70 return false;
73 return true;
76 bool ReadPattern(Scope* scope,
77 const char* name,
78 bool (*validate)(SubstitutionType),
79 Tool* tool,
80 void (Tool::*set)(const SubstitutionPattern&),
81 Err* err) {
82 const Value* value = scope->GetValue(name, true);
83 if (!value)
84 return true; // Not present is fine.
85 if (!value->VerifyTypeIs(Value::STRING, err))
86 return false;
88 SubstitutionPattern pattern;
89 if (!pattern.Parse(*value, err))
90 return false;
91 if (!ValidateSubstitutionList(pattern.required_types(), validate, value, err))
92 return false;
94 (tool->*set)(pattern);
95 return true;
98 bool ReadOutputExtension(Scope* scope, Tool* tool, Err* err) {
99 const Value* value = scope->GetValue("default_output_extension", true);
100 if (!value)
101 return true; // Not present is fine.
102 if (!value->VerifyTypeIs(Value::STRING, err))
103 return false;
105 if (value->string_value().empty())
106 return true; // Accept empty string.
108 if (value->string_value()[0] != '.') {
109 *err = Err(*value, "default_output_extension must begin with a '.'");
110 return false;
113 tool->set_default_output_extension(value->string_value());
114 return true;
117 bool ReadPrecompiledHeaderType(Scope* scope, Tool* tool, Err* err) {
118 const Value* value = scope->GetValue("precompiled_header_type", true);
119 if (!value)
120 return true; // Not present is fine.
121 if (!value->VerifyTypeIs(Value::STRING, err))
122 return false;
124 if (value->string_value().empty())
125 return true; // Accept empty string, do nothing (default is "no PCH").
127 if (value->string_value() == "msvc") {
128 tool->set_precompiled_header_type(Tool::PCH_MSVC);
129 return true;
131 *err = Err(*value, "Invalid precompiled_header_type",
132 "Must either be empty or \"msvc\".");
133 return false;
136 bool ReadDepsFormat(Scope* scope, Tool* tool, Err* err) {
137 const Value* value = scope->GetValue("depsformat", true);
138 if (!value)
139 return true; // Not present is fine.
140 if (!value->VerifyTypeIs(Value::STRING, err))
141 return false;
143 if (value->string_value() == "gcc") {
144 tool->set_depsformat(Tool::DEPS_GCC);
145 } else if (value->string_value() == "msvc") {
146 tool->set_depsformat(Tool::DEPS_MSVC);
147 } else {
148 *err = Err(*value, "Deps format must be \"gcc\" or \"msvc\".");
149 return false;
151 return true;
154 bool ReadOutputs(Scope* scope,
155 const FunctionCallNode* tool_function,
156 bool (*validate)(SubstitutionType),
157 Tool* tool,
158 Err* err) {
159 const Value* value = scope->GetValue("outputs", true);
160 if (!value) {
161 *err = Err(tool_function, "\"outputs\" must be specified for this tool.");
162 return false;
165 SubstitutionList list;
166 if (!list.Parse(*value, err))
167 return false;
169 // Validate the right kinds of patterns are used.
170 if (!ValidateSubstitutionList(list.required_types(), validate, value, err))
171 return false;
173 // There should always be at least one output.
174 if (list.list().empty()) {
175 *err = Err(*value, "Outputs list is empty.", "I need some outputs.");
176 return false;
179 tool->set_outputs(list);
180 return true;
183 bool IsCompilerTool(Toolchain::ToolType type) {
184 return type == Toolchain::TYPE_CC ||
185 type == Toolchain::TYPE_CXX ||
186 type == Toolchain::TYPE_OBJC ||
187 type == Toolchain::TYPE_OBJCXX ||
188 type == Toolchain::TYPE_RC ||
189 type == Toolchain::TYPE_ASM;
192 bool IsLinkerTool(Toolchain::ToolType type) {
193 return type == Toolchain::TYPE_ALINK ||
194 type == Toolchain::TYPE_SOLINK ||
195 type == Toolchain::TYPE_LINK;
198 bool IsPatternInOutputList(const SubstitutionList& output_list,
199 const SubstitutionPattern& pattern) {
200 for (const auto& cur : output_list.list()) {
201 if (pattern.ranges().size() == cur.ranges().size() &&
202 std::equal(pattern.ranges().begin(), pattern.ranges().end(),
203 cur.ranges().begin()))
204 return true;
206 return false;
209 } // namespace
211 // toolchain -------------------------------------------------------------------
213 const char kToolchain[] = "toolchain";
214 const char kToolchain_HelpShort[] =
215 "toolchain: Defines a toolchain.";
216 const char kToolchain_Help[] =
217 "toolchain: Defines a toolchain.\n"
218 "\n"
219 " A toolchain is a set of commands and build flags used to compile the\n"
220 " source code. You can have more than one toolchain in use at once in\n"
221 " a build.\n"
222 "\n"
223 "Functions and variables\n"
224 "\n"
225 " tool()\n"
226 " The tool() function call specifies the commands commands to run for\n"
227 " a given step. See \"gn help tool\".\n"
228 "\n"
229 " toolchain_args()\n"
230 " List of arguments to pass to the toolchain when invoking this\n"
231 " toolchain. This applies only to non-default toolchains. See\n"
232 " \"gn help toolchain_args\" for more.\n"
233 "\n"
234 " deps\n"
235 " Dependencies of this toolchain. These dependencies will be resolved\n"
236 " before any target in the toolchain is compiled. To avoid circular\n"
237 " dependencies these must be targets defined in another toolchain.\n"
238 "\n"
239 " This is expressed as a list of targets, and generally these targets\n"
240 " will always specify a toolchain:\n"
241 " deps = [ \"//foo/bar:baz(//build/toolchain:bootstrap)\" ]\n"
242 "\n"
243 " This concept is somewhat inefficient to express in Ninja (it\n"
244 " requires a lot of duplicate of rules) so should only be used when\n"
245 " absolutely necessary.\n"
246 "\n"
247 " concurrent_links\n"
248 " In integer expressing the number of links that Ninja will perform in\n"
249 " parallel. GN will create a pool for shared library and executable\n"
250 " link steps with this many processes. Since linking is memory- and\n"
251 " I/O-intensive, projects with many large targets may want to limit\n"
252 " the number of parallel steps to avoid overloading the computer.\n"
253 " Since creating static libraries is generally not as intensive\n"
254 " there is no limit to \"alink\" steps.\n"
255 "\n"
256 " Defaults to 0 which Ninja interprets as \"no limit\".\n"
257 "\n"
258 " The value used will be the one from the default toolchain of the\n"
259 " current build.\n"
260 "\n"
261 "Invoking targets in toolchains:\n"
262 "\n"
263 " By default, when a target depends on another, there is an implicit\n"
264 " toolchain label that is inherited, so the dependee has the same one\n"
265 " as the dependent.\n"
266 "\n"
267 " You can override this and refer to any other toolchain by explicitly\n"
268 " labeling the toolchain to use. For example:\n"
269 " data_deps = [ \"//plugins:mine(//toolchains:plugin_toolchain)\" ]\n"
270 " The string \"//build/toolchains:plugin_toolchain\" is a label that\n"
271 " identifies the toolchain declaration for compiling the sources.\n"
272 "\n"
273 " To load a file in an alternate toolchain, GN does the following:\n"
274 "\n"
275 " 1. Loads the file with the toolchain definition in it (as determined\n"
276 " by the toolchain label).\n"
277 " 2. Re-runs the master build configuration file, applying the\n"
278 " arguments specified by the toolchain_args section of the toolchain\n"
279 " definition (see \"gn help toolchain_args\").\n"
280 " 3. Loads the destination build file in the context of the\n"
281 " configuration file in the previous step.\n"
282 "\n"
283 "Example:\n"
284 " toolchain(\"plugin_toolchain\") {\n"
285 " concurrent_links = 8\n"
286 "\n"
287 " tool(\"cc\") {\n"
288 " command = \"gcc {{source}}\"\n"
289 " ...\n"
290 " }\n"
291 "\n"
292 " toolchain_args() {\n"
293 " is_plugin = true\n"
294 " is_32bit = true\n"
295 " is_64bit = false\n"
296 " }\n"
297 " }\n";
299 Value RunToolchain(Scope* scope,
300 const FunctionCallNode* function,
301 const std::vector<Value>& args,
302 BlockNode* block,
303 Err* err) {
304 if (!EnsureNotProcessingImport(function, scope, err) ||
305 !EnsureNotProcessingBuildConfig(function, scope, err))
306 return Value();
308 // Note that we don't want to use MakeLabelForScope since that will include
309 // the toolchain name in the label, and toolchain labels don't themselves
310 // have toolchain names.
311 const SourceDir& input_dir = scope->GetSourceDir();
312 Label label(input_dir, args[0].string_value());
313 if (g_scheduler->verbose_logging())
314 g_scheduler->Log("Defining toolchain", label.GetUserVisibleName(false));
316 // This object will actually be copied into the one owned by the toolchain
317 // manager, but that has to be done in the lock.
318 scoped_ptr<Toolchain> toolchain(new Toolchain(scope->settings(), label));
319 toolchain->set_defined_from(function);
320 toolchain->visibility().SetPublic();
322 Scope block_scope(scope);
323 block_scope.SetProperty(&kToolchainPropertyKey, toolchain.get());
324 block->Execute(&block_scope, err);
325 block_scope.SetProperty(&kToolchainPropertyKey, nullptr);
326 if (err->has_error())
327 return Value();
329 // Read deps (if any).
330 const Value* deps_value = block_scope.GetValue(variables::kDeps, true);
331 if (deps_value) {
332 ExtractListOfLabels(
333 *deps_value, block_scope.GetSourceDir(),
334 ToolchainLabelForScope(&block_scope), &toolchain->deps(), err);
335 if (err->has_error())
336 return Value();
339 // Read concurrent_links (if any).
340 const Value* concurrent_links_value =
341 block_scope.GetValue("concurrent_links", true);
342 if (concurrent_links_value) {
343 if (!concurrent_links_value->VerifyTypeIs(Value::INTEGER, err))
344 return Value();
345 if (concurrent_links_value->int_value() < 0 ||
346 concurrent_links_value->int_value() > std::numeric_limits<int>::max()) {
347 *err = Err(*concurrent_links_value, "Value out of range.");
348 return Value();
350 toolchain->set_concurrent_links(
351 static_cast<int>(concurrent_links_value->int_value()));
354 if (!block_scope.CheckForUnusedVars(err))
355 return Value();
357 // Save this toolchain.
358 toolchain->ToolchainSetupComplete();
359 Scope::ItemVector* collector = scope->GetItemCollector();
360 if (!collector) {
361 *err = Err(function, "Can't define a toolchain in this context.");
362 return Value();
364 collector->push_back(toolchain.release());
365 return Value();
368 // tool ------------------------------------------------------------------------
370 const char kTool[] = "tool";
371 const char kTool_HelpShort[] =
372 "tool: Specify arguments to a toolchain tool.";
373 const char kTool_Help[] =
374 "tool: Specify arguments to a toolchain tool.\n"
375 "\n"
376 "Usage:\n"
377 "\n"
378 " tool(<tool type>) {\n"
379 " <tool variables...>\n"
380 " }\n"
381 "\n"
382 "Tool types\n"
383 "\n"
384 " Compiler tools:\n"
385 " \"cc\": C compiler\n"
386 " \"cxx\": C++ compiler\n"
387 " \"objc\": Objective C compiler\n"
388 " \"objcxx\": Objective C++ compiler\n"
389 " \"rc\": Resource compiler (Windows .rc files)\n"
390 " \"asm\": Assembler\n"
391 "\n"
392 " Linker tools:\n"
393 " \"alink\": Linker for static libraries (archives)\n"
394 " \"solink\": Linker for shared libraries\n"
395 " \"link\": Linker for executables\n"
396 "\n"
397 " Other tools:\n"
398 " \"stamp\": Tool for creating stamp files\n"
399 " \"copy\": Tool to copy files.\n"
400 "\n"
401 "Tool variables\n"
402 "\n"
403 " command [string with substitutions]\n"
404 " Valid for: all tools (required)\n"
405 "\n"
406 " The command to run.\n"
407 "\n"
408 " default_output_extension [string]\n"
409 " Valid for: linker tools\n"
410 "\n"
411 " Extension for the main output of a linkable tool. It includes\n"
412 " the leading dot. This will be the default value for the\n"
413 " {{output_extension}} expansion (discussed below) but will be\n"
414 " overridden by by the \"output extension\" variable in a target,\n"
415 " if one is specified. Empty string means no extension.\n"
416 "\n"
417 " GN doesn't actually do anything with this extension other than\n"
418 " pass it along, potentially with target-specific overrides. One\n"
419 " would typically use the {{output_extension}} value in the\n"
420 " \"outputs\" to read this value.\n"
421 "\n"
422 " Example: default_output_extension = \".exe\"\n"
423 "\n"
424 " depfile [string]\n"
425 " Valid for: compiler tools (optional)\n"
426 "\n"
427 " If the tool can write \".d\" files, this specifies the name of\n"
428 " the resulting file. These files are used to list header file\n"
429 " dependencies (or other implicit input dependencies) that are\n"
430 " discovered at build time. See also \"depsformat\".\n"
431 "\n"
432 " Example: depfile = \"{{output}}.d\"\n"
433 "\n"
434 " depsformat [string]\n"
435 " Valid for: compiler tools (when depfile is specified)\n"
436 "\n"
437 " Format for the deps outputs. This is either \"gcc\" or \"msvc\".\n"
438 " See the ninja documentation for \"deps\" for more information.\n"
439 "\n"
440 " Example: depsformat = \"gcc\"\n"
441 "\n"
442 " description [string with substitutions, optional]\n"
443 " Valid for: all tools\n"
444 "\n"
445 " What to print when the command is run.\n"
446 "\n"
447 " Example: description = \"Compiling {{source}}\"\n"
448 "\n"
449 " lib_switch [string, optional, link tools only]\n"
450 " lib_dir_switch [string, optional, link tools only]\n"
451 " Valid for: Linker tools except \"alink\"\n"
452 "\n"
453 " These strings will be prepended to the libraries and library\n"
454 " search directories, respectively, because linkers differ on how\n"
455 " specify them. If you specified:\n"
456 " lib_switch = \"-l\"\n"
457 " lib_dir_switch = \"-L\"\n"
458 " then the \"{{libs}}\" expansion for [ \"freetype\", \"expat\"]\n"
459 " would be \"-lfreetype -lexpat\".\n"
460 "\n"
461 " outputs [list of strings with substitutions]\n"
462 " Valid for: Linker and compiler tools (required)\n"
463 "\n"
464 " An array of names for the output files the tool produces. These\n"
465 " are relative to the build output directory. There must always be\n"
466 " at least one output file. There can be more than one output (a\n"
467 " linker might produce a library and an import library, for\n"
468 " example).\n"
469 "\n"
470 " This array just declares to GN what files the tool will\n"
471 " produce. It is your responsibility to specify the tool command\n"
472 " that actually produces these files.\n"
473 "\n"
474 " If you specify more than one output for shared library links,\n"
475 " you should consider setting link_output and depend_output.\n"
476 " Otherwise, the first entry in the outputs list should always be\n"
477 " the main output which will be linked to.\n"
478 "\n"
479 " Example for a compiler tool that produces .obj files:\n"
480 " outputs = [\n"
481 " \"{{source_out_dir}}/{{source_name_part}}.obj\"\n"
482 " ]\n"
483 "\n"
484 " Example for a linker tool that produces a .dll and a .lib. The\n"
485 " use of {{output_extension}} rather than hardcoding \".dll\"\n"
486 " allows the extension of the library to be overridden on a\n"
487 " target-by-target basis, but in this example, it always\n"
488 " produces a \".lib\" import library:\n"
489 " outputs = [\n"
490 " \"{{root_out_dir}}/{{target_output_name}}"
491 "{{output_extension}}\",\n"
492 " \"{{root_out_dir}}/{{target_output_name}}.lib\",\n"
493 " ]\n"
494 "\n"
495 " link_output [string with substitutions]\n"
496 " depend_output [string with substitutions]\n"
497 " Valid for: \"solink\" only (optional)\n"
498 "\n"
499 " These two files specify whch of the outputs from the solink\n"
500 " tool should be used for linking and dependency tracking. These\n"
501 " should match entries in the \"outputs\". If unspecified, the\n"
502 " first item in the \"outputs\" array will be used for both. See\n"
503 " \"Separate linking and dependencies for shared libraries\"\n"
504 " below for more.\n"
505 "\n"
506 " On Windows, where the tools produce a .dll shared library and\n"
507 " a .lib import library, you will want both of these to be the\n"
508 " import library. On Linux, if you're not doing the separate\n"
509 " linking/dependency optimization, both of these should be the\n"
510 " .so output.\n"
511 "\n"
512 " output_prefix [string]\n"
513 " Valid for: Linker tools (optional)\n"
514 "\n"
515 " Prefix to use for the output name. Defaults to empty. This\n"
516 " prefix will be prepended to the name of the target (or the\n"
517 " output_name if one is manually specified for it) if the prefix\n"
518 " is not already there. The result will show up in the\n"
519 " {{output_name}} substitution pattern.\n"
520 "\n"
521 " This is typically used to prepend \"lib\" to libraries on\n"
522 " Posix systems:\n"
523 " output_prefix = \"lib\"\n"
524 "\n"
525 " precompiled_header_type [string]\n"
526 " Valid for: \"cc\", \"cxx\", \"objc\", \"objcxx\"\n"
527 "\n"
528 " Type of precompiled headers. If undefined or the empty string,\n"
529 " precompiled headers will not be used for this tool. Otherwise\n"
530 " use \"msvc\" which is the only currently supported value.\n"
531 "\n"
532 " For precompiled headers to be used for a given target, the\n"
533 " target (or a config applied to it) must also specify a\n"
534 " \"precompiled_header\" and, for \"msvc\"-style headers, a\n"
535 " \"precompiled_source\" value.\n"
536 "\n"
537 " See \"gn help precompiled_header\" for more.\n"
538 "\n"
539 " restat [boolean]\n"
540 " Valid for: all tools (optional, defaults to false)\n"
541 "\n"
542 " Requests that Ninja check the file timestamp after this tool has\n"
543 " run to determine if anything changed. Set this if your tool has\n"
544 " the ability to skip writing output if the output file has not\n"
545 " changed.\n"
546 "\n"
547 " Normally, Ninja will assume that when a tool runs the output\n"
548 " be new and downstream dependents must be rebuild. When this is\n"
549 " set to trye, Ninja can skip rebuilding downstream dependents for\n"
550 " input changes that don't actually affect the output.\n"
551 "\n"
552 " Example:\n"
553 " restat = true\n"
554 "\n"
555 " rspfile [string with substitutions]\n"
556 " Valid for: all tools (optional)\n"
557 "\n"
558 " Name of the response file. If empty, no response file will be\n"
559 " used. See \"rspfile_content\".\n"
560 "\n"
561 " rspfile_content [string with substitutions]\n"
562 " Valid for: all tools (required when \"rspfile\" is specified)\n"
563 "\n"
564 " The contents to be written to the response file. This may\n"
565 " include all or part of the command to send to the tool which\n"
566 " allows you to get around OS command-line length limits.\n"
567 "\n"
568 " This example adds the inputs and libraries to a response file,\n"
569 " but passes the linker flags directly on the command line:\n"
570 " tool(\"link\") {\n"
571 " command = \"link -o {{output}} {{ldflags}} @{{output}}.rsp\"\n"
572 " rspfile = \"{{output}}.rsp\"\n"
573 " rspfile_content = \"{{inputs}} {{solibs}} {{libs}}\"\n"
574 " }\n"
575 "\n"
576 "Expansions for tool variables\n"
577 "\n"
578 " All paths are relative to the root build directory, which is the\n"
579 " current directory for running all tools. These expansions are\n"
580 " available to all tools:\n"
581 "\n"
582 " {{label}}\n"
583 " The label of the current target. This is typically used in the\n"
584 " \"description\" field for link tools. The toolchain will be\n"
585 " omitted from the label for targets in the default toolchain, and\n"
586 " will be included for targets in other toolchains.\n"
587 "\n"
588 " {{output}}\n"
589 " The relative path and name of the output(s) of the current\n"
590 " build step. If there is more than one output, this will expand\n"
591 " to a list of all of them.\n"
592 " Example: \"out/base/my_file.o\"\n"
593 "\n"
594 " {{target_gen_dir}}\n"
595 " {{target_out_dir}}\n"
596 " The directory of the generated file and output directories,\n"
597 " respectively, for the current target. There is no trailing\n"
598 " slash.\n"
599 " Example: \"out/base/test\"\n"
600 "\n"
601 " {{target_output_name}}\n"
602 " The short name of the current target with no path information,\n"
603 " or the value of the \"output_name\" variable if one is specified\n"
604 " in the target. This will include the \"output_prefix\" if any.\n"
605 " Example: \"libfoo\" for the target named \"foo\" and an\n"
606 " output prefix for the linker tool of \"lib\".\n"
607 "\n"
608 " Compiler tools have the notion of a single input and a single output,\n"
609 " along with a set of compiler-specific flags. The following expansions\n"
610 " are available:\n"
611 "\n"
612 " {{cflags}}\n"
613 " {{cflags_c}}\n"
614 " {{cflags_cc}}\n"
615 " {{cflags_objc}}\n"
616 " {{cflags_objcc}}\n"
617 " {{defines}}\n"
618 " {{include_dirs}}\n"
619 " Strings correspond that to the processed flags/defines/include\n"
620 " directories specified for the target.\n"
621 " Example: \"--enable-foo --enable-bar\"\n"
622 "\n"
623 " Defines will be prefixed by \"-D\" and include directories will\n"
624 " be prefixed by \"-I\" (these work with Posix tools as well as\n"
625 " Microsoft ones).\n"
626 "\n"
627 " {{source}}\n"
628 " The relative path and name of the current input file.\n"
629 " Example: \"../../base/my_file.cc\"\n"
630 "\n"
631 " {{source_file_part}}\n"
632 " The file part of the source including the extension (with no\n"
633 " directory information).\n"
634 " Example: \"foo.cc\"\n"
635 "\n"
636 " {{source_name_part}}\n"
637 " The filename part of the source file with no directory or\n"
638 " extension.\n"
639 " Example: \"foo\"\n"
640 "\n"
641 " {{source_gen_dir}}\n"
642 " {{source_out_dir}}\n"
643 " The directory in the generated file and output directories,\n"
644 " respectively, for the current input file. If the source file\n"
645 " is in the same directory as the target is declared in, they will\n"
646 " will be the same as the \"target\" versions above.\n"
647 " Example: \"gen/base/test\"\n"
648 "\n"
649 " Linker tools have multiple inputs and (potentially) multiple outputs\n"
650 " The following expansions are available:\n"
651 "\n"
652 " {{inputs}}\n"
653 " {{inputs_newline}}\n"
654 " Expands to the inputs to the link step. This will be a list of\n"
655 " object files and static libraries.\n"
656 " Example: \"obj/foo.o obj/bar.o obj/somelibrary.a\"\n"
657 "\n"
658 " The \"_newline\" version will separate the input files with\n"
659 " newlines instead of spaces. This is useful in response files:\n"
660 " some linkers can take a \"-filelist\" flag which expects newline\n"
661 " separated files, and some Microsoft tools have a fixed-sized\n"
662 " buffer for parsing each line of a response file.\n"
663 "\n"
664 " {{ldflags}}\n"
665 " Expands to the processed set of ldflags and library search paths\n"
666 " specified for the target.\n"
667 " Example: \"-m64 -fPIC -pthread -L/usr/local/mylib\"\n"
668 "\n"
669 " {{libs}}\n"
670 " Expands to the list of system libraries to link to. Each will\n"
671 " be prefixed by the \"lib_prefix\".\n"
672 "\n"
673 " As a special case to support Mac, libraries with names ending in\n"
674 " \".framework\" will be added to the {{libs}} with \"-framework\"\n"
675 " preceeding it, and the lib prefix will be ignored.\n"
676 "\n"
677 " Example: \"-lfoo -lbar\"\n"
678 "\n"
679 " {{output_extension}}\n"
680 " The value of the \"output_extension\" variable in the target,\n"
681 " or the value of the \"default_output_extension\" value in the\n"
682 " tool if the target does not specify an output extension.\n"
683 " Example: \".so\"\n"
684 "\n"
685 " {{solibs}}\n"
686 " Extra libraries from shared library dependencide not specified\n"
687 " in the {{inputs}}. This is the list of link_output files from\n"
688 " shared libraries (if the solink tool specifies a \"link_output\"\n"
689 " variable separate from the \"depend_output\").\n"
690 "\n"
691 " These should generally be treated the same as libs by your tool.\n"
692 " Example: \"libfoo.so libbar.so\"\n"
693 "\n"
694 " The copy tool allows the common compiler/linker substitutions, plus\n"
695 " {{source}} which is the source of the copy. The stamp tool allows\n"
696 " only the common tool substitutions.\n"
697 "\n"
698 "Separate linking and dependencies for shared libraries\n"
699 "\n"
700 " Shared libraries are special in that not all changes to them require\n"
701 " that dependent targets be re-linked. If the shared library is changed\n"
702 " but no imports or exports are different, dependent code needn't be\n"
703 " relinked, which can speed up the build.\n"
704 "\n"
705 " If your link step can output a list of exports from a shared library\n"
706 " and writes the file only if the new one is different, the timestamp of\n"
707 " this file can be used for triggering re-links, while the actual shared\n"
708 " library would be used for linking.\n"
709 "\n"
710 " You will need to specify\n"
711 " restat = true\n"
712 " in the linker tool to make this work, so Ninja will detect if the\n"
713 " timestamp of the dependency file has changed after linking (otherwise\n"
714 " it will always assume that running a command updates the output):\n"
715 "\n"
716 " tool(\"solink\") {\n"
717 " command = \"...\"\n"
718 " outputs = [\n"
719 " \"{{root_out_dir}}/{{target_output_name}}{{output_extension}}\",\n"
720 " \"{{root_out_dir}}/{{target_output_name}}"
721 "{{output_extension}}.TOC\",\n"
722 " ]\n"
723 " link_output =\n"
724 " \"{{root_out_dir}}/{{target_output_name}}{{output_extension}}\"\n"
725 " depend_output =\n"
726 " \"{{root_out_dir}}/{{target_output_name}}"
727 "{{output_extension}}.TOC\"\n"
728 " restat = true\n"
729 " }\n"
730 "\n"
731 "Example\n"
732 "\n"
733 " toolchain(\"my_toolchain\") {\n"
734 " # Put these at the top to apply to all tools below.\n"
735 " lib_prefix = \"-l\"\n"
736 " lib_dir_prefix = \"-L\"\n"
737 "\n"
738 " tool(\"cc\") {\n"
739 " command = \"gcc {{source}} -o {{output}}\"\n"
740 " outputs = [ \"{{source_out_dir}}/{{source_name_part}}.o\" ]\n"
741 " description = \"GCC {{source}}\"\n"
742 " }\n"
743 " tool(\"cxx\") {\n"
744 " command = \"g++ {{source}} -o {{output}}\"\n"
745 " outputs = [ \"{{source_out_dir}}/{{source_name_part}}.o\" ]\n"
746 " description = \"G++ {{source}}\"\n"
747 " }\n"
748 " }\n";
750 Value RunTool(Scope* scope,
751 const FunctionCallNode* function,
752 const std::vector<Value>& args,
753 BlockNode* block,
754 Err* err) {
755 // Find the toolchain definition we're executing inside of. The toolchain
756 // function will set a property pointing to it that we'll pick up.
757 Toolchain* toolchain = reinterpret_cast<Toolchain*>(
758 scope->GetProperty(&kToolchainPropertyKey, nullptr));
759 if (!toolchain) {
760 *err = Err(function->function(), "tool() called outside of toolchain().",
761 "The tool() function can only be used inside a toolchain() "
762 "definition.");
763 return Value();
766 if (!EnsureSingleStringArg(function, args, err))
767 return Value();
768 const std::string& tool_name = args[0].string_value();
769 Toolchain::ToolType tool_type = Toolchain::ToolNameToType(tool_name);
770 if (tool_type == Toolchain::TYPE_NONE) {
771 *err = Err(args[0], "Unknown tool type");
772 return Value();
775 // Run the tool block.
776 Scope block_scope(scope);
777 block->Execute(&block_scope, err);
778 if (err->has_error())
779 return Value();
781 // Figure out which validator to use for the substitution pattern for this
782 // tool type. There are different validators for the "outputs" than for the
783 // rest of the strings.
784 bool (*subst_validator)(SubstitutionType) = nullptr;
785 bool (*subst_output_validator)(SubstitutionType) = nullptr;
786 if (IsCompilerTool(tool_type)) {
787 subst_validator = &IsValidCompilerSubstitution;
788 subst_output_validator = &IsValidCompilerOutputsSubstitution;
789 } else if (IsLinkerTool(tool_type)) {
790 subst_validator = &IsValidLinkerSubstitution;
791 subst_output_validator = &IsValidLinkerOutputsSubstitution;
792 } else if (tool_type == Toolchain::TYPE_COPY) {
793 subst_validator = &IsValidCopySubstitution;
794 subst_output_validator = &IsValidCopySubstitution;
795 } else {
796 subst_validator = &IsValidToolSubstutition;
797 subst_output_validator = &IsValidToolSubstutition;
800 scoped_ptr<Tool> tool(new Tool);
802 if (!ReadPattern(&block_scope, "command", subst_validator, tool.get(),
803 &Tool::set_command, err) ||
804 !ReadOutputExtension(&block_scope, tool.get(), err) ||
805 !ReadPattern(&block_scope, "depfile", subst_validator, tool.get(),
806 &Tool::set_depfile, err) ||
807 !ReadDepsFormat(&block_scope, tool.get(), err) ||
808 !ReadPattern(&block_scope, "description", subst_validator, tool.get(),
809 &Tool::set_description, err) ||
810 !ReadString(&block_scope, "lib_switch", tool.get(),
811 &Tool::set_lib_switch, err) ||
812 !ReadString(&block_scope, "lib_dir_switch", tool.get(),
813 &Tool::set_lib_dir_switch, err) ||
814 !ReadPattern(&block_scope, "link_output", subst_validator, tool.get(),
815 &Tool::set_link_output, err) ||
816 !ReadPattern(&block_scope, "depend_output", subst_validator, tool.get(),
817 &Tool::set_depend_output, err) ||
818 !ReadString(&block_scope, "output_prefix", tool.get(),
819 &Tool::set_output_prefix, err) ||
820 !ReadPrecompiledHeaderType(&block_scope, tool.get(), err) ||
821 !ReadBool(&block_scope, "restat", tool.get(), &Tool::set_restat, err) ||
822 !ReadPattern(&block_scope, "rspfile", subst_validator, tool.get(),
823 &Tool::set_rspfile, err) ||
824 !ReadPattern(&block_scope, "rspfile_content", subst_validator, tool.get(),
825 &Tool::set_rspfile_content, err)) {
826 return Value();
829 if (tool_type != Toolchain::TYPE_COPY && tool_type != Toolchain::TYPE_STAMP) {
830 // All tools except the copy and stamp tools should have outputs. The copy
831 // and stamp tool's outputs are generated internally.
832 if (!ReadOutputs(&block_scope, function, subst_output_validator,
833 tool.get(), err))
834 return Value();
837 // Validate that the link_output and depend_output refer to items in the
838 // outputs and aren't defined for irrelevant tool types.
839 if (!tool->link_output().empty()) {
840 if (tool_type != Toolchain::TYPE_SOLINK) {
841 *err = Err(function, "This tool specifies a link_output.",
842 "This is only valid for solink tools.");
843 return Value();
845 if (!IsPatternInOutputList(tool->outputs(), tool->link_output())) {
846 *err = Err(function, "This tool's link_output is bad.",
847 "It must match one of the outputs.");
848 return Value();
851 if (!tool->depend_output().empty()) {
852 if (tool_type != Toolchain::TYPE_SOLINK) {
853 *err = Err(function, "This tool specifies a depend_output.",
854 "This is only valid for solink tools.");
855 return Value();
857 if (!IsPatternInOutputList(tool->outputs(), tool->depend_output())) {
858 *err = Err(function, "This tool's depend_output is bad.",
859 "It must match one of the outputs.");
860 return Value();
863 if ((!tool->link_output().empty() && tool->depend_output().empty()) ||
864 (tool->link_output().empty() && !tool->depend_output().empty())) {
865 *err = Err(function, "Both link_output and depend_output should either "
866 "be specified or they should both be empty.");
867 return Value();
870 // Make sure there weren't any vars set in this tool that were unused.
871 if (!block_scope.CheckForUnusedVars(err))
872 return Value();
874 toolchain->SetTool(tool_type, tool.Pass());
875 return Value();
878 // toolchain_args --------------------------------------------------------------
880 extern const char kToolchainArgs[] = "toolchain_args";
881 extern const char kToolchainArgs_HelpShort[] =
882 "toolchain_args: Set build arguments for toolchain build setup.";
883 extern const char kToolchainArgs_Help[] =
884 "toolchain_args: Set build arguments for toolchain build setup.\n"
885 "\n"
886 " Used inside a toolchain definition to pass arguments to an alternate\n"
887 " toolchain's invocation of the build.\n"
888 "\n"
889 " When you specify a target using an alternate toolchain, the master\n"
890 " build configuration file is re-interpreted in the context of that\n"
891 " toolchain (see \"gn help toolchain\"). The toolchain_args function\n"
892 " allows you to control the arguments passed into this alternate\n"
893 " invocation of the build.\n"
894 "\n"
895 " Any default system arguments or arguments passed in on the command-\n"
896 " line will also be passed to the alternate invocation unless explicitly\n"
897 " overridden by toolchain_args.\n"
898 "\n"
899 " The toolchain_args will be ignored when the toolchain being defined\n"
900 " is the default. In this case, it's expected you want the default\n"
901 " argument values.\n"
902 "\n"
903 " See also \"gn help buildargs\" for an overview of these arguments.\n"
904 "\n"
905 "Example:\n"
906 " toolchain(\"my_weird_toolchain\") {\n"
907 " ...\n"
908 " toolchain_args() {\n"
909 " # Override the system values for a generic Posix system.\n"
910 " is_win = false\n"
911 " is_posix = true\n"
912 "\n"
913 " # Pass this new value for specific setup for my toolchain.\n"
914 " is_my_weird_system = true\n"
915 " }\n"
916 " }\n";
918 Value RunToolchainArgs(Scope* scope,
919 const FunctionCallNode* function,
920 const std::vector<Value>& args,
921 BlockNode* block,
922 Err* err) {
923 // Find the toolchain definition we're executing inside of. The toolchain
924 // function will set a property pointing to it that we'll pick up.
925 Toolchain* toolchain = reinterpret_cast<Toolchain*>(
926 scope->GetProperty(&kToolchainPropertyKey, nullptr));
927 if (!toolchain) {
928 *err = Err(function->function(),
929 "toolchain_args() called outside of toolchain().",
930 "The toolchain_args() function can only be used inside a "
931 "toolchain() definition.");
932 return Value();
935 if (!args.empty()) {
936 *err = Err(function->function(), "This function takes no arguments.");
937 return Value();
940 // This function makes a new scope with various variable sets on it, which
941 // we then save on the toolchain to use when re-invoking the build.
942 Scope block_scope(scope);
943 block->Execute(&block_scope, err);
944 if (err->has_error())
945 return Value();
947 Scope::KeyValueMap values;
948 block_scope.GetCurrentScopeValues(&values);
949 toolchain->args() = values;
951 return Value();
954 } // namespace functions