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/scope_per_file_provider.h"
6
7 #include <memory>
8
9 #include "gn/filesystem_utils.h"
10 #include "gn/settings.h"
11 #include "gn/source_file.h"
12 #include "gn/value.h"
13 #include "gn/variables.h"
14
15 #include "last_commit_position.h"
16
ScopePerFileProvider(Scope * scope,bool allow_target_vars)17 ScopePerFileProvider::ScopePerFileProvider(Scope* scope, bool allow_target_vars)
18 : ProgrammaticProvider(scope), allow_target_vars_(allow_target_vars) {}
19
20 ScopePerFileProvider::~ScopePerFileProvider() = default;
21
GetProgrammaticValue(std::string_view ident)22 const Value* ScopePerFileProvider::GetProgrammaticValue(
23 std::string_view ident) {
24 if (ident == variables::kCurrentToolchain)
25 return GetCurrentToolchain();
26 if (ident == variables::kDefaultToolchain)
27 return GetDefaultToolchain();
28 if (ident == variables::kGnVersion)
29 return GetGnVersion();
30 if (ident == variables::kPythonPath)
31 return GetPythonPath();
32
33 if (ident == variables::kRootBuildDir)
34 return GetRootBuildDir();
35 if (ident == variables::kRootGenDir)
36 return GetRootGenDir();
37 if (ident == variables::kRootOutDir)
38 return GetRootOutDir();
39
40 if (allow_target_vars_) {
41 if (ident == variables::kTargetGenDir)
42 return GetTargetGenDir();
43 if (ident == variables::kTargetOutDir)
44 return GetTargetOutDir();
45 }
46 return nullptr;
47 }
48
GetCurrentToolchain()49 const Value* ScopePerFileProvider::GetCurrentToolchain() {
50 if (!current_toolchain_) {
51 current_toolchain_ = std::make_unique<Value>(
52 nullptr,
53 scope_->settings()->toolchain_label().GetUserVisibleName(false));
54 }
55 return current_toolchain_.get();
56 }
57
GetDefaultToolchain()58 const Value* ScopePerFileProvider::GetDefaultToolchain() {
59 if (!default_toolchain_) {
60 default_toolchain_ = std::make_unique<Value>(
61 nullptr,
62 scope_->settings()->default_toolchain_label().GetUserVisibleName(
63 false));
64 }
65 return default_toolchain_.get();
66 }
67
GetGnVersion()68 const Value* ScopePerFileProvider::GetGnVersion() {
69 if (!gn_version_) {
70 gn_version_ = std::make_unique<Value>(
71 nullptr, static_cast<int64_t>(LAST_COMMIT_POSITION_NUM));
72 }
73 return gn_version_.get();
74 }
75
GetPythonPath()76 const Value* ScopePerFileProvider::GetPythonPath() {
77 if (!python_path_) {
78 python_path_ = std::make_unique<Value>(
79 nullptr,
80 FilePathToUTF8(scope_->settings()->build_settings()->python_path()));
81 }
82 return python_path_.get();
83 }
84
GetRootBuildDir()85 const Value* ScopePerFileProvider::GetRootBuildDir() {
86 if (!root_build_dir_) {
87 root_build_dir_ = std::make_unique<Value>(
88 nullptr, DirectoryWithNoLastSlash(
89 scope_->settings()->build_settings()->build_dir()));
90 }
91 return root_build_dir_.get();
92 }
93
GetRootGenDir()94 const Value* ScopePerFileProvider::GetRootGenDir() {
95 if (!root_gen_dir_) {
96 root_gen_dir_ = std::make_unique<Value>(
97 nullptr, DirectoryWithNoLastSlash(GetBuildDirAsSourceDir(
98 BuildDirContext(scope_), BuildDirType::GEN)));
99 }
100 return root_gen_dir_.get();
101 }
102
GetRootOutDir()103 const Value* ScopePerFileProvider::GetRootOutDir() {
104 if (!root_out_dir_) {
105 root_out_dir_ = std::make_unique<Value>(
106 nullptr, DirectoryWithNoLastSlash(GetScopeCurrentBuildDirAsSourceDir(
107 scope_, BuildDirType::TOOLCHAIN_ROOT)));
108 }
109 return root_out_dir_.get();
110 }
111
GetTargetGenDir()112 const Value* ScopePerFileProvider::GetTargetGenDir() {
113 if (!target_gen_dir_) {
114 target_gen_dir_ = std::make_unique<Value>(
115 nullptr, DirectoryWithNoLastSlash(GetScopeCurrentBuildDirAsSourceDir(
116 scope_, BuildDirType::GEN)));
117 }
118 return target_gen_dir_.get();
119 }
120
GetTargetOutDir()121 const Value* ScopePerFileProvider::GetTargetOutDir() {
122 if (!target_out_dir_) {
123 target_out_dir_ = std::make_unique<Value>(
124 nullptr, DirectoryWithNoLastSlash(GetScopeCurrentBuildDirAsSourceDir(
125 scope_, BuildDirType::OBJ)));
126 }
127 return target_out_dir_.get();
128 }
129