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 | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 19 | "io/ioutil" |
| 20 | "os" |
Colin Cross | 6a745c6 | 2015-06-16 16:38:10 -0700 | [diff] [blame] | 21 | "path/filepath" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 22 | "reflect" |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 23 | "sort" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 24 | "strings" |
| 25 | |
| 26 | "github.com/google/blueprint" |
| 27 | "github.com/google/blueprint/pathtools" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 30 | var absSrcDir string |
| 31 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 32 | // PathContext is the subset of a (Module|Singleton)Context required by the |
| 33 | // Path methods. |
| 34 | type PathContext interface { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 35 | Config() Config |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 36 | AddNinjaFileDeps(deps ...string) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 39 | type PathGlobContext interface { |
| 40 | GlobWithDeps(globPattern string, excludes []string) ([]string, error) |
| 41 | } |
| 42 | |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 43 | var _ PathContext = SingletonContext(nil) |
| 44 | var _ PathContext = ModuleContext(nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 45 | |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 46 | type ModuleInstallPathContext interface { |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 47 | BaseModuleContext |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 48 | |
| 49 | InstallInData() bool |
Jaewoong Jung | 0949f31 | 2019-09-11 10:25:18 -0700 | [diff] [blame] | 50 | InstallInTestcases() bool |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 51 | InstallInSanitizerDir() bool |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 52 | InstallInRamdisk() bool |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 53 | InstallInRecovery() bool |
Colin Cross | 90ba5f4 | 2019-10-02 11:10:58 -0700 | [diff] [blame] | 54 | InstallInRoot() bool |
Colin Cross | 607d858 | 2019-07-29 16:44:46 -0700 | [diff] [blame] | 55 | InstallBypassMake() bool |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | var _ ModuleInstallPathContext = ModuleContext(nil) |
| 59 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 60 | // errorfContext is the interface containing the Errorf method matching the |
| 61 | // Errorf method in blueprint.SingletonContext. |
| 62 | type errorfContext interface { |
| 63 | Errorf(format string, args ...interface{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 66 | var _ errorfContext = blueprint.SingletonContext(nil) |
| 67 | |
| 68 | // moduleErrorf is the interface containing the ModuleErrorf method matching |
| 69 | // the ModuleErrorf method in blueprint.ModuleContext. |
| 70 | type moduleErrorf interface { |
| 71 | ModuleErrorf(format string, args ...interface{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 74 | var _ moduleErrorf = blueprint.ModuleContext(nil) |
| 75 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 76 | // reportPathError will register an error with the attached context. It |
| 77 | // attempts ctx.ModuleErrorf for a better error message first, then falls |
| 78 | // back to ctx.Errorf. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 79 | func reportPathError(ctx PathContext, err error) { |
| 80 | reportPathErrorf(ctx, "%s", err.Error()) |
| 81 | } |
| 82 | |
| 83 | // reportPathErrorf will register an error with the attached context. It |
| 84 | // attempts ctx.ModuleErrorf for a better error message first, then falls |
| 85 | // back to ctx.Errorf. |
| 86 | func reportPathErrorf(ctx PathContext, format string, args ...interface{}) { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 87 | if mctx, ok := ctx.(moduleErrorf); ok { |
| 88 | mctx.ModuleErrorf(format, args...) |
| 89 | } else if ectx, ok := ctx.(errorfContext); ok { |
| 90 | ectx.Errorf(format, args...) |
| 91 | } else { |
| 92 | panic(fmt.Sprintf(format, args...)) |
Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Colin Cross | 5e70805 | 2019-08-06 13:59:50 -0700 | [diff] [blame] | 96 | func pathContextName(ctx PathContext, module blueprint.Module) string { |
| 97 | if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok { |
| 98 | return x.ModuleName(module) |
| 99 | } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok { |
| 100 | return x.OtherModuleName(module) |
| 101 | } |
| 102 | return "unknown" |
| 103 | } |
| 104 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 105 | type Path interface { |
| 106 | // Returns the path in string form |
| 107 | String() string |
| 108 | |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 109 | // Ext returns the extension of the last element of the path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 110 | Ext() string |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 111 | |
| 112 | // Base returns the last element of the path |
| 113 | Base() string |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 114 | |
| 115 | // Rel returns the portion of the path relative to the directory it was created from. For |
| 116 | // 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] | 117 | // directory, and OutputPath.Join("foo").Rel() would return "foo". |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 118 | Rel() string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // WritablePath is a type of path that can be used as an output for build rules. |
| 122 | type WritablePath interface { |
| 123 | Path |
| 124 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 125 | // return the path to the build directory. |
| 126 | buildDir() string |
| 127 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 128 | // the writablePath method doesn't directly do anything, |
| 129 | // 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] | 130 | writablePath() |
| 131 | } |
| 132 | |
| 133 | type genPathProvider interface { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 134 | genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 135 | } |
| 136 | type objPathProvider interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 137 | objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 138 | } |
| 139 | type resPathProvider interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 140 | resPathWithName(ctx ModuleContext, name string) ModuleResPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // GenPathWithExt derives a new file path in ctx's generated sources directory |
| 144 | // from the current path, but with the new extension. |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 145 | func GenPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 146 | if path, ok := p.(genPathProvider); ok { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 147 | return path.genPathWithExt(ctx, subdir, ext) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 148 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 149 | 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] | 150 | return PathForModuleGen(ctx) |
| 151 | } |
| 152 | |
| 153 | // ObjPathWithExt derives a new file path in ctx's object directory from the |
| 154 | // current path, but with the new extension. |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 155 | func ObjPathWithExt(ctx ModuleContext, subdir string, p Path, ext string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 156 | if path, ok := p.(objPathProvider); ok { |
| 157 | return path.objPathWithExt(ctx, subdir, ext) |
| 158 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 159 | 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] | 160 | return PathForModuleObj(ctx) |
| 161 | } |
| 162 | |
| 163 | // ResPathWithName derives a new path in ctx's output resource directory, using |
| 164 | // the current path to create the directory name, and the `name` argument for |
| 165 | // the filename. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 166 | func ResPathWithName(ctx ModuleContext, p Path, name string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 167 | if path, ok := p.(resPathProvider); ok { |
| 168 | return path.resPathWithName(ctx, name) |
| 169 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 170 | 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] | 171 | return PathForModuleRes(ctx) |
| 172 | } |
| 173 | |
| 174 | // OptionalPath is a container that may or may not contain a valid Path. |
| 175 | type OptionalPath struct { |
| 176 | valid bool |
| 177 | path Path |
| 178 | } |
| 179 | |
| 180 | // OptionalPathForPath returns an OptionalPath containing the path. |
| 181 | func OptionalPathForPath(path Path) OptionalPath { |
| 182 | if path == nil { |
| 183 | return OptionalPath{} |
| 184 | } |
| 185 | return OptionalPath{valid: true, path: path} |
| 186 | } |
| 187 | |
| 188 | // Valid returns whether there is a valid path |
| 189 | func (p OptionalPath) Valid() bool { |
| 190 | return p.valid |
| 191 | } |
| 192 | |
| 193 | // Path returns the Path embedded in this OptionalPath. You must be sure that |
| 194 | // there is a valid path, since this method will panic if there is not. |
| 195 | func (p OptionalPath) Path() Path { |
| 196 | if !p.valid { |
| 197 | panic("Requesting an invalid path") |
| 198 | } |
| 199 | return p.path |
| 200 | } |
| 201 | |
| 202 | // String returns the string version of the Path, or "" if it isn't valid. |
| 203 | func (p OptionalPath) String() string { |
| 204 | if p.valid { |
| 205 | return p.path.String() |
| 206 | } else { |
| 207 | return "" |
Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 210 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 211 | // Paths is a slice of Path objects, with helpers to operate on the collection. |
| 212 | type Paths []Path |
| 213 | |
| 214 | // PathsForSource returns Paths rooted from SrcDir |
| 215 | func PathsForSource(ctx PathContext, paths []string) Paths { |
| 216 | ret := make(Paths, len(paths)) |
| 217 | for i, path := range paths { |
| 218 | ret[i] = PathForSource(ctx, path) |
| 219 | } |
| 220 | return ret |
| 221 | } |
| 222 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 223 | // ExistentPathsForSources returns a list of Paths rooted from SrcDir that are |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 224 | // found in the tree. If any are not found, they are omitted from the list, |
| 225 | // 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] | 226 | func ExistentPathsForSources(ctx PathContext, paths []string) Paths { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 227 | ret := make(Paths, 0, len(paths)) |
| 228 | for _, path := range paths { |
Colin Cross | 32f3898 | 2018-02-22 11:47:25 -0800 | [diff] [blame] | 229 | p := ExistentPathForSource(ctx, path) |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 230 | if p.Valid() { |
| 231 | ret = append(ret, p.Path()) |
| 232 | } |
| 233 | } |
| 234 | return ret |
| 235 | } |
| 236 | |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 237 | // PathsForModuleSrc returns Paths rooted from the module's local source directory. It expands globs, references to |
| 238 | // SourceFileProducer modules using the ":name" syntax, and references to OutputFileProducer modules using the |
| 239 | // ":name{.tag}" syntax. Properties passed as the paths argument must have been annotated with struct tag |
| 240 | // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the |
| 241 | // path_properties mutator. If ctx.Config().AllowMissingDependencies() is true then any missing SourceFileProducer or |
| 242 | // OutputFileProducer dependencies will cause the module to be marked as having missing dependencies. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 243 | func PathsForModuleSrc(ctx ModuleContext, paths []string) Paths { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 244 | return PathsForModuleSrcExcludes(ctx, paths, nil) |
| 245 | } |
| 246 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 247 | // PathsForModuleSrcExcludes returns Paths rooted from the module's local source directory, excluding paths listed in |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 248 | // the excludes arguments. It expands globs, references to SourceFileProducer modules using the ":name" syntax, and |
| 249 | // references to OutputFileProducer modules using the ":name{.tag}" syntax. Properties passed as the paths or excludes |
| 250 | // argument must have been annotated with struct tag `android:"path"` so that dependencies on SourceFileProducer modules |
| 251 | // will have already been handled by the path_properties mutator. If ctx.Config().AllowMissingDependencies() is |
Paul Duffin | 036cace | 2019-07-25 14:44:56 +0100 | [diff] [blame] | 252 | // true then any missing SourceFileProducer or OutputFileProducer dependencies will cause the module to be marked as |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 253 | // having missing dependencies. |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 254 | func PathsForModuleSrcExcludes(ctx ModuleContext, paths, excludes []string) Paths { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 255 | ret, missingDeps := PathsAndMissingDepsForModuleSrcExcludes(ctx, paths, excludes) |
| 256 | if ctx.Config().AllowMissingDependencies() { |
| 257 | ctx.AddMissingDependencies(missingDeps) |
| 258 | } else { |
| 259 | for _, m := range missingDeps { |
| 260 | ctx.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m) |
| 261 | } |
| 262 | } |
| 263 | return ret |
| 264 | } |
| 265 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 266 | // OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection. |
| 267 | type OutputPaths []OutputPath |
| 268 | |
| 269 | // Paths returns the OutputPaths as a Paths |
| 270 | func (p OutputPaths) Paths() Paths { |
| 271 | if p == nil { |
| 272 | return nil |
| 273 | } |
| 274 | ret := make(Paths, len(p)) |
| 275 | for i, path := range p { |
| 276 | ret[i] = path |
| 277 | } |
| 278 | return ret |
| 279 | } |
| 280 | |
| 281 | // Strings returns the string forms of the writable paths. |
| 282 | func (p OutputPaths) Strings() []string { |
| 283 | if p == nil { |
| 284 | return nil |
| 285 | } |
| 286 | ret := make([]string, len(p)) |
| 287 | for i, path := range p { |
| 288 | ret[i] = path.String() |
| 289 | } |
| 290 | return ret |
| 291 | } |
| 292 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 293 | // PathsAndMissingDepsForModuleSrcExcludes returns Paths rooted from the module's local source directory, excluding |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 294 | // paths listed in the excludes arguments, and a list of missing dependencies. It expands globs, references to |
| 295 | // SourceFileProducer modules using the ":name" syntax, and references to OutputFileProducer modules using the |
| 296 | // ":name{.tag}" syntax. Properties passed as the paths or excludes argument must have been annotated with struct tag |
| 297 | // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the |
| 298 | // path_properties mutator. If ctx.Config().AllowMissingDependencies() is true then any missing SourceFileProducer or |
| 299 | // OutputFileProducer dependencies will be returned, and they will NOT cause the module to be marked as having missing |
| 300 | // dependencies. |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 301 | func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleContext, paths, excludes []string) (Paths, []string) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 302 | prefix := pathForModuleSrc(ctx).String() |
| 303 | |
| 304 | var expandedExcludes []string |
| 305 | if excludes != nil { |
| 306 | expandedExcludes = make([]string, 0, len(excludes)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 307 | } |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 308 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 309 | var missingExcludeDeps []string |
| 310 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 311 | for _, e := range excludes { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 312 | if m, t := SrcIsModuleWithTag(e); m != "" { |
| 313 | module := ctx.GetDirectDepWithTag(m, sourceOrOutputDepTag(t)) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 314 | if module == nil { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 315 | missingExcludeDeps = append(missingExcludeDeps, m) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 316 | continue |
| 317 | } |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 318 | if outProducer, ok := module.(OutputFileProducer); ok { |
| 319 | outputFiles, err := outProducer.OutputFiles(t) |
| 320 | if err != nil { |
| 321 | ctx.ModuleErrorf("path dependency %q: %s", e, err) |
| 322 | } |
| 323 | expandedExcludes = append(expandedExcludes, outputFiles.Strings()...) |
| 324 | } else if t != "" { |
| 325 | ctx.ModuleErrorf("path dependency %q is not an output file producing module", e) |
| 326 | } else if srcProducer, ok := module.(SourceFileProducer); ok { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 327 | expandedExcludes = append(expandedExcludes, srcProducer.Srcs().Strings()...) |
| 328 | } else { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 329 | ctx.ModuleErrorf("path dependency %q is not a source file producing module", e) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 330 | } |
| 331 | } else { |
| 332 | expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e)) |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | if paths == nil { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 337 | return nil, missingExcludeDeps |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 340 | var missingDeps []string |
| 341 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 342 | expandedSrcFiles := make(Paths, 0, len(paths)) |
| 343 | for _, s := range paths { |
| 344 | srcFiles, err := expandOneSrcPath(ctx, s, expandedExcludes) |
| 345 | if depErr, ok := err.(missingDependencyError); ok { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 346 | missingDeps = append(missingDeps, depErr.missingDeps...) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 347 | } else if err != nil { |
| 348 | reportPathError(ctx, err) |
| 349 | } |
| 350 | expandedSrcFiles = append(expandedSrcFiles, srcFiles...) |
| 351 | } |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 352 | |
| 353 | return expandedSrcFiles, append(missingDeps, missingExcludeDeps...) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | type missingDependencyError struct { |
| 357 | missingDeps []string |
| 358 | } |
| 359 | |
| 360 | func (e missingDependencyError) Error() string { |
| 361 | return "missing dependencies: " + strings.Join(e.missingDeps, ", ") |
| 362 | } |
| 363 | |
| 364 | func expandOneSrcPath(ctx ModuleContext, s string, expandedExcludes []string) (Paths, error) { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 365 | if m, t := SrcIsModuleWithTag(s); m != "" { |
| 366 | module := ctx.GetDirectDepWithTag(m, sourceOrOutputDepTag(t)) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 367 | if module == nil { |
| 368 | return nil, missingDependencyError{[]string{m}} |
| 369 | } |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 370 | if outProducer, ok := module.(OutputFileProducer); ok { |
| 371 | outputFiles, err := outProducer.OutputFiles(t) |
| 372 | if err != nil { |
| 373 | return nil, fmt.Errorf("path dependency %q: %s", s, err) |
| 374 | } |
| 375 | return outputFiles, nil |
| 376 | } else if t != "" { |
| 377 | return nil, fmt.Errorf("path dependency %q is not an output file producing module", s) |
| 378 | } else if srcProducer, ok := module.(SourceFileProducer); ok { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 379 | moduleSrcs := srcProducer.Srcs() |
| 380 | for _, e := range expandedExcludes { |
| 381 | for j := 0; j < len(moduleSrcs); j++ { |
| 382 | if moduleSrcs[j].String() == e { |
| 383 | moduleSrcs = append(moduleSrcs[:j], moduleSrcs[j+1:]...) |
| 384 | j-- |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | return moduleSrcs, nil |
| 389 | } else { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 390 | return nil, fmt.Errorf("path dependency %q is not a source file producing module", s) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 391 | } |
| 392 | } else if pathtools.IsGlob(s) { |
| 393 | paths := ctx.GlobFiles(pathForModuleSrc(ctx, s).String(), expandedExcludes) |
| 394 | return PathsWithModuleSrcSubDir(ctx, paths, ""), nil |
| 395 | } else { |
| 396 | p := pathForModuleSrc(ctx, s) |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 397 | if exists, _, err := ctx.Config().fs.Exists(p.String()); err != nil { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 398 | reportPathErrorf(ctx, "%s: %s", p, err.Error()) |
| 399 | } else if !exists { |
| 400 | reportPathErrorf(ctx, "module source path %q does not exist", p) |
| 401 | } |
| 402 | |
| 403 | j := findStringInSlice(p.String(), expandedExcludes) |
| 404 | if j >= 0 { |
| 405 | return nil, nil |
| 406 | } |
| 407 | return Paths{p}, nil |
| 408 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // pathsForModuleSrcFromFullPath returns Paths rooted from the module's local |
| 412 | // source directory, but strip the local source directory from the beginning of |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 413 | // 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] | 414 | // It intended for use in globs that only list files that exist, so it allows '$' in |
| 415 | // filenames. |
Colin Cross | 1184b64 | 2019-12-30 18:43:07 -0800 | [diff] [blame] | 416 | func pathsForModuleSrcFromFullPath(ctx EarlyModuleContext, paths []string, incDirs bool) Paths { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 417 | prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/" |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 418 | if prefix == "./" { |
| 419 | prefix = "" |
| 420 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 421 | ret := make(Paths, 0, len(paths)) |
| 422 | for _, p := range paths { |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 423 | if !incDirs && strings.HasSuffix(p, "/") { |
| 424 | continue |
| 425 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 426 | path := filepath.Clean(p) |
| 427 | if !strings.HasPrefix(path, prefix) { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 428 | 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] | 429 | continue |
| 430 | } |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 431 | |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 432 | srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):]) |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 433 | if err != nil { |
| 434 | reportPathError(ctx, err) |
| 435 | continue |
| 436 | } |
| 437 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 438 | srcPath.basePath.rel = srcPath.path |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 439 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 440 | ret = append(ret, srcPath) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 441 | } |
| 442 | return ret |
| 443 | } |
| 444 | |
| 445 | // PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 446 | // 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] | 447 | func PathsWithOptionalDefaultForModuleSrc(ctx ModuleContext, input []string, def string) Paths { |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 448 | if input != nil { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 449 | return PathsForModuleSrc(ctx, input) |
| 450 | } |
| 451 | // Use Glob so that if the default doesn't exist, a dependency is added so that when it |
| 452 | // is created, we're run again. |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 453 | path := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir(), def) |
Colin Cross | 461b445 | 2018-02-23 09:22:42 -0800 | [diff] [blame] | 454 | return ctx.Glob(path, nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // Strings returns the Paths in string form |
| 458 | func (p Paths) Strings() []string { |
| 459 | if p == nil { |
| 460 | return nil |
| 461 | } |
| 462 | ret := make([]string, len(p)) |
| 463 | for i, path := range p { |
| 464 | ret[i] = path.String() |
| 465 | } |
| 466 | return ret |
| 467 | } |
| 468 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 469 | // FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It |
| 470 | // 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] | 471 | func FirstUniquePaths(list Paths) Paths { |
| 472 | k := 0 |
| 473 | outer: |
| 474 | for i := 0; i < len(list); i++ { |
| 475 | for j := 0; j < k; j++ { |
| 476 | if list[i] == list[j] { |
| 477 | continue outer |
| 478 | } |
| 479 | } |
| 480 | list[k] = list[i] |
| 481 | k++ |
| 482 | } |
| 483 | return list[:k] |
| 484 | } |
| 485 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 486 | // LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It |
| 487 | // modifies the Paths slice contents in place, and returns a subslice of the original slice. |
| 488 | func LastUniquePaths(list Paths) Paths { |
| 489 | totalSkip := 0 |
| 490 | for i := len(list) - 1; i >= totalSkip; i-- { |
| 491 | skip := 0 |
| 492 | for j := i - 1; j >= totalSkip; j-- { |
| 493 | if list[i] == list[j] { |
| 494 | skip++ |
| 495 | } else { |
| 496 | list[j+skip] = list[j] |
| 497 | } |
| 498 | } |
| 499 | totalSkip += skip |
| 500 | } |
| 501 | return list[totalSkip:] |
| 502 | } |
| 503 | |
Colin Cross | a140bb0 | 2018-04-17 10:52:26 -0700 | [diff] [blame] | 504 | // ReversePaths returns a copy of a Paths in reverse order. |
| 505 | func ReversePaths(list Paths) Paths { |
| 506 | if list == nil { |
| 507 | return nil |
| 508 | } |
| 509 | ret := make(Paths, len(list)) |
| 510 | for i := range list { |
| 511 | ret[i] = list[len(list)-1-i] |
| 512 | } |
| 513 | return ret |
| 514 | } |
| 515 | |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 516 | func indexPathList(s Path, list []Path) int { |
| 517 | for i, l := range list { |
| 518 | if l == s { |
| 519 | return i |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | return -1 |
| 524 | } |
| 525 | |
| 526 | func inPathList(p Path, list []Path) bool { |
| 527 | return indexPathList(p, list) != -1 |
| 528 | } |
| 529 | |
| 530 | func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) { |
Paul Duffin | 57b9e1d | 2019-12-13 00:03:35 +0000 | [diff] [blame] | 531 | return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) }) |
| 532 | } |
| 533 | |
| 534 | func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 535 | for _, l := range list { |
Paul Duffin | 57b9e1d | 2019-12-13 00:03:35 +0000 | [diff] [blame] | 536 | if predicate(l) { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 537 | filtered = append(filtered, l) |
| 538 | } else { |
| 539 | remainder = append(remainder, l) |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | return |
| 544 | } |
| 545 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 546 | // HasExt returns true of any of the paths have extension ext, otherwise false |
| 547 | func (p Paths) HasExt(ext string) bool { |
| 548 | for _, path := range p { |
| 549 | if path.Ext() == ext { |
| 550 | return true |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | return false |
| 555 | } |
| 556 | |
| 557 | // FilterByExt returns the subset of the paths that have extension ext |
| 558 | func (p Paths) FilterByExt(ext string) Paths { |
| 559 | ret := make(Paths, 0, len(p)) |
| 560 | for _, path := range p { |
| 561 | if path.Ext() == ext { |
| 562 | ret = append(ret, path) |
| 563 | } |
| 564 | } |
| 565 | return ret |
| 566 | } |
| 567 | |
| 568 | // FilterOutByExt returns the subset of the paths that do not have extension ext |
| 569 | func (p Paths) FilterOutByExt(ext string) Paths { |
| 570 | ret := make(Paths, 0, len(p)) |
| 571 | for _, path := range p { |
| 572 | if path.Ext() != ext { |
| 573 | ret = append(ret, path) |
| 574 | } |
| 575 | } |
| 576 | return ret |
| 577 | } |
| 578 | |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 579 | // DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory |
| 580 | // (including subdirectories) are in a contiguous subslice of the list, and can be found in |
| 581 | // O(log(N)) time using a binary search on the directory prefix. |
| 582 | type DirectorySortedPaths Paths |
| 583 | |
| 584 | func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths { |
| 585 | ret := append(DirectorySortedPaths(nil), paths...) |
| 586 | sort.Slice(ret, func(i, j int) bool { |
| 587 | return ret[i].String() < ret[j].String() |
| 588 | }) |
| 589 | return ret |
| 590 | } |
| 591 | |
| 592 | // PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries |
| 593 | // that are in the specified directory and its subdirectories. |
| 594 | func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths { |
| 595 | prefix := filepath.Clean(dir) + "/" |
| 596 | start := sort.Search(len(p), func(i int) bool { |
| 597 | return prefix < p[i].String() |
| 598 | }) |
| 599 | |
| 600 | ret := p[start:] |
| 601 | |
| 602 | end := sort.Search(len(ret), func(i int) bool { |
| 603 | return !strings.HasPrefix(ret[i].String(), prefix) |
| 604 | }) |
| 605 | |
| 606 | ret = ret[:end] |
| 607 | |
| 608 | return Paths(ret) |
| 609 | } |
| 610 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 611 | // WritablePaths is a slice of WritablePaths, used for multiple outputs. |
| 612 | type WritablePaths []WritablePath |
| 613 | |
| 614 | // Strings returns the string forms of the writable paths. |
| 615 | func (p WritablePaths) Strings() []string { |
| 616 | if p == nil { |
| 617 | return nil |
| 618 | } |
| 619 | ret := make([]string, len(p)) |
| 620 | for i, path := range p { |
| 621 | ret[i] = path.String() |
| 622 | } |
| 623 | return ret |
| 624 | } |
| 625 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 626 | // Paths returns the WritablePaths as a Paths |
| 627 | func (p WritablePaths) Paths() Paths { |
| 628 | if p == nil { |
| 629 | return nil |
| 630 | } |
| 631 | ret := make(Paths, len(p)) |
| 632 | for i, path := range p { |
| 633 | ret[i] = path |
| 634 | } |
| 635 | return ret |
| 636 | } |
| 637 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 638 | type basePath struct { |
| 639 | path string |
| 640 | config Config |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 641 | rel string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | func (p basePath) Ext() string { |
| 645 | return filepath.Ext(p.path) |
| 646 | } |
| 647 | |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 648 | func (p basePath) Base() string { |
| 649 | return filepath.Base(p.path) |
| 650 | } |
| 651 | |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 652 | func (p basePath) Rel() string { |
| 653 | if p.rel != "" { |
| 654 | return p.rel |
| 655 | } |
| 656 | return p.path |
| 657 | } |
| 658 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 659 | func (p basePath) String() string { |
| 660 | return p.path |
| 661 | } |
| 662 | |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 663 | func (p basePath) withRel(rel string) basePath { |
| 664 | p.path = filepath.Join(p.path, rel) |
| 665 | p.rel = rel |
| 666 | return p |
| 667 | } |
| 668 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 669 | // SourcePath is a Path representing a file path rooted from SrcDir |
| 670 | type SourcePath struct { |
| 671 | basePath |
| 672 | } |
| 673 | |
| 674 | var _ Path = SourcePath{} |
| 675 | |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 676 | func (p SourcePath) withRel(rel string) SourcePath { |
| 677 | p.basePath = p.basePath.withRel(rel) |
| 678 | return p |
| 679 | } |
| 680 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 681 | // safePathForSource is for paths that we expect are safe -- only for use by go |
| 682 | // code that is embedding ninja variables in paths |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 683 | func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { |
| 684 | p, err := validateSafePath(pathComponents...) |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 685 | ret := SourcePath{basePath{p, ctx.Config(), ""}} |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 686 | if err != nil { |
| 687 | return ret, err |
| 688 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 689 | |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 690 | // absolute path already checked by validateSafePath |
| 691 | if strings.HasPrefix(ret.String(), ctx.Config().buildDir) { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 692 | return ret, fmt.Errorf("source path %q is in output", ret.String()) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 695 | return ret, err |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 698 | // pathForSource creates a SourcePath from pathComponents, but does not check that it exists. |
| 699 | func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 700 | p, err := validatePath(pathComponents...) |
| 701 | ret := SourcePath{basePath{p, ctx.Config(), ""}} |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 702 | if err != nil { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 703 | return ret, err |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 704 | } |
| 705 | |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 706 | // absolute path already checked by validatePath |
| 707 | if strings.HasPrefix(ret.String(), ctx.Config().buildDir) { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 708 | return ret, fmt.Errorf("source path %q is in output", ret.String()) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 711 | return ret, nil |
| 712 | } |
| 713 | |
| 714 | // existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the |
| 715 | // path does not exist. |
| 716 | func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) { |
| 717 | var files []string |
| 718 | |
| 719 | if gctx, ok := ctx.(PathGlobContext); ok { |
| 720 | // Use glob to produce proper dependencies, even though we only want |
| 721 | // a single file. |
| 722 | files, err = gctx.GlobWithDeps(path.String(), nil) |
| 723 | } else { |
| 724 | var deps []string |
| 725 | // We cannot add build statements in this context, so we fall back to |
| 726 | // AddNinjaFileDeps |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 727 | files, deps, err = ctx.Config().fs.Glob(path.String(), nil, pathtools.FollowSymlinks) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 728 | ctx.AddNinjaFileDeps(deps...) |
| 729 | } |
| 730 | |
| 731 | if err != nil { |
| 732 | return false, fmt.Errorf("glob: %s", err.Error()) |
| 733 | } |
| 734 | |
| 735 | return len(files) > 0, nil |
| 736 | } |
| 737 | |
| 738 | // PathForSource joins the provided path components and validates that the result |
| 739 | // neither escapes the source dir nor is in the out dir. |
| 740 | // On error, it will return a usable, but invalid SourcePath, and report a ModuleError. |
| 741 | func PathForSource(ctx PathContext, pathComponents ...string) SourcePath { |
| 742 | path, err := pathForSource(ctx, pathComponents...) |
| 743 | if err != nil { |
| 744 | reportPathError(ctx, err) |
| 745 | } |
| 746 | |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 747 | if pathtools.IsGlob(path.String()) { |
| 748 | reportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) |
| 749 | } |
| 750 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 751 | if modCtx, ok := ctx.(ModuleContext); ok && ctx.Config().AllowMissingDependencies() { |
| 752 | exists, err := existsWithDependencies(ctx, path) |
| 753 | if err != nil { |
| 754 | reportPathError(ctx, err) |
| 755 | } |
| 756 | if !exists { |
| 757 | modCtx.AddMissingDependencies([]string{path.String()}) |
| 758 | } |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 759 | } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 760 | reportPathErrorf(ctx, "%s: %s", path, err.Error()) |
| 761 | } else if !exists { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 762 | reportPathErrorf(ctx, "source path %q does not exist", path) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 763 | } |
| 764 | return path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 767 | // ExistentPathForSource returns an OptionalPath with the SourcePath if the |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 768 | // path exists, or an empty OptionalPath if it doesn't exist. Dependencies are added |
| 769 | // 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] | 770 | func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 771 | path, err := pathForSource(ctx, pathComponents...) |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 772 | if err != nil { |
| 773 | reportPathError(ctx, err) |
| 774 | return OptionalPath{} |
| 775 | } |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 776 | |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 777 | if pathtools.IsGlob(path.String()) { |
| 778 | reportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) |
| 779 | return OptionalPath{} |
| 780 | } |
| 781 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 782 | exists, err := existsWithDependencies(ctx, path) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 783 | if err != nil { |
| 784 | reportPathError(ctx, err) |
| 785 | return OptionalPath{} |
| 786 | } |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 787 | if !exists { |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 788 | return OptionalPath{} |
| 789 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 790 | return OptionalPathForPath(path) |
| 791 | } |
| 792 | |
| 793 | func (p SourcePath) String() string { |
| 794 | return filepath.Join(p.config.srcDir, p.path) |
| 795 | } |
| 796 | |
| 797 | // Join creates a new SourcePath with paths... joined with the current path. The |
| 798 | // provided paths... may not use '..' to escape from the current path. |
| 799 | func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 800 | path, err := validatePath(paths...) |
| 801 | if err != nil { |
| 802 | reportPathError(ctx, err) |
| 803 | } |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 804 | return p.withRel(path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 807 | // join is like Join but does less path validation. |
| 808 | func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath { |
| 809 | path, err := validateSafePath(paths...) |
| 810 | if err != nil { |
| 811 | reportPathError(ctx, err) |
| 812 | } |
| 813 | return p.withRel(path) |
| 814 | } |
| 815 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 816 | // OverlayPath returns the overlay for `path' if it exists. This assumes that the |
| 817 | // SourcePath is the path to a resource overlay directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 818 | func (p SourcePath) OverlayPath(ctx ModuleContext, path Path) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 819 | var relDir string |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 820 | if srcPath, ok := path.(SourcePath); ok { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 821 | relDir = srcPath.path |
| 822 | } else { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 823 | 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] | 824 | return OptionalPath{} |
| 825 | } |
| 826 | dir := filepath.Join(p.config.srcDir, p.path, relDir) |
| 827 | // 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] | 828 | if pathtools.IsGlob(dir) { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 829 | reportPathErrorf(ctx, "Path may not contain a glob: %s", dir) |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 830 | } |
Colin Cross | 461b445 | 2018-02-23 09:22:42 -0800 | [diff] [blame] | 831 | paths, err := ctx.GlobWithDeps(dir, nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 832 | if err != nil { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 833 | reportPathErrorf(ctx, "glob: %s", err.Error()) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 834 | return OptionalPath{} |
| 835 | } |
| 836 | if len(paths) == 0 { |
| 837 | return OptionalPath{} |
| 838 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 839 | relPath := Rel(ctx, p.config.srcDir, paths[0]) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 840 | return OptionalPathForPath(PathForSource(ctx, relPath)) |
| 841 | } |
| 842 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 843 | // OutputPath is a Path representing an intermediates file path rooted from the build directory |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 844 | type OutputPath struct { |
| 845 | basePath |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 846 | fullPath string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 849 | func (p OutputPath) withRel(rel string) OutputPath { |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 850 | p.basePath = p.basePath.withRel(rel) |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 851 | p.fullPath = filepath.Join(p.fullPath, rel) |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 852 | return p |
| 853 | } |
| 854 | |
Colin Cross | 3063b78 | 2018-08-15 11:19:12 -0700 | [diff] [blame] | 855 | func (p OutputPath) WithoutRel() OutputPath { |
| 856 | p.basePath.rel = filepath.Base(p.basePath.path) |
| 857 | return p |
| 858 | } |
| 859 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 860 | func (p OutputPath) buildDir() string { |
| 861 | return p.config.buildDir |
| 862 | } |
| 863 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 864 | var _ Path = OutputPath{} |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 865 | var _ WritablePath = OutputPath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 866 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 867 | // PathForOutput joins the provided paths and returns an OutputPath that is |
| 868 | // validated to not escape the build dir. |
| 869 | // On error, it will return a usable, but invalid OutputPath, and report a ModuleError. |
| 870 | func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 871 | path, err := validatePath(pathComponents...) |
| 872 | if err != nil { |
| 873 | reportPathError(ctx, err) |
| 874 | } |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 875 | fullPath := filepath.Join(ctx.Config().buildDir, path) |
| 876 | path = fullPath[len(fullPath)-len(path):] |
| 877 | return OutputPath{basePath{path, ctx.Config(), ""}, fullPath} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 880 | // PathsForOutput returns Paths rooted from buildDir |
| 881 | func PathsForOutput(ctx PathContext, paths []string) WritablePaths { |
| 882 | ret := make(WritablePaths, len(paths)) |
| 883 | for i, path := range paths { |
| 884 | ret[i] = PathForOutput(ctx, path) |
| 885 | } |
| 886 | return ret |
| 887 | } |
| 888 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 889 | func (p OutputPath) writablePath() {} |
| 890 | |
| 891 | func (p OutputPath) String() string { |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 892 | return p.fullPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | // Join creates a new OutputPath with paths... joined with the current path. The |
| 896 | // provided paths... may not use '..' to escape from the current path. |
| 897 | func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 898 | path, err := validatePath(paths...) |
| 899 | if err != nil { |
| 900 | reportPathError(ctx, err) |
| 901 | } |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 902 | return p.withRel(path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 905 | // ReplaceExtension creates a new OutputPath with the extension replaced with ext. |
| 906 | func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { |
| 907 | if strings.Contains(ext, "/") { |
| 908 | reportPathErrorf(ctx, "extension %q cannot contain /", ext) |
| 909 | } |
| 910 | ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext)) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 911 | ret.rel = pathtools.ReplaceExtension(p.rel, ext) |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 912 | return ret |
| 913 | } |
| 914 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 915 | // InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths. |
| 916 | func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath { |
| 917 | path, err := validatePath(paths...) |
| 918 | if err != nil { |
| 919 | reportPathError(ctx, err) |
| 920 | } |
| 921 | |
| 922 | ret := PathForOutput(ctx, filepath.Dir(p.path), path) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 923 | ret.rel = filepath.Join(filepath.Dir(p.rel), path) |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 924 | return ret |
| 925 | } |
| 926 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 927 | // PathForIntermediates returns an OutputPath representing the top-level |
| 928 | // intermediates directory. |
| 929 | func PathForIntermediates(ctx PathContext, paths ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 930 | path, err := validatePath(paths...) |
| 931 | if err != nil { |
| 932 | reportPathError(ctx, err) |
| 933 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 934 | return PathForOutput(ctx, ".intermediates", path) |
| 935 | } |
| 936 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 937 | var _ genPathProvider = SourcePath{} |
| 938 | var _ objPathProvider = SourcePath{} |
| 939 | var _ resPathProvider = SourcePath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 940 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 941 | // PathForModuleSrc returns a Path representing the paths... under the |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 942 | // module's local source directory. |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 943 | func PathForModuleSrc(ctx ModuleContext, pathComponents ...string) Path { |
| 944 | p, err := validatePath(pathComponents...) |
| 945 | if err != nil { |
| 946 | reportPathError(ctx, err) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 947 | } |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 948 | paths, err := expandOneSrcPath(ctx, p, nil) |
| 949 | if err != nil { |
| 950 | if depErr, ok := err.(missingDependencyError); ok { |
| 951 | if ctx.Config().AllowMissingDependencies() { |
| 952 | ctx.AddMissingDependencies(depErr.missingDeps) |
| 953 | } else { |
| 954 | ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error()) |
| 955 | } |
| 956 | } else { |
| 957 | reportPathError(ctx, err) |
| 958 | } |
| 959 | return nil |
| 960 | } else if len(paths) == 0 { |
| 961 | reportPathErrorf(ctx, "%q produced no files, expected exactly one", p) |
| 962 | return nil |
| 963 | } else if len(paths) > 1 { |
| 964 | reportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths)) |
| 965 | } |
| 966 | return paths[0] |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 969 | func pathForModuleSrc(ctx ModuleContext, paths ...string) SourcePath { |
| 970 | p, err := validatePath(paths...) |
| 971 | if err != nil { |
| 972 | reportPathError(ctx, err) |
| 973 | } |
| 974 | |
| 975 | path, err := pathForSource(ctx, ctx.ModuleDir(), p) |
| 976 | if err != nil { |
| 977 | reportPathError(ctx, err) |
| 978 | } |
| 979 | |
| 980 | path.basePath.rel = p |
| 981 | |
| 982 | return path |
| 983 | } |
| 984 | |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 985 | // PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path |
| 986 | // will return the path relative to subDir in the module's source directory. If any input paths are not located |
| 987 | // inside subDir then a path error will be reported. |
| 988 | func PathsWithModuleSrcSubDir(ctx ModuleContext, paths Paths, subDir string) Paths { |
| 989 | paths = append(Paths(nil), paths...) |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 990 | subDirFullPath := pathForModuleSrc(ctx, subDir) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 991 | for i, path := range paths { |
| 992 | rel := Rel(ctx, subDirFullPath.String(), path.String()) |
| 993 | paths[i] = subDirFullPath.join(ctx, rel) |
| 994 | } |
| 995 | return paths |
| 996 | } |
| 997 | |
| 998 | // PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the |
| 999 | // module's source directory. If the input path is not located inside subDir then a path error will be reported. |
| 1000 | func PathWithModuleSrcSubDir(ctx ModuleContext, path Path, subDir string) Path { |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1001 | subDirFullPath := pathForModuleSrc(ctx, subDir) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1002 | rel := Rel(ctx, subDirFullPath.String(), path.String()) |
| 1003 | return subDirFullPath.Join(ctx, rel) |
| 1004 | } |
| 1005 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1006 | // OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a |
| 1007 | // valid path if p is non-nil. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 1008 | func OptionalPathForModuleSrc(ctx ModuleContext, p *string) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1009 | if p == nil { |
| 1010 | return OptionalPath{} |
| 1011 | } |
| 1012 | return OptionalPathForPath(PathForModuleSrc(ctx, *p)) |
| 1013 | } |
| 1014 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1015 | func (p SourcePath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 1016 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1019 | func (p SourcePath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 1020 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1023 | func (p SourcePath) resPathWithName(ctx ModuleContext, name string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1024 | // TODO: Use full directory if the new ctx is not the current ctx? |
| 1025 | return PathForModuleRes(ctx, p.path, name) |
| 1026 | } |
| 1027 | |
| 1028 | // ModuleOutPath is a Path representing a module's output directory. |
| 1029 | type ModuleOutPath struct { |
| 1030 | OutputPath |
| 1031 | } |
| 1032 | |
| 1033 | var _ Path = ModuleOutPath{} |
| 1034 | |
Pete Bentley | fcf55bf | 2019-08-16 20:14:32 +0100 | [diff] [blame] | 1035 | func (p ModuleOutPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath { |
| 1036 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1037 | } |
| 1038 | |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1039 | func pathForModule(ctx ModuleContext) OutputPath { |
| 1040 | return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir()) |
| 1041 | } |
| 1042 | |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1043 | // PathForVndkRefAbiDump returns an OptionalPath representing the path of the |
| 1044 | // reference abi dump for the given module. This is not guaranteed to be valid. |
| 1045 | func PathForVndkRefAbiDump(ctx ModuleContext, version, fileName string, |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 1046 | isNdk, isLlndkOrVndk, isGzip bool) OptionalPath { |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1047 | |
Jayant Chowdhary | ac066c6 | 2018-02-20 10:53:31 -0800 | [diff] [blame] | 1048 | arches := ctx.DeviceConfig().Arches() |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1049 | if len(arches) == 0 { |
| 1050 | panic("device build with no primary arch") |
| 1051 | } |
Jayant Chowdhary | ac066c6 | 2018-02-20 10:53:31 -0800 | [diff] [blame] | 1052 | currentArch := ctx.Arch() |
| 1053 | archNameAndVariant := currentArch.ArchType.String() |
| 1054 | if currentArch.ArchVariant != "" { |
| 1055 | archNameAndVariant += "_" + currentArch.ArchVariant |
| 1056 | } |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1057 | |
| 1058 | var dirName string |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 1059 | if isNdk { |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1060 | dirName = "ndk" |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 1061 | } else if isLlndkOrVndk { |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1062 | dirName = "vndk" |
Logan Chien | 41eabe6 | 2019-04-10 13:33:58 +0800 | [diff] [blame] | 1063 | } else { |
| 1064 | dirName = "platform" // opt-in libs |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 1065 | } |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1066 | |
Jayant Chowdhary | 34ce67d | 2018-03-08 11:00:50 -0800 | [diff] [blame] | 1067 | binderBitness := ctx.DeviceConfig().BinderBitness() |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1068 | |
| 1069 | var ext string |
| 1070 | if isGzip { |
| 1071 | ext = ".lsdump.gz" |
| 1072 | } else { |
| 1073 | ext = ".lsdump" |
| 1074 | } |
| 1075 | |
| 1076 | return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName, |
| 1077 | version, binderBitness, archNameAndVariant, "source-based", |
| 1078 | fileName+ext) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1081 | // PathForModuleOut returns a Path representing the paths... under the module's |
| 1082 | // output directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 1083 | func PathForModuleOut(ctx ModuleContext, paths ...string) ModuleOutPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1084 | p, err := validatePath(paths...) |
| 1085 | if err != nil { |
| 1086 | reportPathError(ctx, err) |
| 1087 | } |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1088 | return ModuleOutPath{ |
| 1089 | OutputPath: pathForModule(ctx).withRel(p), |
| 1090 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | // ModuleGenPath is a Path representing the 'gen' directory in a module's output |
| 1094 | // directory. Mainly used for generated sources. |
| 1095 | type ModuleGenPath struct { |
| 1096 | ModuleOutPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | var _ Path = ModuleGenPath{} |
| 1100 | var _ genPathProvider = ModuleGenPath{} |
| 1101 | var _ objPathProvider = ModuleGenPath{} |
| 1102 | |
| 1103 | // PathForModuleGen returns a Path representing the paths... under the module's |
| 1104 | // `gen' directory. |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 1105 | func PathForModuleGen(ctx ModuleContext, paths ...string) ModuleGenPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1106 | p, err := validatePath(paths...) |
| 1107 | if err != nil { |
| 1108 | reportPathError(ctx, err) |
| 1109 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1110 | return ModuleGenPath{ |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1111 | ModuleOutPath: ModuleOutPath{ |
| 1112 | OutputPath: pathForModule(ctx).withRel("gen").withRel(p), |
| 1113 | }, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 1117 | func (p ModuleGenPath) genPathWithExt(ctx ModuleContext, subdir, ext string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1118 | // TODO: make a different path for local vs remote generated files? |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 1119 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 1122 | func (p ModuleGenPath) objPathWithExt(ctx ModuleContext, subdir, ext string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1123 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1124 | } |
| 1125 | |
| 1126 | // ModuleObjPath is a Path representing the 'obj' directory in a module's output |
| 1127 | // directory. Used for compiled objects. |
| 1128 | type ModuleObjPath struct { |
| 1129 | ModuleOutPath |
| 1130 | } |
| 1131 | |
| 1132 | var _ Path = ModuleObjPath{} |
| 1133 | |
| 1134 | // PathForModuleObj returns a Path representing the paths... under the module's |
| 1135 | // 'obj' directory. |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1136 | func PathForModuleObj(ctx ModuleContext, pathComponents ...string) ModuleObjPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1137 | p, err := validatePath(pathComponents...) |
| 1138 | if err != nil { |
| 1139 | reportPathError(ctx, err) |
| 1140 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1141 | return ModuleObjPath{PathForModuleOut(ctx, "obj", p)} |
| 1142 | } |
| 1143 | |
| 1144 | // ModuleResPath is a a Path representing the 'res' directory in a module's |
| 1145 | // output directory. |
| 1146 | type ModuleResPath struct { |
| 1147 | ModuleOutPath |
| 1148 | } |
| 1149 | |
| 1150 | var _ Path = ModuleResPath{} |
| 1151 | |
| 1152 | // PathForModuleRes returns a Path representing the paths... under the module's |
| 1153 | // 'res' directory. |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1154 | func PathForModuleRes(ctx ModuleContext, pathComponents ...string) ModuleResPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1155 | p, err := validatePath(pathComponents...) |
| 1156 | if err != nil { |
| 1157 | reportPathError(ctx, err) |
| 1158 | } |
| 1159 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1160 | return ModuleResPath{PathForModuleOut(ctx, "res", p)} |
| 1161 | } |
| 1162 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1163 | // InstallPath is a Path representing a installed file path rooted from the build directory |
| 1164 | type InstallPath struct { |
| 1165 | basePath |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1166 | |
| 1167 | baseDir string // "../" for Make paths to convert "out/soong" to "out", "" for Soong paths |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1170 | func (p InstallPath) buildDir() string { |
| 1171 | return p.config.buildDir |
| 1172 | } |
| 1173 | |
| 1174 | var _ Path = InstallPath{} |
| 1175 | var _ WritablePath = InstallPath{} |
| 1176 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1177 | func (p InstallPath) writablePath() {} |
| 1178 | |
| 1179 | func (p InstallPath) String() string { |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1180 | return filepath.Join(p.config.buildDir, p.baseDir, p.path) |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | // Join creates a new InstallPath with paths... joined with the current path. The |
| 1184 | // provided paths... may not use '..' to escape from the current path. |
| 1185 | func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath { |
| 1186 | path, err := validatePath(paths...) |
| 1187 | if err != nil { |
| 1188 | reportPathError(ctx, err) |
| 1189 | } |
| 1190 | return p.withRel(path) |
| 1191 | } |
| 1192 | |
| 1193 | func (p InstallPath) withRel(rel string) InstallPath { |
| 1194 | p.basePath = p.basePath.withRel(rel) |
| 1195 | return p |
| 1196 | } |
| 1197 | |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1198 | // ToMakePath returns a new InstallPath that points to Make's install directory instead of Soong's, |
| 1199 | // i.e. out/ instead of out/soong/. |
| 1200 | func (p InstallPath) ToMakePath() InstallPath { |
| 1201 | p.baseDir = "../" |
| 1202 | return p |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1205 | // PathForModuleInstall returns a Path representing the install path for the |
| 1206 | // module appended with paths... |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1207 | func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1208 | var outPaths []string |
| 1209 | if ctx.Device() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1210 | partition := modulePartition(ctx) |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 1211 | outPaths = []string{"target", "product", ctx.Config().DeviceName(), partition} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1212 | } else { |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 1213 | switch ctx.Os() { |
| 1214 | case Linux: |
| 1215 | outPaths = []string{"host", "linux-x86"} |
| 1216 | case LinuxBionic: |
| 1217 | // TODO: should this be a separate top level, or shared with linux-x86? |
| 1218 | outPaths = []string{"host", "linux_bionic-x86"} |
| 1219 | default: |
| 1220 | outPaths = []string{"host", ctx.Os().String() + "-x86"} |
| 1221 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1222 | } |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame] | 1223 | if ctx.Debug() { |
| 1224 | outPaths = append([]string{"debug"}, outPaths...) |
| 1225 | } |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1226 | outPaths = append(outPaths, pathComponents...) |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1227 | |
| 1228 | path, err := validatePath(outPaths...) |
| 1229 | if err != nil { |
| 1230 | reportPathError(ctx, err) |
| 1231 | } |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1232 | |
| 1233 | ret := InstallPath{basePath{path, ctx.Config(), ""}, ""} |
| 1234 | if ctx.InstallBypassMake() && ctx.Config().EmbeddedInMake() { |
| 1235 | ret = ret.ToMakePath() |
| 1236 | } |
| 1237 | |
| 1238 | return ret |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1241 | func PathForNdkInstall(ctx PathContext, paths ...string) InstallPath { |
| 1242 | paths = append([]string{"ndk"}, paths...) |
| 1243 | path, err := validatePath(paths...) |
| 1244 | if err != nil { |
| 1245 | reportPathError(ctx, err) |
| 1246 | } |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1247 | return InstallPath{basePath{path, ctx.Config(), ""}, ""} |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1251 | rel := Rel(ctx, PathForOutput(ctx, "target", "product", ctx.Config().DeviceName()).String(), path.String()) |
| 1252 | |
| 1253 | return "/" + rel |
| 1254 | } |
| 1255 | |
| 1256 | func modulePartition(ctx ModuleInstallPathContext) string { |
| 1257 | var partition string |
| 1258 | if ctx.InstallInData() { |
| 1259 | partition = "data" |
Jaewoong Jung | 0949f31 | 2019-09-11 10:25:18 -0700 | [diff] [blame] | 1260 | } else if ctx.InstallInTestcases() { |
| 1261 | partition = "testcases" |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 1262 | } else if ctx.InstallInRamdisk() { |
Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1263 | if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() { |
| 1264 | partition = "recovery/root/first_stage_ramdisk" |
| 1265 | } else { |
| 1266 | partition = "ramdisk" |
| 1267 | } |
| 1268 | if !ctx.InstallInRoot() { |
| 1269 | partition += "/system" |
| 1270 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1271 | } else if ctx.InstallInRecovery() { |
Colin Cross | 90ba5f4 | 2019-10-02 11:10:58 -0700 | [diff] [blame] | 1272 | if ctx.InstallInRoot() { |
| 1273 | partition = "recovery/root" |
| 1274 | } else { |
| 1275 | // the layout of recovery partion is the same as that of system partition |
| 1276 | partition = "recovery/root/system" |
| 1277 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1278 | } else if ctx.SocSpecific() { |
| 1279 | partition = ctx.DeviceConfig().VendorPath() |
| 1280 | } else if ctx.DeviceSpecific() { |
| 1281 | partition = ctx.DeviceConfig().OdmPath() |
| 1282 | } else if ctx.ProductSpecific() { |
| 1283 | partition = ctx.DeviceConfig().ProductPath() |
Justin Yun | d5f6c82 | 2019-06-25 16:47:17 +0900 | [diff] [blame] | 1284 | } else if ctx.SystemExtSpecific() { |
| 1285 | partition = ctx.DeviceConfig().SystemExtPath() |
Colin Cross | 90ba5f4 | 2019-10-02 11:10:58 -0700 | [diff] [blame] | 1286 | } else if ctx.InstallInRoot() { |
| 1287 | partition = "root" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1288 | } else { |
| 1289 | partition = "system" |
| 1290 | } |
| 1291 | if ctx.InstallInSanitizerDir() { |
| 1292 | partition = "data/asan/" + partition |
| 1293 | } |
| 1294 | return partition |
| 1295 | } |
| 1296 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1297 | // validateSafePath validates a path that we trust (may contain ninja variables). |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1298 | // Ensures that each path component does not attempt to leave its component. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1299 | func validateSafePath(pathComponents ...string) (string, error) { |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1300 | for _, path := range pathComponents { |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1301 | path := filepath.Clean(path) |
| 1302 | if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1303 | return "", fmt.Errorf("Path is outside directory: %s", path) |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1304 | } |
| 1305 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1306 | // TODO: filepath.Join isn't necessarily correct with embedded ninja |
| 1307 | // variables. '..' may remove the entire ninja variable, even if it |
| 1308 | // will be expanded to multiple nested directories. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1309 | return filepath.Join(pathComponents...), nil |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1312 | // validatePath validates that a path does not include ninja variables, and that |
| 1313 | // each path component does not attempt to leave its component. Returns a joined |
| 1314 | // version of each path component. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1315 | func validatePath(pathComponents ...string) (string, error) { |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1316 | for _, path := range pathComponents { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1317 | if strings.Contains(path, "$") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1318 | return "", fmt.Errorf("Path contains invalid character($): %s", path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1319 | } |
| 1320 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1321 | return validateSafePath(pathComponents...) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 1322 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1323 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1324 | func PathForPhony(ctx PathContext, phony string) WritablePath { |
| 1325 | if strings.ContainsAny(phony, "$/") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1326 | reportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1327 | } |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 1328 | return PhonyPath{basePath{phony, ctx.Config(), ""}} |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 1331 | type PhonyPath struct { |
| 1332 | basePath |
| 1333 | } |
| 1334 | |
| 1335 | func (p PhonyPath) writablePath() {} |
| 1336 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1337 | func (p PhonyPath) buildDir() string { |
| 1338 | return p.config.buildDir |
| 1339 | } |
| 1340 | |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 1341 | var _ Path = PhonyPath{} |
| 1342 | var _ WritablePath = PhonyPath{} |
| 1343 | |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1344 | type testPath struct { |
| 1345 | basePath |
| 1346 | } |
| 1347 | |
| 1348 | func (p testPath) String() string { |
| 1349 | return p.path |
| 1350 | } |
| 1351 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1352 | // PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from |
| 1353 | // within tests. |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1354 | func PathForTesting(paths ...string) Path { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1355 | p, err := validateSafePath(paths...) |
| 1356 | if err != nil { |
| 1357 | panic(err) |
| 1358 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1359 | return testPath{basePath{path: p, rel: p}} |
| 1360 | } |
| 1361 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1362 | // PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests. |
| 1363 | func PathsForTesting(strs ...string) Paths { |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1364 | p := make(Paths, len(strs)) |
| 1365 | for i, s := range strs { |
| 1366 | p[i] = PathForTesting(s) |
| 1367 | } |
| 1368 | |
| 1369 | return p |
| 1370 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1371 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1372 | type testPathContext struct { |
| 1373 | config Config |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1374 | } |
| 1375 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1376 | func (x *testPathContext) Config() Config { return x.config } |
| 1377 | func (x *testPathContext) AddNinjaFileDeps(...string) {} |
| 1378 | |
| 1379 | // PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with |
| 1380 | // PathForOutput. |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1381 | func PathContextForTesting(config Config) PathContext { |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1382 | return &testPathContext{ |
| 1383 | config: config, |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1387 | // Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if |
| 1388 | // targetPath is not inside basePath. |
| 1389 | func Rel(ctx PathContext, basePath string, targetPath string) string { |
| 1390 | rel, isRel := MaybeRel(ctx, basePath, targetPath) |
| 1391 | if !isRel { |
| 1392 | reportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath) |
| 1393 | return "" |
| 1394 | } |
| 1395 | return rel |
| 1396 | } |
| 1397 | |
| 1398 | // MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if |
| 1399 | // targetPath is not inside basePath. |
| 1400 | func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1401 | rel, isRel, err := maybeRelErr(basePath, targetPath) |
| 1402 | if err != nil { |
| 1403 | reportPathError(ctx, err) |
| 1404 | } |
| 1405 | return rel, isRel |
| 1406 | } |
| 1407 | |
| 1408 | func maybeRelErr(basePath string, targetPath string) (string, bool, error) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1409 | // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first. |
| 1410 | if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1411 | return "", false, nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1412 | } |
| 1413 | rel, err := filepath.Rel(basePath, targetPath) |
| 1414 | if err != nil { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1415 | return "", false, err |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1416 | } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1417 | return "", false, nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1418 | } |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 1419 | return rel, true, nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1420 | } |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 1421 | |
| 1422 | // Writes a file to the output directory. Attempting to write directly to the output directory |
| 1423 | // will fail due to the sandbox of the soong_build process. |
| 1424 | func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error { |
| 1425 | return ioutil.WriteFile(absolutePath(path.String()), data, perm) |
| 1426 | } |
| 1427 | |
| 1428 | func absolutePath(path string) string { |
| 1429 | if filepath.IsAbs(path) { |
| 1430 | return path |
| 1431 | } |
| 1432 | return filepath.Join(absSrcDir, path) |
| 1433 | } |