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