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