[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 32 | // 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. |
| 34 | static const char kThisSourceFile[] = "base/base_paths_linux.cc"; |
| 35 | |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 36 | bool PathProviderPosix(int key, FilePath* result) { |
[email protected] | 640517f | 2008-10-30 23:54:04 | [diff] [blame] | 37 | FilePath path; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 38 | switch (key) { |
| 39 | case base::FILE_EXE: |
[email protected] | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 40 | case base::FILE_MODULE: { // TODO(evanm): is this correct? |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 41 | #if defined(OS_LINUX) |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 42 | FilePath bin_dir; |
| 43 | if (!file_util::ReadSymbolicLink(FilePath(kSelfExe), &bin_dir)) { |
[email protected] | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 44 | NOTREACHED() << "Unable to resolve " << kSelfExe << "."; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 45 | return false; |
| 46 | } |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 47 | *result = bin_dir; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 48 | return true; |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 49 | #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] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame^] | 61 | #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] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 69 | #endif |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 70 | } |
[email protected] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 71 | 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] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 74 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 75 | std::string cr_source_root; |
[email protected] | 3ba7e08 | 2010-08-07 02:57:59 | [diff] [blame] | 76 | if (env->GetVar("CR_SOURCE_ROOT", &cr_source_root)) { |
[email protected] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 77 | path = FilePath(cr_source_root); |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 78 | if (file_util::PathExists(path.Append(kThisSourceFile))) { |
[email protected] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 79 | *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] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 86 | // On POSIX, unit tests execute two levels deep from the source root. |
[email protected] | 016498e | 2010-12-03 00:59:23 | [diff] [blame] | 87 | // For example: out/{Debug|Release}/net_unittest |
[email protected] | 95d050e | 2009-09-10 19:30:46 | [diff] [blame] | 88 | if (PathService::Get(base::DIR_EXE, &path)) { |
| 89 | path = path.DirName().DirName(); |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 90 | if (file_util::PathExists(path.Append(kThisSourceFile))) { |
[email protected] | 95d050e | 2009-09-10 19:30:46 | [diff] [blame] | 91 | *result = path; |
| 92 | return true; |
| 93 | } |
| 94 | } |
[email protected] | 14a25e50 | 2010-06-15 06:53:52 | [diff] [blame] | 95 | // In a case of WebKit-only checkout, executable files are put into |
[email protected] | 53a1b66 | 2011-02-01 23:18:54 | [diff] [blame] | 96 | // <root of checkout>/out/{Debug|Release}, and we should return |
| 97 | // <root of checkout>/Source/WebKit/chromium for DIR_SOURCE_ROOT. |
[email protected] | 14a25e50 | 2010-06-15 06:53:52 | [diff] [blame] | 98 | if (PathService::Get(base::DIR_EXE, &path)) { |
[email protected] | 53a1b66 | 2011-02-01 23:18:54 | [diff] [blame] | 99 | path = path.DirName().DirName().Append("Source/WebKit/chromium"); |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 100 | if (file_util::PathExists(path.Append(kThisSourceFile))) { |
[email protected] | 14a25e50 | 2010-06-15 06:53:52 | [diff] [blame] | 101 | *result = path; |
| 102 | return true; |
| 103 | } |
| 104 | } |
[email protected] | 95d050e | 2009-09-10 19:30:46 | [diff] [blame] | 105 | // 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] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 108 | file_util::PathExists(path.Append(kThisSourceFile))) { |
[email protected] | 95d050e | 2009-09-10 19:30:46 | [diff] [blame] | 109 | *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] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 115 | } |
[email protected] | b411da3 | 2010-11-24 02:23:15 | [diff] [blame] | 116 | case base::DIR_CACHE: |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 117 | scoped_ptr<base::Environment> env(base::Environment::Create()); |
[email protected] | 6b0349ef | 2010-10-16 04:56:06 | [diff] [blame] | 118 | FilePath cache_dir(base::nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", |
| 119 | ".cache")); |
[email protected] | 9e9b6e8e | 2009-12-02 08:45:01 | [diff] [blame] | 120 | *result = cache_dir; |
| 121 | return true; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | } // namespace base |