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