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