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