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.
5 #include "tools/gn/scope_per_file_provider.h"
7 #include "tools/gn/filesystem_utils.h"
8 #include "tools/gn/settings.h"
9 #include "tools/gn/source_file.h"
10 #include "tools/gn/value.h"
11 #include "tools/gn/variables.h"
13 ScopePerFileProvider::ScopePerFileProvider(Scope
* scope
,
14 bool allow_target_vars
)
15 : ProgrammaticProvider(scope
),
16 allow_target_vars_(allow_target_vars
) {
19 ScopePerFileProvider::~ScopePerFileProvider() {
22 const Value
* ScopePerFileProvider::GetProgrammaticValue(
23 const base::StringPiece
& ident
) {
24 if (ident
== variables::kCurrentToolchain
)
25 return GetCurrentToolchain();
26 if (ident
== variables::kDefaultToolchain
)
27 return GetDefaultToolchain();
28 if (ident
== variables::kPythonPath
)
29 return GetPythonPath();
31 if (ident
== variables::kRootBuildDir
)
32 return GetRootBuildDir();
33 if (ident
== variables::kRootGenDir
)
34 return GetRootGenDir();
35 if (ident
== variables::kRootOutDir
)
36 return GetRootOutDir();
38 if (allow_target_vars_
) {
39 if (ident
== variables::kTargetGenDir
)
40 return GetTargetGenDir();
41 if (ident
== variables::kTargetOutDir
)
42 return GetTargetOutDir();
47 const Value
* ScopePerFileProvider::GetCurrentToolchain() {
48 if (!current_toolchain_
) {
49 current_toolchain_
.reset(new Value(NULL
,
50 scope_
->settings()->toolchain_label().GetUserVisibleName(false)));
52 return current_toolchain_
.get();
55 const Value
* ScopePerFileProvider::GetDefaultToolchain() {
56 if (!default_toolchain_
) {
57 default_toolchain_
.reset(new Value(NULL
,
58 scope_
->settings()->default_toolchain_label().GetUserVisibleName(
61 return default_toolchain_
.get();
64 const Value
* ScopePerFileProvider::GetPythonPath() {
66 python_path_
.reset(new Value(NULL
,
67 FilePathToUTF8(scope_
->settings()->build_settings()->python_path())));
69 return python_path_
.get();
72 const Value
* ScopePerFileProvider::GetRootBuildDir() {
73 if (!root_build_dir_
) {
74 root_build_dir_
.reset(new Value(NULL
, DirectoryWithNoLastSlash(
75 scope_
->settings()->build_settings()->build_dir())));
77 return root_build_dir_
.get();
80 const Value
* ScopePerFileProvider::GetRootGenDir() {
82 root_gen_dir_
.reset(new Value(NULL
,
83 DirectoryWithNoLastSlash(GetToolchainGenDir(scope_
->settings()))));
85 return root_gen_dir_
.get();
88 const Value
* ScopePerFileProvider::GetRootOutDir() {
90 root_out_dir_
.reset(new Value(NULL
,
91 DirectoryWithNoLastSlash(GetToolchainOutputDir(scope_
->settings()))));
93 return root_out_dir_
.get();
96 const Value
* ScopePerFileProvider::GetTargetGenDir() {
97 if (!target_gen_dir_
) {
98 target_gen_dir_
.reset(new Value(NULL
,
99 DirectoryWithNoLastSlash(GetCurrentGenDir(scope_
))));
101 return target_gen_dir_
.get();
104 const Value
* ScopePerFileProvider::GetTargetOutDir() {
105 if (!target_out_dir_
) {
106 target_out_dir_
.reset(new Value(NULL
,
107 DirectoryWithNoLastSlash(GetCurrentOutputDir(scope_
))));
109 return target_out_dir_
.get();