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