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