blob: 95e7585ec5c82df93b97fe6436945c56301f8d81 [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]094797b72012-09-15 10:51:0517#include "base/process_util.h"
[email protected]6b0349ef2010-10-16 04:56:0618#include "base/nix/xdg_util.h"
[email protected]b2e97292008-09-02 18:20:3419
[email protected]2edc2862011-04-04 18:04:3720#if defined(OS_FREEBSD)
21#include <sys/param.h>
22#include <sys/sysctl.h>
[email protected]94f8c952011-06-25 04:54:4123#elif defined(OS_SOLARIS)
24#include <stdlib.h>
[email protected]2edc2862011-04-04 18:04:3725#endif
26
[email protected]b2e97292008-09-02 18:20:3427namespace base {
28
[email protected]5d1937bb2009-11-21 01:29:0029bool PathProviderPosix(int key, FilePath* result) {
[email protected]640517f2008-10-30 23:54:0430 FilePath path;
[email protected]b2e97292008-09-02 18:20:3431 switch (key) {
32 case base::FILE_EXE:
[email protected]4a34ce02009-08-31 22:25:0033 case base::FILE_MODULE: { // TODO(evanm): is this correct?
[email protected]99aae102010-05-10 16:30:2734#if defined(OS_LINUX)
[email protected]723571a2010-12-03 17:37:5435 FilePath bin_dir;
[email protected]094797b72012-09-15 10:51:0536 if (!file_util::ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) {
37 NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
[email protected]b2e97292008-09-02 18:20:3438 return false;
39 }
[email protected]723571a2010-12-03 17:37:5440 *result = bin_dir;
[email protected]b2e97292008-09-02 18:20:3441 return true;
[email protected]99aae102010-05-10 16:30:2742#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);
[email protected]72d7b962011-10-25 16:22:2746 // Upon return, |length| is the number of bytes written to |bin_dir|
47 // including the string terminator.
[email protected]99aae102010-05-10 16:30:2748 int error = sysctl(name, 4, bin_dir, &length, NULL, 0);
[email protected]72d7b962011-10-25 16:22:2749 if (error < 0 || length <= 1) {
[email protected]99aae102010-05-10 16:30:2750 NOTREACHED() << "Unable to resolve path.";
51 return false;
52 }
[email protected]72d7b962011-10-25 16:22:2753 *result = FilePath(FilePath::StringType(bin_dir, length - 1));
[email protected]99aae102010-05-10 16:30:2754 return true;
[email protected]94f8c952011-06-25 04:54:4155#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;
[email protected]817f0f142011-10-13 04:23:2263#elif defined(OS_OPENBSD)
64 // There is currently no way to get the executable path on OpenBSD
[email protected]094797b72012-09-15 10:51:0565 char* cpath;
[email protected]ea725b32011-10-25 17:43:0566 if ((cpath = getenv("CHROME_EXE_PATH")) != NULL)
67 *result = FilePath(cpath);
68 else
69 *result = FilePath("/usr/local/chrome/chrome");
[email protected]817f0f142011-10-13 04:23:2270 return true;
[email protected]99aae102010-05-10 16:30:2771#endif
[email protected]b2e97292008-09-02 18:20:3472 }
[email protected]632be2f2010-04-21 23:28:4373 case base::DIR_SOURCE_ROOT: {
[email protected]3766ed1c2012-07-26 20:53:5674 // 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 }
[email protected]5d1937bb2009-11-21 01:29:0088 // On POSIX, unit tests execute two levels deep from the source root.
[email protected]016498e2010-12-03 00:59:2389 // For example: out/{Debug|Release}/net_unittest
[email protected]95d050e2009-09-10 19:30:4690 if (PathService::Get(base::DIR_EXE, &path)) {
[email protected]569edabd2012-02-03 23:10:0491 *result = path.DirName().DirName();
[email protected]95d050e2009-09-10 19:30:4692 return true;
93 }
[email protected]569edabd2012-02-03 23:10:0494
[email protected]a42d4632011-10-26 21:48:0095 DLOG(ERROR) << "Couldn't find your source root. "
96 << "Try running from your chromium/src directory.";
[email protected]95d050e2009-09-10 19:30:4697 return false;
[email protected]632be2f2010-04-21 23:28:4398 }
[email protected]92e44ae0a2012-07-23 08:22:4499 case base::DIR_CACHE: {
[email protected]76b90d312010-08-03 03:00:50100 scoped_ptr<base::Environment> env(base::Environment::Create());
[email protected]6b0349ef2010-10-16 04:56:06101 FilePath cache_dir(base::nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME",
102 ".cache"));
[email protected]9e9b6e8e2009-12-02 08:45:01103 *result = cache_dir;
104 return true;
[email protected]92e44ae0a2012-07-23 08:22:44105 }
106 case base::DIR_HOME: {
107 *result = file_util::GetHomeDir();
108 return true;
109 }
[email protected]b2e97292008-09-02 18:20:34110 }
111 return false;
112}
113
114} // namespace base