Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(289)

Side by Side Diff: tools/gn/xcode_writer.cc

Issue 2544803002: [GN] Sanitize environment variables when running ninja from Xcode. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "tools/gn/xcode_writer.h" 5 #include "tools/gn/xcode_writer.h"
6 6
7 #include <iomanip> 7 #include <iomanip>
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <sstream> 10 #include <sstream>
(...skipping 13 matching lines...) Expand all
24 #include "tools/gn/filesystem_utils.h" 24 #include "tools/gn/filesystem_utils.h"
25 #include "tools/gn/settings.h" 25 #include "tools/gn/settings.h"
26 #include "tools/gn/source_file.h" 26 #include "tools/gn/source_file.h"
27 #include "tools/gn/target.h" 27 #include "tools/gn/target.h"
28 #include "tools/gn/value.h" 28 #include "tools/gn/value.h"
29 #include "tools/gn/variables.h" 29 #include "tools/gn/variables.h"
30 #include "tools/gn/xcode_object.h" 30 #include "tools/gn/xcode_object.h"
31 31
32 namespace { 32 namespace {
33 33
34 struct SafeEnvironmentVariableInfo {
35 const char* name;
36 bool capture_at_generation;
37 };
38
39 SafeEnvironmentVariableInfo kSafeEnvironmentVariables[] = {
40 {"HOME", true}, {"LANG", true}, {"PATH", true},
Dirk Pranke 2016/12/01 20:53:22 nit: weird indentation.
41 {"USER", true}, {"TMPDIR", false},
42 };
43
34 XcodeWriter::TargetOsType GetTargetOs(const Args& args) { 44 XcodeWriter::TargetOsType GetTargetOs(const Args& args) {
35 const Value* target_os_value = args.GetArgOverride(variables::kTargetOs); 45 const Value* target_os_value = args.GetArgOverride(variables::kTargetOs);
36 if (target_os_value) { 46 if (target_os_value) {
37 if (target_os_value->type() == Value::STRING) { 47 if (target_os_value->type() == Value::STRING) {
38 if (target_os_value->string_value() == "ios") 48 if (target_os_value->string_value() == "ios")
39 return XcodeWriter::WRITER_TARGET_OS_IOS; 49 return XcodeWriter::WRITER_TARGET_OS_IOS;
40 } 50 }
41 } 51 }
42 return XcodeWriter::WRITER_TARGET_OS_MACOS; 52 return XcodeWriter::WRITER_TARGET_OS_MACOS;
43 } 53 }
44 54
45 std::string GetBuildScript(const std::string& target_name, 55 std::string GetBuildScript(const std::string& target_name,
46 const std::string& build_path, 56 const std::string& ninja_extra_args,
47 const std::string& ninja_extra_args) { 57 base::Environment* environment) {
48 std::stringstream script; 58 std::stringstream script;
49 script << "echo note: \"Compile and copy " << target_name << " via ninja\"\n" 59 script << "echo note: \"Compile and copy " << target_name << " via ninja\"\n"
50 << "exec "; 60 << "exec ";
51 if (!build_path.empty()) 61
52 script << "env PATH=\"" << build_path << "\" "; 62 // Launch ninja with a sanitized environment (Xcode sets many environment
63 // variable overridding settings, including the SDK, thus breaking hermetic
64 // build).
65 script << "env -i ";
66 for (size_t i = 0; i < arraysize(kSafeEnvironmentVariables); ++i) {
Dirk Pranke 2016/12/01 20:53:22 Can you replace this with a range-based loop? I.e.
67 const auto& variable = kSafeEnvironmentVariables[i];
68 script << variable.name << "=\"";
69
70 std::string value;
71 if (variable.capture_at_generation)
72 environment->GetVar(variable.name, &value);
73
74 if (!value.empty())
75 script << value;
76 else
77 script << "$" << variable.name;
78 script << "\" ";
79 }
80
53 script << "ninja -C ."; 81 script << "ninja -C .";
54 if (!ninja_extra_args.empty()) 82 if (!ninja_extra_args.empty())
55 script << " " << ninja_extra_args; 83 script << " " << ninja_extra_args;
56 if (!target_name.empty()) 84 if (!target_name.empty())
57 script << " " << target_name; 85 script << " " << target_name;
58 script << "\nexit 1\n"; 86 script << "\nexit 1\n";
59 return script.str(); 87 return script.str();
60 } 88 }
61 89
62 class CollectPBXObjectsPerClassHelper : public PBXObjectVisitor { 90 class CollectPBXObjectsPerClassHelper : public PBXObjectVisitor {
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 const std::string& config_name, 274 const std::string& config_name,
247 const std::string& root_target, 275 const std::string& root_target,
248 const std::string& ninja_extra_args, 276 const std::string& ninja_extra_args,
249 const BuildSettings* build_settings, 277 const BuildSettings* build_settings,
250 TargetOsType target_os) { 278 TargetOsType target_os) {
251 std::unique_ptr<PBXProject> main_project( 279 std::unique_ptr<PBXProject> main_project(
252 new PBXProject("products", config_name, source_path, attributes)); 280 new PBXProject("products", config_name, source_path, attributes));
253 281
254 std::string build_path; 282 std::string build_path;
255 std::unique_ptr<base::Environment> env(base::Environment::Create()); 283 std::unique_ptr<base::Environment> env(base::Environment::Create());
256 env->GetVar("PATH", &build_path);
257 284
258 main_project->AddAggregateTarget( 285 main_project->AddAggregateTarget(
259 "All", GetBuildScript(root_target, build_path, ninja_extra_args)); 286 "All", GetBuildScript(root_target, ninja_extra_args, env.get()));
260 287
261 for (const Target* target : targets) { 288 for (const Target* target : targets) {
262 switch (target->output_type()) { 289 switch (target->output_type()) {
263 case Target::EXECUTABLE: 290 case Target::EXECUTABLE:
264 if (target_os == XcodeWriter::WRITER_TARGET_OS_IOS) 291 if (target_os == XcodeWriter::WRITER_TARGET_OS_IOS)
265 continue; 292 continue;
266 293
267 main_project->AddNativeTarget( 294 main_project->AddNativeTarget(
268 target->label().name(), "compiled.mach-o.executable", 295 target->label().name(), "compiled.mach-o.executable",
269 target->output_name().empty() ? target->label().name() 296 target->output_name().empty() ? target->label().name()
270 : target->output_name(), 297 : target->output_name(),
271 "com.apple.product-type.tool", 298 "com.apple.product-type.tool",
272 GetBuildScript(target->label().name(), build_path, 299 GetBuildScript(target->label().name(), ninja_extra_args,
273 ninja_extra_args)); 300 env.get()));
274 break; 301 break;
275 302
276 case Target::CREATE_BUNDLE: 303 case Target::CREATE_BUNDLE:
277 if (target->bundle_data().product_type().empty()) 304 if (target->bundle_data().product_type().empty())
278 continue; 305 continue;
279 306
280 main_project->AddNativeTarget( 307 main_project->AddNativeTarget(
281 target->label().name(), std::string(), 308 target->label().name(), std::string(),
282 RebasePath(target->bundle_data() 309 RebasePath(target->bundle_data()
283 .GetBundleRootDirOutput(target->settings()) 310 .GetBundleRootDirOutput(target->settings())
284 .value(), 311 .value(),
285 build_settings->build_dir()), 312 build_settings->build_dir()),
286 target->bundle_data().product_type(), 313 target->bundle_data().product_type(),
287 GetBuildScript(target->label().name(), build_path, 314 GetBuildScript(target->label().name(), ninja_extra_args,
288 ninja_extra_args)); 315 env.get()));
289 break; 316 break;
290 317
291 default: 318 default:
292 break; 319 break;
293 } 320 }
294 } 321 }
295 322
296 projects_.push_back(std::move(main_project)); 323 projects_.push_back(std::move(main_project));
297 } 324 }
298 325
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 for (auto* object : pair.second) { 428 for (auto* object : pair.second) {
402 object->Print(out, 2); 429 object->Print(out, 2);
403 } 430 }
404 out << "/* End " << ToString(pair.first) << " section */\n"; 431 out << "/* End " << ToString(pair.first) << " section */\n";
405 } 432 }
406 433
407 out << "\t};\n" 434 out << "\t};\n"
408 << "\trootObject = " << project->Reference() << ";\n" 435 << "\trootObject = " << project->Reference() << ";\n"
409 << "}\n"; 436 << "}\n";
410 } 437 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698