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.
4
5 #include <algorithm>
6 #include <limits>
7 #include <memory>
8 #include <utility>
9
10 #include "gn/c_tool.h"
11 #include "gn/err.h"
12 #include "gn/functions.h"
13 #include "gn/general_tool.h"
14 #include "gn/label.h"
15 #include "gn/label_ptr.h"
16 #include "gn/parse_tree.h"
17 #include "gn/scheduler.h"
18 #include "gn/scope.h"
19 #include "gn/settings.h"
20 #include "gn/tool.h"
21 #include "gn/toolchain.h"
22 #include "gn/value_extractors.h"
23 #include "gn/variables.h"
24
25 namespace functions {
26
27 namespace {
28
29 // This is just a unique value to take the address of to use as the key for
30 // the toolchain property on a scope.
31 const int kToolchainPropertyKey = 0;
32
33 } // namespace
34
35 // toolchain -------------------------------------------------------------------
36
37 const char kToolchain[] = "toolchain";
38 const char kToolchain_HelpShort[] = "toolchain: Defines a toolchain.";
39 const char kToolchain_Help[] =
40 R"*(toolchain: Defines a toolchain.
41
42 A toolchain is a set of commands and build flags used to compile the source
43 code. The toolchain() function defines these commands.
44
45 Toolchain overview
46
47 You can have more than one toolchain in use at once in a build and a target
48 can exist simultaneously in multiple toolchains. A build file is executed
49 once for each toolchain it is referenced in so the GN code can vary all
50 parameters of each target (or which targets exist) on a per-toolchain basis.
51
52 When you have a simple build with only one toolchain, the build config file
53 is loaded only once at the beginning of the build. It must call
54 set_default_toolchain() (see "gn help set_default_toolchain") to tell GN the
55 label of the toolchain definition to use. The "toolchain_args" section of the
56 toolchain definition is ignored.
57
58 When a target has a dependency on a target using different toolchain (see "gn
59 help labels" for how to specify this), GN will start a build using that
60 secondary toolchain to resolve the target. GN will load the build config file
61 with the build arguments overridden as specified in the toolchain_args.
62 Because the default toolchain is already known, calls to
63 set_default_toolchain() are ignored.
64
65 To load a file in an alternate toolchain, GN does the following:
66
67 1. Loads the file with the toolchain definition in it (as determined by the
68 toolchain label).
69 2. Re-runs the master build configuration file, applying the arguments
70 specified by the toolchain_args section of the toolchain definition.
71 3. Loads the destination build file in the context of the configuration file
72 in the previous step.
73
74 The toolchain configuration is two-way. In the default toolchain (i.e. the
75 main build target) the configuration flows from the build config file to the
76 toolchain. The build config file looks at the state of the build (OS type,
77 CPU architecture, etc.) and decides which toolchain to use (via
78 set_default_toolchain()). In secondary toolchains, the configuration flows
79 from the toolchain to the build config file: the "toolchain_args" in the
80 toolchain definition specifies the arguments to re-invoke the build.
81
82 Functions and variables
83
84 tool()
85 The tool() function call specifies the commands to run for a given step. See
86 "gn help tool".
87
88 toolchain_args [scope]
89 Overrides for build arguments to pass to the toolchain when invoking it.
90 This is a variable of type "scope" where the variable names correspond to
91 variables in declare_args() blocks.
92
93 When you specify a target using an alternate toolchain, the master build
94 configuration file is re-interpreted in the context of that toolchain.
95 toolchain_args allows you to control the arguments passed into this
96 alternate invocation of the build.
97
98 Any default system arguments or arguments passed in via "gn args" will also
99 be passed to the alternate invocation unless explicitly overridden by
100 toolchain_args.
101
102 The toolchain_args will be ignored when the toolchain being defined is the
103 default. In this case, it's expected you want the default argument values.
104
105 See also "gn help buildargs" for an overview of these arguments.
106
107 propagates_configs [boolean, default=false]
108 Determines whether public_configs and all_dependent_configs in this
109 toolchain propagate to targets in other toolchains.
110
111 When false (the default), this toolchain will not propagate any configs to
112 targets in other toolchains that depend on it targets inside this
113 toolchain. This matches the most common usage of toolchains where they
114 represent different architectures or compilers and the settings that apply
115 to one won't necessarily apply to others.
116
117 When true, configs (public and all-dependent) will cross the boundary out
118 of this toolchain as if the toolchain boundary wasn't there. This only
119 affects one direction of dependencies: a toolchain can't control whether
120 it accepts such configs, only whether it pushes them. The build is
121 responsible for ensuring that any external targets depending on targets in
122 this toolchain are compatible with the compiler flags, etc. that may be
123 propagated.
124
125 deps [string list]
126 Dependencies of this toolchain. These dependencies will be resolved before
127 any target in the toolchain is compiled. To avoid circular dependencies
128 these must be targets defined in another toolchain.
129
130 This is expressed as a list of targets, and generally these targets will
131 always specify a toolchain:
132 deps = [ "//foo/bar:baz(//build/toolchain:bootstrap)" ]
133
134 This concept is somewhat inefficient to express in Ninja (it requires a lot
135 of duplicate of rules) so should only be used when absolutely necessary.
136
137 Example of defining a toolchain
138
139 toolchain("32") {
140 tool("cc") {
141 command = "gcc {{source}}"
142 ...
143 }
144
145 toolchain_args = {
146 use_doom_melon = true # Doom melon always required for 32-bit builds.
147 current_cpu = "x86"
148 }
149 }
150
151 toolchain("64") {
152 tool("cc") {
153 command = "gcc {{source}}"
154 ...
155 }
156
157 toolchain_args = {
158 # use_doom_melon is not overridden here, it will take the default.
159 current_cpu = "x64"
160 }
161 }
162
163 Example of cross-toolchain dependencies
164
165 If a 64-bit target wants to depend on a 32-bit binary, it would specify a
166 dependency using data_deps (data deps are like deps that are only needed at
167 runtime and aren't linked, since you can't link a 32-bit and a 64-bit
168 library).
169
170 executable("my_program") {
171 ...
172 if (target_cpu == "x64") {
173 # The 64-bit build needs this 32-bit helper.
174 data_deps = [ ":helper(//toolchains:32)" ]
175 }
176 }
177
178 if (target_cpu == "x86") {
179 # Our helper library is only compiled in 32-bits.
180 shared_library("helper") {
181 ...
182 }
183 }
184 )*";
185
RunToolchain(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,BlockNode * block,Err * err)186 Value RunToolchain(Scope* scope,
187 const FunctionCallNode* function,
188 const std::vector<Value>& args,
189 BlockNode* block,
190 Err* err) {
191 NonNestableBlock non_nestable(scope, function, "toolchain");
192 if (!non_nestable.Enter(err))
193 return Value();
194
195 if (!EnsureNotProcessingImport(function, scope, err) ||
196 !EnsureNotProcessingBuildConfig(function, scope, err))
197 return Value();
198
199 // Note that we don't want to use MakeLabelForScope since that will include
200 // the toolchain name in the label, and toolchain labels don't themselves
201 // have toolchain names.
202 const SourceDir& input_dir = scope->GetSourceDir();
203 Label label(input_dir, args[0].string_value());
204 if (g_scheduler->verbose_logging())
205 g_scheduler->Log("Defining toolchain", label.GetUserVisibleName(false));
206
207 // This object will actually be copied into the one owned by the toolchain
208 // manager, but that has to be done in the lock.
209 std::unique_ptr<Toolchain> toolchain = std::make_unique<Toolchain>(
210 scope->settings(), label, scope->build_dependency_files());
211 toolchain->set_defined_from(function);
212 toolchain->visibility().SetPublic();
213
214 Scope block_scope(scope);
215 block_scope.SetProperty(&kToolchainPropertyKey, toolchain.get());
216 block->Execute(&block_scope, err);
217 block_scope.SetProperty(&kToolchainPropertyKey, nullptr);
218 if (err->has_error())
219 return Value();
220
221 // Read deps (if any).
222 const Value* deps_value = block_scope.GetValue(variables::kDeps, true);
223 if (deps_value) {
224 ExtractListOfLabels(scope->settings()->build_settings(), *deps_value,
225 block_scope.GetSourceDir(),
226 ToolchainLabelForScope(&block_scope),
227 &toolchain->deps(), err);
228 if (err->has_error())
229 return Value();
230 }
231
232 // Read toolchain args (if any).
233 const Value* toolchain_args = block_scope.GetValue("toolchain_args", true);
234 if (toolchain_args) {
235 if (!toolchain_args->VerifyTypeIs(Value::SCOPE, err))
236 return Value();
237
238 Scope::KeyValueMap values;
239 toolchain_args->scope_value()->GetCurrentScopeValues(&values);
240 toolchain->args() = values;
241 }
242
243 // Read propagates_configs (if present).
244 const Value* propagates_configs =
245 block_scope.GetValue("propagates_configs", true);
246 if (propagates_configs) {
247 if (!propagates_configs->VerifyTypeIs(Value::BOOLEAN, err))
248 return Value();
249 toolchain->set_propagates_configs(propagates_configs->boolean_value());
250 }
251
252 if (!block_scope.CheckForUnusedVars(err))
253 return Value();
254
255 // Save this toolchain.
256 toolchain->ToolchainSetupComplete();
257 Scope::ItemVector* collector = scope->GetItemCollector();
258 if (!collector) {
259 *err = Err(function, "Can't define a toolchain in this context.");
260 return Value();
261 }
262 collector->push_back(std::move(toolchain));
263 return Value();
264 }
265
266 // tool ------------------------------------------------------------------------
267
268 const char kTool[] = "tool";
269 const char kTool_HelpShort[] = "tool: Specify arguments to a toolchain tool.";
270 const char kTool_Help[] =
271 R"(tool: Specify arguments to a toolchain tool.
272
273 Usage
274
275 tool(<tool type>) {
276 <tool variables...>
277 }
278
279 Tool types
280
281 Compiler tools:
282 "cc": C compiler
283 "cxx": C++ compiler
284 "objc": Objective C compiler
285 "objcxx": Objective C++ compiler
286 "rc": Resource compiler (Windows .rc files)
287 "asm": Assembler
288
289 Linker tools:
290 "alink": Linker for static libraries (archives)
291 "solink": Linker for shared libraries
292 "link": Linker for executables
293
294 Other tools:
295 "stamp": Tool for creating stamp files
296 "copy": Tool to copy files.
297 "action": Defaults for actions
298
299 Platform specific tools:
300 "copy_bundle_data": [iOS, macOS] Tool to copy files in a bundle.
301 "compile_xcassets": [iOS, macOS] Tool to compile asset catalogs.
302
303 Rust tools:
304 "rust_bin": Tool for compiling Rust binaries
305 "rust_cdylib": Tool for compiling C-compatible dynamic libraries.
306 "rust_dylib": Tool for compiling Rust dynamic libraries.
307 "rust_macro": Tool for compiling Rust procedural macros.
308 "rust_rlib": Tool for compiling Rust libraries.
309 "rust_staticlib": Tool for compiling Rust static libraries.
310
311 Tool variables
312
313 command [string with substitutions]
314 Valid for: all tools except "action" (required)
315
316 The command to run.
317
318 command_launcher [string]
319 Valid for: all tools except "action" (optional)
320
321 The prefix with which to launch the command (e.g. the path to a Goma or
322 CCache compiler launcher).
323
324 Note that this prefix will not be included in the compilation database or
325 IDE files generated from the build.
326
327 default_output_dir [string with substitutions]
328 Valid for: linker tools
329
330 Default directory name for the output file relative to the
331 root_build_dir. It can contain other substitution patterns. This will
332 be the default value for the {{output_dir}} expansion (discussed below)
333 but will be overridden by the "output_dir" variable in a target, if one
334 is specified.
335
336 GN doesn't do anything with this string other than pass it along,
337 potentially with target-specific overrides. It is the tool's job to use
338 the expansion so that the files will be in the right place.
339
340 default_output_extension [string]
341 Valid for: linker tools
342
343 Extension for the main output of a linkable tool. It includes the
344 leading dot. This will be the default value for the
345 {{output_extension}} expansion (discussed below) but will be overridden
346 by by the "output extension" variable in a target, if one is specified.
347 Empty string means no extension.
348
349 GN doesn't actually do anything with this extension other than pass it
350 along, potentially with target-specific overrides. One would typically
351 use the {{output_extension}} value in the "outputs" to read this value.
352
353 Example: default_output_extension = ".exe"
354
355 depfile [string with substitutions]
356 Valid for: compiler tools (optional)
357
358 If the tool can write ".d" files, this specifies the name of the
359 resulting file. These files are used to list header file dependencies
360 (or other implicit input dependencies) that are discovered at build
361 time. See also "depsformat".
362
363 Example: depfile = "{{output}}.d"
364
365 depsformat [string]
366 Valid for: compiler tools (when depfile is specified)
367
368 Format for the deps outputs. This is either "gcc" or "msvc". See the
369 ninja documentation for "deps" for more information.
370
371 Example: depsformat = "gcc"
372
373 description [string with substitutions, optional]
374 Valid for: all tools
375
376 What to print when the command is run.
377
378 Example: description = "Compiling {{source}}"
379
380 exe_output_extension [string, optional, rust tools only]
381 rlib_output_extension [string, optional, rust tools only]
382 dylib_output_extension [string, optional, rust tools only]
383 cdylib_output_extension [string, optional, rust tools only]
384 rust_proc_macro_output_extension [string, optional, rust tools only]
385 Valid for: Rust tools
386
387 These specify the default tool output for each of the crate types.
388 The default is empty for executables, shared, and static libraries and
389 ".rlib" for rlibs. Note that the Rust compiler complains with an error
390 if external crates do not take the form `lib<name>.rlib` or
391 `lib<name>.<shared_extension>`, where `<shared_extension>` is `.so`,
392 `.dylib`, or `.dll` as appropriate for the platform.
393
394 lib_switch [string, optional, link tools only]
395 lib_dir_switch [string, optional, link tools only]
396 Valid for: Linker tools except "alink"
397
398 These strings will be prepended to the libraries and library search
399 directories, respectively, because linkers differ on how to specify
400 them.
401
402 If you specified:
403 lib_switch = "-l"
404 lib_dir_switch = "-L"
405 then the "{{libs}}" expansion for
406 [ "freetype", "expat" ]
407 would be
408 "-lfreetype -lexpat".
409
410 framework_switch [string, optional, link tools only]
411 framework_dir_switch [string, optional, link tools only]
412 Valid for: Linker tools
413
414 These strings will be prepended to the frameworks and framework search
415 path directories, respectively, because linkers differ on how to specify
416 them.
417
418 If you specified:
419 framework_switch = "-framework "
420 framework_dir_switch = "-F"
421 then the "{{libs}}" expansion for
422 [ "UIKit.framework", "$root_out_dir/Foo.framework" ]
423 would be
424 "-framework UIKit -F. -framework Foo"
425
426 outputs [list of strings with substitutions]
427 Valid for: Linker and compiler tools (required)
428
429 An array of names for the output files the tool produces. These are
430 relative to the build output directory. There must always be at least
431 one output file. There can be more than one output (a linker might
432 produce a library and an import library, for example).
433
434 This array just declares to GN what files the tool will produce. It is
435 your responsibility to specify the tool command that actually produces
436 these files.
437
438 If you specify more than one output for shared library links, you
439 should consider setting link_output, depend_output, and
440 runtime_outputs.
441
442 Example for a compiler tool that produces .obj files:
443 outputs = [
444 "{{source_out_dir}}/{{source_name_part}}.obj"
445 ]
446
447 Example for a linker tool that produces a .dll and a .lib. The use of
448 {{target_output_name}}, {{output_extension}} and {{output_dir}} allows
449 the target to override these values.
450 outputs = [
451 "{{output_dir}}/{{target_output_name}}"
452 "{{output_extension}}",
453 "{{output_dir}}/{{target_output_name}}.lib",
454 ]
455
456 pool [label, optional]
457 Valid for: all tools (optional)
458
459 Label of the pool to use for the tool. Pools are used to limit the
460 number of tasks that can execute concurrently during the build.
461
462 See also "gn help pool".
463
464 link_output [string with substitutions]
465 depend_output [string with substitutions]
466 Valid for: "solink" only (optional)
467
468 These two files specify which of the outputs from the solink tool
469 should be used for linking and dependency tracking. These should match
470 entries in the "outputs". If unspecified, the first item in the
471 "outputs" array will be used for all. See "Separate linking and
472 dependencies for shared libraries" below for more.
473
474 On Windows, where the tools produce a .dll shared library and a .lib
475 import library, you will want the first two to be the import library
476 and the third one to be the .dll file. On Linux, if you're not doing
477 the separate linking/dependency optimization, all of these should be
478 the .so output.
479
480 output_prefix [string]
481 Valid for: Linker tools (optional)
482
483 Prefix to use for the output name. Defaults to empty. This prefix will
484 be prepended to the name of the target (or the output_name if one is
485 manually specified for it) if the prefix is not already there. The
486 result will show up in the {{output_name}} substitution pattern.
487
488 Individual targets can opt-out of the output prefix by setting:
489 output_prefix_override = true
490 (see "gn help output_prefix_override").
491
492 This is typically used to prepend "lib" to libraries on
493 Posix systems:
494 output_prefix = "lib"
495
496 precompiled_header_type [string]
497 Valid for: "cc", "cxx", "objc", "objcxx"
498
499 Type of precompiled headers. If undefined or the empty string,
500 precompiled headers will not be used for this tool. Otherwise use "gcc"
501 or "msvc".
502
503 For precompiled headers to be used for a given target, the target (or a
504 config applied to it) must also specify a "precompiled_header" and, for
505 "msvc"-style headers, a "precompiled_source" value. If the type is
506 "gcc", then both "precompiled_header" and "precompiled_source" must
507 resolve to the same file, despite the different formats required for
508 each."
509
510 See "gn help precompiled_header" for more.
511
512 restat [boolean]
513 Valid for: all tools (optional, defaults to false)
514
515 Requests that Ninja check the file timestamp after this tool has run to
516 determine if anything changed. Set this if your tool has the ability to
517 skip writing output if the output file has not changed.
518
519 Normally, Ninja will assume that when a tool runs the output be new and
520 downstream dependents must be rebuild. When this is set to trye, Ninja
521 can skip rebuilding downstream dependents for input changes that don't
522 actually affect the output.
523
524 Example:
525 restat = true
526
527 rspfile [string with substitutions]
528 Valid for: all tools except "action" (optional)
529
530 Name of the response file. If empty, no response file will be
531 used. See "rspfile_content".
532
533 rspfile_content [string with substitutions]
534 Valid for: all tools except "action" (required when "rspfile" is used)
535
536 The contents to be written to the response file. This may include all
537 or part of the command to send to the tool which allows you to get
538 around OS command-line length limits.
539
540 This example adds the inputs and libraries to a response file, but
541 passes the linker flags directly on the command line:
542 tool("link") {
543 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
544 rspfile = "{{output}}.rsp"
545 rspfile_content = "{{inputs}} {{solibs}} {{libs}}"
546 }
547
548 runtime_outputs [string list with substitutions]
549 Valid for: linker tools
550
551 If specified, this list is the subset of the outputs that should be
552 added to runtime deps (see "gn help runtime_deps"). By default (if
553 runtime_outputs is empty or unspecified), it will be the link_output.
554
555 )" // String break to prevent overflowing the 16K max VC string length.
556 R"(Expansions for tool variables
557
558 All paths are relative to the root build directory, which is the current
559 directory for running all tools. These expansions are available to all tools:
560
561 {{label}}
562 The label of the current target. This is typically used in the
563 "description" field for link tools. The toolchain will be omitted from
564 the label for targets in the default toolchain, and will be included
565 for targets in other toolchains.
566
567 {{label_name}}
568 The short name of the label of the target. This is the part after the
569 colon. For "//foo/bar:baz" this will be "baz". Unlike
570 {{target_output_name}}, this is not affected by the "output_prefix" in
571 the tool or the "output_name" set on the target.
572
573 {{output}}
574 The relative path and name of the output(s) of the current build step.
575 If there is more than one output, this will expand to a list of all of
576 them. Example: "out/base/my_file.o"
577
578 {{target_gen_dir}}
579 {{target_out_dir}}
580 The directory of the generated file and output directories,
581 respectively, for the current target. There is no trailing slash. See
582 also {{output_dir}} for linker tools. Example: "out/base/test"
583
584 {{target_output_name}}
585 The short name of the current target with no path information, or the
586 value of the "output_name" variable if one is specified in the target.
587 This will include the "output_prefix" if any. See also {{label_name}}.
588
589 Example: "libfoo" for the target named "foo" and an output prefix for
590 the linker tool of "lib".
591
592 Compiler tools have the notion of a single input and a single output, along
593 with a set of compiler-specific flags. The following expansions are
594 available:
595
596 {{asmflags}}
597 {{cflags}}
598 {{cflags_c}}
599 {{cflags_cc}}
600 {{cflags_objc}}
601 {{cflags_objcc}}
602 {{defines}}
603 {{include_dirs}}
604 Strings correspond that to the processed flags/defines/include
605 directories specified for the target.
606 Example: "--enable-foo --enable-bar"
607
608 Defines will be prefixed by "-D" and include directories will be
609 prefixed by "-I" (these work with Posix tools as well as Microsoft
610 ones).
611
612 {{source}}
613 The relative path and name of the current input file.
614 Example: "../../base/my_file.cc"
615
616 {{source_file_part}}
617 The file part of the source including the extension (with no directory
618 information).
619 Example: "foo.cc"
620
621 {{source_name_part}}
622 The filename part of the source file with no directory or extension.
623 Example: "foo"
624
625 {{source_gen_dir}}
626 {{source_out_dir}}
627 The directory in the generated file and output directories,
628 respectively, for the current input file. If the source file is in the
629 same directory as the target is declared in, they will will be the same
630 as the "target" versions above. Example: "gen/base/test"
631
632 Linker tools have multiple inputs and (potentially) multiple outputs. The
633 static library tool ("alink") is not considered a linker tool. The following
634 expansions are available:
635
636 {{inputs}}
637 {{inputs_newline}}
638 Expands to the inputs to the link step. This will be a list of object
639 files and static libraries.
640 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
641
642 The "_newline" version will separate the input files with newlines
643 instead of spaces. This is useful in response files: some linkers can
644 take a "-filelist" flag which expects newline separated files, and some
645 Microsoft tools have a fixed-sized buffer for parsing each line of a
646 response file.
647
648 {{ldflags}}
649 Expands to the processed set of ldflags and library search paths
650 specified for the target.
651 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
652
653 {{libs}}
654 Expands to the list of system libraries to link to. Each will be
655 prefixed by the "lib_switch".
656
657 As a special case to support Mac, libraries with names ending in
658 ".framework" will be added to the {{libs}} with "-framework" preceding
659 it, and the lib prefix will be ignored.
660
661 Example: "-lfoo -lbar"
662
663 {{output_dir}}
664 The value of the "output_dir" variable in the target, or the the value
665 of the "default_output_dir" value in the tool if the target does not
666 override the output directory. This will be relative to the
667 root_build_dir and will not end in a slash. Will be "." for output to
668 the root_build_dir.
669
670 This is subtly different than {{target_out_dir}} which is defined by GN
671 based on the target's path and not overridable. {{output_dir}} is for
672 the final output, {{target_out_dir}} is generally for object files and
673 other outputs.
674
675 Usually {{output_dir}} would be defined in terms of either
676 {{target_out_dir}} or {{root_out_dir}}
677
678 {{output_extension}}
679 The value of the "output_extension" variable in the target, or the
680 value of the "default_output_extension" value in the tool if the target
681 does not specify an output extension.
682 Example: ".so"
683
684 {{solibs}}
685 Extra libraries from shared library dependencies not specified in the
686 {{inputs}}. This is the list of link_output files from shared libraries
687 (if the solink tool specifies a "link_output" variable separate from
688 the "depend_output").
689
690 These should generally be treated the same as libs by your tool.
691
692 Example: "libfoo.so libbar.so"
693
694 {{frameworks}}
695 Shared libraries packaged as framework bundle. This is principally
696 used on Apple's platforms (macOS and iOS). All name must be ending
697 with ".framework" suffix; the suffix will be stripped when expanding
698 {{frameworks}} and each item will be preceded by "-framework".
699
700 )" // String break to prevent overflowing the 16K max VC string length.
701 R"( The static library ("alink") tool allows {{arflags}} plus the common tool
702 substitutions.
703
704 The copy tool allows the common compiler/linker substitutions, plus
705 {{source}} which is the source of the copy. The stamp tool allows only the
706 common tool substitutions.
707
708 The copy_bundle_data and compile_xcassets tools only allows the common tool
709 substitutions. Both tools are required to create iOS/macOS bundles and need
710 only be defined on those platforms.
711
712 The copy_bundle_data tool will be called with one source and needs to copy
713 (optionally optimizing the data representation) to its output. It may be
714 called with a directory as input and it needs to be recursively copied.
715
716 The compile_xcassets tool will be called with one or more source (each an
717 asset catalog) that needs to be compiled to a single output. The following
718 substitutions are available:
719
720 {{inputs}}
721 Expands to the list of .xcassets to use as input to compile the asset
722 catalog.
723
724 {{bundle_product_type}}
725 Expands to the product_type of the bundle that will contain the
726 compiled asset catalog. Usually corresponds to the product_type
727 property of the corresponding create_bundle target.
728
729 {{bundle_partial_info_plist}}
730 Expands to the path to the partial Info.plist generated by the
731 assets catalog compiler. Usually based on the target_name of
732 the create_bundle target.
733
734 Rust tools have the notion of a single input and a single output, along
735 with a set of compiler-specific flags. The following expansions are
736 available:
737
738 {{crate_name}}
739 Expands to the string representing the crate name of target under
740 compilation.
741
742 {{crate_type}}
743 Expands to the string representing the type of crate for the target
744 under compilation.
745
746 {{externs}}
747 Expands to the list of --extern flags needed to include addition Rust
748 libraries in this target. Includes any specified renamed dependencies.
749
750 {{rustdeps}}
751 Expands to the list of -Ldependency=<path> strings needed to compile
752 this target.
753
754 {{rustenv}}
755 Expands to the list of environment variables.
756
757 {{rustflags}}
758 Expands to the list of strings representing Rust compiler flags.
759
760 Separate linking and dependencies for shared libraries
761
762 Shared libraries are special in that not all changes to them require that
763 dependent targets be re-linked. If the shared library is changed but no
764 imports or exports are different, dependent code needn't be relinked, which
765 can speed up the build.
766
767 If your link step can output a list of exports from a shared library and
768 writes the file only if the new one is different, the timestamp of this file
769 can be used for triggering re-links, while the actual shared library would be
770 used for linking.
771
772 You will need to specify
773 restat = true
774 in the linker tool to make this work, so Ninja will detect if the timestamp
775 of the dependency file has changed after linking (otherwise it will always
776 assume that running a command updates the output):
777
778 tool("solink") {
779 command = "..."
780 outputs = [
781 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
782 "{{output_dir}}/{{target_output_name}}"
783 "{{output_extension}}.TOC",
784 ]
785 link_output =
786 "{{output_dir}}/{{target_output_name}}{{output_extension}}"
787 depend_output =
788 "{{output_dir}}/{{target_output_name}}"
789 "{{output_extension}}.TOC"
790 restat = true
791 }
792
793 Example
794
795 toolchain("my_toolchain") {
796 # Put these at the top to apply to all tools below.
797 lib_switch = "-l"
798 lib_dir_switch = "-L"
799
800 tool("cc") {
801 command = "gcc {{source}} -o {{output}}"
802 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
803 description = "GCC {{source}}"
804 }
805 tool("cxx") {
806 command = "g++ {{source}} -o {{output}}"
807 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
808 description = "G++ {{source}}"
809 }
810 };
811 )";
812
RunTool(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,BlockNode * block,Err * err)813 Value RunTool(Scope* scope,
814 const FunctionCallNode* function,
815 const std::vector<Value>& args,
816 BlockNode* block,
817 Err* err) {
818 // Find the toolchain definition we're executing inside of. The toolchain
819 // function will set a property pointing to it that we'll pick up.
820 Toolchain* toolchain = reinterpret_cast<Toolchain*>(
821 scope->GetProperty(&kToolchainPropertyKey, nullptr));
822 if (!toolchain) {
823 *err = Err(function->function(), "tool() called outside of toolchain().",
824 "The tool() function can only be used inside a toolchain() "
825 "definition.");
826 return Value();
827 }
828
829 if (!EnsureSingleStringArg(function, args, err))
830 return Value();
831 const std::string& tool_name = args[0].string_value();
832
833 // Run the tool block.
834 Scope block_scope(scope);
835 block->Execute(&block_scope, err);
836 if (err->has_error())
837 return Value();
838
839 std::unique_ptr<Tool> tool =
840 Tool::CreateTool(function, tool_name, &block_scope, toolchain, err);
841
842 if (!tool) {
843 return Value();
844 }
845
846 tool->set_defined_from(function);
847 toolchain->SetTool(std::move(tool));
848
849 // Make sure there weren't any vars set in this tool that were unused.
850 if (!block_scope.CheckForUnusedVars(err))
851 return Value();
852
853 return Value();
854 }
855
856 } // namespace functions
857