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