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 if (!EnsureSingleStringArg(function, args, err))
200 return Value();
201
202 // Note that we don't want to use MakeLabelForScope since that will include
203 // the toolchain name in the label, and toolchain labels don't themselves
204 // have toolchain names.
205 const SourceDir& input_dir = scope->GetSourceDir();
206 Label label(input_dir, args[0].string_value());
207 if (g_scheduler->verbose_logging())
208 g_scheduler->Log("Defining toolchain", label.GetUserVisibleName(false));
209
210 // This object will actually be copied into the one owned by the toolchain
211 // manager, but that has to be done in the lock.
212 std::unique_ptr<Toolchain> toolchain = std::make_unique<Toolchain>(
213 scope->settings(), label, scope->build_dependency_files());
214 toolchain->set_defined_from(function);
215 toolchain->visibility().SetPublic();
216
217 Scope block_scope(scope);
218 block_scope.SetProperty(&kToolchainPropertyKey, toolchain.get());
219 block->Execute(&block_scope, err);
220 block_scope.SetProperty(&kToolchainPropertyKey, nullptr);
221 if (err->has_error())
222 return Value();
223
224 // Read deps (if any).
225 const Value* deps_value = block_scope.GetValue(variables::kDeps, true);
226 if (deps_value) {
227 ExtractListOfLabels(scope->settings()->build_settings(), *deps_value,
228 block_scope.GetSourceDir(),
229 ToolchainLabelForScope(&block_scope),
230 &toolchain->deps(), err);
231 if (err->has_error())
232 return Value();
233 }
234
235 // Read toolchain args (if any).
236 const Value* toolchain_args = block_scope.GetValue("toolchain_args", true);
237 if (toolchain_args) {
238 if (!toolchain_args->VerifyTypeIs(Value::SCOPE, err))
239 return Value();
240
241 Scope::KeyValueMap values;
242 toolchain_args->scope_value()->GetCurrentScopeValues(&values);
243 toolchain->args() = values;
244 }
245
246 // Read propagates_configs (if present).
247 const Value* propagates_configs =
248 block_scope.GetValue("propagates_configs", true);
249 if (propagates_configs) {
250 if (!propagates_configs->VerifyTypeIs(Value::BOOLEAN, err))
251 return Value();
252 toolchain->set_propagates_configs(propagates_configs->boolean_value());
253 }
254
255 if (!block_scope.CheckForUnusedVars(err))
256 return Value();
257
258 // Save this toolchain.
259 toolchain->ToolchainSetupComplete();
260 Scope::ItemVector* collector = scope->GetItemCollector();
261 if (!collector) {
262 *err = Err(function, "Can't define a toolchain in this context.");
263 return Value();
264 }
265 collector->push_back(std::move(toolchain));
266 return Value();
267 }
268
269 // tool ------------------------------------------------------------------------
270
271 const char kTool[] = "tool";
272 const char kTool_HelpShort[] = "tool: Specify arguments to a toolchain tool.";
273 const char kTool_Help[] =
274 R"(tool: Specify arguments to a toolchain tool.
275
276 Usage
277
278 tool(<tool type>) {
279 <tool variables...>
280 }
281
282 Tool types
283
284 Compiler tools:
285 "cc": C compiler
286 "cxx": C++ compiler
287 "cxx_module": C++ compiler used for Clang .modulemap files
288 "objc": Objective C compiler
289 "objcxx": Objective C++ compiler
290 "rc": Resource compiler (Windows .rc files)
291 "asm": Assembler
292 "swift": Swift compiler driver
293
294 Linker tools:
295 "alink": Linker for static libraries (archives)
296 "solink": Linker for shared libraries
297 "link": Linker for executables
298
299 Other tools:
300 "stamp": Tool for creating stamp files
301 "copy": Tool to copy files.
302 "action": Defaults for actions
303
304 Platform specific tools:
305 "copy_bundle_data": [iOS, macOS] Tool to copy files in a bundle.
306 "compile_xcassets": [iOS, macOS] Tool to compile asset catalogs.
307
308 Rust tools:
309 "rust_bin": Tool for compiling Rust binaries
310 "rust_cdylib": Tool for compiling C-compatible dynamic libraries.
311 "rust_dylib": Tool for compiling Rust dynamic libraries.
312 "rust_macro": Tool for compiling Rust procedural macros.
313 "rust_rlib": Tool for compiling Rust libraries.
314 "rust_staticlib": Tool for compiling Rust static libraries.
315
316 Tool variables
317
318 command [string with substitutions]
319 Valid for: all tools except "action" (required)
320
321 The command to run.
322
323 command_launcher [string]
324 Valid for: all tools except "action" (optional)
325
326 The prefix with which to launch the command (e.g. the path to a Goma or
327 CCache compiler launcher).
328
329 Note that this prefix will not be included in the compilation database or
330 IDE files generated from the build.
331
332 default_output_dir [string with substitutions]
333 Valid for: linker tools
334
335 Default directory name for the output file relative to the
336 root_build_dir. It can contain other substitution patterns. This will
337 be the default value for the {{output_dir}} expansion (discussed below)
338 but will be overridden by the "output_dir" variable in a target, if one
339 is specified.
340
341 GN doesn't do anything with this string other than pass it along,
342 potentially with target-specific overrides. It is the tool's job to use
343 the expansion so that the files will be in the right place.
344
345 default_output_extension [string]
346 Valid for: linker tools
347
348 Extension for the main output of a linkable tool. It includes the
349 leading dot. This will be the default value for the
350 {{output_extension}} expansion (discussed below) but will be overridden
351 by by the "output extension" variable in a target, if one is specified.
352 Empty string means no extension.
353
354 GN doesn't actually do anything with this extension other than pass it
355 along, potentially with target-specific overrides. One would typically
356 use the {{output_extension}} value in the "outputs" to read this value.
357
358 Example: default_output_extension = ".exe"
359
360 depfile [string with substitutions]
361 Valid for: compiler tools (optional)
362
363 If the tool can write ".d" files, this specifies the name of the
364 resulting file. These files are used to list header file dependencies
365 (or other implicit input dependencies) that are discovered at build
366 time. See also "depsformat".
367
368 Example: depfile = "{{output}}.d"
369
370 depsformat [string]
371 Valid for: compiler tools (when depfile is specified)
372
373 Format for the deps outputs. This is either "gcc" or "msvc". See the
374 ninja documentation for "deps" for more information.
375
376 Example: depsformat = "gcc"
377
378 description [string with substitutions, optional]
379 Valid for: all tools
380
381 What to print when the command is run.
382
383 Example: description = "Compiling {{source}}"
384
385 exe_output_extension [string, optional, rust tools only]
386 rlib_output_extension [string, optional, rust tools only]
387 dylib_output_extension [string, optional, rust tools only]
388 cdylib_output_extension [string, optional, rust tools only]
389 rust_proc_macro_output_extension [string, optional, rust tools only]
390 Valid for: Rust tools
391
392 These specify the default tool output for each of the crate types.
393 The default is empty for executables, shared, and static libraries and
394 ".rlib" for rlibs. Note that the Rust compiler complains with an error
395 if external crates do not take the form `lib<name>.rlib` or
396 `lib<name>.<shared_extension>`, where `<shared_extension>` is `.so`,
397 `.dylib`, or `.dll` as appropriate for the platform.
398
399 lib_switch [string, optional, link tools only]
400 lib_dir_switch [string, optional, link tools only]
401 Valid for: Linker tools except "alink"
402
403 These strings will be prepended to the libraries and library search
404 directories, respectively, because linkers differ on how to specify
405 them.
406
407 If you specified:
408 lib_switch = "-l"
409 lib_dir_switch = "-L"
410 then the "{{libs}}" expansion for
411 [ "freetype", "expat" ]
412 would be
413 "-lfreetype -lexpat".
414
415 framework_switch [string, optional, link tools only]
416 weak_framework_switch [string, optional, link tools only]
417 framework_dir_switch [string, optional, link tools only]
418 Valid for: Linker tools
419
420 These strings will be prepended to the frameworks and framework search
421 path directories, respectively, because linkers differ on how to specify
422 them.
423
424 If you specified:
425 framework_switch = "-framework "
426 weak_framework_switch = "-weak_framework "
427 framework_dir_switch = "-F"
428 and:
429 framework_dirs = [ "$root_out_dir" ]
430 frameworks = [ "UIKit.framework", "Foo.framework" ]
431 weak_frameworks = [ "MediaPlayer.framework" ]
432 would be:
433 "-F. -framework UIKit -framework Foo -weak_framework MediaPlayer"
434
435 swiftmodule_switch [string, optional, link tools only]
436 Valid for: Linker tools except "alink"
437
438 The string will be prependend to the path to the .swiftmodule files
439 that are embedded in the linker output.
440
441 If you specified:
442 swiftmodule_swift = "-Wl,-add_ast_path,"
443 then the "{{swiftmodules}}" expansion for
444 [ "obj/foo/Foo.swiftmodule" ]
445 would be
446 "-Wl,-add_ast_path,obj/foo/Foo.swiftmodule"
447
448 outputs [list of strings with substitutions]
449 Valid for: Linker and compiler tools (required)
450
451 An array of names for the output files the tool produces. These are
452 relative to the build output directory. There must always be at least
453 one output file. There can be more than one output (a linker might
454 produce a library and an import library, for example).
455
456 This array just declares to GN what files the tool will produce. It is
457 your responsibility to specify the tool command that actually produces
458 these files.
459
460 If you specify more than one output for shared library links, you
461 should consider setting link_output, depend_output, and
462 runtime_outputs.
463
464 Example for a compiler tool that produces .obj files:
465 outputs = [
466 "{{source_out_dir}}/{{source_name_part}}.obj"
467 ]
468
469 Example for a linker tool that produces a .dll and a .lib. The use of
470 {{target_output_name}}, {{output_extension}} and {{output_dir}} allows
471 the target to override these values.
472 outputs = [
473 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
474 "{{output_dir}}/{{target_output_name}}.lib",
475 ]
476
477 partial_outputs [list of strings with substitutions]
478 Valid for: "swift" only
479
480 An array of names for the partial outputs the tool produces. These
481 are relative to the build output directory. The expansion will be
482 evaluated for each file listed in the "sources" of the target.
483
484 This is used to deal with whole module optimization, allowing to
485 list one object file per source file when whole module optimization
486 is disabled.
487
488 pool [label, optional]
489 Valid for: all tools (optional)
490
491 Label of the pool to use for the tool. Pools are used to limit the
492 number of tasks that can execute concurrently during the build.
493
494 See also "gn help pool".
495
496 link_output [string with substitutions]
497 depend_output [string with substitutions]
498 Valid for: "solink" only (optional)
499
500 These two files specify which of the outputs from the solink tool
501 should be used for linking and dependency tracking. These should match
502 entries in the "outputs". If unspecified, the first item in the
503 "outputs" array will be used for all. See "Separate linking and
504 dependencies for shared libraries" below for more.
505
506 On Windows, where the tools produce a .dll shared library and a .lib
507 import library, you will want the first two to be the import library
508 and the third one to be the .dll file. On Linux, if you're not doing
509 the separate linking/dependency optimization, all of these should be
510 the .so output.
511
512 output_prefix [string]
513 Valid for: Linker tools (optional)
514
515 Prefix to use for the output name. Defaults to empty. This prefix will
516 be prepended to the name of the target (or the output_name if one is
517 manually specified for it) if the prefix is not already there. The
518 result will show up in the {{output_name}} substitution pattern.
519
520 Individual targets can opt-out of the output prefix by setting:
521 output_prefix_override = true
522 (see "gn help output_prefix_override").
523
524 This is typically used to prepend "lib" to libraries on
525 Posix systems:
526 output_prefix = "lib"
527
528 precompiled_header_type [string]
529 Valid for: "cc", "cxx", "objc", "objcxx"
530
531 Type of precompiled headers. If undefined or the empty string,
532 precompiled headers will not be used for this tool. Otherwise use "gcc"
533 or "msvc".
534
535 For precompiled headers to be used for a given target, the target (or a
536 config applied to it) must also specify a "precompiled_header" and, for
537 "msvc"-style headers, a "precompiled_source" value. If the type is
538 "gcc", then both "precompiled_header" and "precompiled_source" must
539 resolve to the same file, despite the different formats required for
540 each."
541
542 See "gn help precompiled_header" for more.
543
544 restat [boolean]
545 Valid for: all tools (optional, defaults to false)
546
547 Requests that Ninja check the file timestamp after this tool has run to
548 determine if anything changed. Set this if your tool has the ability to
549 skip writing output if the output file has not changed.
550
551 Normally, Ninja will assume that when a tool runs the output be new and
552 downstream dependents must be rebuild. When this is set to trye, Ninja
553 can skip rebuilding downstream dependents for input changes that don't
554 actually affect the output.
555
556 Example:
557 restat = true
558
559 rspfile [string with substitutions]
560 Valid for: all tools except "action" (optional)
561
562 Name of the response file. If empty, no response file will be
563 used. See "rspfile_content".
564
565 rspfile_content [string with substitutions]
566 Valid for: all tools except "action" (required when "rspfile" is used)
567
568 The contents to be written to the response file. This may include all
569 or part of the command to send to the tool which allows you to get
570 around OS command-line length limits.
571
572 This example adds the inputs and libraries to a response file, but
573 passes the linker flags directly on the command line:
574 tool("link") {
575 command = "link -o {{output}} {{ldflags}} @{{output}}.rsp"
576 rspfile = "{{output}}.rsp"
577 rspfile_content = "{{inputs}} {{solibs}} {{libs}} {{rlibs}}"
578 }
579
580 runtime_outputs [string list with substitutions]
581 Valid for: linker tools
582
583 If specified, this list is the subset of the outputs that should be
584 added to runtime deps (see "gn help runtime_deps"). By default (if
585 runtime_outputs is empty or unspecified), it will be the link_output.
586
587 rust_sysroot
588 Valid for: Rust tools
589
590 A path relative to root_out_dir. This is not used in the build
591 process, but may be used when generating metadata for rust-analyzer.
592 (See --export-rust-project). It enables such metadata to include
593 information about the Rust standard library.
594
595 dynamic_link_switch
596 Valid for: Rust tools which link
597
598 A switch to be optionally inserted into linker command lines
599 to indicate that subsequent items may be dynamically linked.
600 For ld-like linkers, -Clink-arg=-Bdynamic may be a good choice.
601 This switch is inserted by gn into rustc command lines before
602 listing any non-Rust dependencies. This may be necessary because
603 sometimes rustc puts the linker into a mode where it would otherwise
604 link against static libraries by default. This flag will be
605 inserted into the {{rustdeps}} variable at the appropriate place;
606 {{ldflags}} can't be used for the same purpose because the flags
607 may not be inserted at the desired place in the command line.
608
609 )" // String break to prevent overflowing the 16K max VC string length.
610 R"(Expansions for tool variables
611
612 All paths are relative to the root build directory, which is the current
613 directory for running all tools. These expansions are available to all tools:
614
615 {{label}}
616 The label of the current target. This is typically used in the
617 "description" field for link tools. The toolchain will be omitted from
618 the label for targets in the default toolchain, and will be included
619 for targets in other toolchains.
620
621 {{label_name}}
622 The short name of the label of the target. This is the part after the
623 colon. For "//foo/bar:baz" this will be "baz". Unlike
624 {{target_output_name}}, this is not affected by the "output_prefix" in
625 the tool or the "output_name" set on the target.
626
627 {{label_no_toolchain}}
628 The label of the current target, never including the toolchain
629 (otherwise, this is identical to {{label}}). This is used as the module
630 name when using .modulemap files.
631
632 {{output}}
633 The relative path and name of the output(s) of the current build step.
634 If there is more than one output, this will expand to a list of all of
635 them. Example: "out/base/my_file.o"
636
637 {{target_gen_dir}}
638 {{target_out_dir}}
639 The directory of the generated file and output directories,
640 respectively, for the current target. There is no trailing slash. See
641 also {{output_dir}} for linker tools. Example: "out/base/test"
642
643 {{target_output_name}}
644 The short name of the current target with no path information, or the
645 value of the "output_name" variable if one is specified in the target.
646 This will include the "output_prefix" if any. See also {{label_name}}.
647
648 Example: "libfoo" for the target named "foo" and an output prefix for
649 the linker tool of "lib".
650
651 Compiler tools have the notion of a single input and a single output, along
652 with a set of compiler-specific flags. The following expansions are
653 available:
654
655 {{asmflags}}
656 {{cflags}}
657 {{cflags_c}}
658 {{cflags_cc}}
659 {{cflags_objc}}
660 {{cflags_objcc}}
661 {{defines}}
662 {{include_dirs}}
663 Strings correspond that to the processed flags/defines/include
664 directories specified for the target.
665 Example: "--enable-foo --enable-bar"
666
667 Defines will be prefixed by "-D" and include directories will be
668 prefixed by "-I" (these work with Posix tools as well as Microsoft
669 ones).
670
671 {{module_deps}}
672 {{module_deps_no_self}}
673 Strings that correspond to the flags necessary to depend upon the Clang
674 modules referenced by the current target. The "_no_self" version doesn't
675 include the module for the current target, and can be used to compile
676 the pcm itself.
677
678 {{source}}
679 The relative path and name of the current input file.
680 Example: "../../base/my_file.cc"
681
682 {{source_file_part}}
683 The file part of the source including the extension (with no directory
684 information).
685 Example: "foo.cc"
686
687 {{source_name_part}}
688 The filename part of the source file with no directory or extension.
689 Example: "foo"
690
691 {{source_gen_dir}}
692 {{source_out_dir}}
693 The directory in the generated file and output directories,
694 respectively, for the current input file. If the source file is in the
695 same directory as the target is declared in, they will will be the same
696 as the "target" versions above. Example: "gen/base/test"
697
698 Linker tools have multiple inputs and (potentially) multiple outputs. The
699 static library tool ("alink") is not considered a linker tool. The following
700 expansions are available:
701
702 {{inputs}}
703 {{inputs_newline}}
704 Expands to the inputs to the link step. This will be a list of object
705 files and static libraries.
706 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
707
708 The "_newline" version will separate the input files with newlines
709 instead of spaces. This is useful in response files: some linkers can
710 take a "-filelist" flag which expects newline separated files, and some
711 Microsoft tools have a fixed-sized buffer for parsing each line of a
712 response file.
713
714 {{ldflags}}
715 Expands to the processed set of ldflags and library search paths
716 specified for the target.
717 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
718
719 {{libs}}
720 Expands to the list of system libraries to link to. Each will be
721 prefixed by the "lib_switch".
722
723 Example: "-lfoo -lbar"
724
725 {{output_dir}}
726 The value of the "output_dir" variable in the target, or the the value
727 of the "default_output_dir" value in the tool if the target does not
728 override the output directory. This will be relative to the
729 root_build_dir and will not end in a slash. Will be "." for output to
730 the root_build_dir.
731
732 This is subtly different than {{target_out_dir}} which is defined by GN
733 based on the target's path and not overridable. {{output_dir}} is for
734 the final output, {{target_out_dir}} is generally for object files and
735 other outputs.
736
737 Usually {{output_dir}} would be defined in terms of either
738 {{target_out_dir}} or {{root_out_dir}}
739
740 {{output_extension}}
741 The value of the "output_extension" variable in the target, or the
742 value of the "default_output_extension" value in the tool if the target
743 does not specify an output extension.
744 Example: ".so"
745
746 {{solibs}}
747 Extra libraries from shared library dependencies not specified in the
748 {{inputs}}. This is the list of link_output files from shared libraries
749 (if the solink tool specifies a "link_output" variable separate from
750 the "depend_output").
751
752 These should generally be treated the same as libs by your tool.
753
754 Example: "libfoo.so libbar.so"
755
756 {{rlibs}}
757 Any Rust .rlibs which need to be linked into a final C++ target.
758 These should be treated as {{inputs}} except that sometimes
759 they might have different linker directives applied.
760
761 Example: "obj/foo/libfoo.rlib"
762
763 {{frameworks}}
764 Shared libraries packaged as framework bundle. This is principally
765 used on Apple's platforms (macOS and iOS). All name must be ending
766 with ".framework" suffix; the suffix will be stripped when expanding
767 {{frameworks}} and each item will be preceded by "-framework" or
768 "-weak_framework".
769
770 {{swiftmodules}}
771 Swift .swiftmodule files that needs to be embedded into the binary.
772 This is necessary to correctly link with object generated by the
773 Swift compiler (the .swiftmodule file cannot be embedded in object
774 files directly). Those will be prefixed with "swiftmodule_switch"
775 value.
776
777 )" // String break to prevent overflowing the 16K max VC string length.
778 R"( The static library ("alink") tool allows {{arflags}} plus the common tool
779 substitutions.
780
781 The copy tool allows the common compiler/linker substitutions, plus
782 {{source}} which is the source of the copy. The stamp tool allows only the
783 common tool substitutions.
784
785 The copy_bundle_data and compile_xcassets tools only allows the common tool
786 substitutions. Both tools are required to create iOS/macOS bundles and need
787 only be defined on those platforms.
788
789 The copy_bundle_data tool will be called with one source and needs to copy
790 (optionally optimizing the data representation) to its output. It may be
791 called with a directory as input and it needs to be recursively copied.
792
793 The compile_xcassets tool will be called with one or more source (each an
794 asset catalog) that needs to be compiled to a single output. The following
795 substitutions are available:
796
797 {{inputs}}
798 Expands to the list of .xcassets to use as input to compile the asset
799 catalog.
800
801 {{bundle_product_type}}
802 Expands to the product_type of the bundle that will contain the
803 compiled asset catalog. Usually corresponds to the product_type
804 property of the corresponding create_bundle target.
805
806 {{bundle_partial_info_plist}}
807 Expands to the path to the partial Info.plist generated by the
808 assets catalog compiler. Usually based on the target_name of
809 the create_bundle target.
810
811 {{xcasset_compiler_flags}}
812 Expands to the list of flags specified in corresponding
813 create_bundle target.
814
815 The inputs for compile_xcassets tool will be found from the bundle_data
816 dependencies by looking for any file matching "*/*.xcassets/*" pattern.
817 The "$assets.xcassets" directory will be added as input to the tool.
818
819 The Swift tool has multiple input and outputs. It must have exactly one
820 output of .swiftmodule type, but can have one or more object file outputs,
821 in addition to other type of outputs. The following expansions are available:
822
823 {{module_name}}
824 Expands to the string representing the module name of target under
825 compilation (see "module_name" variable).
826
827 {{module_dirs}}
828 Expands to the list of -I<path> for the target Swift module search
829 path computed from target dependencies.
830
831 {{swiftflags}}
832 Expands to the list of strings representing Swift compiler flags.
833
834 Rust tools have the notion of a single input and a single output, along
835 with a set of compiler-specific flags. The following expansions are
836 available:
837
838 {{crate_name}}
839 Expands to the string representing the crate name of target under
840 compilation.
841
842 {{crate_type}}
843 Expands to the string representing the type of crate for the target
844 under compilation.
845
846 {{externs}}
847 Expands to the list of --extern flags needed to include addition Rust
848 libraries in this target. Includes any specified renamed dependencies.
849
850 {{rustdeps}}
851 Expands to the list of -Ldependency=<path> strings needed to compile
852 this target.
853
854 {{rustenv}}
855 Expands to the list of environment variables.
856
857 {{rustflags}}
858 Expands to the list of strings representing Rust compiler flags.
859
860 Separate linking and dependencies for shared libraries
861
862 Shared libraries are special in that not all changes to them require that
863 dependent targets be re-linked. If the shared library is changed but no
864 imports or exports are different, dependent code needn't be relinked, which
865 can speed up the build.
866
867 If your link step can output a list of exports from a shared library and
868 writes the file only if the new one is different, the timestamp of this file
869 can be used for triggering re-links, while the actual shared library would be
870 used for linking.
871
872 You will need to specify
873 restat = true
874 in the linker tool to make this work, so Ninja will detect if the timestamp
875 of the dependency file has changed after linking (otherwise it will always
876 assume that running a command updates the output):
877
878 tool("solink") {
879 command = "..."
880 outputs = [
881 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
882 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC",
883 ]
884 link_output =
885 "{{output_dir}}/{{target_output_name}}{{output_extension}}"
886 depend_output =
887 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC"
888 restat = true
889 }
890
891 Example
892
893 toolchain("my_toolchain") {
894 # Put these at the top to apply to all tools below.
895 lib_switch = "-l"
896 lib_dir_switch = "-L"
897
898 tool("cc") {
899 command = "gcc {{source}} -o {{output}}"
900 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
901 description = "GCC {{source}}"
902 }
903 tool("cxx") {
904 command = "g++ {{source}} -o {{output}}"
905 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
906 description = "G++ {{source}}"
907 }
908 };
909 )";
910
RunTool(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,BlockNode * block,Err * err)911 Value RunTool(Scope* scope,
912 const FunctionCallNode* function,
913 const std::vector<Value>& args,
914 BlockNode* block,
915 Err* err) {
916 // Find the toolchain definition we're executing inside of. The toolchain
917 // function will set a property pointing to it that we'll pick up.
918 Toolchain* toolchain = reinterpret_cast<Toolchain*>(
919 scope->GetProperty(&kToolchainPropertyKey, nullptr));
920 if (!toolchain) {
921 *err = Err(function->function(), "tool() called outside of toolchain().",
922 "The tool() function can only be used inside a toolchain() "
923 "definition.");
924 return Value();
925 }
926
927 if (!EnsureSingleStringArg(function, args, err))
928 return Value();
929 const std::string& tool_name = args[0].string_value();
930
931 // Run the tool block.
932 Scope block_scope(scope);
933 block->Execute(&block_scope, err);
934 if (err->has_error())
935 return Value();
936
937 std::unique_ptr<Tool> tool =
938 Tool::CreateTool(function, tool_name, &block_scope, toolchain, err);
939
940 if (!tool) {
941 return Value();
942 }
943
944 tool->set_defined_from(function);
945 toolchain->SetTool(std::move(tool));
946
947 // Make sure there weren't any vars set in this tool that were unused.
948 if (!block_scope.CheckForUnusedVars(err))
949 return Value();
950
951 return Value();
952 }
953
954 } // namespace functions
955