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