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