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 )" // String break to prevent overflowing the 16K max VC string length.
596 R"(Expansions for tool variables
597
598 All paths are relative to the root build directory, which is the current
599 directory for running all tools. These expansions are available to all tools:
600
601 {{label}}
602 The label of the current target. This is typically used in the
603 "description" field for link tools. The toolchain will be omitted from
604 the label for targets in the default toolchain, and will be included
605 for targets in other toolchains.
606
607 {{label_name}}
608 The short name of the label of the target. This is the part after the
609 colon. For "//foo/bar:baz" this will be "baz". Unlike
610 {{target_output_name}}, this is not affected by the "output_prefix" in
611 the tool or the "output_name" set on the target.
612
613 {{label_no_toolchain}}
614 The label of the current target, never including the toolchain
615 (otherwise, this is identical to {{label}}). This is used as the module
616 name when using .modulemap files.
617
618 {{output}}
619 The relative path and name of the output(s) of the current build step.
620 If there is more than one output, this will expand to a list of all of
621 them. Example: "out/base/my_file.o"
622
623 {{target_gen_dir}}
624 {{target_out_dir}}
625 The directory of the generated file and output directories,
626 respectively, for the current target. There is no trailing slash. See
627 also {{output_dir}} for linker tools. Example: "out/base/test"
628
629 {{target_output_name}}
630 The short name of the current target with no path information, or the
631 value of the "output_name" variable if one is specified in the target.
632 This will include the "output_prefix" if any. See also {{label_name}}.
633
634 Example: "libfoo" for the target named "foo" and an output prefix for
635 the linker tool of "lib".
636
637 Compiler tools have the notion of a single input and a single output, along
638 with a set of compiler-specific flags. The following expansions are
639 available:
640
641 {{asmflags}}
642 {{cflags}}
643 {{cflags_c}}
644 {{cflags_cc}}
645 {{cflags_objc}}
646 {{cflags_objcc}}
647 {{defines}}
648 {{include_dirs}}
649 Strings correspond that to the processed flags/defines/include
650 directories specified for the target.
651 Example: "--enable-foo --enable-bar"
652
653 Defines will be prefixed by "-D" and include directories will be
654 prefixed by "-I" (these work with Posix tools as well as Microsoft
655 ones).
656
657 {{module_deps}}
658 {{module_deps_no_self}}
659 Strings that correspond to the flags necessary to depend upon the Clang
660 modules referenced by the current target. The "_no_self" version doesn't
661 include the module for the current target, and can be used to compile
662 the pcm itself.
663
664 {{source}}
665 The relative path and name of the current input file.
666 Example: "../../base/my_file.cc"
667
668 {{source_file_part}}
669 The file part of the source including the extension (with no directory
670 information).
671 Example: "foo.cc"
672
673 {{source_name_part}}
674 The filename part of the source file with no directory or extension.
675 Example: "foo"
676
677 {{source_gen_dir}}
678 {{source_out_dir}}
679 The directory in the generated file and output directories,
680 respectively, for the current input file. If the source file is in the
681 same directory as the target is declared in, they will will be the same
682 as the "target" versions above. Example: "gen/base/test"
683
684 Linker tools have multiple inputs and (potentially) multiple outputs. The
685 static library tool ("alink") is not considered a linker tool. The following
686 expansions are available:
687
688 {{inputs}}
689 {{inputs_newline}}
690 Expands to the inputs to the link step. This will be a list of object
691 files and static libraries.
692 Example: "obj/foo.o obj/bar.o obj/somelibrary.a"
693
694 The "_newline" version will separate the input files with newlines
695 instead of spaces. This is useful in response files: some linkers can
696 take a "-filelist" flag which expects newline separated files, and some
697 Microsoft tools have a fixed-sized buffer for parsing each line of a
698 response file.
699
700 {{ldflags}}
701 Expands to the processed set of ldflags and library search paths
702 specified for the target.
703 Example: "-m64 -fPIC -pthread -L/usr/local/mylib"
704
705 {{libs}}
706 Expands to the list of system libraries to link to. Each will be
707 prefixed by the "lib_switch".
708
709 Example: "-lfoo -lbar"
710
711 {{output_dir}}
712 The value of the "output_dir" variable in the target, or the the value
713 of the "default_output_dir" value in the tool if the target does not
714 override the output directory. This will be relative to the
715 root_build_dir and will not end in a slash. Will be "." for output to
716 the root_build_dir.
717
718 This is subtly different than {{target_out_dir}} which is defined by GN
719 based on the target's path and not overridable. {{output_dir}} is for
720 the final output, {{target_out_dir}} is generally for object files and
721 other outputs.
722
723 Usually {{output_dir}} would be defined in terms of either
724 {{target_out_dir}} or {{root_out_dir}}
725
726 {{output_extension}}
727 The value of the "output_extension" variable in the target, or the
728 value of the "default_output_extension" value in the tool if the target
729 does not specify an output extension.
730 Example: ".so"
731
732 {{solibs}}
733 Extra libraries from shared library dependencies not specified in the
734 {{inputs}}. This is the list of link_output files from shared libraries
735 (if the solink tool specifies a "link_output" variable separate from
736 the "depend_output").
737
738 These should generally be treated the same as libs by your tool.
739
740 Example: "libfoo.so libbar.so"
741
742 {{rlibs}}
743 Any Rust .rlibs which need to be linked into a final C++ target.
744 These should be treated as {{inputs}} except that sometimes
745 they might have different linker directives applied.
746
747 Example: "obj/foo/libfoo.rlib"
748
749 {{frameworks}}
750 Shared libraries packaged as framework bundle. This is principally
751 used on Apple's platforms (macOS and iOS). All name must be ending
752 with ".framework" suffix; the suffix will be stripped when expanding
753 {{frameworks}} and each item will be preceded by "-framework" or
754 "-weak_framework".
755
756 {{swiftmodules}}
757 Swift .swiftmodule files that needs to be embedded into the binary.
758 This is necessary to correctly link with object generated by the
759 Swift compiler (the .swiftmodule file cannot be embedded in object
760 files directly). Those will be prefixed with "swiftmodule_switch"
761 value.
762
763 )" // String break to prevent overflowing the 16K max VC string length.
764 R"( The static library ("alink") tool allows {{arflags}} plus the common tool
765 substitutions.
766
767 The copy tool allows the common compiler/linker substitutions, plus
768 {{source}} which is the source of the copy. The stamp tool allows only the
769 common tool substitutions.
770
771 The copy_bundle_data and compile_xcassets tools only allows the common tool
772 substitutions. Both tools are required to create iOS/macOS bundles and need
773 only be defined on those platforms.
774
775 The copy_bundle_data tool will be called with one source and needs to copy
776 (optionally optimizing the data representation) to its output. It may be
777 called with a directory as input and it needs to be recursively copied.
778
779 The compile_xcassets tool will be called with one or more source (each an
780 asset catalog) that needs to be compiled to a single output. The following
781 substitutions are available:
782
783 {{inputs}}
784 Expands to the list of .xcassets to use as input to compile the asset
785 catalog.
786
787 {{bundle_product_type}}
788 Expands to the product_type of the bundle that will contain the
789 compiled asset catalog. Usually corresponds to the product_type
790 property of the corresponding create_bundle target.
791
792 {{bundle_partial_info_plist}}
793 Expands to the path to the partial Info.plist generated by the
794 assets catalog compiler. Usually based on the target_name of
795 the create_bundle target.
796
797 {{xcasset_compiler_flags}}
798 Expands to the list of flags specified in corresponding
799 create_bundle target.
800
801 The Swift tool has multiple input and outputs. It must have exactly one
802 output of .swiftmodule type, but can have one or more object file outputs,
803 in addition to other type of outputs. The following expansions are available:
804
805 {{module_name}}
806 Expands to the string representing the module name of target under
807 compilation (see "module_name" variable).
808
809 {{module_dirs}}
810 Expands to the list of -I<path> for the target Swift module search
811 path computed from target dependencies.
812
813 {{swiftflags}}
814 Expands to the list of strings representing Swift compiler flags.
815
816 Rust tools have the notion of a single input and a single output, along
817 with a set of compiler-specific flags. The following expansions are
818 available:
819
820 {{crate_name}}
821 Expands to the string representing the crate name of target under
822 compilation.
823
824 {{crate_type}}
825 Expands to the string representing the type of crate for the target
826 under compilation.
827
828 {{externs}}
829 Expands to the list of --extern flags needed to include addition Rust
830 libraries in this target. Includes any specified renamed dependencies.
831
832 {{rustdeps}}
833 Expands to the list of -Ldependency=<path> strings needed to compile
834 this target.
835
836 {{rustenv}}
837 Expands to the list of environment variables.
838
839 {{rustflags}}
840 Expands to the list of strings representing Rust compiler flags.
841
842 Separate linking and dependencies for shared libraries
843
844 Shared libraries are special in that not all changes to them require that
845 dependent targets be re-linked. If the shared library is changed but no
846 imports or exports are different, dependent code needn't be relinked, which
847 can speed up the build.
848
849 If your link step can output a list of exports from a shared library and
850 writes the file only if the new one is different, the timestamp of this file
851 can be used for triggering re-links, while the actual shared library would be
852 used for linking.
853
854 You will need to specify
855 restat = true
856 in the linker tool to make this work, so Ninja will detect if the timestamp
857 of the dependency file has changed after linking (otherwise it will always
858 assume that running a command updates the output):
859
860 tool("solink") {
861 command = "..."
862 outputs = [
863 "{{output_dir}}/{{target_output_name}}{{output_extension}}",
864 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC",
865 ]
866 link_output =
867 "{{output_dir}}/{{target_output_name}}{{output_extension}}"
868 depend_output =
869 "{{output_dir}}/{{target_output_name}}{{output_extension}}.TOC"
870 restat = true
871 }
872
873 Example
874
875 toolchain("my_toolchain") {
876 # Put these at the top to apply to all tools below.
877 lib_switch = "-l"
878 lib_dir_switch = "-L"
879
880 tool("cc") {
881 command = "gcc {{source}} -o {{output}}"
882 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
883 description = "GCC {{source}}"
884 }
885 tool("cxx") {
886 command = "g++ {{source}} -o {{output}}"
887 outputs = [ "{{source_out_dir}}/{{source_name_part}}.o" ]
888 description = "G++ {{source}}"
889 }
890 };
891 )";
892
RunTool(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,BlockNode * block,Err * err)893 Value RunTool(Scope* scope,
894 const FunctionCallNode* function,
895 const std::vector<Value>& args,
896 BlockNode* block,
897 Err* err) {
898 // Find the toolchain definition we're executing inside of. The toolchain
899 // function will set a property pointing to it that we'll pick up.
900 Toolchain* toolchain = reinterpret_cast<Toolchain*>(
901 scope->GetProperty(&kToolchainPropertyKey, nullptr));
902 if (!toolchain) {
903 *err = Err(function->function(), "tool() called outside of toolchain().",
904 "The tool() function can only be used inside a toolchain() "
905 "definition.");
906 return Value();
907 }
908
909 if (!EnsureSingleStringArg(function, args, err))
910 return Value();
911 const std::string& tool_name = args[0].string_value();
912
913 // Run the tool block.
914 Scope block_scope(scope);
915 block->Execute(&block_scope, err);
916 if (err->has_error())
917 return Value();
918
919 std::unique_ptr<Tool> tool =
920 Tool::CreateTool(function, tool_name, &block_scope, toolchain, err);
921
922 if (!tool) {
923 return Value();
924 }
925
926 tool->set_defined_from(function);
927 toolchain->SetTool(std::move(tool));
928
929 // Make sure there weren't any vars set in this tool that were unused.
930 if (!block_scope.CheckForUnusedVars(err))
931 return Value();
932
933 return Value();
934 }
935
936 } // namespace functions
937