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 |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 50 | InstallInRecovery() bool |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | var _ ModuleInstallPathContext = ModuleContext(nil) |
| 54 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 55 | // errorfContext is the interface containing the Errorf method matching the |
| 56 | // Errorf method in blueprint.SingletonContext. |
| 57 | type errorfContext interface { |
| 58 | Errorf(format string, args ...interface{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 61 | var _ errorfContext = blueprint.SingletonContext(nil) |
| 62 | |
| 63 | // moduleErrorf is the interface containing the ModuleErrorf method matching |
| 64 | // the ModuleErrorf method in blueprint.ModuleContext. |
| 65 | type moduleErrorf interface { |
| 66 | ModuleErrorf(format string, args ...interface{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 69 | var _ moduleErrorf = blueprint.ModuleContext(nil) |
| 70 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 71 | // reportPathError will register an error with the attached context. It |
| 72 | // attempts ctx.ModuleErrorf for a better error message first, then falls |
| 73 | // back to ctx.Errorf. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 74 | func reportPathError(ctx PathContext, err error) { |
| 75 | reportPathErrorf(ctx, "%s", err.Error()) |
| 76 | } |
| 77 | |
| 78 | // reportPathErrorf 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 reportPathErrorf(ctx PathContext, format string, args ...interface{}) { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 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 |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 103 | // directory, and OutputPath.Join("foo").Rel() would return "foo". |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 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 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 132 | 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] | 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 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 142 | 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] | 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 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 153 | 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] | 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 { |
| 199 | ret := make(Paths, len(paths)) |
| 200 | for i, path := range paths { |
| 201 | ret[i] = PathForSource(ctx, path) |
| 202 | } |
| 203 | return ret |
| 204 | } |
| 205 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 206 | // ExistentPathsForSources returns a list of Paths rooted from SrcDir that are |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 207 | // found in the tree. If any are not found, they are omitted from the list, |
| 208 | // 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] | 209 | func ExistentPathsForSources(ctx PathContext, paths []string) Paths { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 210 | ret := make(Paths, 0, len(paths)) |
| 211 | for _, path := range paths { |
Colin Cross | 32f3898 | 2018-02-22 11:47:25 -0800 | [diff] [blame] | 212 | p := ExistentPathForSource(ctx, path) |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 213 | if p.Valid() { |
| 214 | ret = append(ret, p.Path()) |
| 215 | } |
| 216 | } |
| 217 | return ret |
| 218 | } |
| 219 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 220 | // PathsForModuleSrc returns Paths rooted from the module's local source |
| 221 | // directory |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 222 | func PathsForModuleSrc(ctx ModuleContext, paths []string) Paths { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 223 | ret := make(Paths, len(paths)) |
| 224 | for i, path := range paths { |
| 225 | ret[i] = PathForModuleSrc(ctx, path) |
| 226 | } |
| 227 | return ret |
| 228 | } |
| 229 | |
| 230 | // pathsForModuleSrcFromFullPath returns Paths rooted from the module's local |
| 231 | // source directory, but strip the local source directory from the beginning of |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 232 | // each string. If incDirs is false, strip paths with a trailing '/' from the list. |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 233 | // It intended for use in globs that only list files that exist, so it allows '$' in |
| 234 | // filenames. |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 235 | func pathsForModuleSrcFromFullPath(ctx ModuleContext, paths []string, incDirs bool) Paths { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 236 | prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/" |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 237 | if prefix == "./" { |
| 238 | prefix = "" |
| 239 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 240 | ret := make(Paths, 0, len(paths)) |
| 241 | for _, p := range paths { |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 242 | if !incDirs && strings.HasSuffix(p, "/") { |
| 243 | continue |
| 244 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 245 | path := filepath.Clean(p) |
| 246 | if !strings.HasPrefix(path, prefix) { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 247 | reportPathErrorf(ctx, "Path %q is not in module source directory %q", p, prefix) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 248 | continue |
| 249 | } |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 250 | |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 251 | srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):]) |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 252 | if err != nil { |
| 253 | reportPathError(ctx, err) |
| 254 | continue |
| 255 | } |
| 256 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 257 | srcPath.basePath.rel = srcPath.path |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 258 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 259 | ret = append(ret, srcPath) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 260 | } |
| 261 | return ret |
| 262 | } |
| 263 | |
| 264 | // PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 265 | // local source directory. If input is nil, use the default if it exists. If input is empty, returns nil. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 266 | func PathsWithOptionalDefaultForModuleSrc(ctx ModuleContext, input []string, def string) Paths { |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 267 | if input != nil { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 268 | return PathsForModuleSrc(ctx, input) |
| 269 | } |
| 270 | // Use Glob so that if the default doesn't exist, a dependency is added so that when it |
| 271 | // is created, we're run again. |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 272 | path := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir(), def) |
Colin Cross | 461b445 | 2018-02-23 09:22:42 -0800 | [diff] [blame] | 273 | return ctx.Glob(path, nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | // Strings returns the Paths in string form |
| 277 | func (p Paths) Strings() []string { |
| 278 | if p == nil { |
| 279 | return nil |
| 280 | } |
| 281 | ret := make([]string, len(p)) |
| 282 | for i, path := range p { |
| 283 | ret[i] = path.String() |
| 284 | } |
| 285 | return ret |
| 286 | } |
| 287 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 288 | // FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It |
| 289 | // 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] | 290 | func FirstUniquePaths(list Paths) Paths { |
| 291 | k := 0 |
| 292 | outer: |
| 293 | for i := 0; i < len(list); i++ { |
| 294 | for j := 0; j < k; j++ { |
| 295 | if list[i] == list[j] { |
| 296 | continue outer |
| 297 | } |
| 298 | } |
| 299 | list[k] = list[i] |
| 300 | k++ |
| 301 | } |
| 302 | return list[:k] |
| 303 | } |
| 304 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 305 | // LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It |
| 306 | // modifies the Paths slice contents in place, and returns a subslice of the original slice. |
| 307 | func LastUniquePaths(list Paths) Paths { |
| 308 | totalSkip := 0 |
| 309 | for i := len(list) - 1; i >= totalSkip; i-- { |
| 310 | skip := 0 |
| 311 | for j := i - 1; j >= totalSkip; j-- { |
| 312 | if list[i] == list[j] { |
| 313 | skip++ |
| 314 | } else { |
| 315 | list[j+skip] = list[j] |
| 316 | } |
| 317 | } |
| 318 | totalSkip += skip |
| 319 | } |
| 320 | return list[totalSkip:] |
| 321 | } |
| 322 | |
Colin Cross | a140bb0 | 2018-04-17 10:52:26 -0700 | [diff] [blame] | 323 | // ReversePaths returns a copy of a Paths in reverse order. |
| 324 | func ReversePaths(list Paths) Paths { |
| 325 | if list == nil { |
| 326 | return nil |
| 327 | } |
| 328 | ret := make(Paths, len(list)) |
| 329 | for i := range list { |
| 330 | ret[i] = list[len(list)-1-i] |
| 331 | } |
| 332 | return ret |
| 333 | } |
| 334 | |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 335 | func indexPathList(s Path, list []Path) int { |
| 336 | for i, l := range list { |
| 337 | if l == s { |
| 338 | return i |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return -1 |
| 343 | } |
| 344 | |
| 345 | func inPathList(p Path, list []Path) bool { |
| 346 | return indexPathList(p, list) != -1 |
| 347 | } |
| 348 | |
| 349 | func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) { |
| 350 | for _, l := range list { |
| 351 | if inPathList(l, filter) { |
| 352 | filtered = append(filtered, l) |
| 353 | } else { |
| 354 | remainder = append(remainder, l) |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return |
| 359 | } |
| 360 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 361 | // HasExt returns true of any of the paths have extension ext, otherwise false |
| 362 | func (p Paths) HasExt(ext string) bool { |
| 363 | for _, path := range p { |
| 364 | if path.Ext() == ext { |
| 365 | return true |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return false |
| 370 | } |
| 371 | |
| 372 | // FilterByExt returns the subset of the paths that have extension ext |
| 373 | func (p Paths) FilterByExt(ext string) Paths { |
| 374 | ret := make(Paths, 0, len(p)) |
| 375 | for _, path := range p { |
| 376 | if path.Ext() == ext { |
| 377 | ret = append(ret, path) |
| 378 | } |
| 379 | } |
| 380 | return ret |
| 381 | } |
| 382 | |
| 383 | // FilterOutByExt returns the subset of the paths that do not have extension ext |
| 384 | func (p Paths) FilterOutByExt(ext string) Paths { |
| 385 | ret := make(Paths, 0, len(p)) |
| 386 | for _, path := range p { |
| 387 | if path.Ext() != ext { |
| 388 | ret = append(ret, path) |
| 389 | } |
| 390 | } |
| 391 | return ret |
| 392 | } |
| 393 | |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 394 | // DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory |
| 395 | // (including subdirectories) are in a contiguous subslice of the list, and can be found in |
| 396 | // O(log(N)) time using a binary search on the directory prefix. |
| 397 | type DirectorySortedPaths Paths |
| 398 | |
| 399 | func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths { |
| 400 | ret := append(DirectorySortedPaths(nil), paths...) |
| 401 | sort.Slice(ret, func(i, j int) bool { |
| 402 | return ret[i].String() < ret[j].String() |
| 403 | }) |
| 404 | return ret |
| 405 | } |
| 406 | |
| 407 | // PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries |
| 408 | // that are in the specified directory and its subdirectories. |
| 409 | func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths { |
| 410 | prefix := filepath.Clean(dir) + "/" |
| 411 | start := sort.Search(len(p), func(i int) bool { |
| 412 | return prefix < p[i].String() |
| 413 | }) |
| 414 | |
| 415 | ret := p[start:] |
| 416 | |
| 417 | end := sort.Search(len(ret), func(i int) bool { |
| 418 | return !strings.HasPrefix(ret[i].String(), prefix) |
| 419 | }) |
| 420 | |
| 421 | ret = ret[:end] |
| 422 | |
| 423 | return Paths(ret) |
| 424 | } |
| 425 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 426 | // WritablePaths is a slice of WritablePaths, used for multiple outputs. |
| 427 | type WritablePaths []WritablePath |
| 428 | |
| 429 | // Strings returns the string forms of the writable paths. |
| 430 | func (p WritablePaths) Strings() []string { |
| 431 | if p == nil { |
| 432 | return nil |
| 433 | } |
| 434 | ret := make([]string, len(p)) |
| 435 | for i, path := range p { |
| 436 | ret[i] = path.String() |
| 437 | } |
| 438 | return ret |
| 439 | } |
| 440 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 441 | // Paths returns the WritablePaths as a Paths |
| 442 | func (p WritablePaths) Paths() Paths { |
| 443 | if p == nil { |
| 444 | return nil |
| 445 | } |
| 446 | ret := make(Paths, len(p)) |
| 447 | for i, path := range p { |
| 448 | ret[i] = path |
| 449 | } |
| 450 | return ret |
| 451 | } |
| 452 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 453 | type basePath struct { |
| 454 | path string |
| 455 | config Config |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 456 | rel string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | func (p basePath) Ext() string { |
| 460 | return filepath.Ext(p.path) |
| 461 | } |
| 462 | |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 463 | func (p basePath) Base() string { |
| 464 | return filepath.Base(p.path) |
| 465 | } |
| 466 | |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 467 | func (p basePath) Rel() string { |
| 468 | if p.rel != "" { |
| 469 | return p.rel |
| 470 | } |
| 471 | return p.path |
| 472 | } |
| 473 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 474 | func (p basePath) String() string { |
| 475 | return p.path |
| 476 | } |
| 477 | |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 478 | func (p basePath) withRel(rel string) basePath { |
| 479 | p.path = filepath.Join(p.path, rel) |
| 480 | p.rel = rel |
| 481 | return p |
| 482 | } |
| 483 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 484 | // SourcePath is a Path representing a file path rooted from SrcDir |
| 485 | type SourcePath struct { |
| 486 | basePath |
| 487 | } |
| 488 | |
| 489 | var _ Path = SourcePath{} |
| 490 | |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 491 | func (p SourcePath) withRel(rel string) SourcePath { |
| 492 | p.basePath = p.basePath.withRel(rel) |
| 493 | return p |
| 494 | } |
| 495 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 496 | // safePathForSource is for paths that we expect are safe -- only for use by go |
| 497 | // code that is embedding ninja variables in paths |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 498 | func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { |
| 499 | p, err := validateSafePath(pathComponents...) |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 500 | ret := SourcePath{basePath{p, ctx.Config(), ""}} |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 501 | if err != nil { |
| 502 | return ret, err |
| 503 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 504 | |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 505 | // absolute path already checked by validateSafePath |
| 506 | if strings.HasPrefix(ret.String(), ctx.Config().buildDir) { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 507 | return ret, fmt.Errorf("source path %q is in output", ret.String()) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 510 | return ret, err |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 513 | // pathForSource creates a SourcePath from pathComponents, but does not check that it exists. |
| 514 | func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 515 | p, err := validatePath(pathComponents...) |
| 516 | ret := SourcePath{basePath{p, ctx.Config(), ""}} |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 517 | if err != nil { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 518 | return ret, err |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 521 | // absolute path already checked by validatePath |
| 522 | if strings.HasPrefix(ret.String(), ctx.Config().buildDir) { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 523 | return ret, fmt.Errorf("source path %q is in output", ret.String()) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 526 | return ret, nil |
| 527 | } |
| 528 | |
| 529 | // existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the |
| 530 | // path does not exist. |
| 531 | func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) { |
| 532 | var files []string |
| 533 | |
| 534 | if gctx, ok := ctx.(PathGlobContext); ok { |
| 535 | // Use glob to produce proper dependencies, even though we only want |
| 536 | // a single file. |
| 537 | files, err = gctx.GlobWithDeps(path.String(), nil) |
| 538 | } else { |
| 539 | var deps []string |
| 540 | // We cannot add build statements in this context, so we fall back to |
| 541 | // AddNinjaFileDeps |
Colin Cross | 3f4d116 | 2018-09-21 15:11:48 -0700 | [diff] [blame] | 542 | files, deps, err = pathtools.Glob(path.String(), nil, pathtools.FollowSymlinks) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 543 | ctx.AddNinjaFileDeps(deps...) |
| 544 | } |
| 545 | |
| 546 | if err != nil { |
| 547 | return false, fmt.Errorf("glob: %s", err.Error()) |
| 548 | } |
| 549 | |
| 550 | return len(files) > 0, nil |
| 551 | } |
| 552 | |
| 553 | // PathForSource joins the provided path components and validates that the result |
| 554 | // neither escapes the source dir nor is in the out dir. |
| 555 | // On error, it will return a usable, but invalid SourcePath, and report a ModuleError. |
| 556 | func PathForSource(ctx PathContext, pathComponents ...string) SourcePath { |
| 557 | path, err := pathForSource(ctx, pathComponents...) |
| 558 | if err != nil { |
| 559 | reportPathError(ctx, err) |
| 560 | } |
| 561 | |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 562 | if pathtools.IsGlob(path.String()) { |
| 563 | reportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) |
| 564 | } |
| 565 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 566 | if modCtx, ok := ctx.(ModuleContext); ok && ctx.Config().AllowMissingDependencies() { |
| 567 | exists, err := existsWithDependencies(ctx, path) |
| 568 | if err != nil { |
| 569 | reportPathError(ctx, err) |
| 570 | } |
| 571 | if !exists { |
| 572 | modCtx.AddMissingDependencies([]string{path.String()}) |
| 573 | } |
| 574 | } else if exists, _, err := ctx.Fs().Exists(path.String()); err != nil { |
| 575 | reportPathErrorf(ctx, "%s: %s", path, err.Error()) |
| 576 | } else if !exists { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 577 | reportPathErrorf(ctx, "source path %q does not exist", path) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 578 | } |
| 579 | return path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 582 | // ExistentPathForSource returns an OptionalPath with the SourcePath if the |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 583 | // path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added |
| 584 | // 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] | 585 | func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 586 | path, err := pathForSource(ctx, pathComponents...) |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 587 | if err != nil { |
| 588 | reportPathError(ctx, err) |
| 589 | return OptionalPath{} |
| 590 | } |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 591 | |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 592 | if pathtools.IsGlob(path.String()) { |
| 593 | reportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) |
| 594 | return OptionalPath{} |
| 595 | } |
| 596 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 597 | exists, err := existsWithDependencies(ctx, path) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 598 | if err != nil { |
| 599 | reportPathError(ctx, err) |
| 600 | return OptionalPath{} |
| 601 | } |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 602 | if !exists { |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 603 | return OptionalPath{} |
| 604 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 605 | return OptionalPathForPath(path) |
| 606 | } |
| 607 | |
| 608 | func (p SourcePath) String() string { |
| 609 | return filepath.Join(p.config.srcDir, p.path) |
| 610 | } |
| 611 | |
| 612 | // Join creates a new SourcePath with paths... joined with the current path. The |
| 613 | // provided paths... may not use '..' to escape from the current path. |
| 614 | func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 615 | path, err := validatePath(paths...) |
| 616 | if err != nil { |
| 617 | reportPathError(ctx, err) |
| 618 | } |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 619 | return p.withRel(path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 622 | // join is like Join but does less path validation. |
| 623 | func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath { |
| 624 | path, err := validateSafePath(paths...) |
| 625 | if err != nil { |
| 626 | reportPathError(ctx, err) |
| 627 | } |
| 628 | return p.withRel(path) |
| 629 | } |
| 630 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 631 | // OverlayPath returns the overlay for `path' if it exists. This assumes that the |
| 632 | // SourcePath is the path to a resource overlay directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 633 | func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 634 | var relDir string |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 635 | if srcPath, ok := path.(SourcePath); ok { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 636 | relDir = srcPath.path |
| 637 | } else { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 638 | 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] | 639 | return OptionalPath{} |
| 640 | } |
| 641 | dir := filepath.Join(p.config.srcDir, p.path, relDir) |
| 642 | // 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] | 643 | if pathtools.IsGlob(dir) { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 644 | reportPathErrorf(ctx, "Path may not contain a glob: %s", dir) |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 645 | } |
Colin Cross | 461b445 | 2018-02-23 09:22:42 -0800 | [diff] [blame] | 646 | paths, err := ctx.GlobWithDeps(dir, nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 647 | if err != nil { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 648 | reportPathErrorf(ctx, "glob: %s", err.Error()) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 649 | return OptionalPath{} |
| 650 | } |
| 651 | if len(paths) == 0 { |
| 652 | return OptionalPath{} |
| 653 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 654 | relPath := Rel(ctx, p.config.srcDir, paths[0]) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 655 | return OptionalPathForPath(PathForSource(ctx, relPath)) |
| 656 | } |
| 657 | |
| 658 | // OutputPath is a Path representing a file path rooted from the build directory |
| 659 | type OutputPath struct { |
| 660 | basePath |
| 661 | } |
| 662 | |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 663 | func (p OutputPath) withRel(rel string) OutputPath { |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 664 | p.basePath = p.basePath.withRel(rel) |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 665 | return p |
| 666 | } |
| 667 | |
Colin Cross | 3063b78 | 2018-08-15 11:19:12 -0700 | [diff] [blame] | 668 | func (p OutputPath) WithoutRel() OutputPath { |
| 669 | p.basePath.rel = filepath.Base(p.basePath.path) |
| 670 | return p |
| 671 | } |
| 672 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 673 | var _ Path = OutputPath{} |
| 674 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 675 | // PathForOutput joins the provided paths and returns an OutputPath that is |
| 676 | // validated to not escape the build dir. |
| 677 | // On error, it will return a usable, but invalid OutputPath, and report a ModuleError. |
| 678 | func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 679 | path, err := validatePath(pathComponents...) |
| 680 | if err != nil { |
| 681 | reportPathError(ctx, err) |
| 682 | } |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 683 | return OutputPath{basePath{path, ctx.Config(), ""}} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 686 | // PathsForOutput returns Paths rooted from buildDir |
| 687 | func PathsForOutput(ctx PathContext, paths []string) WritablePaths { |
| 688 | ret := make(WritablePaths, len(paths)) |
| 689 | for i, path := range paths { |
| 690 | ret[i] = PathForOutput(ctx, path) |
| 691 | } |
| 692 | return ret |
| 693 | } |
| 694 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 695 | func (p OutputPath) writablePath() {} |
| 696 | |
| 697 | func (p OutputPath) String() string { |
| 698 | return filepath.Join(p.config.buildDir, p.path) |
| 699 | } |
| 700 | |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 701 | func (p OutputPath) RelPathString() string { |
| 702 | return p.path |
| 703 | } |
| 704 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 705 | // Join creates a new OutputPath with paths... joined with the current path. The |
| 706 | // provided paths... may not use '..' to escape from the current path. |
| 707 | func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 708 | path, err := validatePath(paths...) |
| 709 | if err != nil { |
| 710 | reportPathError(ctx, err) |
| 711 | } |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 712 | return p.withRel(path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 715 | // ReplaceExtension creates a new OutputPath with the extension replaced with ext. |
| 716 | func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { |
| 717 | if strings.Contains(ext, "/") { |
| 718 | reportPathErrorf(ctx, "extension %q cannot contain /", ext) |
| 719 | } |
| 720 | ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext)) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 721 | ret.rel = pathtools.ReplaceExtension(p.rel, ext) |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 722 | return ret |
| 723 | } |
| 724 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 725 | // InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths. |
| 726 | func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath { |
| 727 | path, err := validatePath(paths...) |
| 728 | if err != nil { |
| 729 | reportPathError(ctx, err) |
| 730 | } |
| 731 | |
| 732 | ret := PathForOutput(ctx, filepath.Dir(p.path), path) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 733 | ret.rel = filepath.Join(filepath.Dir(p.rel), path) |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 734 | return ret |
| 735 | } |
| 736 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 737 | // PathForIntermediates returns an OutputPath representing the top-level |
| 738 | // intermediates directory. |
| 739 | func PathForIntermediates(ctx PathContext, paths ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 740 | path, err := validatePath(paths...) |
| 741 | if err != nil { |
| 742 | reportPathError(ctx, err) |
| 743 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 744 | return PathForOutput(ctx, ".intermediates", path) |
| 745 | } |
| 746 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 747 | var _ genPathProvider = SourcePath{} |
| 748 | var _ objPathProvider = SourcePath{} |
| 749 | var _ resPathProvider = SourcePath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 750 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 751 | // PathForModuleSrc returns a Path representing the paths... under the |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 752 | // module's local source directory. |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 753 | func PathForModuleSrc(ctx ModuleContext, paths ...string) Path { |
| 754 | path := pathForModuleSrc(ctx, paths...) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 755 | |
| 756 | if exists, _, err := ctx.Fs().Exists(path.String()); err != nil { |
| 757 | reportPathErrorf(ctx, "%s: %s", path, err.Error()) |
| 758 | } else if !exists { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 759 | reportPathErrorf(ctx, "module source path %q does not exist", path) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 760 | } |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 761 | return path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 762 | } |
| 763 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 764 | func pathForModuleSrc(ctx ModuleContext, paths ...string) SourcePath { |
| 765 | p, err := validatePath(paths...) |
| 766 | if err != nil { |
| 767 | reportPathError(ctx, err) |
| 768 | } |
| 769 | |
| 770 | path, err := pathForSource(ctx, ctx.ModuleDir(), p) |
| 771 | if err != nil { |
| 772 | reportPathError(ctx, err) |
| 773 | } |
| 774 | |
| 775 | path.basePath.rel = p |
| 776 | |
| 777 | return path |
| 778 | } |
| 779 | |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 780 | // PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path |
| 781 | // will return the path relative to subDir in the module's source directory. If any input paths are not located |
| 782 | // inside subDir then a path error will be reported. |
| 783 | func PathsWithModuleSrcSubDir(ctx ModuleContext, paths Paths, subDir string) Paths { |
| 784 | paths = append(Paths(nil), paths...) |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 785 | subDirFullPath := pathForModuleSrc(ctx, subDir) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 786 | for i, path := range paths { |
| 787 | rel := Rel(ctx, subDirFullPath.String(), path.String()) |
| 788 | paths[i] = subDirFullPath.join(ctx, rel) |
| 789 | } |
| 790 | return paths |
| 791 | } |
| 792 | |
| 793 | // PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the |
| 794 | // module's source directory. If the input path is not located inside subDir then a path error will be reported. |
| 795 | func PathWithModuleSrcSubDir(ctx ModuleContext, path Path, subDir string) Path { |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 796 | subDirFullPath := pathForModuleSrc(ctx, subDir) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 797 | rel := Rel(ctx, subDirFullPath.String(), path.String()) |
| 798 | return subDirFullPath.Join(ctx, rel) |
| 799 | } |
| 800 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 801 | // OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a |
| 802 | // valid path if p is non-nil. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 803 | func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 804 | if p == nil { |
| 805 | return OptionalPath{} |
| 806 | } |
| 807 | return OptionalPathForPath(PathForModuleSrc(ctx, *p)) |
| 808 | } |
| 809 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 810 | func (p SourcePath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 811 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 814 | func (p SourcePath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 815 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 818 | func (p SourcePath) resPathWithName(ctx ModuleContext, name string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 819 | // TODO: Use full directory if the new ctx is not the current ctx? |
| 820 | return PathForModuleRes(ctx, p.path, name) |
| 821 | } |
| 822 | |
| 823 | // ModuleOutPath is a Path representing a module's output directory. |
| 824 | type ModuleOutPath struct { |
| 825 | OutputPath |
| 826 | } |
| 827 | |
| 828 | var _ Path = ModuleOutPath{} |
| 829 | |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 830 | func pathForModule(ctx ModuleContext) OutputPath { |
| 831 | return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir()) |
| 832 | } |
| 833 | |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 834 | // PathForVndkRefAbiDump returns an OptionalPath representing the path of the |
| 835 | // reference abi dump for the given module. This is not guaranteed to be valid. |
| 836 | func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string, |
| 837 | isLlndk, isGzip bool) OptionalPath { |
| 838 | |
Jayant Chowdhary | ac066c6 | 2018-02-20 10:53:31 -0800 | [diff] [blame] | 839 | arches := ctx.DeviceConfig().Arches() |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 840 | if len(arches) == 0 { |
| 841 | panic("device build with no primary arch") |
| 842 | } |
Jayant Chowdhary | ac066c6 | 2018-02-20 10:53:31 -0800 | [diff] [blame] | 843 | currentArch := ctx.Arch() |
| 844 | archNameAndVariant := currentArch.ArchType.String() |
| 845 | if currentArch.ArchVariant != "" { |
| 846 | archNameAndVariant += "_" + currentArch.ArchVariant |
| 847 | } |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 848 | |
| 849 | var dirName string |
| 850 | if isLlndk { |
| 851 | dirName = "ndk" |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 852 | } else { |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 853 | dirName = "vndk" |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 854 | } |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 855 | |
Jayant Chowdhary | 34ce67d | 2018-03-08 11:00:50 -0800 | [diff] [blame] | 856 | binderBitness := ctx.DeviceConfig().BinderBitness() |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 857 | |
| 858 | var ext string |
| 859 | if isGzip { |
| 860 | ext = ".lsdump.gz" |
| 861 | } else { |
| 862 | ext = ".lsdump" |
| 863 | } |
| 864 | |
| 865 | return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName, |
| 866 | version, binderBitness, archNameAndVariant, "source-based", |
| 867 | fileName+ext) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 868 | } |
| 869 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 870 | // PathForModuleOut returns a Path representing the paths... under the module's |
| 871 | // output directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 872 | func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 873 | p, err := validatePath(paths...) |
| 874 | if err != nil { |
| 875 | reportPathError(ctx, err) |
| 876 | } |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 877 | return ModuleOutPath{ |
| 878 | OutputPath: pathForModule(ctx).withRel(p), |
| 879 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | // ModuleGenPath is a Path representing the 'gen' directory in a module's output |
| 883 | // directory. Mainly used for generated sources. |
| 884 | type ModuleGenPath struct { |
| 885 | ModuleOutPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | var _ Path = ModuleGenPath{} |
| 889 | var _ genPathProvider = ModuleGenPath{} |
| 890 | var _ objPathProvider = ModuleGenPath{} |
| 891 | |
| 892 | // PathForModuleGen returns a Path representing the paths... under the module's |
| 893 | // `gen' directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 894 | func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 895 | p, err := validatePath(paths...) |
| 896 | if err != nil { |
| 897 | reportPathError(ctx, err) |
| 898 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 899 | return ModuleGenPath{ |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 900 | ModuleOutPath: ModuleOutPath{ |
| 901 | OutputPath: pathForModule(ctx).withRel("gen").withRel(p), |
| 902 | }, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 906 | func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 907 | // TODO: make a different path for local vs remote generated files? |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 908 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 911 | func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 912 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 913 | } |
| 914 | |
| 915 | // ModuleObjPath is a Path representing the 'obj' directory in a module's output |
| 916 | // directory. Used for compiled objects. |
| 917 | type ModuleObjPath struct { |
| 918 | ModuleOutPath |
| 919 | } |
| 920 | |
| 921 | var _ Path = ModuleObjPath{} |
| 922 | |
| 923 | // PathForModuleObj returns a Path representing the paths... under the module's |
| 924 | // 'obj' directory. |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 925 | func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 926 | p, err := validatePath(pathComponents...) |
| 927 | if err != nil { |
| 928 | reportPathError(ctx, err) |
| 929 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 930 | return ModuleObjPath{PathForModuleOut(ctx, "obj", p)} |
| 931 | } |
| 932 | |
| 933 | // ModuleResPath is a a Path representing the 'res' directory in a module's |
| 934 | // output directory. |
| 935 | type ModuleResPath struct { |
| 936 | ModuleOutPath |
| 937 | } |
| 938 | |
| 939 | var _ Path = ModuleResPath{} |
| 940 | |
| 941 | // PathForModuleRes returns a Path representing the paths... under the module's |
| 942 | // 'res' directory. |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 943 | func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 944 | p, err := validatePath(pathComponents...) |
| 945 | if err != nil { |
| 946 | reportPathError(ctx, err) |
| 947 | } |
| 948 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 949 | return ModuleResPath{PathForModuleOut(ctx, "res", p)} |
| 950 | } |
| 951 | |
| 952 | // PathForModuleInstall returns a Path representing the install path for the |
| 953 | // module appended with paths... |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 954 | func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) OutputPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 955 | var outPaths []string |
| 956 | if ctx.Device() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 957 | partition := modulePartition(ctx) |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 958 | outPaths = []string{"target", "product", ctx.Config().DeviceName(), partition} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 959 | } else { |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 960 | switch ctx.Os() { |
| 961 | case Linux: |
| 962 | outPaths = []string{"host", "linux-x86"} |
| 963 | case LinuxBionic: |
| 964 | // TODO: should this be a separate top level, or shared with linux-x86? |
| 965 | outPaths = []string{"host", "linux_bionic-x86"} |
| 966 | default: |
| 967 | outPaths = []string{"host", ctx.Os().String() + "-x86"} |
| 968 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 969 | } |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame] | 970 | if ctx.Debug() { |
| 971 | outPaths = append([]string{"debug"}, outPaths...) |
| 972 | } |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 973 | outPaths = append(outPaths, pathComponents...) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 974 | return PathForOutput(ctx, outPaths...) |
| 975 | } |
| 976 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 977 | func InstallPathToOnDevicePath(ctx PathContext, path OutputPath) string { |
| 978 | rel := Rel(ctx, PathForOutput(ctx, "target", "product", ctx.Config().DeviceName()).String(), path.String()) |
| 979 | |
| 980 | return "/" + rel |
| 981 | } |
| 982 | |
| 983 | func modulePartition(ctx ModuleInstallPathContext) string { |
| 984 | var partition string |
| 985 | if ctx.InstallInData() { |
| 986 | partition = "data" |
| 987 | } else if ctx.InstallInRecovery() { |
| 988 | // the layout of recovery partion is the same as that of system partition |
| 989 | partition = "recovery/root/system" |
| 990 | } else if ctx.SocSpecific() { |
| 991 | partition = ctx.DeviceConfig().VendorPath() |
| 992 | } else if ctx.DeviceSpecific() { |
| 993 | partition = ctx.DeviceConfig().OdmPath() |
| 994 | } else if ctx.ProductSpecific() { |
| 995 | partition = ctx.DeviceConfig().ProductPath() |
| 996 | } else if ctx.ProductServicesSpecific() { |
| 997 | partition = ctx.DeviceConfig().ProductServicesPath() |
| 998 | } else { |
| 999 | partition = "system" |
| 1000 | } |
| 1001 | if ctx.InstallInSanitizerDir() { |
| 1002 | partition = "data/asan/" + partition |
| 1003 | } |
| 1004 | return partition |
| 1005 | } |
| 1006 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1007 | // validateSafePath validates a path that we trust (may contain ninja variables). |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1008 | // Ensures that each path component does not attempt to leave its component. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1009 | func validateSafePath(pathComponents ...string) (string, error) { |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1010 | for _, path := range pathComponents { |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1011 | path := filepath.Clean(path) |
| 1012 | if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1013 | return "", fmt.Errorf("Path is outside directory: %s", path) |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1014 | } |
| 1015 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1016 | // TODO: filepath.Join isn't necessarily correct with embedded ninja |
| 1017 | // variables. '..' may remove the entire ninja variable, even if it |
| 1018 | // will be expanded to multiple nested directories. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1019 | return filepath.Join(pathComponents...), nil |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1022 | // validatePath validates that a path does not include ninja variables, and that |
| 1023 | // each path component does not attempt to leave its component. Returns a joined |
| 1024 | // version of each path component. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1025 | func validatePath(pathComponents ...string) (string, error) { |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1026 | for _, path := range pathComponents { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1027 | if strings.Contains(path, "$") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1028 | return "", fmt.Errorf("Path contains invalid character($): %s", path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1029 | } |
| 1030 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1031 | return validateSafePath(pathComponents...) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 1032 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1033 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1034 | func PathForPhony(ctx PathContext, phony string) WritablePath { |
| 1035 | if strings.ContainsAny(phony, "$/") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1036 | reportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1037 | } |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 1038 | return PhonyPath{basePath{phony, ctx.Config(), ""}} |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1039 | } |
| 1040 | |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 1041 | type PhonyPath struct { |
| 1042 | basePath |
| 1043 | } |
| 1044 | |
| 1045 | func (p PhonyPath) writablePath() {} |
| 1046 | |
| 1047 | var _ Path = PhonyPath{} |
| 1048 | var _ WritablePath = PhonyPath{} |
| 1049 | |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1050 | type testPath struct { |
| 1051 | basePath |
| 1052 | } |
| 1053 | |
| 1054 | func (p testPath) String() string { |
| 1055 | return p.path |
| 1056 | } |
| 1057 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1058 | type testWritablePath struct { |
| 1059 | testPath |
| 1060 | } |
| 1061 | |
| 1062 | func (p testPath) writablePath() {} |
| 1063 | |
| 1064 | // PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from |
| 1065 | // within tests. |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1066 | func PathForTesting(paths ...string) Path { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1067 | p, err := validateSafePath(paths...) |
| 1068 | if err != nil { |
| 1069 | panic(err) |
| 1070 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1071 | return testPath{basePath{path: p, rel: p}} |
| 1072 | } |
| 1073 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1074 | // PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests. |
| 1075 | func PathsForTesting(strs ...string) Paths { |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1076 | p := make(Paths, len(strs)) |
| 1077 | for i, s := range strs { |
| 1078 | p[i] = PathForTesting(s) |
| 1079 | } |
| 1080 | |
| 1081 | return p |
| 1082 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1083 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1084 | // WritablePathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be |
| 1085 | // used from within tests. |
| 1086 | func WritablePathForTesting(paths ...string) WritablePath { |
| 1087 | p, err := validateSafePath(paths...) |
| 1088 | if err != nil { |
| 1089 | panic(err) |
| 1090 | } |
| 1091 | return testWritablePath{testPath{basePath{path: p, rel: p}}} |
| 1092 | } |
| 1093 | |
| 1094 | // WritablePathsForTesting returns a Path constructed from each element in strs. It should only be used from within |
| 1095 | // tests. |
| 1096 | func WritablePathsForTesting(strs ...string) WritablePaths { |
| 1097 | p := make(WritablePaths, len(strs)) |
| 1098 | for i, s := range strs { |
| 1099 | p[i] = WritablePathForTesting(s) |
| 1100 | } |
| 1101 | |
| 1102 | return p |
| 1103 | } |
| 1104 | |
| 1105 | type testPathContext struct { |
| 1106 | config Config |
| 1107 | fs pathtools.FileSystem |
| 1108 | } |
| 1109 | |
| 1110 | func (x *testPathContext) Fs() pathtools.FileSystem { return x.fs } |
| 1111 | func (x *testPathContext) Config() Config { return x.config } |
| 1112 | func (x *testPathContext) AddNinjaFileDeps(...string) {} |
| 1113 | |
| 1114 | // PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with |
| 1115 | // PathForOutput. |
| 1116 | func PathContextForTesting(config Config, fs map[string][]byte) PathContext { |
| 1117 | return &testPathContext{ |
| 1118 | config: config, |
| 1119 | fs: pathtools.MockFs(fs), |
| 1120 | } |
| 1121 | } |
| 1122 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1123 | // Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if |
| 1124 | // targetPath is not inside basePath. |
| 1125 | func Rel(ctx PathContext, basePath string, targetPath string) string { |
| 1126 | rel, isRel := MaybeRel(ctx, basePath, targetPath) |
| 1127 | if !isRel { |
| 1128 | reportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath) |
| 1129 | return "" |
| 1130 | } |
| 1131 | return rel |
| 1132 | } |
| 1133 | |
| 1134 | // MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if |
| 1135 | // targetPath is not inside basePath. |
| 1136 | func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) { |
| 1137 | // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first. |
| 1138 | if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) { |
| 1139 | return "", false |
| 1140 | } |
| 1141 | rel, err := filepath.Rel(basePath, targetPath) |
| 1142 | if err != nil { |
| 1143 | reportPathError(ctx, err) |
| 1144 | return "", false |
| 1145 | } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") { |
| 1146 | return "", false |
| 1147 | } |
| 1148 | return rel, true |
| 1149 | } |