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" |
Colin Cross | 0e44615 | 2021-05-03 13:35:32 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint/bootstrap" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint/pathtools" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 31 | var absSrcDir string |
| 32 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 33 | // PathContext is the subset of a (Module|Singleton)Context required by the |
| 34 | // Path methods. |
| 35 | type PathContext interface { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 36 | Config() Config |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 37 | AddNinjaFileDeps(deps ...string) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Colin Cross | 7f19f37 | 2016-11-01 11:10:25 -0700 | [diff] [blame] | 40 | type PathGlobContext interface { |
| 41 | GlobWithDeps(globPattern string, excludes []string) ([]string, error) |
| 42 | } |
| 43 | |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 44 | var _ PathContext = SingletonContext(nil) |
| 45 | var _ PathContext = ModuleContext(nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 46 | |
Ulya Trafimovich | 8640ab9 | 2020-05-11 18:06:15 +0100 | [diff] [blame] | 47 | // "Null" path context is a minimal path context for a given config. |
| 48 | type NullPathContext struct { |
| 49 | config Config |
| 50 | } |
| 51 | |
| 52 | func (NullPathContext) AddNinjaFileDeps(...string) {} |
| 53 | func (ctx NullPathContext) Config() Config { return ctx.config } |
| 54 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 55 | // EarlyModulePathContext is a subset of EarlyModuleContext methods required by the |
| 56 | // Path methods. These path methods can be called before any mutators have run. |
| 57 | type EarlyModulePathContext interface { |
| 58 | PathContext |
| 59 | PathGlobContext |
| 60 | |
| 61 | ModuleDir() string |
| 62 | ModuleErrorf(fmt string, args ...interface{}) |
| 63 | } |
| 64 | |
| 65 | var _ EarlyModulePathContext = ModuleContext(nil) |
| 66 | |
| 67 | // Glob globs files and directories matching globPattern relative to ModuleDir(), |
| 68 | // paths in the excludes parameter will be omitted. |
| 69 | func Glob(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths { |
| 70 | ret, err := ctx.GlobWithDeps(globPattern, excludes) |
| 71 | if err != nil { |
| 72 | ctx.ModuleErrorf("glob: %s", err.Error()) |
| 73 | } |
| 74 | return pathsForModuleSrcFromFullPath(ctx, ret, true) |
| 75 | } |
| 76 | |
| 77 | // GlobFiles globs *only* files (not directories) matching globPattern relative to ModuleDir(). |
| 78 | // Paths in the excludes parameter will be omitted. |
| 79 | func GlobFiles(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths { |
| 80 | ret, err := ctx.GlobWithDeps(globPattern, excludes) |
| 81 | if err != nil { |
| 82 | ctx.ModuleErrorf("glob: %s", err.Error()) |
| 83 | } |
| 84 | return pathsForModuleSrcFromFullPath(ctx, ret, false) |
| 85 | } |
| 86 | |
| 87 | // ModuleWithDepsPathContext is a subset of *ModuleContext methods required by |
| 88 | // the Path methods that rely on module dependencies having been resolved. |
| 89 | type ModuleWithDepsPathContext interface { |
| 90 | EarlyModulePathContext |
Paul Duffin | 40131a3 | 2021-07-09 17:10:35 +0100 | [diff] [blame^] | 91 | VisitDirectDepsBlueprint(visit func(blueprint.Module)) |
| 92 | OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // ModuleMissingDepsPathContext is a subset of *ModuleContext methods required by |
| 96 | // the Path methods that rely on module dependencies having been resolved and ability to report |
| 97 | // missing dependency errors. |
| 98 | type ModuleMissingDepsPathContext interface { |
| 99 | ModuleWithDepsPathContext |
| 100 | AddMissingDependencies(missingDeps []string) |
| 101 | } |
| 102 | |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 103 | type ModuleInstallPathContext interface { |
Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 104 | BaseModuleContext |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 105 | |
| 106 | InstallInData() bool |
Jaewoong Jung | 0949f31 | 2019-09-11 10:25:18 -0700 | [diff] [blame] | 107 | InstallInTestcases() bool |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 108 | InstallInSanitizerDir() bool |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 109 | InstallInRamdisk() bool |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 110 | InstallInVendorRamdisk() bool |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 111 | InstallInDebugRamdisk() bool |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 112 | InstallInRecovery() bool |
Colin Cross | 90ba5f4 | 2019-10-02 11:10:58 -0700 | [diff] [blame] | 113 | InstallInRoot() bool |
Colin Cross | 607d858 | 2019-07-29 16:44:46 -0700 | [diff] [blame] | 114 | InstallBypassMake() bool |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 115 | InstallForceOS() (*OsType, *ArchType) |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | var _ ModuleInstallPathContext = ModuleContext(nil) |
| 119 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 120 | // errorfContext is the interface containing the Errorf method matching the |
| 121 | // Errorf method in blueprint.SingletonContext. |
| 122 | type errorfContext interface { |
| 123 | Errorf(format string, args ...interface{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 126 | var _ errorfContext = blueprint.SingletonContext(nil) |
| 127 | |
| 128 | // moduleErrorf is the interface containing the ModuleErrorf method matching |
| 129 | // the ModuleErrorf method in blueprint.ModuleContext. |
| 130 | type moduleErrorf interface { |
| 131 | ModuleErrorf(format string, args ...interface{}) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 134 | var _ moduleErrorf = blueprint.ModuleContext(nil) |
| 135 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 136 | // reportPathError will register an error with the attached context. It |
| 137 | // attempts ctx.ModuleErrorf for a better error message first, then falls |
| 138 | // back to ctx.Errorf. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 139 | func reportPathError(ctx PathContext, err error) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 140 | ReportPathErrorf(ctx, "%s", err.Error()) |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 143 | // ReportPathErrorf will register an error with the attached context. It |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 144 | // attempts ctx.ModuleErrorf for a better error message first, then falls |
| 145 | // back to ctx.Errorf. |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 146 | func ReportPathErrorf(ctx PathContext, format string, args ...interface{}) { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 147 | if mctx, ok := ctx.(moduleErrorf); ok { |
| 148 | mctx.ModuleErrorf(format, args...) |
| 149 | } else if ectx, ok := ctx.(errorfContext); ok { |
| 150 | ectx.Errorf(format, args...) |
| 151 | } else { |
| 152 | panic(fmt.Sprintf(format, args...)) |
Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Colin Cross | 5e70805 | 2019-08-06 13:59:50 -0700 | [diff] [blame] | 156 | func pathContextName(ctx PathContext, module blueprint.Module) string { |
| 157 | if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok { |
| 158 | return x.ModuleName(module) |
| 159 | } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok { |
| 160 | return x.OtherModuleName(module) |
| 161 | } |
| 162 | return "unknown" |
| 163 | } |
| 164 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 165 | type Path interface { |
| 166 | // Returns the path in string form |
| 167 | String() string |
| 168 | |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 169 | // Ext returns the extension of the last element of the path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 170 | Ext() string |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 171 | |
| 172 | // Base returns the last element of the path |
| 173 | Base() string |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 174 | |
| 175 | // Rel returns the portion of the path relative to the directory it was created from. For |
| 176 | // 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] | 177 | // directory, and OutputPath.Join("foo").Rel() would return "foo". |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 178 | Rel() string |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 179 | |
| 180 | // RelativeToTop returns a new path relative to the top, it is provided solely for use in tests. |
| 181 | // |
| 182 | // It is guaranteed to always return the same type as it is called on, e.g. if called on an |
| 183 | // InstallPath then the returned value can be converted to an InstallPath. |
| 184 | // |
| 185 | // A standard build has the following structure: |
| 186 | // ../top/ |
| 187 | // out/ - make install files go here. |
| 188 | // out/soong - this is the buildDir passed to NewTestConfig() |
| 189 | // ... - the source files |
| 190 | // |
| 191 | // This function converts a path so that it appears relative to the ../top/ directory, i.e. |
| 192 | // * Make install paths, which have the pattern "buildDir/../<path>" are converted into the top |
| 193 | // relative path "out/<path>" |
| 194 | // * Soong install paths and other writable paths, which have the pattern "buildDir/<path>" are |
| 195 | // converted into the top relative path "out/soong/<path>". |
| 196 | // * Source paths are already relative to the top. |
| 197 | // * Phony paths are not relative to anything. |
| 198 | // * toolDepPath have an absolute but known value in so don't need making relative to anything in |
| 199 | // order to test. |
| 200 | RelativeToTop() Path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 203 | const ( |
| 204 | OutDir = "out" |
| 205 | OutSoongDir = OutDir + "/soong" |
| 206 | ) |
| 207 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 208 | // WritablePath is a type of path that can be used as an output for build rules. |
| 209 | type WritablePath interface { |
| 210 | Path |
| 211 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 212 | // return the path to the build directory. |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 213 | getBuildDir() string |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 214 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 215 | // the writablePath method doesn't directly do anything, |
| 216 | // 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] | 217 | writablePath() |
Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 218 | |
| 219 | ReplaceExtension(ctx PathContext, ext string) OutputPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | type genPathProvider interface { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 223 | genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 224 | } |
| 225 | type objPathProvider interface { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 226 | objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 227 | } |
| 228 | type resPathProvider interface { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 229 | resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | // GenPathWithExt derives a new file path in ctx's generated sources directory |
| 233 | // from the current path, but with the new extension. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 234 | func GenPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 235 | if path, ok := p.(genPathProvider); ok { |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 236 | return path.genPathWithExt(ctx, subdir, ext) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 237 | } |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 238 | 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] | 239 | return PathForModuleGen(ctx) |
| 240 | } |
| 241 | |
| 242 | // ObjPathWithExt derives a new file path in ctx's object directory from the |
| 243 | // current path, but with the new extension. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 244 | func ObjPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 245 | if path, ok := p.(objPathProvider); ok { |
| 246 | return path.objPathWithExt(ctx, subdir, ext) |
| 247 | } |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 248 | 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] | 249 | return PathForModuleObj(ctx) |
| 250 | } |
| 251 | |
| 252 | // ResPathWithName derives a new path in ctx's output resource directory, using |
| 253 | // the current path to create the directory name, and the `name` argument for |
| 254 | // the filename. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 255 | func ResPathWithName(ctx ModuleOutPathContext, p Path, name string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 256 | if path, ok := p.(resPathProvider); ok { |
| 257 | return path.resPathWithName(ctx, name) |
| 258 | } |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 259 | 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] | 260 | return PathForModuleRes(ctx) |
| 261 | } |
| 262 | |
| 263 | // OptionalPath is a container that may or may not contain a valid Path. |
| 264 | type OptionalPath struct { |
| 265 | valid bool |
| 266 | path Path |
| 267 | } |
| 268 | |
| 269 | // OptionalPathForPath returns an OptionalPath containing the path. |
| 270 | func OptionalPathForPath(path Path) OptionalPath { |
| 271 | if path == nil { |
| 272 | return OptionalPath{} |
| 273 | } |
| 274 | return OptionalPath{valid: true, path: path} |
| 275 | } |
| 276 | |
| 277 | // Valid returns whether there is a valid path |
| 278 | func (p OptionalPath) Valid() bool { |
| 279 | return p.valid |
| 280 | } |
| 281 | |
| 282 | // Path returns the Path embedded in this OptionalPath. You must be sure that |
| 283 | // there is a valid path, since this method will panic if there is not. |
| 284 | func (p OptionalPath) Path() Path { |
| 285 | if !p.valid { |
| 286 | panic("Requesting an invalid path") |
| 287 | } |
| 288 | return p.path |
| 289 | } |
| 290 | |
Paul Duffin | ef08185 | 2021-05-13 11:11:15 +0100 | [diff] [blame] | 291 | // AsPaths converts the OptionalPath into Paths. |
| 292 | // |
| 293 | // It returns nil if this is not valid, or a single length slice containing the Path embedded in |
| 294 | // this OptionalPath. |
| 295 | func (p OptionalPath) AsPaths() Paths { |
| 296 | if !p.valid { |
| 297 | return nil |
| 298 | } |
| 299 | return Paths{p.path} |
| 300 | } |
| 301 | |
Paul Duffin | afdd406 | 2021-03-30 19:44:07 +0100 | [diff] [blame] | 302 | // RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the |
| 303 | // result of calling Path.RelativeToTop on it. |
| 304 | func (p OptionalPath) RelativeToTop() OptionalPath { |
Paul Duffin | a5b8135 | 2021-03-28 23:57:19 +0100 | [diff] [blame] | 305 | if !p.valid { |
| 306 | return p |
| 307 | } |
| 308 | p.path = p.path.RelativeToTop() |
| 309 | return p |
| 310 | } |
| 311 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 312 | // String returns the string version of the Path, or "" if it isn't valid. |
| 313 | func (p OptionalPath) String() string { |
| 314 | if p.valid { |
| 315 | return p.path.String() |
| 316 | } else { |
| 317 | return "" |
Colin Cross | f229827 | 2015-05-12 11:36:53 -0700 | [diff] [blame] | 318 | } |
| 319 | } |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 320 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 321 | // Paths is a slice of Path objects, with helpers to operate on the collection. |
| 322 | type Paths []Path |
| 323 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 324 | // RelativeToTop creates a new Paths containing the result of calling Path.RelativeToTop on each |
| 325 | // item in this slice. |
| 326 | func (p Paths) RelativeToTop() Paths { |
| 327 | ensureTestOnly() |
| 328 | if p == nil { |
| 329 | return p |
| 330 | } |
| 331 | ret := make(Paths, len(p)) |
| 332 | for i, path := range p { |
| 333 | ret[i] = path.RelativeToTop() |
| 334 | } |
| 335 | return ret |
| 336 | } |
| 337 | |
Jingwen Chen | 40fd90a | 2020-06-15 05:24:19 +0000 | [diff] [blame] | 338 | func (paths Paths) containsPath(path Path) bool { |
| 339 | for _, p := range paths { |
| 340 | if p == path { |
| 341 | return true |
| 342 | } |
| 343 | } |
| 344 | return false |
| 345 | } |
| 346 | |
Liz Kammer | 7aa5288 | 2021-02-11 09:16:14 -0500 | [diff] [blame] | 347 | // PathsForSource returns Paths rooted from SrcDir, *not* rooted from the module's local source |
| 348 | // directory |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 349 | func PathsForSource(ctx PathContext, paths []string) Paths { |
| 350 | ret := make(Paths, len(paths)) |
| 351 | for i, path := range paths { |
| 352 | ret[i] = PathForSource(ctx, path) |
| 353 | } |
| 354 | return ret |
| 355 | } |
| 356 | |
Liz Kammer | 7aa5288 | 2021-02-11 09:16:14 -0500 | [diff] [blame] | 357 | // ExistentPathsForSources returns a list of Paths rooted from SrcDir, *not* rooted from the |
| 358 | // module's local source directory, that are found in the tree. If any are not found, they are |
| 359 | // omitted from the list, 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] | 360 | func ExistentPathsForSources(ctx PathContext, paths []string) Paths { |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 361 | ret := make(Paths, 0, len(paths)) |
| 362 | for _, path := range paths { |
Colin Cross | 32f3898 | 2018-02-22 11:47:25 -0800 | [diff] [blame] | 363 | p := ExistentPathForSource(ctx, path) |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 364 | if p.Valid() { |
| 365 | ret = append(ret, p.Path()) |
| 366 | } |
| 367 | } |
| 368 | return ret |
| 369 | } |
| 370 | |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 371 | // PathsForModuleSrc returns a Paths{} containing the resolved references in paths: |
| 372 | // * filepath, relative to local module directory, resolves as a filepath relative to the local |
| 373 | // source directory |
| 374 | // * glob, relative to the local module directory, resolves as filepath(s), relative to the local |
| 375 | // source directory. |
| 376 | // * other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer |
| 377 | // or OutputFileProducer. These resolve as a filepath to an output filepath or generated source |
| 378 | // filepath. |
| 379 | // Properties passed as the paths argument must have been annotated with struct tag |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 380 | // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 381 | // path_deps mutator. |
| 382 | // If a requested module is not found as a dependency: |
| 383 | // * if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having |
| 384 | // missing dependencies |
| 385 | // * otherwise, a ModuleError is thrown. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 386 | func PathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 387 | return PathsForModuleSrcExcludes(ctx, paths, nil) |
| 388 | } |
| 389 | |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 390 | // PathsForModuleSrcExcludes returns a Paths{} containing the resolved references in paths, minus |
| 391 | // those listed in excludes. Elements of paths and excludes are resolved as: |
| 392 | // * filepath, relative to local module directory, resolves as a filepath relative to the local |
| 393 | // source directory |
| 394 | // * glob, relative to the local module directory, resolves as filepath(s), relative to the local |
| 395 | // source directory. Not valid in excludes. |
| 396 | // * other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer |
| 397 | // or OutputFileProducer. These resolve as a filepath to an output filepath or generated source |
| 398 | // filepath. |
| 399 | // excluding the items (similarly resolved |
| 400 | // Properties passed as the paths argument must have been annotated with struct tag |
| 401 | // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the |
| 402 | // path_deps mutator. |
| 403 | // If a requested module is not found as a dependency: |
| 404 | // * if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having |
| 405 | // missing dependencies |
| 406 | // * otherwise, a ModuleError is thrown. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 407 | func PathsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) Paths { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 408 | ret, missingDeps := PathsAndMissingDepsForModuleSrcExcludes(ctx, paths, excludes) |
| 409 | if ctx.Config().AllowMissingDependencies() { |
| 410 | ctx.AddMissingDependencies(missingDeps) |
| 411 | } else { |
| 412 | for _, m := range missingDeps { |
| 413 | ctx.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m) |
| 414 | } |
| 415 | } |
| 416 | return ret |
| 417 | } |
| 418 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 419 | // OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection. |
| 420 | type OutputPaths []OutputPath |
| 421 | |
| 422 | // Paths returns the OutputPaths as a Paths |
| 423 | func (p OutputPaths) Paths() Paths { |
| 424 | if p == nil { |
| 425 | return nil |
| 426 | } |
| 427 | ret := make(Paths, len(p)) |
| 428 | for i, path := range p { |
| 429 | ret[i] = path |
| 430 | } |
| 431 | return ret |
| 432 | } |
| 433 | |
| 434 | // Strings returns the string forms of the writable paths. |
| 435 | func (p OutputPaths) Strings() []string { |
| 436 | if p == nil { |
| 437 | return nil |
| 438 | } |
| 439 | ret := make([]string, len(p)) |
| 440 | for i, path := range p { |
| 441 | ret[i] = path.String() |
| 442 | } |
| 443 | return ret |
| 444 | } |
| 445 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 446 | // Expands Paths to a SourceFileProducer or OutputFileProducer module dependency referenced via ":name" or ":name{.tag}" syntax. |
| 447 | // If the dependency is not found, a missingErrorDependency is returned. |
| 448 | // If the module dependency is not a SourceFileProducer or OutputFileProducer, appropriate errors will be returned. |
| 449 | func getPathsFromModuleDep(ctx ModuleWithDepsPathContext, path, moduleName, tag string) (Paths, error) { |
Paul Duffin | d5cf92e | 2021-07-09 17:38:55 +0100 | [diff] [blame] | 450 | module := GetModuleFromPathDep(ctx, moduleName, tag) |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 451 | if module == nil { |
| 452 | return nil, missingDependencyError{[]string{moduleName}} |
| 453 | } |
Colin Cross | fa65cee | 2021-03-22 17:05:59 -0700 | [diff] [blame] | 454 | if aModule, ok := module.(Module); ok && !aModule.Enabled() { |
| 455 | return nil, missingDependencyError{[]string{moduleName}} |
| 456 | } |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 457 | if outProducer, ok := module.(OutputFileProducer); ok { |
| 458 | outputFiles, err := outProducer.OutputFiles(tag) |
| 459 | if err != nil { |
| 460 | return nil, fmt.Errorf("path dependency %q: %s", path, err) |
| 461 | } |
| 462 | return outputFiles, nil |
| 463 | } else if tag != "" { |
| 464 | return nil, fmt.Errorf("path dependency %q is not an output file producing module", path) |
Colin Cross | 0e44615 | 2021-05-03 13:35:32 -0700 | [diff] [blame] | 465 | } else if goBinary, ok := module.(bootstrap.GoBinaryTool); ok { |
| 466 | if rel, err := filepath.Rel(PathForOutput(ctx).String(), goBinary.InstallPath()); err == nil { |
| 467 | return Paths{PathForOutput(ctx, rel).WithoutRel()}, nil |
| 468 | } else { |
| 469 | return nil, fmt.Errorf("cannot find output path for %q: %w", goBinary.InstallPath(), err) |
| 470 | } |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 471 | } else if srcProducer, ok := module.(SourceFileProducer); ok { |
| 472 | return srcProducer.Srcs(), nil |
| 473 | } else { |
| 474 | return nil, fmt.Errorf("path dependency %q is not a source file producing module", path) |
| 475 | } |
| 476 | } |
| 477 | |
Paul Duffin | d5cf92e | 2021-07-09 17:38:55 +0100 | [diff] [blame] | 478 | // GetModuleFromPathDep will return the module that was added as a dependency automatically for |
| 479 | // properties tagged with `android:"path"` or manually using ExtractSourceDeps or |
| 480 | // ExtractSourcesDeps. |
| 481 | // |
| 482 | // The moduleName and tag supplied to this should be the values returned from SrcIsModuleWithTag. |
| 483 | // Or, if no tag is expected then the moduleName should be the value returned by SrcIsModule and |
| 484 | // the tag must be "". |
| 485 | // |
| 486 | // If tag is "" then the returned module will be the dependency that was added for ":moduleName". |
| 487 | // Otherwise, it is the dependency that was added for ":moduleName{tag}". |
Paul Duffin | d5cf92e | 2021-07-09 17:38:55 +0100 | [diff] [blame] | 488 | func GetModuleFromPathDep(ctx ModuleWithDepsPathContext, moduleName, tag string) blueprint.Module { |
Paul Duffin | 40131a3 | 2021-07-09 17:10:35 +0100 | [diff] [blame^] | 489 | var found blueprint.Module |
| 490 | // The sourceOrOutputDepTag uniquely identifies the module dependency as it contains both the |
| 491 | // module name and the tag. Dependencies added automatically for properties tagged with |
| 492 | // `android:"path"` are deduped so are guaranteed to be unique. It is possible for duplicate |
| 493 | // dependencies to be added manually using ExtractSourcesDeps or ExtractSourceDeps but even then |
| 494 | // it will always be the case that the dependencies will be identical, i.e. the same tag and same |
| 495 | // moduleName referring to the same dependency module. |
| 496 | // |
| 497 | // It does not matter whether the moduleName is a fully qualified name or if the module |
| 498 | // dependency is a prebuilt module. All that matters is the same information is supplied to |
| 499 | // create the tag here as was supplied to create the tag when the dependency was added so that |
| 500 | // this finds the matching dependency module. |
| 501 | expectedTag := sourceOrOutputDepTag(moduleName, tag) |
| 502 | ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) { |
| 503 | depTag := ctx.OtherModuleDependencyTag(module) |
| 504 | if depTag == expectedTag { |
| 505 | found = module |
| 506 | } |
| 507 | }) |
| 508 | return found |
Paul Duffin | d5cf92e | 2021-07-09 17:38:55 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 511 | // PathsAndMissingDepsForModuleSrcExcludes returns a Paths{} containing the resolved references in |
| 512 | // paths, minus those listed in excludes. Elements of paths and excludes are resolved as: |
| 513 | // * filepath, relative to local module directory, resolves as a filepath relative to the local |
| 514 | // source directory |
| 515 | // * glob, relative to the local module directory, resolves as filepath(s), relative to the local |
| 516 | // source directory. Not valid in excludes. |
| 517 | // * other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer |
| 518 | // or OutputFileProducer. These resolve as a filepath to an output filepath or generated source |
| 519 | // filepath. |
| 520 | // and a list of the module names of missing module dependencies are returned as the second return. |
| 521 | // Properties passed as the paths argument must have been annotated with struct tag |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 522 | // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 523 | // path_deps mutator. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 524 | func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleWithDepsPathContext, paths, excludes []string) (Paths, []string) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 525 | prefix := pathForModuleSrc(ctx).String() |
| 526 | |
| 527 | var expandedExcludes []string |
| 528 | if excludes != nil { |
| 529 | expandedExcludes = make([]string, 0, len(excludes)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 530 | } |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 531 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 532 | var missingExcludeDeps []string |
| 533 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 534 | for _, e := range excludes { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 535 | if m, t := SrcIsModuleWithTag(e); m != "" { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 536 | modulePaths, err := getPathsFromModuleDep(ctx, e, m, t) |
| 537 | if m, ok := err.(missingDependencyError); ok { |
| 538 | missingExcludeDeps = append(missingExcludeDeps, m.missingDeps...) |
| 539 | } else if err != nil { |
| 540 | reportPathError(ctx, err) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 541 | } else { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 542 | expandedExcludes = append(expandedExcludes, modulePaths.Strings()...) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 543 | } |
| 544 | } else { |
| 545 | expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e)) |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | if paths == nil { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 550 | return nil, missingExcludeDeps |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 553 | var missingDeps []string |
| 554 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 555 | expandedSrcFiles := make(Paths, 0, len(paths)) |
| 556 | for _, s := range paths { |
| 557 | srcFiles, err := expandOneSrcPath(ctx, s, expandedExcludes) |
| 558 | if depErr, ok := err.(missingDependencyError); ok { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 559 | missingDeps = append(missingDeps, depErr.missingDeps...) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 560 | } else if err != nil { |
| 561 | reportPathError(ctx, err) |
| 562 | } |
| 563 | expandedSrcFiles = append(expandedSrcFiles, srcFiles...) |
| 564 | } |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 565 | |
| 566 | return expandedSrcFiles, append(missingDeps, missingExcludeDeps...) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | type missingDependencyError struct { |
| 570 | missingDeps []string |
| 571 | } |
| 572 | |
| 573 | func (e missingDependencyError) Error() string { |
| 574 | return "missing dependencies: " + strings.Join(e.missingDeps, ", ") |
| 575 | } |
| 576 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 577 | // Expands one path string to Paths rooted from the module's local source |
| 578 | // directory, excluding those listed in the expandedExcludes. |
| 579 | // Expands globs, references to SourceFileProducer or OutputFileProducer modules using the ":name" and ":name{.tag}" syntax. |
| 580 | func expandOneSrcPath(ctx ModuleWithDepsPathContext, sPath string, expandedExcludes []string) (Paths, error) { |
Jooyung Han | 7607dd3 | 2020-07-05 10:23:14 +0900 | [diff] [blame] | 581 | excludePaths := func(paths Paths) Paths { |
| 582 | if len(expandedExcludes) == 0 { |
| 583 | return paths |
| 584 | } |
| 585 | remainder := make(Paths, 0, len(paths)) |
| 586 | for _, p := range paths { |
| 587 | if !InList(p.String(), expandedExcludes) { |
| 588 | remainder = append(remainder, p) |
| 589 | } |
| 590 | } |
| 591 | return remainder |
| 592 | } |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 593 | if m, t := SrcIsModuleWithTag(sPath); m != "" { |
| 594 | modulePaths, err := getPathsFromModuleDep(ctx, sPath, m, t) |
| 595 | if err != nil { |
| 596 | return nil, err |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 597 | } else { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 598 | return excludePaths(modulePaths), nil |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 599 | } |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 600 | } else if pathtools.IsGlob(sPath) { |
| 601 | paths := GlobFiles(ctx, pathForModuleSrc(ctx, sPath).String(), expandedExcludes) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 602 | return PathsWithModuleSrcSubDir(ctx, paths, ""), nil |
| 603 | } else { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 604 | p := pathForModuleSrc(ctx, sPath) |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 605 | if exists, _, err := ctx.Config().fs.Exists(p.String()); err != nil { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 606 | ReportPathErrorf(ctx, "%s: %s", p, err.Error()) |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 607 | } else if !exists && !ctx.Config().TestAllowNonExistentPaths { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 608 | ReportPathErrorf(ctx, "module source path %q does not exist", p) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 609 | } |
| 610 | |
Jooyung Han | 7607dd3 | 2020-07-05 10:23:14 +0900 | [diff] [blame] | 611 | if InList(p.String(), expandedExcludes) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 612 | return nil, nil |
| 613 | } |
| 614 | return Paths{p}, nil |
| 615 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | // pathsForModuleSrcFromFullPath returns Paths rooted from the module's local |
| 619 | // source directory, but strip the local source directory from the beginning of |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 620 | // 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] | 621 | // It intended for use in globs that only list files that exist, so it allows '$' in |
| 622 | // filenames. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 623 | func pathsForModuleSrcFromFullPath(ctx EarlyModulePathContext, paths []string, incDirs bool) Paths { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 624 | prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/" |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 625 | if prefix == "./" { |
| 626 | prefix = "" |
| 627 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 628 | ret := make(Paths, 0, len(paths)) |
| 629 | for _, p := range paths { |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 630 | if !incDirs && strings.HasSuffix(p, "/") { |
| 631 | continue |
| 632 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 633 | path := filepath.Clean(p) |
| 634 | if !strings.HasPrefix(path, prefix) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 635 | 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] | 636 | continue |
| 637 | } |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 638 | |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 639 | srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):]) |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 640 | if err != nil { |
| 641 | reportPathError(ctx, err) |
| 642 | continue |
| 643 | } |
| 644 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 645 | srcPath.basePath.rel = srcPath.path |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 646 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 647 | ret = append(ret, srcPath) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 648 | } |
| 649 | return ret |
| 650 | } |
| 651 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 652 | // PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's local source |
| 653 | // directory. If input is nil, use the default if it exists. If input is empty, returns nil. |
| 654 | func PathsWithOptionalDefaultForModuleSrc(ctx ModuleMissingDepsPathContext, input []string, def string) Paths { |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 655 | if input != nil { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 656 | return PathsForModuleSrc(ctx, input) |
| 657 | } |
| 658 | // Use Glob so that if the default doesn't exist, a dependency is added so that when it |
| 659 | // is created, we're run again. |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 660 | path := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir(), def) |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 661 | return Glob(ctx, path, nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | // Strings returns the Paths in string form |
| 665 | func (p Paths) Strings() []string { |
| 666 | if p == nil { |
| 667 | return nil |
| 668 | } |
| 669 | ret := make([]string, len(p)) |
| 670 | for i, path := range p { |
| 671 | ret[i] = path.String() |
| 672 | } |
| 673 | return ret |
| 674 | } |
| 675 | |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 676 | func CopyOfPaths(paths Paths) Paths { |
| 677 | return append(Paths(nil), paths...) |
| 678 | } |
| 679 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 680 | // FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It |
| 681 | // 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] | 682 | func FirstUniquePaths(list Paths) Paths { |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 683 | // 128 was chosen based on BenchmarkFirstUniquePaths results. |
| 684 | if len(list) > 128 { |
| 685 | return firstUniquePathsMap(list) |
| 686 | } |
| 687 | return firstUniquePathsList(list) |
| 688 | } |
| 689 | |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 690 | // SortedUniquePaths returns all unique elements of a Paths in sorted order. It modifies the |
| 691 | // 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] | 692 | func SortedUniquePaths(list Paths) Paths { |
| 693 | unique := FirstUniquePaths(list) |
| 694 | sort.Slice(unique, func(i, j int) bool { |
| 695 | return unique[i].String() < unique[j].String() |
| 696 | }) |
| 697 | return unique |
| 698 | } |
| 699 | |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 700 | func firstUniquePathsList(list Paths) Paths { |
Dan Willemsen | fe92c96 | 2017-08-29 12:28:37 -0700 | [diff] [blame] | 701 | k := 0 |
| 702 | outer: |
| 703 | for i := 0; i < len(list); i++ { |
| 704 | for j := 0; j < k; j++ { |
| 705 | if list[i] == list[j] { |
| 706 | continue outer |
| 707 | } |
| 708 | } |
| 709 | list[k] = list[i] |
| 710 | k++ |
| 711 | } |
| 712 | return list[:k] |
| 713 | } |
| 714 | |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 715 | func firstUniquePathsMap(list Paths) Paths { |
| 716 | k := 0 |
| 717 | seen := make(map[Path]bool, len(list)) |
| 718 | for i := 0; i < len(list); i++ { |
| 719 | if seen[list[i]] { |
| 720 | continue |
| 721 | } |
| 722 | seen[list[i]] = true |
| 723 | list[k] = list[i] |
| 724 | k++ |
| 725 | } |
| 726 | return list[:k] |
| 727 | } |
| 728 | |
Colin Cross | 5d58395 | 2020-11-24 16:21:24 -0800 | [diff] [blame] | 729 | // FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each. It |
| 730 | // modifies the InstallPaths slice contents in place, and returns a subslice of the original slice. |
| 731 | func FirstUniqueInstallPaths(list InstallPaths) InstallPaths { |
| 732 | // 128 was chosen based on BenchmarkFirstUniquePaths results. |
| 733 | if len(list) > 128 { |
| 734 | return firstUniqueInstallPathsMap(list) |
| 735 | } |
| 736 | return firstUniqueInstallPathsList(list) |
| 737 | } |
| 738 | |
| 739 | func firstUniqueInstallPathsList(list InstallPaths) InstallPaths { |
| 740 | k := 0 |
| 741 | outer: |
| 742 | for i := 0; i < len(list); i++ { |
| 743 | for j := 0; j < k; j++ { |
| 744 | if list[i] == list[j] { |
| 745 | continue outer |
| 746 | } |
| 747 | } |
| 748 | list[k] = list[i] |
| 749 | k++ |
| 750 | } |
| 751 | return list[:k] |
| 752 | } |
| 753 | |
| 754 | func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths { |
| 755 | k := 0 |
| 756 | seen := make(map[InstallPath]bool, len(list)) |
| 757 | for i := 0; i < len(list); i++ { |
| 758 | if seen[list[i]] { |
| 759 | continue |
| 760 | } |
| 761 | seen[list[i]] = true |
| 762 | list[k] = list[i] |
| 763 | k++ |
| 764 | } |
| 765 | return list[:k] |
| 766 | } |
| 767 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 768 | // LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It |
| 769 | // modifies the Paths slice contents in place, and returns a subslice of the original slice. |
| 770 | func LastUniquePaths(list Paths) Paths { |
| 771 | totalSkip := 0 |
| 772 | for i := len(list) - 1; i >= totalSkip; i-- { |
| 773 | skip := 0 |
| 774 | for j := i - 1; j >= totalSkip; j-- { |
| 775 | if list[i] == list[j] { |
| 776 | skip++ |
| 777 | } else { |
| 778 | list[j+skip] = list[j] |
| 779 | } |
| 780 | } |
| 781 | totalSkip += skip |
| 782 | } |
| 783 | return list[totalSkip:] |
| 784 | } |
| 785 | |
Colin Cross | a140bb0 | 2018-04-17 10:52:26 -0700 | [diff] [blame] | 786 | // ReversePaths returns a copy of a Paths in reverse order. |
| 787 | func ReversePaths(list Paths) Paths { |
| 788 | if list == nil { |
| 789 | return nil |
| 790 | } |
| 791 | ret := make(Paths, len(list)) |
| 792 | for i := range list { |
| 793 | ret[i] = list[len(list)-1-i] |
| 794 | } |
| 795 | return ret |
| 796 | } |
| 797 | |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 798 | func indexPathList(s Path, list []Path) int { |
| 799 | for i, l := range list { |
| 800 | if l == s { |
| 801 | return i |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | return -1 |
| 806 | } |
| 807 | |
| 808 | func inPathList(p Path, list []Path) bool { |
| 809 | return indexPathList(p, list) != -1 |
| 810 | } |
| 811 | |
| 812 | func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) { |
Paul Duffin | 57b9e1d | 2019-12-13 00:03:35 +0000 | [diff] [blame] | 813 | return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) }) |
| 814 | } |
| 815 | |
| 816 | func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 817 | for _, l := range list { |
Paul Duffin | 57b9e1d | 2019-12-13 00:03:35 +0000 | [diff] [blame] | 818 | if predicate(l) { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 819 | filtered = append(filtered, l) |
| 820 | } else { |
| 821 | remainder = append(remainder, l) |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | return |
| 826 | } |
| 827 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 828 | // HasExt returns true of any of the paths have extension ext, otherwise false |
| 829 | func (p Paths) HasExt(ext string) bool { |
| 830 | for _, path := range p { |
| 831 | if path.Ext() == ext { |
| 832 | return true |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | return false |
| 837 | } |
| 838 | |
| 839 | // FilterByExt returns the subset of the paths that have extension ext |
| 840 | func (p Paths) FilterByExt(ext string) Paths { |
| 841 | ret := make(Paths, 0, len(p)) |
| 842 | for _, path := range p { |
| 843 | if path.Ext() == ext { |
| 844 | ret = append(ret, path) |
| 845 | } |
| 846 | } |
| 847 | return ret |
| 848 | } |
| 849 | |
| 850 | // FilterOutByExt returns the subset of the paths that do not have extension ext |
| 851 | func (p Paths) FilterOutByExt(ext string) Paths { |
| 852 | ret := make(Paths, 0, len(p)) |
| 853 | for _, path := range p { |
| 854 | if path.Ext() != ext { |
| 855 | ret = append(ret, path) |
| 856 | } |
| 857 | } |
| 858 | return ret |
| 859 | } |
| 860 | |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 861 | // DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory |
| 862 | // (including subdirectories) are in a contiguous subslice of the list, and can be found in |
| 863 | // O(log(N)) time using a binary search on the directory prefix. |
| 864 | type DirectorySortedPaths Paths |
| 865 | |
| 866 | func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths { |
| 867 | ret := append(DirectorySortedPaths(nil), paths...) |
| 868 | sort.Slice(ret, func(i, j int) bool { |
| 869 | return ret[i].String() < ret[j].String() |
| 870 | }) |
| 871 | return ret |
| 872 | } |
| 873 | |
| 874 | // PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries |
| 875 | // that are in the specified directory and its subdirectories. |
| 876 | func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths { |
| 877 | prefix := filepath.Clean(dir) + "/" |
| 878 | start := sort.Search(len(p), func(i int) bool { |
| 879 | return prefix < p[i].String() |
| 880 | }) |
| 881 | |
| 882 | ret := p[start:] |
| 883 | |
| 884 | end := sort.Search(len(ret), func(i int) bool { |
| 885 | return !strings.HasPrefix(ret[i].String(), prefix) |
| 886 | }) |
| 887 | |
| 888 | ret = ret[:end] |
| 889 | |
| 890 | return Paths(ret) |
| 891 | } |
| 892 | |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 893 | // WritablePaths is a slice of WritablePath, used for multiple outputs. |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 894 | type WritablePaths []WritablePath |
| 895 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 896 | // RelativeToTop creates a new WritablePaths containing the result of calling Path.RelativeToTop on |
| 897 | // each item in this slice. |
| 898 | func (p WritablePaths) RelativeToTop() WritablePaths { |
| 899 | ensureTestOnly() |
| 900 | if p == nil { |
| 901 | return p |
| 902 | } |
| 903 | ret := make(WritablePaths, len(p)) |
| 904 | for i, path := range p { |
| 905 | ret[i] = path.RelativeToTop().(WritablePath) |
| 906 | } |
| 907 | return ret |
| 908 | } |
| 909 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 910 | // Strings returns the string forms of the writable paths. |
| 911 | func (p WritablePaths) Strings() []string { |
| 912 | if p == nil { |
| 913 | return nil |
| 914 | } |
| 915 | ret := make([]string, len(p)) |
| 916 | for i, path := range p { |
| 917 | ret[i] = path.String() |
| 918 | } |
| 919 | return ret |
| 920 | } |
| 921 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 922 | // Paths returns the WritablePaths as a Paths |
| 923 | func (p WritablePaths) Paths() Paths { |
| 924 | if p == nil { |
| 925 | return nil |
| 926 | } |
| 927 | ret := make(Paths, len(p)) |
| 928 | for i, path := range p { |
| 929 | ret[i] = path |
| 930 | } |
| 931 | return ret |
| 932 | } |
| 933 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 934 | type basePath struct { |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 935 | path string |
| 936 | rel string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | func (p basePath) Ext() string { |
| 940 | return filepath.Ext(p.path) |
| 941 | } |
| 942 | |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 943 | func (p basePath) Base() string { |
| 944 | return filepath.Base(p.path) |
| 945 | } |
| 946 | |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 947 | func (p basePath) Rel() string { |
| 948 | if p.rel != "" { |
| 949 | return p.rel |
| 950 | } |
| 951 | return p.path |
| 952 | } |
| 953 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 954 | func (p basePath) String() string { |
| 955 | return p.path |
| 956 | } |
| 957 | |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 958 | func (p basePath) withRel(rel string) basePath { |
| 959 | p.path = filepath.Join(p.path, rel) |
| 960 | p.rel = rel |
| 961 | return p |
| 962 | } |
| 963 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 964 | // SourcePath is a Path representing a file path rooted from SrcDir |
| 965 | type SourcePath struct { |
| 966 | basePath |
Paul Duffin | 580efc8 | 2021-03-24 09:04:03 +0000 | [diff] [blame] | 967 | |
| 968 | // The sources root, i.e. Config.SrcDir() |
| 969 | srcDir string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 970 | } |
| 971 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 972 | func (p SourcePath) RelativeToTop() Path { |
| 973 | ensureTestOnly() |
| 974 | return p |
| 975 | } |
| 976 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 977 | var _ Path = SourcePath{} |
| 978 | |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 979 | func (p SourcePath) withRel(rel string) SourcePath { |
| 980 | p.basePath = p.basePath.withRel(rel) |
| 981 | return p |
| 982 | } |
| 983 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 984 | // safePathForSource is for paths that we expect are safe -- only for use by go |
| 985 | // code that is embedding ninja variables in paths |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 986 | func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { |
| 987 | p, err := validateSafePath(pathComponents...) |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 988 | ret := SourcePath{basePath{p, ""}, ctx.Config().srcDir} |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 989 | if err != nil { |
| 990 | return ret, err |
| 991 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 992 | |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 993 | // absolute path already checked by validateSafePath |
| 994 | if strings.HasPrefix(ret.String(), ctx.Config().buildDir) { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 995 | return ret, fmt.Errorf("source path %q is in output", ret.String()) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 996 | } |
| 997 | |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 998 | return ret, err |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1001 | // pathForSource creates a SourcePath from pathComponents, but does not check that it exists. |
| 1002 | func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1003 | p, err := validatePath(pathComponents...) |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1004 | ret := SourcePath{basePath{p, ""}, ctx.Config().srcDir} |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1005 | if err != nil { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1006 | return ret, err |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 1009 | // absolute path already checked by validatePath |
| 1010 | if strings.HasPrefix(ret.String(), ctx.Config().buildDir) { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 1011 | return ret, fmt.Errorf("source path %q is in output", ret.String()) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1014 | return ret, nil |
| 1015 | } |
| 1016 | |
| 1017 | // existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the |
| 1018 | // path does not exist. |
| 1019 | func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) { |
| 1020 | var files []string |
| 1021 | |
| 1022 | if gctx, ok := ctx.(PathGlobContext); ok { |
| 1023 | // Use glob to produce proper dependencies, even though we only want |
| 1024 | // a single file. |
| 1025 | files, err = gctx.GlobWithDeps(path.String(), nil) |
| 1026 | } else { |
Colin Cross | 82ea3fb | 2021-04-05 17:48:26 -0700 | [diff] [blame] | 1027 | var result pathtools.GlobResult |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1028 | // We cannot add build statements in this context, so we fall back to |
| 1029 | // AddNinjaFileDeps |
Colin Cross | 82ea3fb | 2021-04-05 17:48:26 -0700 | [diff] [blame] | 1030 | result, err = ctx.Config().fs.Glob(path.String(), nil, pathtools.FollowSymlinks) |
| 1031 | ctx.AddNinjaFileDeps(result.Deps...) |
| 1032 | files = result.Matches |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | if err != nil { |
| 1036 | return false, fmt.Errorf("glob: %s", err.Error()) |
| 1037 | } |
| 1038 | |
| 1039 | return len(files) > 0, nil |
| 1040 | } |
| 1041 | |
| 1042 | // PathForSource joins the provided path components and validates that the result |
| 1043 | // neither escapes the source dir nor is in the out dir. |
| 1044 | // On error, it will return a usable, but invalid SourcePath, and report a ModuleError. |
| 1045 | func PathForSource(ctx PathContext, pathComponents ...string) SourcePath { |
| 1046 | path, err := pathForSource(ctx, pathComponents...) |
| 1047 | if err != nil { |
| 1048 | reportPathError(ctx, err) |
| 1049 | } |
| 1050 | |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 1051 | if pathtools.IsGlob(path.String()) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1052 | ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1055 | if modCtx, ok := ctx.(ModuleMissingDepsPathContext); ok && ctx.Config().AllowMissingDependencies() { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1056 | exists, err := existsWithDependencies(ctx, path) |
| 1057 | if err != nil { |
| 1058 | reportPathError(ctx, err) |
| 1059 | } |
| 1060 | if !exists { |
| 1061 | modCtx.AddMissingDependencies([]string{path.String()}) |
| 1062 | } |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 1063 | } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1064 | ReportPathErrorf(ctx, "%s: %s", path, err.Error()) |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 1065 | } else if !exists && !ctx.Config().TestAllowNonExistentPaths { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1066 | ReportPathErrorf(ctx, "source path %q does not exist", path) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1067 | } |
| 1068 | return path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1069 | } |
| 1070 | |
Liz Kammer | 7aa5288 | 2021-02-11 09:16:14 -0500 | [diff] [blame] | 1071 | // ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not* |
| 1072 | // rooted from the module's local source directory, if the path exists, or an empty OptionalPath if |
| 1073 | // it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state |
| 1074 | // of the path changes. |
Colin Cross | 32f3898 | 2018-02-22 11:47:25 -0800 | [diff] [blame] | 1075 | func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1076 | path, err := pathForSource(ctx, pathComponents...) |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1077 | if err != nil { |
| 1078 | reportPathError(ctx, err) |
| 1079 | return OptionalPath{} |
| 1080 | } |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1081 | |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 1082 | if pathtools.IsGlob(path.String()) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1083 | ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 1084 | return OptionalPath{} |
| 1085 | } |
| 1086 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1087 | exists, err := existsWithDependencies(ctx, path) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1088 | if err != nil { |
| 1089 | reportPathError(ctx, err) |
| 1090 | return OptionalPath{} |
| 1091 | } |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1092 | if !exists { |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1093 | return OptionalPath{} |
| 1094 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1095 | return OptionalPathForPath(path) |
| 1096 | } |
| 1097 | |
| 1098 | func (p SourcePath) String() string { |
Paul Duffin | 580efc8 | 2021-03-24 09:04:03 +0000 | [diff] [blame] | 1099 | return filepath.Join(p.srcDir, p.path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | // Join creates a new SourcePath with paths... joined with the current path. The |
| 1103 | // provided paths... may not use '..' to escape from the current path. |
| 1104 | func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1105 | path, err := validatePath(paths...) |
| 1106 | if err != nil { |
| 1107 | reportPathError(ctx, err) |
| 1108 | } |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1109 | return p.withRel(path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1112 | // join is like Join but does less path validation. |
| 1113 | func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath { |
| 1114 | path, err := validateSafePath(paths...) |
| 1115 | if err != nil { |
| 1116 | reportPathError(ctx, err) |
| 1117 | } |
| 1118 | return p.withRel(path) |
| 1119 | } |
| 1120 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1121 | // OverlayPath returns the overlay for `path' if it exists. This assumes that the |
| 1122 | // SourcePath is the path to a resource overlay directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1123 | func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1124 | var relDir string |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1125 | if srcPath, ok := path.(SourcePath); ok { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1126 | relDir = srcPath.path |
| 1127 | } else { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1128 | 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] | 1129 | return OptionalPath{} |
| 1130 | } |
Paul Duffin | 580efc8 | 2021-03-24 09:04:03 +0000 | [diff] [blame] | 1131 | dir := filepath.Join(p.srcDir, p.path, relDir) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1132 | // 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] | 1133 | if pathtools.IsGlob(dir) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1134 | ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir) |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 1135 | } |
Colin Cross | 461b445 | 2018-02-23 09:22:42 -0800 | [diff] [blame] | 1136 | paths, err := ctx.GlobWithDeps(dir, nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1137 | if err != nil { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1138 | ReportPathErrorf(ctx, "glob: %s", err.Error()) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1139 | return OptionalPath{} |
| 1140 | } |
| 1141 | if len(paths) == 0 { |
| 1142 | return OptionalPath{} |
| 1143 | } |
Paul Duffin | 580efc8 | 2021-03-24 09:04:03 +0000 | [diff] [blame] | 1144 | relPath := Rel(ctx, p.srcDir, paths[0]) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1145 | return OptionalPathForPath(PathForSource(ctx, relPath)) |
| 1146 | } |
| 1147 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1148 | // 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] | 1149 | type OutputPath struct { |
| 1150 | basePath |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1151 | |
| 1152 | // The soong build directory, i.e. Config.BuildDir() |
| 1153 | buildDir string |
| 1154 | |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1155 | fullPath string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1158 | func (p OutputPath) withRel(rel string) OutputPath { |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1159 | p.basePath = p.basePath.withRel(rel) |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1160 | p.fullPath = filepath.Join(p.fullPath, rel) |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1161 | return p |
| 1162 | } |
| 1163 | |
Colin Cross | 3063b78 | 2018-08-15 11:19:12 -0700 | [diff] [blame] | 1164 | func (p OutputPath) WithoutRel() OutputPath { |
| 1165 | p.basePath.rel = filepath.Base(p.basePath.path) |
| 1166 | return p |
| 1167 | } |
| 1168 | |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1169 | func (p OutputPath) getBuildDir() string { |
| 1170 | return p.buildDir |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1173 | func (p OutputPath) RelativeToTop() Path { |
| 1174 | return p.outputPathRelativeToTop() |
| 1175 | } |
| 1176 | |
| 1177 | func (p OutputPath) outputPathRelativeToTop() OutputPath { |
| 1178 | p.fullPath = StringPathRelativeToTop(p.buildDir, p.fullPath) |
| 1179 | p.buildDir = OutSoongDir |
| 1180 | return p |
| 1181 | } |
| 1182 | |
Paul Duffin | 0267d49 | 2021-02-02 10:05:52 +0000 | [diff] [blame] | 1183 | func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
| 1184 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1185 | } |
| 1186 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1187 | var _ Path = OutputPath{} |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1188 | var _ WritablePath = OutputPath{} |
Paul Duffin | 0267d49 | 2021-02-02 10:05:52 +0000 | [diff] [blame] | 1189 | var _ objPathProvider = OutputPath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1190 | |
Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 1191 | // toolDepPath is a Path representing a dependency of the build tool. |
| 1192 | type toolDepPath struct { |
| 1193 | basePath |
| 1194 | } |
| 1195 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1196 | func (t toolDepPath) RelativeToTop() Path { |
| 1197 | ensureTestOnly() |
| 1198 | return t |
| 1199 | } |
| 1200 | |
Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 1201 | var _ Path = toolDepPath{} |
| 1202 | |
| 1203 | // pathForBuildToolDep returns a toolDepPath representing the given path string. |
| 1204 | // There is no validation for the path, as it is "trusted": It may fail |
| 1205 | // normal validation checks. For example, it may be an absolute path. |
| 1206 | // Only use this function to construct paths for dependencies of the build |
| 1207 | // tool invocation. |
| 1208 | func pathForBuildToolDep(ctx PathContext, path string) toolDepPath { |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1209 | return toolDepPath{basePath{path, ""}} |
Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 1210 | } |
| 1211 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1212 | // PathForOutput joins the provided paths and returns an OutputPath that is |
| 1213 | // validated to not escape the build dir. |
| 1214 | // On error, it will return a usable, but invalid OutputPath, and report a ModuleError. |
| 1215 | func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1216 | path, err := validatePath(pathComponents...) |
| 1217 | if err != nil { |
| 1218 | reportPathError(ctx, err) |
| 1219 | } |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1220 | fullPath := filepath.Join(ctx.Config().buildDir, path) |
| 1221 | path = fullPath[len(fullPath)-len(path):] |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1222 | return OutputPath{basePath{path, ""}, ctx.Config().buildDir, fullPath} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1225 | // PathsForOutput returns Paths rooted from buildDir |
| 1226 | func PathsForOutput(ctx PathContext, paths []string) WritablePaths { |
| 1227 | ret := make(WritablePaths, len(paths)) |
| 1228 | for i, path := range paths { |
| 1229 | ret[i] = PathForOutput(ctx, path) |
| 1230 | } |
| 1231 | return ret |
| 1232 | } |
| 1233 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1234 | func (p OutputPath) writablePath() {} |
| 1235 | |
| 1236 | func (p OutputPath) String() string { |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1237 | return p.fullPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | // Join creates a new OutputPath with paths... joined with the current path. The |
| 1241 | // provided paths... may not use '..' to escape from the current path. |
| 1242 | func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1243 | path, err := validatePath(paths...) |
| 1244 | if err != nil { |
| 1245 | reportPathError(ctx, err) |
| 1246 | } |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1247 | return p.withRel(path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1250 | // ReplaceExtension creates a new OutputPath with the extension replaced with ext. |
| 1251 | func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { |
| 1252 | if strings.Contains(ext, "/") { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1253 | ReportPathErrorf(ctx, "extension %q cannot contain /", ext) |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1254 | } |
| 1255 | ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext)) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1256 | ret.rel = pathtools.ReplaceExtension(p.rel, ext) |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1257 | return ret |
| 1258 | } |
| 1259 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1260 | // InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths. |
| 1261 | func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath { |
| 1262 | path, err := validatePath(paths...) |
| 1263 | if err != nil { |
| 1264 | reportPathError(ctx, err) |
| 1265 | } |
| 1266 | |
| 1267 | ret := PathForOutput(ctx, filepath.Dir(p.path), path) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1268 | ret.rel = filepath.Join(filepath.Dir(p.rel), path) |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1269 | return ret |
| 1270 | } |
| 1271 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1272 | // PathForIntermediates returns an OutputPath representing the top-level |
| 1273 | // intermediates directory. |
| 1274 | func PathForIntermediates(ctx PathContext, paths ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1275 | path, err := validatePath(paths...) |
| 1276 | if err != nil { |
| 1277 | reportPathError(ctx, err) |
| 1278 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1279 | return PathForOutput(ctx, ".intermediates", path) |
| 1280 | } |
| 1281 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1282 | var _ genPathProvider = SourcePath{} |
| 1283 | var _ objPathProvider = SourcePath{} |
| 1284 | var _ resPathProvider = SourcePath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1285 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1286 | // PathForModuleSrc returns a Path representing the paths... under the |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1287 | // module's local source directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1288 | func PathForModuleSrc(ctx ModuleMissingDepsPathContext, pathComponents ...string) Path { |
Paul Duffin | 407501b | 2021-07-09 16:56:35 +0100 | [diff] [blame] | 1289 | // Just join the components textually just to make sure that it does not corrupt a fully qualified |
| 1290 | // module reference, e.g. if the pathComponents is "://other:foo" then using filepath.Join() or |
| 1291 | // validatePath() will corrupt it, e.g. replace "//" with "/". If the path is not a module |
| 1292 | // reference then it will be validated by expandOneSrcPath anyway when it calls expandOneSrcPath. |
| 1293 | p := strings.Join(pathComponents, string(filepath.Separator)) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1294 | paths, err := expandOneSrcPath(ctx, p, nil) |
| 1295 | if err != nil { |
| 1296 | if depErr, ok := err.(missingDependencyError); ok { |
| 1297 | if ctx.Config().AllowMissingDependencies() { |
| 1298 | ctx.AddMissingDependencies(depErr.missingDeps) |
| 1299 | } else { |
| 1300 | ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error()) |
| 1301 | } |
| 1302 | } else { |
| 1303 | reportPathError(ctx, err) |
| 1304 | } |
| 1305 | return nil |
| 1306 | } else if len(paths) == 0 { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1307 | ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1308 | return nil |
| 1309 | } else if len(paths) > 1 { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1310 | ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths)) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1311 | } |
| 1312 | return paths[0] |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1315 | func pathForModuleSrc(ctx EarlyModulePathContext, paths ...string) SourcePath { |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1316 | p, err := validatePath(paths...) |
| 1317 | if err != nil { |
| 1318 | reportPathError(ctx, err) |
| 1319 | } |
| 1320 | |
| 1321 | path, err := pathForSource(ctx, ctx.ModuleDir(), p) |
| 1322 | if err != nil { |
| 1323 | reportPathError(ctx, err) |
| 1324 | } |
| 1325 | |
| 1326 | path.basePath.rel = p |
| 1327 | |
| 1328 | return path |
| 1329 | } |
| 1330 | |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1331 | // PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path |
| 1332 | // will return the path relative to subDir in the module's source directory. If any input paths are not located |
| 1333 | // inside subDir then a path error will be reported. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1334 | func PathsWithModuleSrcSubDir(ctx EarlyModulePathContext, paths Paths, subDir string) Paths { |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1335 | paths = append(Paths(nil), paths...) |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1336 | subDirFullPath := pathForModuleSrc(ctx, subDir) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1337 | for i, path := range paths { |
| 1338 | rel := Rel(ctx, subDirFullPath.String(), path.String()) |
| 1339 | paths[i] = subDirFullPath.join(ctx, rel) |
| 1340 | } |
| 1341 | return paths |
| 1342 | } |
| 1343 | |
| 1344 | // PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the |
| 1345 | // module's source directory. If the input path is not located inside subDir then a path error will be reported. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1346 | func PathWithModuleSrcSubDir(ctx EarlyModulePathContext, path Path, subDir string) Path { |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1347 | subDirFullPath := pathForModuleSrc(ctx, subDir) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1348 | rel := Rel(ctx, subDirFullPath.String(), path.String()) |
| 1349 | return subDirFullPath.Join(ctx, rel) |
| 1350 | } |
| 1351 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1352 | // OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a |
| 1353 | // valid path if p is non-nil. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1354 | func OptionalPathForModuleSrc(ctx ModuleMissingDepsPathContext, p *string) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1355 | if p == nil { |
| 1356 | return OptionalPath{} |
| 1357 | } |
| 1358 | return OptionalPathForPath(PathForModuleSrc(ctx, *p)) |
| 1359 | } |
| 1360 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1361 | func (p SourcePath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 1362 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1365 | func (p SourcePath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 1366 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1369 | func (p SourcePath) resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1370 | // TODO: Use full directory if the new ctx is not the current ctx? |
| 1371 | return PathForModuleRes(ctx, p.path, name) |
| 1372 | } |
| 1373 | |
| 1374 | // ModuleOutPath is a Path representing a module's output directory. |
| 1375 | type ModuleOutPath struct { |
| 1376 | OutputPath |
| 1377 | } |
| 1378 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1379 | func (p ModuleOutPath) RelativeToTop() Path { |
| 1380 | p.OutputPath = p.outputPathRelativeToTop() |
| 1381 | return p |
| 1382 | } |
| 1383 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1384 | var _ Path = ModuleOutPath{} |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1385 | var _ WritablePath = ModuleOutPath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1386 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1387 | func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
Pete Bentley | fcf55bf | 2019-08-16 20:14:32 +0100 | [diff] [blame] | 1388 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1389 | } |
| 1390 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1391 | // ModuleOutPathContext Subset of ModuleContext functions necessary for output path methods. |
| 1392 | type ModuleOutPathContext interface { |
| 1393 | PathContext |
| 1394 | |
| 1395 | ModuleName() string |
| 1396 | ModuleDir() string |
| 1397 | ModuleSubDir() string |
| 1398 | } |
| 1399 | |
| 1400 | func pathForModuleOut(ctx ModuleOutPathContext) OutputPath { |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1401 | return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir()) |
| 1402 | } |
| 1403 | |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1404 | // PathForVndkRefAbiDump returns an OptionalPath representing the path of the |
| 1405 | // reference abi dump for the given module. This is not guaranteed to be valid. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1406 | func PathForVndkRefAbiDump(ctx ModuleInstallPathContext, version, fileName string, |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 1407 | isNdk, isLlndkOrVndk, isGzip bool) OptionalPath { |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1408 | |
Jayant Chowdhary | ac066c6 | 2018-02-20 10:53:31 -0800 | [diff] [blame] | 1409 | arches := ctx.DeviceConfig().Arches() |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1410 | if len(arches) == 0 { |
| 1411 | panic("device build with no primary arch") |
| 1412 | } |
Jayant Chowdhary | ac066c6 | 2018-02-20 10:53:31 -0800 | [diff] [blame] | 1413 | currentArch := ctx.Arch() |
| 1414 | archNameAndVariant := currentArch.ArchType.String() |
| 1415 | if currentArch.ArchVariant != "" { |
| 1416 | archNameAndVariant += "_" + currentArch.ArchVariant |
| 1417 | } |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1418 | |
| 1419 | var dirName string |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 1420 | if isNdk { |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1421 | dirName = "ndk" |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 1422 | } else if isLlndkOrVndk { |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1423 | dirName = "vndk" |
Logan Chien | 41eabe6 | 2019-04-10 13:33:58 +0800 | [diff] [blame] | 1424 | } else { |
| 1425 | dirName = "platform" // opt-in libs |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 1426 | } |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1427 | |
Jayant Chowdhary | 34ce67d | 2018-03-08 11:00:50 -0800 | [diff] [blame] | 1428 | binderBitness := ctx.DeviceConfig().BinderBitness() |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1429 | |
| 1430 | var ext string |
| 1431 | if isGzip { |
| 1432 | ext = ".lsdump.gz" |
| 1433 | } else { |
| 1434 | ext = ".lsdump" |
| 1435 | } |
| 1436 | |
| 1437 | return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName, |
| 1438 | version, binderBitness, archNameAndVariant, "source-based", |
| 1439 | fileName+ext) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 1440 | } |
| 1441 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1442 | // PathForModuleOut returns a Path representing the paths... under the module's |
| 1443 | // output directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1444 | func PathForModuleOut(ctx ModuleOutPathContext, paths ...string) ModuleOutPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1445 | p, err := validatePath(paths...) |
| 1446 | if err != nil { |
| 1447 | reportPathError(ctx, err) |
| 1448 | } |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1449 | return ModuleOutPath{ |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1450 | OutputPath: pathForModuleOut(ctx).withRel(p), |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1451 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | // ModuleGenPath is a Path representing the 'gen' directory in a module's output |
| 1455 | // directory. Mainly used for generated sources. |
| 1456 | type ModuleGenPath struct { |
| 1457 | ModuleOutPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1460 | func (p ModuleGenPath) RelativeToTop() Path { |
| 1461 | p.OutputPath = p.outputPathRelativeToTop() |
| 1462 | return p |
| 1463 | } |
| 1464 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1465 | var _ Path = ModuleGenPath{} |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1466 | var _ WritablePath = ModuleGenPath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1467 | var _ genPathProvider = ModuleGenPath{} |
| 1468 | var _ objPathProvider = ModuleGenPath{} |
| 1469 | |
| 1470 | // PathForModuleGen returns a Path representing the paths... under the module's |
| 1471 | // `gen' directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1472 | func PathForModuleGen(ctx ModuleOutPathContext, paths ...string) ModuleGenPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1473 | p, err := validatePath(paths...) |
| 1474 | if err != nil { |
| 1475 | reportPathError(ctx, err) |
| 1476 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1477 | return ModuleGenPath{ |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1478 | ModuleOutPath: ModuleOutPath{ |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1479 | OutputPath: pathForModuleOut(ctx).withRel("gen").withRel(p), |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1480 | }, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1484 | func (p ModuleGenPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1485 | // TODO: make a different path for local vs remote generated files? |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 1486 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1489 | func (p ModuleGenPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1490 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1491 | } |
| 1492 | |
| 1493 | // ModuleObjPath is a Path representing the 'obj' directory in a module's output |
| 1494 | // directory. Used for compiled objects. |
| 1495 | type ModuleObjPath struct { |
| 1496 | ModuleOutPath |
| 1497 | } |
| 1498 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1499 | func (p ModuleObjPath) RelativeToTop() Path { |
| 1500 | p.OutputPath = p.outputPathRelativeToTop() |
| 1501 | return p |
| 1502 | } |
| 1503 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1504 | var _ Path = ModuleObjPath{} |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1505 | var _ WritablePath = ModuleObjPath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1506 | |
| 1507 | // PathForModuleObj returns a Path representing the paths... under the module's |
| 1508 | // 'obj' directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1509 | func PathForModuleObj(ctx ModuleOutPathContext, pathComponents ...string) ModuleObjPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1510 | p, err := validatePath(pathComponents...) |
| 1511 | if err != nil { |
| 1512 | reportPathError(ctx, err) |
| 1513 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1514 | return ModuleObjPath{PathForModuleOut(ctx, "obj", p)} |
| 1515 | } |
| 1516 | |
| 1517 | // ModuleResPath is a a Path representing the 'res' directory in a module's |
| 1518 | // output directory. |
| 1519 | type ModuleResPath struct { |
| 1520 | ModuleOutPath |
| 1521 | } |
| 1522 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1523 | func (p ModuleResPath) RelativeToTop() Path { |
| 1524 | p.OutputPath = p.outputPathRelativeToTop() |
| 1525 | return p |
| 1526 | } |
| 1527 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1528 | var _ Path = ModuleResPath{} |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1529 | var _ WritablePath = ModuleResPath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1530 | |
| 1531 | // PathForModuleRes returns a Path representing the paths... under the module's |
| 1532 | // 'res' directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1533 | func PathForModuleRes(ctx ModuleOutPathContext, pathComponents ...string) ModuleResPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1534 | p, err := validatePath(pathComponents...) |
| 1535 | if err != nil { |
| 1536 | reportPathError(ctx, err) |
| 1537 | } |
| 1538 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1539 | return ModuleResPath{PathForModuleOut(ctx, "res", p)} |
| 1540 | } |
| 1541 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1542 | // InstallPath is a Path representing a installed file path rooted from the build directory |
| 1543 | type InstallPath struct { |
| 1544 | basePath |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1545 | |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1546 | // The soong build directory, i.e. Config.BuildDir() |
| 1547 | buildDir string |
| 1548 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1549 | // partitionDir is the part of the InstallPath that is automatically determined according to the context. |
| 1550 | // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules. |
| 1551 | partitionDir string |
| 1552 | |
| 1553 | // makePath indicates whether this path is for Soong (false) or Make (true). |
| 1554 | makePath bool |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1557 | // Will panic if called from outside a test environment. |
| 1558 | func ensureTestOnly() { |
Martin Stjernholm | 32312eb | 2021-03-27 18:54:49 +0000 | [diff] [blame] | 1559 | if PrefixInList(os.Args, "-test.") { |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1560 | return |
| 1561 | } |
Martin Stjernholm | 32312eb | 2021-03-27 18:54:49 +0000 | [diff] [blame] | 1562 | panic(fmt.Errorf("Not in test. Command line:\n %s", strings.Join(os.Args, "\n "))) |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | func (p InstallPath) RelativeToTop() Path { |
| 1566 | ensureTestOnly() |
| 1567 | p.buildDir = OutSoongDir |
| 1568 | return p |
| 1569 | } |
| 1570 | |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1571 | func (p InstallPath) getBuildDir() string { |
| 1572 | return p.buildDir |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 1575 | func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { |
| 1576 | panic("Not implemented") |
| 1577 | } |
| 1578 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1579 | var _ Path = InstallPath{} |
| 1580 | var _ WritablePath = InstallPath{} |
| 1581 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1582 | func (p InstallPath) writablePath() {} |
| 1583 | |
| 1584 | func (p InstallPath) String() string { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1585 | if p.makePath { |
| 1586 | // Make path starts with out/ instead of out/soong. |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1587 | return filepath.Join(p.buildDir, "../", p.path) |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1588 | } else { |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1589 | return filepath.Join(p.buildDir, p.path) |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | // PartitionDir returns the path to the partition where the install path is rooted at. It is |
| 1594 | // out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules. |
| 1595 | // The ./soong is dropped if the install path is for Make. |
| 1596 | func (p InstallPath) PartitionDir() string { |
| 1597 | if p.makePath { |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1598 | return filepath.Join(p.buildDir, "../", p.partitionDir) |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1599 | } else { |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1600 | return filepath.Join(p.buildDir, p.partitionDir) |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1601 | } |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | // Join creates a new InstallPath with paths... joined with the current path. The |
| 1605 | // provided paths... may not use '..' to escape from the current path. |
| 1606 | func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath { |
| 1607 | path, err := validatePath(paths...) |
| 1608 | if err != nil { |
| 1609 | reportPathError(ctx, err) |
| 1610 | } |
| 1611 | return p.withRel(path) |
| 1612 | } |
| 1613 | |
| 1614 | func (p InstallPath) withRel(rel string) InstallPath { |
| 1615 | p.basePath = p.basePath.withRel(rel) |
| 1616 | return p |
| 1617 | } |
| 1618 | |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1619 | // ToMakePath returns a new InstallPath that points to Make's install directory instead of Soong's, |
| 1620 | // i.e. out/ instead of out/soong/. |
| 1621 | func (p InstallPath) ToMakePath() InstallPath { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1622 | p.makePath = true |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1623 | return p |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1624 | } |
| 1625 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1626 | // PathForModuleInstall returns a Path representing the install path for the |
| 1627 | // module appended with paths... |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1628 | func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath { |
Spandan Das | 5d1b929 | 2021-06-03 19:36:41 +0000 | [diff] [blame] | 1629 | os, arch := osAndArch(ctx) |
| 1630 | partition := modulePartition(ctx, os) |
| 1631 | return makePathForInstall(ctx, os, arch, partition, ctx.Debug(), pathComponents...) |
| 1632 | } |
| 1633 | |
| 1634 | // PathForModuleInPartitionInstall is similar to PathForModuleInstall but partition is provided by the caller |
| 1635 | func PathForModuleInPartitionInstall(ctx ModuleInstallPathContext, partition string, pathComponents ...string) InstallPath { |
| 1636 | os, arch := osAndArch(ctx) |
| 1637 | return makePathForInstall(ctx, os, arch, partition, ctx.Debug(), pathComponents...) |
| 1638 | } |
| 1639 | |
| 1640 | func osAndArch(ctx ModuleInstallPathContext) (OsType, ArchType) { |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1641 | os := ctx.Os() |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1642 | arch := ctx.Arch().ArchType |
| 1643 | forceOS, forceArch := ctx.InstallForceOS() |
| 1644 | if forceOS != nil { |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1645 | os = *forceOS |
| 1646 | } |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1647 | if forceArch != nil { |
| 1648 | arch = *forceArch |
| 1649 | } |
Spandan Das | 5d1b929 | 2021-06-03 19:36:41 +0000 | [diff] [blame] | 1650 | return os, arch |
| 1651 | } |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1652 | |
Spandan Das | 5d1b929 | 2021-06-03 19:36:41 +0000 | [diff] [blame] | 1653 | func makePathForInstall(ctx ModuleInstallPathContext, os OsType, arch ArchType, partition string, debug bool, pathComponents ...string) InstallPath { |
| 1654 | ret := pathForInstall(ctx, os, arch, partition, debug, pathComponents...) |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 1655 | if ctx.InstallBypassMake() && ctx.Config().KatiEnabled() { |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1656 | ret = ret.ToMakePath() |
| 1657 | } |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1658 | return ret |
| 1659 | } |
| 1660 | |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1661 | func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string, debug bool, |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1662 | pathComponents ...string) InstallPath { |
| 1663 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1664 | var partionPaths []string |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1665 | |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1666 | if os.Class == Device { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1667 | partionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1668 | } else { |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1669 | osName := os.String() |
| 1670 | if os == Linux { |
| 1671 | // instead of linux_glibc |
| 1672 | osName = "linux" |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 1673 | } |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1674 | // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH) |
| 1675 | // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem |
| 1676 | // to have a plan to fix it (see the comment in build/make/core/envsetup.mk). |
| 1677 | // Let's keep using x86 for the existing cases until we have a need to support |
| 1678 | // other architectures. |
| 1679 | archName := arch.String() |
| 1680 | if os.Class == Host && (arch == X86_64 || arch == Common) { |
| 1681 | archName = "x86" |
| 1682 | } |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1683 | partionPaths = []string{"host", osName + "-" + archName, partition} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1684 | } |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1685 | if debug { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1686 | partionPaths = append([]string{"debug"}, partionPaths...) |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame] | 1687 | } |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1688 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1689 | partionPath, err := validatePath(partionPaths...) |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1690 | if err != nil { |
| 1691 | reportPathError(ctx, err) |
| 1692 | } |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1693 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1694 | base := InstallPath{ |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1695 | basePath: basePath{partionPath, ""}, |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1696 | buildDir: ctx.Config().buildDir, |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1697 | partitionDir: partionPath, |
| 1698 | makePath: false, |
| 1699 | } |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1700 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1701 | return base.Join(ctx, pathComponents...) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 1704 | func pathForNdkOrSdkInstall(ctx PathContext, prefix string, paths []string) InstallPath { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1705 | base := InstallPath{ |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1706 | basePath: basePath{prefix, ""}, |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1707 | buildDir: ctx.Config().buildDir, |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1708 | partitionDir: prefix, |
| 1709 | makePath: false, |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1710 | } |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1711 | return base.Join(ctx, paths...) |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1712 | } |
| 1713 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 1714 | func PathForNdkInstall(ctx PathContext, paths ...string) InstallPath { |
| 1715 | return pathForNdkOrSdkInstall(ctx, "ndk", paths) |
| 1716 | } |
| 1717 | |
| 1718 | func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath { |
| 1719 | return pathForNdkOrSdkInstall(ctx, "mainline-sdks", paths) |
| 1720 | } |
| 1721 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1722 | func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1723 | rel := Rel(ctx, PathForOutput(ctx, "target", "product", ctx.Config().DeviceName()).String(), path.String()) |
| 1724 | |
| 1725 | return "/" + rel |
| 1726 | } |
| 1727 | |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1728 | func modulePartition(ctx ModuleInstallPathContext, os OsType) string { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1729 | var partition string |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1730 | if ctx.InstallInTestcases() { |
| 1731 | // "testcases" install directory can be used for host or device modules. |
Jaewoong Jung | 0949f31 | 2019-09-11 10:25:18 -0700 | [diff] [blame] | 1732 | partition = "testcases" |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1733 | } else if os.Class == Device { |
| 1734 | if ctx.InstallInData() { |
| 1735 | partition = "data" |
| 1736 | } else if ctx.InstallInRamdisk() { |
| 1737 | if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() { |
| 1738 | partition = "recovery/root/first_stage_ramdisk" |
| 1739 | } else { |
| 1740 | partition = "ramdisk" |
| 1741 | } |
| 1742 | if !ctx.InstallInRoot() { |
| 1743 | partition += "/system" |
| 1744 | } |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 1745 | } else if ctx.InstallInVendorRamdisk() { |
Yifan Hong | 39143a9 | 2020-10-26 12:43:12 -0700 | [diff] [blame] | 1746 | // The module is only available after switching root into |
| 1747 | // /first_stage_ramdisk. To expose the module before switching root |
| 1748 | // on a device without a dedicated recovery partition, install the |
| 1749 | // recovery variant. |
Yifan Hong | dd8dacc | 2020-10-21 15:40:17 -0700 | [diff] [blame] | 1750 | if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() { |
Petri Gynther | ac22956 | 2021-03-02 23:44:02 -0800 | [diff] [blame] | 1751 | partition = "vendor_ramdisk/first_stage_ramdisk" |
Yifan Hong | dd8dacc | 2020-10-21 15:40:17 -0700 | [diff] [blame] | 1752 | } else { |
Petri Gynther | ac22956 | 2021-03-02 23:44:02 -0800 | [diff] [blame] | 1753 | partition = "vendor_ramdisk" |
Yifan Hong | dd8dacc | 2020-10-21 15:40:17 -0700 | [diff] [blame] | 1754 | } |
| 1755 | if !ctx.InstallInRoot() { |
| 1756 | partition += "/system" |
| 1757 | } |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 1758 | } else if ctx.InstallInDebugRamdisk() { |
| 1759 | partition = "debug_ramdisk" |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1760 | } else if ctx.InstallInRecovery() { |
| 1761 | if ctx.InstallInRoot() { |
| 1762 | partition = "recovery/root" |
| 1763 | } else { |
| 1764 | // the layout of recovery partion is the same as that of system partition |
| 1765 | partition = "recovery/root/system" |
| 1766 | } |
| 1767 | } else if ctx.SocSpecific() { |
| 1768 | partition = ctx.DeviceConfig().VendorPath() |
| 1769 | } else if ctx.DeviceSpecific() { |
| 1770 | partition = ctx.DeviceConfig().OdmPath() |
| 1771 | } else if ctx.ProductSpecific() { |
| 1772 | partition = ctx.DeviceConfig().ProductPath() |
| 1773 | } else if ctx.SystemExtSpecific() { |
| 1774 | partition = ctx.DeviceConfig().SystemExtPath() |
| 1775 | } else if ctx.InstallInRoot() { |
| 1776 | partition = "root" |
Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1777 | } else { |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1778 | partition = "system" |
Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1779 | } |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1780 | if ctx.InstallInSanitizerDir() { |
| 1781 | partition = "data/asan/" + partition |
Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1782 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1783 | } |
| 1784 | return partition |
| 1785 | } |
| 1786 | |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1787 | type InstallPaths []InstallPath |
| 1788 | |
| 1789 | // Paths returns the InstallPaths as a Paths |
| 1790 | func (p InstallPaths) Paths() Paths { |
| 1791 | if p == nil { |
| 1792 | return nil |
| 1793 | } |
| 1794 | ret := make(Paths, len(p)) |
| 1795 | for i, path := range p { |
| 1796 | ret[i] = path |
| 1797 | } |
| 1798 | return ret |
| 1799 | } |
| 1800 | |
| 1801 | // Strings returns the string forms of the install paths. |
| 1802 | func (p InstallPaths) Strings() []string { |
| 1803 | if p == nil { |
| 1804 | return nil |
| 1805 | } |
| 1806 | ret := make([]string, len(p)) |
| 1807 | for i, path := range p { |
| 1808 | ret[i] = path.String() |
| 1809 | } |
| 1810 | return ret |
| 1811 | } |
| 1812 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1813 | // validateSafePath validates a path that we trust (may contain ninja variables). |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1814 | // Ensures that each path component does not attempt to leave its component. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1815 | func validateSafePath(pathComponents ...string) (string, error) { |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1816 | for _, path := range pathComponents { |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1817 | path := filepath.Clean(path) |
| 1818 | if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1819 | return "", fmt.Errorf("Path is outside directory: %s", path) |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1820 | } |
| 1821 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1822 | // TODO: filepath.Join isn't necessarily correct with embedded ninja |
| 1823 | // variables. '..' may remove the entire ninja variable, even if it |
| 1824 | // will be expanded to multiple nested directories. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1825 | return filepath.Join(pathComponents...), nil |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1828 | // validatePath validates that a path does not include ninja variables, and that |
| 1829 | // each path component does not attempt to leave its component. Returns a joined |
| 1830 | // version of each path component. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1831 | func validatePath(pathComponents ...string) (string, error) { |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1832 | for _, path := range pathComponents { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1833 | if strings.Contains(path, "$") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1834 | return "", fmt.Errorf("Path contains invalid character($): %s", path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1835 | } |
| 1836 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1837 | return validateSafePath(pathComponents...) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 1838 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1839 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1840 | func PathForPhony(ctx PathContext, phony string) WritablePath { |
| 1841 | if strings.ContainsAny(phony, "$/") { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1842 | ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1843 | } |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1844 | return PhonyPath{basePath{phony, ""}} |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1845 | } |
| 1846 | |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 1847 | type PhonyPath struct { |
| 1848 | basePath |
| 1849 | } |
| 1850 | |
| 1851 | func (p PhonyPath) writablePath() {} |
| 1852 | |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1853 | func (p PhonyPath) getBuildDir() string { |
| 1854 | // A phone path cannot contain any / so cannot be relative to the build directory. |
| 1855 | return "" |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1858 | func (p PhonyPath) RelativeToTop() Path { |
| 1859 | ensureTestOnly() |
| 1860 | // A phony path cannot contain any / so does not have a build directory so switching to a new |
| 1861 | // build directory has no effect so just return this path. |
| 1862 | return p |
| 1863 | } |
| 1864 | |
Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 1865 | func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { |
| 1866 | panic("Not implemented") |
| 1867 | } |
| 1868 | |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 1869 | var _ Path = PhonyPath{} |
| 1870 | var _ WritablePath = PhonyPath{} |
| 1871 | |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1872 | type testPath struct { |
| 1873 | basePath |
| 1874 | } |
| 1875 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1876 | func (p testPath) RelativeToTop() Path { |
| 1877 | ensureTestOnly() |
| 1878 | return p |
| 1879 | } |
| 1880 | |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1881 | func (p testPath) String() string { |
| 1882 | return p.path |
| 1883 | } |
| 1884 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1885 | var _ Path = testPath{} |
| 1886 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1887 | // PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from |
| 1888 | // within tests. |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1889 | func PathForTesting(paths ...string) Path { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1890 | p, err := validateSafePath(paths...) |
| 1891 | if err != nil { |
| 1892 | panic(err) |
| 1893 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1894 | return testPath{basePath{path: p, rel: p}} |
| 1895 | } |
| 1896 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1897 | // PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests. |
| 1898 | func PathsForTesting(strs ...string) Paths { |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 1899 | p := make(Paths, len(strs)) |
| 1900 | for i, s := range strs { |
| 1901 | p[i] = PathForTesting(s) |
| 1902 | } |
| 1903 | |
| 1904 | return p |
| 1905 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1906 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1907 | type testPathContext struct { |
| 1908 | config Config |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1909 | } |
| 1910 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1911 | func (x *testPathContext) Config() Config { return x.config } |
| 1912 | func (x *testPathContext) AddNinjaFileDeps(...string) {} |
| 1913 | |
| 1914 | // PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with |
| 1915 | // PathForOutput. |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 1916 | func PathContextForTesting(config Config) PathContext { |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1917 | return &testPathContext{ |
| 1918 | config: config, |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1919 | } |
| 1920 | } |
| 1921 | |
Ulya Trafimovich | ccc8c85 | 2020-10-14 11:29:07 +0100 | [diff] [blame] | 1922 | type testModuleInstallPathContext struct { |
| 1923 | baseModuleContext |
| 1924 | |
| 1925 | inData bool |
| 1926 | inTestcases bool |
| 1927 | inSanitizerDir bool |
| 1928 | inRamdisk bool |
| 1929 | inVendorRamdisk bool |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 1930 | inDebugRamdisk bool |
Ulya Trafimovich | ccc8c85 | 2020-10-14 11:29:07 +0100 | [diff] [blame] | 1931 | inRecovery bool |
| 1932 | inRoot bool |
| 1933 | forceOS *OsType |
| 1934 | forceArch *ArchType |
| 1935 | } |
| 1936 | |
| 1937 | func (m testModuleInstallPathContext) Config() Config { |
| 1938 | return m.baseModuleContext.config |
| 1939 | } |
| 1940 | |
| 1941 | func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {} |
| 1942 | |
| 1943 | func (m testModuleInstallPathContext) InstallInData() bool { |
| 1944 | return m.inData |
| 1945 | } |
| 1946 | |
| 1947 | func (m testModuleInstallPathContext) InstallInTestcases() bool { |
| 1948 | return m.inTestcases |
| 1949 | } |
| 1950 | |
| 1951 | func (m testModuleInstallPathContext) InstallInSanitizerDir() bool { |
| 1952 | return m.inSanitizerDir |
| 1953 | } |
| 1954 | |
| 1955 | func (m testModuleInstallPathContext) InstallInRamdisk() bool { |
| 1956 | return m.inRamdisk |
| 1957 | } |
| 1958 | |
| 1959 | func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool { |
| 1960 | return m.inVendorRamdisk |
| 1961 | } |
| 1962 | |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 1963 | func (m testModuleInstallPathContext) InstallInDebugRamdisk() bool { |
| 1964 | return m.inDebugRamdisk |
| 1965 | } |
| 1966 | |
Ulya Trafimovich | ccc8c85 | 2020-10-14 11:29:07 +0100 | [diff] [blame] | 1967 | func (m testModuleInstallPathContext) InstallInRecovery() bool { |
| 1968 | return m.inRecovery |
| 1969 | } |
| 1970 | |
| 1971 | func (m testModuleInstallPathContext) InstallInRoot() bool { |
| 1972 | return m.inRoot |
| 1973 | } |
| 1974 | |
| 1975 | func (m testModuleInstallPathContext) InstallBypassMake() bool { |
| 1976 | return false |
| 1977 | } |
| 1978 | |
| 1979 | func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) { |
| 1980 | return m.forceOS, m.forceArch |
| 1981 | } |
| 1982 | |
| 1983 | // Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is |
| 1984 | // default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are |
| 1985 | // delegated to it will panic. |
| 1986 | func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext { |
| 1987 | ctx := &testModuleInstallPathContext{} |
| 1988 | ctx.config = config |
| 1989 | ctx.os = Android |
| 1990 | return ctx |
| 1991 | } |
| 1992 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1993 | // Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if |
| 1994 | // targetPath is not inside basePath. |
| 1995 | func Rel(ctx PathContext, basePath string, targetPath string) string { |
| 1996 | rel, isRel := MaybeRel(ctx, basePath, targetPath) |
| 1997 | if !isRel { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1998 | ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1999 | return "" |
| 2000 | } |
| 2001 | return rel |
| 2002 | } |
| 2003 | |
| 2004 | // MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if |
| 2005 | // targetPath is not inside basePath. |
| 2006 | func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2007 | rel, isRel, err := maybeRelErr(basePath, targetPath) |
| 2008 | if err != nil { |
| 2009 | reportPathError(ctx, err) |
| 2010 | } |
| 2011 | return rel, isRel |
| 2012 | } |
| 2013 | |
| 2014 | func maybeRelErr(basePath string, targetPath string) (string, bool, error) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2015 | // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first. |
| 2016 | if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2017 | return "", false, nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2018 | } |
| 2019 | rel, err := filepath.Rel(basePath, targetPath) |
| 2020 | if err != nil { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2021 | return "", false, err |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2022 | } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2023 | return "", false, nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2024 | } |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2025 | return rel, true, nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2026 | } |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 2027 | |
| 2028 | // Writes a file to the output directory. Attempting to write directly to the output directory |
| 2029 | // will fail due to the sandbox of the soong_build process. |
| 2030 | func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error { |
| 2031 | return ioutil.WriteFile(absolutePath(path.String()), data, perm) |
| 2032 | } |
| 2033 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 2034 | func RemoveAllOutputDir(path WritablePath) error { |
| 2035 | return os.RemoveAll(absolutePath(path.String())) |
| 2036 | } |
| 2037 | |
| 2038 | func CreateOutputDirIfNonexistent(path WritablePath, perm os.FileMode) error { |
| 2039 | dir := absolutePath(path.String()) |
Liz Kammer | 09f947d | 2021-05-12 14:51:49 -0400 | [diff] [blame] | 2040 | return createDirIfNonexistent(dir, perm) |
| 2041 | } |
| 2042 | |
| 2043 | func createDirIfNonexistent(dir string, perm os.FileMode) error { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 2044 | if _, err := os.Stat(dir); os.IsNotExist(err) { |
| 2045 | return os.MkdirAll(dir, os.ModePerm) |
| 2046 | } else { |
| 2047 | return err |
| 2048 | } |
| 2049 | } |
| 2050 | |
Jingwen Chen | 78257e5 | 2021-05-21 02:34:24 +0000 | [diff] [blame] | 2051 | // absolutePath is deliberately private so that Soong's Go plugins can't use it to find and |
| 2052 | // read arbitrary files without going through the methods in the current package that track |
| 2053 | // dependencies. |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 2054 | func absolutePath(path string) string { |
| 2055 | if filepath.IsAbs(path) { |
| 2056 | return path |
| 2057 | } |
| 2058 | return filepath.Join(absSrcDir, path) |
| 2059 | } |
Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 2060 | |
| 2061 | // A DataPath represents the path of a file to be used as data, for example |
| 2062 | // a test library to be installed alongside a test. |
| 2063 | // The data file should be installed (copied from `<SrcPath>`) to |
| 2064 | // `<install_root>/<RelativeInstallPath>/<filename>`, or |
| 2065 | // `<install_root>/<filename>` if RelativeInstallPath is empty. |
| 2066 | type DataPath struct { |
| 2067 | // The path of the data file that should be copied into the data directory |
| 2068 | SrcPath Path |
| 2069 | // The install path of the data file, relative to the install root. |
| 2070 | RelativeInstallPath string |
| 2071 | } |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 2072 | |
| 2073 | // PathsIfNonNil returns a Paths containing only the non-nil input arguments. |
| 2074 | func PathsIfNonNil(paths ...Path) Paths { |
| 2075 | if len(paths) == 0 { |
| 2076 | // Fast path for empty argument list |
| 2077 | return nil |
| 2078 | } else if len(paths) == 1 { |
| 2079 | // Fast path for a single argument |
| 2080 | if paths[0] != nil { |
| 2081 | return paths |
| 2082 | } else { |
| 2083 | return nil |
| 2084 | } |
| 2085 | } |
| 2086 | ret := make(Paths, 0, len(paths)) |
| 2087 | for _, path := range paths { |
| 2088 | if path != nil { |
| 2089 | ret = append(ret, path) |
| 2090 | } |
| 2091 | } |
| 2092 | if len(ret) == 0 { |
| 2093 | return nil |
| 2094 | } |
| 2095 | return ret |
| 2096 | } |