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