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