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