blob: 95e7585ec5c82df93b97fe6436945c56301f8d81 [file] [log] [blame]
jochen@chromium.org48995032012-02-04 08:10:04 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
mmentovai@google.comaa13be62008-09-03 03:20:34 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
wjia@chromium.org9d594d12012-09-20 10:59:36 +09005#include "base/base_paths.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +09006
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +09007#include <ostream>
8#include <string>
mmentovai@google.comaa13be62008-09-03 03:20:34 +09009
wjia@chromium.org9d594d12012-09-20 10:59:36 +090010#include "build/build_config.h"
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +090011#include "base/environment.h"
evanm@google.com874d1672008-10-31 08:54:04 +090012#include "base/file_path.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090013#include "base/file_util.h"
14#include "base/logging.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090015#include "base/memory/scoped_ptr.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090016#include "base/path_service.h"
thestig@chromium.org0c1da822012-09-15 19:51:05 +090017#include "base/process_util.h"
brettw@chromium.orge47345a2010-10-16 13:56:06 +090018#include "base/nix/xdg_util.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090019
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090020#if defined(OS_FREEBSD)
21#include <sys/param.h>
22#include <sys/sysctl.h>
chromium@hybridsource.org8f85a6a2011-06-25 13:54:41 +090023#elif defined(OS_SOLARIS)
24#include <stdlib.h>
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090025#endif
26
mmentovai@google.comaa13be62008-09-03 03:20:34 +090027namespace base {
28
evan@chromium.orgc1365092009-11-21 10:29:00 +090029bool PathProviderPosix(int key, FilePath* result) {
evanm@google.com874d1672008-10-31 08:54:04 +090030 FilePath path;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090031 switch (key) {
32 case base::FILE_EXE:
wtc@chromium.org9b07a8f2009-09-01 07:25:00 +090033 case base::FILE_MODULE: { // TODO(evanm): is this correct?
pvalchev@google.comcba6a202010-05-11 01:30:27 +090034#if defined(OS_LINUX)
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +090035 FilePath bin_dir;
thestig@chromium.org0c1da822012-09-15 19:51:05 +090036 if (!file_util::ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) {
37 NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
mmentovai@google.comaa13be62008-09-03 03:20:34 +090038 return false;
39 }
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +090040 *result = bin_dir;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090041 return true;
pvalchev@google.comcba6a202010-05-11 01:30:27 +090042#elif defined(OS_FREEBSD)
43 int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
44 char bin_dir[PATH_MAX + 1];
45 size_t length = sizeof(bin_dir);
grt@chromium.orgb3b798c2011-10-26 01:22:27 +090046 // Upon return, |length| is the number of bytes written to |bin_dir|
47 // including the string terminator.
pvalchev@google.comcba6a202010-05-11 01:30:27 +090048 int error = sysctl(name, 4, bin_dir, &length, NULL, 0);
grt@chromium.orgb3b798c2011-10-26 01:22:27 +090049 if (error < 0 || length <= 1) {
pvalchev@google.comcba6a202010-05-11 01:30:27 +090050 NOTREACHED() << "Unable to resolve path.";
51 return false;
52 }
grt@chromium.orgb3b798c2011-10-26 01:22:27 +090053 *result = FilePath(FilePath::StringType(bin_dir, length - 1));
pvalchev@google.comcba6a202010-05-11 01:30:27 +090054 return true;
chromium@hybridsource.org8f85a6a2011-06-25 13:54:41 +090055#elif defined(OS_SOLARIS)
56 char bin_dir[PATH_MAX + 1];
57 if (realpath(getexecname(), bin_dir) == NULL) {
58 NOTREACHED() << "Unable to resolve " << getexecname() << ".";
59 return false;
60 }
61 *result = FilePath(bin_dir);
62 return true;
mark@chromium.org2e9e81c2011-10-13 13:23:22 +090063#elif defined(OS_OPENBSD)
64 // There is currently no way to get the executable path on OpenBSD
thestig@chromium.org0c1da822012-09-15 19:51:05 +090065 char* cpath;
robert.nagy@gmail.comdd706e92011-10-26 02:43:05 +090066 if ((cpath = getenv("CHROME_EXE_PATH")) != NULL)
67 *result = FilePath(cpath);
68 else
69 *result = FilePath("/usr/local/chrome/chrome");
mark@chromium.org2e9e81c2011-10-13 13:23:22 +090070 return true;
pvalchev@google.comcba6a202010-05-11 01:30:27 +090071#endif
mmentovai@google.comaa13be62008-09-03 03:20:34 +090072 }
mmoss@google.com9d288752010-04-22 08:28:43 +090073 case base::DIR_SOURCE_ROOT: {
maruel@chromium.org89f74e22012-07-27 05:53:56 +090074 // Allow passing this in the environment, for more flexibility in build
75 // tree configurations (sub-project builds, gyp --output_dir, etc.)
76 scoped_ptr<base::Environment> env(base::Environment::Create());
77 std::string cr_source_root;
78 if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) {
79 path = FilePath(cr_source_root);
80 if (file_util::PathExists(path)) {
81 *result = path;
82 return true;
83 } else {
84 DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not "
85 << "point to a directory.";
86 }
87 }
evan@chromium.orgc1365092009-11-21 10:29:00 +090088 // On POSIX, unit tests execute two levels deep from the source root.
evan@chromium.orgb33d2542010-12-03 09:59:23 +090089 // For example: out/{Debug|Release}/net_unittest
mattm@chromium.org1ed95d52009-09-11 04:30:46 +090090 if (PathService::Get(base::DIR_EXE, &path)) {
jochen@chromium.org48995032012-02-04 08:10:04 +090091 *result = path.DirName().DirName();
mattm@chromium.org1ed95d52009-09-11 04:30:46 +090092 return true;
93 }
jochen@chromium.org48995032012-02-04 08:10:04 +090094
brettw@chromium.org5faed3c2011-10-27 06:48:00 +090095 DLOG(ERROR) << "Couldn't find your source root. "
96 << "Try running from your chromium/src directory.";
mattm@chromium.org1ed95d52009-09-11 04:30:46 +090097 return false;
mmoss@google.com9d288752010-04-22 08:28:43 +090098 }
thorogood@chromium.orge77009d2012-07-23 17:22:44 +090099 case base::DIR_CACHE: {
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +0900100 scoped_ptr<base::Environment> env(base::Environment::Create());
brettw@chromium.orge47345a2010-10-16 13:56:06 +0900101 FilePath cache_dir(base::nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME",
102 ".cache"));
thestig@chromium.orgeea83062009-12-02 17:45:01 +0900103 *result = cache_dir;
104 return true;
thorogood@chromium.orge77009d2012-07-23 17:22:44 +0900105 }
wjia@chromium.org9d594d12012-09-20 10:59:36 +0900106 case base::DIR_HOME: {
thorogood@chromium.orge77009d2012-07-23 17:22:44 +0900107 *result = file_util::GetHomeDir();
108 return true;
wjia@chromium.org9d594d12012-09-20 10:59:36 +0900109 }
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900110 }
111 return false;
112}
113
114} // namespace base