[email protected] | 569edabd | 2012-02-03 23:10:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 5 | #include "base/base_paths.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 6 | |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 7 | #include <ostream> |
| 8 | #include <string> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 9 | |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 10 | #include "build/build_config.h" |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 11 | #include "base/environment.h" |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 12 | #include "base/file_path.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 13 | #include "base/file_util.h" |
| 14 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 16 | #include "base/path_service.h" |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 17 | #include "base/nix/xdg_util.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 18 | |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 19 | #if defined(OS_FREEBSD) |
| 20 | #include <sys/param.h> |
| 21 | #include <sys/sysctl.h> |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 22 | #elif defined(OS_SOLARIS) |
| 23 | #include <stdlib.h> |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 24 | #endif |
| 25 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 26 | namespace base { |
| 27 | |
[email protected] | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 28 | #if defined(OS_LINUX) |
| 29 | const char kSelfExe[] = "/proc/self/exe"; |
[email protected] | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 30 | #endif |
| 31 | |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 32 | bool PathProviderPosix(int key, FilePath* result) { |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 33 | FilePath path; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 34 | switch (key) { |
| 35 | case base::FILE_EXE: |
[email protected] | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 36 | case base::FILE_MODULE: { // TODO(evanm): is this correct? |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 37 | #if defined(OS_LINUX) |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 38 | FilePath bin_dir; |
| 39 | if (!file_util::ReadSymbolicLink(FilePath(kSelfExe), &bin_dir)) { |
[email protected] | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 40 | NOTREACHED() << "Unable to resolve " << kSelfExe << "."; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 41 | return false; |
| 42 | } |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 43 | *result = bin_dir; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 44 | return true; |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 45 | #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] | 72d7b96 | 2011-10-25 16:22:27 | [diff] [blame] | 49 | // Upon return, |length| is the number of bytes written to |bin_dir| |
| 50 | // including the string terminator. |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 51 | int error = sysctl(name, 4, bin_dir, &length, NULL, 0); |
[email protected] | 72d7b96 | 2011-10-25 16:22:27 | [diff] [blame] | 52 | if (error < 0 || length <= 1) { |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 53 | NOTREACHED() << "Unable to resolve path."; |
| 54 | return false; |
| 55 | } |
[email protected] | 72d7b96 | 2011-10-25 16:22:27 | [diff] [blame] | 56 | *result = FilePath(FilePath::StringType(bin_dir, length - 1)); |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 57 | return true; |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 58 | #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] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 66 | #elif defined(OS_OPENBSD) |
| 67 | // There is currently no way to get the executable path on OpenBSD |
[email protected] | ea725b3 | 2011-10-25 17:43:05 | [diff] [blame] | 68 | 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] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 73 | return true; |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 74 | #endif |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 75 | } |
[email protected] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 76 | 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] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 79 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 80 | std::string cr_source_root; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 81 | if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) { |
[email protected] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 82 | path = FilePath(cr_source_root); |
[email protected] | 569edabd | 2012-02-03 23:10:04 | [diff] [blame] | 83 | if (file_util::PathExists(path)) { |
[email protected] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 84 | *result = path; |
| 85 | return true; |
| 86 | } else { |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 87 | DLOG(WARNING) << "CR_SOURCE_ROOT is set, but it appears to not " |
[email protected] | 569edabd | 2012-02-03 23:10:04 | [diff] [blame] | 88 | << "point to a directory."; |
[email protected] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 89 | } |
| 90 | } |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 91 | // On POSIX, unit tests execute two levels deep from the source root. |
[email protected] | 016498e | 2010-12-03 00:59:23 | [diff] [blame] | 92 | // For example: out/{Debug|Release}/net_unittest |
[email protected] | 95d050e | 2009-09-10 19:30:46 | [diff] [blame] | 93 | if (PathService::Get(base::DIR_EXE, &path)) { |
[email protected] | 569edabd | 2012-02-03 23:10:04 | [diff] [blame] | 94 | *result = path.DirName().DirName(); |
[email protected] | 95d050e | 2009-09-10 19:30:46 | [diff] [blame] | 95 | return true; |
| 96 | } |
[email protected] | 569edabd | 2012-02-03 23:10:04 | [diff] [blame] | 97 | |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 98 | DLOG(ERROR) << "Couldn't find your source root. " |
| 99 | << "Try running from your chromium/src directory."; |
[email protected] | 95d050e | 2009-09-10 19:30:46 | [diff] [blame] | 100 | return false; |
[email protected] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 101 | } |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 102 | case base::DIR_CACHE: |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 103 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 104 | FilePath cache_dir(base::nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", |
| 105 | ".cache")); |
[email protected] | 9e9b6e8e | 2009-12-02 08:45:01 | [diff] [blame] | 106 | *result = cache_dir; |
| 107 | return true; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | } // namespace base |