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