Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2006-2008 The Chromium Authors |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
| 5 | #include"base/base_paths.h" |
| 6 | |
Paul Semel | f90c9dc5 | 2023-11-03 12:19:56 | [diff] [blame] | 7 | #include"base/environment.h" |
brettw@chromium.org | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 8 | #include"base/files/file_path.h" |
brettw@chromium.org | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 9 | #include"base/files/file_util.h" |
Paul Semel | f90c9dc5 | 2023-11-03 12:19:56 | [diff] [blame] | 10 | #include"base/logging.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 11 | #include"base/path_service.h" |
Paul Semel | f90c9dc5 | 2023-11-03 12:19:56 | [diff] [blame] | 12 | #include"base/strings/utf_string_conversions.h" |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 13 | #include"build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 14 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | namespace base{ |
| 16 | |
Paul Semel | f90c9dc5 | 2023-11-03 12:19:56 | [diff] [blame] | 17 | // This provider aims at overriding the initial behaviour for all platforms. It |
| 18 | // is meant to be run **before** the platform specific provider so that this one |
| 19 | // prevails in case the overriding conditions are met. This provider is also |
| 20 | // meant to fallback on the platform specific provider, which means it should |
| 21 | // not handle the `BasePathKey` for which we do not have overriding behaviours. |
| 22 | boolEnvOverridePathProvider(int key,FilePath* result){ |
| 23 | switch(key){ |
| 24 | case base::DIR_SRC_TEST_DATA_ROOT:{ |
| 25 | // Allow passing this in the environment, for more flexibility in build |
| 26 | // tree configurations (sub-project builds, gyp --output_dir, etc.) |
| 27 | std::unique_ptr<Environment> env(Environment::Create()); |
Helmut Januschka | 726658b | 2025-03-21 22:44:57 | [diff] [blame] | 28 | std::optional<std::string> cr_source_root= env->GetVar("CR_SOURCE_ROOT"); |
| 29 | if(cr_source_root.has_value()){ |
| 30 | FilePath path; |
Paul Semel | f90c9dc5 | 2023-11-03 12:19:56 | [diff] [blame] | 31 | #if BUILDFLAG(IS_WIN) |
Helmut Januschka | 726658b | 2025-03-21 22:44:57 | [diff] [blame] | 32 | path=FilePath(UTF8ToWide(cr_source_root.value())); |
Paul Semel | f90c9dc5 | 2023-11-03 12:19:56 | [diff] [blame] | 33 | #else |
Helmut Januschka | 726658b | 2025-03-21 22:44:57 | [diff] [blame] | 34 | path=FilePath(cr_source_root.value()); |
Paul Semel | f90c9dc5 | 2023-11-03 12:19:56 | [diff] [blame] | 35 | #endif |
| 36 | if(!path.IsAbsolute()){ |
| 37 | FilePath root; |
| 38 | if(PathService::Get(DIR_EXE,&root)){ |
| 39 | path= root.Append(path); |
| 40 | } |
| 41 | } |
| 42 | if(DirectoryExists(path)){ |
| 43 | *result= path; |
| 44 | returntrue; |
| 45 | } |
| 46 | DLOG(WARNING)<<"CR_SOURCE_ROOT is set, but it appears to not " |
| 47 | <<"point to a directory."; |
| 48 | } |
| 49 | returnfalse; |
| 50 | } |
| 51 | default: |
| 52 | break; |
| 53 | } |
| 54 | returnfalse; |
| 55 | } |
| 56 | |
evanm@google.com | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 57 | boolPathProvider(int key,FilePath* result){ |
gab@chromium.org | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 58 | // NOTE: DIR_CURRENT is a special case in PathService::Get |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 59 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 60 | switch(key){ |
tfarina@chromium.org | c4803e43 | 2013-03-28 00:40:04 | [diff] [blame] | 61 | case DIR_EXE: |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 62 | if(!PathService::Get(FILE_EXE, result)){ |
Sergey Ulanov | d5ae68e | 2018-02-07 20:14:21 | [diff] [blame] | 63 | returnfalse; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 64 | } |
brettw@chromium.org | ffaee18e | 2014-02-19 20:34:23 | [diff] [blame] | 65 | *result= result->DirName(); |
| 66 | returntrue; |
Sylvain Defresne | e7b6655 | 2023-06-20 08:23:30 | [diff] [blame] | 67 | #if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS) |
tfarina@chromium.org | c4803e43 | 2013-03-28 00:40:04 | [diff] [blame] | 68 | case DIR_MODULE: |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 69 | if(!PathService::Get(FILE_MODULE, result)){ |
Sergey Ulanov | d5ae68e | 2018-02-07 20:14:21 | [diff] [blame] | 70 | returnfalse; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 71 | } |
brettw@chromium.org | ffaee18e | 2014-02-19 20:34:23 | [diff] [blame] | 72 | *result= result->DirName(); |
| 73 | returntrue; |
Sergey Ulanov | d5ae68e | 2018-02-07 20:14:21 | [diff] [blame] | 74 | case DIR_ASSETS: |
| 75 | returnPathService::Get(DIR_MODULE, result); |
Sylvain Defresne | e7b6655 | 2023-06-20 08:23:30 | [diff] [blame] | 76 | #endif// !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS) |
tfarina@chromium.org | c4803e43 | 2013-03-28 00:40:04 | [diff] [blame] | 77 | case DIR_TEMP: |
Sergey Ulanov | d5ae68e | 2018-02-07 20:14:21 | [diff] [blame] | 78 | returnGetTempDir(result); |
David Dorwin | c694f72 | 2021-10-29 22:46:59 | [diff] [blame] | 79 | case DIR_HOME: |
brettw@chromium.org | ffaee18e | 2014-02-19 20:34:23 | [diff] [blame] | 80 | *result=GetHomeDir(); |
| 81 | returntrue; |
Etienne Pierre-doray | f4dcbd6a | 2023-07-06 16:12:03 | [diff] [blame] | 82 | case base::DIR_SRC_TEST_DATA_ROOT: |
| 83 | // This is only used by tests and overridden by each platform. |
Peter Boström | de57333 | 2024-08-26 20:42:45 | [diff] [blame] | 84 | NOTREACHED(); |
Etienne Pierre-doray | f4dcbd6a | 2023-07-06 16:12:03 | [diff] [blame] | 85 | #if !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS) |
| 86 | case DIR_OUT_TEST_DATA_ROOT: |
| 87 | // On most platforms test binaries are run directly from the build-output |
| 88 | // directory, so return the directory containing the executable. |
| 89 | returnPathService::Get(DIR_MODULE, result); |
| 90 | #endif// !BUILDFLAG(IS_FUCHSIA) && !BUILDFLAG(IS_IOS) |
Etienne Pierre-doray | bd109b4 | 2023-06-15 15:55:31 | [diff] [blame] | 91 | case DIR_GEN_TEST_DATA_ROOT: |
Etienne Pierre-doray | f4dcbd6a | 2023-07-06 16:12:03 | [diff] [blame] | 92 | if(!PathService::Get(DIR_OUT_TEST_DATA_ROOT, result)){ |
Etienne Pierre-doray | bd109b4 | 2023-06-15 15:55:31 | [diff] [blame] | 93 | returnfalse; |
| 94 | } |
| 95 | *result= result->Append(FILE_PATH_LITERAL("gen")); |
| 96 | returntrue; |
maniscalco | 6e14781 | 2015-12-11 18:23:13 | [diff] [blame] | 97 | case DIR_TEST_DATA:{ |
| 98 | FilePath test_data_path; |
Etienne Pierre-doray | f4dcbd6a | 2023-07-06 16:12:03 | [diff] [blame] | 99 | if(!PathService::Get(DIR_SRC_TEST_DATA_ROOT,&test_data_path)){ |
tfarina@chromium.org | c4803e43 | 2013-03-28 00:40:04 | [diff] [blame] | 100 | returnfalse; |
Etienne Pierre-doray | f4dcbd6a | 2023-07-06 16:12:03 | [diff] [blame] | 101 | } |
maniscalco | 6e14781 | 2015-12-11 18:23:13 | [diff] [blame] | 102 | test_data_path= test_data_path.Append(FILE_PATH_LITERAL("base")); |
| 103 | test_data_path= test_data_path.Append(FILE_PATH_LITERAL("test")); |
| 104 | test_data_path= test_data_path.Append(FILE_PATH_LITERAL("data")); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 105 | if(!PathExists(test_data_path)){// We don't want to create this. |
tfarina@chromium.org | c4803e43 | 2013-03-28 00:40:04 | [diff] [blame] | 106 | returnfalse; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 107 | } |
maniscalco | 6e14781 | 2015-12-11 18:23:13 | [diff] [blame] | 108 | *result= test_data_path; |
brettw@chromium.org | ffaee18e | 2014-02-19 20:34:23 | [diff] [blame] | 109 | returntrue; |
maniscalco | 6e14781 | 2015-12-11 18:23:13 | [diff] [blame] | 110 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 111 | } |
David Dorwin | c694f72 | 2021-10-29 22:46:59 | [diff] [blame] | 112 | |
| 113 | returnfalse; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | }// namespace base |