blob: a1c45a03df1c470bd15a9af31044ac4241780bc4 [file] [log] [blame]
[email protected]569edabd2012-02-03 23:10:041// Copyright (c) 2012 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]5d1937bb2009-11-21 01:29:0032bool PathProviderPosix(int key, FilePath* result) {
[email protected]640517f2008-10-30 23:54:0433 FilePath path;
[email protected]b2e97292008-09-02 18:20:3434 switch (key) {
35 case base::FILE_EXE:
[email protected]4a34ce02009-08-31 22:25:0036 case base::FILE_MODULE: { // TODO(evanm): is this correct?
[email protected]99aae102010-05-10 16:30:2737#if defined(OS_LINUX)
[email protected]723571a2010-12-03 17:37:5438 FilePath bin_dir;
39 if (!file_util::ReadSymbolicLink(FilePath(kSelfExe), &bin_dir)) {
[email protected]4a34ce02009-08-31 22:25:0040 NOTREACHED() << "Unable to resolve " << kSelfExe << ".";
[email protected]b2e97292008-09-02 18:20:3441 return false;
42 }
[email protected]723571a2010-12-03 17:37:5443 *result = bin_dir;
[email protected]b2e97292008-09-02 18:20:3444 return true;
[email protected]99aae102010-05-10 16:30:2745#elif defined(OS_FREEBSD)
46 int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
47 char bin_dir[PATH_MAX + 1];
48 size_t length = sizeof(bin_dir);
[email protected]72d7b962011-10-25 16:22:2749 // Upon return, |length| is the number of bytes written to |bin_dir|
50 // including the string terminator.
[email protected]99aae102010-05-10 16:30:2751 int error = sysctl(name, 4, bin_dir, &length, NULL, 0);
[email protected]72d7b962011-10-25 16:22:2752 if (error < 0 || length <= 1) {
[email protected]99aae102010-05-10 16:30:2753 NOTREACHED() << "Unable to resolve path.";
54 return false;
55 }
[email protected]72d7b962011-10-25 16:22:2756 *result = FilePath(FilePath::StringType(bin_dir, length - 1));
[email protected]99aae102010-05-10 16:30:2757 return true;
[email protected]94f8c952011-06-25 04:54:4158#elif defined(OS_SOLARIS)
59 char bin_dir[PATH_MAX + 1];
60 if (realpath(getexecname(), bin_dir) == NULL) {
61 NOTREACHED() << "Unable to resolve " << getexecname() << ".";
62 return false;
63 }
64 *result = FilePath(bin_dir);
65 return true;
[email protected]817f0f142011-10-13 04:23:2266#elif defined(OS_OPENBSD)
67 // There is currently no way to get the executable path on OpenBSD
[email protected]ea725b32011-10-25 17:43:0568 char *cpath;
69 if ((cpath = getenv("CHROME_EXE_PATH")) != NULL)
70 *result = FilePath(cpath);
71 else
72 *result = FilePath("/usr/local/chrome/chrome");
[email protected]817f0f142011-10-13 04:23:2273 return true;
[email protected]99aae102010-05-10 16:30:2774#endif
[email protected]b2e97292008-09-02 18:20:3475 }
[email protected]632be2f2010-04-21 23:28:4376 case base::DIR_SOURCE_ROOT: {
77 // Allow passing this in the environment, for more flexibility in build
78 // tree configurations (sub-project builds, gyp --output_dir, etc.)
[email protected]76b90d312010-08-03 03:00:5079 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]632be2f2010-04-21 23:28:4380 std::string cr_source_root;
[email protected]3ba7e082010-08-07 02:57:5981 if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) {
[email protected]632be2f2010-04-21 23:28:4382 path = FilePath(cr_source_root);
[email protected]569edabd2012-02-03 23:10:0483 if (file_util::PathExists(path)) {
[email protected]632be2f2010-04-21 23:28:4384 *result = path;
85 return true;
86 } else {
[email protected]a42d4632011-10-26 21:48:0087 DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not "
[email protected]569edabd2012-02-03 23:10:0488 << "point to a directory.";
[email protected]632be2f2010-04-21 23:28:4389 }
90 }
[email protected]5d1937bb2009-11-21 01:29:0091 // On POSIX, unit tests execute two levels deep from the source root.
[email protected]016498e2010-12-03 00:59:2392 // For example: out/{Debug|Release}/net_unittest
[email protected]95d050e2009-09-10 19:30:4693 if (PathService::Get(base::DIR_EXE, &path)) {
[email protected]569edabd2012-02-03 23:10:0494 *result = path.DirName().DirName();
[email protected]95d050e2009-09-10 19:30:4695 return true;
96 }
[email protected]569edabd2012-02-03 23:10:0497
[email protected]a42d4632011-10-26 21:48:0098 DLOG(ERROR) << "Couldn't find your source root. "
99 << "Try running from your chromium/src directory.";
[email protected]95d050e2009-09-10 19:30:46100 return false;
[email protected]632be2f2010-04-21 23:28:43101 }
[email protected]b411da32010-11-24 02:23:15102 case base::DIR_CACHE:
[email protected]76b90d312010-08-03 03:00:50103 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]6b0349ef2010-10-16 04:56:06104 FilePath cache_dir(base::nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME",
105 ".cache"));
[email protected]9e9b6e8e2009-12-02 08:45:01106 *result = cache_dir;
107 return true;
[email protected]b2e97292008-09-02 18:20:34108 }
109 return false;
110}
111
112} // namespace base