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