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 "gn/build_settings.h"
6 #include "gn/functions.h"
7 #include "gn/loader.h"
8 #include "gn/parse_tree.h"
9 #include "gn/scope.h"
10 #include "gn/settings.h"
11
12 namespace functions {
13
14 const char kSetDefaultToolchain[] = "set_default_toolchain";
15 const char kSetDefaultToolchain_HelpShort[] =
16 "set_default_toolchain: Sets the default toolchain name.";
17 const char kSetDefaultToolchain_Help[] =
18 R"(set_default_toolchain: Sets the default toolchain name.
19
20 set_default_toolchain(toolchain_label)
21
22 The given label should identify a toolchain definition (see "gn help
23 toolchain"). This toolchain will be used for all targets unless otherwise
24 specified.
25
26 This function is only valid to call during the processing of the build
27 configuration file. Since the build configuration file is processed
28 separately for each toolchain, this function will be a no-op when called
29 under any non-default toolchains.
30
31 For example, the default toolchain should be appropriate for the current
32 environment. If the current environment is 32-bit and somebody references a
33 target with a 64-bit toolchain, we wouldn't want processing of the build
34 config file for the 64-bit toolchain to reset the default toolchain to
35 64-bit, we want to keep it 32-bits.
36
37 Argument
38
39 toolchain_label
40 Toolchain name.
41
42 Example
43
44 # Set default toolchain only has an effect when run in the context of the
45 # default toolchain. Pick the right one according to the current CPU
46 # architecture.
47 if (target_cpu == "x64") {
48 set_default_toolchain("//toolchains:64")
49 } else if (target_cpu == "x86") {
50 set_default_toolchain("//toolchains:32")
51 }
52 )";
53
RunSetDefaultToolchain(Scope * scope,const FunctionCallNode * function,const std::vector<Value> & args,Err * err)54 Value RunSetDefaultToolchain(Scope* scope,
55 const FunctionCallNode* function,
56 const std::vector<Value>& args,
57 Err* err) {
58 if (!scope->IsProcessingBuildConfig()) {
59 *err = Err(
60 function->function(), "Must be called from build config.",
61 "set_default_toolchain can only be called from the build configuration "
62 "file.");
63 return Value();
64 }
65
66 // When the loader is expecting the default toolchain to be set, it will set
67 // this key on the scope to point to the destination.
68 Label* default_toolchain_dest = static_cast<Label*>(
69 scope->GetProperty(Loader::kDefaultToolchainKey, nullptr));
70 if (!default_toolchain_dest)
71 return Value();
72
73 const SourceDir& current_dir = scope->GetSourceDir();
74 const Label& default_toolchain = ToolchainLabelForScope(scope);
75
76 if (!EnsureSingleStringArg(function, args, err))
77 return Value();
78 Label toolchain_label(Label::Resolve(
79 current_dir, scope->settings()->build_settings()->root_path_utf8(),
80 default_toolchain, args[0], err));
81 if (toolchain_label.is_null())
82 return Value();
83
84 *default_toolchain_dest = toolchain_label;
85 return Value();
86 }
87
88 } // namespace functions
89