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.
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"
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
,
30 void (Tool::*set
)(bool),
32 const Value
* v
= scope
->GetValue(var
, true);
34 return true; // Not present is fine.
35 if (!v
->VerifyTypeIs(Value::BOOLEAN
, err
))
38 (tool
->*set
)(v
->boolean_value());
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
,
47 void (Tool::*set
)(const std::string
&),
49 const Value
* v
= scope
->GetValue(var
, true);
51 return true; // Not present is fine.
52 if (!v
->VerifyTypeIs(Value::STRING
, err
))
55 (tool
->*set
)(v
->string_value());
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
),
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.");
76 bool ReadPattern(Scope
* scope
,
78 bool (*validate
)(SubstitutionType
),
80 void (Tool::*set
)(const SubstitutionPattern
&),
82 const Value
* value
= scope
->GetValue(name
, true);
84 return true; // Not present is fine.
85 if (!value
->VerifyTypeIs(Value::STRING
, err
))
88 SubstitutionPattern pattern
;
89 if (!pattern
.Parse(*value
, err
))
91 if (!ValidateSubstitutionList(pattern
.required_types(), validate
, value
, err
))
94 (tool
->*set
)(pattern
);
98 bool ReadOutputExtension(Scope
* scope
, Tool
* tool
, Err
* err
) {
99 const Value
* value
= scope
->GetValue("default_output_extension", true);
101 return true; // Not present is fine.
102 if (!value
->VerifyTypeIs(Value::STRING
, err
))
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 '.'");
113 tool
->set_default_output_extension(value
->string_value());
117 bool ReadPrecompiledHeaderType(Scope
* scope
, Tool
* tool
, Err
* err
) {
118 const Value
* value
= scope
->GetValue("precompiled_header_type", true);
120 return true; // Not present is fine.
121 if (!value
->VerifyTypeIs(Value::STRING
, err
))
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
);
131 *err
= Err(*value
, "Invalid precompiled_header_type",
132 "Must either be empty or \"msvc\".");
136 bool ReadDepsFormat(Scope
* scope
, Tool
* tool
, Err
* err
) {
137 const Value
* value
= scope
->GetValue("depsformat", true);
139 return true; // Not present is fine.
140 if (!value
->VerifyTypeIs(Value::STRING
, err
))
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
);
148 *err
= Err(*value
, "Deps format must be \"gcc\" or \"msvc\".");
154 bool ReadOutputs(Scope
* scope
,
155 const FunctionCallNode
* tool_function
,
156 bool (*validate
)(SubstitutionType
),
159 const Value
* value
= scope
->GetValue("outputs", true);
161 *err
= Err(tool_function
, "\"outputs\" must be specified for this tool.");
165 SubstitutionList list
;
166 if (!list
.Parse(*value
, err
))
169 // Validate the right kinds of patterns are used.
170 if (!ValidateSubstitutionList(list
.required_types(), validate
, value
, err
))
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.");
179 tool
->set_outputs(list
);
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()))
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"
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"
223 "Functions and variables\n"
226 " The tool() function call specifies the commands commands to run for\n"
227 " a given step. See \"gn help tool\".\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"
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"
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"
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"
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"
256 " Defaults to 0 which Ninja interprets as \"no limit\".\n"
258 " The value used will be the one from the default toolchain of the\n"
261 "Invoking targets in toolchains:\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"
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"
273 " To load a file in an alternate toolchain, GN does the following:\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"
284 " toolchain(\"plugin_toolchain\") {\n"
285 " concurrent_links = 8\n"
288 " command = \"gcc {{source}}\"\n"
292 " toolchain_args() {\n"
293 " is_plugin = true\n"
295 " is_64bit = false\n"
299 Value
RunToolchain(Scope
* scope
,
300 const FunctionCallNode
* function
,
301 const std::vector
<Value
>& args
,
304 if (!EnsureNotProcessingImport(function
, scope
, err
) ||
305 !EnsureNotProcessingBuildConfig(function
, scope
, err
))
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())
329 // Read deps (if any).
330 const Value
* deps_value
= block_scope
.GetValue(variables::kDeps
, true);
333 *deps_value
, block_scope
.GetSourceDir(),
334 ToolchainLabelForScope(&block_scope
), &toolchain
->deps(), err
);
335 if (err
->has_error())
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
))
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.");
350 toolchain
->set_concurrent_links(
351 static_cast<int>(concurrent_links_value
->int_value()));
354 if (!block_scope
.CheckForUnusedVars(err
))
357 // Save this toolchain.
358 toolchain
->ToolchainSetupComplete();
359 Scope::ItemVector
* collector
= scope
->GetItemCollector();
361 *err
= Err(function
, "Can't define a toolchain in this context.");
364 collector
->push_back(toolchain
.release());
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"
378 " tool(<tool type>) {\n"
379 " <tool variables...>\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"
393 " \"alink\": Linker for static libraries (archives)\n"
394 " \"solink\": Linker for shared libraries\n"
395 " \"link\": Linker for executables\n"
398 " \"stamp\": Tool for creating stamp files\n"
399 " \"copy\": Tool to copy files.\n"
403 " command [string with substitutions]\n"
404 " Valid for: all tools (required)\n"
406 " The command to run.\n"
408 " default_output_extension [string]\n"
409 " Valid for: linker tools\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"
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"
422 " Example: default_output_extension = \".exe\"\n"
424 " depfile [string]\n"
425 " Valid for: compiler tools (optional)\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"
432 " Example: depfile = \"{{output}}.d\"\n"
434 " depsformat [string]\n"
435 " Valid for: compiler tools (when depfile is specified)\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"
440 " Example: depsformat = \"gcc\"\n"
442 " description [string with substitutions, optional]\n"
443 " Valid for: all tools\n"
445 " What to print when the command is run.\n"
447 " Example: description = \"Compiling {{source}}\"\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"
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"
461 " outputs [list of strings with substitutions]\n"
462 " Valid for: Linker and compiler tools (required)\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"
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"
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"
479 " Example for a compiler tool that produces .obj files:\n"
481 " \"{{source_out_dir}}/{{source_name_part}}.obj\"\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"
490 " \"{{root_out_dir}}/{{target_output_name}}"
491 "{{output_extension}}\",\n"
492 " \"{{root_out_dir}}/{{target_output_name}}.lib\",\n"
495 " link_output [string with substitutions]\n"
496 " depend_output [string with substitutions]\n"
497 " Valid for: \"solink\" only (optional)\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"
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"
512 " output_prefix [string]\n"
513 " Valid for: Linker tools (optional)\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"
521 " This is typically used to prepend \"lib\" to libraries on\n"
523 " output_prefix = \"lib\"\n"
525 " precompiled_header_type [string]\n"
526 " Valid for: \"cc\", \"cxx\", \"objc\", \"objcxx\"\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"
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"
537 " See \"gn help precompiled_header\" for more.\n"
539 " restat [boolean]\n"
540 " Valid for: all tools (optional, defaults to false)\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"
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"
555 " rspfile [string with substitutions]\n"
556 " Valid for: all tools (optional)\n"
558 " Name of the response file. If empty, no response file will be\n"
559 " used. See \"rspfile_content\".\n"
561 " rspfile_content [string with substitutions]\n"
562 " Valid for: all tools (required when \"rspfile\" is specified)\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"
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"
576 "Expansions for tool variables\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"
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"
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"
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"
599 " Example: \"out/base/test\"\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"
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"
616 " {{cflags_objcc}}\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"
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"
628 " The relative path and name of the current input file.\n"
629 " Example: \"../../base/my_file.cc\"\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"
636 " {{source_name_part}}\n"
637 " The filename part of the source file with no directory or\n"
639 " Example: \"foo\"\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"
649 " Linker tools have multiple inputs and (potentially) multiple outputs\n"
650 " The following expansions are available:\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"
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"
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"
670 " Expands to the list of system libraries to link to. Each will\n"
671 " be prefixed by the \"lib_prefix\".\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"
677 " Example: \"-lfoo -lbar\"\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"
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"
691 " These should generally be treated the same as libs by your tool.\n"
692 " Example: \"libfoo.so libbar.so\"\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"
698 "Separate linking and dependencies for shared libraries\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"
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"
710 " You will need to specify\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"
716 " tool(\"solink\") {\n"
717 " command = \"...\"\n"
719 " \"{{root_out_dir}}/{{target_output_name}}{{output_extension}}\",\n"
720 " \"{{root_out_dir}}/{{target_output_name}}"
721 "{{output_extension}}.TOC\",\n"
724 " \"{{root_out_dir}}/{{target_output_name}}{{output_extension}}\"\n"
726 " \"{{root_out_dir}}/{{target_output_name}}"
727 "{{output_extension}}.TOC\"\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"
739 " command = \"gcc {{source}} -o {{output}}\"\n"
740 " outputs = [ \"{{source_out_dir}}/{{source_name_part}}.o\" ]\n"
741 " description = \"GCC {{source}}\"\n"
744 " command = \"g++ {{source}} -o {{output}}\"\n"
745 " outputs = [ \"{{source_out_dir}}/{{source_name_part}}.o\" ]\n"
746 " description = \"G++ {{source}}\"\n"
750 Value
RunTool(Scope
* scope
,
751 const FunctionCallNode
* function
,
752 const std::vector
<Value
>& args
,
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));
760 *err
= Err(function
->function(), "tool() called outside of toolchain().",
761 "The tool() function can only be used inside a toolchain() "
766 if (!EnsureSingleStringArg(function
, args
, err
))
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");
775 // Run the tool block.
776 Scope
block_scope(scope
);
777 block
->Execute(&block_scope
, err
);
778 if (err
->has_error())
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
;
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
)) {
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
,
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.");
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.");
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.");
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.");
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.");
870 // Make sure there weren't any vars set in this tool that were unused.
871 if (!block_scope
.CheckForUnusedVars(err
))
874 toolchain
->SetTool(tool_type
, tool
.Pass());
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"
886 " Used inside a toolchain definition to pass arguments to an alternate\n"
887 " toolchain's invocation of the build.\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"
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"
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"
903 " See also \"gn help buildargs\" for an overview of these arguments.\n"
906 " toolchain(\"my_weird_toolchain\") {\n"
908 " toolchain_args() {\n"
909 " # Override the system values for a generic Posix system.\n"
913 " # Pass this new value for specific setup for my toolchain.\n"
914 " is_my_weird_system = true\n"
918 Value
RunToolchainArgs(Scope
* scope
,
919 const FunctionCallNode
* function
,
920 const std::vector
<Value
>& args
,
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));
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.");
936 *err
= Err(function
->function(), "This function takes no arguments.");
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())
947 Scope::KeyValueMap values
;
948 block_scope
.GetCurrentScopeValues(&values
);
949 toolchain
->args() = values
;
954 } // namespace functions