blob: 9f940cb986cb4d6145401c1d3c0a74dbf380e2fe [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]b2e97292008-09-02 18:20:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]5d1937bb2009-11-21 01:29:005#include "base/base_paths.h"
[email protected]b2e97292008-09-02 18:20:346
[email protected]2edc2862011-04-04 18:04:377#include <ostream>
8#include <string>
[email protected]b2e97292008-09-02 18:20:349
[email protected]2edc2862011-04-04 18:04:3710#include "build/build_config.h"
[email protected]76b90d312010-08-03 03:00:5011#include "base/environment.h"
[email protected]640517f2008-10-30 23:54:0412#include "base/file_path.h"
[email protected]b2e97292008-09-02 18:20:3413#include "base/file_util.h"
14#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/scoped_ptr.h"
[email protected]b2e97292008-09-02 18:20:3416#include "base/path_service.h"
[email protected]6b0349ef2010-10-16 04:56:0617#include "base/nix/xdg_util.h"
[email protected]b2e97292008-09-02 18:20:3418
[email protected]2edc2862011-04-04 18:04:3719#if defined(OS_FREEBSD)
20#include <sys/param.h>
21#include <sys/sysctl.h>
[email protected]94f8c952011-06-25 04:54:4122#elif defined(OS_SOLARIS)
23#include <stdlib.h>
[email protected]2edc2862011-04-04 18:04:3724#endif
25
[email protected]b2e97292008-09-02 18:20:3426namespace base {
27
[email protected]4a34ce02009-08-31 22:25:0028#if defined(OS_LINUX)
29const char kSelfExe[] = "/proc/self/exe";
[email protected]4a34ce02009-08-31 22:25:0030#endif
31
[email protected]6b0349ef2010-10-16 04:56:0632// 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
[email protected]5d1937bb2009-11-21 01:29:0036bool PathProviderPosix(int key, FilePath* result) {
[email protected]640517f2008-10-30 23:54:0437 FilePath path;
[email protected]b2e97292008-09-02 18:20:3438 switch (key) {
39 case base::FILE_EXE:
[email protected]4a34ce02009-08-31 22:25:0040 case base::FILE_MODULE: { // TODO(evanm): is this correct?
[email protected]99aae102010-05-10 16:30:2741#if defined(OS_LINUX)
[email protected]723571a2010-12-03 17:37:5442 FilePath bin_dir;
43 if (!file_util::ReadSymbolicLink(FilePath(kSelfExe), &bin_dir)) {
[email protected]4a34ce02009-08-31 22:25:0044 NOTREACHED() << "Unable to resolve " << kSelfExe << ".";
[email protected]b2e97292008-09-02 18:20:3445 return false;
46 }
[email protected]723571a2010-12-03 17:37:5447 *result = bin_dir;
[email protected]b2e97292008-09-02 18:20:3448 return true;
[email protected]99aae102010-05-10 16:30:2749#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;
[email protected]94f8c952011-06-25 04:54:4161#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;
[email protected]99aae102010-05-10 16:30:2769#endif
[email protected]b2e97292008-09-02 18:20:3470 }
[email protected]632be2f2010-04-21 23:28:4371 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.)
[email protected]76b90d312010-08-03 03:00:5074 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]632be2f2010-04-21 23:28:4375 std::string cr_source_root;
[email protected]3ba7e082010-08-07 02:57:5976 if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) {
[email protected]632be2f2010-04-21 23:28:4377 path = FilePath(cr_source_root);
[email protected]6b0349ef2010-10-16 04:56:0678 if (file_util::PathExists(path.Append(kThisSourceFile))) {
[email protected]632be2f2010-04-21 23:28:4379 *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 }
[email protected]5d1937bb2009-11-21 01:29:0086 // On POSIX, unit tests execute two levels deep from the source root.
[email protected]016498e2010-12-03 00:59:2387 // For example: out/{Debug|Release}/net_unittest
[email protected]95d050e2009-09-10 19:30:4688 if (PathService::Get(base::DIR_EXE, &path)) {
89 path = path.DirName().DirName();
[email protected]6b0349ef2010-10-16 04:56:0690 if (file_util::PathExists(path.Append(kThisSourceFile))) {
[email protected]95d050e2009-09-10 19:30:4691 *result = path;
92 return true;
93 }
94 }
[email protected]14a25e502010-06-15 06:53:5295 // In a case of WebKit-only checkout, executable files are put into
[email protected]53a1b662011-02-01 23:18:5496 // <root of checkout>/out/{Debug|Release}, and we should return
97 // <root of checkout>/Source/WebKit/chromium for DIR_SOURCE_ROOT.
[email protected]14a25e502010-06-15 06:53:5298 if (PathService::Get(base::DIR_EXE, &path)) {
[email protected]53a1b662011-02-01 23:18:5499 path = path.DirName().DirName().Append("Source/WebKit/chromium");
[email protected]6b0349ef2010-10-16 04:56:06100 if (file_util::PathExists(path.Append(kThisSourceFile))) {
[email protected]14a25e502010-06-15 06:53:52101 *result = path;
102 return true;
103 }
104 }
[email protected]95d050e2009-09-10 19:30:46105 // 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) &&
[email protected]6b0349ef2010-10-16 04:56:06108 file_util::PathExists(path.Append(kThisSourceFile))) {
[email protected]95d050e2009-09-10 19:30:46109 *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;
[email protected]632be2f2010-04-21 23:28:43115 }
[email protected]b411da32010-11-24 02:23:15116 case base::DIR_CACHE:
[email protected]76b90d312010-08-03 03:00:50117 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]6b0349ef2010-10-16 04:56:06118 FilePath cache_dir(base::nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME",
119 ".cache"));
[email protected]9e9b6e8e2009-12-02 08:45:01120 *result = cache_dir;
121 return true;
[email protected]b2e97292008-09-02 18:20:34122 }
123 return false;
124}
125
126} // namespace base