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 { |
| 44 | PathContext |
| 45 | |
| 46 | androidBaseContext |
| 47 | |
| 48 | InstallInData() bool |
| 49 | InstallInSanitizerDir() bool |
| 50 | } |
| 51 | |
| 52 | var _ ModuleInstallPathContext = ModuleContext(nil) |
| 53 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 54 | // errorfContext is the interface containing the Errorf method matching the |
| 55 | // Errorf method in blueprint.SingletonContext. |
| 56 | type errorfContext interface { |
| 57 | Errorf(format string, args ...interface{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 60 | var _ errorfContext = blueprint.SingletonContext(nil) |
| 61 | |
| 62 | // moduleErrorf is the interface containing the ModuleErrorf method matching |
| 63 | // the ModuleErrorf method in blueprint.ModuleContext. |
| 64 | type moduleErrorf interface { |
| 65 | ModuleErrorf(format string, args ...interface{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 68 | var _ moduleErrorf = blueprint.ModuleContext(nil) |
| 69 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 70 | // reportPathError will register an error with the attached context. It |
| 71 | // attempts ctx.ModuleErrorf for a better error message first, then falls |
| 72 | // back to ctx.Errorf. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 73 | func reportPathError(ctx PathContext, err error) { |
| 74 | reportPathErrorf(ctx, "%s", err.Error()) |
| 75 | } |
| 76 | |
| 77 | // reportPathErrorf will register an error with the attached context. It |
| 78 | // attempts ctx.ModuleErrorf for a better error message first, then falls |
| 79 | // back to ctx.Errorf. |
| 80 | func reportPathErrorf(ctx PathContext, format string, args ...interface{}) { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 81 | if mctx, ok := ctx.(moduleErrorf); ok { |
| 82 | mctx.ModuleErrorf(format, args...) |
| 83 | } else if ectx, ok := ctx.(errorfContext); ok { |
| 84 | ectx.Errorf(format, args...) |
| 85 | } else { |
| 86 | panic(fmt.Sprintf(format, args...)) |
Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 90 | type Path interface { |
| 91 | // Returns the path in string form |
| 92 | String() string |
| 93 | |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 94 | // Ext returns the extension of the last element of the path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 95 | Ext() string |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 96 | |
| 97 | // Base returns the last element of the path |
| 98 | Base() string |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 99 | |
| 100 | // Rel returns the portion of the path relative to the directory it was created from. For |
| 101 | // 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] | 102 | // directory, and OutputPath.Join("foo").Rel() would return "foo". |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 103 | Rel() string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // WritablePath is a type of path that can be used as an output for build rules. |
| 107 | type WritablePath interface { |
| 108 | Path |
| 109 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 110 | // the writablePath method doesn't directly do anything, |
| 111 | // 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] | 112 | writablePath() |
| 113 | } |
| 114 | |
| 115 | type genPathProvider interface { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 116 | genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 117 | } |
| 118 | type objPathProvider interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 119 | objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 120 | } |
| 121 | type resPathProvider interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 122 | resPathWithName(ctx ModuleContext, name string) ModuleResPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // GenPathWithExt derives a new file path in ctx's generated sources directory |
| 126 | // from the current path, but with the new extension. |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 127 | func GenPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 128 | if path, ok := p.(genPathProvider); ok { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 129 | return path.genPathWithExt(ctx, subdir, ext) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 130 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 131 | 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] | 132 | return PathForModuleGen(ctx) |
| 133 | } |
| 134 | |
| 135 | // ObjPathWithExt derives a new file path in ctx's object directory from the |
| 136 | // current path, but with the new extension. |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 137 | func ObjPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 138 | if path, ok := p.(objPathProvider); ok { |
| 139 | return path.objPathWithExt(ctx, subdir, ext) |
| 140 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 141 | 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] | 142 | return PathForModuleObj(ctx) |
| 143 | } |
| 144 | |
| 145 | // ResPathWithName derives a new path in ctx's output resource directory, using |
| 146 | // the current path to create the directory name, and the `name` argument for |
| 147 | // the filename. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 148 | func ResPathWithName(ctx ModuleContext, p Path, name string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 149 | if path, ok := p.(resPathProvider); ok { |
| 150 | return path.resPathWithName(ctx, name) |
| 151 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 152 | 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] | 153 | return PathForModuleRes(ctx) |
| 154 | } |
| 155 | |
| 156 | // OptionalPath is a container that may or may not contain a valid Path. |
| 157 | type OptionalPath struct { |
| 158 | valid bool |
| 159 | path Path |
| 160 | } |
| 161 | |
| 162 | // OptionalPathForPath returns an OptionalPath containing the path. |
| 163 | func OptionalPathForPath(path Path) OptionalPath { |
| 164 | if path == nil { |
| 165 | return OptionalPath{} |
| 166 | } |
| 167 | return OptionalPath{valid: true, path: path} |
| 168 | } |
| 169 | |
| 170 | // Valid returns whether there is a valid path |
| 171 | func (p OptionalPath) Valid() bool { |
| 172 | return p.valid |
| 173 | } |
| 174 | |
| 175 | // Path returns the Path embedded in this OptionalPath. You must be sure that |
| 176 | // there is a valid path, since this method will panic if there is not. |
| 177 | func (p OptionalPath) Path() Path { |
| 178 | if !p.valid { |
| 179 | panic("Requesting an invalid path") |
| 180 | } |
| 181 | return p.path |
| 182 | } |
| 183 | |
| 184 | // String returns the string version of the Path, or "" if it isn't valid. |
| 185 | func (p OptionalPath) String() string { |
| 186 | if p.valid { |
| 187 | return p.path.String() |
| 188 | } else { |
| 189 | return "" |
Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 192 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 193 | // Paths is a slice of Path objects, with helpers to operate on the collection. |
| 194 | type Paths []Path |
| 195 | |
| 196 | // PathsForSource returns Paths rooted from SrcDir |
| 197 | func PathsForSource(ctx PathContext, paths []string) Paths { |
| 198 | ret := make(Paths, len(paths)) |
| 199 | for i, path := range paths { |
| 200 | ret[i] = PathForSource(ctx, path) |
| 201 | } |
| 202 | return ret |
| 203 | } |
| 204 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 205 | // ExistentPathsForSources returns a list of Paths rooted from SrcDir that are |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 206 | // found in the tree. If any are not found, they are omitted from the list, |
| 207 | // 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] | 208 | func ExistentPathsForSources(ctx PathContext, paths []string) Paths { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 209 | ret := make(Paths, 0, len(paths)) |
| 210 | for _, path := range paths { |
Colin Cross | 32f3898 | 2018-02-22 11:47:25 -0800 | [diff] [blame] | 211 | p := ExistentPathForSource(ctx, path) |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 212 | if p.Valid() { |
| 213 | ret = append(ret, p.Path()) |
| 214 | } |
| 215 | } |
| 216 | return ret |
| 217 | } |
| 218 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 219 | // PathsForModuleSrc returns Paths rooted from the module's local source |
| 220 | // directory |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 221 | func PathsForModuleSrc(ctx ModuleContext, paths []string) Paths { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 222 | ret := make(Paths, len(paths)) |
| 223 | for i, path := range paths { |
| 224 | ret[i] = PathForModuleSrc(ctx, path) |
| 225 | } |
| 226 | return ret |
| 227 | } |
| 228 | |
| 229 | // pathsForModuleSrcFromFullPath returns Paths rooted from the module's local |
| 230 | // source directory, but strip the local source directory from the beginning of |
| 231 | // each string. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 232 | func pathsForModuleSrcFromFullPath(ctx ModuleContext, paths []string) Paths { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 233 | prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/" |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 234 | if prefix == "./" { |
| 235 | prefix = "" |
| 236 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 237 | ret := make(Paths, 0, len(paths)) |
| 238 | for _, p := range paths { |
| 239 | path := filepath.Clean(p) |
| 240 | if !strings.HasPrefix(path, prefix) { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 241 | reportPathErrorf(ctx, "Path '%s' is not in module source directory '%s'", p, prefix) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 242 | continue |
| 243 | } |
| 244 | ret = append(ret, PathForModuleSrc(ctx, path[len(prefix):])) |
| 245 | } |
| 246 | return ret |
| 247 | } |
| 248 | |
| 249 | // PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's |
| 250 | // local source directory. If none are provided, use the default if it exists. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 251 | func PathsWithOptionalDefaultForModuleSrc(ctx ModuleContext, input []string, def string) Paths { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 252 | if len(input) > 0 { |
| 253 | return PathsForModuleSrc(ctx, input) |
| 254 | } |
| 255 | // Use Glob so that if the default doesn't exist, a dependency is added so that when it |
| 256 | // is created, we're run again. |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 257 | path := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir(), def) |
Colin Cross | 461b445 | 2018-02-23 09:22:42 -0800 | [diff] [blame] | 258 | return ctx.Glob(path, nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Strings returns the Paths in string form |
| 262 | func (p Paths) Strings() []string { |
| 263 | if p == nil { |
| 264 | return nil |
| 265 | } |
| 266 | ret := make([]string, len(p)) |
| 267 | for i, path := range p { |
| 268 | ret[i] = path.String() |
| 269 | } |
| 270 | return ret |
| 271 | } |
| 272 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 273 | // FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It |
| 274 | // 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] | 275 | func FirstUniquePaths(list Paths) Paths { |
| 276 | k := 0 |
| 277 | outer: |
| 278 | for i := 0; i < len(list); i++ { |
| 279 | for j := 0; j < k; j++ { |
| 280 | if list[i] == list[j] { |
| 281 | continue outer |
| 282 | } |
| 283 | } |
| 284 | list[k] = list[i] |
| 285 | k++ |
| 286 | } |
| 287 | return list[:k] |
| 288 | } |
| 289 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 290 | // LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It |
| 291 | // modifies the Paths slice contents in place, and returns a subslice of the original slice. |
| 292 | func LastUniquePaths(list Paths) Paths { |
| 293 | totalSkip := 0 |
| 294 | for i := len(list) - 1; i >= totalSkip; i-- { |
| 295 | skip := 0 |
| 296 | for j := i - 1; j >= totalSkip; j-- { |
| 297 | if list[i] == list[j] { |
| 298 | skip++ |
| 299 | } else { |
| 300 | list[j+skip] = list[j] |
| 301 | } |
| 302 | } |
| 303 | totalSkip += skip |
| 304 | } |
| 305 | return list[totalSkip:] |
| 306 | } |
| 307 | |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 308 | func indexPathList(s Path, list []Path) int { |
| 309 | for i, l := range list { |
| 310 | if l == s { |
| 311 | return i |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | return -1 |
| 316 | } |
| 317 | |
| 318 | func inPathList(p Path, list []Path) bool { |
| 319 | return indexPathList(p, list) != -1 |
| 320 | } |
| 321 | |
| 322 | func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) { |
| 323 | for _, l := range list { |
| 324 | if inPathList(l, filter) { |
| 325 | filtered = append(filtered, l) |
| 326 | } else { |
| 327 | remainder = append(remainder, l) |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return |
| 332 | } |
| 333 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 334 | // HasExt returns true of any of the paths have extension ext, otherwise false |
| 335 | func (p Paths) HasExt(ext string) bool { |
| 336 | for _, path := range p { |
| 337 | if path.Ext() == ext { |
| 338 | return true |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return false |
| 343 | } |
| 344 | |
| 345 | // FilterByExt returns the subset of the paths that have extension ext |
| 346 | func (p Paths) FilterByExt(ext string) Paths { |
| 347 | ret := make(Paths, 0, len(p)) |
| 348 | for _, path := range p { |
| 349 | if path.Ext() == ext { |
| 350 | ret = append(ret, path) |
| 351 | } |
| 352 | } |
| 353 | return ret |
| 354 | } |
| 355 | |
| 356 | // FilterOutByExt returns the subset of the paths that do not have extension ext |
| 357 | func (p Paths) FilterOutByExt(ext string) Paths { |
| 358 | ret := make(Paths, 0, len(p)) |
| 359 | for _, path := range p { |
| 360 | if path.Ext() != ext { |
| 361 | ret = append(ret, path) |
| 362 | } |
| 363 | } |
| 364 | return ret |
| 365 | } |
| 366 | |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 367 | // DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory |
| 368 | // (including subdirectories) are in a contiguous subslice of the list, and can be found in |
| 369 | // O(log(N)) time using a binary search on the directory prefix. |
| 370 | type DirectorySortedPaths Paths |
| 371 | |
| 372 | func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths { |
| 373 | ret := append(DirectorySortedPaths(nil), paths...) |
| 374 | sort.Slice(ret, func(i, j int) bool { |
| 375 | return ret[i].String() < ret[j].String() |
| 376 | }) |
| 377 | return ret |
| 378 | } |
| 379 | |
| 380 | // PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries |
| 381 | // that are in the specified directory and its subdirectories. |
| 382 | func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths { |
| 383 | prefix := filepath.Clean(dir) + "/" |
| 384 | start := sort.Search(len(p), func(i int) bool { |
| 385 | return prefix < p[i].String() |
| 386 | }) |
| 387 | |
| 388 | ret := p[start:] |
| 389 | |
| 390 | end := sort.Search(len(ret), func(i int) bool { |
| 391 | return !strings.HasPrefix(ret[i].String(), prefix) |
| 392 | }) |
| 393 | |
| 394 | ret = ret[:end] |
| 395 | |
| 396 | return Paths(ret) |
| 397 | } |
| 398 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 399 | // WritablePaths is a slice of WritablePaths, used for multiple outputs. |
| 400 | type WritablePaths []WritablePath |
| 401 | |
| 402 | // Strings returns the string forms of the writable paths. |
| 403 | func (p WritablePaths) Strings() []string { |
| 404 | if p == nil { |
| 405 | return nil |
| 406 | } |
| 407 | ret := make([]string, len(p)) |
| 408 | for i, path := range p { |
| 409 | ret[i] = path.String() |
| 410 | } |
| 411 | return ret |
| 412 | } |
| 413 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 414 | // Paths returns the WritablePaths as a Paths |
| 415 | func (p WritablePaths) Paths() Paths { |
| 416 | if p == nil { |
| 417 | return nil |
| 418 | } |
| 419 | ret := make(Paths, len(p)) |
| 420 | for i, path := range p { |
| 421 | ret[i] = path |
| 422 | } |
| 423 | return ret |
| 424 | } |
| 425 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 426 | type basePath struct { |
| 427 | path string |
| 428 | config Config |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 429 | rel string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | func (p basePath) Ext() string { |
| 433 | return filepath.Ext(p.path) |
| 434 | } |
| 435 | |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 436 | func (p basePath) Base() string { |
| 437 | return filepath.Base(p.path) |
| 438 | } |
| 439 | |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 440 | func (p basePath) Rel() string { |
| 441 | if p.rel != "" { |
| 442 | return p.rel |
| 443 | } |
| 444 | return p.path |
| 445 | } |
| 446 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 447 | func (p basePath) String() string { |
| 448 | return p.path |
| 449 | } |
| 450 | |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 451 | func (p basePath) withRel(rel string) basePath { |
| 452 | p.path = filepath.Join(p.path, rel) |
| 453 | p.rel = rel |
| 454 | return p |
| 455 | } |
| 456 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 457 | // SourcePath is a Path representing a file path rooted from SrcDir |
| 458 | type SourcePath struct { |
| 459 | basePath |
| 460 | } |
| 461 | |
| 462 | var _ Path = SourcePath{} |
| 463 | |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 464 | func (p SourcePath) withRel(rel string) SourcePath { |
| 465 | p.basePath = p.basePath.withRel(rel) |
| 466 | return p |
| 467 | } |
| 468 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 469 | // safePathForSource is for paths that we expect are safe -- only for use by go |
| 470 | // code that is embedding ninja variables in paths |
| 471 | func safePathForSource(ctx PathContext, path string) SourcePath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 472 | p, err := validateSafePath(path) |
| 473 | if err != nil { |
| 474 | reportPathError(ctx, err) |
| 475 | } |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 476 | ret := SourcePath{basePath{p, ctx.Config(), ""}} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 477 | |
| 478 | abs, err := filepath.Abs(ret.String()) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 479 | if err != nil { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 480 | reportPathError(ctx, err) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 481 | return ret |
| 482 | } |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 483 | buildroot, err := filepath.Abs(ctx.Config().buildDir) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 484 | if err != nil { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 485 | reportPathError(ctx, err) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 486 | return ret |
| 487 | } |
| 488 | if strings.HasPrefix(abs, buildroot) { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 489 | reportPathErrorf(ctx, "source path %s is in output", abs) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 490 | return ret |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 493 | return ret |
| 494 | } |
| 495 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame^] | 496 | // pathForSource creates a SourcePath from pathComponents, but does not check that it exists. |
| 497 | func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 498 | p, err := validatePath(pathComponents...) |
| 499 | ret := SourcePath{basePath{p, ctx.Config(), ""}} |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 500 | if err != nil { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame^] | 501 | return ret, err |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 502 | } |
| 503 | |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 504 | abs, err := filepath.Abs(ret.String()) |
| 505 | if err != nil { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame^] | 506 | return ret, err |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 507 | } |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 508 | buildroot, err := filepath.Abs(ctx.Config().buildDir) |
| 509 | if err != nil { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame^] | 510 | return ret, err |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 511 | } |
| 512 | if strings.HasPrefix(abs, buildroot) { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame^] | 513 | return ret, fmt.Errorf("source path %s is in output", abs) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame^] | 516 | if pathtools.IsGlob(ret.String()) { |
| 517 | return ret, fmt.Errorf("path may not contain a glob: %s", ret.String()) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 518 | } |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame^] | 519 | |
| 520 | return ret, nil |
| 521 | } |
| 522 | |
| 523 | // existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the |
| 524 | // path does not exist. |
| 525 | func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) { |
| 526 | var files []string |
| 527 | |
| 528 | if gctx, ok := ctx.(PathGlobContext); ok { |
| 529 | // Use glob to produce proper dependencies, even though we only want |
| 530 | // a single file. |
| 531 | files, err = gctx.GlobWithDeps(path.String(), nil) |
| 532 | } else { |
| 533 | var deps []string |
| 534 | // We cannot add build statements in this context, so we fall back to |
| 535 | // AddNinjaFileDeps |
| 536 | files, deps, err = pathtools.Glob(path.String(), nil) |
| 537 | ctx.AddNinjaFileDeps(deps...) |
| 538 | } |
| 539 | |
| 540 | if err != nil { |
| 541 | return false, fmt.Errorf("glob: %s", err.Error()) |
| 542 | } |
| 543 | |
| 544 | return len(files) > 0, nil |
| 545 | } |
| 546 | |
| 547 | // PathForSource joins the provided path components and validates that the result |
| 548 | // neither escapes the source dir nor is in the out dir. |
| 549 | // On error, it will return a usable, but invalid SourcePath, and report a ModuleError. |
| 550 | func PathForSource(ctx PathContext, pathComponents ...string) SourcePath { |
| 551 | path, err := pathForSource(ctx, pathComponents...) |
| 552 | if err != nil { |
| 553 | reportPathError(ctx, err) |
| 554 | } |
| 555 | |
| 556 | if modCtx, ok := ctx.(ModuleContext); ok && ctx.Config().AllowMissingDependencies() { |
| 557 | exists, err := existsWithDependencies(ctx, path) |
| 558 | if err != nil { |
| 559 | reportPathError(ctx, err) |
| 560 | } |
| 561 | if !exists { |
| 562 | modCtx.AddMissingDependencies([]string{path.String()}) |
| 563 | } |
| 564 | } else if exists, _, err := ctx.Fs().Exists(path.String()); err != nil { |
| 565 | reportPathErrorf(ctx, "%s: %s", path, err.Error()) |
| 566 | } else if !exists { |
| 567 | reportPathErrorf(ctx, "source path %s does not exist", path) |
| 568 | } |
| 569 | return path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 572 | // ExistentPathForSource returns an OptionalPath with the SourcePath if the |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 573 | // path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added |
| 574 | // 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] | 575 | func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame^] | 576 | path, err := pathForSource(ctx, pathComponents...) |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 577 | if err != nil { |
| 578 | reportPathError(ctx, err) |
| 579 | return OptionalPath{} |
| 580 | } |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 581 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame^] | 582 | exists, err := existsWithDependencies(ctx, path) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 583 | if err != nil { |
| 584 | reportPathError(ctx, err) |
| 585 | return OptionalPath{} |
| 586 | } |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame^] | 587 | if !exists { |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 588 | return OptionalPath{} |
| 589 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 590 | return OptionalPathForPath(path) |
| 591 | } |
| 592 | |
| 593 | func (p SourcePath) String() string { |
| 594 | return filepath.Join(p.config.srcDir, p.path) |
| 595 | } |
| 596 | |
| 597 | // Join creates a new SourcePath with paths... joined with the current path. The |
| 598 | // provided paths... may not use '..' to escape from the current path. |
| 599 | func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 600 | path, err := validatePath(paths...) |
| 601 | if err != nil { |
| 602 | reportPathError(ctx, err) |
| 603 | } |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 604 | return p.withRel(path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | // OverlayPath returns the overlay for `path' if it exists. This assumes that the |
| 608 | // SourcePath is the path to a resource overlay directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 609 | func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 610 | var relDir string |
| 611 | if moduleSrcPath, ok := path.(ModuleSrcPath); ok { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 612 | relDir = moduleSrcPath.path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 613 | } else if srcPath, ok := path.(SourcePath); ok { |
| 614 | relDir = srcPath.path |
| 615 | } else { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 616 | 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] | 617 | return OptionalPath{} |
| 618 | } |
| 619 | dir := filepath.Join(p.config.srcDir, p.path, relDir) |
| 620 | // 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] | 621 | if pathtools.IsGlob(dir) { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 622 | reportPathErrorf(ctx, "Path may not contain a glob: %s", dir) |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 623 | } |
Colin Cross | 461b445 | 2018-02-23 09:22:42 -0800 | [diff] [blame] | 624 | paths, err := ctx.GlobWithDeps(dir, nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 625 | if err != nil { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 626 | reportPathErrorf(ctx, "glob: %s", err.Error()) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 627 | return OptionalPath{} |
| 628 | } |
| 629 | if len(paths) == 0 { |
| 630 | return OptionalPath{} |
| 631 | } |
| 632 | relPath, err := filepath.Rel(p.config.srcDir, paths[0]) |
| 633 | if err != nil { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 634 | reportPathError(ctx, err) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 635 | return OptionalPath{} |
| 636 | } |
| 637 | return OptionalPathForPath(PathForSource(ctx, relPath)) |
| 638 | } |
| 639 | |
| 640 | // OutputPath is a Path representing a file path rooted from the build directory |
| 641 | type OutputPath struct { |
| 642 | basePath |
| 643 | } |
| 644 | |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 645 | func (p OutputPath) withRel(rel string) OutputPath { |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 646 | p.basePath = p.basePath.withRel(rel) |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 647 | return p |
| 648 | } |
| 649 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 650 | var _ Path = OutputPath{} |
| 651 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 652 | // PathForOutput joins the provided paths and returns an OutputPath that is |
| 653 | // validated to not escape the build dir. |
| 654 | // On error, it will return a usable, but invalid OutputPath, and report a ModuleError. |
| 655 | func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 656 | path, err := validatePath(pathComponents...) |
| 657 | if err != nil { |
| 658 | reportPathError(ctx, err) |
| 659 | } |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 660 | return OutputPath{basePath{path, ctx.Config(), ""}} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | func (p OutputPath) writablePath() {} |
| 664 | |
| 665 | func (p OutputPath) String() string { |
| 666 | return filepath.Join(p.config.buildDir, p.path) |
| 667 | } |
| 668 | |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 669 | func (p OutputPath) RelPathString() string { |
| 670 | return p.path |
| 671 | } |
| 672 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 673 | // Join creates a new OutputPath with paths... joined with the current path. The |
| 674 | // provided paths... may not use '..' to escape from the current path. |
| 675 | func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 676 | path, err := validatePath(paths...) |
| 677 | if err != nil { |
| 678 | reportPathError(ctx, err) |
| 679 | } |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 680 | return p.withRel(path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | // PathForIntermediates returns an OutputPath representing the top-level |
| 684 | // intermediates directory. |
| 685 | func PathForIntermediates(ctx PathContext, paths ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 686 | path, err := validatePath(paths...) |
| 687 | if err != nil { |
| 688 | reportPathError(ctx, err) |
| 689 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 690 | return PathForOutput(ctx, ".intermediates", path) |
| 691 | } |
| 692 | |
| 693 | // ModuleSrcPath is a Path representing a file rooted from a module's local source dir |
| 694 | type ModuleSrcPath struct { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 695 | SourcePath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | var _ Path = ModuleSrcPath{} |
| 699 | var _ genPathProvider = ModuleSrcPath{} |
| 700 | var _ objPathProvider = ModuleSrcPath{} |
| 701 | var _ resPathProvider = ModuleSrcPath{} |
| 702 | |
| 703 | // PathForModuleSrc returns a ModuleSrcPath representing the paths... under the |
| 704 | // module's local source directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 705 | func PathForModuleSrc(ctx ModuleContext, paths ...string) ModuleSrcPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 706 | p, err := validatePath(paths...) |
| 707 | if err != nil { |
| 708 | reportPathError(ctx, err) |
| 709 | } |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame^] | 710 | |
| 711 | srcPath, err := pathForSource(ctx, ctx.ModuleDir(), p) |
| 712 | if err != nil { |
| 713 | reportPathError(ctx, err) |
| 714 | } |
| 715 | |
| 716 | path := ModuleSrcPath{srcPath} |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 717 | path.basePath.rel = p |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame^] | 718 | |
| 719 | if exists, _, err := ctx.Fs().Exists(path.String()); err != nil { |
| 720 | reportPathErrorf(ctx, "%s: %s", path, err.Error()) |
| 721 | } else if !exists { |
| 722 | reportPathErrorf(ctx, "module source path %s does not exist", path) |
| 723 | } |
| 724 | |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 725 | return path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | // OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a |
| 729 | // valid path if p is non-nil. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 730 | func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 731 | if p == nil { |
| 732 | return OptionalPath{} |
| 733 | } |
| 734 | return OptionalPathForPath(PathForModuleSrc(ctx, *p)) |
| 735 | } |
| 736 | |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 737 | func (p ModuleSrcPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 738 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 741 | func (p ModuleSrcPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 742 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 745 | func (p ModuleSrcPath) resPathWithName(ctx ModuleContext, name string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 746 | // TODO: Use full directory if the new ctx is not the current ctx? |
| 747 | return PathForModuleRes(ctx, p.path, name) |
| 748 | } |
| 749 | |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 750 | func (p ModuleSrcPath) WithSubDir(ctx ModuleContext, subdir string) ModuleSrcPath { |
| 751 | subdir = PathForModuleSrc(ctx, subdir).String() |
| 752 | var err error |
| 753 | rel, err := filepath.Rel(subdir, p.path) |
| 754 | if err != nil { |
| 755 | ctx.ModuleErrorf("source file %q is not under path %q", p.path, subdir) |
| 756 | return p |
| 757 | } |
| 758 | p.rel = rel |
| 759 | return p |
| 760 | } |
| 761 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 762 | // ModuleOutPath is a Path representing a module's output directory. |
| 763 | type ModuleOutPath struct { |
| 764 | OutputPath |
| 765 | } |
| 766 | |
| 767 | var _ Path = ModuleOutPath{} |
| 768 | |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 769 | func pathForModule(ctx ModuleContext) OutputPath { |
| 770 | return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir()) |
| 771 | } |
| 772 | |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 773 | // PathForVndkRefDump returns an OptionalPath representing the path of the reference |
| 774 | // abi dump for the given module. This is not guaranteed to be valid. |
| 775 | func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string, vndkOrNdk, isSourceDump bool) OptionalPath { |
| 776 | archName := ctx.Arch().ArchType.Name |
| 777 | var sourceOrBinaryDir string |
| 778 | var vndkOrNdkDir string |
| 779 | var ext string |
| 780 | if isSourceDump { |
Jayant Chowdhary | 715cac3 | 2017-04-20 06:53:59 -0700 | [diff] [blame] | 781 | ext = ".lsdump.gz" |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 782 | sourceOrBinaryDir = "source-based" |
| 783 | } else { |
Jayant Chowdhary | 715cac3 | 2017-04-20 06:53:59 -0700 | [diff] [blame] | 784 | ext = ".bdump.gz" |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 785 | sourceOrBinaryDir = "binary-based" |
| 786 | } |
| 787 | if vndkOrNdk { |
| 788 | vndkOrNdkDir = "vndk" |
| 789 | } else { |
| 790 | vndkOrNdkDir = "ndk" |
| 791 | } |
| 792 | refDumpFileStr := "prebuilts/abi-dumps/" + vndkOrNdkDir + "/" + version + "/" + |
| 793 | archName + "/" + sourceOrBinaryDir + "/" + fileName + ext |
Colin Cross | 32f3898 | 2018-02-22 11:47:25 -0800 | [diff] [blame] | 794 | return ExistentPathForSource(ctx, refDumpFileStr) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 795 | } |
| 796 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 797 | // PathForModuleOut returns a Path representing the paths... under the module's |
| 798 | // output directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 799 | func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 800 | p, err := validatePath(paths...) |
| 801 | if err != nil { |
| 802 | reportPathError(ctx, err) |
| 803 | } |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 804 | return ModuleOutPath{ |
| 805 | OutputPath: pathForModule(ctx).withRel(p), |
| 806 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | // ModuleGenPath is a Path representing the 'gen' directory in a module's output |
| 810 | // directory. Mainly used for generated sources. |
| 811 | type ModuleGenPath struct { |
| 812 | ModuleOutPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | var _ Path = ModuleGenPath{} |
| 816 | var _ genPathProvider = ModuleGenPath{} |
| 817 | var _ objPathProvider = ModuleGenPath{} |
| 818 | |
| 819 | // PathForModuleGen returns a Path representing the paths... under the module's |
| 820 | // `gen' directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 821 | func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 822 | p, err := validatePath(paths...) |
| 823 | if err != nil { |
| 824 | reportPathError(ctx, err) |
| 825 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 826 | return ModuleGenPath{ |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 827 | ModuleOutPath: ModuleOutPath{ |
| 828 | OutputPath: pathForModule(ctx).withRel("gen").withRel(p), |
| 829 | }, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 830 | } |
| 831 | } |
| 832 | |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 833 | func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 834 | // TODO: make a different path for local vs remote generated files? |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 835 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 838 | func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 839 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 840 | } |
| 841 | |
| 842 | // ModuleObjPath is a Path representing the 'obj' directory in a module's output |
| 843 | // directory. Used for compiled objects. |
| 844 | type ModuleObjPath struct { |
| 845 | ModuleOutPath |
| 846 | } |
| 847 | |
| 848 | var _ Path = ModuleObjPath{} |
| 849 | |
| 850 | // PathForModuleObj returns a Path representing the paths... under the module's |
| 851 | // 'obj' directory. |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 852 | func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 853 | p, err := validatePath(pathComponents...) |
| 854 | if err != nil { |
| 855 | reportPathError(ctx, err) |
| 856 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 857 | return ModuleObjPath{PathForModuleOut(ctx, "obj", p)} |
| 858 | } |
| 859 | |
| 860 | // ModuleResPath is a a Path representing the 'res' directory in a module's |
| 861 | // output directory. |
| 862 | type ModuleResPath struct { |
| 863 | ModuleOutPath |
| 864 | } |
| 865 | |
| 866 | var _ Path = ModuleResPath{} |
| 867 | |
| 868 | // PathForModuleRes returns a Path representing the paths... under the module's |
| 869 | // 'res' directory. |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 870 | func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 871 | p, err := validatePath(pathComponents...) |
| 872 | if err != nil { |
| 873 | reportPathError(ctx, err) |
| 874 | } |
| 875 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 876 | return ModuleResPath{PathForModuleOut(ctx, "res", p)} |
| 877 | } |
| 878 | |
| 879 | // PathForModuleInstall returns a Path representing the install path for the |
| 880 | // module appended with paths... |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 881 | func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) OutputPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 882 | var outPaths []string |
| 883 | if ctx.Device() { |
Vishwath Mohan | 87f3b24 | 2017-06-07 12:31:57 -0700 | [diff] [blame] | 884 | var partition string |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 885 | if ctx.InstallInData() { |
Vishwath Mohan | 87f3b24 | 2017-06-07 12:31:57 -0700 | [diff] [blame] | 886 | partition = "data" |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 887 | } else if ctx.SocSpecific() { |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 888 | partition = ctx.DeviceConfig().VendorPath() |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 889 | } else if ctx.DeviceSpecific() { |
| 890 | partition = ctx.DeviceConfig().OdmPath() |
| 891 | } else if ctx.ProductSpecific() { |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 892 | partition = ctx.DeviceConfig().ProductPath() |
Vishwath Mohan | 87f3b24 | 2017-06-07 12:31:57 -0700 | [diff] [blame] | 893 | } else { |
| 894 | partition = "system" |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame] | 895 | } |
Vishwath Mohan | 1dd8839 | 2017-03-29 22:00:18 -0700 | [diff] [blame] | 896 | |
| 897 | if ctx.InstallInSanitizerDir() { |
| 898 | partition = "data/asan/" + partition |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame] | 899 | } |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 900 | outPaths = []string{"target", "product", ctx.Config().DeviceName(), partition} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 901 | } else { |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 902 | switch ctx.Os() { |
| 903 | case Linux: |
| 904 | outPaths = []string{"host", "linux-x86"} |
| 905 | case LinuxBionic: |
| 906 | // TODO: should this be a separate top level, or shared with linux-x86? |
| 907 | outPaths = []string{"host", "linux_bionic-x86"} |
| 908 | default: |
| 909 | outPaths = []string{"host", ctx.Os().String() + "-x86"} |
| 910 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 911 | } |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame] | 912 | if ctx.Debug() { |
| 913 | outPaths = append([]string{"debug"}, outPaths...) |
| 914 | } |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 915 | outPaths = append(outPaths, pathComponents...) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 916 | return PathForOutput(ctx, outPaths...) |
| 917 | } |
| 918 | |
| 919 | // validateSafePath validates a path that we trust (may contain ninja variables). |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 920 | // Ensures that each path component does not attempt to leave its component. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 921 | func validateSafePath(pathComponents ...string) (string, error) { |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 922 | for _, path := range pathComponents { |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 923 | path := filepath.Clean(path) |
| 924 | if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 925 | return "", fmt.Errorf("Path is outside directory: %s", path) |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 926 | } |
| 927 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 928 | // TODO: filepath.Join isn't necessarily correct with embedded ninja |
| 929 | // variables. '..' may remove the entire ninja variable, even if it |
| 930 | // will be expanded to multiple nested directories. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 931 | return filepath.Join(pathComponents...), nil |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 934 | // validatePath validates that a path does not include ninja variables, and that |
| 935 | // each path component does not attempt to leave its component. Returns a joined |
| 936 | // version of each path component. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 937 | func validatePath(pathComponents ...string) (string, error) { |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 938 | for _, path := range pathComponents { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 939 | if strings.Contains(path, "$") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 940 | return "", fmt.Errorf("Path contains invalid character($): %s", path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 941 | } |
| 942 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 943 | return validateSafePath(pathComponents...) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 944 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 945 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 946 | func PathForPhony(ctx PathContext, phony string) WritablePath { |
| 947 | if strings.ContainsAny(phony, "$/") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 948 | reportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 949 | } |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 950 | return PhonyPath{basePath{phony, ctx.Config(), ""}} |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 951 | } |
| 952 | |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 953 | type PhonyPath struct { |
| 954 | basePath |
| 955 | } |
| 956 | |
| 957 | func (p PhonyPath) writablePath() {} |
| 958 | |
| 959 | var _ Path = PhonyPath{} |
| 960 | var _ WritablePath = PhonyPath{} |
| 961 | |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 962 | type testPath struct { |
| 963 | basePath |
| 964 | } |
| 965 | |
| 966 | func (p testPath) String() string { |
| 967 | return p.path |
| 968 | } |
| 969 | |
| 970 | func PathForTesting(paths ...string) Path { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 971 | p, err := validateSafePath(paths...) |
| 972 | if err != nil { |
| 973 | panic(err) |
| 974 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 975 | return testPath{basePath{path: p, rel: p}} |
| 976 | } |
| 977 | |
| 978 | func PathsForTesting(strs []string) Paths { |
| 979 | p := make(Paths, len(strs)) |
| 980 | for i, s := range strs { |
| 981 | p[i] = PathForTesting(s) |
| 982 | } |
| 983 | |
| 984 | return p |
| 985 | } |