Chromium Code Reviews| Index: tools/gn/setup.cc |
| diff --git a/tools/gn/setup.cc b/tools/gn/setup.cc |
| index 033dab4fa15c1fc844e7be54834b852ec8f8b25e..adc07dd7ef767fad1edc6fbf380694b3c35acf73 100644 |
| --- a/tools/gn/setup.cc |
| +++ b/tools/gn/setup.cc |
| @@ -148,6 +148,39 @@ void DecrementWorkCount() { |
| g_scheduler->DecrementWorkCount(); |
| } |
| +#if defined(OS_WIN) |
| +base::FilePath FindWindowsPython() { |
| + const base::char16 kPythonExeName[] = L"python.exe"; |
|
scottmg
2015/11/21 00:23:34
Could hoist this out so that the failure case can
|
| + base::char16 current_directory[MAX_PATH]; |
| + ::GetCurrentDirectory(MAX_PATH, current_directory); |
| + |
| + // First search for python.exe in the current directory. |
| + base::FilePath cur_dir_candidate_exe = |
| + base::FilePath(current_directory).Append(kPythonExeName); |
| + if (base::PathExists(cur_dir_candidate_exe)) |
| + return cur_dir_candidate_exe; |
| + |
| + // Get the path. |
| + const base::char16 kPathEnvVarName[] = L"Path"; |
| + DWORD path_length = ::GetEnvironmentVariable(kPathEnvVarName, NULL, 0); |
|
scottmg
2015/11/21 00:23:34
nullptr
|
| + if (path_length == 0) |
| + return base::FilePath(); |
| + scoped_ptr<base::char16[]> full_path(new base::char16[path_length]); |
| + ::GetEnvironmentVariable(kPathEnvVarName, full_path.get(), path_length); |
|
scottmg
2015/11/21 00:23:34
DCHECK return value == path_length - 1 maybe.
|
| + |
| + // Search for python.exe in the path. |
| + for (const auto& component : base::SplitStringPiece( |
| + base::StringPiece16(full_path.get(), path_length), L";", |
| + base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) { |
| + base::FilePath candidate_exe = |
| + base::FilePath(component).Append(kPythonExeName); |
| + if (base::PathExists(candidate_exe)) |
| + return candidate_exe; |
| + } |
| + return base::FilePath(); |
| +} |
| +#endif |
| + |
| } // namespace |
| const char Setup::kBuildArgFileName[] = "args.gn"; |
| @@ -478,21 +511,13 @@ void Setup::FillPythonPath() { |
| // Trace this since it tends to be a bit slow on Windows. |
| ScopedTrace setup_trace(TraceItem::TRACE_SETUP, "Fill Python Path"); |
| #if defined(OS_WIN) |
| - // Find Python on the path so we can use the absolute path in the build. |
| - const base::char16 kGetPython[] = |
| - L"cmd.exe /c python -c \"import sys; print sys.executable\""; |
| - std::string python_path; |
| - if (base::GetAppOutput(kGetPython, &python_path)) { |
| - base::TrimWhitespaceASCII(python_path, base::TRIM_ALL, &python_path); |
| - if (scheduler_.verbose_logging()) |
| - scheduler_.Log("Found python", python_path); |
| - } else { |
| + base::FilePath python_path = FindWindowsPython(); |
| + if (python_path.empty()) { |
| scheduler_.Log("WARNING", "Could not find python on path, using " |
| "just \"python.exe\""); |
| - python_path = "python.exe"; |
| + python_path = base::FilePath(L"python.exe"); |
| } |
| - build_settings_.set_python_path(base::FilePath(base::UTF8ToUTF16(python_path)) |
| - .NormalizePathSeparatorsTo('/')); |
| + build_settings_.set_python_path(python_path.NormalizePathSeparatorsTo('/')); |
| #else |
| build_settings_.set_python_path(base::FilePath("python")); |
| #endif |