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