blob: 9f940cb986cb4d6145401c1d3c0a74dbf380e2fe [file] [log] [blame]
levin@chromium.org5c528682011-03-28 10:54:15 +09001// Copyright (c) 2011 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
evan@chromium.orgc1365092009-11-21 10:29:00 +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
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +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"
brettw@chromium.orge47345a2010-10-16 13:56:06 +090017#include "base/nix/xdg_util.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090018
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090019#if defined(OS_FREEBSD)
20#include <sys/param.h>
21#include <sys/sysctl.h>
chromium@hybridsource.org8f85a6a2011-06-25 13:54:41 +090022#elif defined(OS_SOLARIS)
23#include <stdlib.h>
jhawkins@chromium.org8e73d062011-04-05 03:04:37 +090024#endif
25
mmentovai@google.comaa13be62008-09-03 03:20:34 +090026namespace base {
27
wtc@chromium.org9b07a8f2009-09-01 07:25:00 +090028#if defined(OS_LINUX)
29const char kSelfExe[] = "/proc/self/exe";
wtc@chromium.org9b07a8f2009-09-01 07:25:00 +090030#endif
31
brettw@chromium.orge47345a2010-10-16 13:56:06 +090032// The name of this file relative to the source root. This is used for checking
33// that the source checkout is in the correct place.
34static const char kThisSourceFile[] = "base/base_paths_linux.cc";
35
evan@chromium.orgc1365092009-11-21 10:29:00 +090036bool PathProviderPosix(int key, FilePath* result) {
evanm@google.com874d1672008-10-31 08:54:04 +090037 FilePath path;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090038 switch (key) {
39 case base::FILE_EXE:
wtc@chromium.org9b07a8f2009-09-01 07:25:00 +090040 case base::FILE_MODULE: { // TODO(evanm): is this correct?
pvalchev@google.comcba6a202010-05-11 01:30:27 +090041#if defined(OS_LINUX)
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +090042 FilePath bin_dir;
43 if (!file_util::ReadSymbolicLink(FilePath(kSelfExe), &bin_dir)) {
wtc@chromium.org9b07a8f2009-09-01 07:25:00 +090044 NOTREACHED() << "Unable to resolve " << kSelfExe << ".";
mmentovai@google.comaa13be62008-09-03 03:20:34 +090045 return false;
46 }
gspencer@chromium.org3c6690c2010-12-04 02:37:54 +090047 *result = bin_dir;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090048 return true;
pvalchev@google.comcba6a202010-05-11 01:30:27 +090049#elif defined(OS_FREEBSD)
50 int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
51 char bin_dir[PATH_MAX + 1];
52 size_t length = sizeof(bin_dir);
53 int error = sysctl(name, 4, bin_dir, &length, NULL, 0);
54 if (error < 0 || length == 0 || strlen(bin_dir) == 0) {
55 NOTREACHED() << "Unable to resolve path.";
56 return false;
57 }
58 bin_dir[strlen(bin_dir)] = 0;
59 *result = FilePath(bin_dir);
60 return true;
chromium@hybridsource.org8f85a6a2011-06-25 13:54:41 +090061#elif defined(OS_SOLARIS)
62 char bin_dir[PATH_MAX + 1];
63 if (realpath(getexecname(), bin_dir) == NULL) {
64 NOTREACHED() << "Unable to resolve " << getexecname() << ".";
65 return false;
66 }
67 *result = FilePath(bin_dir);
68 return true;
pvalchev@google.comcba6a202010-05-11 01:30:27 +090069#endif
mmentovai@google.comaa13be62008-09-03 03:20:34 +090070 }
mmoss@google.com9d288752010-04-22 08:28:43 +090071 case base::DIR_SOURCE_ROOT: {
72 // Allow passing this in the environment, for more flexibility in build
73 // tree configurations (sub-project builds, gyp --output_dir, etc.)
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +090074 scoped_ptr<base::Environment> env(base::Environment::Create());
mmoss@google.com9d288752010-04-22 08:28:43 +090075 std::string cr_source_root;
tfarina@chromium.org8f115a82010-08-07 11:57:59 +090076 if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) {
mmoss@google.com9d288752010-04-22 08:28:43 +090077 path = FilePath(cr_source_root);
brettw@chromium.orge47345a2010-10-16 13:56:06 +090078 if (file_util::PathExists(path.Append(kThisSourceFile))) {
mmoss@google.com9d288752010-04-22 08:28:43 +090079 *result = path;
80 return true;
81 } else {
82 LOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not "
83 << "point to the correct source root directory.";
84 }
85 }
evan@chromium.orgc1365092009-11-21 10:29:00 +090086 // On POSIX, unit tests execute two levels deep from the source root.
evan@chromium.orgb33d2542010-12-03 09:59:23 +090087 // For example: out/{Debug|Release}/net_unittest
mattm@chromium.org1ed95d52009-09-11 04:30:46 +090088 if (PathService::Get(base::DIR_EXE, &path)) {
89 path = path.DirName().DirName();
brettw@chromium.orge47345a2010-10-16 13:56:06 +090090 if (file_util::PathExists(path.Append(kThisSourceFile))) {
mattm@chromium.org1ed95d52009-09-11 04:30:46 +090091 *result = path;
92 return true;
93 }
94 }
tkent@chromium.org57f11212010-06-15 15:53:52 +090095 // In a case of WebKit-only checkout, executable files are put into
jamesr@chromium.org8fba15c2011-02-02 08:18:54 +090096 // <root of checkout>/out/{Debug|Release}, and we should return
97 // <root of checkout>/Source/WebKit/chromium for DIR_SOURCE_ROOT.
tkent@chromium.org57f11212010-06-15 15:53:52 +090098 if (PathService::Get(base::DIR_EXE, &path)) {
jamesr@chromium.org8fba15c2011-02-02 08:18:54 +090099 path = path.DirName().DirName().Append("Source/WebKit/chromium");
brettw@chromium.orge47345a2010-10-16 13:56:06 +0900100 if (file_util::PathExists(path.Append(kThisSourceFile))) {
tkent@chromium.org57f11212010-06-15 15:53:52 +0900101 *result = path;
102 return true;
103 }
104 }
mattm@chromium.org1ed95d52009-09-11 04:30:46 +0900105 // If that failed (maybe the build output is symlinked to a different
106 // drive) try assuming the current directory is the source root.
107 if (file_util::GetCurrentDirectory(&path) &&
brettw@chromium.orge47345a2010-10-16 13:56:06 +0900108 file_util::PathExists(path.Append(kThisSourceFile))) {
mattm@chromium.org1ed95d52009-09-11 04:30:46 +0900109 *result = path;
110 return true;
111 }
112 LOG(ERROR) << "Couldn't find your source root. "
113 << "Try running from your chromium/src directory.";
114 return false;
mmoss@google.com9d288752010-04-22 08:28:43 +0900115 }
viettrungluu@chromium.org366266c2010-11-24 11:23:15 +0900116 case base::DIR_CACHE:
tfarina@chromium.org6d36c5d2010-08-03 12:00:50 +0900117 scoped_ptr<base::Environment> env(base::Environment::Create());
brettw@chromium.orge47345a2010-10-16 13:56:06 +0900118 FilePath cache_dir(base::nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME",
119 ".cache"));
thestig@chromium.orgeea83062009-12-02 17:45:01 +0900120 *result = cache_dir;
121 return true;
mmentovai@google.comaa13be62008-09-03 03:20:34 +0900122 }
123 return false;
124}
125
126} // namespace base