Partially fix compilation of media_unittests with Xcode 7 (OS X 10.11 SDK).
[chromium-blink-merge.git] / tools / gn / function_toolchain.cc
blob4e1fe2d418e684c07616a352b3689b66e2642c7b
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 NonNestableBlock non_nestable(scope, function, "toolchain");
305 if (!non_nestable.Enter(err))
306 return Value();
308 if (!EnsureNotProcessingImport(function, scope, err) ||
309 !EnsureNotProcessingBuildConfig(function, scope, err))
310 return Value();
312 // Note that we don't want to use MakeLabelForScope since that will include
313 // the toolchain name in the label, and toolchain labels don't themselves
314 // have toolchain names.
315 const SourceDir& input_dir = scope->GetSourceDir();
316 Label label(input_dir, args[0].string_value());
317 if (g_scheduler->verbose_logging())
318 g_scheduler->Log("Defining toolchain", label.GetUserVisibleName(false));
320 // This object will actually be copied into the one owned by the toolchain
321 // manager, but that has to be done in the lock.
322 scoped_ptr<Toolchain> toolchain(new Toolchain(scope->settings(), label));
323 toolchain->set_defined_from(function);
324 toolchain->visibility().SetPublic();
326 Scope block_scope(scope);
327 block_scope.SetProperty(&kToolchainPropertyKey, toolchain.get());
328 block->Execute(&block_scope, err);
329 block_scope.SetProperty(&kToolchainPropertyKey, nullptr);
330 if (err->has_error())
331 return Value();
333 // Read deps (if any).
334 const Value* deps_value = block_scope.GetValue(variables::kDeps, true);
335 if (deps_value) {
336 ExtractListOfLabels(
337 *deps_value, block_scope.GetSourceDir(),
338 ToolchainLabelForScope(&block_scope), &toolchain->deps(), err);
339 if (err->has_error())
340 return Value();
343 // Read concurrent_links (if any).
344 const Value* concurrent_links_value =
345 block_scope.GetValue("concurrent_links", true);
346 if (concurrent_links_value) {
347 if (!concurrent_links_value->VerifyTypeIs(Value::INTEGER, err))
348 return Value();
349 if (concurrent_links_value->int_value() < 0 ||
350 concurrent_links_value->int_value() > std::numeric_limits<int>::max()) {
351 *err = Err(*concurrent_links_value, "Value out of range.");
352 return Value();
354 toolchain->set_concurrent_links(
355 static_cast<int>(concurrent_links_value->int_value()));
358 if (!block_scope.CheckForUnusedVars(err))
359 return Value();
361 // Save this toolchain.
362 toolchain->ToolchainSetupComplete();
363 Scope::ItemVector* collector = scope->GetItemCollector();
364 if (!collector) {
365 *err = Err(function, "Can't define a toolchain in this context.");
366 return Value();
368 collector->push_back(toolchain.release());
369 return Value();
372 // tool ------------------------------------------------------------------------
374 const char kTool[] = "tool";
375 const char kTool_HelpShort[] =
376 "tool: Specify arguments to a toolchain tool.";
377 const char kTool_Help[] =
378 "tool: Specify arguments to a toolchain tool.\n"
379 "\n"
380 "Usage:\n"
381 "\n"
382 " tool(<tool type>) {\n"
383 " <tool variables...>\n"
384 " }\n"
385 "\n"
386 "Tool types\n"
387 "\n"
388 " Compiler tools:\n"
389 " \"cc\": C compiler\n"
390 " \"cxx\": C++ compiler\n"
391 " \"objc\": Objective C compiler\n"
392 " \"objcxx\": Objective C++ compiler\n"
393 " \"rc\": Resource compiler (Windows .rc files)\n"
394 " \"asm\": Assembler\n"
395 "\n"
396 " Linker tools:\n"
397 " \"alink\": Linker for static libraries (archives)\n"
398 " \"solink\": Linker for shared libraries\n"
399 " \"link\": Linker for executables\n"
400 "\n"
401 " Other tools:\n"
402 " \"stamp\": Tool for creating stamp files\n"
403 " \"copy\": Tool to copy files.\n"
404 "\n"
405 "Tool variables\n"
406 "\n"
407 " command [string with substitutions]\n"
408 " Valid for: all tools (required)\n"
409 "\n"
410 " The command to run.\n"
411 "\n"
412 " default_output_extension [string]\n"
413 " Valid for: linker tools\n"
414 "\n"
415 " Extension for the main output of a linkable tool. It includes\n"
416 " the leading dot. This will be the default value for the\n"
417 " {{output_extension}} expansion (discussed below) but will be\n"
418 " overridden by by the \"output extension\" variable in a target,\n"
419 " if one is specified. Empty string means no extension.\n"
420 "\n"
421 " GN doesn't actually do anything with this extension other than\n"
422 " pass it along, potentially with target-specific overrides. One\n"
423 " would typically use the {{output_extension}} value in the\n"
424 " \"outputs\" to read this value.\n"
425 "\n"
426 " Example: default_output_extension = \".exe\"\n"
427 "\n"
428 " depfile [string]\n"
429 " Valid for: compiler tools (optional)\n"
430 "\n"
431 " If the tool can write \".d\" files, this specifies the name of\n"
432 " the resulting file. These files are used to list header file\n"
433 " dependencies (or other implicit input dependencies) that are\n"
434 " discovered at build time. See also \"depsformat\".\n"
435 "\n"
436 " Example: depfile = \"{{output}}.d\"\n"
437 "\n"
438 " depsformat [string]\n"
439 " Valid for: compiler tools (when depfile is specified)\n"
440 "\n"
441 " Format for the deps outputs. This is either \"gcc\" or \"msvc\".\n"
442 " See the ninja documentation for \"deps\" for more information.\n"
443 "\n"
444 " Example: depsformat = \"gcc\"\n"
445 "\n"
446 " description [string with substitutions, optional]\n"
447 " Valid for: all tools\n"
448 "\n"
449 " What to print when the command is run.\n"
450 "\n"
451 " Example: description = \"Compiling {{source}}\"\n"
452 "\n"
453 " lib_switch [string, optional, link tools only]\n"
454 " lib_dir_switch [string, optional, link tools only]\n"
455 " Valid for: Linker tools except \"alink\"\n"
456 "\n"
457 " These strings will be prepended to the libraries and library\n"
458 " search directories, respectively, because linkers differ on how\n"
459 " specify them. If you specified:\n"
460 " lib_switch = \"-l\"\n"
461 " lib_dir_switch = \"-L\"\n"
462 " then the \"{{libs}}\" expansion for [ \"freetype\", \"expat\"]\n"
463 " would be \"-lfreetype -lexpat\".\n"
464 "\n"
465 " outputs [list of strings with substitutions]\n"
466 " Valid for: Linker and compiler tools (required)\n"
467 "\n"
468 " An array of names for the output files the tool produces. These\n"
469 " are relative to the build output directory. There must always be\n"
470 " at least one output file. There can be more than one output (a\n"
471 " linker might produce a library and an import library, for\n"
472 " example).\n"
473 "\n"
474 " This array just declares to GN what files the tool will\n"
475 " produce. It is your responsibility to specify the tool command\n"
476 " that actually produces these files.\n"
477 "\n"
478 " If you specify more than one output for shared library links,\n"
479 " you should consider setting link_output and depend_output.\n"
480 " Otherwise, the first entry in the outputs list should always be\n"
481 " the main output which will be linked to.\n"
482 "\n"
483 " Example for a compiler tool that produces .obj files:\n"
484 " outputs = [\n"
485 " \"{{source_out_dir}}/{{source_name_part}}.obj\"\n"
486 " ]\n"
487 "\n"
488 " Example for a linker tool that produces a .dll and a .lib. The\n"
489 " use of {{output_extension}} rather than hardcoding \".dll\"\n"
490 " allows the extension of the library to be overridden on a\n"
491 " target-by-target basis, but in this example, it always\n"
492 " produces a \".lib\" import library:\n"
493 " outputs = [\n"
494 " \"{{root_out_dir}}/{{target_output_name}}"
495 "{{output_extension}}\",\n"
496 " \"{{root_out_dir}}/{{target_output_name}}.lib\",\n"
497 " ]\n"
498 "\n"
499 " link_output [string with substitutions]\n"
500 " depend_output [string with substitutions]\n"
501 " Valid for: \"solink\" only (optional)\n"
502 "\n"
503 " These two files specify whch of the outputs from the solink\n"
504 " tool should be used for linking and dependency tracking. These\n"
505 " should match entries in the \"outputs\". If unspecified, the\n"
506 " first item in the \"outputs\" array will be used for both. See\n"
507 " \"Separate linking and dependencies for shared libraries\"\n"
508 " below for more.\n"
509 "\n"
510 " On Windows, where the tools produce a .dll shared library and\n"
511 " a .lib import library, you will want both of these to be the\n"
512 " import library. On Linux, if you're not doing the separate\n"
513 " linking/dependency optimization, both of these should be the\n"
514 " .so output.\n"
515 "\n"
516 " output_prefix [string]\n"
517 " Valid for: Linker tools (optional)\n"
518 "\n"
519 " Prefix to use for the output name. Defaults to empty. This\n"
520 " prefix will be prepended to the name of the target (or the\n"
521 " output_name if one is manually specified for it) if the prefix\n"
522 " is not already there. The result will show up in the\n"
523 " {{output_name}} substitution pattern.\n"
524 "\n"
525 " This is typically used to prepend \"lib\" to libraries on\n"
526 " Posix systems:\n"
527 " output_prefix = \"lib\"\n"
528 "\n"
529 " precompiled_header_type [string]\n"
530 " Valid for: \"cc\", \"cxx\", \"objc\", \"objcxx\"\n"
531 "\n"
532 " Type of precompiled headers. If undefined or the empty string,\n"
533 " precompiled headers will not be used for this tool. Otherwise\n"
534 " use \"msvc\" which is the only currently supported value.\n"
535 "\n"
536 " For precompiled headers to be used for a given target, the\n"
537 " target (or a config applied to it) must also specify a\n"
538 " \"precompiled_header\" and, for \"msvc\"-style headers, a\n"
539 " \"precompiled_source\" value.\n"
540 "\n"
541 " See \"gn help precompiled_header\" for more.\n"
542 "\n"
543 " restat [boolean]\n"
544 " Valid for: all tools (optional, defaults to false)\n"
545 "\n"
546 " Requests that Ninja check the file timestamp after this tool has\n"
547 " run to determine if anything changed. Set this if your tool has\n"
548 " the ability to skip writing output if the output file has not\n"
549 " changed.\n"
550 "\n"
551 " Normally, Ninja will assume that when a tool runs the output\n"
552 " be new and downstream dependents must be rebuild. When this is\n"
553 " set to trye, Ninja can skip rebuilding downstream dependents for\n"
554 " input changes that don't actually affect the output.\n"
555 "\n"
556 " Example:\n"
557 " restat = true\n"
558 "\n"
559 " rspfile [string with substitutions]\n"
560 " Valid for: all tools (optional)\n"
561 "\n"
562 " Name of the response file. If empty, no response file will be\n"
563 " used. See \"rspfile_content\".\n"
564 "\n"
565 " rspfile_content [string with substitutions]\n"
566 " Valid for: all tools (required when \"rspfile\" is specified)\n"
567 "\n"
568 " The contents to be written to the response file. This may\n"
569 " include all or part of the command to send to the tool which\n"
570 " allows you to get around OS command-line length limits.\n"
571 "\n"
572 " This example adds the inputs and libraries to a response file,\n"
573 " but passes the linker flags directly on the command line:\n"
574 " tool(\"link\") {\n"
575 " command = \"link -o {{output}} {{ldflags}} @{{output}}.rsp\"\n"
576 " rspfile = \"{{output}}.rsp\"\n"
577 " rspfile_content = \"{{inputs}} {{solibs}} {{libs}}\"\n"
578 " }\n"
579 "\n"
580 "Expansions for tool variables\n"
581 "\n"
582 " All paths are relative to the root build directory, which is the\n"
583 " current directory for running all tools. These expansions are\n"
584 " available to all tools:\n"
585 "\n"
586 " {{label}}\n"
587 " The label of the current target. This is typically used in the\n"
588 " \"description\" field for link tools. The toolchain will be\n"
589 " omitted from the label for targets in the default toolchain, and\n"
590 " will be included for targets in other toolchains.\n"
591 "\n"
592 " {{output}}\n"
593 " The relative path and name of the output(s) of the current\n"
594 " build step. If there is more than one output, this will expand\n"
595 " to a list of all of them.\n"
596 " Example: \"out/base/my_file.o\"\n"
597 "\n"
598 " {{target_gen_dir}}\n"
599 " {{target_out_dir}}\n"
600 " The directory of the generated file and output directories,\n"
601 " respectively, for the current target. There is no trailing\n"
602 " slash.\n"
603 " Example: \"out/base/test\"\n"
604 "\n"
605 " {{target_output_name}}\n"
606 " The short name of the current target with no path information,\n"
607 " or the value of the \"output_name\" variable if one is specified\n"
608 " in the target. This will include the \"output_prefix\" if any.\n"
609 " Example: \"libfoo\" for the target named \"foo\" and an\n"
610 " output prefix for the linker tool of \"lib\".\n"
611 "\n"
612 " Compiler tools have the notion of a single input and a single output,\n"
613 " along with a set of compiler-specific flags. The following expansions\n"
614 " are available:\n"
615 "\n"
616 " {{cflags}}\n"
617 " {{cflags_c}}\n"
618 " {{cflags_cc}}\n"
619 " {{cflags_objc}}\n"
620 " {{cflags_objcc}}\n"
621 " {{defines}}\n"
622 " {{include_dirs}}\n"
623 " Strings correspond that to the processed flags/defines/include\n"
624 " directories specified for the target.\n"
625 " Example: \"--enable-foo --enable-bar\"\n"
626 "\n"
627 " Defines will be prefixed by \"-D\" and include directories will\n"
628 " be prefixed by \"-I\" (these work with Posix tools as well as\n"
629 " Microsoft ones).\n"
630 "\n"
631 " {{source}}\n"
632 " The relative path and name of the current input file.\n"
633 " Example: \"../../base/my_file.cc\"\n"
634 "\n"
635 " {{source_file_part}}\n"
636 " The file part of the source including the extension (with no\n"
637 " directory information).\n"
638 " Example: \"foo.cc\"\n"
639 "\n"
640 " {{source_name_part}}\n"
641 " The filename part of the source file with no directory or\n"
642 " extension.\n"
643 " Example: \"foo\"\n"
644 "\n"
645 " {{source_gen_dir}}\n"
646 " {{source_out_dir}}\n"
647 " The directory in the generated file and output directories,\n"
648 " respectively, for the current input file. If the source file\n"
649 " is in the same directory as the target is declared in, they will\n"
650 " will be the same as the \"target\" versions above.\n"
651 " Example: \"gen/base/test\"\n"
652 "\n"
653 " Linker tools have multiple inputs and (potentially) multiple outputs\n"
654 " The following expansions are available:\n"
655 "\n"
656 " {{inputs}}\n"
657 " {{inputs_newline}}\n"
658 " Expands to the inputs to the link step. This will be a list of\n"
659 " object files and static libraries.\n"
660 " Example: \"obj/foo.o obj/bar.o obj/somelibrary.a\"\n"
661 "\n"
662 " The \"_newline\" version will separate the input files with\n"
663 " newlines instead of spaces. This is useful in response files:\n"
664 " some linkers can take a \"-filelist\" flag which expects newline\n"
665 " separated files, and some Microsoft tools have a fixed-sized\n"
666 " buffer for parsing each line of a response file.\n"
667 "\n"
668 " {{ldflags}}\n"
669 " Expands to the processed set of ldflags and library search paths\n"
670 " specified for the target.\n"
671 " Example: \"-m64 -fPIC -pthread -L/usr/local/mylib\"\n"
672 "\n"
673 " {{libs}}\n"
674 " Expands to the list of system libraries to link to. Each will\n"
675 " be prefixed by the \"lib_prefix\".\n"
676 "\n"
677 " As a special case to support Mac, libraries with names ending in\n"
678 " \".framework\" will be added to the {{libs}} with \"-framework\"\n"
679 " preceeding it, and the lib prefix will be ignored.\n"
680 "\n"
681 " Example: \"-lfoo -lbar\"\n"
682 "\n"
683 " {{output_extension}}\n"
684 " The value of the \"output_extension\" variable in the target,\n"
685 " or the value of the \"default_output_extension\" value in the\n"
686 " tool if the target does not specify an output extension.\n"
687 " Example: \".so\"\n"
688 "\n"
689 " {{solibs}}\n"
690 " Extra libraries from shared library dependencide not specified\n"
691 " in the {{inputs}}. This is the list of link_output files from\n"
692 " shared libraries (if the solink tool specifies a \"link_output\"\n"
693 " variable separate from the \"depend_output\").\n"
694 "\n"
695 " These should generally be treated the same as libs by your tool.\n"
696 " Example: \"libfoo.so libbar.so\"\n"
697 "\n"
698 " The copy tool allows the common compiler/linker substitutions, plus\n"
699 " {{source}} which is the source of the copy. The stamp tool allows\n"
700 " only the common tool substitutions.\n"
701 "\n"
702 "Separate linking and dependencies for shared libraries\n"
703 "\n"
704 " Shared libraries are special in that not all changes to them require\n"
705 " that dependent targets be re-linked. If the shared library is changed\n"
706 " but no imports or exports are different, dependent code needn't be\n"
707 " relinked, which can speed up the build.\n"
708 "\n"
709 " If your link step can output a list of exports from a shared library\n"
710 " and writes the file only if the new one is different, the timestamp of\n"
711 " this file can be used for triggering re-links, while the actual shared\n"
712 " library would be used for linking.\n"
713 "\n"
714 " You will need to specify\n"
715 " restat = true\n"
716 " in the linker tool to make this work, so Ninja will detect if the\n"
717 " timestamp of the dependency file has changed after linking (otherwise\n"
718 " it will always assume that running a command updates the output):\n"
719 "\n"
720 " tool(\"solink\") {\n"
721 " command = \"...\"\n"
722 " outputs = [\n"
723 " \"{{root_out_dir}}/{{target_output_name}}{{output_extension}}\",\n"
724 " \"{{root_out_dir}}/{{target_output_name}}"
725 "{{output_extension}}.TOC\",\n"
726 " ]\n"
727 " link_output =\n"
728 " \"{{root_out_dir}}/{{target_output_name}}{{output_extension}}\"\n"
729 " depend_output =\n"
730 " \"{{root_out_dir}}/{{target_output_name}}"
731 "{{output_extension}}.TOC\"\n"
732 " restat = true\n"
733 " }\n"
734 "\n"
735 "Example\n"
736 "\n"
737 " toolchain(\"my_toolchain\") {\n"
738 " # Put these at the top to apply to all tools below.\n"
739 " lib_prefix = \"-l\"\n"
740 " lib_dir_prefix = \"-L\"\n"
741 "\n"
742 " tool(\"cc\") {\n"
743 " command = \"gcc {{source}} -o {{output}}\"\n"
744 " outputs = [ \"{{source_out_dir}}/{{source_name_part}}.o\" ]\n"
745 " description = \"GCC {{source}}\"\n"
746 " }\n"
747 " tool(\"cxx\") {\n"
748 " command = \"g++ {{source}} -o {{output}}\"\n"
749 " outputs = [ \"{{source_out_dir}}/{{source_name_part}}.o\" ]\n"
750 " description = \"G++ {{source}}\"\n"
751 " }\n"
752 " }\n";
754 Value RunTool(Scope* scope,
755 const FunctionCallNode* function,
756 const std::vector<Value>& args,
757 BlockNode* block,
758 Err* err) {
759 // Find the toolchain definition we're executing inside of. The toolchain
760 // function will set a property pointing to it that we'll pick up.
761 Toolchain* toolchain = reinterpret_cast<Toolchain*>(
762 scope->GetProperty(&kToolchainPropertyKey, nullptr));
763 if (!toolchain) {
764 *err = Err(function->function(), "tool() called outside of toolchain().",
765 "The tool() function can only be used inside a toolchain() "
766 "definition.");
767 return Value();
770 if (!EnsureSingleStringArg(function, args, err))
771 return Value();
772 const std::string& tool_name = args[0].string_value();
773 Toolchain::ToolType tool_type = Toolchain::ToolNameToType(tool_name);
774 if (tool_type == Toolchain::TYPE_NONE) {
775 *err = Err(args[0], "Unknown tool type");
776 return Value();
779 // Run the tool block.
780 Scope block_scope(scope);
781 block->Execute(&block_scope, err);
782 if (err->has_error())
783 return Value();
785 // Figure out which validator to use for the substitution pattern for this
786 // tool type. There are different validators for the "outputs" than for the
787 // rest of the strings.
788 bool (*subst_validator)(SubstitutionType) = nullptr;
789 bool (*subst_output_validator)(SubstitutionType) = nullptr;
790 if (IsCompilerTool(tool_type)) {
791 subst_validator = &IsValidCompilerSubstitution;
792 subst_output_validator = &IsValidCompilerOutputsSubstitution;
793 } else if (IsLinkerTool(tool_type)) {
794 subst_validator = &IsValidLinkerSubstitution;
795 subst_output_validator = &IsValidLinkerOutputsSubstitution;
796 } else if (tool_type == Toolchain::TYPE_COPY) {
797 subst_validator = &IsValidCopySubstitution;
798 subst_output_validator = &IsValidCopySubstitution;
799 } else {
800 subst_validator = &IsValidToolSubstutition;
801 subst_output_validator = &IsValidToolSubstutition;
804 scoped_ptr<Tool> tool(new Tool);
806 if (!ReadPattern(&block_scope, "command", subst_validator, tool.get(),
807 &Tool::set_command, err) ||
808 !ReadOutputExtension(&block_scope, tool.get(), err) ||
809 !ReadPattern(&block_scope, "depfile", subst_validator, tool.get(),
810 &Tool::set_depfile, err) ||
811 !ReadDepsFormat(&block_scope, tool.get(), err) ||
812 !ReadPattern(&block_scope, "description", subst_validator, tool.get(),
813 &Tool::set_description, err) ||
814 !ReadString(&block_scope, "lib_switch", tool.get(),
815 &Tool::set_lib_switch, err) ||
816 !ReadString(&block_scope, "lib_dir_switch", tool.get(),
817 &Tool::set_lib_dir_switch, err) ||
818 !ReadPattern(&block_scope, "link_output", subst_validator, tool.get(),
819 &Tool::set_link_output, err) ||
820 !ReadPattern(&block_scope, "depend_output", subst_validator, tool.get(),
821 &Tool::set_depend_output, err) ||
822 !ReadString(&block_scope, "output_prefix", tool.get(),
823 &Tool::set_output_prefix, err) ||
824 !ReadPrecompiledHeaderType(&block_scope, tool.get(), err) ||
825 !ReadBool(&block_scope, "restat", tool.get(), &Tool::set_restat, err) ||
826 !ReadPattern(&block_scope, "rspfile", subst_validator, tool.get(),
827 &Tool::set_rspfile, err) ||
828 !ReadPattern(&block_scope, "rspfile_content", subst_validator, tool.get(),
829 &Tool::set_rspfile_content, err)) {
830 return Value();
833 if (tool_type != Toolchain::TYPE_COPY && tool_type != Toolchain::TYPE_STAMP) {
834 // All tools except the copy and stamp tools should have outputs. The copy
835 // and stamp tool's outputs are generated internally.
836 if (!ReadOutputs(&block_scope, function, subst_output_validator,
837 tool.get(), err))
838 return Value();
841 // Validate that the link_output and depend_output refer to items in the
842 // outputs and aren't defined for irrelevant tool types.
843 if (!tool->link_output().empty()) {
844 if (tool_type != Toolchain::TYPE_SOLINK) {
845 *err = Err(function, "This tool specifies a link_output.",
846 "This is only valid for solink tools.");
847 return Value();
849 if (!IsPatternInOutputList(tool->outputs(), tool->link_output())) {
850 *err = Err(function, "This tool's link_output is bad.",
851 "It must match one of the outputs.");
852 return Value();
855 if (!tool->depend_output().empty()) {
856 if (tool_type != Toolchain::TYPE_SOLINK) {
857 *err = Err(function, "This tool specifies a depend_output.",
858 "This is only valid for solink tools.");
859 return Value();
861 if (!IsPatternInOutputList(tool->outputs(), tool->depend_output())) {
862 *err = Err(function, "This tool's depend_output is bad.",
863 "It must match one of the outputs.");
864 return Value();
867 if ((!tool->link_output().empty() && tool->depend_output().empty()) ||
868 (tool->link_output().empty() && !tool->depend_output().empty())) {
869 *err = Err(function, "Both link_output and depend_output should either "
870 "be specified or they should both be empty.");
871 return Value();
874 // Make sure there weren't any vars set in this tool that were unused.
875 if (!block_scope.CheckForUnusedVars(err))
876 return Value();
878 toolchain->SetTool(tool_type, tool.Pass());
879 return Value();
882 // toolchain_args --------------------------------------------------------------
884 extern const char kToolchainArgs[] = "toolchain_args";
885 extern const char kToolchainArgs_HelpShort[] =
886 "toolchain_args: Set build arguments for toolchain build setup.";
887 extern const char kToolchainArgs_Help[] =
888 "toolchain_args: Set build arguments for toolchain build setup.\n"
889 "\n"
890 " Used inside a toolchain definition to pass arguments to an alternate\n"
891 " toolchain's invocation of the build.\n"
892 "\n"
893 " When you specify a target using an alternate toolchain, the master\n"
894 " build configuration file is re-interpreted in the context of that\n"
895 " toolchain (see \"gn help toolchain\"). The toolchain_args function\n"
896 " allows you to control the arguments passed into this alternate\n"
897 " invocation of the build.\n"
898 "\n"
899 " Any default system arguments or arguments passed in on the command-\n"
900 " line will also be passed to the alternate invocation unless explicitly\n"
901 " overridden by toolchain_args.\n"
902 "\n"
903 " The toolchain_args will be ignored when the toolchain being defined\n"
904 " is the default. In this case, it's expected you want the default\n"
905 " argument values.\n"
906 "\n"
907 " See also \"gn help buildargs\" for an overview of these arguments.\n"
908 "\n"
909 "Example:\n"
910 " toolchain(\"my_weird_toolchain\") {\n"
911 " ...\n"
912 " toolchain_args() {\n"
913 " # Override the system values for a generic Posix system.\n"
914 " is_win = false\n"
915 " is_posix = true\n"
916 "\n"
917 " # Pass this new value for specific setup for my toolchain.\n"
918 " is_my_weird_system = true\n"
919 " }\n"
920 " }\n";
922 Value RunToolchainArgs(Scope* scope,
923 const FunctionCallNode* function,
924 const std::vector<Value>& args,
925 BlockNode* block,
926 Err* err) {
927 // Find the toolchain definition we're executing inside of. The toolchain
928 // function will set a property pointing to it that we'll pick up.
929 Toolchain* toolchain = reinterpret_cast<Toolchain*>(
930 scope->GetProperty(&kToolchainPropertyKey, nullptr));
931 if (!toolchain) {
932 *err = Err(function->function(),
933 "toolchain_args() called outside of toolchain().",
934 "The toolchain_args() function can only be used inside a "
935 "toolchain() definition.");
936 return Value();
939 if (!args.empty()) {
940 *err = Err(function->function(), "This function takes no arguments.");
941 return Value();
944 // This function makes a new scope with various variable sets on it, which
945 // we then save on the toolchain to use when re-invoking the build.
946 Scope block_scope(scope);
947 block->Execute(&block_scope, err);
948 if (err->has_error())
949 return Value();
951 Scope::KeyValueMap values;
952 block_scope.GetCurrentScopeValues(&values);
953 toolchain->args() = values;
955 return Value();
958 } // namespace functions