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