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