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