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