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