| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. | 
|  | 2 | // | 
|  | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | // you may not use this file except in compliance with the License. | 
|  | 5 | // You may obtain a copy of the License at | 
|  | 6 | // | 
|  | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | // | 
|  | 9 | // Unless required by applicable law or agreed to in writing, software | 
|  | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | // See the License for the specific language governing permissions and | 
|  | 13 | // limitations under the License. | 
|  | 14 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 16 |  | 
|  | 17 | import ( | 
| Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 18 | "fmt" | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 19 | "os" | 
| Colin Cross | 6a745c6 | 2015-06-16 16:38:10 -0700 | [diff] [blame] | 20 | "path/filepath" | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 21 | "reflect" | 
| Chris Wailes | b2703ad | 2021-07-30 13:25:42 -0700 | [diff] [blame] | 22 | "regexp" | 
| Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 23 | "sort" | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 24 | "strings" | 
|  | 25 |  | 
|  | 26 | "github.com/google/blueprint" | 
| Colin Cross | 0e44615 | 2021-05-03 13:35:32 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint/bootstrap" | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint/pathtools" | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 29 | ) | 
|  | 30 |  | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 31 | var absSrcDir string | 
|  | 32 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 33 | // PathContext is the subset of a (Module|Singleton)Context required by the | 
|  | 34 | // Path methods. | 
|  | 35 | type PathContext interface { | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 36 | Config() Config | 
| Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 37 | AddNinjaFileDeps(deps ...string) | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 38 | } | 
|  | 39 |  | 
| Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 40 | type PathGlobContext interface { | 
| Colin Cross | 662d614 | 2022-11-03 20:38:01 -0700 | [diff] [blame] | 41 | PathContext | 
| Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 42 | GlobWithDeps(globPattern string, excludes []string) ([]string, error) | 
|  | 43 | } | 
|  | 44 |  | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 45 | var _ PathContext = SingletonContext(nil) | 
|  | 46 | var _ PathContext = ModuleContext(nil) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 47 |  | 
| Ulya Trafimovich | 8640ab9 | 2020-05-11 18:06:15 +0100 | [diff] [blame] | 48 | // "Null" path context is a minimal path context for a given config. | 
|  | 49 | type NullPathContext struct { | 
|  | 50 | config Config | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | func (NullPathContext) AddNinjaFileDeps(...string) {} | 
|  | 54 | func (ctx NullPathContext) Config() Config         { return ctx.config } | 
|  | 55 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 56 | // EarlyModulePathContext is a subset of EarlyModuleContext methods required by the | 
|  | 57 | // Path methods. These path methods can be called before any mutators have run. | 
|  | 58 | type EarlyModulePathContext interface { | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 59 | PathGlobContext | 
|  | 60 |  | 
|  | 61 | ModuleDir() string | 
|  | 62 | ModuleErrorf(fmt string, args ...interface{}) | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | var _ EarlyModulePathContext = ModuleContext(nil) | 
|  | 66 |  | 
|  | 67 | // Glob globs files and directories matching globPattern relative to ModuleDir(), | 
|  | 68 | // paths in the excludes parameter will be omitted. | 
|  | 69 | func Glob(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths { | 
|  | 70 | ret, err := ctx.GlobWithDeps(globPattern, excludes) | 
|  | 71 | if err != nil { | 
|  | 72 | ctx.ModuleErrorf("glob: %s", err.Error()) | 
|  | 73 | } | 
|  | 74 | return pathsForModuleSrcFromFullPath(ctx, ret, true) | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | // GlobFiles globs *only* files (not directories) matching globPattern relative to ModuleDir(). | 
|  | 78 | // Paths in the excludes parameter will be omitted. | 
|  | 79 | func GlobFiles(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths { | 
|  | 80 | ret, err := ctx.GlobWithDeps(globPattern, excludes) | 
|  | 81 | if err != nil { | 
|  | 82 | ctx.ModuleErrorf("glob: %s", err.Error()) | 
|  | 83 | } | 
|  | 84 | return pathsForModuleSrcFromFullPath(ctx, ret, false) | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | // ModuleWithDepsPathContext is a subset of *ModuleContext methods required by | 
|  | 88 | // the Path methods that rely on module dependencies having been resolved. | 
|  | 89 | type ModuleWithDepsPathContext interface { | 
|  | 90 | EarlyModulePathContext | 
| Paul Duffin | 40131a3 | 2021-07-09 17:10:35 +0100 | [diff] [blame] | 91 | VisitDirectDepsBlueprint(visit func(blueprint.Module)) | 
|  | 92 | OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
|  | 95 | // ModuleMissingDepsPathContext is a subset of *ModuleContext methods required by | 
|  | 96 | // the Path methods that rely on module dependencies having been resolved and ability to report | 
|  | 97 | // missing dependency errors. | 
|  | 98 | type ModuleMissingDepsPathContext interface { | 
|  | 99 | ModuleWithDepsPathContext | 
|  | 100 | AddMissingDependencies(missingDeps []string) | 
|  | 101 | } | 
|  | 102 |  | 
| Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 103 | type ModuleInstallPathContext interface { | 
| Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 104 | BaseModuleContext | 
| Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 105 |  | 
|  | 106 | InstallInData() bool | 
| Jaewoong Jung | 0949f31 | 2019-09-11 10:25:18 -0700 | [diff] [blame] | 107 | InstallInTestcases() bool | 
| Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 108 | InstallInSanitizerDir() bool | 
| Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 109 | InstallInRamdisk() bool | 
| Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 110 | InstallInVendorRamdisk() bool | 
| Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 111 | InstallInDebugRamdisk() bool | 
| Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 112 | InstallInRecovery() bool | 
| Colin Cross | 90ba5f4 | 2019-10-02 11:10:58 -0700 | [diff] [blame] | 113 | InstallInRoot() bool | 
| Colin Cross | ea30d85 | 2023-11-29 16:00:16 -0800 | [diff] [blame] | 114 | InstallInOdm() bool | 
|  | 115 | InstallInProduct() bool | 
|  | 116 | InstallInVendor() bool | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 117 | InstallForceOS() (*OsType, *ArchType) | 
| Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 118 | } | 
|  | 119 |  | 
|  | 120 | var _ ModuleInstallPathContext = ModuleContext(nil) | 
|  | 121 |  | 
| Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame] | 122 | type baseModuleContextToModuleInstallPathContext struct { | 
|  | 123 | BaseModuleContext | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | func (ctx *baseModuleContextToModuleInstallPathContext) InstallInData() bool { | 
|  | 127 | return ctx.Module().InstallInData() | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | func (ctx *baseModuleContextToModuleInstallPathContext) InstallInTestcases() bool { | 
|  | 131 | return ctx.Module().InstallInTestcases() | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | func (ctx *baseModuleContextToModuleInstallPathContext) InstallInSanitizerDir() bool { | 
|  | 135 | return ctx.Module().InstallInSanitizerDir() | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRamdisk() bool { | 
|  | 139 | return ctx.Module().InstallInRamdisk() | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendorRamdisk() bool { | 
|  | 143 | return ctx.Module().InstallInVendorRamdisk() | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | func (ctx *baseModuleContextToModuleInstallPathContext) InstallInDebugRamdisk() bool { | 
|  | 147 | return ctx.Module().InstallInDebugRamdisk() | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRecovery() bool { | 
|  | 151 | return ctx.Module().InstallInRecovery() | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRoot() bool { | 
|  | 155 | return ctx.Module().InstallInRoot() | 
|  | 156 | } | 
|  | 157 |  | 
| Colin Cross | ea30d85 | 2023-11-29 16:00:16 -0800 | [diff] [blame] | 158 | func (ctx *baseModuleContextToModuleInstallPathContext) InstallInOdm() bool { | 
|  | 159 | return ctx.Module().InstallInOdm() | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | func (ctx *baseModuleContextToModuleInstallPathContext) InstallInProduct() bool { | 
|  | 163 | return ctx.Module().InstallInProduct() | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendor() bool { | 
|  | 167 | return ctx.Module().InstallInVendor() | 
|  | 168 | } | 
|  | 169 |  | 
| Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame] | 170 | func (ctx *baseModuleContextToModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) { | 
|  | 171 | return ctx.Module().InstallForceOS() | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | var _ ModuleInstallPathContext = (*baseModuleContextToModuleInstallPathContext)(nil) | 
|  | 175 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 176 | // errorfContext is the interface containing the Errorf method matching the | 
|  | 177 | // Errorf method in blueprint.SingletonContext. | 
|  | 178 | type errorfContext interface { | 
|  | 179 | Errorf(format string, args ...interface{}) | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 182 | var _ errorfContext = blueprint.SingletonContext(nil) | 
|  | 183 |  | 
| Spandan Das | 59a4a2b | 2024-01-09 21:35:56 +0000 | [diff] [blame] | 184 | // ModuleErrorfContext is the interface containing the ModuleErrorf method matching | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 185 | // the ModuleErrorf method in blueprint.ModuleContext. | 
| Spandan Das | 59a4a2b | 2024-01-09 21:35:56 +0000 | [diff] [blame] | 186 | type ModuleErrorfContext interface { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 187 | ModuleErrorf(format string, args ...interface{}) | 
| Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| Spandan Das | 59a4a2b | 2024-01-09 21:35:56 +0000 | [diff] [blame] | 190 | var _ ModuleErrorfContext = blueprint.ModuleContext(nil) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 191 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 192 | // reportPathError will register an error with the attached context. It | 
|  | 193 | // attempts ctx.ModuleErrorf for a better error message first, then falls | 
|  | 194 | // back to ctx.Errorf. | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 195 | func reportPathError(ctx PathContext, err error) { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 196 | ReportPathErrorf(ctx, "%s", err.Error()) | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 199 | // ReportPathErrorf will register an error with the attached context. It | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 200 | // attempts ctx.ModuleErrorf for a better error message first, then falls | 
|  | 201 | // back to ctx.Errorf. | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 202 | func ReportPathErrorf(ctx PathContext, format string, args ...interface{}) { | 
| Spandan Das | 59a4a2b | 2024-01-09 21:35:56 +0000 | [diff] [blame] | 203 | if mctx, ok := ctx.(ModuleErrorfContext); ok { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 204 | mctx.ModuleErrorf(format, args...) | 
|  | 205 | } else if ectx, ok := ctx.(errorfContext); ok { | 
|  | 206 | ectx.Errorf(format, args...) | 
|  | 207 | } else { | 
|  | 208 | panic(fmt.Sprintf(format, args...)) | 
| Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 209 | } | 
|  | 210 | } | 
|  | 211 |  | 
| Colin Cross | 5e70805 | 2019-08-06 13:59:50 -0700 | [diff] [blame] | 212 | func pathContextName(ctx PathContext, module blueprint.Module) string { | 
|  | 213 | if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok { | 
|  | 214 | return x.ModuleName(module) | 
|  | 215 | } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok { | 
|  | 216 | return x.OtherModuleName(module) | 
|  | 217 | } | 
|  | 218 | return "unknown" | 
|  | 219 | } | 
|  | 220 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 221 | type Path interface { | 
|  | 222 | // Returns the path in string form | 
|  | 223 | String() string | 
|  | 224 |  | 
| Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 225 | // Ext returns the extension of the last element of the path | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 226 | Ext() string | 
| Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 227 |  | 
|  | 228 | // Base returns the last element of the path | 
|  | 229 | Base() string | 
| Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 230 |  | 
|  | 231 | // Rel returns the portion of the path relative to the directory it was created from.  For | 
|  | 232 | // example, Rel on a PathsForModuleSrc would return the path relative to the module source | 
| Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 233 | // directory, and OutputPath.Join("foo").Rel() would return "foo". | 
| Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 234 | Rel() string | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 235 |  | 
|  | 236 | // RelativeToTop returns a new path relative to the top, it is provided solely for use in tests. | 
|  | 237 | // | 
|  | 238 | // It is guaranteed to always return the same type as it is called on, e.g. if called on an | 
|  | 239 | // InstallPath then the returned value can be converted to an InstallPath. | 
|  | 240 | // | 
|  | 241 | // A standard build has the following structure: | 
|  | 242 | //   ../top/ | 
|  | 243 | //          out/ - make install files go here. | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 244 | //          out/soong - this is the soongOutDir passed to NewTestConfig() | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 245 | //          ... - the source files | 
|  | 246 | // | 
|  | 247 | // This function converts a path so that it appears relative to the ../top/ directory, i.e. | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 248 | // * Make install paths, which have the pattern "soongOutDir/../<path>" are converted into the top | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 249 | //   relative path "out/<path>" | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 250 | // * Soong install paths and other writable paths, which have the pattern "soongOutDir/<path>" are | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 251 | //   converted into the top relative path "out/soong/<path>". | 
|  | 252 | // * Source paths are already relative to the top. | 
|  | 253 | // * Phony paths are not relative to anything. | 
|  | 254 | // * toolDepPath have an absolute but known value in so don't need making relative to anything in | 
|  | 255 | //   order to test. | 
|  | 256 | RelativeToTop() Path | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 257 | } | 
|  | 258 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 259 | const ( | 
|  | 260 | OutDir      = "out" | 
|  | 261 | OutSoongDir = OutDir + "/soong" | 
|  | 262 | ) | 
|  | 263 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 264 | // WritablePath is a type of path that can be used as an output for build rules. | 
|  | 265 | type WritablePath interface { | 
|  | 266 | Path | 
|  | 267 |  | 
| Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 268 | // return the path to the build directory. | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 269 | getSoongOutDir() string | 
| Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 270 |  | 
| Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 271 | // the writablePath method doesn't directly do anything, | 
|  | 272 | // but it allows a struct to distinguish between whether or not it implements the WritablePath interface | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 273 | writablePath() | 
| Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 274 |  | 
|  | 275 | ReplaceExtension(ctx PathContext, ext string) OutputPath | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
|  | 278 | type genPathProvider interface { | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 279 | genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 280 | } | 
|  | 281 | type objPathProvider interface { | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 282 | objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 283 | } | 
|  | 284 | type resPathProvider interface { | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 285 | resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 286 | } | 
|  | 287 |  | 
|  | 288 | // GenPathWithExt derives a new file path in ctx's generated sources directory | 
|  | 289 | // from the current path, but with the new extension. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 290 | func GenPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleGenPath { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 291 | if path, ok := p.(genPathProvider); ok { | 
| Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 292 | return path.genPathWithExt(ctx, subdir, ext) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 293 | } | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 294 | ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 295 | return PathForModuleGen(ctx) | 
|  | 296 | } | 
|  | 297 |  | 
|  | 298 | // ObjPathWithExt derives a new file path in ctx's object directory from the | 
|  | 299 | // current path, but with the new extension. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 300 | func ObjPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleObjPath { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 301 | if path, ok := p.(objPathProvider); ok { | 
|  | 302 | return path.objPathWithExt(ctx, subdir, ext) | 
|  | 303 | } | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 304 | ReportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 305 | return PathForModuleObj(ctx) | 
|  | 306 | } | 
|  | 307 |  | 
|  | 308 | // ResPathWithName derives a new path in ctx's output resource directory, using | 
|  | 309 | // the current path to create the directory name, and the `name` argument for | 
|  | 310 | // the filename. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 311 | func ResPathWithName(ctx ModuleOutPathContext, p Path, name string) ModuleResPath { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 312 | if path, ok := p.(resPathProvider); ok { | 
|  | 313 | return path.resPathWithName(ctx, name) | 
|  | 314 | } | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 315 | ReportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 316 | return PathForModuleRes(ctx) | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | // OptionalPath is a container that may or may not contain a valid Path. | 
|  | 320 | type OptionalPath struct { | 
| Martin Stjernholm | c32dd1c | 2021-09-15 02:39:00 +0100 | [diff] [blame] | 321 | path          Path   // nil if invalid. | 
|  | 322 | invalidReason string // Not applicable if path != nil. "" if the reason is unknown. | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
|  | 325 | // OptionalPathForPath returns an OptionalPath containing the path. | 
|  | 326 | func OptionalPathForPath(path Path) OptionalPath { | 
| Martin Stjernholm | 2fee27f | 2021-09-16 14:11:12 +0100 | [diff] [blame] | 327 | return OptionalPath{path: path} | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 328 | } | 
|  | 329 |  | 
| Martin Stjernholm | c32dd1c | 2021-09-15 02:39:00 +0100 | [diff] [blame] | 330 | // InvalidOptionalPath returns an OptionalPath that is invalid with the given reason. | 
|  | 331 | func InvalidOptionalPath(reason string) OptionalPath { | 
|  | 332 |  | 
|  | 333 | return OptionalPath{invalidReason: reason} | 
|  | 334 | } | 
|  | 335 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 336 | // Valid returns whether there is a valid path | 
|  | 337 | func (p OptionalPath) Valid() bool { | 
| Martin Stjernholm | 2fee27f | 2021-09-16 14:11:12 +0100 | [diff] [blame] | 338 | return p.path != nil | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 339 | } | 
|  | 340 |  | 
|  | 341 | // Path returns the Path embedded in this OptionalPath. You must be sure that | 
|  | 342 | // there is a valid path, since this method will panic if there is not. | 
|  | 343 | func (p OptionalPath) Path() Path { | 
| Martin Stjernholm | 2fee27f | 2021-09-16 14:11:12 +0100 | [diff] [blame] | 344 | if p.path == nil { | 
| Martin Stjernholm | c32dd1c | 2021-09-15 02:39:00 +0100 | [diff] [blame] | 345 | msg := "Requesting an invalid path" | 
|  | 346 | if p.invalidReason != "" { | 
|  | 347 | msg += ": " + p.invalidReason | 
|  | 348 | } | 
|  | 349 | panic(msg) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 350 | } | 
|  | 351 | return p.path | 
|  | 352 | } | 
|  | 353 |  | 
| Martin Stjernholm | c32dd1c | 2021-09-15 02:39:00 +0100 | [diff] [blame] | 354 | // InvalidReason returns the reason that the optional path is invalid, or "" if it is valid. | 
|  | 355 | func (p OptionalPath) InvalidReason() string { | 
|  | 356 | if p.path != nil { | 
|  | 357 | return "" | 
|  | 358 | } | 
|  | 359 | if p.invalidReason == "" { | 
|  | 360 | return "unknown" | 
|  | 361 | } | 
|  | 362 | return p.invalidReason | 
|  | 363 | } | 
|  | 364 |  | 
| Paul Duffin | ef08185 | 2021-05-13 11:11:15 +0100 | [diff] [blame] | 365 | // AsPaths converts the OptionalPath into Paths. | 
|  | 366 | // | 
|  | 367 | // It returns nil if this is not valid, or a single length slice containing the Path embedded in | 
|  | 368 | // this OptionalPath. | 
|  | 369 | func (p OptionalPath) AsPaths() Paths { | 
| Martin Stjernholm | 2fee27f | 2021-09-16 14:11:12 +0100 | [diff] [blame] | 370 | if p.path == nil { | 
| Paul Duffin | ef08185 | 2021-05-13 11:11:15 +0100 | [diff] [blame] | 371 | return nil | 
|  | 372 | } | 
|  | 373 | return Paths{p.path} | 
|  | 374 | } | 
|  | 375 |  | 
| Paul Duffin | afdd406 | 2021-03-30 19:44:07 +0100 | [diff] [blame] | 376 | // RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the | 
|  | 377 | // result of calling Path.RelativeToTop on it. | 
|  | 378 | func (p OptionalPath) RelativeToTop() OptionalPath { | 
| Martin Stjernholm | 2fee27f | 2021-09-16 14:11:12 +0100 | [diff] [blame] | 379 | if p.path == nil { | 
| Paul Duffin | a5b8135 | 2021-03-28 23:57:19 +0100 | [diff] [blame] | 380 | return p | 
|  | 381 | } | 
|  | 382 | p.path = p.path.RelativeToTop() | 
|  | 383 | return p | 
|  | 384 | } | 
|  | 385 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 386 | // String returns the string version of the Path, or "" if it isn't valid. | 
|  | 387 | func (p OptionalPath) String() string { | 
| Martin Stjernholm | 2fee27f | 2021-09-16 14:11:12 +0100 | [diff] [blame] | 388 | if p.path != nil { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 389 | return p.path.String() | 
|  | 390 | } else { | 
|  | 391 | return "" | 
| Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 392 | } | 
|  | 393 | } | 
| Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 394 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 395 | // Paths is a slice of Path objects, with helpers to operate on the collection. | 
|  | 396 | type Paths []Path | 
|  | 397 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 398 | // RelativeToTop creates a new Paths containing the result of calling Path.RelativeToTop on each | 
|  | 399 | // item in this slice. | 
|  | 400 | func (p Paths) RelativeToTop() Paths { | 
|  | 401 | ensureTestOnly() | 
|  | 402 | if p == nil { | 
|  | 403 | return p | 
|  | 404 | } | 
|  | 405 | ret := make(Paths, len(p)) | 
|  | 406 | for i, path := range p { | 
|  | 407 | ret[i] = path.RelativeToTop() | 
|  | 408 | } | 
|  | 409 | return ret | 
|  | 410 | } | 
|  | 411 |  | 
| Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 412 | func (paths Paths) containsPath(path Path) bool { | 
|  | 413 | for _, p := range paths { | 
|  | 414 | if p == path { | 
|  | 415 | return true | 
|  | 416 | } | 
|  | 417 | } | 
|  | 418 | return false | 
|  | 419 | } | 
|  | 420 |  | 
| Liz Kammer | 7aa5288 | 2021-02-11 09:16:14 -0500 | [diff] [blame] | 421 | // PathsForSource returns Paths rooted from SrcDir, *not* rooted from the module's local source | 
|  | 422 | // directory | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 423 | func PathsForSource(ctx PathContext, paths []string) Paths { | 
|  | 424 | ret := make(Paths, len(paths)) | 
|  | 425 | for i, path := range paths { | 
|  | 426 | ret[i] = PathForSource(ctx, path) | 
|  | 427 | } | 
|  | 428 | return ret | 
|  | 429 | } | 
|  | 430 |  | 
| Liz Kammer | 7aa5288 | 2021-02-11 09:16:14 -0500 | [diff] [blame] | 431 | // ExistentPathsForSources returns a list of Paths rooted from SrcDir, *not* rooted from the | 
|  | 432 | // module's local source directory, that are found in the tree. If any are not found, they are | 
|  | 433 | // omitted from the list, and dependencies are added so that we're re-run when they are added. | 
| Colin Cross | 662d614 | 2022-11-03 20:38:01 -0700 | [diff] [blame] | 434 | func ExistentPathsForSources(ctx PathGlobContext, paths []string) Paths { | 
| Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 435 | ret := make(Paths, 0, len(paths)) | 
|  | 436 | for _, path := range paths { | 
| Colin Cross | 32f3898 | 2018-02-22 11:47:25 -0800 | [diff] [blame] | 437 | p := ExistentPathForSource(ctx, path) | 
| Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 438 | if p.Valid() { | 
|  | 439 | ret = append(ret, p.Path()) | 
|  | 440 | } | 
|  | 441 | } | 
|  | 442 | return ret | 
|  | 443 | } | 
|  | 444 |  | 
| Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 445 | // PathsForModuleSrc returns a Paths{} containing the resolved references in paths: | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 446 | //   - filepath, relative to local module directory, resolves as a filepath relative to the local | 
|  | 447 | //     source directory | 
|  | 448 | //   - glob, relative to the local module directory, resolves as filepath(s), relative to the local | 
|  | 449 | //     source directory. | 
|  | 450 | //   - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer | 
|  | 451 | //     or OutputFileProducer. These resolve as a filepath to an output filepath or generated source | 
|  | 452 | //     filepath. | 
|  | 453 | // | 
| Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 454 | // Properties passed as the paths argument must have been annotated with struct tag | 
| Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 455 | // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the | 
| Spandan Das | 950091c | 2023-07-19 22:26:37 +0000 | [diff] [blame] | 456 | // pathdeps mutator. | 
| Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 457 | // If a requested module is not found as a dependency: | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 458 | //   - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having | 
| Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 459 | //     missing dependencies | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 460 | //   - otherwise, a ModuleError is thrown. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 461 | func PathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths { | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 462 | return PathsForModuleSrcExcludes(ctx, paths, nil) | 
|  | 463 | } | 
|  | 464 |  | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 465 | type SourceInput struct { | 
|  | 466 | Context      ModuleMissingDepsPathContext | 
|  | 467 | Paths        []string | 
|  | 468 | ExcludePaths []string | 
|  | 469 | IncludeDirs  bool | 
|  | 470 | } | 
|  | 471 |  | 
| Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 472 | // PathsForModuleSrcExcludes returns a Paths{} containing the resolved references in paths, minus | 
|  | 473 | // those listed in excludes. Elements of paths and excludes are resolved as: | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 474 | //   - filepath, relative to local module directory, resolves as a filepath relative to the local | 
|  | 475 | //     source directory | 
|  | 476 | //   - glob, relative to the local module directory, resolves as filepath(s), relative to the local | 
|  | 477 | //     source directory. Not valid in excludes. | 
|  | 478 | //   - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer | 
|  | 479 | //     or OutputFileProducer. These resolve as a filepath to an output filepath or generated source | 
|  | 480 | //     filepath. | 
|  | 481 | // | 
| Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 482 | // excluding the items (similarly resolved | 
|  | 483 | // Properties passed as the paths argument must have been annotated with struct tag | 
|  | 484 | // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the | 
| Spandan Das | 950091c | 2023-07-19 22:26:37 +0000 | [diff] [blame] | 485 | // pathdeps mutator. | 
| Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 486 | // If a requested module is not found as a dependency: | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 487 | //   - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having | 
| Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 488 | //     missing dependencies | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 489 | //   - otherwise, a ModuleError is thrown. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 490 | func PathsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) Paths { | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 491 | return PathsRelativeToModuleSourceDir(SourceInput{ | 
|  | 492 | Context:      ctx, | 
|  | 493 | Paths:        paths, | 
|  | 494 | ExcludePaths: excludes, | 
|  | 495 | IncludeDirs:  true, | 
|  | 496 | }) | 
|  | 497 | } | 
|  | 498 |  | 
|  | 499 | func PathsRelativeToModuleSourceDir(input SourceInput) Paths { | 
|  | 500 | ret, missingDeps := PathsAndMissingDepsRelativeToModuleSourceDir(input) | 
|  | 501 | if input.Context.Config().AllowMissingDependencies() { | 
|  | 502 | input.Context.AddMissingDependencies(missingDeps) | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 503 | } else { | 
|  | 504 | for _, m := range missingDeps { | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 505 | input.Context.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m) | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 506 | } | 
|  | 507 | } | 
|  | 508 | return ret | 
|  | 509 | } | 
|  | 510 |  | 
| Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 511 | // OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection. | 
|  | 512 | type OutputPaths []OutputPath | 
|  | 513 |  | 
|  | 514 | // Paths returns the OutputPaths as a Paths | 
|  | 515 | func (p OutputPaths) Paths() Paths { | 
|  | 516 | if p == nil { | 
|  | 517 | return nil | 
|  | 518 | } | 
|  | 519 | ret := make(Paths, len(p)) | 
|  | 520 | for i, path := range p { | 
|  | 521 | ret[i] = path | 
|  | 522 | } | 
|  | 523 | return ret | 
|  | 524 | } | 
|  | 525 |  | 
|  | 526 | // Strings returns the string forms of the writable paths. | 
|  | 527 | func (p OutputPaths) Strings() []string { | 
|  | 528 | if p == nil { | 
|  | 529 | return nil | 
|  | 530 | } | 
|  | 531 | ret := make([]string, len(p)) | 
|  | 532 | for i, path := range p { | 
|  | 533 | ret[i] = path.String() | 
|  | 534 | } | 
|  | 535 | return ret | 
|  | 536 | } | 
|  | 537 |  | 
| Colin Cross | a44551f | 2021-10-25 15:36:21 -0700 | [diff] [blame] | 538 | // PathForGoBinary returns the path to the installed location of a bootstrap_go_binary module. | 
|  | 539 | func PathForGoBinary(ctx PathContext, goBinary bootstrap.GoBinaryTool) Path { | 
| Cole Faust | 3b703f3 | 2023-10-16 13:30:51 -0700 | [diff] [blame] | 540 | goBinaryInstallDir := pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "bin") | 
| Colin Cross | a44551f | 2021-10-25 15:36:21 -0700 | [diff] [blame] | 541 | rel := Rel(ctx, goBinaryInstallDir.String(), goBinary.InstallPath()) | 
|  | 542 | return goBinaryInstallDir.Join(ctx, rel) | 
|  | 543 | } | 
|  | 544 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 545 | // Expands Paths to a SourceFileProducer or OutputFileProducer module dependency referenced via ":name" or ":name{.tag}" syntax. | 
|  | 546 | // If the dependency is not found, a missingErrorDependency is returned. | 
|  | 547 | // If the module dependency is not a SourceFileProducer or OutputFileProducer, appropriate errors will be returned. | 
|  | 548 | func getPathsFromModuleDep(ctx ModuleWithDepsPathContext, path, moduleName, tag string) (Paths, error) { | 
| Paul Duffin | d5cf92e | 2021-07-09 17:38:55 +0100 | [diff] [blame] | 549 | module := GetModuleFromPathDep(ctx, moduleName, tag) | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 550 | if module == nil { | 
|  | 551 | return nil, missingDependencyError{[]string{moduleName}} | 
|  | 552 | } | 
| Colin Cross | fa65cee | 2021-03-22 17:05:59 -0700 | [diff] [blame] | 553 | if aModule, ok := module.(Module); ok && !aModule.Enabled() { | 
|  | 554 | return nil, missingDependencyError{[]string{moduleName}} | 
|  | 555 | } | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 556 | if outProducer, ok := module.(OutputFileProducer); ok { | 
|  | 557 | outputFiles, err := outProducer.OutputFiles(tag) | 
|  | 558 | if err != nil { | 
|  | 559 | return nil, fmt.Errorf("path dependency %q: %s", path, err) | 
|  | 560 | } | 
|  | 561 | return outputFiles, nil | 
|  | 562 | } else if tag != "" { | 
|  | 563 | return nil, fmt.Errorf("path dependency %q is not an output file producing module", path) | 
| Colin Cross | 0e44615 | 2021-05-03 13:35:32 -0700 | [diff] [blame] | 564 | } else if goBinary, ok := module.(bootstrap.GoBinaryTool); ok { | 
| Colin Cross | a44551f | 2021-10-25 15:36:21 -0700 | [diff] [blame] | 565 | goBinaryPath := PathForGoBinary(ctx, goBinary) | 
|  | 566 | return Paths{goBinaryPath}, nil | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 567 | } else if srcProducer, ok := module.(SourceFileProducer); ok { | 
|  | 568 | return srcProducer.Srcs(), nil | 
|  | 569 | } else { | 
|  | 570 | return nil, fmt.Errorf("path dependency %q is not a source file producing module", path) | 
|  | 571 | } | 
|  | 572 | } | 
|  | 573 |  | 
| Paul Duffin | d5cf92e | 2021-07-09 17:38:55 +0100 | [diff] [blame] | 574 | // GetModuleFromPathDep will return the module that was added as a dependency automatically for | 
|  | 575 | // properties tagged with `android:"path"` or manually using ExtractSourceDeps or | 
|  | 576 | // ExtractSourcesDeps. | 
|  | 577 | // | 
|  | 578 | // The moduleName and tag supplied to this should be the values returned from SrcIsModuleWithTag. | 
|  | 579 | // Or, if no tag is expected then the moduleName should be the value returned by  SrcIsModule and | 
|  | 580 | // the tag must be "". | 
|  | 581 | // | 
|  | 582 | // If tag is "" then the returned module will be the dependency that was added for ":moduleName". | 
|  | 583 | // Otherwise, it is the dependency that was added for ":moduleName{tag}". | 
| Paul Duffin | d5cf92e | 2021-07-09 17:38:55 +0100 | [diff] [blame] | 584 | func GetModuleFromPathDep(ctx ModuleWithDepsPathContext, moduleName, tag string) blueprint.Module { | 
| Paul Duffin | 40131a3 | 2021-07-09 17:10:35 +0100 | [diff] [blame] | 585 | var found blueprint.Module | 
|  | 586 | // The sourceOrOutputDepTag uniquely identifies the module dependency as it contains both the | 
|  | 587 | // module name and the tag. Dependencies added automatically for properties tagged with | 
|  | 588 | // `android:"path"` are deduped so are guaranteed to be unique. It is possible for duplicate | 
|  | 589 | // dependencies to be added manually using ExtractSourcesDeps or ExtractSourceDeps but even then | 
|  | 590 | // it will always be the case that the dependencies will be identical, i.e. the same tag and same | 
|  | 591 | // moduleName referring to the same dependency module. | 
|  | 592 | // | 
|  | 593 | // It does not matter whether the moduleName is a fully qualified name or if the module | 
|  | 594 | // dependency is a prebuilt module. All that matters is the same information is supplied to | 
|  | 595 | // create the tag here as was supplied to create the tag when the dependency was added so that | 
|  | 596 | // this finds the matching dependency module. | 
|  | 597 | expectedTag := sourceOrOutputDepTag(moduleName, tag) | 
|  | 598 | ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) { | 
|  | 599 | depTag := ctx.OtherModuleDependencyTag(module) | 
|  | 600 | if depTag == expectedTag { | 
|  | 601 | found = module | 
|  | 602 | } | 
|  | 603 | }) | 
|  | 604 | return found | 
| Paul Duffin | d5cf92e | 2021-07-09 17:38:55 +0100 | [diff] [blame] | 605 | } | 
|  | 606 |  | 
| Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 607 | // PathsAndMissingDepsForModuleSrcExcludes returns a Paths{} containing the resolved references in | 
|  | 608 | // paths, minus those listed in excludes. Elements of paths and excludes are resolved as: | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 609 | //   - filepath, relative to local module directory, resolves as a filepath relative to the local | 
|  | 610 | //     source directory | 
|  | 611 | //   - glob, relative to the local module directory, resolves as filepath(s), relative to the local | 
|  | 612 | //     source directory. Not valid in excludes. | 
|  | 613 | //   - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer | 
|  | 614 | //     or OutputFileProducer. These resolve as a filepath to an output filepath or generated source | 
|  | 615 | //     filepath. | 
|  | 616 | // | 
| Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 617 | // and a list of the module names of missing module dependencies are returned as the second return. | 
|  | 618 | // Properties passed as the paths argument must have been annotated with struct tag | 
| Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 619 | // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the | 
| Spandan Das | 950091c | 2023-07-19 22:26:37 +0000 | [diff] [blame] | 620 | // pathdeps mutator. | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 621 | func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) (Paths, []string) { | 
|  | 622 | return PathsAndMissingDepsRelativeToModuleSourceDir(SourceInput{ | 
|  | 623 | Context:      ctx, | 
|  | 624 | Paths:        paths, | 
|  | 625 | ExcludePaths: excludes, | 
|  | 626 | IncludeDirs:  true, | 
|  | 627 | }) | 
|  | 628 | } | 
|  | 629 |  | 
|  | 630 | func PathsAndMissingDepsRelativeToModuleSourceDir(input SourceInput) (Paths, []string) { | 
|  | 631 | prefix := pathForModuleSrc(input.Context).String() | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 632 |  | 
|  | 633 | var expandedExcludes []string | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 634 | if input.ExcludePaths != nil { | 
|  | 635 | expandedExcludes = make([]string, 0, len(input.ExcludePaths)) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 636 | } | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 637 |  | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 638 | var missingExcludeDeps []string | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 639 | for _, e := range input.ExcludePaths { | 
| Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 640 | if m, t := SrcIsModuleWithTag(e); m != "" { | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 641 | modulePaths, err := getPathsFromModuleDep(input.Context, e, m, t) | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 642 | if m, ok := err.(missingDependencyError); ok { | 
|  | 643 | missingExcludeDeps = append(missingExcludeDeps, m.missingDeps...) | 
|  | 644 | } else if err != nil { | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 645 | reportPathError(input.Context, err) | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 646 | } else { | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 647 | expandedExcludes = append(expandedExcludes, modulePaths.Strings()...) | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 648 | } | 
|  | 649 | } else { | 
|  | 650 | expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e)) | 
|  | 651 | } | 
|  | 652 | } | 
|  | 653 |  | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 654 | if input.Paths == nil { | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 655 | return nil, missingExcludeDeps | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 656 | } | 
|  | 657 |  | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 658 | var missingDeps []string | 
|  | 659 |  | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 660 | expandedSrcFiles := make(Paths, 0, len(input.Paths)) | 
|  | 661 | for _, s := range input.Paths { | 
|  | 662 | srcFiles, err := expandOneSrcPath(sourcePathInput{ | 
|  | 663 | context:          input.Context, | 
|  | 664 | path:             s, | 
|  | 665 | expandedExcludes: expandedExcludes, | 
|  | 666 | includeDirs:      input.IncludeDirs, | 
|  | 667 | }) | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 668 | if depErr, ok := err.(missingDependencyError); ok { | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 669 | missingDeps = append(missingDeps, depErr.missingDeps...) | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 670 | } else if err != nil { | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 671 | reportPathError(input.Context, err) | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 672 | } | 
|  | 673 | expandedSrcFiles = append(expandedSrcFiles, srcFiles...) | 
|  | 674 | } | 
| Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 675 |  | 
|  | 676 | return expandedSrcFiles, append(missingDeps, missingExcludeDeps...) | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 677 | } | 
|  | 678 |  | 
|  | 679 | type missingDependencyError struct { | 
|  | 680 | missingDeps []string | 
|  | 681 | } | 
|  | 682 |  | 
|  | 683 | func (e missingDependencyError) Error() string { | 
|  | 684 | return "missing dependencies: " + strings.Join(e.missingDeps, ", ") | 
|  | 685 | } | 
|  | 686 |  | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 687 | type sourcePathInput struct { | 
|  | 688 | context          ModuleWithDepsPathContext | 
|  | 689 | path             string | 
|  | 690 | expandedExcludes []string | 
|  | 691 | includeDirs      bool | 
|  | 692 | } | 
|  | 693 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 694 | // Expands one path string to Paths rooted from the module's local source | 
|  | 695 | // directory, excluding those listed in the expandedExcludes. | 
|  | 696 | // Expands globs, references to SourceFileProducer or OutputFileProducer modules using the ":name" and ":name{.tag}" syntax. | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 697 | func expandOneSrcPath(input sourcePathInput) (Paths, error) { | 
| Jooyung Han | 7607dd3 | 2020-07-05 10:23:14 +0900 | [diff] [blame] | 698 | excludePaths := func(paths Paths) Paths { | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 699 | if len(input.expandedExcludes) == 0 { | 
| Jooyung Han | 7607dd3 | 2020-07-05 10:23:14 +0900 | [diff] [blame] | 700 | return paths | 
|  | 701 | } | 
|  | 702 | remainder := make(Paths, 0, len(paths)) | 
|  | 703 | for _, p := range paths { | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 704 | if !InList(p.String(), input.expandedExcludes) { | 
| Jooyung Han | 7607dd3 | 2020-07-05 10:23:14 +0900 | [diff] [blame] | 705 | remainder = append(remainder, p) | 
|  | 706 | } | 
|  | 707 | } | 
|  | 708 | return remainder | 
|  | 709 | } | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 710 | if m, t := SrcIsModuleWithTag(input.path); m != "" { | 
|  | 711 | modulePaths, err := getPathsFromModuleDep(input.context, input.path, m, t) | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 712 | if err != nil { | 
|  | 713 | return nil, err | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 714 | } else { | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 715 | return excludePaths(modulePaths), nil | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 716 | } | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 717 | } else { | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 718 | p := pathForModuleSrc(input.context, input.path) | 
|  | 719 | if pathtools.IsGlob(input.path) { | 
|  | 720 | paths := GlobFiles(input.context, p.String(), input.expandedExcludes) | 
|  | 721 | return PathsWithModuleSrcSubDir(input.context, paths, ""), nil | 
|  | 722 | } else { | 
|  | 723 | if exists, _, err := input.context.Config().fs.Exists(p.String()); err != nil { | 
|  | 724 | ReportPathErrorf(input.context, "%s: %s", p, err.Error()) | 
|  | 725 | } else if !exists && !input.context.Config().TestAllowNonExistentPaths { | 
|  | 726 | ReportPathErrorf(input.context, "module source path %q does not exist", p) | 
|  | 727 | } else if !input.includeDirs { | 
|  | 728 | if isDir, err := input.context.Config().fs.IsDir(p.String()); exists && err != nil { | 
|  | 729 | ReportPathErrorf(input.context, "%s: %s", p, err.Error()) | 
|  | 730 | } else if isDir { | 
|  | 731 | ReportPathErrorf(input.context, "module source path %q is a directory", p) | 
|  | 732 | } | 
|  | 733 | } | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 734 |  | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 735 | if InList(p.String(), input.expandedExcludes) { | 
|  | 736 | return nil, nil | 
|  | 737 | } | 
|  | 738 | return Paths{p}, nil | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 739 | } | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 740 | } | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 741 | } | 
|  | 742 |  | 
|  | 743 | // pathsForModuleSrcFromFullPath returns Paths rooted from the module's local | 
|  | 744 | // source directory, but strip the local source directory from the beginning of | 
| Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 745 | // each string. If incDirs is false, strip paths with a trailing '/' from the list. | 
| Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 746 | // It intended for use in globs that only list files that exist, so it allows '$' in | 
|  | 747 | // filenames. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 748 | func pathsForModuleSrcFromFullPath(ctx EarlyModulePathContext, paths []string, incDirs bool) Paths { | 
| Lukacs T. Berki | f7e36d8 | 2021-08-16 17:05:09 +0200 | [diff] [blame] | 749 | prefix := ctx.ModuleDir() + "/" | 
| Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 750 | if prefix == "./" { | 
|  | 751 | prefix = "" | 
|  | 752 | } | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 753 | ret := make(Paths, 0, len(paths)) | 
|  | 754 | for _, p := range paths { | 
| Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 755 | if !incDirs && strings.HasSuffix(p, "/") { | 
|  | 756 | continue | 
|  | 757 | } | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 758 | path := filepath.Clean(p) | 
|  | 759 | if !strings.HasPrefix(path, prefix) { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 760 | ReportPathErrorf(ctx, "Path %q is not in module source directory %q", p, prefix) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 761 | continue | 
|  | 762 | } | 
| Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 763 |  | 
| Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 764 | srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):]) | 
| Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 765 | if err != nil { | 
|  | 766 | reportPathError(ctx, err) | 
|  | 767 | continue | 
|  | 768 | } | 
|  | 769 |  | 
| Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 770 | srcPath.basePath.rel = srcPath.path | 
| Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 771 |  | 
| Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 772 | ret = append(ret, srcPath) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 773 | } | 
|  | 774 | return ret | 
|  | 775 | } | 
|  | 776 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 777 | // PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's local source | 
|  | 778 | // directory. If input is nil, use the default if it exists.  If input is empty, returns nil. | 
|  | 779 | func PathsWithOptionalDefaultForModuleSrc(ctx ModuleMissingDepsPathContext, input []string, def string) Paths { | 
| Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 780 | if input != nil { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 781 | return PathsForModuleSrc(ctx, input) | 
|  | 782 | } | 
|  | 783 | // Use Glob so that if the default doesn't exist, a dependency is added so that when it | 
|  | 784 | // is created, we're run again. | 
| Lukacs T. Berki | f7e36d8 | 2021-08-16 17:05:09 +0200 | [diff] [blame] | 785 | path := filepath.Join(ctx.ModuleDir(), def) | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 786 | return Glob(ctx, path, nil) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 787 | } | 
|  | 788 |  | 
|  | 789 | // Strings returns the Paths in string form | 
|  | 790 | func (p Paths) Strings() []string { | 
|  | 791 | if p == nil { | 
|  | 792 | return nil | 
|  | 793 | } | 
|  | 794 | ret := make([]string, len(p)) | 
|  | 795 | for i, path := range p { | 
|  | 796 | ret[i] = path.String() | 
|  | 797 | } | 
|  | 798 | return ret | 
|  | 799 | } | 
|  | 800 |  | 
| Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 801 | func CopyOfPaths(paths Paths) Paths { | 
|  | 802 | return append(Paths(nil), paths...) | 
|  | 803 | } | 
|  | 804 |  | 
| Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 805 | // FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each.  It | 
|  | 806 | // modifies the Paths slice contents in place, and returns a subslice of the original slice. | 
| Dan Willemsen | fe92c96 | 2017-08-29 12:28:37 -0700 | [diff] [blame] | 807 | func FirstUniquePaths(list Paths) Paths { | 
| Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 808 | // 128 was chosen based on BenchmarkFirstUniquePaths results. | 
|  | 809 | if len(list) > 128 { | 
|  | 810 | return firstUniquePathsMap(list) | 
|  | 811 | } | 
|  | 812 | return firstUniquePathsList(list) | 
|  | 813 | } | 
|  | 814 |  | 
| Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 815 | // SortedUniquePaths returns all unique elements of a Paths in sorted order.  It modifies the | 
|  | 816 | // Paths slice contents in place, and returns a subslice of the original slice. | 
| Jiyong Park | 33c7736 | 2020-05-29 22:00:16 +0900 | [diff] [blame] | 817 | func SortedUniquePaths(list Paths) Paths { | 
|  | 818 | unique := FirstUniquePaths(list) | 
|  | 819 | sort.Slice(unique, func(i, j int) bool { | 
|  | 820 | return unique[i].String() < unique[j].String() | 
|  | 821 | }) | 
|  | 822 | return unique | 
|  | 823 | } | 
|  | 824 |  | 
| Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 825 | func firstUniquePathsList(list Paths) Paths { | 
| Dan Willemsen | fe92c96 | 2017-08-29 12:28:37 -0700 | [diff] [blame] | 826 | k := 0 | 
|  | 827 | outer: | 
|  | 828 | for i := 0; i < len(list); i++ { | 
|  | 829 | for j := 0; j < k; j++ { | 
|  | 830 | if list[i] == list[j] { | 
|  | 831 | continue outer | 
|  | 832 | } | 
|  | 833 | } | 
|  | 834 | list[k] = list[i] | 
|  | 835 | k++ | 
|  | 836 | } | 
|  | 837 | return list[:k] | 
|  | 838 | } | 
|  | 839 |  | 
| Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 840 | func firstUniquePathsMap(list Paths) Paths { | 
|  | 841 | k := 0 | 
|  | 842 | seen := make(map[Path]bool, len(list)) | 
|  | 843 | for i := 0; i < len(list); i++ { | 
|  | 844 | if seen[list[i]] { | 
|  | 845 | continue | 
|  | 846 | } | 
|  | 847 | seen[list[i]] = true | 
|  | 848 | list[k] = list[i] | 
|  | 849 | k++ | 
|  | 850 | } | 
|  | 851 | return list[:k] | 
|  | 852 | } | 
|  | 853 |  | 
| Colin Cross | 5d58395 | 2020-11-24 16:21:24 -0800 | [diff] [blame] | 854 | // FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each.  It | 
|  | 855 | // modifies the InstallPaths slice contents in place, and returns a subslice of the original slice. | 
|  | 856 | func FirstUniqueInstallPaths(list InstallPaths) InstallPaths { | 
|  | 857 | // 128 was chosen based on BenchmarkFirstUniquePaths results. | 
|  | 858 | if len(list) > 128 { | 
|  | 859 | return firstUniqueInstallPathsMap(list) | 
|  | 860 | } | 
|  | 861 | return firstUniqueInstallPathsList(list) | 
|  | 862 | } | 
|  | 863 |  | 
|  | 864 | func firstUniqueInstallPathsList(list InstallPaths) InstallPaths { | 
|  | 865 | k := 0 | 
|  | 866 | outer: | 
|  | 867 | for i := 0; i < len(list); i++ { | 
|  | 868 | for j := 0; j < k; j++ { | 
|  | 869 | if list[i] == list[j] { | 
|  | 870 | continue outer | 
|  | 871 | } | 
|  | 872 | } | 
|  | 873 | list[k] = list[i] | 
|  | 874 | k++ | 
|  | 875 | } | 
|  | 876 | return list[:k] | 
|  | 877 | } | 
|  | 878 |  | 
|  | 879 | func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths { | 
|  | 880 | k := 0 | 
|  | 881 | seen := make(map[InstallPath]bool, len(list)) | 
|  | 882 | for i := 0; i < len(list); i++ { | 
|  | 883 | if seen[list[i]] { | 
|  | 884 | continue | 
|  | 885 | } | 
|  | 886 | seen[list[i]] = true | 
|  | 887 | list[k] = list[i] | 
|  | 888 | k++ | 
|  | 889 | } | 
|  | 890 | return list[:k] | 
|  | 891 | } | 
|  | 892 |  | 
| Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 893 | // LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each.  It | 
|  | 894 | // modifies the Paths slice contents in place, and returns a subslice of the original slice. | 
|  | 895 | func LastUniquePaths(list Paths) Paths { | 
|  | 896 | totalSkip := 0 | 
|  | 897 | for i := len(list) - 1; i >= totalSkip; i-- { | 
|  | 898 | skip := 0 | 
|  | 899 | for j := i - 1; j >= totalSkip; j-- { | 
|  | 900 | if list[i] == list[j] { | 
|  | 901 | skip++ | 
|  | 902 | } else { | 
|  | 903 | list[j+skip] = list[j] | 
|  | 904 | } | 
|  | 905 | } | 
|  | 906 | totalSkip += skip | 
|  | 907 | } | 
|  | 908 | return list[totalSkip:] | 
|  | 909 | } | 
|  | 910 |  | 
| Colin Cross | a140bb0 | 2018-04-17 10:52:26 -0700 | [diff] [blame] | 911 | // ReversePaths returns a copy of a Paths in reverse order. | 
|  | 912 | func ReversePaths(list Paths) Paths { | 
|  | 913 | if list == nil { | 
|  | 914 | return nil | 
|  | 915 | } | 
|  | 916 | ret := make(Paths, len(list)) | 
|  | 917 | for i := range list { | 
|  | 918 | ret[i] = list[len(list)-1-i] | 
|  | 919 | } | 
|  | 920 | return ret | 
|  | 921 | } | 
|  | 922 |  | 
| Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 923 | func indexPathList(s Path, list []Path) int { | 
|  | 924 | for i, l := range list { | 
|  | 925 | if l == s { | 
|  | 926 | return i | 
|  | 927 | } | 
|  | 928 | } | 
|  | 929 |  | 
|  | 930 | return -1 | 
|  | 931 | } | 
|  | 932 |  | 
|  | 933 | func inPathList(p Path, list []Path) bool { | 
|  | 934 | return indexPathList(p, list) != -1 | 
|  | 935 | } | 
|  | 936 |  | 
|  | 937 | func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) { | 
| Paul Duffin | 57b9e1d | 2019-12-13 00:03:35 +0000 | [diff] [blame] | 938 | return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) }) | 
|  | 939 | } | 
|  | 940 |  | 
|  | 941 | func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) { | 
| Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 942 | for _, l := range list { | 
| Paul Duffin | 57b9e1d | 2019-12-13 00:03:35 +0000 | [diff] [blame] | 943 | if predicate(l) { | 
| Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 944 | filtered = append(filtered, l) | 
|  | 945 | } else { | 
|  | 946 | remainder = append(remainder, l) | 
|  | 947 | } | 
|  | 948 | } | 
|  | 949 |  | 
|  | 950 | return | 
|  | 951 | } | 
|  | 952 |  | 
| Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 953 | // HasExt returns true of any of the paths have extension ext, otherwise false | 
|  | 954 | func (p Paths) HasExt(ext string) bool { | 
|  | 955 | for _, path := range p { | 
|  | 956 | if path.Ext() == ext { | 
|  | 957 | return true | 
|  | 958 | } | 
|  | 959 | } | 
|  | 960 |  | 
|  | 961 | return false | 
|  | 962 | } | 
|  | 963 |  | 
|  | 964 | // FilterByExt returns the subset of the paths that have extension ext | 
|  | 965 | func (p Paths) FilterByExt(ext string) Paths { | 
|  | 966 | ret := make(Paths, 0, len(p)) | 
|  | 967 | for _, path := range p { | 
|  | 968 | if path.Ext() == ext { | 
|  | 969 | ret = append(ret, path) | 
|  | 970 | } | 
|  | 971 | } | 
|  | 972 | return ret | 
|  | 973 | } | 
|  | 974 |  | 
|  | 975 | // FilterOutByExt returns the subset of the paths that do not have extension ext | 
|  | 976 | func (p Paths) FilterOutByExt(ext string) Paths { | 
|  | 977 | ret := make(Paths, 0, len(p)) | 
|  | 978 | for _, path := range p { | 
|  | 979 | if path.Ext() != ext { | 
|  | 980 | ret = append(ret, path) | 
|  | 981 | } | 
|  | 982 | } | 
|  | 983 | return ret | 
|  | 984 | } | 
|  | 985 |  | 
| Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 986 | // DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory | 
|  | 987 | // (including subdirectories) are in a contiguous subslice of the list, and can be found in | 
|  | 988 | // O(log(N)) time using a binary search on the directory prefix. | 
|  | 989 | type DirectorySortedPaths Paths | 
|  | 990 |  | 
|  | 991 | func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths { | 
|  | 992 | ret := append(DirectorySortedPaths(nil), paths...) | 
|  | 993 | sort.Slice(ret, func(i, j int) bool { | 
|  | 994 | return ret[i].String() < ret[j].String() | 
|  | 995 | }) | 
|  | 996 | return ret | 
|  | 997 | } | 
|  | 998 |  | 
|  | 999 | // PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries | 
|  | 1000 | // that are in the specified directory and its subdirectories. | 
|  | 1001 | func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths { | 
|  | 1002 | prefix := filepath.Clean(dir) + "/" | 
|  | 1003 | start := sort.Search(len(p), func(i int) bool { | 
|  | 1004 | return prefix < p[i].String() | 
|  | 1005 | }) | 
|  | 1006 |  | 
|  | 1007 | ret := p[start:] | 
|  | 1008 |  | 
|  | 1009 | end := sort.Search(len(ret), func(i int) bool { | 
|  | 1010 | return !strings.HasPrefix(ret[i].String(), prefix) | 
|  | 1011 | }) | 
|  | 1012 |  | 
|  | 1013 | ret = ret[:end] | 
|  | 1014 |  | 
|  | 1015 | return Paths(ret) | 
|  | 1016 | } | 
|  | 1017 |  | 
| Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 1018 | // WritablePaths is a slice of WritablePath, used for multiple outputs. | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1019 | type WritablePaths []WritablePath | 
|  | 1020 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1021 | // RelativeToTop creates a new WritablePaths containing the result of calling Path.RelativeToTop on | 
|  | 1022 | // each item in this slice. | 
|  | 1023 | func (p WritablePaths) RelativeToTop() WritablePaths { | 
|  | 1024 | ensureTestOnly() | 
|  | 1025 | if p == nil { | 
|  | 1026 | return p | 
|  | 1027 | } | 
|  | 1028 | ret := make(WritablePaths, len(p)) | 
|  | 1029 | for i, path := range p { | 
|  | 1030 | ret[i] = path.RelativeToTop().(WritablePath) | 
|  | 1031 | } | 
|  | 1032 | return ret | 
|  | 1033 | } | 
|  | 1034 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1035 | // Strings returns the string forms of the writable paths. | 
|  | 1036 | func (p WritablePaths) Strings() []string { | 
|  | 1037 | if p == nil { | 
|  | 1038 | return nil | 
|  | 1039 | } | 
|  | 1040 | ret := make([]string, len(p)) | 
|  | 1041 | for i, path := range p { | 
|  | 1042 | ret[i] = path.String() | 
|  | 1043 | } | 
|  | 1044 | return ret | 
|  | 1045 | } | 
|  | 1046 |  | 
| Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 1047 | // Paths returns the WritablePaths as a Paths | 
|  | 1048 | func (p WritablePaths) Paths() Paths { | 
|  | 1049 | if p == nil { | 
|  | 1050 | return nil | 
|  | 1051 | } | 
|  | 1052 | ret := make(Paths, len(p)) | 
|  | 1053 | for i, path := range p { | 
|  | 1054 | ret[i] = path | 
|  | 1055 | } | 
|  | 1056 | return ret | 
|  | 1057 | } | 
|  | 1058 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1059 | type basePath struct { | 
| Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1060 | path string | 
|  | 1061 | rel  string | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1062 | } | 
|  | 1063 |  | 
|  | 1064 | func (p basePath) Ext() string { | 
|  | 1065 | return filepath.Ext(p.path) | 
|  | 1066 | } | 
|  | 1067 |  | 
| Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 1068 | func (p basePath) Base() string { | 
|  | 1069 | return filepath.Base(p.path) | 
|  | 1070 | } | 
|  | 1071 |  | 
| Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 1072 | func (p basePath) Rel() string { | 
|  | 1073 | if p.rel != "" { | 
|  | 1074 | return p.rel | 
|  | 1075 | } | 
|  | 1076 | return p.path | 
|  | 1077 | } | 
|  | 1078 |  | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1079 | func (p basePath) String() string { | 
|  | 1080 | return p.path | 
|  | 1081 | } | 
|  | 1082 |  | 
| Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1083 | func (p basePath) withRel(rel string) basePath { | 
|  | 1084 | p.path = filepath.Join(p.path, rel) | 
|  | 1085 | p.rel = rel | 
|  | 1086 | return p | 
|  | 1087 | } | 
|  | 1088 |  | 
| Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1089 | func (p basePath) RelativeToTop() Path { | 
|  | 1090 | ensureTestOnly() | 
|  | 1091 | return p | 
|  | 1092 | } | 
|  | 1093 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1094 | // SourcePath is a Path representing a file path rooted from SrcDir | 
|  | 1095 | type SourcePath struct { | 
|  | 1096 | basePath | 
|  | 1097 | } | 
|  | 1098 |  | 
|  | 1099 | var _ Path = SourcePath{} | 
|  | 1100 |  | 
| Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1101 | func (p SourcePath) withRel(rel string) SourcePath { | 
|  | 1102 | p.basePath = p.basePath.withRel(rel) | 
|  | 1103 | return p | 
|  | 1104 | } | 
|  | 1105 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1106 | // safePathForSource is for paths that we expect are safe -- only for use by go | 
|  | 1107 | // code that is embedding ninja variables in paths | 
| Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 1108 | func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { | 
|  | 1109 | p, err := validateSafePath(pathComponents...) | 
| Cole Faust | 483d1f7 | 2023-01-09 14:35:27 -0800 | [diff] [blame] | 1110 | ret := SourcePath{basePath{p, ""}} | 
| Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 1111 | if err != nil { | 
|  | 1112 | return ret, err | 
|  | 1113 | } | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1114 |  | 
| Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 1115 | // absolute path already checked by validateSafePath | 
| Inseob Kim | 5eb7ee9 | 2022-04-27 10:30:34 +0900 | [diff] [blame] | 1116 | // special-case api surface gen files for now | 
|  | 1117 | if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") { | 
| Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 1118 | return ret, fmt.Errorf("source path %q is in output", ret.String()) | 
| Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 1119 | } | 
|  | 1120 |  | 
| Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 1121 | return ret, err | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1122 | } | 
|  | 1123 |  | 
| Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1124 | // pathForSource creates a SourcePath from pathComponents, but does not check that it exists. | 
|  | 1125 | func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { | 
| Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1126 | p, err := validatePath(pathComponents...) | 
| Cole Faust | 483d1f7 | 2023-01-09 14:35:27 -0800 | [diff] [blame] | 1127 | ret := SourcePath{basePath{p, ""}} | 
| Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1128 | if err != nil { | 
| Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1129 | return ret, err | 
| Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1130 | } | 
|  | 1131 |  | 
| Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 1132 | // absolute path already checked by validatePath | 
| Inseob Kim | 5eb7ee9 | 2022-04-27 10:30:34 +0900 | [diff] [blame] | 1133 | // special-case for now | 
|  | 1134 | if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") { | 
| Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 1135 | return ret, fmt.Errorf("source path %q is in output", ret.String()) | 
| Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1136 | } | 
|  | 1137 |  | 
| Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1138 | return ret, nil | 
|  | 1139 | } | 
|  | 1140 |  | 
|  | 1141 | // existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the | 
|  | 1142 | // path does not exist. | 
| Colin Cross | 662d614 | 2022-11-03 20:38:01 -0700 | [diff] [blame] | 1143 | func existsWithDependencies(ctx PathGlobContext, path SourcePath) (exists bool, err error) { | 
| Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1144 | var files []string | 
|  | 1145 |  | 
| Colin Cross | 662d614 | 2022-11-03 20:38:01 -0700 | [diff] [blame] | 1146 | // Use glob to produce proper dependencies, even though we only want | 
|  | 1147 | // a single file. | 
|  | 1148 | files, err = ctx.GlobWithDeps(path.String(), nil) | 
| Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1149 |  | 
|  | 1150 | if err != nil { | 
|  | 1151 | return false, fmt.Errorf("glob: %s", err.Error()) | 
|  | 1152 | } | 
|  | 1153 |  | 
|  | 1154 | return len(files) > 0, nil | 
|  | 1155 | } | 
|  | 1156 |  | 
|  | 1157 | // PathForSource joins the provided path components and validates that the result | 
|  | 1158 | // neither escapes the source dir nor is in the out dir. | 
|  | 1159 | // On error, it will return a usable, but invalid SourcePath, and report a ModuleError. | 
|  | 1160 | func PathForSource(ctx PathContext, pathComponents ...string) SourcePath { | 
|  | 1161 | path, err := pathForSource(ctx, pathComponents...) | 
|  | 1162 | if err != nil { | 
|  | 1163 | reportPathError(ctx, err) | 
|  | 1164 | } | 
|  | 1165 |  | 
| Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 1166 | if pathtools.IsGlob(path.String()) { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1167 | ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) | 
| Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 1168 | } | 
|  | 1169 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1170 | if modCtx, ok := ctx.(ModuleMissingDepsPathContext); ok && ctx.Config().AllowMissingDependencies() { | 
| Colin Cross | 662d614 | 2022-11-03 20:38:01 -0700 | [diff] [blame] | 1171 | exists, err := existsWithDependencies(modCtx, path) | 
| Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1172 | if err != nil { | 
|  | 1173 | reportPathError(ctx, err) | 
|  | 1174 | } | 
|  | 1175 | if !exists { | 
|  | 1176 | modCtx.AddMissingDependencies([]string{path.String()}) | 
|  | 1177 | } | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 1178 | } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1179 | ReportPathErrorf(ctx, "%s: %s", path, err.Error()) | 
| Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 1180 | } else if !exists && !ctx.Config().TestAllowNonExistentPaths { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1181 | ReportPathErrorf(ctx, "source path %q does not exist", path) | 
| Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1182 | } | 
|  | 1183 | return path | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1184 | } | 
|  | 1185 |  | 
| Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame] | 1186 | // PathForArbitraryOutput creates a path for the given components. Unlike PathForOutput, | 
|  | 1187 | // the path is relative to the root of the output folder, not the out/soong folder. | 
|  | 1188 | func PathForArbitraryOutput(ctx PathContext, pathComponents ...string) Path { | 
|  | 1189 | p, err := validatePath(pathComponents...) | 
|  | 1190 | if err != nil { | 
|  | 1191 | reportPathError(ctx, err) | 
|  | 1192 | } | 
|  | 1193 | return basePath{path: filepath.Join(ctx.Config().OutDir(), p)} | 
|  | 1194 | } | 
|  | 1195 |  | 
| Spandan Das | c6c10fa | 2022-10-21 21:52:13 +0000 | [diff] [blame] | 1196 | // MaybeExistentPathForSource joins the provided path components and validates that the result | 
|  | 1197 | // neither escapes the source dir nor is in the out dir. | 
|  | 1198 | // It does not validate whether the path exists. | 
|  | 1199 | func MaybeExistentPathForSource(ctx PathContext, pathComponents ...string) SourcePath { | 
|  | 1200 | path, err := pathForSource(ctx, pathComponents...) | 
|  | 1201 | if err != nil { | 
|  | 1202 | reportPathError(ctx, err) | 
|  | 1203 | } | 
|  | 1204 |  | 
|  | 1205 | if pathtools.IsGlob(path.String()) { | 
|  | 1206 | ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) | 
|  | 1207 | } | 
|  | 1208 | return path | 
|  | 1209 | } | 
|  | 1210 |  | 
| Liz Kammer | 7aa5288 | 2021-02-11 09:16:14 -0500 | [diff] [blame] | 1211 | // ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not* | 
|  | 1212 | // rooted from the module's local source directory, if the path exists, or an empty OptionalPath if | 
|  | 1213 | // it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state | 
|  | 1214 | // of the path changes. | 
| Colin Cross | 662d614 | 2022-11-03 20:38:01 -0700 | [diff] [blame] | 1215 | func ExistentPathForSource(ctx PathGlobContext, pathComponents ...string) OptionalPath { | 
| Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1216 | path, err := pathForSource(ctx, pathComponents...) | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1217 | if err != nil { | 
|  | 1218 | reportPathError(ctx, err) | 
| Martin Stjernholm | c32dd1c | 2021-09-15 02:39:00 +0100 | [diff] [blame] | 1219 | // No need to put the error message into the returned path since it has been reported already. | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1220 | return OptionalPath{} | 
|  | 1221 | } | 
| Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1222 |  | 
| Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 1223 | if pathtools.IsGlob(path.String()) { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1224 | ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) | 
| Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 1225 | return OptionalPath{} | 
|  | 1226 | } | 
|  | 1227 |  | 
| Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1228 | exists, err := existsWithDependencies(ctx, path) | 
| Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1229 | if err != nil { | 
|  | 1230 | reportPathError(ctx, err) | 
|  | 1231 | return OptionalPath{} | 
|  | 1232 | } | 
| Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1233 | if !exists { | 
| Martin Stjernholm | c32dd1c | 2021-09-15 02:39:00 +0100 | [diff] [blame] | 1234 | return InvalidOptionalPath(path.String() + " does not exist") | 
| Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1235 | } | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1236 | return OptionalPathForPath(path) | 
|  | 1237 | } | 
|  | 1238 |  | 
|  | 1239 | func (p SourcePath) String() string { | 
| Cole Faust | 483d1f7 | 2023-01-09 14:35:27 -0800 | [diff] [blame] | 1240 | if p.path == "" { | 
|  | 1241 | return "." | 
|  | 1242 | } | 
|  | 1243 | return p.path | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1244 | } | 
|  | 1245 |  | 
|  | 1246 | // Join creates a new SourcePath with paths... joined with the current path. The | 
|  | 1247 | // provided paths... may not use '..' to escape from the current path. | 
|  | 1248 | func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath { | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1249 | path, err := validatePath(paths...) | 
|  | 1250 | if err != nil { | 
|  | 1251 | reportPathError(ctx, err) | 
|  | 1252 | } | 
| Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1253 | return p.withRel(path) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1254 | } | 
|  | 1255 |  | 
| Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1256 | // join is like Join but does less path validation. | 
|  | 1257 | func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath { | 
|  | 1258 | path, err := validateSafePath(paths...) | 
|  | 1259 | if err != nil { | 
|  | 1260 | reportPathError(ctx, err) | 
|  | 1261 | } | 
|  | 1262 | return p.withRel(path) | 
|  | 1263 | } | 
|  | 1264 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1265 | // OverlayPath returns the overlay for `path' if it exists. This assumes that the | 
|  | 1266 | // SourcePath is the path to a resource overlay directory. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1267 | func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) OptionalPath { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1268 | var relDir string | 
| Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1269 | if srcPath, ok := path.(SourcePath); ok { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1270 | relDir = srcPath.path | 
|  | 1271 | } else { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1272 | ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path) | 
| Martin Stjernholm | c32dd1c | 2021-09-15 02:39:00 +0100 | [diff] [blame] | 1273 | // No need to put the error message into the returned path since it has been reported already. | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1274 | return OptionalPath{} | 
|  | 1275 | } | 
| Cole Faust | 483d1f7 | 2023-01-09 14:35:27 -0800 | [diff] [blame] | 1276 | dir := filepath.Join(p.path, relDir) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1277 | // Use Glob so that we are run again if the directory is added. | 
| Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 1278 | if pathtools.IsGlob(dir) { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1279 | ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir) | 
| Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 1280 | } | 
| Colin Cross | 461b445 | 2018-02-23 09:22:42 -0800 | [diff] [blame] | 1281 | paths, err := ctx.GlobWithDeps(dir, nil) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1282 | if err != nil { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1283 | ReportPathErrorf(ctx, "glob: %s", err.Error()) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1284 | return OptionalPath{} | 
|  | 1285 | } | 
|  | 1286 | if len(paths) == 0 { | 
| Martin Stjernholm | c32dd1c | 2021-09-15 02:39:00 +0100 | [diff] [blame] | 1287 | return InvalidOptionalPath(dir + " does not exist") | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1288 | } | 
| Cole Faust | 483d1f7 | 2023-01-09 14:35:27 -0800 | [diff] [blame] | 1289 | return OptionalPathForPath(PathForSource(ctx, paths[0])) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1290 | } | 
|  | 1291 |  | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1292 | // OutputPath is a Path representing an intermediates file path rooted from the build directory | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1293 | type OutputPath struct { | 
|  | 1294 | basePath | 
| Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1295 |  | 
| Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 1296 | // The soong build directory, i.e. Config.SoongOutDir() | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1297 | soongOutDir string | 
| Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1298 |  | 
| Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1299 | fullPath string | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1300 | } | 
|  | 1301 |  | 
| Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1302 | func (p OutputPath) withRel(rel string) OutputPath { | 
| Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1303 | p.basePath = p.basePath.withRel(rel) | 
| Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1304 | p.fullPath = filepath.Join(p.fullPath, rel) | 
| Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1305 | return p | 
|  | 1306 | } | 
|  | 1307 |  | 
| Colin Cross | 3063b78 | 2018-08-15 11:19:12 -0700 | [diff] [blame] | 1308 | func (p OutputPath) WithoutRel() OutputPath { | 
|  | 1309 | p.basePath.rel = filepath.Base(p.basePath.path) | 
|  | 1310 | return p | 
|  | 1311 | } | 
|  | 1312 |  | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1313 | func (p OutputPath) getSoongOutDir() string { | 
|  | 1314 | return p.soongOutDir | 
| Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1315 | } | 
|  | 1316 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1317 | func (p OutputPath) RelativeToTop() Path { | 
|  | 1318 | return p.outputPathRelativeToTop() | 
|  | 1319 | } | 
|  | 1320 |  | 
|  | 1321 | func (p OutputPath) outputPathRelativeToTop() OutputPath { | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1322 | p.fullPath = StringPathRelativeToTop(p.soongOutDir, p.fullPath) | 
|  | 1323 | p.soongOutDir = OutSoongDir | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1324 | return p | 
|  | 1325 | } | 
|  | 1326 |  | 
| Paul Duffin | 0267d49 | 2021-02-02 10:05:52 +0000 | [diff] [blame] | 1327 | func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { | 
|  | 1328 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) | 
|  | 1329 | } | 
|  | 1330 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1331 | var _ Path = OutputPath{} | 
| Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1332 | var _ WritablePath = OutputPath{} | 
| Paul Duffin | 0267d49 | 2021-02-02 10:05:52 +0000 | [diff] [blame] | 1333 | var _ objPathProvider = OutputPath{} | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1334 |  | 
| Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 1335 | // toolDepPath is a Path representing a dependency of the build tool. | 
|  | 1336 | type toolDepPath struct { | 
|  | 1337 | basePath | 
|  | 1338 | } | 
|  | 1339 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1340 | func (t toolDepPath) RelativeToTop() Path { | 
|  | 1341 | ensureTestOnly() | 
|  | 1342 | return t | 
|  | 1343 | } | 
|  | 1344 |  | 
| Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 1345 | var _ Path = toolDepPath{} | 
|  | 1346 |  | 
|  | 1347 | // pathForBuildToolDep returns a toolDepPath representing the given path string. | 
|  | 1348 | // There is no validation for the path, as it is "trusted": It may fail | 
|  | 1349 | // normal validation checks. For example, it may be an absolute path. | 
|  | 1350 | // Only use this function to construct paths for dependencies of the build | 
|  | 1351 | // tool invocation. | 
|  | 1352 | func pathForBuildToolDep(ctx PathContext, path string) toolDepPath { | 
| Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1353 | return toolDepPath{basePath{path, ""}} | 
| Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 1354 | } | 
|  | 1355 |  | 
| Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1356 | // PathForOutput joins the provided paths and returns an OutputPath that is | 
|  | 1357 | // validated to not escape the build dir. | 
|  | 1358 | // On error, it will return a usable, but invalid OutputPath, and report a ModuleError. | 
|  | 1359 | func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath { | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1360 | path, err := validatePath(pathComponents...) | 
|  | 1361 | if err != nil { | 
|  | 1362 | reportPathError(ctx, err) | 
|  | 1363 | } | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1364 | fullPath := filepath.Join(ctx.Config().soongOutDir, path) | 
| Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1365 | path = fullPath[len(fullPath)-len(path):] | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1366 | return OutputPath{basePath{path, ""}, ctx.Config().soongOutDir, fullPath} | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1367 | } | 
|  | 1368 |  | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1369 | // PathsForOutput returns Paths rooted from soongOutDir | 
| Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1370 | func PathsForOutput(ctx PathContext, paths []string) WritablePaths { | 
|  | 1371 | ret := make(WritablePaths, len(paths)) | 
|  | 1372 | for i, path := range paths { | 
|  | 1373 | ret[i] = PathForOutput(ctx, path) | 
|  | 1374 | } | 
|  | 1375 | return ret | 
|  | 1376 | } | 
|  | 1377 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1378 | func (p OutputPath) writablePath() {} | 
|  | 1379 |  | 
|  | 1380 | func (p OutputPath) String() string { | 
| Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1381 | return p.fullPath | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1382 | } | 
|  | 1383 |  | 
|  | 1384 | // Join creates a new OutputPath with paths... joined with the current path. The | 
|  | 1385 | // provided paths... may not use '..' to escape from the current path. | 
|  | 1386 | func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath { | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1387 | path, err := validatePath(paths...) | 
|  | 1388 | if err != nil { | 
|  | 1389 | reportPathError(ctx, err) | 
|  | 1390 | } | 
| Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1391 | return p.withRel(path) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1392 | } | 
|  | 1393 |  | 
| Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1394 | // ReplaceExtension creates a new OutputPath with the extension replaced with ext. | 
|  | 1395 | func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { | 
|  | 1396 | if strings.Contains(ext, "/") { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1397 | ReportPathErrorf(ctx, "extension %q cannot contain /", ext) | 
| Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1398 | } | 
|  | 1399 | ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext)) | 
| Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1400 | ret.rel = pathtools.ReplaceExtension(p.rel, ext) | 
| Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1401 | return ret | 
|  | 1402 | } | 
|  | 1403 |  | 
| Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1404 | // InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths. | 
|  | 1405 | func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath { | 
|  | 1406 | path, err := validatePath(paths...) | 
|  | 1407 | if err != nil { | 
|  | 1408 | reportPathError(ctx, err) | 
|  | 1409 | } | 
|  | 1410 |  | 
|  | 1411 | ret := PathForOutput(ctx, filepath.Dir(p.path), path) | 
| Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1412 | ret.rel = filepath.Join(filepath.Dir(p.rel), path) | 
| Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1413 | return ret | 
|  | 1414 | } | 
|  | 1415 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1416 | // PathForIntermediates returns an OutputPath representing the top-level | 
|  | 1417 | // intermediates directory. | 
|  | 1418 | func PathForIntermediates(ctx PathContext, paths ...string) OutputPath { | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1419 | path, err := validatePath(paths...) | 
|  | 1420 | if err != nil { | 
|  | 1421 | reportPathError(ctx, err) | 
|  | 1422 | } | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1423 | return PathForOutput(ctx, ".intermediates", path) | 
|  | 1424 | } | 
|  | 1425 |  | 
| Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1426 | var _ genPathProvider = SourcePath{} | 
|  | 1427 | var _ objPathProvider = SourcePath{} | 
|  | 1428 | var _ resPathProvider = SourcePath{} | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1429 |  | 
| Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1430 | // PathForModuleSrc returns a Path representing the paths... under the | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1431 | // module's local source directory. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1432 | func PathForModuleSrc(ctx ModuleMissingDepsPathContext, pathComponents ...string) Path { | 
| Paul Duffin | 407501b | 2021-07-09 16:56:35 +0100 | [diff] [blame] | 1433 | // Just join the components textually just to make sure that it does not corrupt a fully qualified | 
|  | 1434 | // module reference, e.g. if the pathComponents is "://other:foo" then using filepath.Join() or | 
|  | 1435 | // validatePath() will corrupt it, e.g. replace "//" with "/". If the path is not a module | 
|  | 1436 | // reference then it will be validated by expandOneSrcPath anyway when it calls expandOneSrcPath. | 
|  | 1437 | p := strings.Join(pathComponents, string(filepath.Separator)) | 
| Liz Kammer | 619be46 | 2022-01-28 15:13:39 -0500 | [diff] [blame] | 1438 | paths, err := expandOneSrcPath(sourcePathInput{context: ctx, path: p, includeDirs: true}) | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1439 | if err != nil { | 
|  | 1440 | if depErr, ok := err.(missingDependencyError); ok { | 
|  | 1441 | if ctx.Config().AllowMissingDependencies() { | 
|  | 1442 | ctx.AddMissingDependencies(depErr.missingDeps) | 
|  | 1443 | } else { | 
|  | 1444 | ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error()) | 
|  | 1445 | } | 
|  | 1446 | } else { | 
|  | 1447 | reportPathError(ctx, err) | 
|  | 1448 | } | 
|  | 1449 | return nil | 
|  | 1450 | } else if len(paths) == 0 { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1451 | ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p) | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1452 | return nil | 
|  | 1453 | } else if len(paths) > 1 { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1454 | ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths)) | 
| Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1455 | } | 
|  | 1456 | return paths[0] | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1457 | } | 
|  | 1458 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1459 | func pathForModuleSrc(ctx EarlyModulePathContext, paths ...string) SourcePath { | 
| Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1460 | p, err := validatePath(paths...) | 
|  | 1461 | if err != nil { | 
|  | 1462 | reportPathError(ctx, err) | 
|  | 1463 | } | 
|  | 1464 |  | 
|  | 1465 | path, err := pathForSource(ctx, ctx.ModuleDir(), p) | 
|  | 1466 | if err != nil { | 
|  | 1467 | reportPathError(ctx, err) | 
|  | 1468 | } | 
|  | 1469 |  | 
|  | 1470 | path.basePath.rel = p | 
|  | 1471 |  | 
|  | 1472 | return path | 
|  | 1473 | } | 
|  | 1474 |  | 
| Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1475 | // PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path | 
|  | 1476 | // will return the path relative to subDir in the module's source directory.  If any input paths are not located | 
|  | 1477 | // inside subDir then a path error will be reported. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1478 | func PathsWithModuleSrcSubDir(ctx EarlyModulePathContext, paths Paths, subDir string) Paths { | 
| Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1479 | paths = append(Paths(nil), paths...) | 
| Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1480 | subDirFullPath := pathForModuleSrc(ctx, subDir) | 
| Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1481 | for i, path := range paths { | 
|  | 1482 | rel := Rel(ctx, subDirFullPath.String(), path.String()) | 
|  | 1483 | paths[i] = subDirFullPath.join(ctx, rel) | 
|  | 1484 | } | 
|  | 1485 | return paths | 
|  | 1486 | } | 
|  | 1487 |  | 
|  | 1488 | // PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the | 
|  | 1489 | // module's source directory.  If the input path is not located inside subDir then a path error will be reported. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1490 | func PathWithModuleSrcSubDir(ctx EarlyModulePathContext, path Path, subDir string) Path { | 
| Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1491 | subDirFullPath := pathForModuleSrc(ctx, subDir) | 
| Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1492 | rel := Rel(ctx, subDirFullPath.String(), path.String()) | 
|  | 1493 | return subDirFullPath.Join(ctx, rel) | 
|  | 1494 | } | 
|  | 1495 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1496 | // OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a | 
|  | 1497 | // valid path if p is non-nil. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1498 | func OptionalPathForModuleSrc(ctx ModuleMissingDepsPathContext, p *string) OptionalPath { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1499 | if p == nil { | 
|  | 1500 | return OptionalPath{} | 
|  | 1501 | } | 
|  | 1502 | return OptionalPathForPath(PathForModuleSrc(ctx, *p)) | 
|  | 1503 | } | 
|  | 1504 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1505 | func (p SourcePath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath { | 
| Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 1506 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1507 | } | 
|  | 1508 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1509 | func (p SourcePath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { | 
| Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 1510 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1511 | } | 
|  | 1512 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1513 | func (p SourcePath) resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1514 | // TODO: Use full directory if the new ctx is not the current ctx? | 
|  | 1515 | return PathForModuleRes(ctx, p.path, name) | 
|  | 1516 | } | 
|  | 1517 |  | 
|  | 1518 | // ModuleOutPath is a Path representing a module's output directory. | 
|  | 1519 | type ModuleOutPath struct { | 
|  | 1520 | OutputPath | 
|  | 1521 | } | 
|  | 1522 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1523 | func (p ModuleOutPath) RelativeToTop() Path { | 
|  | 1524 | p.OutputPath = p.outputPathRelativeToTop() | 
|  | 1525 | return p | 
|  | 1526 | } | 
|  | 1527 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1528 | var _ Path = ModuleOutPath{} | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1529 | var _ WritablePath = ModuleOutPath{} | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1530 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1531 | func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { | 
| Pete Bentley | fcf55bf | 2019-08-16 20:14:32 +0100 | [diff] [blame] | 1532 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) | 
|  | 1533 | } | 
|  | 1534 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1535 | // ModuleOutPathContext Subset of ModuleContext functions necessary for output path methods. | 
|  | 1536 | type ModuleOutPathContext interface { | 
|  | 1537 | PathContext | 
|  | 1538 |  | 
|  | 1539 | ModuleName() string | 
|  | 1540 | ModuleDir() string | 
|  | 1541 | ModuleSubDir() string | 
| Inseob Kim | 8ff69de | 2023-06-16 14:19:33 +0900 | [diff] [blame] | 1542 | SoongConfigTraceHash() string | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1543 | } | 
|  | 1544 |  | 
|  | 1545 | func pathForModuleOut(ctx ModuleOutPathContext) OutputPath { | 
| Inseob Kim | 8ff69de | 2023-06-16 14:19:33 +0900 | [diff] [blame] | 1546 | return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir(), ctx.SoongConfigTraceHash()) | 
| Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1547 | } | 
|  | 1548 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1549 | // PathForModuleOut returns a Path representing the paths... under the module's | 
|  | 1550 | // output directory. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1551 | func PathForModuleOut(ctx ModuleOutPathContext, paths ...string) ModuleOutPath { | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1552 | p, err := validatePath(paths...) | 
|  | 1553 | if err != nil { | 
|  | 1554 | reportPathError(ctx, err) | 
|  | 1555 | } | 
| Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1556 | return ModuleOutPath{ | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1557 | OutputPath: pathForModuleOut(ctx).withRel(p), | 
| Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1558 | } | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1559 | } | 
|  | 1560 |  | 
|  | 1561 | // ModuleGenPath is a Path representing the 'gen' directory in a module's output | 
|  | 1562 | // directory. Mainly used for generated sources. | 
|  | 1563 | type ModuleGenPath struct { | 
|  | 1564 | ModuleOutPath | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1565 | } | 
|  | 1566 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1567 | func (p ModuleGenPath) RelativeToTop() Path { | 
|  | 1568 | p.OutputPath = p.outputPathRelativeToTop() | 
|  | 1569 | return p | 
|  | 1570 | } | 
|  | 1571 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1572 | var _ Path = ModuleGenPath{} | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1573 | var _ WritablePath = ModuleGenPath{} | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1574 | var _ genPathProvider = ModuleGenPath{} | 
|  | 1575 | var _ objPathProvider = ModuleGenPath{} | 
|  | 1576 |  | 
|  | 1577 | // PathForModuleGen returns a Path representing the paths... under the module's | 
|  | 1578 | // `gen' directory. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1579 | func PathForModuleGen(ctx ModuleOutPathContext, paths ...string) ModuleGenPath { | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1580 | p, err := validatePath(paths...) | 
|  | 1581 | if err != nil { | 
|  | 1582 | reportPathError(ctx, err) | 
|  | 1583 | } | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1584 | return ModuleGenPath{ | 
| Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1585 | ModuleOutPath: ModuleOutPath{ | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1586 | OutputPath: pathForModuleOut(ctx).withRel("gen").withRel(p), | 
| Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1587 | }, | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1588 | } | 
|  | 1589 | } | 
|  | 1590 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1591 | func (p ModuleGenPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1592 | // TODO: make a different path for local vs remote generated files? | 
| Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 1593 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1594 | } | 
|  | 1595 |  | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1596 | func (p ModuleGenPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1597 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) | 
|  | 1598 | } | 
|  | 1599 |  | 
|  | 1600 | // ModuleObjPath is a Path representing the 'obj' directory in a module's output | 
|  | 1601 | // directory. Used for compiled objects. | 
|  | 1602 | type ModuleObjPath struct { | 
|  | 1603 | ModuleOutPath | 
|  | 1604 | } | 
|  | 1605 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1606 | func (p ModuleObjPath) RelativeToTop() Path { | 
|  | 1607 | p.OutputPath = p.outputPathRelativeToTop() | 
|  | 1608 | return p | 
|  | 1609 | } | 
|  | 1610 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1611 | var _ Path = ModuleObjPath{} | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1612 | var _ WritablePath = ModuleObjPath{} | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1613 |  | 
|  | 1614 | // PathForModuleObj returns a Path representing the paths... under the module's | 
|  | 1615 | // 'obj' directory. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1616 | func PathForModuleObj(ctx ModuleOutPathContext, pathComponents ...string) ModuleObjPath { | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1617 | p, err := validatePath(pathComponents...) | 
|  | 1618 | if err != nil { | 
|  | 1619 | reportPathError(ctx, err) | 
|  | 1620 | } | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1621 | return ModuleObjPath{PathForModuleOut(ctx, "obj", p)} | 
|  | 1622 | } | 
|  | 1623 |  | 
|  | 1624 | // ModuleResPath is a a Path representing the 'res' directory in a module's | 
|  | 1625 | // output directory. | 
|  | 1626 | type ModuleResPath struct { | 
|  | 1627 | ModuleOutPath | 
|  | 1628 | } | 
|  | 1629 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1630 | func (p ModuleResPath) RelativeToTop() Path { | 
|  | 1631 | p.OutputPath = p.outputPathRelativeToTop() | 
|  | 1632 | return p | 
|  | 1633 | } | 
|  | 1634 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1635 | var _ Path = ModuleResPath{} | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1636 | var _ WritablePath = ModuleResPath{} | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1637 |  | 
|  | 1638 | // PathForModuleRes returns a Path representing the paths... under the module's | 
|  | 1639 | // 'res' directory. | 
| Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1640 | func PathForModuleRes(ctx ModuleOutPathContext, pathComponents ...string) ModuleResPath { | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1641 | p, err := validatePath(pathComponents...) | 
|  | 1642 | if err != nil { | 
|  | 1643 | reportPathError(ctx, err) | 
|  | 1644 | } | 
|  | 1645 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1646 | return ModuleResPath{PathForModuleOut(ctx, "res", p)} | 
|  | 1647 | } | 
|  | 1648 |  | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1649 | // InstallPath is a Path representing a installed file path rooted from the build directory | 
|  | 1650 | type InstallPath struct { | 
|  | 1651 | basePath | 
| Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1652 |  | 
| Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 1653 | // The soong build directory, i.e. Config.SoongOutDir() | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1654 | soongOutDir string | 
| Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1655 |  | 
| Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1656 | // partitionDir is the part of the InstallPath that is automatically determined according to the context. | 
|  | 1657 | // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules. | 
|  | 1658 | partitionDir string | 
|  | 1659 |  | 
| Colin Cross | b1692a3 | 2021-10-25 15:39:01 -0700 | [diff] [blame] | 1660 | partition string | 
|  | 1661 |  | 
| Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1662 | // makePath indicates whether this path is for Soong (false) or Make (true). | 
|  | 1663 | makePath bool | 
| Colin Cross | c0e42d5 | 2024-02-01 16:42:36 -0800 | [diff] [blame] | 1664 |  | 
|  | 1665 | fullPath string | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1666 | } | 
|  | 1667 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1668 | // Will panic if called from outside a test environment. | 
|  | 1669 | func ensureTestOnly() { | 
| Martin Stjernholm | 32312eb | 2021-03-27 18:54:49 +0000 | [diff] [blame] | 1670 | if PrefixInList(os.Args, "-test.") { | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1671 | return | 
|  | 1672 | } | 
| Martin Stjernholm | 32312eb | 2021-03-27 18:54:49 +0000 | [diff] [blame] | 1673 | panic(fmt.Errorf("Not in test. Command line:\n  %s", strings.Join(os.Args, "\n  "))) | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1674 | } | 
|  | 1675 |  | 
|  | 1676 | func (p InstallPath) RelativeToTop() Path { | 
|  | 1677 | ensureTestOnly() | 
| Colin Cross | c0e42d5 | 2024-02-01 16:42:36 -0800 | [diff] [blame] | 1678 | if p.makePath { | 
|  | 1679 | p.soongOutDir = OutDir | 
|  | 1680 | } else { | 
|  | 1681 | p.soongOutDir = OutSoongDir | 
|  | 1682 | } | 
|  | 1683 | p.fullPath = filepath.Join(p.soongOutDir, p.path) | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1684 | return p | 
|  | 1685 | } | 
|  | 1686 |  | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1687 | func (p InstallPath) getSoongOutDir() string { | 
|  | 1688 | return p.soongOutDir | 
| Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1689 | } | 
|  | 1690 |  | 
| Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 1691 | func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { | 
|  | 1692 | panic("Not implemented") | 
|  | 1693 | } | 
|  | 1694 |  | 
| Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1695 | var _ Path = InstallPath{} | 
|  | 1696 | var _ WritablePath = InstallPath{} | 
|  | 1697 |  | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1698 | func (p InstallPath) writablePath() {} | 
|  | 1699 |  | 
|  | 1700 | func (p InstallPath) String() string { | 
| Colin Cross | c0e42d5 | 2024-02-01 16:42:36 -0800 | [diff] [blame] | 1701 | return p.fullPath | 
| Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1702 | } | 
|  | 1703 |  | 
|  | 1704 | // PartitionDir returns the path to the partition where the install path is rooted at. It is | 
|  | 1705 | // out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules. | 
|  | 1706 | // The ./soong is dropped if the install path is for Make. | 
|  | 1707 | func (p InstallPath) PartitionDir() string { | 
|  | 1708 | if p.makePath { | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1709 | return filepath.Join(p.soongOutDir, "../", p.partitionDir) | 
| Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1710 | } else { | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1711 | return filepath.Join(p.soongOutDir, p.partitionDir) | 
| Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1712 | } | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1713 | } | 
|  | 1714 |  | 
| Jihoon Kang | f78a890 | 2022-09-01 22:47:07 +0000 | [diff] [blame] | 1715 | func (p InstallPath) Partition() string { | 
|  | 1716 | return p.partition | 
|  | 1717 | } | 
|  | 1718 |  | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1719 | // Join creates a new InstallPath with paths... joined with the current path. The | 
|  | 1720 | // provided paths... may not use '..' to escape from the current path. | 
|  | 1721 | func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath { | 
|  | 1722 | path, err := validatePath(paths...) | 
|  | 1723 | if err != nil { | 
|  | 1724 | reportPathError(ctx, err) | 
|  | 1725 | } | 
|  | 1726 | return p.withRel(path) | 
|  | 1727 | } | 
|  | 1728 |  | 
|  | 1729 | func (p InstallPath) withRel(rel string) InstallPath { | 
|  | 1730 | p.basePath = p.basePath.withRel(rel) | 
| Colin Cross | c0e42d5 | 2024-02-01 16:42:36 -0800 | [diff] [blame] | 1731 | p.fullPath = filepath.Join(p.fullPath, rel) | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1732 | return p | 
|  | 1733 | } | 
|  | 1734 |  | 
| Colin Cross | c68db4b | 2021-11-11 18:59:15 -0800 | [diff] [blame] | 1735 | // Deprecated: ToMakePath is a noop, PathForModuleInstall always returns Make paths when building | 
|  | 1736 | // embedded in Make. | 
| Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1737 | func (p InstallPath) ToMakePath() InstallPath { | 
| Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1738 | p.makePath = true | 
| Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1739 | return p | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1740 | } | 
|  | 1741 |  | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1742 | // PathForModuleInstall returns a Path representing the install path for the | 
|  | 1743 | // module appended with paths... | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1744 | func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath { | 
| Spandan Das | 5d1b929 | 2021-06-03 19:36:41 +0000 | [diff] [blame] | 1745 | os, arch := osAndArch(ctx) | 
| Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame] | 1746 | partition := modulePartition(ctx, os.Class == Device) | 
| Cole Faust | 3b703f3 | 2023-10-16 13:30:51 -0700 | [diff] [blame] | 1747 | return pathForInstall(ctx, os, arch, partition, pathComponents...) | 
| Spandan Das | 5d1b929 | 2021-06-03 19:36:41 +0000 | [diff] [blame] | 1748 | } | 
|  | 1749 |  | 
| Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 1750 | // PathForHostDexInstall returns an InstallPath representing the install path for the | 
|  | 1751 | // module appended with paths... | 
|  | 1752 | func PathForHostDexInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath { | 
| Cole Faust | 3b703f3 | 2023-10-16 13:30:51 -0700 | [diff] [blame] | 1753 | return pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "", pathComponents...) | 
| Colin Cross | 1d0eb7a | 2021-11-03 14:08:20 -0700 | [diff] [blame] | 1754 | } | 
|  | 1755 |  | 
| Spandan Das | 5d1b929 | 2021-06-03 19:36:41 +0000 | [diff] [blame] | 1756 | // PathForModuleInPartitionInstall is similar to PathForModuleInstall but partition is provided by the caller | 
|  | 1757 | func PathForModuleInPartitionInstall(ctx ModuleInstallPathContext, partition string, pathComponents ...string) InstallPath { | 
|  | 1758 | os, arch := osAndArch(ctx) | 
| Cole Faust | 3b703f3 | 2023-10-16 13:30:51 -0700 | [diff] [blame] | 1759 | return pathForInstall(ctx, os, arch, partition, pathComponents...) | 
| Spandan Das | 5d1b929 | 2021-06-03 19:36:41 +0000 | [diff] [blame] | 1760 | } | 
|  | 1761 |  | 
|  | 1762 | func osAndArch(ctx ModuleInstallPathContext) (OsType, ArchType) { | 
| Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1763 | os := ctx.Os() | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1764 | arch := ctx.Arch().ArchType | 
|  | 1765 | forceOS, forceArch := ctx.InstallForceOS() | 
|  | 1766 | if forceOS != nil { | 
| Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1767 | os = *forceOS | 
|  | 1768 | } | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1769 | if forceArch != nil { | 
|  | 1770 | arch = *forceArch | 
|  | 1771 | } | 
| Spandan Das | 5d1b929 | 2021-06-03 19:36:41 +0000 | [diff] [blame] | 1772 | return os, arch | 
|  | 1773 | } | 
| Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1774 |  | 
| Colin Cross | c0e42d5 | 2024-02-01 16:42:36 -0800 | [diff] [blame] | 1775 | func pathForPartitionInstallDir(ctx PathContext, partition, partitionPath string, makePath bool) InstallPath { | 
|  | 1776 | fullPath := ctx.Config().SoongOutDir() | 
|  | 1777 | if makePath { | 
|  | 1778 | // Make path starts with out/ instead of out/soong. | 
|  | 1779 | fullPath = filepath.Join(fullPath, "../", partitionPath) | 
|  | 1780 | } else { | 
|  | 1781 | fullPath = filepath.Join(fullPath, partitionPath) | 
|  | 1782 | } | 
|  | 1783 |  | 
|  | 1784 | return InstallPath{ | 
|  | 1785 | basePath:     basePath{partitionPath, ""}, | 
|  | 1786 | soongOutDir:  ctx.Config().soongOutDir, | 
|  | 1787 | partitionDir: partitionPath, | 
|  | 1788 | partition:    partition, | 
|  | 1789 | makePath:     makePath, | 
|  | 1790 | fullPath:     fullPath, | 
|  | 1791 | } | 
|  | 1792 | } | 
|  | 1793 |  | 
| Cole Faust | 3b703f3 | 2023-10-16 13:30:51 -0700 | [diff] [blame] | 1794 | func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string, | 
| Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1795 | pathComponents ...string) InstallPath { | 
|  | 1796 |  | 
| Jiyong Park | 9785915 | 2023-02-14 17:05:48 +0900 | [diff] [blame] | 1797 | var partitionPaths []string | 
| Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1798 |  | 
| Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1799 | if os.Class == Device { | 
| Jiyong Park | 9785915 | 2023-02-14 17:05:48 +0900 | [diff] [blame] | 1800 | partitionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition} | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1801 | } else { | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1802 | osName := os.String() | 
| Colin Cross | a9b2aac | 2022-06-15 17:25:51 -0700 | [diff] [blame] | 1803 | if os == Linux { | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1804 | // instead of linux_glibc | 
|  | 1805 | osName = "linux" | 
| Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 1806 | } | 
| Colin Cross | a9b2aac | 2022-06-15 17:25:51 -0700 | [diff] [blame] | 1807 | if os == LinuxMusl && ctx.Config().UseHostMusl() { | 
|  | 1808 | // When using musl instead of glibc, use "linux" instead of "linux_musl".  When cross | 
|  | 1809 | // compiling we will still use "linux_musl". | 
|  | 1810 | osName = "linux" | 
|  | 1811 | } | 
|  | 1812 |  | 
| Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1813 | // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH) | 
|  | 1814 | // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem | 
|  | 1815 | // to have a plan to fix it (see the comment in build/make/core/envsetup.mk). | 
|  | 1816 | // Let's keep using x86 for the existing cases until we have a need to support | 
|  | 1817 | // other architectures. | 
|  | 1818 | archName := arch.String() | 
|  | 1819 | if os.Class == Host && (arch == X86_64 || arch == Common) { | 
|  | 1820 | archName = "x86" | 
|  | 1821 | } | 
| Jiyong Park | 9785915 | 2023-02-14 17:05:48 +0900 | [diff] [blame] | 1822 | partitionPaths = []string{"host", osName + "-" + archName, partition} | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1823 | } | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1824 |  | 
| Jiyong Park | 9785915 | 2023-02-14 17:05:48 +0900 | [diff] [blame] | 1825 | partitionPath, err := validatePath(partitionPaths...) | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1826 | if err != nil { | 
|  | 1827 | reportPathError(ctx, err) | 
|  | 1828 | } | 
| Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1829 |  | 
| Colin Cross | c0e42d5 | 2024-02-01 16:42:36 -0800 | [diff] [blame] | 1830 | base := pathForPartitionInstallDir(ctx, partition, partitionPath, ctx.Config().KatiEnabled()) | 
| Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1831 | return base.Join(ctx, pathComponents...) | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1832 | } | 
|  | 1833 |  | 
| Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 1834 | func pathForNdkOrSdkInstall(ctx PathContext, prefix string, paths []string) InstallPath { | 
| Colin Cross | c0e42d5 | 2024-02-01 16:42:36 -0800 | [diff] [blame] | 1835 | base := pathForPartitionInstallDir(ctx, "", prefix, false) | 
| Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1836 | return base.Join(ctx, paths...) | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1837 | } | 
|  | 1838 |  | 
| Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 1839 | func PathForNdkInstall(ctx PathContext, paths ...string) InstallPath { | 
|  | 1840 | return pathForNdkOrSdkInstall(ctx, "ndk", paths) | 
|  | 1841 | } | 
|  | 1842 |  | 
|  | 1843 | func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath { | 
|  | 1844 | return pathForNdkOrSdkInstall(ctx, "mainline-sdks", paths) | 
|  | 1845 | } | 
|  | 1846 |  | 
| Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1847 | func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string { | 
| Colin Cross | b1692a3 | 2021-10-25 15:39:01 -0700 | [diff] [blame] | 1848 | rel := Rel(ctx, strings.TrimSuffix(path.PartitionDir(), path.partition), path.String()) | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1849 | return "/" + rel | 
|  | 1850 | } | 
|  | 1851 |  | 
| Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame] | 1852 | func modulePartition(ctx ModuleInstallPathContext, device bool) string { | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1853 | var partition string | 
| Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1854 | if ctx.InstallInTestcases() { | 
|  | 1855 | // "testcases" install directory can be used for host or device modules. | 
| Jaewoong Jung | 0949f31 | 2019-09-11 10:25:18 -0700 | [diff] [blame] | 1856 | partition = "testcases" | 
| Cole Faust | 11edf55 | 2023-10-13 11:32:14 -0700 | [diff] [blame] | 1857 | } else if device { | 
| Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1858 | if ctx.InstallInData() { | 
|  | 1859 | partition = "data" | 
|  | 1860 | } else if ctx.InstallInRamdisk() { | 
|  | 1861 | if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() { | 
|  | 1862 | partition = "recovery/root/first_stage_ramdisk" | 
|  | 1863 | } else { | 
|  | 1864 | partition = "ramdisk" | 
|  | 1865 | } | 
|  | 1866 | if !ctx.InstallInRoot() { | 
|  | 1867 | partition += "/system" | 
|  | 1868 | } | 
| Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 1869 | } else if ctx.InstallInVendorRamdisk() { | 
| Yifan Hong | 39143a9 | 2020-10-26 12:43:12 -0700 | [diff] [blame] | 1870 | // The module is only available after switching root into | 
|  | 1871 | // /first_stage_ramdisk. To expose the module before switching root | 
|  | 1872 | // on a device without a dedicated recovery partition, install the | 
|  | 1873 | // recovery variant. | 
| Yifan Hong | dd8dacc | 2020-10-21 15:40:17 -0700 | [diff] [blame] | 1874 | if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() { | 
| Petri Gynther | ac22956 | 2021-03-02 23:44:02 -0800 | [diff] [blame] | 1875 | partition = "vendor_ramdisk/first_stage_ramdisk" | 
| Yifan Hong | dd8dacc | 2020-10-21 15:40:17 -0700 | [diff] [blame] | 1876 | } else { | 
| Petri Gynther | ac22956 | 2021-03-02 23:44:02 -0800 | [diff] [blame] | 1877 | partition = "vendor_ramdisk" | 
| Yifan Hong | dd8dacc | 2020-10-21 15:40:17 -0700 | [diff] [blame] | 1878 | } | 
|  | 1879 | if !ctx.InstallInRoot() { | 
|  | 1880 | partition += "/system" | 
|  | 1881 | } | 
| Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 1882 | } else if ctx.InstallInDebugRamdisk() { | 
|  | 1883 | partition = "debug_ramdisk" | 
| Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1884 | } else if ctx.InstallInRecovery() { | 
|  | 1885 | if ctx.InstallInRoot() { | 
|  | 1886 | partition = "recovery/root" | 
|  | 1887 | } else { | 
|  | 1888 | // the layout of recovery partion is the same as that of system partition | 
|  | 1889 | partition = "recovery/root/system" | 
|  | 1890 | } | 
| Colin Cross | ea30d85 | 2023-11-29 16:00:16 -0800 | [diff] [blame] | 1891 | } else if ctx.SocSpecific() || ctx.InstallInVendor() { | 
| Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1892 | partition = ctx.DeviceConfig().VendorPath() | 
| Colin Cross | ea30d85 | 2023-11-29 16:00:16 -0800 | [diff] [blame] | 1893 | } else if ctx.DeviceSpecific() || ctx.InstallInOdm() { | 
| Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1894 | partition = ctx.DeviceConfig().OdmPath() | 
| Colin Cross | ea30d85 | 2023-11-29 16:00:16 -0800 | [diff] [blame] | 1895 | } else if ctx.ProductSpecific() || ctx.InstallInProduct() { | 
| Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1896 | partition = ctx.DeviceConfig().ProductPath() | 
|  | 1897 | } else if ctx.SystemExtSpecific() { | 
|  | 1898 | partition = ctx.DeviceConfig().SystemExtPath() | 
|  | 1899 | } else if ctx.InstallInRoot() { | 
|  | 1900 | partition = "root" | 
| Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1901 | } else { | 
| Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1902 | partition = "system" | 
| Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1903 | } | 
| Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1904 | if ctx.InstallInSanitizerDir() { | 
|  | 1905 | partition = "data/asan/" + partition | 
| Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1906 | } | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1907 | } | 
|  | 1908 | return partition | 
|  | 1909 | } | 
|  | 1910 |  | 
| Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1911 | type InstallPaths []InstallPath | 
|  | 1912 |  | 
|  | 1913 | // Paths returns the InstallPaths as a Paths | 
|  | 1914 | func (p InstallPaths) Paths() Paths { | 
|  | 1915 | if p == nil { | 
|  | 1916 | return nil | 
|  | 1917 | } | 
|  | 1918 | ret := make(Paths, len(p)) | 
|  | 1919 | for i, path := range p { | 
|  | 1920 | ret[i] = path | 
|  | 1921 | } | 
|  | 1922 | return ret | 
|  | 1923 | } | 
|  | 1924 |  | 
|  | 1925 | // Strings returns the string forms of the install paths. | 
|  | 1926 | func (p InstallPaths) Strings() []string { | 
|  | 1927 | if p == nil { | 
|  | 1928 | return nil | 
|  | 1929 | } | 
|  | 1930 | ret := make([]string, len(p)) | 
|  | 1931 | for i, path := range p { | 
|  | 1932 | ret[i] = path.String() | 
|  | 1933 | } | 
|  | 1934 | return ret | 
|  | 1935 | } | 
|  | 1936 |  | 
| Jingwen Chen | 24d0c56 | 2023-02-07 09:29:36 +0000 | [diff] [blame] | 1937 | // validatePathInternal ensures that a path does not leave its component, and | 
|  | 1938 | // optionally doesn't contain Ninja variables. | 
|  | 1939 | func validatePathInternal(allowNinjaVariables bool, pathComponents ...string) (string, error) { | 
| Colin Cross | bf9ed3f | 2023-10-24 14:17:03 -0700 | [diff] [blame] | 1940 | initialEmpty := 0 | 
|  | 1941 | finalEmpty := 0 | 
|  | 1942 | for i, path := range pathComponents { | 
| Jingwen Chen | 24d0c56 | 2023-02-07 09:29:36 +0000 | [diff] [blame] | 1943 | if !allowNinjaVariables && strings.Contains(path, "$") { | 
|  | 1944 | return "", fmt.Errorf("Path contains invalid character($): %s", path) | 
|  | 1945 | } | 
|  | 1946 |  | 
| Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1947 | path := filepath.Clean(path) | 
|  | 1948 | if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") { | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1949 | return "", fmt.Errorf("Path is outside directory: %s", path) | 
| Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1950 | } | 
| Colin Cross | bf9ed3f | 2023-10-24 14:17:03 -0700 | [diff] [blame] | 1951 |  | 
|  | 1952 | if i == initialEmpty && pathComponents[i] == "" { | 
|  | 1953 | initialEmpty++ | 
|  | 1954 | } | 
|  | 1955 | if i == finalEmpty && pathComponents[len(pathComponents)-1-i] == "" { | 
|  | 1956 | finalEmpty++ | 
|  | 1957 | } | 
| Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1958 | } | 
| Colin Cross | bf9ed3f | 2023-10-24 14:17:03 -0700 | [diff] [blame] | 1959 | // Optimization: filepath.Join("foo", "") returns a newly allocated copy | 
|  | 1960 | // of "foo", while filepath.Join("foo") does not.  Strip out any empty | 
|  | 1961 | // path components. | 
|  | 1962 | if initialEmpty == len(pathComponents) { | 
|  | 1963 | return "", nil | 
|  | 1964 | } | 
|  | 1965 | nonEmptyPathComponents := pathComponents[initialEmpty : len(pathComponents)-finalEmpty] | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1966 | // TODO: filepath.Join isn't necessarily correct with embedded ninja | 
|  | 1967 | // variables. '..' may remove the entire ninja variable, even if it | 
|  | 1968 | // will be expanded to multiple nested directories. | 
| Colin Cross | bf9ed3f | 2023-10-24 14:17:03 -0700 | [diff] [blame] | 1969 | return filepath.Join(nonEmptyPathComponents...), nil | 
| Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1970 | } | 
|  | 1971 |  | 
| Jingwen Chen | 24d0c56 | 2023-02-07 09:29:36 +0000 | [diff] [blame] | 1972 | // validateSafePath validates a path that we trust (may contain ninja | 
|  | 1973 | // variables).  Ensures that each path component does not attempt to leave its | 
|  | 1974 | // component. Returns a joined version of each path component. | 
|  | 1975 | func validateSafePath(pathComponents ...string) (string, error) { | 
|  | 1976 | return validatePathInternal(true, pathComponents...) | 
|  | 1977 | } | 
|  | 1978 |  | 
| Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1979 | // validatePath validates that a path does not include ninja variables, and that | 
|  | 1980 | // each path component does not attempt to leave its component. Returns a joined | 
|  | 1981 | // version of each path component. | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1982 | func validatePath(pathComponents ...string) (string, error) { | 
| Jingwen Chen | 24d0c56 | 2023-02-07 09:29:36 +0000 | [diff] [blame] | 1983 | return validatePathInternal(false, pathComponents...) | 
| Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 1984 | } | 
| Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1985 |  | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1986 | func PathForPhony(ctx PathContext, phony string) WritablePath { | 
|  | 1987 | if strings.ContainsAny(phony, "$/") { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1988 | ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony) | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1989 | } | 
| Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1990 | return PhonyPath{basePath{phony, ""}} | 
| Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1991 | } | 
|  | 1992 |  | 
| Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 1993 | type PhonyPath struct { | 
|  | 1994 | basePath | 
|  | 1995 | } | 
|  | 1996 |  | 
|  | 1997 | func (p PhonyPath) writablePath() {} | 
|  | 1998 |  | 
| Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 1999 | func (p PhonyPath) getSoongOutDir() string { | 
| Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 2000 | // A phone path cannot contain any / so cannot be relative to the build directory. | 
|  | 2001 | return "" | 
| Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 2002 | } | 
|  | 2003 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 2004 | func (p PhonyPath) RelativeToTop() Path { | 
|  | 2005 | ensureTestOnly() | 
|  | 2006 | // A phony path cannot contain any / so does not have a build directory so switching to a new | 
|  | 2007 | // build directory has no effect so just return this path. | 
|  | 2008 | return p | 
|  | 2009 | } | 
|  | 2010 |  | 
| Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 2011 | func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { | 
|  | 2012 | panic("Not implemented") | 
|  | 2013 | } | 
|  | 2014 |  | 
| Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 2015 | var _ Path = PhonyPath{} | 
|  | 2016 | var _ WritablePath = PhonyPath{} | 
|  | 2017 |  | 
| Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 2018 | type testPath struct { | 
|  | 2019 | basePath | 
|  | 2020 | } | 
|  | 2021 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 2022 | func (p testPath) RelativeToTop() Path { | 
|  | 2023 | ensureTestOnly() | 
|  | 2024 | return p | 
|  | 2025 | } | 
|  | 2026 |  | 
| Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 2027 | func (p testPath) String() string { | 
|  | 2028 | return p.path | 
|  | 2029 | } | 
|  | 2030 |  | 
| Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 2031 | var _ Path = testPath{} | 
|  | 2032 |  | 
| Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2033 | // PathForTesting returns a Path constructed from joining the elements of paths with '/'.  It should only be used from | 
|  | 2034 | // within tests. | 
| Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 2035 | func PathForTesting(paths ...string) Path { | 
| Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 2036 | p, err := validateSafePath(paths...) | 
|  | 2037 | if err != nil { | 
|  | 2038 | panic(err) | 
|  | 2039 | } | 
| Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 2040 | return testPath{basePath{path: p, rel: p}} | 
|  | 2041 | } | 
|  | 2042 |  | 
| Sam Delmerico | 2351eac | 2022-05-24 17:10:02 +0000 | [diff] [blame] | 2043 | func PathForTestingWithRel(path, rel string) Path { | 
|  | 2044 | p, err := validateSafePath(path, rel) | 
|  | 2045 | if err != nil { | 
|  | 2046 | panic(err) | 
|  | 2047 | } | 
|  | 2048 | r, err := validatePath(rel) | 
|  | 2049 | if err != nil { | 
|  | 2050 | panic(err) | 
|  | 2051 | } | 
|  | 2052 | return testPath{basePath{path: p, rel: r}} | 
|  | 2053 | } | 
|  | 2054 |  | 
| Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2055 | // PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests. | 
|  | 2056 | func PathsForTesting(strs ...string) Paths { | 
| Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 2057 | p := make(Paths, len(strs)) | 
|  | 2058 | for i, s := range strs { | 
|  | 2059 | p[i] = PathForTesting(s) | 
|  | 2060 | } | 
|  | 2061 |  | 
|  | 2062 | return p | 
|  | 2063 | } | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2064 |  | 
| Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2065 | type testPathContext struct { | 
|  | 2066 | config Config | 
| Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2067 | } | 
|  | 2068 |  | 
| Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2069 | func (x *testPathContext) Config() Config             { return x.config } | 
|  | 2070 | func (x *testPathContext) AddNinjaFileDeps(...string) {} | 
|  | 2071 |  | 
|  | 2072 | // PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with | 
|  | 2073 | // PathForOutput. | 
| Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 2074 | func PathContextForTesting(config Config) PathContext { | 
| Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2075 | return &testPathContext{ | 
|  | 2076 | config: config, | 
| Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2077 | } | 
|  | 2078 | } | 
|  | 2079 |  | 
| Ulya Trafimovich | ccc8c85 | 2020-10-14 11:29:07 +0100 | [diff] [blame] | 2080 | type testModuleInstallPathContext struct { | 
|  | 2081 | baseModuleContext | 
|  | 2082 |  | 
|  | 2083 | inData          bool | 
|  | 2084 | inTestcases     bool | 
|  | 2085 | inSanitizerDir  bool | 
|  | 2086 | inRamdisk       bool | 
|  | 2087 | inVendorRamdisk bool | 
| Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 2088 | inDebugRamdisk  bool | 
| Ulya Trafimovich | ccc8c85 | 2020-10-14 11:29:07 +0100 | [diff] [blame] | 2089 | inRecovery      bool | 
|  | 2090 | inRoot          bool | 
| Colin Cross | ea30d85 | 2023-11-29 16:00:16 -0800 | [diff] [blame] | 2091 | inOdm           bool | 
|  | 2092 | inProduct       bool | 
|  | 2093 | inVendor        bool | 
| Ulya Trafimovich | ccc8c85 | 2020-10-14 11:29:07 +0100 | [diff] [blame] | 2094 | forceOS         *OsType | 
|  | 2095 | forceArch       *ArchType | 
|  | 2096 | } | 
|  | 2097 |  | 
|  | 2098 | func (m testModuleInstallPathContext) Config() Config { | 
|  | 2099 | return m.baseModuleContext.config | 
|  | 2100 | } | 
|  | 2101 |  | 
|  | 2102 | func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {} | 
|  | 2103 |  | 
|  | 2104 | func (m testModuleInstallPathContext) InstallInData() bool { | 
|  | 2105 | return m.inData | 
|  | 2106 | } | 
|  | 2107 |  | 
|  | 2108 | func (m testModuleInstallPathContext) InstallInTestcases() bool { | 
|  | 2109 | return m.inTestcases | 
|  | 2110 | } | 
|  | 2111 |  | 
|  | 2112 | func (m testModuleInstallPathContext) InstallInSanitizerDir() bool { | 
|  | 2113 | return m.inSanitizerDir | 
|  | 2114 | } | 
|  | 2115 |  | 
|  | 2116 | func (m testModuleInstallPathContext) InstallInRamdisk() bool { | 
|  | 2117 | return m.inRamdisk | 
|  | 2118 | } | 
|  | 2119 |  | 
|  | 2120 | func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool { | 
|  | 2121 | return m.inVendorRamdisk | 
|  | 2122 | } | 
|  | 2123 |  | 
| Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 2124 | func (m testModuleInstallPathContext) InstallInDebugRamdisk() bool { | 
|  | 2125 | return m.inDebugRamdisk | 
|  | 2126 | } | 
|  | 2127 |  | 
| Ulya Trafimovich | ccc8c85 | 2020-10-14 11:29:07 +0100 | [diff] [blame] | 2128 | func (m testModuleInstallPathContext) InstallInRecovery() bool { | 
|  | 2129 | return m.inRecovery | 
|  | 2130 | } | 
|  | 2131 |  | 
|  | 2132 | func (m testModuleInstallPathContext) InstallInRoot() bool { | 
|  | 2133 | return m.inRoot | 
|  | 2134 | } | 
|  | 2135 |  | 
| Colin Cross | ea30d85 | 2023-11-29 16:00:16 -0800 | [diff] [blame] | 2136 | func (m testModuleInstallPathContext) InstallInOdm() bool { | 
|  | 2137 | return m.inOdm | 
|  | 2138 | } | 
|  | 2139 |  | 
|  | 2140 | func (m testModuleInstallPathContext) InstallInProduct() bool { | 
|  | 2141 | return m.inProduct | 
|  | 2142 | } | 
|  | 2143 |  | 
|  | 2144 | func (m testModuleInstallPathContext) InstallInVendor() bool { | 
|  | 2145 | return m.inVendor | 
|  | 2146 | } | 
|  | 2147 |  | 
| Ulya Trafimovich | ccc8c85 | 2020-10-14 11:29:07 +0100 | [diff] [blame] | 2148 | func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) { | 
|  | 2149 | return m.forceOS, m.forceArch | 
|  | 2150 | } | 
|  | 2151 |  | 
|  | 2152 | // Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is | 
|  | 2153 | // default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are | 
|  | 2154 | // delegated to it will panic. | 
|  | 2155 | func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext { | 
|  | 2156 | ctx := &testModuleInstallPathContext{} | 
|  | 2157 | ctx.config = config | 
|  | 2158 | ctx.os = Android | 
|  | 2159 | return ctx | 
|  | 2160 | } | 
|  | 2161 |  | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2162 | // Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if | 
|  | 2163 | // targetPath is not inside basePath. | 
|  | 2164 | func Rel(ctx PathContext, basePath string, targetPath string) string { | 
|  | 2165 | rel, isRel := MaybeRel(ctx, basePath, targetPath) | 
|  | 2166 | if !isRel { | 
| Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 2167 | ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath) | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2168 | return "" | 
|  | 2169 | } | 
|  | 2170 | return rel | 
|  | 2171 | } | 
|  | 2172 |  | 
|  | 2173 | // MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if | 
|  | 2174 | // targetPath is not inside basePath. | 
|  | 2175 | func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) { | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2176 | rel, isRel, err := maybeRelErr(basePath, targetPath) | 
|  | 2177 | if err != nil { | 
|  | 2178 | reportPathError(ctx, err) | 
|  | 2179 | } | 
|  | 2180 | return rel, isRel | 
|  | 2181 | } | 
|  | 2182 |  | 
|  | 2183 | func maybeRelErr(basePath string, targetPath string) (string, bool, error) { | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2184 | // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first. | 
|  | 2185 | if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) { | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2186 | return "", false, nil | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2187 | } | 
|  | 2188 | rel, err := filepath.Rel(basePath, targetPath) | 
|  | 2189 | if err != nil { | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2190 | return "", false, err | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2191 | } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") { | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2192 | return "", false, nil | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2193 | } | 
| Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2194 | return rel, true, nil | 
| Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2195 | } | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 2196 |  | 
|  | 2197 | // Writes a file to the output directory.  Attempting to write directly to the output directory | 
|  | 2198 | // will fail due to the sandbox of the soong_build process. | 
| Chris Parsons | 1a12d03 | 2023-02-06 22:37:41 -0500 | [diff] [blame] | 2199 | // Only writes the file if the file doesn't exist or if it has different contents, to prevent | 
|  | 2200 | // updating the timestamp if no changes would be made. (This is better for incremental | 
|  | 2201 | // performance.) | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 2202 | func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error { | 
| Colin Cross | d642113 | 2021-11-09 12:32:34 -0800 | [diff] [blame] | 2203 | absPath := absolutePath(path.String()) | 
|  | 2204 | err := os.MkdirAll(filepath.Dir(absPath), 0777) | 
|  | 2205 | if err != nil { | 
|  | 2206 | return err | 
|  | 2207 | } | 
| Chris Parsons | 1a12d03 | 2023-02-06 22:37:41 -0500 | [diff] [blame] | 2208 | return pathtools.WriteFileIfChanged(absPath, data, perm) | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 2209 | } | 
|  | 2210 |  | 
| Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 2211 | func RemoveAllOutputDir(path WritablePath) error { | 
|  | 2212 | return os.RemoveAll(absolutePath(path.String())) | 
|  | 2213 | } | 
|  | 2214 |  | 
|  | 2215 | func CreateOutputDirIfNonexistent(path WritablePath, perm os.FileMode) error { | 
|  | 2216 | dir := absolutePath(path.String()) | 
| Liz Kammer | 09f947d | 2021-05-12 14:51:49 -0400 | [diff] [blame] | 2217 | return createDirIfNonexistent(dir, perm) | 
|  | 2218 | } | 
|  | 2219 |  | 
|  | 2220 | func createDirIfNonexistent(dir string, perm os.FileMode) error { | 
| Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 2221 | if _, err := os.Stat(dir); os.IsNotExist(err) { | 
|  | 2222 | return os.MkdirAll(dir, os.ModePerm) | 
|  | 2223 | } else { | 
|  | 2224 | return err | 
|  | 2225 | } | 
|  | 2226 | } | 
|  | 2227 |  | 
| Jingwen Chen | 78257e5 | 2021-05-21 02:34:24 +0000 | [diff] [blame] | 2228 | // absolutePath is deliberately private so that Soong's Go plugins can't use it to find and | 
|  | 2229 | // read arbitrary files without going through the methods in the current package that track | 
|  | 2230 | // dependencies. | 
| Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 2231 | func absolutePath(path string) string { | 
|  | 2232 | if filepath.IsAbs(path) { | 
|  | 2233 | return path | 
|  | 2234 | } | 
|  | 2235 | return filepath.Join(absSrcDir, path) | 
|  | 2236 | } | 
| Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 2237 |  | 
|  | 2238 | // A DataPath represents the path of a file to be used as data, for example | 
|  | 2239 | // a test library to be installed alongside a test. | 
|  | 2240 | // The data file should be installed (copied from `<SrcPath>`) to | 
|  | 2241 | // `<install_root>/<RelativeInstallPath>/<filename>`, or | 
|  | 2242 | // `<install_root>/<filename>` if RelativeInstallPath is empty. | 
|  | 2243 | type DataPath struct { | 
|  | 2244 | // The path of the data file that should be copied into the data directory | 
|  | 2245 | SrcPath Path | 
|  | 2246 | // The install path of the data file, relative to the install root. | 
|  | 2247 | RelativeInstallPath string | 
| Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 2248 | // If WithoutRel is true, use SrcPath.Base() instead of SrcPath.Rel() as the filename. | 
|  | 2249 | WithoutRel bool | 
| Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 2250 | } | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 2251 |  | 
| Colin Cross | d442a0e | 2023-11-16 11:19:26 -0800 | [diff] [blame] | 2252 | func (d *DataPath) ToRelativeInstallPath() string { | 
|  | 2253 | relPath := d.SrcPath.Rel() | 
| Colin Cross | 5c1d5fb | 2023-11-15 12:39:40 -0800 | [diff] [blame] | 2254 | if d.WithoutRel { | 
|  | 2255 | relPath = d.SrcPath.Base() | 
|  | 2256 | } | 
| Colin Cross | d442a0e | 2023-11-16 11:19:26 -0800 | [diff] [blame] | 2257 | if d.RelativeInstallPath != "" { | 
|  | 2258 | relPath = filepath.Join(d.RelativeInstallPath, relPath) | 
|  | 2259 | } | 
|  | 2260 | return relPath | 
|  | 2261 | } | 
|  | 2262 |  | 
| Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 2263 | // PathsIfNonNil returns a Paths containing only the non-nil input arguments. | 
|  | 2264 | func PathsIfNonNil(paths ...Path) Paths { | 
|  | 2265 | if len(paths) == 0 { | 
|  | 2266 | // Fast path for empty argument list | 
|  | 2267 | return nil | 
|  | 2268 | } else if len(paths) == 1 { | 
|  | 2269 | // Fast path for a single argument | 
|  | 2270 | if paths[0] != nil { | 
|  | 2271 | return paths | 
|  | 2272 | } else { | 
|  | 2273 | return nil | 
|  | 2274 | } | 
|  | 2275 | } | 
|  | 2276 | ret := make(Paths, 0, len(paths)) | 
|  | 2277 | for _, path := range paths { | 
|  | 2278 | if path != nil { | 
|  | 2279 | ret = append(ret, path) | 
|  | 2280 | } | 
|  | 2281 | } | 
|  | 2282 | if len(ret) == 0 { | 
|  | 2283 | return nil | 
|  | 2284 | } | 
|  | 2285 | return ret | 
|  | 2286 | } | 
| Chris Wailes | b2703ad | 2021-07-30 13:25:42 -0700 | [diff] [blame] | 2287 |  | 
|  | 2288 | var thirdPartyDirPrefixExceptions = []*regexp.Regexp{ | 
|  | 2289 | regexp.MustCompile("^vendor/[^/]*google[^/]*/"), | 
|  | 2290 | regexp.MustCompile("^hardware/google/"), | 
|  | 2291 | regexp.MustCompile("^hardware/interfaces/"), | 
|  | 2292 | regexp.MustCompile("^hardware/libhardware[^/]*/"), | 
|  | 2293 | regexp.MustCompile("^hardware/ril/"), | 
|  | 2294 | } | 
|  | 2295 |  | 
|  | 2296 | func IsThirdPartyPath(path string) bool { | 
|  | 2297 | thirdPartyDirPrefixes := []string{"external/", "vendor/", "hardware/"} | 
|  | 2298 |  | 
|  | 2299 | if HasAnyPrefix(path, thirdPartyDirPrefixes) { | 
|  | 2300 | for _, prefix := range thirdPartyDirPrefixExceptions { | 
|  | 2301 | if prefix.MatchString(path) { | 
|  | 2302 | return false | 
|  | 2303 | } | 
|  | 2304 | } | 
|  | 2305 | return true | 
|  | 2306 | } | 
|  | 2307 | return false | 
|  | 2308 | } |