1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Defines base::PathProviderPosix, default path provider on POSIX OSes that
6 // don't have their own base_paths_OS.cc implementation (i.e. all but Mac and
9 #include "base/base_paths.h"
18 #include "base/environment.h"
19 #include "base/files/file_path.h"
20 #include "base/files/file_util.h"
21 #include "base/logging.h"
22 #include "base/nix/xdg_util.h"
23 #include "base/notreached.h"
24 #include "base/path_service.h"
25 #include "base/posix/sysctl.h"
26 #include "base/process/process_metrics.h"
27 #include "build/build_config.h"
29 #if BUILDFLAG(IS_FREEBSD)
30 #include <sys/param.h>
31 #include <sys/sysctl.h>
32 #elif BUILDFLAG(IS_SOLARIS) || BUILDFLAG(IS_AIX)
38 bool PathProviderPosix(int key, FilePath* result) {
41 case FILE_MODULE: { // TODO(evanm): is this correct?
42 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
44 if (!ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) {
45 NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
50 #elif BUILDFLAG(IS_FREEBSD)
51 int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
52 absl::optional<std::string> bin_dir = StringSysctl(name, std::size(name));
53 if (!bin_dir.has_value() || bin_dir.value().length() <= 1) {
54 NOTREACHED() << "Unable to resolve path.";
57 *result = FilePath(bin_dir.value());
59 #elif BUILDFLAG(IS_SOLARIS)
60 char bin_dir[PATH_MAX + 1];
61 if (realpath(getexecname(), bin_dir) == NULL) {
62 NOTREACHED() << "Unable to resolve " << getexecname() << ".";
65 *result = FilePath(bin_dir);
67 #elif BUILDFLAG(IS_OPENBSD) || BUILDFLAG(IS_AIX)
68 // There is currently no way to get the executable path on OpenBSD
70 if ((cpath = getenv("CHROME_EXE_PATH")) != NULL)
71 *result = FilePath(cpath);
73 *result = FilePath("/usr/local/chrome/chrome");
77 case DIR_SRC_TEST_DATA_ROOT: {
78 // Allow passing this in the environment, for more flexibility in build
79 // tree configurations (sub-project builds, gyp --output_dir, etc.)
80 std::unique_ptr<Environment> env(Environment::Create());
81 std::string cr_source_root;
83 if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) {
84 path = FilePath(cr_source_root);
85 if (PathExists(path)) {
89 DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not "
90 << "point to a directory.";
92 // On POSIX, unit tests execute two levels deep from the source root.
93 // For example: out/{Debug|Release}/net_unittest
94 if (PathService::Get(DIR_EXE, &path)) {
95 *result = path.DirName().DirName();
99 DLOG(ERROR) << "Couldn't find your source root. "
100 << "Try running from your chromium/src directory.";
103 case DIR_USER_DESKTOP:
104 *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop");
107 std::unique_ptr<Environment> env(Environment::Create());
109 nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", ".cache"));