OLD | NEW |
| (Empty) |
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/toolchain_manager.h" | |
11 #include "tools/gn/value.h" | |
12 | |
13 const char* ScopePerFileProvider::kDefaultToolchain = | |
14 "default_toolchain"; | |
15 const char* ScopePerFileProvider::kPythonPath = | |
16 "python_path"; | |
17 const char* ScopePerFileProvider::kToolchain = | |
18 "toolchain"; | |
19 const char* ScopePerFileProvider::kRootOutputDirName = | |
20 "root_output_dir"; | |
21 const char* ScopePerFileProvider::kRootGenDirName = | |
22 "root_gen_dir"; | |
23 const char* ScopePerFileProvider::kTargetOutputDirName = | |
24 "target_output_dir"; | |
25 const char* ScopePerFileProvider::kTargetGenDirName = | |
26 "target_gen_dir"; | |
27 const char* ScopePerFileProvider::kRelativeRootOutputDirName = | |
28 "relative_root_output_dir"; | |
29 const char* ScopePerFileProvider::kRelativeRootGenDirName = | |
30 "relative_root_gen_dir"; | |
31 const char* ScopePerFileProvider::kRelativeTargetOutputDirName = | |
32 "relative_target_output_dir"; | |
33 const char* ScopePerFileProvider::kRelativeTargetGenDirName = | |
34 "relative_target_gen_dir"; | |
35 | |
36 ScopePerFileProvider::ScopePerFileProvider(Scope* scope, | |
37 const SourceFile& source_file) | |
38 : ProgrammaticProvider(scope), | |
39 source_file_(source_file) { | |
40 } | |
41 | |
42 ScopePerFileProvider::~ScopePerFileProvider() { | |
43 } | |
44 | |
45 const Value* ScopePerFileProvider::GetProgrammaticValue( | |
46 const base::StringPiece& ident) { | |
47 if (ident == kDefaultToolchain) | |
48 return GetDefaultToolchain(); | |
49 if (ident == kPythonPath) | |
50 return GetPythonPath(); | |
51 | |
52 if (ident == kTargetOutputDirName) | |
53 return GetTargetOutputDir(); | |
54 if (ident == kTargetGenDirName) | |
55 return GetTargetGenDir(); | |
56 | |
57 if (ident == kRelativeRootOutputDirName) | |
58 return GetRelativeRootOutputDir(); | |
59 if (ident == kRelativeRootGenDirName) | |
60 return GetRelativeRootGenDir(); | |
61 if (ident == kRelativeTargetOutputDirName) | |
62 return GetRelativeTargetOutputDir(); | |
63 if (ident == kRelativeTargetGenDirName) | |
64 return GetRelativeTargetGenDir(); | |
65 return NULL; | |
66 } | |
67 | |
68 // static | |
69 Value ScopePerFileProvider::GetRootOutputDir(const Settings* settings) { | |
70 return Value(NULL, GetRootOutputDirWithNoLastSlash(settings)); | |
71 } | |
72 | |
73 // static | |
74 Value ScopePerFileProvider::GetRootGenDir(const Settings* settings) { | |
75 return Value(NULL, GetRootGenDirWithNoLastSlash(settings)); | |
76 } | |
77 | |
78 const Value* ScopePerFileProvider::GetDefaultToolchain() { | |
79 if (!default_toolchain_) { | |
80 const ToolchainManager& toolchain_manager = | |
81 scope_->settings()->build_settings()->toolchain_manager(); | |
82 default_toolchain_.reset(new Value(NULL, | |
83 toolchain_manager.GetDefaultToolchainUnlocked().GetUserVisibleName( | |
84 false))); | |
85 } | |
86 return default_toolchain_.get(); | |
87 } | |
88 | |
89 const Value* ScopePerFileProvider::GetPythonPath() { | |
90 if (!python_path_) { | |
91 python_path_.reset(new Value(NULL, | |
92 FilePathToUTF8(scope_->settings()->build_settings()->python_path()))); | |
93 } | |
94 return python_path_.get(); | |
95 } | |
96 | |
97 const Value* ScopePerFileProvider::GetToolchain() { | |
98 if (!toolchain_) { | |
99 toolchain_.reset(new Value(NULL, | |
100 scope_->settings()->toolchain()->label().GetUserVisibleName(false))); | |
101 } | |
102 return toolchain_.get(); | |
103 } | |
104 | |
105 const Value* ScopePerFileProvider::GetTargetOutputDir() { | |
106 if (!target_output_dir_) { | |
107 target_output_dir_.reset(new Value(NULL, | |
108 GetRootOutputDirWithNoLastSlash(scope_->settings()) + | |
109 GetFileDirWithNoLastSlash())); | |
110 } | |
111 return target_output_dir_.get(); | |
112 } | |
113 | |
114 const Value* ScopePerFileProvider::GetTargetGenDir() { | |
115 if (!target_output_dir_) { | |
116 target_gen_dir_.reset(new Value(NULL, | |
117 GetRootGenDirWithNoLastSlash(scope_->settings()) + | |
118 GetFileDirWithNoLastSlash())); | |
119 } | |
120 return target_gen_dir_.get(); | |
121 } | |
122 | |
123 const Value* ScopePerFileProvider::GetRelativeRootOutputDir() { | |
124 if (!relative_root_output_dir_) { | |
125 relative_root_output_dir_.reset(new Value(NULL, | |
126 GetRelativeRootWithNoLastSlash() + | |
127 GetRootOutputDirWithNoLastSlash(scope_->settings()))); | |
128 } | |
129 return relative_root_output_dir_.get(); | |
130 } | |
131 | |
132 const Value* ScopePerFileProvider::GetRelativeRootGenDir() { | |
133 if (!relative_root_gen_dir_) { | |
134 relative_root_gen_dir_.reset(new Value(NULL, | |
135 GetRelativeRootWithNoLastSlash() + | |
136 GetRootGenDirWithNoLastSlash(scope_->settings()))); | |
137 } | |
138 return relative_root_gen_dir_.get(); | |
139 } | |
140 | |
141 const Value* ScopePerFileProvider::GetRelativeTargetOutputDir() { | |
142 if (!relative_target_output_dir_) { | |
143 relative_target_output_dir_.reset(new Value(NULL, | |
144 GetRelativeRootWithNoLastSlash() + | |
145 GetRootOutputDirWithNoLastSlash(scope_->settings()) + "obj/" + | |
146 GetFileDirWithNoLastSlash())); | |
147 } | |
148 return relative_target_output_dir_.get(); | |
149 } | |
150 | |
151 const Value* ScopePerFileProvider::GetRelativeTargetGenDir() { | |
152 if (!relative_target_gen_dir_) { | |
153 relative_target_gen_dir_.reset(new Value(NULL, | |
154 GetRelativeRootWithNoLastSlash() + | |
155 GetRootGenDirWithNoLastSlash(scope_->settings()) + | |
156 GetFileDirWithNoLastSlash())); | |
157 } | |
158 return relative_target_gen_dir_.get(); | |
159 } | |
160 | |
161 // static | |
162 std::string ScopePerFileProvider::GetRootOutputDirWithNoLastSlash( | |
163 const Settings* settings) { | |
164 const std::string& output_dir = | |
165 settings->build_settings()->build_dir().value(); | |
166 CHECK(!output_dir.empty()); | |
167 return output_dir.substr(0, output_dir.size() - 1); | |
168 } | |
169 | |
170 // static | |
171 std::string ScopePerFileProvider::GetRootGenDirWithNoLastSlash( | |
172 const Settings* settings) { | |
173 return GetRootOutputDirWithNoLastSlash(settings) + "/gen"; | |
174 } | |
175 | |
176 std::string ScopePerFileProvider::GetFileDirWithNoLastSlash() const { | |
177 std::string dir_value = source_file_.GetDir().value(); | |
178 return dir_value.substr(0, dir_value.size() - 1); | |
179 } | |
180 | |
181 std::string ScopePerFileProvider::GetRelativeRootWithNoLastSlash() const { | |
182 std::string inverted = InvertDir(source_file_.GetDir()); | |
183 if (inverted.empty()) | |
184 return "."; | |
185 return inverted.substr(0, inverted.size() - 1); | |
186 } | |
OLD | NEW |