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