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 | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 19 | "os" |
Colin Cross | 6a745c6 | 2015-06-16 16:38:10 -0700 | [diff] [blame] | 20 | "path/filepath" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 21 | "reflect" |
| 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 { |
| 31 | Config() interface{} |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 32 | AddNinjaFileDeps(deps ...string) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 35 | type PathGlobContext interface { |
| 36 | GlobWithDeps(globPattern string, excludes []string) ([]string, error) |
| 37 | } |
| 38 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 39 | var _ PathContext = blueprint.SingletonContext(nil) |
| 40 | var _ PathContext = blueprint.ModuleContext(nil) |
| 41 | |
| 42 | // errorfContext is the interface containing the Errorf method matching the |
| 43 | // Errorf method in blueprint.SingletonContext. |
| 44 | type errorfContext interface { |
| 45 | Errorf(format string, args ...interface{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 48 | var _ errorfContext = blueprint.SingletonContext(nil) |
| 49 | |
| 50 | // moduleErrorf is the interface containing the ModuleErrorf method matching |
| 51 | // the ModuleErrorf method in blueprint.ModuleContext. |
| 52 | type moduleErrorf interface { |
| 53 | ModuleErrorf(format string, args ...interface{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 56 | var _ moduleErrorf = blueprint.ModuleContext(nil) |
| 57 | |
| 58 | // pathConfig returns the android Config interface associated to the context. |
| 59 | // Panics if the context isn't affiliated with an android build. |
| 60 | func pathConfig(ctx PathContext) Config { |
| 61 | if ret, ok := ctx.Config().(Config); ok { |
| 62 | return ret |
| 63 | } |
| 64 | panic("Paths may only be used on Soong builds") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 67 | // reportPathError will register an error with the attached context. It |
| 68 | // attempts ctx.ModuleErrorf for a better error message first, then falls |
| 69 | // back to ctx.Errorf. |
| 70 | func reportPathError(ctx PathContext, format string, args ...interface{}) { |
| 71 | if mctx, ok := ctx.(moduleErrorf); ok { |
| 72 | mctx.ModuleErrorf(format, args...) |
| 73 | } else if ectx, ok := ctx.(errorfContext); ok { |
| 74 | ectx.Errorf(format, args...) |
| 75 | } else { |
| 76 | panic(fmt.Sprintf(format, args...)) |
Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 80 | type Path interface { |
| 81 | // Returns the path in string form |
| 82 | String() string |
| 83 | |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 84 | // Ext returns the extension of the last element of the path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 85 | Ext() string |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 86 | |
| 87 | // Base returns the last element of the path |
| 88 | Base() string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // WritablePath is a type of path that can be used as an output for build rules. |
| 92 | type WritablePath interface { |
| 93 | Path |
| 94 | |
| 95 | writablePath() |
| 96 | } |
| 97 | |
| 98 | type genPathProvider interface { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 99 | genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 100 | } |
| 101 | type objPathProvider interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 102 | objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 103 | } |
| 104 | type resPathProvider interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 105 | resPathWithName(ctx ModuleContext, name string) ModuleResPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // GenPathWithExt derives a new file path in ctx's generated sources directory |
| 109 | // from the current path, but with the new extension. |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 110 | func GenPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 111 | if path, ok := p.(genPathProvider); ok { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 112 | return path.genPathWithExt(ctx, subdir, ext) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 113 | } |
| 114 | reportPathError(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p) |
| 115 | return PathForModuleGen(ctx) |
| 116 | } |
| 117 | |
| 118 | // ObjPathWithExt derives a new file path in ctx's object directory from the |
| 119 | // current path, but with the new extension. |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 120 | func ObjPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 121 | if path, ok := p.(objPathProvider); ok { |
| 122 | return path.objPathWithExt(ctx, subdir, ext) |
| 123 | } |
| 124 | reportPathError(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p) |
| 125 | return PathForModuleObj(ctx) |
| 126 | } |
| 127 | |
| 128 | // ResPathWithName derives a new path in ctx's output resource directory, using |
| 129 | // the current path to create the directory name, and the `name` argument for |
| 130 | // the filename. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 131 | func ResPathWithName(ctx ModuleContext, p Path, name string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 132 | if path, ok := p.(resPathProvider); ok { |
| 133 | return path.resPathWithName(ctx, name) |
| 134 | } |
| 135 | reportPathError(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p) |
| 136 | return PathForModuleRes(ctx) |
| 137 | } |
| 138 | |
| 139 | // OptionalPath is a container that may or may not contain a valid Path. |
| 140 | type OptionalPath struct { |
| 141 | valid bool |
| 142 | path Path |
| 143 | } |
| 144 | |
| 145 | // OptionalPathForPath returns an OptionalPath containing the path. |
| 146 | func OptionalPathForPath(path Path) OptionalPath { |
| 147 | if path == nil { |
| 148 | return OptionalPath{} |
| 149 | } |
| 150 | return OptionalPath{valid: true, path: path} |
| 151 | } |
| 152 | |
| 153 | // Valid returns whether there is a valid path |
| 154 | func (p OptionalPath) Valid() bool { |
| 155 | return p.valid |
| 156 | } |
| 157 | |
| 158 | // Path returns the Path embedded in this OptionalPath. You must be sure that |
| 159 | // there is a valid path, since this method will panic if there is not. |
| 160 | func (p OptionalPath) Path() Path { |
| 161 | if !p.valid { |
| 162 | panic("Requesting an invalid path") |
| 163 | } |
| 164 | return p.path |
| 165 | } |
| 166 | |
| 167 | // String returns the string version of the Path, or "" if it isn't valid. |
| 168 | func (p OptionalPath) String() string { |
| 169 | if p.valid { |
| 170 | return p.path.String() |
| 171 | } else { |
| 172 | return "" |
Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 175 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 176 | // Paths is a slice of Path objects, with helpers to operate on the collection. |
| 177 | type Paths []Path |
| 178 | |
| 179 | // PathsForSource returns Paths rooted from SrcDir |
| 180 | func PathsForSource(ctx PathContext, paths []string) Paths { |
Dan Willemsen | e23dfb7 | 2016-03-11 15:02:46 -0800 | [diff] [blame] | 181 | if pathConfig(ctx).AllowMissingDependencies() { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 182 | if modCtx, ok := ctx.(ModuleContext); ok { |
Dan Willemsen | e23dfb7 | 2016-03-11 15:02:46 -0800 | [diff] [blame] | 183 | ret := make(Paths, 0, len(paths)) |
Dan Willemsen | 0f6042e | 2016-03-11 17:01:03 -0800 | [diff] [blame] | 184 | intermediates := filepath.Join(modCtx.ModuleDir(), modCtx.ModuleName(), modCtx.ModuleSubDir(), "missing") |
Dan Willemsen | e23dfb7 | 2016-03-11 15:02:46 -0800 | [diff] [blame] | 185 | for _, path := range paths { |
| 186 | p := OptionalPathForSource(ctx, intermediates, path) |
| 187 | if p.Valid() { |
| 188 | ret = append(ret, p.Path()) |
| 189 | } else { |
| 190 | modCtx.AddMissingDependencies([]string{path}) |
| 191 | } |
| 192 | } |
| 193 | return ret |
| 194 | } |
| 195 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 196 | ret := make(Paths, len(paths)) |
| 197 | for i, path := range paths { |
| 198 | ret[i] = PathForSource(ctx, path) |
| 199 | } |
| 200 | return ret |
| 201 | } |
| 202 | |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 203 | // PathsForOptionalSource returns a list of Paths rooted from SrcDir that are |
| 204 | // found in the tree. If any are not found, they are omitted from the list, |
| 205 | // and dependencies are added so that we're re-run when they are added. |
| 206 | func PathsForOptionalSource(ctx PathContext, intermediates string, paths []string) Paths { |
| 207 | ret := make(Paths, 0, len(paths)) |
| 208 | for _, path := range paths { |
| 209 | p := OptionalPathForSource(ctx, intermediates, path) |
| 210 | if p.Valid() { |
| 211 | ret = append(ret, p.Path()) |
| 212 | } |
| 213 | } |
| 214 | return ret |
| 215 | } |
| 216 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 217 | // PathsForModuleSrc returns Paths rooted from the module's local source |
| 218 | // directory |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 219 | func PathsForModuleSrc(ctx ModuleContext, paths []string) Paths { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 220 | ret := make(Paths, len(paths)) |
| 221 | for i, path := range paths { |
| 222 | ret[i] = PathForModuleSrc(ctx, path) |
| 223 | } |
| 224 | return ret |
| 225 | } |
| 226 | |
| 227 | // pathsForModuleSrcFromFullPath returns Paths rooted from the module's local |
| 228 | // source directory, but strip the local source directory from the beginning of |
| 229 | // each string. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 230 | func pathsForModuleSrcFromFullPath(ctx ModuleContext, paths []string) Paths { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 231 | prefix := filepath.Join(ctx.AConfig().srcDir, ctx.ModuleDir()) + "/" |
| 232 | ret := make(Paths, 0, len(paths)) |
| 233 | for _, p := range paths { |
| 234 | path := filepath.Clean(p) |
| 235 | if !strings.HasPrefix(path, prefix) { |
| 236 | reportPathError(ctx, "Path '%s' is not in module source directory '%s'", p, prefix) |
| 237 | continue |
| 238 | } |
| 239 | ret = append(ret, PathForModuleSrc(ctx, path[len(prefix):])) |
| 240 | } |
| 241 | return ret |
| 242 | } |
| 243 | |
| 244 | // PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's |
| 245 | // 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] | 246 | func PathsWithOptionalDefaultForModuleSrc(ctx ModuleContext, input []string, def string) Paths { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 247 | if len(input) > 0 { |
| 248 | return PathsForModuleSrc(ctx, input) |
| 249 | } |
| 250 | // Use Glob so that if the default doesn't exist, a dependency is added so that when it |
| 251 | // is created, we're run again. |
| 252 | path := filepath.Join(ctx.AConfig().srcDir, ctx.ModuleDir(), def) |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 253 | return ctx.Glob(path, []string{}) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Strings returns the Paths in string form |
| 257 | func (p Paths) Strings() []string { |
| 258 | if p == nil { |
| 259 | return nil |
| 260 | } |
| 261 | ret := make([]string, len(p)) |
| 262 | for i, path := range p { |
| 263 | ret[i] = path.String() |
| 264 | } |
| 265 | return ret |
| 266 | } |
| 267 | |
| 268 | // WritablePaths is a slice of WritablePaths, used for multiple outputs. |
| 269 | type WritablePaths []WritablePath |
| 270 | |
| 271 | // Strings returns the string forms of the writable paths. |
| 272 | func (p WritablePaths) Strings() []string { |
| 273 | if p == nil { |
| 274 | return nil |
| 275 | } |
| 276 | ret := make([]string, len(p)) |
| 277 | for i, path := range p { |
| 278 | ret[i] = path.String() |
| 279 | } |
| 280 | return ret |
| 281 | } |
| 282 | |
| 283 | type basePath struct { |
| 284 | path string |
| 285 | config Config |
| 286 | } |
| 287 | |
| 288 | func (p basePath) Ext() string { |
| 289 | return filepath.Ext(p.path) |
| 290 | } |
| 291 | |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 292 | func (p basePath) Base() string { |
| 293 | return filepath.Base(p.path) |
| 294 | } |
| 295 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 296 | // SourcePath is a Path representing a file path rooted from SrcDir |
| 297 | type SourcePath struct { |
| 298 | basePath |
| 299 | } |
| 300 | |
| 301 | var _ Path = SourcePath{} |
| 302 | |
| 303 | // safePathForSource is for paths that we expect are safe -- only for use by go |
| 304 | // code that is embedding ninja variables in paths |
| 305 | func safePathForSource(ctx PathContext, path string) SourcePath { |
| 306 | p := validateSafePath(ctx, path) |
| 307 | ret := SourcePath{basePath{p, pathConfig(ctx)}} |
| 308 | |
| 309 | abs, err := filepath.Abs(ret.String()) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 310 | if err != nil { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 311 | reportPathError(ctx, "%s", err.Error()) |
| 312 | return ret |
| 313 | } |
| 314 | buildroot, err := filepath.Abs(pathConfig(ctx).buildDir) |
| 315 | if err != nil { |
| 316 | reportPathError(ctx, "%s", err.Error()) |
| 317 | return ret |
| 318 | } |
| 319 | if strings.HasPrefix(abs, buildroot) { |
| 320 | reportPathError(ctx, "source path %s is in output", abs) |
| 321 | return ret |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 324 | return ret |
| 325 | } |
| 326 | |
| 327 | // PathForSource returns a SourcePath for the provided paths... (which are |
| 328 | // joined together with filepath.Join). This also validates that the path |
| 329 | // doesn't escape the source dir, or is contained in the build dir. On error, it |
| 330 | // will return a usable, but invalid SourcePath, and report a ModuleError. |
| 331 | func PathForSource(ctx PathContext, paths ...string) SourcePath { |
| 332 | p := validatePath(ctx, paths...) |
| 333 | ret := SourcePath{basePath{p, pathConfig(ctx)}} |
| 334 | |
| 335 | abs, err := filepath.Abs(ret.String()) |
| 336 | if err != nil { |
| 337 | reportPathError(ctx, "%s", err.Error()) |
| 338 | return ret |
| 339 | } |
| 340 | buildroot, err := filepath.Abs(pathConfig(ctx).buildDir) |
| 341 | if err != nil { |
| 342 | reportPathError(ctx, "%s", err.Error()) |
| 343 | return ret |
| 344 | } |
| 345 | if strings.HasPrefix(abs, buildroot) { |
| 346 | reportPathError(ctx, "source path %s is in output", abs) |
| 347 | return ret |
| 348 | } |
| 349 | |
| 350 | if _, err = os.Stat(ret.String()); err != nil { |
| 351 | if os.IsNotExist(err) { |
| 352 | reportPathError(ctx, "source path %s does not exist", ret) |
| 353 | } else { |
| 354 | reportPathError(ctx, "%s: %s", ret, err.Error()) |
| 355 | } |
| 356 | } |
| 357 | return ret |
| 358 | } |
| 359 | |
| 360 | // OptionalPathForSource returns an OptionalPath with the SourcePath if the |
| 361 | // path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added |
| 362 | // so that the ninja file will be regenerated if the state of the path changes. |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 363 | func OptionalPathForSource(ctx PathContext, intermediates string, paths ...string) OptionalPath { |
| 364 | if len(paths) == 0 { |
| 365 | // For when someone forgets the 'intermediates' argument |
| 366 | panic("Missing path(s)") |
| 367 | } |
| 368 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 369 | p := validatePath(ctx, paths...) |
| 370 | path := SourcePath{basePath{p, pathConfig(ctx)}} |
| 371 | |
| 372 | abs, err := filepath.Abs(path.String()) |
| 373 | if err != nil { |
| 374 | reportPathError(ctx, "%s", err.Error()) |
| 375 | return OptionalPath{} |
| 376 | } |
| 377 | buildroot, err := filepath.Abs(pathConfig(ctx).buildDir) |
| 378 | if err != nil { |
| 379 | reportPathError(ctx, "%s", err.Error()) |
| 380 | return OptionalPath{} |
| 381 | } |
| 382 | if strings.HasPrefix(abs, buildroot) { |
| 383 | reportPathError(ctx, "source path %s is in output", abs) |
| 384 | return OptionalPath{} |
| 385 | } |
| 386 | |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 387 | if pathtools.IsGlob(path.String()) { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 388 | reportPathError(ctx, "path may not contain a glob: %s", path.String()) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 389 | return OptionalPath{} |
| 390 | } |
| 391 | |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 392 | if gctx, ok := ctx.(PathGlobContext); ok { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 393 | // Use glob to produce proper dependencies, even though we only want |
| 394 | // a single file. |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 395 | files, err := gctx.GlobWithDeps(path.String(), nil) |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 396 | if err != nil { |
| 397 | reportPathError(ctx, "glob: %s", err.Error()) |
| 398 | return OptionalPath{} |
| 399 | } |
| 400 | |
| 401 | if len(files) == 0 { |
| 402 | return OptionalPath{} |
| 403 | } |
| 404 | } else { |
| 405 | // We cannot add build statements in this context, so we fall back to |
| 406 | // AddNinjaFileDeps |
| 407 | files, dirs, err := pathtools.Glob(path.String()) |
| 408 | if err != nil { |
| 409 | reportPathError(ctx, "glob: %s", err.Error()) |
| 410 | return OptionalPath{} |
| 411 | } |
| 412 | |
| 413 | ctx.AddNinjaFileDeps(dirs...) |
| 414 | |
| 415 | if len(files) == 0 { |
| 416 | return OptionalPath{} |
| 417 | } |
| 418 | |
| 419 | ctx.AddNinjaFileDeps(path.String()) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 420 | } |
| 421 | return OptionalPathForPath(path) |
| 422 | } |
| 423 | |
| 424 | func (p SourcePath) String() string { |
| 425 | return filepath.Join(p.config.srcDir, p.path) |
| 426 | } |
| 427 | |
| 428 | // Join creates a new SourcePath with paths... joined with the current path. The |
| 429 | // provided paths... may not use '..' to escape from the current path. |
| 430 | func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath { |
| 431 | path := validatePath(ctx, paths...) |
| 432 | return PathForSource(ctx, p.path, path) |
| 433 | } |
| 434 | |
| 435 | // OverlayPath returns the overlay for `path' if it exists. This assumes that the |
| 436 | // SourcePath is the path to a resource overlay directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 437 | func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 438 | var relDir string |
| 439 | if moduleSrcPath, ok := path.(ModuleSrcPath); ok { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame^] | 440 | relDir = moduleSrcPath.path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 441 | } else if srcPath, ok := path.(SourcePath); ok { |
| 442 | relDir = srcPath.path |
| 443 | } else { |
| 444 | reportPathError(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path) |
| 445 | return OptionalPath{} |
| 446 | } |
| 447 | dir := filepath.Join(p.config.srcDir, p.path, relDir) |
| 448 | // 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] | 449 | if pathtools.IsGlob(dir) { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 450 | reportPathError(ctx, "Path may not contain a glob: %s", dir) |
| 451 | } |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 452 | paths, err := ctx.GlobWithDeps(dir, []string{}) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 453 | if err != nil { |
| 454 | reportPathError(ctx, "glob: %s", err.Error()) |
| 455 | return OptionalPath{} |
| 456 | } |
| 457 | if len(paths) == 0 { |
| 458 | return OptionalPath{} |
| 459 | } |
| 460 | relPath, err := filepath.Rel(p.config.srcDir, paths[0]) |
| 461 | if err != nil { |
| 462 | reportPathError(ctx, "%s", err.Error()) |
| 463 | return OptionalPath{} |
| 464 | } |
| 465 | return OptionalPathForPath(PathForSource(ctx, relPath)) |
| 466 | } |
| 467 | |
| 468 | // OutputPath is a Path representing a file path rooted from the build directory |
| 469 | type OutputPath struct { |
| 470 | basePath |
| 471 | } |
| 472 | |
| 473 | var _ Path = OutputPath{} |
| 474 | |
| 475 | // PathForOutput returns an OutputPath for the provided paths... (which are |
| 476 | // joined together with filepath.Join). This also validates that the path |
| 477 | // does not escape the build dir. On error, it will return a usable, but invalid |
| 478 | // OutputPath, and report a ModuleError. |
| 479 | func PathForOutput(ctx PathContext, paths ...string) OutputPath { |
| 480 | path := validatePath(ctx, paths...) |
| 481 | return OutputPath{basePath{path, pathConfig(ctx)}} |
| 482 | } |
| 483 | |
| 484 | func (p OutputPath) writablePath() {} |
| 485 | |
| 486 | func (p OutputPath) String() string { |
| 487 | return filepath.Join(p.config.buildDir, p.path) |
| 488 | } |
| 489 | |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 490 | func (p OutputPath) RelPathString() string { |
| 491 | return p.path |
| 492 | } |
| 493 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 494 | // Join creates a new OutputPath with paths... joined with the current path. The |
| 495 | // provided paths... may not use '..' to escape from the current path. |
| 496 | func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath { |
| 497 | path := validatePath(ctx, paths...) |
| 498 | return PathForOutput(ctx, p.path, path) |
| 499 | } |
| 500 | |
| 501 | // PathForIntermediates returns an OutputPath representing the top-level |
| 502 | // intermediates directory. |
| 503 | func PathForIntermediates(ctx PathContext, paths ...string) OutputPath { |
| 504 | path := validatePath(ctx, paths...) |
| 505 | return PathForOutput(ctx, ".intermediates", path) |
| 506 | } |
| 507 | |
| 508 | // ModuleSrcPath is a Path representing a file rooted from a module's local source dir |
| 509 | type ModuleSrcPath struct { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame^] | 510 | SourcePath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | var _ Path = ModuleSrcPath{} |
| 514 | var _ genPathProvider = ModuleSrcPath{} |
| 515 | var _ objPathProvider = ModuleSrcPath{} |
| 516 | var _ resPathProvider = ModuleSrcPath{} |
| 517 | |
| 518 | // PathForModuleSrc returns a ModuleSrcPath representing the paths... under the |
| 519 | // module's local source directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 520 | func PathForModuleSrc(ctx ModuleContext, paths ...string) ModuleSrcPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 521 | path := validatePath(ctx, paths...) |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame^] | 522 | return ModuleSrcPath{PathForSource(ctx, ctx.ModuleDir(), path)} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | // OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a |
| 526 | // valid path if p is non-nil. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 527 | func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 528 | if p == nil { |
| 529 | return OptionalPath{} |
| 530 | } |
| 531 | return OptionalPathForPath(PathForModuleSrc(ctx, *p)) |
| 532 | } |
| 533 | |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 534 | func (p ModuleSrcPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame^] | 535 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 538 | func (p ModuleSrcPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame^] | 539 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 542 | func (p ModuleSrcPath) resPathWithName(ctx ModuleContext, name string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 543 | // TODO: Use full directory if the new ctx is not the current ctx? |
| 544 | return PathForModuleRes(ctx, p.path, name) |
| 545 | } |
| 546 | |
| 547 | // ModuleOutPath is a Path representing a module's output directory. |
| 548 | type ModuleOutPath struct { |
| 549 | OutputPath |
| 550 | } |
| 551 | |
| 552 | var _ Path = ModuleOutPath{} |
| 553 | |
| 554 | // PathForModuleOut returns a Path representing the paths... under the module's |
| 555 | // output directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 556 | func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 557 | p := validatePath(ctx, paths...) |
| 558 | return ModuleOutPath{PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir(), p)} |
| 559 | } |
| 560 | |
| 561 | // ModuleGenPath is a Path representing the 'gen' directory in a module's output |
| 562 | // directory. Mainly used for generated sources. |
| 563 | type ModuleGenPath struct { |
| 564 | ModuleOutPath |
| 565 | path string |
| 566 | } |
| 567 | |
| 568 | var _ Path = ModuleGenPath{} |
| 569 | var _ genPathProvider = ModuleGenPath{} |
| 570 | var _ objPathProvider = ModuleGenPath{} |
| 571 | |
| 572 | // PathForModuleGen returns a Path representing the paths... under the module's |
| 573 | // `gen' directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 574 | func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 575 | p := validatePath(ctx, paths...) |
| 576 | return ModuleGenPath{ |
| 577 | PathForModuleOut(ctx, "gen", p), |
| 578 | p, |
| 579 | } |
| 580 | } |
| 581 | |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 582 | func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 583 | // TODO: make a different path for local vs remote generated files? |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 584 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 587 | func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 588 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 589 | } |
| 590 | |
| 591 | // ModuleObjPath is a Path representing the 'obj' directory in a module's output |
| 592 | // directory. Used for compiled objects. |
| 593 | type ModuleObjPath struct { |
| 594 | ModuleOutPath |
| 595 | } |
| 596 | |
| 597 | var _ Path = ModuleObjPath{} |
| 598 | |
| 599 | // PathForModuleObj returns a Path representing the paths... under the module's |
| 600 | // 'obj' directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 601 | func PathForModuleObj(ctx ModuleContext, paths ...string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 602 | p := validatePath(ctx, paths...) |
| 603 | return ModuleObjPath{PathForModuleOut(ctx, "obj", p)} |
| 604 | } |
| 605 | |
| 606 | // ModuleResPath is a a Path representing the 'res' directory in a module's |
| 607 | // output directory. |
| 608 | type ModuleResPath struct { |
| 609 | ModuleOutPath |
| 610 | } |
| 611 | |
| 612 | var _ Path = ModuleResPath{} |
| 613 | |
| 614 | // PathForModuleRes returns a Path representing the paths... under the module's |
| 615 | // 'res' directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 616 | func PathForModuleRes(ctx ModuleContext, paths ...string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 617 | p := validatePath(ctx, paths...) |
| 618 | return ModuleResPath{PathForModuleOut(ctx, "res", p)} |
| 619 | } |
| 620 | |
| 621 | // PathForModuleInstall returns a Path representing the install path for the |
| 622 | // module appended with paths... |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 623 | func PathForModuleInstall(ctx ModuleContext, paths ...string) OutputPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 624 | var outPaths []string |
| 625 | if ctx.Device() { |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame] | 626 | partition := "system" |
| 627 | if ctx.Proprietary() { |
Dan Willemsen | 4353bc4 | 2016-12-05 17:16:02 -0800 | [diff] [blame] | 628 | partition = ctx.DeviceConfig().VendorPath() |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame] | 629 | } |
| 630 | if ctx.InstallInData() { |
| 631 | partition = "data" |
| 632 | } |
| 633 | outPaths = []string{"target", "product", ctx.AConfig().DeviceName(), partition} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 634 | } else { |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 635 | outPaths = []string{"host", ctx.Os().String() + "-x86"} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 636 | } |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame] | 637 | if ctx.Debug() { |
| 638 | outPaths = append([]string{"debug"}, outPaths...) |
| 639 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 640 | outPaths = append(outPaths, paths...) |
| 641 | return PathForOutput(ctx, outPaths...) |
| 642 | } |
| 643 | |
| 644 | // validateSafePath validates a path that we trust (may contain ninja variables). |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 645 | // Ensures that each path component does not attempt to leave its component. |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 646 | func validateSafePath(ctx PathContext, paths ...string) string { |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 647 | for _, path := range paths { |
| 648 | path := filepath.Clean(path) |
| 649 | if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") { |
| 650 | reportPathError(ctx, "Path is outside directory: %s", path) |
| 651 | return "" |
| 652 | } |
| 653 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 654 | // TODO: filepath.Join isn't necessarily correct with embedded ninja |
| 655 | // variables. '..' may remove the entire ninja variable, even if it |
| 656 | // will be expanded to multiple nested directories. |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 657 | return filepath.Join(paths...) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 660 | // validatePath validates that a path does not include ninja variables, and that |
| 661 | // each path component does not attempt to leave its component. Returns a joined |
| 662 | // version of each path component. |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 663 | func validatePath(ctx PathContext, paths ...string) string { |
| 664 | for _, path := range paths { |
| 665 | if strings.Contains(path, "$") { |
| 666 | reportPathError(ctx, "Path contains invalid character($): %s", path) |
| 667 | return "" |
| 668 | } |
| 669 | } |
| 670 | return validateSafePath(ctx, paths...) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 671 | } |