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 | "io/ioutil" |
| 20 | "os" |
Colin Cross | 6a745c6 | 2015-06-16 16:38:10 -0700 | [diff] [blame] | 21 | "path/filepath" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 22 | "reflect" |
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" |
| 27 | "github.com/google/blueprint/pathtools" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 30 | var absSrcDir string |
| 31 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 32 | // PathContext is the subset of a (Module|Singleton)Context required by the |
| 33 | // Path methods. |
| 34 | type PathContext interface { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 35 | Config() Config |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 36 | AddNinjaFileDeps(deps ...string) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 39 | type PathGlobContext interface { |
| 40 | GlobWithDeps(globPattern string, excludes []string) ([]string, error) |
| 41 | } |
| 42 | |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 43 | var _ PathContext = SingletonContext(nil) |
| 44 | var _ PathContext = ModuleContext(nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 45 | |
Ulya Trafimovich | 8640ab9 | 2020-05-11 18:06:15 +0100 | [diff] [blame] | 46 | // "Null" path context is a minimal path context for a given config. |
| 47 | type NullPathContext struct { |
| 48 | config Config |
| 49 | } |
| 50 | |
| 51 | func (NullPathContext) AddNinjaFileDeps(...string) {} |
| 52 | func (ctx NullPathContext) Config() Config { return ctx.config } |
| 53 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 54 | // EarlyModulePathContext is a subset of EarlyModuleContext methods required by the |
| 55 | // Path methods. These path methods can be called before any mutators have run. |
| 56 | type EarlyModulePathContext interface { |
| 57 | PathContext |
| 58 | PathGlobContext |
| 59 | |
| 60 | ModuleDir() string |
| 61 | ModuleErrorf(fmt string, args ...interface{}) |
| 62 | } |
| 63 | |
| 64 | var _ EarlyModulePathContext = ModuleContext(nil) |
| 65 | |
| 66 | // Glob globs files and directories matching globPattern relative to ModuleDir(), |
| 67 | // paths in the excludes parameter will be omitted. |
| 68 | func Glob(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths { |
| 69 | ret, err := ctx.GlobWithDeps(globPattern, excludes) |
| 70 | if err != nil { |
| 71 | ctx.ModuleErrorf("glob: %s", err.Error()) |
| 72 | } |
| 73 | return pathsForModuleSrcFromFullPath(ctx, ret, true) |
| 74 | } |
| 75 | |
| 76 | // GlobFiles globs *only* files (not directories) matching globPattern relative to ModuleDir(). |
| 77 | // Paths in the excludes parameter will be omitted. |
| 78 | func GlobFiles(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths { |
| 79 | ret, err := ctx.GlobWithDeps(globPattern, excludes) |
| 80 | if err != nil { |
| 81 | ctx.ModuleErrorf("glob: %s", err.Error()) |
| 82 | } |
| 83 | return pathsForModuleSrcFromFullPath(ctx, ret, false) |
| 84 | } |
| 85 | |
| 86 | // ModuleWithDepsPathContext is a subset of *ModuleContext methods required by |
| 87 | // the Path methods that rely on module dependencies having been resolved. |
| 88 | type ModuleWithDepsPathContext interface { |
| 89 | EarlyModulePathContext |
| 90 | GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module |
| 91 | } |
| 92 | |
| 93 | // ModuleMissingDepsPathContext is a subset of *ModuleContext methods required by |
| 94 | // the Path methods that rely on module dependencies having been resolved and ability to report |
| 95 | // missing dependency errors. |
| 96 | type ModuleMissingDepsPathContext interface { |
| 97 | ModuleWithDepsPathContext |
| 98 | AddMissingDependencies(missingDeps []string) |
| 99 | } |
| 100 | |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 101 | type ModuleInstallPathContext interface { |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 102 | BaseModuleContext |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 103 | |
| 104 | InstallInData() bool |
Jaewoong Jung | 0949f31 | 2019-09-11 10:25:18 -0700 | [diff] [blame] | 105 | InstallInTestcases() bool |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 106 | InstallInSanitizerDir() bool |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 107 | InstallInRamdisk() bool |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 108 | InstallInVendorRamdisk() bool |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 109 | InstallInRecovery() bool |
Colin Cross | 90ba5f4 | 2019-10-02 11:10:58 -0700 | [diff] [blame] | 110 | InstallInRoot() bool |
Colin Cross | 607d858 | 2019-07-29 16:44:46 -0700 | [diff] [blame] | 111 | InstallBypassMake() bool |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 112 | InstallForceOS() (*OsType, *ArchType) |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | var _ ModuleInstallPathContext = ModuleContext(nil) |
| 116 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 117 | // errorfContext is the interface containing the Errorf method matching the |
| 118 | // Errorf method in blueprint.SingletonContext. |
| 119 | type errorfContext interface { |
| 120 | Errorf(format string, args ...interface{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 123 | var _ errorfContext = blueprint.SingletonContext(nil) |
| 124 | |
| 125 | // moduleErrorf is the interface containing the ModuleErrorf method matching |
| 126 | // the ModuleErrorf method in blueprint.ModuleContext. |
| 127 | type moduleErrorf interface { |
| 128 | ModuleErrorf(format string, args ...interface{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 131 | var _ moduleErrorf = blueprint.ModuleContext(nil) |
| 132 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 133 | // reportPathError will register an error with the attached context. It |
| 134 | // attempts ctx.ModuleErrorf for a better error message first, then falls |
| 135 | // back to ctx.Errorf. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 136 | func reportPathError(ctx PathContext, err error) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 137 | ReportPathErrorf(ctx, "%s", err.Error()) |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 140 | // ReportPathErrorf will register an error with the attached context. It |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 141 | // attempts ctx.ModuleErrorf for a better error message first, then falls |
| 142 | // back to ctx.Errorf. |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 143 | func ReportPathErrorf(ctx PathContext, format string, args ...interface{}) { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 144 | if mctx, ok := ctx.(moduleErrorf); ok { |
| 145 | mctx.ModuleErrorf(format, args...) |
| 146 | } else if ectx, ok := ctx.(errorfContext); ok { |
| 147 | ectx.Errorf(format, args...) |
| 148 | } else { |
| 149 | panic(fmt.Sprintf(format, args...)) |
Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
Colin Cross | 5e70805 | 2019-08-06 13:59:50 -0700 | [diff] [blame] | 153 | func pathContextName(ctx PathContext, module blueprint.Module) string { |
| 154 | if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok { |
| 155 | return x.ModuleName(module) |
| 156 | } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok { |
| 157 | return x.OtherModuleName(module) |
| 158 | } |
| 159 | return "unknown" |
| 160 | } |
| 161 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 162 | type Path interface { |
| 163 | // Returns the path in string form |
| 164 | String() string |
| 165 | |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 166 | // Ext returns the extension of the last element of the path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 167 | Ext() string |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 168 | |
| 169 | // Base returns the last element of the path |
| 170 | Base() string |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 171 | |
| 172 | // Rel returns the portion of the path relative to the directory it was created from. For |
| 173 | // 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] | 174 | // directory, and OutputPath.Join("foo").Rel() would return "foo". |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 175 | Rel() string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // WritablePath is a type of path that can be used as an output for build rules. |
| 179 | type WritablePath interface { |
| 180 | Path |
| 181 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 182 | // return the path to the build directory. |
| 183 | buildDir() string |
| 184 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 185 | // the writablePath method doesn't directly do anything, |
| 186 | // 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] | 187 | writablePath() |
Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 188 | |
| 189 | ReplaceExtension(ctx PathContext, ext string) OutputPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | type genPathProvider interface { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 193 | genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 194 | } |
| 195 | type objPathProvider interface { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 196 | objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 197 | } |
| 198 | type resPathProvider interface { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 199 | resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // GenPathWithExt derives a new file path in ctx's generated sources directory |
| 203 | // from the current path, but with the new extension. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 204 | func GenPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 205 | if path, ok := p.(genPathProvider); ok { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 206 | return path.genPathWithExt(ctx, subdir, ext) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 207 | } |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 208 | 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] | 209 | return PathForModuleGen(ctx) |
| 210 | } |
| 211 | |
| 212 | // ObjPathWithExt derives a new file path in ctx's object directory from the |
| 213 | // current path, but with the new extension. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 214 | func ObjPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 215 | if path, ok := p.(objPathProvider); ok { |
| 216 | return path.objPathWithExt(ctx, subdir, ext) |
| 217 | } |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 218 | 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] | 219 | return PathForModuleObj(ctx) |
| 220 | } |
| 221 | |
| 222 | // ResPathWithName derives a new path in ctx's output resource directory, using |
| 223 | // the current path to create the directory name, and the `name` argument for |
| 224 | // the filename. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 225 | func ResPathWithName(ctx ModuleOutPathContext, p Path, name string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 226 | if path, ok := p.(resPathProvider); ok { |
| 227 | return path.resPathWithName(ctx, name) |
| 228 | } |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 229 | 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] | 230 | return PathForModuleRes(ctx) |
| 231 | } |
| 232 | |
| 233 | // OptionalPath is a container that may or may not contain a valid Path. |
| 234 | type OptionalPath struct { |
| 235 | valid bool |
| 236 | path Path |
| 237 | } |
| 238 | |
| 239 | // OptionalPathForPath returns an OptionalPath containing the path. |
| 240 | func OptionalPathForPath(path Path) OptionalPath { |
| 241 | if path == nil { |
| 242 | return OptionalPath{} |
| 243 | } |
| 244 | return OptionalPath{valid: true, path: path} |
| 245 | } |
| 246 | |
| 247 | // Valid returns whether there is a valid path |
| 248 | func (p OptionalPath) Valid() bool { |
| 249 | return p.valid |
| 250 | } |
| 251 | |
| 252 | // Path returns the Path embedded in this OptionalPath. You must be sure that |
| 253 | // there is a valid path, since this method will panic if there is not. |
| 254 | func (p OptionalPath) Path() Path { |
| 255 | if !p.valid { |
| 256 | panic("Requesting an invalid path") |
| 257 | } |
| 258 | return p.path |
| 259 | } |
| 260 | |
| 261 | // String returns the string version of the Path, or "" if it isn't valid. |
| 262 | func (p OptionalPath) String() string { |
| 263 | if p.valid { |
| 264 | return p.path.String() |
| 265 | } else { |
| 266 | return "" |
Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 269 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 270 | // Paths is a slice of Path objects, with helpers to operate on the collection. |
| 271 | type Paths []Path |
| 272 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 273 | func (paths Paths) containsPath(path Path) bool { |
| 274 | for _, p := range paths { |
| 275 | if p == path { |
| 276 | return true |
| 277 | } |
| 278 | } |
| 279 | return false |
| 280 | } |
| 281 | |
Liz Kammer | 7aa5288 | 2021-02-11 09:16:14 -0500 | [diff] [blame^] | 282 | // PathsForSource returns Paths rooted from SrcDir, *not* rooted from the module's local source |
| 283 | // directory |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 284 | func PathsForSource(ctx PathContext, paths []string) Paths { |
| 285 | ret := make(Paths, len(paths)) |
| 286 | for i, path := range paths { |
| 287 | ret[i] = PathForSource(ctx, path) |
| 288 | } |
| 289 | return ret |
| 290 | } |
| 291 | |
Liz Kammer | 7aa5288 | 2021-02-11 09:16:14 -0500 | [diff] [blame^] | 292 | // ExistentPathsForSources returns a list of Paths rooted from SrcDir, *not* rooted from the |
| 293 | // module's local source directory, that are found in the tree. If any are not found, they are |
| 294 | // omitted from the list, and dependencies are added so that we're re-run when they are added. |
Colin Cross | 32f3898 | 2018-02-22 11:47:25 -0800 | [diff] [blame] | 295 | func ExistentPathsForSources(ctx PathContext, paths []string) Paths { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 296 | ret := make(Paths, 0, len(paths)) |
| 297 | for _, path := range paths { |
Colin Cross | 32f3898 | 2018-02-22 11:47:25 -0800 | [diff] [blame] | 298 | p := ExistentPathForSource(ctx, path) |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 299 | if p.Valid() { |
| 300 | ret = append(ret, p.Path()) |
| 301 | } |
| 302 | } |
| 303 | return ret |
| 304 | } |
| 305 | |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 306 | // PathsForModuleSrc returns Paths rooted from the module's local source directory. It expands globs, references to |
| 307 | // SourceFileProducer modules using the ":name" syntax, and references to OutputFileProducer modules using the |
| 308 | // ":name{.tag}" syntax. Properties passed as the paths argument must have been annotated with struct tag |
| 309 | // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the |
| 310 | // path_properties mutator. If ctx.Config().AllowMissingDependencies() is true then any missing SourceFileProducer or |
| 311 | // OutputFileProducer dependencies will cause the module to be marked as having missing dependencies. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 312 | func PathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 313 | return PathsForModuleSrcExcludes(ctx, paths, nil) |
| 314 | } |
| 315 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 316 | // PathsForModuleSrcExcludes returns Paths rooted from the module's local source directory, excluding paths listed in |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 317 | // the excludes arguments. It expands globs, references to SourceFileProducer modules using the ":name" syntax, and |
| 318 | // references to OutputFileProducer modules using the ":name{.tag}" syntax. Properties passed as the paths or excludes |
| 319 | // argument must have been annotated with struct tag `android:"path"` so that dependencies on SourceFileProducer modules |
| 320 | // will have already been handled by the path_properties mutator. If ctx.Config().AllowMissingDependencies() is |
Paul Duffin | 036cace | 2019-07-25 14:44:56 +0100 | [diff] [blame] | 321 | // true then any missing SourceFileProducer or OutputFileProducer dependencies will cause the module to be marked as |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 322 | // having missing dependencies. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 323 | func PathsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) Paths { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 324 | ret, missingDeps := PathsAndMissingDepsForModuleSrcExcludes(ctx, paths, excludes) |
| 325 | if ctx.Config().AllowMissingDependencies() { |
| 326 | ctx.AddMissingDependencies(missingDeps) |
| 327 | } else { |
| 328 | for _, m := range missingDeps { |
| 329 | ctx.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m) |
| 330 | } |
| 331 | } |
| 332 | return ret |
| 333 | } |
| 334 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 335 | // OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection. |
| 336 | type OutputPaths []OutputPath |
| 337 | |
| 338 | // Paths returns the OutputPaths as a Paths |
| 339 | func (p OutputPaths) Paths() Paths { |
| 340 | if p == nil { |
| 341 | return nil |
| 342 | } |
| 343 | ret := make(Paths, len(p)) |
| 344 | for i, path := range p { |
| 345 | ret[i] = path |
| 346 | } |
| 347 | return ret |
| 348 | } |
| 349 | |
| 350 | // Strings returns the string forms of the writable paths. |
| 351 | func (p OutputPaths) Strings() []string { |
| 352 | if p == nil { |
| 353 | return nil |
| 354 | } |
| 355 | ret := make([]string, len(p)) |
| 356 | for i, path := range p { |
| 357 | ret[i] = path.String() |
| 358 | } |
| 359 | return ret |
| 360 | } |
| 361 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 362 | // Expands Paths to a SourceFileProducer or OutputFileProducer module dependency referenced via ":name" or ":name{.tag}" syntax. |
| 363 | // If the dependency is not found, a missingErrorDependency is returned. |
| 364 | // If the module dependency is not a SourceFileProducer or OutputFileProducer, appropriate errors will be returned. |
| 365 | func getPathsFromModuleDep(ctx ModuleWithDepsPathContext, path, moduleName, tag string) (Paths, error) { |
| 366 | module := ctx.GetDirectDepWithTag(moduleName, sourceOrOutputDepTag(tag)) |
| 367 | if module == nil { |
| 368 | return nil, missingDependencyError{[]string{moduleName}} |
| 369 | } |
| 370 | if outProducer, ok := module.(OutputFileProducer); ok { |
| 371 | outputFiles, err := outProducer.OutputFiles(tag) |
| 372 | if err != nil { |
| 373 | return nil, fmt.Errorf("path dependency %q: %s", path, err) |
| 374 | } |
| 375 | return outputFiles, nil |
| 376 | } else if tag != "" { |
| 377 | return nil, fmt.Errorf("path dependency %q is not an output file producing module", path) |
| 378 | } else if srcProducer, ok := module.(SourceFileProducer); ok { |
| 379 | return srcProducer.Srcs(), nil |
| 380 | } else { |
| 381 | return nil, fmt.Errorf("path dependency %q is not a source file producing module", path) |
| 382 | } |
| 383 | } |
| 384 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 385 | // PathsAndMissingDepsForModuleSrcExcludes returns Paths rooted from the module's local source directory, excluding |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 386 | // paths listed in the excludes arguments, and a list of missing dependencies. It expands globs, references to |
| 387 | // SourceFileProducer modules using the ":name" syntax, and references to OutputFileProducer modules using the |
| 388 | // ":name{.tag}" syntax. Properties passed as the paths or excludes argument must have been annotated with struct tag |
| 389 | // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the |
| 390 | // path_properties mutator. If ctx.Config().AllowMissingDependencies() is true then any missing SourceFileProducer or |
| 391 | // OutputFileProducer dependencies will be returned, and they will NOT cause the module to be marked as having missing |
| 392 | // dependencies. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 393 | func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleWithDepsPathContext, paths, excludes []string) (Paths, []string) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 394 | prefix := pathForModuleSrc(ctx).String() |
| 395 | |
| 396 | var expandedExcludes []string |
| 397 | if excludes != nil { |
| 398 | expandedExcludes = make([]string, 0, len(excludes)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 399 | } |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 400 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 401 | var missingExcludeDeps []string |
| 402 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 403 | for _, e := range excludes { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 404 | if m, t := SrcIsModuleWithTag(e); m != "" { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 405 | modulePaths, err := getPathsFromModuleDep(ctx, e, m, t) |
| 406 | if m, ok := err.(missingDependencyError); ok { |
| 407 | missingExcludeDeps = append(missingExcludeDeps, m.missingDeps...) |
| 408 | } else if err != nil { |
| 409 | reportPathError(ctx, err) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 410 | } else { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 411 | expandedExcludes = append(expandedExcludes, modulePaths.Strings()...) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 412 | } |
| 413 | } else { |
| 414 | expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e)) |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if paths == nil { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 419 | return nil, missingExcludeDeps |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 422 | var missingDeps []string |
| 423 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 424 | expandedSrcFiles := make(Paths, 0, len(paths)) |
| 425 | for _, s := range paths { |
| 426 | srcFiles, err := expandOneSrcPath(ctx, s, expandedExcludes) |
| 427 | if depErr, ok := err.(missingDependencyError); ok { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 428 | missingDeps = append(missingDeps, depErr.missingDeps...) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 429 | } else if err != nil { |
| 430 | reportPathError(ctx, err) |
| 431 | } |
| 432 | expandedSrcFiles = append(expandedSrcFiles, srcFiles...) |
| 433 | } |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 434 | |
| 435 | return expandedSrcFiles, append(missingDeps, missingExcludeDeps...) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | type missingDependencyError struct { |
| 439 | missingDeps []string |
| 440 | } |
| 441 | |
| 442 | func (e missingDependencyError) Error() string { |
| 443 | return "missing dependencies: " + strings.Join(e.missingDeps, ", ") |
| 444 | } |
| 445 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 446 | // Expands one path string to Paths rooted from the module's local source |
| 447 | // directory, excluding those listed in the expandedExcludes. |
| 448 | // Expands globs, references to SourceFileProducer or OutputFileProducer modules using the ":name" and ":name{.tag}" syntax. |
| 449 | func expandOneSrcPath(ctx ModuleWithDepsPathContext, sPath string, expandedExcludes []string) (Paths, error) { |
Jooyung Han | 7607dd3 | 2020-07-05 10:23:14 +0900 | [diff] [blame] | 450 | excludePaths := func(paths Paths) Paths { |
| 451 | if len(expandedExcludes) == 0 { |
| 452 | return paths |
| 453 | } |
| 454 | remainder := make(Paths, 0, len(paths)) |
| 455 | for _, p := range paths { |
| 456 | if !InList(p.String(), expandedExcludes) { |
| 457 | remainder = append(remainder, p) |
| 458 | } |
| 459 | } |
| 460 | return remainder |
| 461 | } |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 462 | if m, t := SrcIsModuleWithTag(sPath); m != "" { |
| 463 | modulePaths, err := getPathsFromModuleDep(ctx, sPath, m, t) |
| 464 | if err != nil { |
| 465 | return nil, err |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 466 | } else { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 467 | return excludePaths(modulePaths), nil |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 468 | } |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 469 | } else if pathtools.IsGlob(sPath) { |
| 470 | paths := GlobFiles(ctx, pathForModuleSrc(ctx, sPath).String(), expandedExcludes) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 471 | return PathsWithModuleSrcSubDir(ctx, paths, ""), nil |
| 472 | } else { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 473 | p := pathForModuleSrc(ctx, sPath) |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 474 | if exists, _, err := ctx.Config().fs.Exists(p.String()); err != nil { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 475 | ReportPathErrorf(ctx, "%s: %s", p, err.Error()) |
Colin Cross | 5e6a797 | 2020-06-07 16:56:32 -0700 | [diff] [blame] | 476 | } else if !exists && !ctx.Config().testAllowNonExistentPaths { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 477 | ReportPathErrorf(ctx, "module source path %q does not exist", p) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Jooyung Han | 7607dd3 | 2020-07-05 10:23:14 +0900 | [diff] [blame] | 480 | if InList(p.String(), expandedExcludes) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 481 | return nil, nil |
| 482 | } |
| 483 | return Paths{p}, nil |
| 484 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | // pathsForModuleSrcFromFullPath returns Paths rooted from the module's local |
| 488 | // source directory, but strip the local source directory from the beginning of |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 489 | // 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] | 490 | // It intended for use in globs that only list files that exist, so it allows '$' in |
| 491 | // filenames. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 492 | func pathsForModuleSrcFromFullPath(ctx EarlyModulePathContext, paths []string, incDirs bool) Paths { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 493 | prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/" |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 494 | if prefix == "./" { |
| 495 | prefix = "" |
| 496 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 497 | ret := make(Paths, 0, len(paths)) |
| 498 | for _, p := range paths { |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 499 | if !incDirs && strings.HasSuffix(p, "/") { |
| 500 | continue |
| 501 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 502 | path := filepath.Clean(p) |
| 503 | if !strings.HasPrefix(path, prefix) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 504 | 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] | 505 | continue |
| 506 | } |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 507 | |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 508 | srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):]) |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 509 | if err != nil { |
| 510 | reportPathError(ctx, err) |
| 511 | continue |
| 512 | } |
| 513 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 514 | srcPath.basePath.rel = srcPath.path |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 515 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 516 | ret = append(ret, srcPath) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 517 | } |
| 518 | return ret |
| 519 | } |
| 520 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 521 | // PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's local source |
| 522 | // directory. If input is nil, use the default if it exists. If input is empty, returns nil. |
| 523 | func PathsWithOptionalDefaultForModuleSrc(ctx ModuleMissingDepsPathContext, input []string, def string) Paths { |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 524 | if input != nil { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 525 | return PathsForModuleSrc(ctx, input) |
| 526 | } |
| 527 | // Use Glob so that if the default doesn't exist, a dependency is added so that when it |
| 528 | // is created, we're run again. |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 529 | path := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir(), def) |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 530 | return Glob(ctx, path, nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // Strings returns the Paths in string form |
| 534 | func (p Paths) Strings() []string { |
| 535 | if p == nil { |
| 536 | return nil |
| 537 | } |
| 538 | ret := make([]string, len(p)) |
| 539 | for i, path := range p { |
| 540 | ret[i] = path.String() |
| 541 | } |
| 542 | return ret |
| 543 | } |
| 544 | |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 545 | func CopyOfPaths(paths Paths) Paths { |
| 546 | return append(Paths(nil), paths...) |
| 547 | } |
| 548 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 549 | // FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It |
| 550 | // 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] | 551 | func FirstUniquePaths(list Paths) Paths { |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 552 | // 128 was chosen based on BenchmarkFirstUniquePaths results. |
| 553 | if len(list) > 128 { |
| 554 | return firstUniquePathsMap(list) |
| 555 | } |
| 556 | return firstUniquePathsList(list) |
| 557 | } |
| 558 | |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 559 | // SortedUniquePaths returns all unique elements of a Paths in sorted order. It modifies the |
| 560 | // 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] | 561 | func SortedUniquePaths(list Paths) Paths { |
| 562 | unique := FirstUniquePaths(list) |
| 563 | sort.Slice(unique, func(i, j int) bool { |
| 564 | return unique[i].String() < unique[j].String() |
| 565 | }) |
| 566 | return unique |
| 567 | } |
| 568 | |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 569 | func firstUniquePathsList(list Paths) Paths { |
Dan Willemsen | fe92c96 | 2017-08-29 12:28:37 -0700 | [diff] [blame] | 570 | k := 0 |
| 571 | outer: |
| 572 | for i := 0; i < len(list); i++ { |
| 573 | for j := 0; j < k; j++ { |
| 574 | if list[i] == list[j] { |
| 575 | continue outer |
| 576 | } |
| 577 | } |
| 578 | list[k] = list[i] |
| 579 | k++ |
| 580 | } |
| 581 | return list[:k] |
| 582 | } |
| 583 | |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 584 | func firstUniquePathsMap(list Paths) Paths { |
| 585 | k := 0 |
| 586 | seen := make(map[Path]bool, len(list)) |
| 587 | for i := 0; i < len(list); i++ { |
| 588 | if seen[list[i]] { |
| 589 | continue |
| 590 | } |
| 591 | seen[list[i]] = true |
| 592 | list[k] = list[i] |
| 593 | k++ |
| 594 | } |
| 595 | return list[:k] |
| 596 | } |
| 597 | |
Colin Cross | 5d58395 | 2020-11-24 16:21:24 -0800 | [diff] [blame] | 598 | // FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each. It |
| 599 | // modifies the InstallPaths slice contents in place, and returns a subslice of the original slice. |
| 600 | func FirstUniqueInstallPaths(list InstallPaths) InstallPaths { |
| 601 | // 128 was chosen based on BenchmarkFirstUniquePaths results. |
| 602 | if len(list) > 128 { |
| 603 | return firstUniqueInstallPathsMap(list) |
| 604 | } |
| 605 | return firstUniqueInstallPathsList(list) |
| 606 | } |
| 607 | |
| 608 | func firstUniqueInstallPathsList(list InstallPaths) InstallPaths { |
| 609 | k := 0 |
| 610 | outer: |
| 611 | for i := 0; i < len(list); i++ { |
| 612 | for j := 0; j < k; j++ { |
| 613 | if list[i] == list[j] { |
| 614 | continue outer |
| 615 | } |
| 616 | } |
| 617 | list[k] = list[i] |
| 618 | k++ |
| 619 | } |
| 620 | return list[:k] |
| 621 | } |
| 622 | |
| 623 | func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths { |
| 624 | k := 0 |
| 625 | seen := make(map[InstallPath]bool, len(list)) |
| 626 | for i := 0; i < len(list); i++ { |
| 627 | if seen[list[i]] { |
| 628 | continue |
| 629 | } |
| 630 | seen[list[i]] = true |
| 631 | list[k] = list[i] |
| 632 | k++ |
| 633 | } |
| 634 | return list[:k] |
| 635 | } |
| 636 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 637 | // LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It |
| 638 | // modifies the Paths slice contents in place, and returns a subslice of the original slice. |
| 639 | func LastUniquePaths(list Paths) Paths { |
| 640 | totalSkip := 0 |
| 641 | for i := len(list) - 1; i >= totalSkip; i-- { |
| 642 | skip := 0 |
| 643 | for j := i - 1; j >= totalSkip; j-- { |
| 644 | if list[i] == list[j] { |
| 645 | skip++ |
| 646 | } else { |
| 647 | list[j+skip] = list[j] |
| 648 | } |
| 649 | } |
| 650 | totalSkip += skip |
| 651 | } |
| 652 | return list[totalSkip:] |
| 653 | } |
| 654 | |
Colin Cross | a140bb0 | 2018-04-17 10:52:26 -0700 | [diff] [blame] | 655 | // ReversePaths returns a copy of a Paths in reverse order. |
| 656 | func ReversePaths(list Paths) Paths { |
| 657 | if list == nil { |
| 658 | return nil |
| 659 | } |
| 660 | ret := make(Paths, len(list)) |
| 661 | for i := range list { |
| 662 | ret[i] = list[len(list)-1-i] |
| 663 | } |
| 664 | return ret |
| 665 | } |
| 666 | |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 667 | func indexPathList(s Path, list []Path) int { |
| 668 | for i, l := range list { |
| 669 | if l == s { |
| 670 | return i |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | return -1 |
| 675 | } |
| 676 | |
| 677 | func inPathList(p Path, list []Path) bool { |
| 678 | return indexPathList(p, list) != -1 |
| 679 | } |
| 680 | |
| 681 | func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) { |
Paul Duffin | 57b9e1d | 2019-12-13 00:03:35 +0000 | [diff] [blame] | 682 | return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) }) |
| 683 | } |
| 684 | |
| 685 | func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 686 | for _, l := range list { |
Paul Duffin | 57b9e1d | 2019-12-13 00:03:35 +0000 | [diff] [blame] | 687 | if predicate(l) { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 688 | filtered = append(filtered, l) |
| 689 | } else { |
| 690 | remainder = append(remainder, l) |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | return |
| 695 | } |
| 696 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 697 | // HasExt returns true of any of the paths have extension ext, otherwise false |
| 698 | func (p Paths) HasExt(ext string) bool { |
| 699 | for _, path := range p { |
| 700 | if path.Ext() == ext { |
| 701 | return true |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | return false |
| 706 | } |
| 707 | |
| 708 | // FilterByExt returns the subset of the paths that have extension ext |
| 709 | func (p Paths) FilterByExt(ext string) Paths { |
| 710 | ret := make(Paths, 0, len(p)) |
| 711 | for _, path := range p { |
| 712 | if path.Ext() == ext { |
| 713 | ret = append(ret, path) |
| 714 | } |
| 715 | } |
| 716 | return ret |
| 717 | } |
| 718 | |
| 719 | // FilterOutByExt returns the subset of the paths that do not have extension ext |
| 720 | func (p Paths) FilterOutByExt(ext string) Paths { |
| 721 | ret := make(Paths, 0, len(p)) |
| 722 | for _, path := range p { |
| 723 | if path.Ext() != ext { |
| 724 | ret = append(ret, path) |
| 725 | } |
| 726 | } |
| 727 | return ret |
| 728 | } |
| 729 | |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 730 | // DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory |
| 731 | // (including subdirectories) are in a contiguous subslice of the list, and can be found in |
| 732 | // O(log(N)) time using a binary search on the directory prefix. |
| 733 | type DirectorySortedPaths Paths |
| 734 | |
| 735 | func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths { |
| 736 | ret := append(DirectorySortedPaths(nil), paths...) |
| 737 | sort.Slice(ret, func(i, j int) bool { |
| 738 | return ret[i].String() < ret[j].String() |
| 739 | }) |
| 740 | return ret |
| 741 | } |
| 742 | |
| 743 | // PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries |
| 744 | // that are in the specified directory and its subdirectories. |
| 745 | func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths { |
| 746 | prefix := filepath.Clean(dir) + "/" |
| 747 | start := sort.Search(len(p), func(i int) bool { |
| 748 | return prefix < p[i].String() |
| 749 | }) |
| 750 | |
| 751 | ret := p[start:] |
| 752 | |
| 753 | end := sort.Search(len(ret), func(i int) bool { |
| 754 | return !strings.HasPrefix(ret[i].String(), prefix) |
| 755 | }) |
| 756 | |
| 757 | ret = ret[:end] |
| 758 | |
| 759 | return Paths(ret) |
| 760 | } |
| 761 | |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 762 | // WritablePaths is a slice of WritablePath, used for multiple outputs. |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 763 | type WritablePaths []WritablePath |
| 764 | |
| 765 | // Strings returns the string forms of the writable paths. |
| 766 | func (p WritablePaths) Strings() []string { |
| 767 | if p == nil { |
| 768 | return nil |
| 769 | } |
| 770 | ret := make([]string, len(p)) |
| 771 | for i, path := range p { |
| 772 | ret[i] = path.String() |
| 773 | } |
| 774 | return ret |
| 775 | } |
| 776 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 777 | // Paths returns the WritablePaths as a Paths |
| 778 | func (p WritablePaths) Paths() Paths { |
| 779 | if p == nil { |
| 780 | return nil |
| 781 | } |
| 782 | ret := make(Paths, len(p)) |
| 783 | for i, path := range p { |
| 784 | ret[i] = path |
| 785 | } |
| 786 | return ret |
| 787 | } |
| 788 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 789 | type basePath struct { |
| 790 | path string |
| 791 | config Config |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 792 | rel string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | func (p basePath) Ext() string { |
| 796 | return filepath.Ext(p.path) |
| 797 | } |
| 798 | |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 799 | func (p basePath) Base() string { |
| 800 | return filepath.Base(p.path) |
| 801 | } |
| 802 | |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 803 | func (p basePath) Rel() string { |
| 804 | if p.rel != "" { |
| 805 | return p.rel |
| 806 | } |
| 807 | return p.path |
| 808 | } |
| 809 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 810 | func (p basePath) String() string { |
| 811 | return p.path |
| 812 | } |
| 813 | |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 814 | func (p basePath) withRel(rel string) basePath { |
| 815 | p.path = filepath.Join(p.path, rel) |
| 816 | p.rel = rel |
| 817 | return p |
| 818 | } |
| 819 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 820 | // SourcePath is a Path representing a file path rooted from SrcDir |
| 821 | type SourcePath struct { |
| 822 | basePath |
| 823 | } |
| 824 | |
| 825 | var _ Path = SourcePath{} |
| 826 | |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 827 | func (p SourcePath) withRel(rel string) SourcePath { |
| 828 | p.basePath = p.basePath.withRel(rel) |
| 829 | return p |
| 830 | } |
| 831 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 832 | // safePathForSource is for paths that we expect are safe -- only for use by go |
| 833 | // code that is embedding ninja variables in paths |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 834 | func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { |
| 835 | p, err := validateSafePath(pathComponents...) |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 836 | ret := SourcePath{basePath{p, ctx.Config(), ""}} |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 837 | if err != nil { |
| 838 | return ret, err |
| 839 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 840 | |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 841 | // absolute path already checked by validateSafePath |
| 842 | if strings.HasPrefix(ret.String(), ctx.Config().buildDir) { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 843 | return ret, fmt.Errorf("source path %q is in output", ret.String()) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 846 | return ret, err |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 849 | // pathForSource creates a SourcePath from pathComponents, but does not check that it exists. |
| 850 | func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 851 | p, err := validatePath(pathComponents...) |
| 852 | ret := SourcePath{basePath{p, ctx.Config(), ""}} |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 853 | if err != nil { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 854 | return ret, err |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 855 | } |
| 856 | |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 857 | // absolute path already checked by validatePath |
| 858 | if strings.HasPrefix(ret.String(), ctx.Config().buildDir) { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 859 | return ret, fmt.Errorf("source path %q is in output", ret.String()) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 862 | return ret, nil |
| 863 | } |
| 864 | |
| 865 | // existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the |
| 866 | // path does not exist. |
| 867 | func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) { |
| 868 | var files []string |
| 869 | |
| 870 | if gctx, ok := ctx.(PathGlobContext); ok { |
| 871 | // Use glob to produce proper dependencies, even though we only want |
| 872 | // a single file. |
| 873 | files, err = gctx.GlobWithDeps(path.String(), nil) |
| 874 | } else { |
| 875 | var deps []string |
| 876 | // We cannot add build statements in this context, so we fall back to |
| 877 | // AddNinjaFileDeps |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 878 | files, deps, err = ctx.Config().fs.Glob(path.String(), nil, pathtools.FollowSymlinks) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 879 | ctx.AddNinjaFileDeps(deps...) |
| 880 | } |
| 881 | |
| 882 | if err != nil { |
| 883 | return false, fmt.Errorf("glob: %s", err.Error()) |
| 884 | } |
| 885 | |
| 886 | return len(files) > 0, nil |
| 887 | } |
| 888 | |
| 889 | // PathForSource joins the provided path components and validates that the result |
| 890 | // neither escapes the source dir nor is in the out dir. |
| 891 | // On error, it will return a usable, but invalid SourcePath, and report a ModuleError. |
| 892 | func PathForSource(ctx PathContext, pathComponents ...string) SourcePath { |
| 893 | path, err := pathForSource(ctx, pathComponents...) |
| 894 | if err != nil { |
| 895 | reportPathError(ctx, err) |
| 896 | } |
| 897 | |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 898 | if pathtools.IsGlob(path.String()) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 899 | ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 902 | if modCtx, ok := ctx.(ModuleMissingDepsPathContext); ok && ctx.Config().AllowMissingDependencies() { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 903 | exists, err := existsWithDependencies(ctx, path) |
| 904 | if err != nil { |
| 905 | reportPathError(ctx, err) |
| 906 | } |
| 907 | if !exists { |
| 908 | modCtx.AddMissingDependencies([]string{path.String()}) |
| 909 | } |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 910 | } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 911 | ReportPathErrorf(ctx, "%s: %s", path, err.Error()) |
Colin Cross | 5e6a797 | 2020-06-07 16:56:32 -0700 | [diff] [blame] | 912 | } else if !exists && !ctx.Config().testAllowNonExistentPaths { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 913 | ReportPathErrorf(ctx, "source path %q does not exist", path) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 914 | } |
| 915 | return path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Liz Kammer | 7aa5288 | 2021-02-11 09:16:14 -0500 | [diff] [blame^] | 918 | // ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not* |
| 919 | // rooted from the module's local source directory, if the path exists, or an empty OptionalPath if |
| 920 | // it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state |
| 921 | // of the path changes. |
Colin Cross | 32f3898 | 2018-02-22 11:47:25 -0800 | [diff] [blame] | 922 | func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 923 | path, err := pathForSource(ctx, pathComponents...) |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 924 | if err != nil { |
| 925 | reportPathError(ctx, err) |
| 926 | return OptionalPath{} |
| 927 | } |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 928 | |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 929 | if pathtools.IsGlob(path.String()) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 930 | ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 931 | return OptionalPath{} |
| 932 | } |
| 933 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 934 | exists, err := existsWithDependencies(ctx, path) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 935 | if err != nil { |
| 936 | reportPathError(ctx, err) |
| 937 | return OptionalPath{} |
| 938 | } |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 939 | if !exists { |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 940 | return OptionalPath{} |
| 941 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 942 | return OptionalPathForPath(path) |
| 943 | } |
| 944 | |
| 945 | func (p SourcePath) String() string { |
| 946 | return filepath.Join(p.config.srcDir, p.path) |
| 947 | } |
| 948 | |
| 949 | // Join creates a new SourcePath with paths... joined with the current path. The |
| 950 | // provided paths... may not use '..' to escape from the current path. |
| 951 | func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 952 | path, err := validatePath(paths...) |
| 953 | if err != nil { |
| 954 | reportPathError(ctx, err) |
| 955 | } |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 956 | return p.withRel(path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 957 | } |
| 958 | |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 959 | // join is like Join but does less path validation. |
| 960 | func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath { |
| 961 | path, err := validateSafePath(paths...) |
| 962 | if err != nil { |
| 963 | reportPathError(ctx, err) |
| 964 | } |
| 965 | return p.withRel(path) |
| 966 | } |
| 967 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 968 | // OverlayPath returns the overlay for `path' if it exists. This assumes that the |
| 969 | // SourcePath is the path to a resource overlay directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 970 | func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 971 | var relDir string |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 972 | if srcPath, ok := path.(SourcePath); ok { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 973 | relDir = srcPath.path |
| 974 | } else { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 975 | ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 976 | return OptionalPath{} |
| 977 | } |
| 978 | dir := filepath.Join(p.config.srcDir, p.path, relDir) |
| 979 | // 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] | 980 | if pathtools.IsGlob(dir) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 981 | ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir) |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 982 | } |
Colin Cross | 461b445 | 2018-02-23 09:22:42 -0800 | [diff] [blame] | 983 | paths, err := ctx.GlobWithDeps(dir, nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 984 | if err != nil { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 985 | ReportPathErrorf(ctx, "glob: %s", err.Error()) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 986 | return OptionalPath{} |
| 987 | } |
| 988 | if len(paths) == 0 { |
| 989 | return OptionalPath{} |
| 990 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 991 | relPath := Rel(ctx, p.config.srcDir, paths[0]) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 992 | return OptionalPathForPath(PathForSource(ctx, relPath)) |
| 993 | } |
| 994 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 995 | // 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] | 996 | type OutputPath struct { |
| 997 | basePath |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 998 | fullPath string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1001 | func (p OutputPath) withRel(rel string) OutputPath { |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1002 | p.basePath = p.basePath.withRel(rel) |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1003 | p.fullPath = filepath.Join(p.fullPath, rel) |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1004 | return p |
| 1005 | } |
| 1006 | |
Colin Cross | 3063b78 | 2018-08-15 11:19:12 -0700 | [diff] [blame] | 1007 | func (p OutputPath) WithoutRel() OutputPath { |
| 1008 | p.basePath.rel = filepath.Base(p.basePath.path) |
| 1009 | return p |
| 1010 | } |
| 1011 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1012 | func (p OutputPath) buildDir() string { |
| 1013 | return p.config.buildDir |
| 1014 | } |
| 1015 | |
Paul Duffin | 0267d49 | 2021-02-02 10:05:52 +0000 | [diff] [blame] | 1016 | func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
| 1017 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1018 | } |
| 1019 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1020 | var _ Path = OutputPath{} |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1021 | var _ WritablePath = OutputPath{} |
Paul Duffin | 0267d49 | 2021-02-02 10:05:52 +0000 | [diff] [blame] | 1022 | var _ objPathProvider = OutputPath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1023 | |
Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 1024 | // toolDepPath is a Path representing a dependency of the build tool. |
| 1025 | type toolDepPath struct { |
| 1026 | basePath |
| 1027 | } |
| 1028 | |
| 1029 | var _ Path = toolDepPath{} |
| 1030 | |
| 1031 | // pathForBuildToolDep returns a toolDepPath representing the given path string. |
| 1032 | // There is no validation for the path, as it is "trusted": It may fail |
| 1033 | // normal validation checks. For example, it may be an absolute path. |
| 1034 | // Only use this function to construct paths for dependencies of the build |
| 1035 | // tool invocation. |
| 1036 | func pathForBuildToolDep(ctx PathContext, path string) toolDepPath { |
| 1037 | return toolDepPath{basePath{path, ctx.Config(), ""}} |
| 1038 | } |
| 1039 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1040 | // PathForOutput joins the provided paths and returns an OutputPath that is |
| 1041 | // validated to not escape the build dir. |
| 1042 | // On error, it will return a usable, but invalid OutputPath, and report a ModuleError. |
| 1043 | func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1044 | path, err := validatePath(pathComponents...) |
| 1045 | if err != nil { |
| 1046 | reportPathError(ctx, err) |
| 1047 | } |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1048 | fullPath := filepath.Join(ctx.Config().buildDir, path) |
| 1049 | path = fullPath[len(fullPath)-len(path):] |
| 1050 | return OutputPath{basePath{path, ctx.Config(), ""}, fullPath} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1053 | // PathsForOutput returns Paths rooted from buildDir |
| 1054 | func PathsForOutput(ctx PathContext, paths []string) WritablePaths { |
| 1055 | ret := make(WritablePaths, len(paths)) |
| 1056 | for i, path := range paths { |
| 1057 | ret[i] = PathForOutput(ctx, path) |
| 1058 | } |
| 1059 | return ret |
| 1060 | } |
| 1061 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1062 | func (p OutputPath) writablePath() {} |
| 1063 | |
| 1064 | func (p OutputPath) String() string { |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1065 | return p.fullPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | // Join creates a new OutputPath with paths... joined with the current path. The |
| 1069 | // provided paths... may not use '..' to escape from the current path. |
| 1070 | func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1071 | path, err := validatePath(paths...) |
| 1072 | if err != nil { |
| 1073 | reportPathError(ctx, err) |
| 1074 | } |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1075 | return p.withRel(path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1078 | // ReplaceExtension creates a new OutputPath with the extension replaced with ext. |
| 1079 | func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { |
| 1080 | if strings.Contains(ext, "/") { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1081 | ReportPathErrorf(ctx, "extension %q cannot contain /", ext) |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1082 | } |
| 1083 | ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext)) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1084 | ret.rel = pathtools.ReplaceExtension(p.rel, ext) |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1085 | return ret |
| 1086 | } |
| 1087 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1088 | // InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths. |
| 1089 | func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath { |
| 1090 | path, err := validatePath(paths...) |
| 1091 | if err != nil { |
| 1092 | reportPathError(ctx, err) |
| 1093 | } |
| 1094 | |
| 1095 | ret := PathForOutput(ctx, filepath.Dir(p.path), path) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1096 | ret.rel = filepath.Join(filepath.Dir(p.rel), path) |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1097 | return ret |
| 1098 | } |
| 1099 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1100 | // PathForIntermediates returns an OutputPath representing the top-level |
| 1101 | // intermediates directory. |
| 1102 | func PathForIntermediates(ctx PathContext, paths ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1103 | path, err := validatePath(paths...) |
| 1104 | if err != nil { |
| 1105 | reportPathError(ctx, err) |
| 1106 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1107 | return PathForOutput(ctx, ".intermediates", path) |
| 1108 | } |
| 1109 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1110 | var _ genPathProvider = SourcePath{} |
| 1111 | var _ objPathProvider = SourcePath{} |
| 1112 | var _ resPathProvider = SourcePath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1113 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1114 | // PathForModuleSrc returns a Path representing the paths... under the |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1115 | // module's local source directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1116 | func PathForModuleSrc(ctx ModuleMissingDepsPathContext, pathComponents ...string) Path { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1117 | p, err := validatePath(pathComponents...) |
| 1118 | if err != nil { |
| 1119 | reportPathError(ctx, err) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1120 | } |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1121 | paths, err := expandOneSrcPath(ctx, p, nil) |
| 1122 | if err != nil { |
| 1123 | if depErr, ok := err.(missingDependencyError); ok { |
| 1124 | if ctx.Config().AllowMissingDependencies() { |
| 1125 | ctx.AddMissingDependencies(depErr.missingDeps) |
| 1126 | } else { |
| 1127 | ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error()) |
| 1128 | } |
| 1129 | } else { |
| 1130 | reportPathError(ctx, err) |
| 1131 | } |
| 1132 | return nil |
| 1133 | } else if len(paths) == 0 { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1134 | ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1135 | return nil |
| 1136 | } else if len(paths) > 1 { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1137 | ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths)) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1138 | } |
| 1139 | return paths[0] |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1142 | func pathForModuleSrc(ctx EarlyModulePathContext, paths ...string) SourcePath { |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1143 | p, err := validatePath(paths...) |
| 1144 | if err != nil { |
| 1145 | reportPathError(ctx, err) |
| 1146 | } |
| 1147 | |
| 1148 | path, err := pathForSource(ctx, ctx.ModuleDir(), p) |
| 1149 | if err != nil { |
| 1150 | reportPathError(ctx, err) |
| 1151 | } |
| 1152 | |
| 1153 | path.basePath.rel = p |
| 1154 | |
| 1155 | return path |
| 1156 | } |
| 1157 | |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1158 | // PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path |
| 1159 | // will return the path relative to subDir in the module's source directory. If any input paths are not located |
| 1160 | // inside subDir then a path error will be reported. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1161 | func PathsWithModuleSrcSubDir(ctx EarlyModulePathContext, paths Paths, subDir string) Paths { |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1162 | paths = append(Paths(nil), paths...) |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1163 | subDirFullPath := pathForModuleSrc(ctx, subDir) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1164 | for i, path := range paths { |
| 1165 | rel := Rel(ctx, subDirFullPath.String(), path.String()) |
| 1166 | paths[i] = subDirFullPath.join(ctx, rel) |
| 1167 | } |
| 1168 | return paths |
| 1169 | } |
| 1170 | |
| 1171 | // PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the |
| 1172 | // 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] | 1173 | func PathWithModuleSrcSubDir(ctx EarlyModulePathContext, path Path, subDir string) Path { |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1174 | subDirFullPath := pathForModuleSrc(ctx, subDir) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1175 | rel := Rel(ctx, subDirFullPath.String(), path.String()) |
| 1176 | return subDirFullPath.Join(ctx, rel) |
| 1177 | } |
| 1178 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1179 | // OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a |
| 1180 | // valid path if p is non-nil. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1181 | func OptionalPathForModuleSrc(ctx ModuleMissingDepsPathContext, p *string) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1182 | if p == nil { |
| 1183 | return OptionalPath{} |
| 1184 | } |
| 1185 | return OptionalPathForPath(PathForModuleSrc(ctx, *p)) |
| 1186 | } |
| 1187 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1188 | func (p SourcePath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 1189 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1192 | func (p SourcePath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 1193 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1196 | func (p SourcePath) resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1197 | // TODO: Use full directory if the new ctx is not the current ctx? |
| 1198 | return PathForModuleRes(ctx, p.path, name) |
| 1199 | } |
| 1200 | |
| 1201 | // ModuleOutPath is a Path representing a module's output directory. |
| 1202 | type ModuleOutPath struct { |
| 1203 | OutputPath |
| 1204 | } |
| 1205 | |
| 1206 | var _ Path = ModuleOutPath{} |
| 1207 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1208 | func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
Pete Bentley | fcf55bf | 2019-08-16 20:14:32 +0100 | [diff] [blame] | 1209 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1210 | } |
| 1211 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1212 | // ModuleOutPathContext Subset of ModuleContext functions necessary for output path methods. |
| 1213 | type ModuleOutPathContext interface { |
| 1214 | PathContext |
| 1215 | |
| 1216 | ModuleName() string |
| 1217 | ModuleDir() string |
| 1218 | ModuleSubDir() string |
| 1219 | } |
| 1220 | |
| 1221 | func pathForModuleOut(ctx ModuleOutPathContext) OutputPath { |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1222 | return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir()) |
| 1223 | } |
| 1224 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1225 | type BazelOutPath struct { |
| 1226 | OutputPath |
| 1227 | } |
| 1228 | |
| 1229 | var _ Path = BazelOutPath{} |
| 1230 | var _ objPathProvider = BazelOutPath{} |
| 1231 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1232 | func (p BazelOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1233 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1234 | } |
| 1235 | |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1236 | // PathForVndkRefAbiDump returns an OptionalPath representing the path of the |
| 1237 | // reference abi dump for the given module. This is not guaranteed to be valid. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1238 | func PathForVndkRefAbiDump(ctx ModuleInstallPathContext, version, fileName string, |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 1239 | isNdk, isLlndkOrVndk, isGzip bool) OptionalPath { |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1240 | |
Jayant Chowdhary | ac066c6 | 2018-02-20 10:53:31 -0800 | [diff] [blame] | 1241 | arches := ctx.DeviceConfig().Arches() |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1242 | if len(arches) == 0 { |
| 1243 | panic("device build with no primary arch") |
| 1244 | } |
Jayant Chowdhary | ac066c6 | 2018-02-20 10:53:31 -0800 | [diff] [blame] | 1245 | currentArch := ctx.Arch() |
| 1246 | archNameAndVariant := currentArch.ArchType.String() |
| 1247 | if currentArch.ArchVariant != "" { |
| 1248 | archNameAndVariant += "_" + currentArch.ArchVariant |
| 1249 | } |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1250 | |
| 1251 | var dirName string |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 1252 | if isNdk { |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1253 | dirName = "ndk" |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 1254 | } else if isLlndkOrVndk { |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1255 | dirName = "vndk" |
Logan Chien | 41eabe6 | 2019-04-10 13:33:58 +0800 | [diff] [blame] | 1256 | } else { |
| 1257 | dirName = "platform" // opt-in libs |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 1258 | } |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1259 | |
Jayant Chowdhary | 34ce67d | 2018-03-08 11:00:50 -0800 | [diff] [blame] | 1260 | binderBitness := ctx.DeviceConfig().BinderBitness() |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1261 | |
| 1262 | var ext string |
| 1263 | if isGzip { |
| 1264 | ext = ".lsdump.gz" |
| 1265 | } else { |
| 1266 | ext = ".lsdump" |
| 1267 | } |
| 1268 | |
| 1269 | return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName, |
| 1270 | version, binderBitness, archNameAndVariant, "source-based", |
| 1271 | fileName+ext) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1274 | // PathForBazelOut returns a Path representing the paths... under an output directory dedicated to |
| 1275 | // bazel-owned outputs. |
| 1276 | func PathForBazelOut(ctx PathContext, paths ...string) BazelOutPath { |
| 1277 | execRootPathComponents := append([]string{"execroot", "__main__"}, paths...) |
| 1278 | execRootPath := filepath.Join(execRootPathComponents...) |
| 1279 | validatedExecRootPath, err := validatePath(execRootPath) |
| 1280 | if err != nil { |
| 1281 | reportPathError(ctx, err) |
| 1282 | } |
| 1283 | |
| 1284 | outputPath := OutputPath{basePath{"", ctx.Config(), ""}, |
| 1285 | ctx.Config().BazelContext.OutputBase()} |
| 1286 | |
| 1287 | return BazelOutPath{ |
| 1288 | OutputPath: outputPath.withRel(validatedExecRootPath), |
| 1289 | } |
| 1290 | } |
| 1291 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1292 | // PathForModuleOut returns a Path representing the paths... under the module's |
| 1293 | // output directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1294 | func PathForModuleOut(ctx ModuleOutPathContext, paths ...string) ModuleOutPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1295 | p, err := validatePath(paths...) |
| 1296 | if err != nil { |
| 1297 | reportPathError(ctx, err) |
| 1298 | } |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1299 | return ModuleOutPath{ |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1300 | OutputPath: pathForModuleOut(ctx).withRel(p), |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1301 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | // ModuleGenPath is a Path representing the 'gen' directory in a module's output |
| 1305 | // directory. Mainly used for generated sources. |
| 1306 | type ModuleGenPath struct { |
| 1307 | ModuleOutPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | var _ Path = ModuleGenPath{} |
| 1311 | var _ genPathProvider = ModuleGenPath{} |
| 1312 | var _ objPathProvider = ModuleGenPath{} |
| 1313 | |
| 1314 | // PathForModuleGen returns a Path representing the paths... under the module's |
| 1315 | // `gen' directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1316 | func PathForModuleGen(ctx ModuleOutPathContext, paths ...string) ModuleGenPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1317 | p, err := validatePath(paths...) |
| 1318 | if err != nil { |
| 1319 | reportPathError(ctx, err) |
| 1320 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1321 | return ModuleGenPath{ |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1322 | ModuleOutPath: ModuleOutPath{ |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1323 | OutputPath: pathForModuleOut(ctx).withRel("gen").withRel(p), |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1324 | }, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1328 | func (p ModuleGenPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1329 | // TODO: make a different path for local vs remote generated files? |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 1330 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1333 | func (p ModuleGenPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1334 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1335 | } |
| 1336 | |
| 1337 | // ModuleObjPath is a Path representing the 'obj' directory in a module's output |
| 1338 | // directory. Used for compiled objects. |
| 1339 | type ModuleObjPath struct { |
| 1340 | ModuleOutPath |
| 1341 | } |
| 1342 | |
| 1343 | var _ Path = ModuleObjPath{} |
| 1344 | |
| 1345 | // PathForModuleObj returns a Path representing the paths... under the module's |
| 1346 | // 'obj' directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1347 | func PathForModuleObj(ctx ModuleOutPathContext, pathComponents ...string) ModuleObjPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1348 | p, err := validatePath(pathComponents...) |
| 1349 | if err != nil { |
| 1350 | reportPathError(ctx, err) |
| 1351 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1352 | return ModuleObjPath{PathForModuleOut(ctx, "obj", p)} |
| 1353 | } |
| 1354 | |
| 1355 | // ModuleResPath is a a Path representing the 'res' directory in a module's |
| 1356 | // output directory. |
| 1357 | type ModuleResPath struct { |
| 1358 | ModuleOutPath |
| 1359 | } |
| 1360 | |
| 1361 | var _ Path = ModuleResPath{} |
| 1362 | |
| 1363 | // PathForModuleRes returns a Path representing the paths... under the module's |
| 1364 | // 'res' directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1365 | func PathForModuleRes(ctx ModuleOutPathContext, pathComponents ...string) ModuleResPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1366 | p, err := validatePath(pathComponents...) |
| 1367 | if err != nil { |
| 1368 | reportPathError(ctx, err) |
| 1369 | } |
| 1370 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1371 | return ModuleResPath{PathForModuleOut(ctx, "res", p)} |
| 1372 | } |
| 1373 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1374 | // InstallPath is a Path representing a installed file path rooted from the build directory |
| 1375 | type InstallPath struct { |
| 1376 | basePath |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1377 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1378 | // partitionDir is the part of the InstallPath that is automatically determined according to the context. |
| 1379 | // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules. |
| 1380 | partitionDir string |
| 1381 | |
| 1382 | // makePath indicates whether this path is for Soong (false) or Make (true). |
| 1383 | makePath bool |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1386 | func (p InstallPath) buildDir() string { |
| 1387 | return p.config.buildDir |
| 1388 | } |
| 1389 | |
Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 1390 | func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { |
| 1391 | panic("Not implemented") |
| 1392 | } |
| 1393 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1394 | var _ Path = InstallPath{} |
| 1395 | var _ WritablePath = InstallPath{} |
| 1396 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1397 | func (p InstallPath) writablePath() {} |
| 1398 | |
| 1399 | func (p InstallPath) String() string { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1400 | if p.makePath { |
| 1401 | // Make path starts with out/ instead of out/soong. |
| 1402 | return filepath.Join(p.config.buildDir, "../", p.path) |
| 1403 | } else { |
| 1404 | return filepath.Join(p.config.buildDir, p.path) |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | // PartitionDir returns the path to the partition where the install path is rooted at. It is |
| 1409 | // out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules. |
| 1410 | // The ./soong is dropped if the install path is for Make. |
| 1411 | func (p InstallPath) PartitionDir() string { |
| 1412 | if p.makePath { |
| 1413 | return filepath.Join(p.config.buildDir, "../", p.partitionDir) |
| 1414 | } else { |
| 1415 | return filepath.Join(p.config.buildDir, p.partitionDir) |
| 1416 | } |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | // Join creates a new InstallPath with paths... joined with the current path. The |
| 1420 | // provided paths... may not use '..' to escape from the current path. |
| 1421 | func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath { |
| 1422 | path, err := validatePath(paths...) |
| 1423 | if err != nil { |
| 1424 | reportPathError(ctx, err) |
| 1425 | } |
| 1426 | return p.withRel(path) |
| 1427 | } |
| 1428 | |
| 1429 | func (p InstallPath) withRel(rel string) InstallPath { |
| 1430 | p.basePath = p.basePath.withRel(rel) |
| 1431 | return p |
| 1432 | } |
| 1433 | |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1434 | // ToMakePath returns a new InstallPath that points to Make's install directory instead of Soong's, |
| 1435 | // i.e. out/ instead of out/soong/. |
| 1436 | func (p InstallPath) ToMakePath() InstallPath { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1437 | p.makePath = true |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1438 | return p |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1441 | // PathForModuleInstall returns a Path representing the install path for the |
| 1442 | // module appended with paths... |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1443 | func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath { |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1444 | os := ctx.Os() |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1445 | arch := ctx.Arch().ArchType |
| 1446 | forceOS, forceArch := ctx.InstallForceOS() |
| 1447 | if forceOS != nil { |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1448 | os = *forceOS |
| 1449 | } |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1450 | if forceArch != nil { |
| 1451 | arch = *forceArch |
| 1452 | } |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1453 | partition := modulePartition(ctx, os) |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1454 | |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1455 | ret := pathForInstall(ctx, os, arch, partition, ctx.Debug(), pathComponents...) |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1456 | |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 1457 | if ctx.InstallBypassMake() && ctx.Config().KatiEnabled() { |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1458 | ret = ret.ToMakePath() |
| 1459 | } |
| 1460 | |
| 1461 | return ret |
| 1462 | } |
| 1463 | |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1464 | func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string, debug bool, |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1465 | pathComponents ...string) InstallPath { |
| 1466 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1467 | var partionPaths []string |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1468 | |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1469 | if os.Class == Device { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1470 | partionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1471 | } else { |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1472 | osName := os.String() |
| 1473 | if os == Linux { |
| 1474 | // instead of linux_glibc |
| 1475 | osName = "linux" |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 1476 | } |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1477 | // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH) |
| 1478 | // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem |
| 1479 | // to have a plan to fix it (see the comment in build/make/core/envsetup.mk). |
| 1480 | // Let's keep using x86 for the existing cases until we have a need to support |
| 1481 | // other architectures. |
| 1482 | archName := arch.String() |
| 1483 | if os.Class == Host && (arch == X86_64 || arch == Common) { |
| 1484 | archName = "x86" |
| 1485 | } |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1486 | partionPaths = []string{"host", osName + "-" + archName, partition} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1487 | } |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1488 | if debug { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1489 | partionPaths = append([]string{"debug"}, partionPaths...) |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame] | 1490 | } |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1491 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1492 | partionPath, err := validatePath(partionPaths...) |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1493 | if err != nil { |
| 1494 | reportPathError(ctx, err) |
| 1495 | } |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1496 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1497 | base := InstallPath{ |
| 1498 | basePath: basePath{partionPath, ctx.Config(), ""}, |
| 1499 | partitionDir: partionPath, |
| 1500 | makePath: false, |
| 1501 | } |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1502 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1503 | return base.Join(ctx, pathComponents...) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 1506 | func pathForNdkOrSdkInstall(ctx PathContext, prefix string, paths []string) InstallPath { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1507 | base := InstallPath{ |
| 1508 | basePath: basePath{prefix, ctx.Config(), ""}, |
| 1509 | partitionDir: prefix, |
| 1510 | makePath: false, |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1511 | } |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1512 | return base.Join(ctx, paths...) |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 1515 | func PathForNdkInstall(ctx PathContext, paths ...string) InstallPath { |
| 1516 | return pathForNdkOrSdkInstall(ctx, "ndk", paths) |
| 1517 | } |
| 1518 | |
| 1519 | func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath { |
| 1520 | return pathForNdkOrSdkInstall(ctx, "mainline-sdks", paths) |
| 1521 | } |
| 1522 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1523 | func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1524 | rel := Rel(ctx, PathForOutput(ctx, "target", "product", ctx.Config().DeviceName()).String(), path.String()) |
| 1525 | |
| 1526 | return "/" + rel |
| 1527 | } |
| 1528 | |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1529 | func modulePartition(ctx ModuleInstallPathContext, os OsType) string { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1530 | var partition string |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1531 | if ctx.InstallInTestcases() { |
| 1532 | // "testcases" install directory can be used for host or device modules. |
Jaewoong Jung | 0949f31 | 2019-09-11 10:25:18 -0700 | [diff] [blame] | 1533 | partition = "testcases" |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1534 | } else if os.Class == Device { |
| 1535 | if ctx.InstallInData() { |
| 1536 | partition = "data" |
| 1537 | } else if ctx.InstallInRamdisk() { |
| 1538 | if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() { |
| 1539 | partition = "recovery/root/first_stage_ramdisk" |
| 1540 | } else { |
| 1541 | partition = "ramdisk" |
| 1542 | } |
| 1543 | if !ctx.InstallInRoot() { |
| 1544 | partition += "/system" |
| 1545 | } |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 1546 | } else if ctx.InstallInVendorRamdisk() { |
Yifan Hong | 39143a9 | 2020-10-26 12:43:12 -0700 | [diff] [blame] | 1547 | // The module is only available after switching root into |
| 1548 | // /first_stage_ramdisk. To expose the module before switching root |
| 1549 | // on a device without a dedicated recovery partition, install the |
| 1550 | // recovery variant. |
Yifan Hong | dd8dacc | 2020-10-21 15:40:17 -0700 | [diff] [blame] | 1551 | if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() { |
Yifan Hong | 39143a9 | 2020-10-26 12:43:12 -0700 | [diff] [blame] | 1552 | partition = "vendor-ramdisk/first_stage_ramdisk" |
Yifan Hong | dd8dacc | 2020-10-21 15:40:17 -0700 | [diff] [blame] | 1553 | } else { |
| 1554 | partition = "vendor-ramdisk" |
| 1555 | } |
| 1556 | if !ctx.InstallInRoot() { |
| 1557 | partition += "/system" |
| 1558 | } |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1559 | } else if ctx.InstallInRecovery() { |
| 1560 | if ctx.InstallInRoot() { |
| 1561 | partition = "recovery/root" |
| 1562 | } else { |
| 1563 | // the layout of recovery partion is the same as that of system partition |
| 1564 | partition = "recovery/root/system" |
| 1565 | } |
| 1566 | } else if ctx.SocSpecific() { |
| 1567 | partition = ctx.DeviceConfig().VendorPath() |
| 1568 | } else if ctx.DeviceSpecific() { |
| 1569 | partition = ctx.DeviceConfig().OdmPath() |
| 1570 | } else if ctx.ProductSpecific() { |
| 1571 | partition = ctx.DeviceConfig().ProductPath() |
| 1572 | } else if ctx.SystemExtSpecific() { |
| 1573 | partition = ctx.DeviceConfig().SystemExtPath() |
| 1574 | } else if ctx.InstallInRoot() { |
| 1575 | partition = "root" |
Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1576 | } else { |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1577 | partition = "system" |
Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1578 | } |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1579 | if ctx.InstallInSanitizerDir() { |
| 1580 | partition = "data/asan/" + partition |
Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1581 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1582 | } |
| 1583 | return partition |
| 1584 | } |
| 1585 | |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1586 | type InstallPaths []InstallPath |
| 1587 | |
| 1588 | // Paths returns the InstallPaths as a Paths |
| 1589 | func (p InstallPaths) Paths() Paths { |
| 1590 | if p == nil { |
| 1591 | return nil |
| 1592 | } |
| 1593 | ret := make(Paths, len(p)) |
| 1594 | for i, path := range p { |
| 1595 | ret[i] = path |
| 1596 | } |
| 1597 | return ret |
| 1598 | } |
| 1599 | |
| 1600 | // Strings returns the string forms of the install paths. |
| 1601 | func (p InstallPaths) Strings() []string { |
| 1602 | if p == nil { |
| 1603 | return nil |
| 1604 | } |
| 1605 | ret := make([]string, len(p)) |
| 1606 | for i, path := range p { |
| 1607 | ret[i] = path.String() |
| 1608 | } |
| 1609 | return ret |
| 1610 | } |
| 1611 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1612 | // validateSafePath validates a path that we trust (may contain ninja variables). |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1613 | // Ensures that each path component does not attempt to leave its component. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1614 | func validateSafePath(pathComponents ...string) (string, error) { |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1615 | for _, path := range pathComponents { |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1616 | path := filepath.Clean(path) |
| 1617 | if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1618 | return "", fmt.Errorf("Path is outside directory: %s", path) |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1619 | } |
| 1620 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1621 | // TODO: filepath.Join isn't necessarily correct with embedded ninja |
| 1622 | // variables. '..' may remove the entire ninja variable, even if it |
| 1623 | // will be expanded to multiple nested directories. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1624 | return filepath.Join(pathComponents...), nil |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1625 | } |
| 1626 | |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1627 | // validatePath validates that a path does not include ninja variables, and that |
| 1628 | // each path component does not attempt to leave its component. Returns a joined |
| 1629 | // version of each path component. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1630 | func validatePath(pathComponents ...string) (string, error) { |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1631 | for _, path := range pathComponents { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1632 | if strings.Contains(path, "$") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1633 | return "", fmt.Errorf("Path contains invalid character($): %s", path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1634 | } |
| 1635 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1636 | return validateSafePath(pathComponents...) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 1637 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1638 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1639 | func PathForPhony(ctx PathContext, phony string) WritablePath { |
| 1640 | if strings.ContainsAny(phony, "$/") { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1641 | ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1642 | } |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 1643 | return PhonyPath{basePath{phony, ctx.Config(), ""}} |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 1646 | type PhonyPath struct { |
| 1647 | basePath |
| 1648 | } |
| 1649 | |
| 1650 | func (p PhonyPath) writablePath() {} |
| 1651 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1652 | func (p PhonyPath) buildDir() string { |
| 1653 | return p.config.buildDir |
| 1654 | } |
| 1655 | |
Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 1656 | func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { |
| 1657 | panic("Not implemented") |
| 1658 | } |
| 1659 | |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 1660 | var _ Path = PhonyPath{} |
| 1661 | var _ WritablePath = PhonyPath{} |
| 1662 | |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1663 | type testPath struct { |
| 1664 | basePath |
| 1665 | } |
| 1666 | |
| 1667 | func (p testPath) String() string { |
| 1668 | return p.path |
| 1669 | } |
| 1670 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1671 | // PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from |
| 1672 | // within tests. |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1673 | func PathForTesting(paths ...string) Path { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1674 | p, err := validateSafePath(paths...) |
| 1675 | if err != nil { |
| 1676 | panic(err) |
| 1677 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1678 | return testPath{basePath{path: p, rel: p}} |
| 1679 | } |
| 1680 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1681 | // PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests. |
| 1682 | func PathsForTesting(strs ...string) Paths { |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1683 | p := make(Paths, len(strs)) |
| 1684 | for i, s := range strs { |
| 1685 | p[i] = PathForTesting(s) |
| 1686 | } |
| 1687 | |
| 1688 | return p |
| 1689 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1690 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1691 | type testPathContext struct { |
| 1692 | config Config |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1693 | } |
| 1694 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1695 | func (x *testPathContext) Config() Config { return x.config } |
| 1696 | func (x *testPathContext) AddNinjaFileDeps(...string) {} |
| 1697 | |
| 1698 | // PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with |
| 1699 | // PathForOutput. |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1700 | func PathContextForTesting(config Config) PathContext { |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1701 | return &testPathContext{ |
| 1702 | config: config, |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1703 | } |
| 1704 | } |
| 1705 | |
Ulya Trafimovich | ccc8c85 | 2020-10-14 11:29:07 +0100 | [diff] [blame] | 1706 | type testModuleInstallPathContext struct { |
| 1707 | baseModuleContext |
| 1708 | |
| 1709 | inData bool |
| 1710 | inTestcases bool |
| 1711 | inSanitizerDir bool |
| 1712 | inRamdisk bool |
| 1713 | inVendorRamdisk bool |
| 1714 | inRecovery bool |
| 1715 | inRoot bool |
| 1716 | forceOS *OsType |
| 1717 | forceArch *ArchType |
| 1718 | } |
| 1719 | |
| 1720 | func (m testModuleInstallPathContext) Config() Config { |
| 1721 | return m.baseModuleContext.config |
| 1722 | } |
| 1723 | |
| 1724 | func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {} |
| 1725 | |
| 1726 | func (m testModuleInstallPathContext) InstallInData() bool { |
| 1727 | return m.inData |
| 1728 | } |
| 1729 | |
| 1730 | func (m testModuleInstallPathContext) InstallInTestcases() bool { |
| 1731 | return m.inTestcases |
| 1732 | } |
| 1733 | |
| 1734 | func (m testModuleInstallPathContext) InstallInSanitizerDir() bool { |
| 1735 | return m.inSanitizerDir |
| 1736 | } |
| 1737 | |
| 1738 | func (m testModuleInstallPathContext) InstallInRamdisk() bool { |
| 1739 | return m.inRamdisk |
| 1740 | } |
| 1741 | |
| 1742 | func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool { |
| 1743 | return m.inVendorRamdisk |
| 1744 | } |
| 1745 | |
| 1746 | func (m testModuleInstallPathContext) InstallInRecovery() bool { |
| 1747 | return m.inRecovery |
| 1748 | } |
| 1749 | |
| 1750 | func (m testModuleInstallPathContext) InstallInRoot() bool { |
| 1751 | return m.inRoot |
| 1752 | } |
| 1753 | |
| 1754 | func (m testModuleInstallPathContext) InstallBypassMake() bool { |
| 1755 | return false |
| 1756 | } |
| 1757 | |
| 1758 | func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) { |
| 1759 | return m.forceOS, m.forceArch |
| 1760 | } |
| 1761 | |
| 1762 | // Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is |
| 1763 | // default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are |
| 1764 | // delegated to it will panic. |
| 1765 | func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext { |
| 1766 | ctx := &testModuleInstallPathContext{} |
| 1767 | ctx.config = config |
| 1768 | ctx.os = Android |
| 1769 | return ctx |
| 1770 | } |
| 1771 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1772 | // Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if |
| 1773 | // targetPath is not inside basePath. |
| 1774 | func Rel(ctx PathContext, basePath string, targetPath string) string { |
| 1775 | rel, isRel := MaybeRel(ctx, basePath, targetPath) |
| 1776 | if !isRel { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1777 | ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1778 | return "" |
| 1779 | } |
| 1780 | return rel |
| 1781 | } |
| 1782 | |
| 1783 | // MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if |
| 1784 | // targetPath is not inside basePath. |
| 1785 | func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1786 | rel, isRel, err := maybeRelErr(basePath, targetPath) |
| 1787 | if err != nil { |
| 1788 | reportPathError(ctx, err) |
| 1789 | } |
| 1790 | return rel, isRel |
| 1791 | } |
| 1792 | |
| 1793 | func maybeRelErr(basePath string, targetPath string) (string, bool, error) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1794 | // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first. |
| 1795 | if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1796 | return "", false, nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1797 | } |
| 1798 | rel, err := filepath.Rel(basePath, targetPath) |
| 1799 | if err != nil { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1800 | return "", false, err |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1801 | } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1802 | return "", false, nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1803 | } |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1804 | return rel, true, nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1805 | } |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 1806 | |
| 1807 | // Writes a file to the output directory. Attempting to write directly to the output directory |
| 1808 | // will fail due to the sandbox of the soong_build process. |
| 1809 | func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error { |
| 1810 | return ioutil.WriteFile(absolutePath(path.String()), data, perm) |
| 1811 | } |
| 1812 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1813 | func RemoveAllOutputDir(path WritablePath) error { |
| 1814 | return os.RemoveAll(absolutePath(path.String())) |
| 1815 | } |
| 1816 | |
| 1817 | func CreateOutputDirIfNonexistent(path WritablePath, perm os.FileMode) error { |
| 1818 | dir := absolutePath(path.String()) |
| 1819 | if _, err := os.Stat(dir); os.IsNotExist(err) { |
| 1820 | return os.MkdirAll(dir, os.ModePerm) |
| 1821 | } else { |
| 1822 | return err |
| 1823 | } |
| 1824 | } |
| 1825 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 1826 | func absolutePath(path string) string { |
| 1827 | if filepath.IsAbs(path) { |
| 1828 | return path |
| 1829 | } |
| 1830 | return filepath.Join(absSrcDir, path) |
| 1831 | } |
Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 1832 | |
| 1833 | // A DataPath represents the path of a file to be used as data, for example |
| 1834 | // a test library to be installed alongside a test. |
| 1835 | // The data file should be installed (copied from `<SrcPath>`) to |
| 1836 | // `<install_root>/<RelativeInstallPath>/<filename>`, or |
| 1837 | // `<install_root>/<filename>` if RelativeInstallPath is empty. |
| 1838 | type DataPath struct { |
| 1839 | // The path of the data file that should be copied into the data directory |
| 1840 | SrcPath Path |
| 1841 | // The install path of the data file, relative to the install root. |
| 1842 | RelativeInstallPath string |
| 1843 | } |