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/scope_per_file_provider.h"
6
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"
12
13ScopePerFileProvider::ScopePerFileProvider(Scope* scope,
14                                           bool allow_target_vars)
15    : ProgrammaticProvider(scope),
16      allow_target_vars_(allow_target_vars) {
17}
18
19ScopePerFileProvider::~ScopePerFileProvider() {
20}
21
22const 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();
30
31  if (ident == variables::kRootBuildDir)
32    return GetRootBuildDir();
33  if (ident == variables::kRootGenDir)
34    return GetRootGenDir();
35  if (ident == variables::kRootOutDir)
36    return GetRootOutDir();
37
38  if (allow_target_vars_) {
39    if (ident == variables::kTargetGenDir)
40      return GetTargetGenDir();
41    if (ident == variables::kTargetOutDir)
42      return GetTargetOutDir();
43  }
44  return NULL;
45}
46
47const Value* ScopePerFileProvider::GetCurrentToolchain() {
48  if (!current_toolchain_) {
49    current_toolchain_.reset(new Value(NULL,
50        scope_->settings()->toolchain_label().GetUserVisibleName(false)));
51  }
52  return current_toolchain_.get();
53}
54
55const Value* ScopePerFileProvider::GetDefaultToolchain() {
56  if (!default_toolchain_) {
57    default_toolchain_.reset(new Value(NULL,
58        scope_->settings()->default_toolchain_label().GetUserVisibleName(
59            false)));
60  }
61  return default_toolchain_.get();
62}
63
64const Value* ScopePerFileProvider::GetPythonPath() {
65  if (!python_path_) {
66    python_path_.reset(new Value(NULL,
67        FilePathToUTF8(scope_->settings()->build_settings()->python_path())));
68  }
69  return python_path_.get();
70}
71
72const Value* ScopePerFileProvider::GetRootBuildDir() {
73  if (!root_build_dir_) {
74    root_build_dir_.reset(new Value(NULL, DirectoryWithNoLastSlash(
75        scope_->settings()->build_settings()->build_dir())));
76  }
77  return root_build_dir_.get();
78}
79
80const Value* ScopePerFileProvider::GetRootGenDir() {
81  if (!root_gen_dir_) {
82    root_gen_dir_.reset(new Value(NULL,
83        DirectoryWithNoLastSlash(GetToolchainGenDir(scope_->settings()))));
84  }
85  return root_gen_dir_.get();
86}
87
88const Value* ScopePerFileProvider::GetRootOutDir() {
89  if (!root_out_dir_) {
90    root_out_dir_.reset(new Value(NULL,
91        DirectoryWithNoLastSlash(GetToolchainOutputDir(scope_->settings()))));
92  }
93  return root_out_dir_.get();
94}
95
96const Value* ScopePerFileProvider::GetTargetGenDir() {
97  if (!target_gen_dir_) {
98    target_gen_dir_.reset(new Value(NULL,
99        DirectoryWithNoLastSlash(GetCurrentGenDir(scope_))));
100  }
101  return target_gen_dir_.get();
102}
103
104const Value* ScopePerFileProvider::GetTargetOutDir() {
105  if (!target_out_dir_) {
106    target_out_dir_.reset(new Value(NULL,
107        DirectoryWithNoLastSlash(GetCurrentOutputDir(scope_))));
108  }
109  return target_out_dir_.get();
110}
111