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 ( |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 18 | "android/soong/bazel" |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 19 | "fmt" |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 20 | "io/ioutil" |
| 21 | "os" |
Colin Cross | 6a745c6 | 2015-06-16 16:38:10 -0700 | [diff] [blame] | 22 | "path/filepath" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 23 | "reflect" |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 24 | "sort" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 25 | "strings" |
| 26 | |
| 27 | "github.com/google/blueprint" |
| 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 | |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 358 | // PathsForModuleSrc returns Paths rooted from the module's local source directory. It expands globs, references to |
| 359 | // SourceFileProducer modules using the ":name" syntax, and references to OutputFileProducer modules using the |
| 360 | // ":name{.tag}" syntax. Properties passed as the paths argument must have been annotated with struct tag |
| 361 | // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the |
| 362 | // path_properties mutator. If ctx.Config().AllowMissingDependencies() is true then any missing SourceFileProducer or |
| 363 | // OutputFileProducer dependencies will cause the module to be marked as having missing dependencies. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 364 | func PathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 365 | return PathsForModuleSrcExcludes(ctx, paths, nil) |
| 366 | } |
| 367 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 368 | // PathsForModuleSrcExcludes returns Paths rooted from the module's local source directory, excluding paths listed in |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 369 | // the excludes arguments. It expands globs, references to SourceFileProducer modules using the ":name" syntax, and |
| 370 | // references to OutputFileProducer modules using the ":name{.tag}" syntax. Properties passed as the paths or excludes |
| 371 | // argument must have been annotated with struct tag `android:"path"` so that dependencies on SourceFileProducer modules |
| 372 | // will have already been handled by the path_properties mutator. If ctx.Config().AllowMissingDependencies() is |
Paul Duffin | 036cace | 2019-07-25 14:44:56 +0100 | [diff] [blame] | 373 | // true then any missing SourceFileProducer or OutputFileProducer dependencies will cause the module to be marked as |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 374 | // having missing dependencies. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 375 | func PathsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) Paths { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 376 | ret, missingDeps := PathsAndMissingDepsForModuleSrcExcludes(ctx, paths, excludes) |
| 377 | if ctx.Config().AllowMissingDependencies() { |
| 378 | ctx.AddMissingDependencies(missingDeps) |
| 379 | } else { |
| 380 | for _, m := range missingDeps { |
| 381 | ctx.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m) |
| 382 | } |
| 383 | } |
| 384 | return ret |
| 385 | } |
| 386 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 387 | // A subset of the ModuleContext methods which are sufficient to resolve references to paths/deps in |
| 388 | // order to form a Bazel-compatible label for conversion. |
| 389 | type BazelConversionPathContext interface { |
| 390 | EarlyModulePathContext |
| 391 | |
| 392 | GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 393 | Module() Module |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 394 | ModuleType() string |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 395 | OtherModuleName(m blueprint.Module) string |
| 396 | OtherModuleDir(m blueprint.Module) string |
| 397 | } |
| 398 | |
| 399 | // BazelLabelForModuleDeps returns a Bazel-compatible label for the requested modules which |
| 400 | // correspond to dependencies on the module within the given ctx. |
| 401 | func BazelLabelForModuleDeps(ctx BazelConversionPathContext, modules []string) bazel.LabelList { |
| 402 | var labels bazel.LabelList |
| 403 | for _, module := range modules { |
| 404 | bpText := module |
| 405 | if m := SrcIsModule(module); m == "" { |
| 406 | module = ":" + module |
| 407 | } |
| 408 | if m, t := SrcIsModuleWithTag(module); m != "" { |
| 409 | l := getOtherModuleLabel(ctx, m, t) |
| 410 | l.Bp_text = bpText |
| 411 | labels.Includes = append(labels.Includes, l) |
| 412 | } else { |
| 413 | ctx.ModuleErrorf("%q, is not a module reference", module) |
| 414 | } |
| 415 | } |
| 416 | return labels |
| 417 | } |
| 418 | |
Rupert Shuttleworth | c143cc5 | 2021-04-13 13:08:04 -0400 | [diff] [blame^] | 419 | // Returns true if a prefix + components[:i] + /Android.bp exists |
| 420 | // TODO(b/185358476) Could check for BUILD file instead of checking for Android.bp file, or ensure BUILD is always generated? |
| 421 | func directoryHasBlueprint(fs pathtools.FileSystem, prefix string, components []string, componentIndex int) bool { |
| 422 | blueprintPath := prefix |
| 423 | if blueprintPath != "" { |
| 424 | blueprintPath = blueprintPath + "/" |
| 425 | } |
| 426 | blueprintPath = blueprintPath + strings.Join(components[:componentIndex+1], "/") |
| 427 | blueprintPath = blueprintPath + "/Android.bp" |
| 428 | if exists, _, _ := fs.Exists(blueprintPath); exists { |
| 429 | return true |
| 430 | } else { |
| 431 | return false |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // Transform a path (if necessary) to acknowledge package boundaries |
| 436 | // |
| 437 | // e.g. something like |
| 438 | // async_safe/include/async_safe/CHECK.h |
| 439 | // might become |
| 440 | // //bionic/libc/async_safe:include/async_safe/CHECK.h |
| 441 | // if the "async_safe" directory is actually a package and not just a directory. |
| 442 | // |
| 443 | // In particular, paths that extend into packages are transformed into absolute labels beginning with //. |
| 444 | func transformSubpackagePath(ctx BazelConversionPathContext, path bazel.Label) bazel.Label { |
| 445 | var newPath bazel.Label |
| 446 | |
| 447 | // Don't transform Bp_text |
| 448 | newPath.Bp_text = path.Bp_text |
| 449 | |
| 450 | if strings.HasPrefix(path.Label, "//") { |
| 451 | // Assume absolute labels are already correct (e.g. //path/to/some/package:foo.h) |
| 452 | newPath.Label = path.Label |
| 453 | return newPath |
| 454 | } |
| 455 | |
| 456 | newLabel := "" |
| 457 | pathComponents := strings.Split(path.Label, "/") |
| 458 | foundBlueprint := false |
| 459 | // Check the deepest subdirectory first and work upwards |
| 460 | for i := len(pathComponents) - 1; i >= 0; i-- { |
| 461 | pathComponent := pathComponents[i] |
| 462 | var sep string |
| 463 | if !foundBlueprint && directoryHasBlueprint(ctx.Config().fs, ctx.ModuleDir(), pathComponents, i) { |
| 464 | sep = ":" |
| 465 | foundBlueprint = true |
| 466 | } else { |
| 467 | sep = "/" |
| 468 | } |
| 469 | if newLabel == "" { |
| 470 | newLabel = pathComponent |
| 471 | } else { |
| 472 | newLabel = pathComponent + sep + newLabel |
| 473 | } |
| 474 | } |
| 475 | if foundBlueprint { |
| 476 | // Ensure paths end up looking like //bionic/... instead of //./bionic/... |
| 477 | moduleDir := ctx.ModuleDir() |
| 478 | if strings.HasPrefix(moduleDir, ".") { |
| 479 | moduleDir = moduleDir[1:] |
| 480 | } |
| 481 | // Make the path into an absolute label (e.g. //bionic/libc/foo:bar.h instead of just foo:bar.h) |
| 482 | if moduleDir == "" { |
| 483 | newLabel = "//" + newLabel |
| 484 | } else { |
| 485 | newLabel = "//" + moduleDir + "/" + newLabel |
| 486 | } |
| 487 | } |
| 488 | newPath.Label = newLabel |
| 489 | |
| 490 | return newPath |
| 491 | } |
| 492 | |
| 493 | // Transform paths to acknowledge package boundaries |
| 494 | // See transformSubpackagePath() for more information |
| 495 | func transformSubpackagePaths(ctx BazelConversionPathContext, paths bazel.LabelList) bazel.LabelList { |
| 496 | var newPaths bazel.LabelList |
| 497 | for _, include := range paths.Includes { |
| 498 | newPaths.Includes = append(newPaths.Includes, transformSubpackagePath(ctx, include)) |
| 499 | } |
| 500 | for _, exclude := range paths.Excludes { |
| 501 | newPaths.Excludes = append(newPaths.Excludes, transformSubpackagePath(ctx, exclude)) |
| 502 | } |
| 503 | return newPaths |
| 504 | } |
| 505 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 506 | // BazelLabelForModuleSrc returns bazel.LabelList with paths rooted from the module's local source |
| 507 | // directory. It expands globs, and resolves references to modules using the ":name" syntax to |
| 508 | // bazel-compatible labels. Properties passed as the paths or excludes argument must have been |
| 509 | // annotated with struct tag `android:"path"` so that dependencies on other modules will have |
| 510 | // already been handled by the path_properties mutator. |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 511 | // |
| 512 | // With expanded globs, we can catch package boundaries problem instead of |
| 513 | // silently failing to potentially missing files from Bazel's globs. |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 514 | func BazelLabelForModuleSrc(ctx BazelConversionPathContext, paths []string) bazel.LabelList { |
| 515 | return BazelLabelForModuleSrcExcludes(ctx, paths, []string(nil)) |
| 516 | } |
| 517 | |
| 518 | // BazelLabelForModuleSrcExcludes returns bazel.LabelList with paths rooted from the module's local |
| 519 | // source directory, excluding labels included in the excludes argument. It expands globs, and |
| 520 | // resolves references to modules using the ":name" syntax to bazel-compatible labels. Properties |
| 521 | // passed as the paths or excludes argument must have been annotated with struct tag |
| 522 | // `android:"path"` so that dependencies on other modules will have already been handled by the |
| 523 | // path_properties mutator. |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 524 | // |
| 525 | // With expanded globs, we can catch package boundaries problem instead of |
| 526 | // silently failing to potentially missing files from Bazel's globs. |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 527 | func BazelLabelForModuleSrcExcludes(ctx BazelConversionPathContext, paths, excludes []string) bazel.LabelList { |
| 528 | excludeLabels := expandSrcsForBazel(ctx, excludes, []string(nil)) |
| 529 | excluded := make([]string, 0, len(excludeLabels.Includes)) |
| 530 | for _, e := range excludeLabels.Includes { |
| 531 | excluded = append(excluded, e.Label) |
| 532 | } |
| 533 | labels := expandSrcsForBazel(ctx, paths, excluded) |
| 534 | labels.Excludes = excludeLabels.Includes |
Rupert Shuttleworth | c143cc5 | 2021-04-13 13:08:04 -0400 | [diff] [blame^] | 535 | labels = transformSubpackagePaths(ctx, labels) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 536 | return labels |
| 537 | } |
| 538 | |
| 539 | // expandSrcsForBazel returns bazel.LabelList with paths rooted from the module's local |
| 540 | // source directory, excluding labels included in the excludes argument. It expands globs, and |
| 541 | // resolves references to modules using the ":name" syntax to bazel-compatible labels. Properties |
| 542 | // passed as the paths or excludes argument must have been annotated with struct tag |
| 543 | // `android:"path"` so that dependencies on other modules will have already been handled by the |
| 544 | // path_properties mutator. |
| 545 | func expandSrcsForBazel(ctx BazelConversionPathContext, paths, expandedExcludes []string) bazel.LabelList { |
Liz Kammer | ebfcf67 | 2021-02-16 15:00:05 -0500 | [diff] [blame] | 546 | if paths == nil { |
| 547 | return bazel.LabelList{} |
| 548 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 549 | labels := bazel.LabelList{ |
| 550 | Includes: []bazel.Label{}, |
| 551 | } |
| 552 | for _, p := range paths { |
| 553 | if m, tag := SrcIsModuleWithTag(p); m != "" { |
| 554 | l := getOtherModuleLabel(ctx, m, tag) |
| 555 | if !InList(l.Label, expandedExcludes) { |
| 556 | l.Bp_text = fmt.Sprintf(":%s", m) |
| 557 | labels.Includes = append(labels.Includes, l) |
| 558 | } |
| 559 | } else { |
| 560 | var expandedPaths []bazel.Label |
| 561 | if pathtools.IsGlob(p) { |
| 562 | globbedPaths := GlobFiles(ctx, pathForModuleSrc(ctx, p).String(), expandedExcludes) |
| 563 | globbedPaths = PathsWithModuleSrcSubDir(ctx, globbedPaths, "") |
| 564 | for _, path := range globbedPaths { |
| 565 | s := path.Rel() |
| 566 | expandedPaths = append(expandedPaths, bazel.Label{Label: s}) |
| 567 | } |
| 568 | } else { |
| 569 | if !InList(p, expandedExcludes) { |
| 570 | expandedPaths = append(expandedPaths, bazel.Label{Label: p}) |
| 571 | } |
| 572 | } |
| 573 | labels.Includes = append(labels.Includes, expandedPaths...) |
| 574 | } |
| 575 | } |
| 576 | return labels |
| 577 | } |
| 578 | |
| 579 | // getOtherModuleLabel returns a bazel.Label for the given dependency/tag combination for the |
| 580 | // module. The label will be relative to the current directory if appropriate. The dependency must |
| 581 | // already be resolved by either deps mutator or path deps mutator. |
| 582 | func getOtherModuleLabel(ctx BazelConversionPathContext, dep, tag string) bazel.Label { |
| 583 | m, _ := ctx.GetDirectDep(dep) |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 584 | if m == nil { |
| 585 | panic(fmt.Errorf("cannot get direct dep %s of %s", dep, ctx.Module().Name())) |
| 586 | } |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 587 | otherLabel := bazelModuleLabel(ctx, m, tag) |
| 588 | label := bazelModuleLabel(ctx, ctx.Module(), "") |
| 589 | if samePackage(label, otherLabel) { |
| 590 | otherLabel = bazelShortLabel(otherLabel) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 591 | } |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 592 | |
| 593 | return bazel.Label{ |
| 594 | Label: otherLabel, |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | func bazelModuleLabel(ctx BazelConversionPathContext, module blueprint.Module, tag string) string { |
| 599 | // TODO(b/165114590): Convert tag (":name{.tag}") to corresponding Bazel implicit output targets. |
| 600 | b, ok := module.(Bazelable) |
| 601 | // TODO(b/181155349): perhaps return an error here if the module can't be/isn't being converted |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 602 | if !ok || !b.ConvertedToBazel(ctx) { |
Liz Kammer | bdc6099 | 2021-02-24 16:55:11 -0500 | [diff] [blame] | 603 | return bp2buildModuleLabel(ctx, module) |
| 604 | } |
| 605 | return b.GetBazelLabel(ctx, module) |
| 606 | } |
| 607 | |
| 608 | func bazelShortLabel(label string) string { |
| 609 | i := strings.Index(label, ":") |
| 610 | return label[i:] |
| 611 | } |
| 612 | |
| 613 | func bazelPackage(label string) string { |
| 614 | i := strings.Index(label, ":") |
| 615 | return label[0:i] |
| 616 | } |
| 617 | |
| 618 | func samePackage(label1, label2 string) bool { |
| 619 | return bazelPackage(label1) == bazelPackage(label2) |
| 620 | } |
| 621 | |
| 622 | func bp2buildModuleLabel(ctx BazelConversionPathContext, module blueprint.Module) string { |
| 623 | moduleName := ctx.OtherModuleName(module) |
| 624 | moduleDir := ctx.OtherModuleDir(module) |
| 625 | return fmt.Sprintf("//%s:%s", moduleDir, moduleName) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 626 | } |
| 627 | |
Ulya Trafimovich | 4d2eeed | 2019-11-08 10:54:21 +0000 | [diff] [blame] | 628 | // OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection. |
| 629 | type OutputPaths []OutputPath |
| 630 | |
| 631 | // Paths returns the OutputPaths as a Paths |
| 632 | func (p OutputPaths) Paths() Paths { |
| 633 | if p == nil { |
| 634 | return nil |
| 635 | } |
| 636 | ret := make(Paths, len(p)) |
| 637 | for i, path := range p { |
| 638 | ret[i] = path |
| 639 | } |
| 640 | return ret |
| 641 | } |
| 642 | |
| 643 | // Strings returns the string forms of the writable paths. |
| 644 | func (p OutputPaths) Strings() []string { |
| 645 | if p == nil { |
| 646 | return nil |
| 647 | } |
| 648 | ret := make([]string, len(p)) |
| 649 | for i, path := range p { |
| 650 | ret[i] = path.String() |
| 651 | } |
| 652 | return ret |
| 653 | } |
| 654 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 655 | // Expands Paths to a SourceFileProducer or OutputFileProducer module dependency referenced via ":name" or ":name{.tag}" syntax. |
| 656 | // If the dependency is not found, a missingErrorDependency is returned. |
| 657 | // If the module dependency is not a SourceFileProducer or OutputFileProducer, appropriate errors will be returned. |
| 658 | func getPathsFromModuleDep(ctx ModuleWithDepsPathContext, path, moduleName, tag string) (Paths, error) { |
| 659 | module := ctx.GetDirectDepWithTag(moduleName, sourceOrOutputDepTag(tag)) |
| 660 | if module == nil { |
| 661 | return nil, missingDependencyError{[]string{moduleName}} |
| 662 | } |
Colin Cross | fa65cee | 2021-03-22 17:05:59 -0700 | [diff] [blame] | 663 | if aModule, ok := module.(Module); ok && !aModule.Enabled() { |
| 664 | return nil, missingDependencyError{[]string{moduleName}} |
| 665 | } |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 666 | if outProducer, ok := module.(OutputFileProducer); ok { |
| 667 | outputFiles, err := outProducer.OutputFiles(tag) |
| 668 | if err != nil { |
| 669 | return nil, fmt.Errorf("path dependency %q: %s", path, err) |
| 670 | } |
| 671 | return outputFiles, nil |
| 672 | } else if tag != "" { |
| 673 | return nil, fmt.Errorf("path dependency %q is not an output file producing module", path) |
| 674 | } else if srcProducer, ok := module.(SourceFileProducer); ok { |
| 675 | return srcProducer.Srcs(), nil |
| 676 | } else { |
| 677 | return nil, fmt.Errorf("path dependency %q is not a source file producing module", path) |
| 678 | } |
| 679 | } |
| 680 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 681 | // PathsAndMissingDepsForModuleSrcExcludes returns Paths rooted from the module's local source directory, excluding |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 682 | // paths listed in the excludes arguments, and a list of missing dependencies. It expands globs, references to |
| 683 | // SourceFileProducer modules using the ":name" syntax, and references to OutputFileProducer modules using the |
| 684 | // ":name{.tag}" syntax. Properties passed as the paths or excludes argument must have been annotated with struct tag |
| 685 | // `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the |
| 686 | // path_properties mutator. If ctx.Config().AllowMissingDependencies() is true then any missing SourceFileProducer or |
| 687 | // OutputFileProducer dependencies will be returned, and they will NOT cause the module to be marked as having missing |
| 688 | // dependencies. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 689 | func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleWithDepsPathContext, paths, excludes []string) (Paths, []string) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 690 | prefix := pathForModuleSrc(ctx).String() |
| 691 | |
| 692 | var expandedExcludes []string |
| 693 | if excludes != nil { |
| 694 | expandedExcludes = make([]string, 0, len(excludes)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 695 | } |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 696 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 697 | var missingExcludeDeps []string |
| 698 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 699 | for _, e := range excludes { |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 700 | if m, t := SrcIsModuleWithTag(e); m != "" { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 701 | modulePaths, err := getPathsFromModuleDep(ctx, e, m, t) |
| 702 | if m, ok := err.(missingDependencyError); ok { |
| 703 | missingExcludeDeps = append(missingExcludeDeps, m.missingDeps...) |
| 704 | } else if err != nil { |
| 705 | reportPathError(ctx, err) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 706 | } else { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 707 | expandedExcludes = append(expandedExcludes, modulePaths.Strings()...) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 708 | } |
| 709 | } else { |
| 710 | expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e)) |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | if paths == nil { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 715 | return nil, missingExcludeDeps |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 716 | } |
| 717 | |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 718 | var missingDeps []string |
| 719 | |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 720 | expandedSrcFiles := make(Paths, 0, len(paths)) |
| 721 | for _, s := range paths { |
| 722 | srcFiles, err := expandOneSrcPath(ctx, s, expandedExcludes) |
| 723 | if depErr, ok := err.(missingDependencyError); ok { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 724 | missingDeps = append(missingDeps, depErr.missingDeps...) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 725 | } else if err != nil { |
| 726 | reportPathError(ctx, err) |
| 727 | } |
| 728 | expandedSrcFiles = append(expandedSrcFiles, srcFiles...) |
| 729 | } |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 730 | |
| 731 | return expandedSrcFiles, append(missingDeps, missingExcludeDeps...) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | type missingDependencyError struct { |
| 735 | missingDeps []string |
| 736 | } |
| 737 | |
| 738 | func (e missingDependencyError) Error() string { |
| 739 | return "missing dependencies: " + strings.Join(e.missingDeps, ", ") |
| 740 | } |
| 741 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 742 | // Expands one path string to Paths rooted from the module's local source |
| 743 | // directory, excluding those listed in the expandedExcludes. |
| 744 | // Expands globs, references to SourceFileProducer or OutputFileProducer modules using the ":name" and ":name{.tag}" syntax. |
| 745 | func expandOneSrcPath(ctx ModuleWithDepsPathContext, sPath string, expandedExcludes []string) (Paths, error) { |
Jooyung Han | 7607dd3 | 2020-07-05 10:23:14 +0900 | [diff] [blame] | 746 | excludePaths := func(paths Paths) Paths { |
| 747 | if len(expandedExcludes) == 0 { |
| 748 | return paths |
| 749 | } |
| 750 | remainder := make(Paths, 0, len(paths)) |
| 751 | for _, p := range paths { |
| 752 | if !InList(p.String(), expandedExcludes) { |
| 753 | remainder = append(remainder, p) |
| 754 | } |
| 755 | } |
| 756 | return remainder |
| 757 | } |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 758 | if m, t := SrcIsModuleWithTag(sPath); m != "" { |
| 759 | modulePaths, err := getPathsFromModuleDep(ctx, sPath, m, t) |
| 760 | if err != nil { |
| 761 | return nil, err |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 762 | } else { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 763 | return excludePaths(modulePaths), nil |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 764 | } |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 765 | } else if pathtools.IsGlob(sPath) { |
| 766 | paths := GlobFiles(ctx, pathForModuleSrc(ctx, sPath).String(), expandedExcludes) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 767 | return PathsWithModuleSrcSubDir(ctx, paths, ""), nil |
| 768 | } else { |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 769 | p := pathForModuleSrc(ctx, sPath) |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 770 | if exists, _, err := ctx.Config().fs.Exists(p.String()); err != nil { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 771 | ReportPathErrorf(ctx, "%s: %s", p, err.Error()) |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 772 | } else if !exists && !ctx.Config().TestAllowNonExistentPaths { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 773 | ReportPathErrorf(ctx, "module source path %q does not exist", p) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Jooyung Han | 7607dd3 | 2020-07-05 10:23:14 +0900 | [diff] [blame] | 776 | if InList(p.String(), expandedExcludes) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 777 | return nil, nil |
| 778 | } |
| 779 | return Paths{p}, nil |
| 780 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | // pathsForModuleSrcFromFullPath returns Paths rooted from the module's local |
| 784 | // source directory, but strip the local source directory from the beginning of |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 785 | // 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] | 786 | // It intended for use in globs that only list files that exist, so it allows '$' in |
| 787 | // filenames. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 788 | func pathsForModuleSrcFromFullPath(ctx EarlyModulePathContext, paths []string, incDirs bool) Paths { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 789 | prefix := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir()) + "/" |
Colin Cross | 0f37af0 | 2017-09-27 17:42:05 -0700 | [diff] [blame] | 790 | if prefix == "./" { |
| 791 | prefix = "" |
| 792 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 793 | ret := make(Paths, 0, len(paths)) |
| 794 | for _, p := range paths { |
Dan Willemsen | 540a78c | 2018-02-26 21:50:08 -0800 | [diff] [blame] | 795 | if !incDirs && strings.HasSuffix(p, "/") { |
| 796 | continue |
| 797 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 798 | path := filepath.Clean(p) |
| 799 | if !strings.HasPrefix(path, prefix) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 800 | 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] | 801 | continue |
| 802 | } |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 803 | |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 804 | srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):]) |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 805 | if err != nil { |
| 806 | reportPathError(ctx, err) |
| 807 | continue |
| 808 | } |
| 809 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 810 | srcPath.basePath.rel = srcPath.path |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 811 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 812 | ret = append(ret, srcPath) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 813 | } |
| 814 | return ret |
| 815 | } |
| 816 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 817 | // PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's local source |
| 818 | // directory. If input is nil, use the default if it exists. If input is empty, returns nil. |
| 819 | func PathsWithOptionalDefaultForModuleSrc(ctx ModuleMissingDepsPathContext, input []string, def string) Paths { |
Colin Cross | 0ddae7f | 2019-02-07 15:30:01 -0800 | [diff] [blame] | 820 | if input != nil { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 821 | return PathsForModuleSrc(ctx, input) |
| 822 | } |
| 823 | // Use Glob so that if the default doesn't exist, a dependency is added so that when it |
| 824 | // is created, we're run again. |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 825 | path := filepath.Join(ctx.Config().srcDir, ctx.ModuleDir(), def) |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 826 | return Glob(ctx, path, nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | // Strings returns the Paths in string form |
| 830 | func (p Paths) Strings() []string { |
| 831 | if p == nil { |
| 832 | return nil |
| 833 | } |
| 834 | ret := make([]string, len(p)) |
| 835 | for i, path := range p { |
| 836 | ret[i] = path.String() |
| 837 | } |
| 838 | return ret |
| 839 | } |
| 840 | |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 841 | func CopyOfPaths(paths Paths) Paths { |
| 842 | return append(Paths(nil), paths...) |
| 843 | } |
| 844 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 845 | // FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It |
| 846 | // 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] | 847 | func FirstUniquePaths(list Paths) Paths { |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 848 | // 128 was chosen based on BenchmarkFirstUniquePaths results. |
| 849 | if len(list) > 128 { |
| 850 | return firstUniquePathsMap(list) |
| 851 | } |
| 852 | return firstUniquePathsList(list) |
| 853 | } |
| 854 | |
Colin Cross | c0efd1d | 2020-07-03 11:56:24 -0700 | [diff] [blame] | 855 | // SortedUniquePaths returns all unique elements of a Paths in sorted order. It modifies the |
| 856 | // 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] | 857 | func SortedUniquePaths(list Paths) Paths { |
| 858 | unique := FirstUniquePaths(list) |
| 859 | sort.Slice(unique, func(i, j int) bool { |
| 860 | return unique[i].String() < unique[j].String() |
| 861 | }) |
| 862 | return unique |
| 863 | } |
| 864 | |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 865 | func firstUniquePathsList(list Paths) Paths { |
Dan Willemsen | fe92c96 | 2017-08-29 12:28:37 -0700 | [diff] [blame] | 866 | k := 0 |
| 867 | outer: |
| 868 | for i := 0; i < len(list); i++ { |
| 869 | for j := 0; j < k; j++ { |
| 870 | if list[i] == list[j] { |
| 871 | continue outer |
| 872 | } |
| 873 | } |
| 874 | list[k] = list[i] |
| 875 | k++ |
| 876 | } |
| 877 | return list[:k] |
| 878 | } |
| 879 | |
Colin Cross | 27027c7 | 2020-02-28 15:34:17 -0800 | [diff] [blame] | 880 | func firstUniquePathsMap(list Paths) Paths { |
| 881 | k := 0 |
| 882 | seen := make(map[Path]bool, len(list)) |
| 883 | for i := 0; i < len(list); i++ { |
| 884 | if seen[list[i]] { |
| 885 | continue |
| 886 | } |
| 887 | seen[list[i]] = true |
| 888 | list[k] = list[i] |
| 889 | k++ |
| 890 | } |
| 891 | return list[:k] |
| 892 | } |
| 893 | |
Colin Cross | 5d58395 | 2020-11-24 16:21:24 -0800 | [diff] [blame] | 894 | // FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each. It |
| 895 | // modifies the InstallPaths slice contents in place, and returns a subslice of the original slice. |
| 896 | func FirstUniqueInstallPaths(list InstallPaths) InstallPaths { |
| 897 | // 128 was chosen based on BenchmarkFirstUniquePaths results. |
| 898 | if len(list) > 128 { |
| 899 | return firstUniqueInstallPathsMap(list) |
| 900 | } |
| 901 | return firstUniqueInstallPathsList(list) |
| 902 | } |
| 903 | |
| 904 | func firstUniqueInstallPathsList(list InstallPaths) InstallPaths { |
| 905 | k := 0 |
| 906 | outer: |
| 907 | for i := 0; i < len(list); i++ { |
| 908 | for j := 0; j < k; j++ { |
| 909 | if list[i] == list[j] { |
| 910 | continue outer |
| 911 | } |
| 912 | } |
| 913 | list[k] = list[i] |
| 914 | k++ |
| 915 | } |
| 916 | return list[:k] |
| 917 | } |
| 918 | |
| 919 | func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths { |
| 920 | k := 0 |
| 921 | seen := make(map[InstallPath]bool, len(list)) |
| 922 | for i := 0; i < len(list); i++ { |
| 923 | if seen[list[i]] { |
| 924 | continue |
| 925 | } |
| 926 | seen[list[i]] = true |
| 927 | list[k] = list[i] |
| 928 | k++ |
| 929 | } |
| 930 | return list[:k] |
| 931 | } |
| 932 | |
Colin Cross | b671544 | 2017-10-24 11:13:31 -0700 | [diff] [blame] | 933 | // LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It |
| 934 | // modifies the Paths slice contents in place, and returns a subslice of the original slice. |
| 935 | func LastUniquePaths(list Paths) Paths { |
| 936 | totalSkip := 0 |
| 937 | for i := len(list) - 1; i >= totalSkip; i-- { |
| 938 | skip := 0 |
| 939 | for j := i - 1; j >= totalSkip; j-- { |
| 940 | if list[i] == list[j] { |
| 941 | skip++ |
| 942 | } else { |
| 943 | list[j+skip] = list[j] |
| 944 | } |
| 945 | } |
| 946 | totalSkip += skip |
| 947 | } |
| 948 | return list[totalSkip:] |
| 949 | } |
| 950 | |
Colin Cross | a140bb0 | 2018-04-17 10:52:26 -0700 | [diff] [blame] | 951 | // ReversePaths returns a copy of a Paths in reverse order. |
| 952 | func ReversePaths(list Paths) Paths { |
| 953 | if list == nil { |
| 954 | return nil |
| 955 | } |
| 956 | ret := make(Paths, len(list)) |
| 957 | for i := range list { |
| 958 | ret[i] = list[len(list)-1-i] |
| 959 | } |
| 960 | return ret |
| 961 | } |
| 962 | |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 963 | func indexPathList(s Path, list []Path) int { |
| 964 | for i, l := range list { |
| 965 | if l == s { |
| 966 | return i |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | return -1 |
| 971 | } |
| 972 | |
| 973 | func inPathList(p Path, list []Path) bool { |
| 974 | return indexPathList(p, list) != -1 |
| 975 | } |
| 976 | |
| 977 | func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) { |
Paul Duffin | 57b9e1d | 2019-12-13 00:03:35 +0000 | [diff] [blame] | 978 | return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) }) |
| 979 | } |
| 980 | |
| 981 | func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 982 | for _, l := range list { |
Paul Duffin | 57b9e1d | 2019-12-13 00:03:35 +0000 | [diff] [blame] | 983 | if predicate(l) { |
Jeff Gaston | 294356f | 2017-09-27 17:05:30 -0700 | [diff] [blame] | 984 | filtered = append(filtered, l) |
| 985 | } else { |
| 986 | remainder = append(remainder, l) |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | return |
| 991 | } |
| 992 | |
Colin Cross | 93e8595 | 2017-08-15 13:34:18 -0700 | [diff] [blame] | 993 | // HasExt returns true of any of the paths have extension ext, otherwise false |
| 994 | func (p Paths) HasExt(ext string) bool { |
| 995 | for _, path := range p { |
| 996 | if path.Ext() == ext { |
| 997 | return true |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | return false |
| 1002 | } |
| 1003 | |
| 1004 | // FilterByExt returns the subset of the paths that have extension ext |
| 1005 | func (p Paths) FilterByExt(ext string) Paths { |
| 1006 | ret := make(Paths, 0, len(p)) |
| 1007 | for _, path := range p { |
| 1008 | if path.Ext() == ext { |
| 1009 | ret = append(ret, path) |
| 1010 | } |
| 1011 | } |
| 1012 | return ret |
| 1013 | } |
| 1014 | |
| 1015 | // FilterOutByExt returns the subset of the paths that do not have extension ext |
| 1016 | func (p Paths) FilterOutByExt(ext string) Paths { |
| 1017 | ret := make(Paths, 0, len(p)) |
| 1018 | for _, path := range p { |
| 1019 | if path.Ext() != ext { |
| 1020 | ret = append(ret, path) |
| 1021 | } |
| 1022 | } |
| 1023 | return ret |
| 1024 | } |
| 1025 | |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 1026 | // DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory |
| 1027 | // (including subdirectories) are in a contiguous subslice of the list, and can be found in |
| 1028 | // O(log(N)) time using a binary search on the directory prefix. |
| 1029 | type DirectorySortedPaths Paths |
| 1030 | |
| 1031 | func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths { |
| 1032 | ret := append(DirectorySortedPaths(nil), paths...) |
| 1033 | sort.Slice(ret, func(i, j int) bool { |
| 1034 | return ret[i].String() < ret[j].String() |
| 1035 | }) |
| 1036 | return ret |
| 1037 | } |
| 1038 | |
| 1039 | // PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries |
| 1040 | // that are in the specified directory and its subdirectories. |
| 1041 | func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths { |
| 1042 | prefix := filepath.Clean(dir) + "/" |
| 1043 | start := sort.Search(len(p), func(i int) bool { |
| 1044 | return prefix < p[i].String() |
| 1045 | }) |
| 1046 | |
| 1047 | ret := p[start:] |
| 1048 | |
| 1049 | end := sort.Search(len(ret), func(i int) bool { |
| 1050 | return !strings.HasPrefix(ret[i].String(), prefix) |
| 1051 | }) |
| 1052 | |
| 1053 | ret = ret[:end] |
| 1054 | |
| 1055 | return Paths(ret) |
| 1056 | } |
| 1057 | |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 1058 | // WritablePaths is a slice of WritablePath, used for multiple outputs. |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1059 | type WritablePaths []WritablePath |
| 1060 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1061 | // RelativeToTop creates a new WritablePaths containing the result of calling Path.RelativeToTop on |
| 1062 | // each item in this slice. |
| 1063 | func (p WritablePaths) RelativeToTop() WritablePaths { |
| 1064 | ensureTestOnly() |
| 1065 | if p == nil { |
| 1066 | return p |
| 1067 | } |
| 1068 | ret := make(WritablePaths, len(p)) |
| 1069 | for i, path := range p { |
| 1070 | ret[i] = path.RelativeToTop().(WritablePath) |
| 1071 | } |
| 1072 | return ret |
| 1073 | } |
| 1074 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1075 | // Strings returns the string forms of the writable paths. |
| 1076 | func (p WritablePaths) Strings() []string { |
| 1077 | if p == nil { |
| 1078 | return nil |
| 1079 | } |
| 1080 | ret := make([]string, len(p)) |
| 1081 | for i, path := range p { |
| 1082 | ret[i] = path.String() |
| 1083 | } |
| 1084 | return ret |
| 1085 | } |
| 1086 | |
Colin Cross | 3bc7ffa | 2017-11-22 16:19:37 -0800 | [diff] [blame] | 1087 | // Paths returns the WritablePaths as a Paths |
| 1088 | func (p WritablePaths) Paths() Paths { |
| 1089 | if p == nil { |
| 1090 | return nil |
| 1091 | } |
| 1092 | ret := make(Paths, len(p)) |
| 1093 | for i, path := range p { |
| 1094 | ret[i] = path |
| 1095 | } |
| 1096 | return ret |
| 1097 | } |
| 1098 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1099 | type basePath struct { |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1100 | path string |
| 1101 | rel string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | func (p basePath) Ext() string { |
| 1105 | return filepath.Ext(p.path) |
| 1106 | } |
| 1107 | |
Colin Cross | 4f6fc9c | 2016-10-26 10:05:25 -0700 | [diff] [blame] | 1108 | func (p basePath) Base() string { |
| 1109 | return filepath.Base(p.path) |
| 1110 | } |
| 1111 | |
Colin Cross | faeb7aa | 2017-02-01 14:12:44 -0800 | [diff] [blame] | 1112 | func (p basePath) Rel() string { |
| 1113 | if p.rel != "" { |
| 1114 | return p.rel |
| 1115 | } |
| 1116 | return p.path |
| 1117 | } |
| 1118 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 1119 | func (p basePath) String() string { |
| 1120 | return p.path |
| 1121 | } |
| 1122 | |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1123 | func (p basePath) withRel(rel string) basePath { |
| 1124 | p.path = filepath.Join(p.path, rel) |
| 1125 | p.rel = rel |
| 1126 | return p |
| 1127 | } |
| 1128 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1129 | // SourcePath is a Path representing a file path rooted from SrcDir |
| 1130 | type SourcePath struct { |
| 1131 | basePath |
Paul Duffin | 580efc8 | 2021-03-24 09:04:03 +0000 | [diff] [blame] | 1132 | |
| 1133 | // The sources root, i.e. Config.SrcDir() |
| 1134 | srcDir string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1137 | func (p SourcePath) RelativeToTop() Path { |
| 1138 | ensureTestOnly() |
| 1139 | return p |
| 1140 | } |
| 1141 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1142 | var _ Path = SourcePath{} |
| 1143 | |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1144 | func (p SourcePath) withRel(rel string) SourcePath { |
| 1145 | p.basePath = p.basePath.withRel(rel) |
| 1146 | return p |
| 1147 | } |
| 1148 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1149 | // safePathForSource is for paths that we expect are safe -- only for use by go |
| 1150 | // code that is embedding ninja variables in paths |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 1151 | func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { |
| 1152 | p, err := validateSafePath(pathComponents...) |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1153 | ret := SourcePath{basePath{p, ""}, ctx.Config().srcDir} |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 1154 | if err != nil { |
| 1155 | return ret, err |
| 1156 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1157 | |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 1158 | // absolute path already checked by validateSafePath |
| 1159 | if strings.HasPrefix(ret.String(), ctx.Config().buildDir) { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 1160 | return ret, fmt.Errorf("source path %q is in output", ret.String()) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Colin Cross | fe4bc36 | 2018-09-12 10:02:13 -0700 | [diff] [blame] | 1163 | return ret, err |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1166 | // pathForSource creates a SourcePath from pathComponents, but does not check that it exists. |
| 1167 | func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) { |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1168 | p, err := validatePath(pathComponents...) |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1169 | ret := SourcePath{basePath{p, ""}, ctx.Config().srcDir} |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1170 | if err != nil { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1171 | return ret, err |
Colin Cross | 94a3210 | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1172 | } |
| 1173 | |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 1174 | // absolute path already checked by validatePath |
| 1175 | if strings.HasPrefix(ret.String(), ctx.Config().buildDir) { |
Mikhail Naganov | ab1f518 | 2019-02-08 13:17:55 -0800 | [diff] [blame] | 1176 | return ret, fmt.Errorf("source path %q is in output", ret.String()) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1179 | return ret, nil |
| 1180 | } |
| 1181 | |
| 1182 | // existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the |
| 1183 | // path does not exist. |
| 1184 | func existsWithDependencies(ctx PathContext, path SourcePath) (exists bool, err error) { |
| 1185 | var files []string |
| 1186 | |
| 1187 | if gctx, ok := ctx.(PathGlobContext); ok { |
| 1188 | // Use glob to produce proper dependencies, even though we only want |
| 1189 | // a single file. |
| 1190 | files, err = gctx.GlobWithDeps(path.String(), nil) |
| 1191 | } else { |
Colin Cross | 82ea3fb | 2021-04-05 17:48:26 -0700 | [diff] [blame] | 1192 | var result pathtools.GlobResult |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1193 | // We cannot add build statements in this context, so we fall back to |
| 1194 | // AddNinjaFileDeps |
Colin Cross | 82ea3fb | 2021-04-05 17:48:26 -0700 | [diff] [blame] | 1195 | result, err = ctx.Config().fs.Glob(path.String(), nil, pathtools.FollowSymlinks) |
| 1196 | ctx.AddNinjaFileDeps(result.Deps...) |
| 1197 | files = result.Matches |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | if err != nil { |
| 1201 | return false, fmt.Errorf("glob: %s", err.Error()) |
| 1202 | } |
| 1203 | |
| 1204 | return len(files) > 0, nil |
| 1205 | } |
| 1206 | |
| 1207 | // PathForSource joins the provided path components and validates that the result |
| 1208 | // neither escapes the source dir nor is in the out dir. |
| 1209 | // On error, it will return a usable, but invalid SourcePath, and report a ModuleError. |
| 1210 | func PathForSource(ctx PathContext, pathComponents ...string) SourcePath { |
| 1211 | path, err := pathForSource(ctx, pathComponents...) |
| 1212 | if err != nil { |
| 1213 | reportPathError(ctx, err) |
| 1214 | } |
| 1215 | |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 1216 | if pathtools.IsGlob(path.String()) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1217 | ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1220 | if modCtx, ok := ctx.(ModuleMissingDepsPathContext); ok && ctx.Config().AllowMissingDependencies() { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1221 | exists, err := existsWithDependencies(ctx, path) |
| 1222 | if err != nil { |
| 1223 | reportPathError(ctx, err) |
| 1224 | } |
| 1225 | if !exists { |
| 1226 | modCtx.AddMissingDependencies([]string{path.String()}) |
| 1227 | } |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 1228 | } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1229 | ReportPathErrorf(ctx, "%s: %s", path, err.Error()) |
Pedro Loureiro | 5d190cc | 2021-02-15 15:41:33 +0000 | [diff] [blame] | 1230 | } else if !exists && !ctx.Config().TestAllowNonExistentPaths { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1231 | ReportPathErrorf(ctx, "source path %q does not exist", path) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1232 | } |
| 1233 | return path |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Liz Kammer | 7aa5288 | 2021-02-11 09:16:14 -0500 | [diff] [blame] | 1236 | // ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not* |
| 1237 | // rooted from the module's local source directory, if the path exists, or an empty OptionalPath if |
| 1238 | // it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state |
| 1239 | // of the path changes. |
Colin Cross | 32f3898 | 2018-02-22 11:47:25 -0800 | [diff] [blame] | 1240 | func ExistentPathForSource(ctx PathContext, pathComponents ...string) OptionalPath { |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1241 | path, err := pathForSource(ctx, pathComponents...) |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1242 | if err != nil { |
| 1243 | reportPathError(ctx, err) |
| 1244 | return OptionalPath{} |
| 1245 | } |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1246 | |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 1247 | if pathtools.IsGlob(path.String()) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1248 | ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String()) |
Colin Cross | e3924e1 | 2018-08-15 20:18:53 -0700 | [diff] [blame] | 1249 | return OptionalPath{} |
| 1250 | } |
| 1251 | |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1252 | exists, err := existsWithDependencies(ctx, path) |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1253 | if err != nil { |
| 1254 | reportPathError(ctx, err) |
| 1255 | return OptionalPath{} |
| 1256 | } |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1257 | if !exists { |
Colin Cross | c48c143 | 2018-02-23 07:09:01 +0000 | [diff] [blame] | 1258 | return OptionalPath{} |
| 1259 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1260 | return OptionalPathForPath(path) |
| 1261 | } |
| 1262 | |
| 1263 | func (p SourcePath) String() string { |
Paul Duffin | 580efc8 | 2021-03-24 09:04:03 +0000 | [diff] [blame] | 1264 | return filepath.Join(p.srcDir, p.path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | // Join creates a new SourcePath with paths... joined with the current path. The |
| 1268 | // provided paths... may not use '..' to escape from the current path. |
| 1269 | func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1270 | path, err := validatePath(paths...) |
| 1271 | if err != nil { |
| 1272 | reportPathError(ctx, err) |
| 1273 | } |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1274 | return p.withRel(path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1277 | // join is like Join but does less path validation. |
| 1278 | func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath { |
| 1279 | path, err := validateSafePath(paths...) |
| 1280 | if err != nil { |
| 1281 | reportPathError(ctx, err) |
| 1282 | } |
| 1283 | return p.withRel(path) |
| 1284 | } |
| 1285 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1286 | // OverlayPath returns the overlay for `path' if it exists. This assumes that the |
| 1287 | // SourcePath is the path to a resource overlay directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1288 | func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1289 | var relDir string |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1290 | if srcPath, ok := path.(SourcePath); ok { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1291 | relDir = srcPath.path |
| 1292 | } else { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1293 | 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] | 1294 | return OptionalPath{} |
| 1295 | } |
Paul Duffin | 580efc8 | 2021-03-24 09:04:03 +0000 | [diff] [blame] | 1296 | dir := filepath.Join(p.srcDir, p.path, relDir) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1297 | // 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] | 1298 | if pathtools.IsGlob(dir) { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1299 | ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir) |
Dan Willemsen | 7b310ee | 2015-12-18 15:11:17 -0800 | [diff] [blame] | 1300 | } |
Colin Cross | 461b445 | 2018-02-23 09:22:42 -0800 | [diff] [blame] | 1301 | paths, err := ctx.GlobWithDeps(dir, nil) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1302 | if err != nil { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1303 | ReportPathErrorf(ctx, "glob: %s", err.Error()) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1304 | return OptionalPath{} |
| 1305 | } |
| 1306 | if len(paths) == 0 { |
| 1307 | return OptionalPath{} |
| 1308 | } |
Paul Duffin | 580efc8 | 2021-03-24 09:04:03 +0000 | [diff] [blame] | 1309 | relPath := Rel(ctx, p.srcDir, paths[0]) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1310 | return OptionalPathForPath(PathForSource(ctx, relPath)) |
| 1311 | } |
| 1312 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1313 | // 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] | 1314 | type OutputPath struct { |
| 1315 | basePath |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1316 | |
| 1317 | // The soong build directory, i.e. Config.BuildDir() |
| 1318 | buildDir string |
| 1319 | |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1320 | fullPath string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1323 | func (p OutputPath) withRel(rel string) OutputPath { |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1324 | p.basePath = p.basePath.withRel(rel) |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1325 | p.fullPath = filepath.Join(p.fullPath, rel) |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1326 | return p |
| 1327 | } |
| 1328 | |
Colin Cross | 3063b78 | 2018-08-15 11:19:12 -0700 | [diff] [blame] | 1329 | func (p OutputPath) WithoutRel() OutputPath { |
| 1330 | p.basePath.rel = filepath.Base(p.basePath.path) |
| 1331 | return p |
| 1332 | } |
| 1333 | |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1334 | func (p OutputPath) getBuildDir() string { |
| 1335 | return p.buildDir |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1338 | func (p OutputPath) RelativeToTop() Path { |
| 1339 | return p.outputPathRelativeToTop() |
| 1340 | } |
| 1341 | |
| 1342 | func (p OutputPath) outputPathRelativeToTop() OutputPath { |
| 1343 | p.fullPath = StringPathRelativeToTop(p.buildDir, p.fullPath) |
| 1344 | p.buildDir = OutSoongDir |
| 1345 | return p |
| 1346 | } |
| 1347 | |
Paul Duffin | 0267d49 | 2021-02-02 10:05:52 +0000 | [diff] [blame] | 1348 | func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
| 1349 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1350 | } |
| 1351 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1352 | var _ Path = OutputPath{} |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1353 | var _ WritablePath = OutputPath{} |
Paul Duffin | 0267d49 | 2021-02-02 10:05:52 +0000 | [diff] [blame] | 1354 | var _ objPathProvider = OutputPath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1355 | |
Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 1356 | // toolDepPath is a Path representing a dependency of the build tool. |
| 1357 | type toolDepPath struct { |
| 1358 | basePath |
| 1359 | } |
| 1360 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1361 | func (t toolDepPath) RelativeToTop() Path { |
| 1362 | ensureTestOnly() |
| 1363 | return t |
| 1364 | } |
| 1365 | |
Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 1366 | var _ Path = toolDepPath{} |
| 1367 | |
| 1368 | // pathForBuildToolDep returns a toolDepPath representing the given path string. |
| 1369 | // There is no validation for the path, as it is "trusted": It may fail |
| 1370 | // normal validation checks. For example, it may be an absolute path. |
| 1371 | // Only use this function to construct paths for dependencies of the build |
| 1372 | // tool invocation. |
| 1373 | func pathForBuildToolDep(ctx PathContext, path string) toolDepPath { |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1374 | return toolDepPath{basePath{path, ""}} |
Chris Parsons | 8f232a2 | 2020-06-23 17:37:05 -0400 | [diff] [blame] | 1375 | } |
| 1376 | |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1377 | // PathForOutput joins the provided paths and returns an OutputPath that is |
| 1378 | // validated to not escape the build dir. |
| 1379 | // On error, it will return a usable, but invalid OutputPath, and report a ModuleError. |
| 1380 | func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1381 | path, err := validatePath(pathComponents...) |
| 1382 | if err != nil { |
| 1383 | reportPathError(ctx, err) |
| 1384 | } |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1385 | fullPath := filepath.Join(ctx.Config().buildDir, path) |
| 1386 | path = fullPath[len(fullPath)-len(path):] |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1387 | return OutputPath{basePath{path, ""}, ctx.Config().buildDir, fullPath} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1390 | // PathsForOutput returns Paths rooted from buildDir |
| 1391 | func PathsForOutput(ctx PathContext, paths []string) WritablePaths { |
| 1392 | ret := make(WritablePaths, len(paths)) |
| 1393 | for i, path := range paths { |
| 1394 | ret[i] = PathForOutput(ctx, path) |
| 1395 | } |
| 1396 | return ret |
| 1397 | } |
| 1398 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1399 | func (p OutputPath) writablePath() {} |
| 1400 | |
| 1401 | func (p OutputPath) String() string { |
Colin Cross | d63c9a7 | 2020-01-29 16:52:50 -0800 | [diff] [blame] | 1402 | return p.fullPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | // Join creates a new OutputPath with paths... joined with the current path. The |
| 1406 | // provided paths... may not use '..' to escape from the current path. |
| 1407 | func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1408 | path, err := validatePath(paths...) |
| 1409 | if err != nil { |
| 1410 | reportPathError(ctx, err) |
| 1411 | } |
Colin Cross | 0db5568 | 2017-12-05 15:36:55 -0800 | [diff] [blame] | 1412 | return p.withRel(path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1413 | } |
| 1414 | |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1415 | // ReplaceExtension creates a new OutputPath with the extension replaced with ext. |
| 1416 | func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { |
| 1417 | if strings.Contains(ext, "/") { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1418 | ReportPathErrorf(ctx, "extension %q cannot contain /", ext) |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1419 | } |
| 1420 | ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext)) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1421 | ret.rel = pathtools.ReplaceExtension(p.rel, ext) |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 1422 | return ret |
| 1423 | } |
| 1424 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1425 | // InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths. |
| 1426 | func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath { |
| 1427 | path, err := validatePath(paths...) |
| 1428 | if err != nil { |
| 1429 | reportPathError(ctx, err) |
| 1430 | } |
| 1431 | |
| 1432 | ret := PathForOutput(ctx, filepath.Dir(p.path), path) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 1433 | ret.rel = filepath.Join(filepath.Dir(p.rel), path) |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 1434 | return ret |
| 1435 | } |
| 1436 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1437 | // PathForIntermediates returns an OutputPath representing the top-level |
| 1438 | // intermediates directory. |
| 1439 | func PathForIntermediates(ctx PathContext, paths ...string) OutputPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1440 | path, err := validatePath(paths...) |
| 1441 | if err != nil { |
| 1442 | reportPathError(ctx, err) |
| 1443 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1444 | return PathForOutput(ctx, ".intermediates", path) |
| 1445 | } |
| 1446 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1447 | var _ genPathProvider = SourcePath{} |
| 1448 | var _ objPathProvider = SourcePath{} |
| 1449 | var _ resPathProvider = SourcePath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1450 | |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1451 | // PathForModuleSrc returns a Path representing the paths... under the |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1452 | // module's local source directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1453 | func PathForModuleSrc(ctx ModuleMissingDepsPathContext, pathComponents ...string) Path { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1454 | p, err := validatePath(pathComponents...) |
| 1455 | if err != nil { |
| 1456 | reportPathError(ctx, err) |
Colin Cross | 192e97a | 2018-02-22 14:21:02 -0800 | [diff] [blame] | 1457 | } |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1458 | paths, err := expandOneSrcPath(ctx, p, nil) |
| 1459 | if err != nil { |
| 1460 | if depErr, ok := err.(missingDependencyError); ok { |
| 1461 | if ctx.Config().AllowMissingDependencies() { |
| 1462 | ctx.AddMissingDependencies(depErr.missingDeps) |
| 1463 | } else { |
| 1464 | ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error()) |
| 1465 | } |
| 1466 | } else { |
| 1467 | reportPathError(ctx, err) |
| 1468 | } |
| 1469 | return nil |
| 1470 | } else if len(paths) == 0 { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1471 | ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1472 | return nil |
| 1473 | } else if len(paths) > 1 { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 1474 | ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths)) |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 1475 | } |
| 1476 | return paths[0] |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1479 | func pathForModuleSrc(ctx EarlyModulePathContext, paths ...string) SourcePath { |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1480 | p, err := validatePath(paths...) |
| 1481 | if err != nil { |
| 1482 | reportPathError(ctx, err) |
| 1483 | } |
| 1484 | |
| 1485 | path, err := pathForSource(ctx, ctx.ModuleDir(), p) |
| 1486 | if err != nil { |
| 1487 | reportPathError(ctx, err) |
| 1488 | } |
| 1489 | |
| 1490 | path.basePath.rel = p |
| 1491 | |
| 1492 | return path |
| 1493 | } |
| 1494 | |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1495 | // PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path |
| 1496 | // will return the path relative to subDir in the module's source directory. If any input paths are not located |
| 1497 | // inside subDir then a path error will be reported. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1498 | func PathsWithModuleSrcSubDir(ctx EarlyModulePathContext, paths Paths, subDir string) Paths { |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1499 | paths = append(Paths(nil), paths...) |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1500 | subDirFullPath := pathForModuleSrc(ctx, subDir) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1501 | for i, path := range paths { |
| 1502 | rel := Rel(ctx, subDirFullPath.String(), path.String()) |
| 1503 | paths[i] = subDirFullPath.join(ctx, rel) |
| 1504 | } |
| 1505 | return paths |
| 1506 | } |
| 1507 | |
| 1508 | // PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the |
| 1509 | // 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] | 1510 | func PathWithModuleSrcSubDir(ctx EarlyModulePathContext, path Path, subDir string) Path { |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame] | 1511 | subDirFullPath := pathForModuleSrc(ctx, subDir) |
Colin Cross | 2fafa3e | 2019-03-05 12:39:51 -0800 | [diff] [blame] | 1512 | rel := Rel(ctx, subDirFullPath.String(), path.String()) |
| 1513 | return subDirFullPath.Join(ctx, rel) |
| 1514 | } |
| 1515 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1516 | // OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a |
| 1517 | // valid path if p is non-nil. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1518 | func OptionalPathForModuleSrc(ctx ModuleMissingDepsPathContext, p *string) OptionalPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1519 | if p == nil { |
| 1520 | return OptionalPath{} |
| 1521 | } |
| 1522 | return OptionalPathForPath(PathForModuleSrc(ctx, *p)) |
| 1523 | } |
| 1524 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1525 | func (p SourcePath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 1526 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1529 | func (p SourcePath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
Colin Cross | 7fc17db | 2017-02-01 14:07:55 -0800 | [diff] [blame] | 1530 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1531 | } |
| 1532 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1533 | func (p SourcePath) resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1534 | // TODO: Use full directory if the new ctx is not the current ctx? |
| 1535 | return PathForModuleRes(ctx, p.path, name) |
| 1536 | } |
| 1537 | |
| 1538 | // ModuleOutPath is a Path representing a module's output directory. |
| 1539 | type ModuleOutPath struct { |
| 1540 | OutputPath |
| 1541 | } |
| 1542 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1543 | func (p ModuleOutPath) RelativeToTop() Path { |
| 1544 | p.OutputPath = p.outputPathRelativeToTop() |
| 1545 | return p |
| 1546 | } |
| 1547 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1548 | var _ Path = ModuleOutPath{} |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1549 | var _ WritablePath = ModuleOutPath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1550 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1551 | func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
Pete Bentley | fcf55bf | 2019-08-16 20:14:32 +0100 | [diff] [blame] | 1552 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1553 | } |
| 1554 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1555 | // ModuleOutPathContext Subset of ModuleContext functions necessary for output path methods. |
| 1556 | type ModuleOutPathContext interface { |
| 1557 | PathContext |
| 1558 | |
| 1559 | ModuleName() string |
| 1560 | ModuleDir() string |
| 1561 | ModuleSubDir() string |
| 1562 | } |
| 1563 | |
| 1564 | func pathForModuleOut(ctx ModuleOutPathContext) OutputPath { |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1565 | return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir()) |
| 1566 | } |
| 1567 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1568 | type BazelOutPath struct { |
| 1569 | OutputPath |
| 1570 | } |
| 1571 | |
| 1572 | var _ Path = BazelOutPath{} |
| 1573 | var _ objPathProvider = BazelOutPath{} |
| 1574 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1575 | func (p BazelOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1576 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1577 | } |
| 1578 | |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1579 | // PathForVndkRefAbiDump returns an OptionalPath representing the path of the |
| 1580 | // 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] | 1581 | func PathForVndkRefAbiDump(ctx ModuleInstallPathContext, version, fileName string, |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 1582 | isNdk, isLlndkOrVndk, isGzip bool) OptionalPath { |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1583 | |
Jayant Chowdhary | ac066c6 | 2018-02-20 10:53:31 -0800 | [diff] [blame] | 1584 | arches := ctx.DeviceConfig().Arches() |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1585 | if len(arches) == 0 { |
| 1586 | panic("device build with no primary arch") |
| 1587 | } |
Jayant Chowdhary | ac066c6 | 2018-02-20 10:53:31 -0800 | [diff] [blame] | 1588 | currentArch := ctx.Arch() |
| 1589 | archNameAndVariant := currentArch.ArchType.String() |
| 1590 | if currentArch.ArchVariant != "" { |
| 1591 | archNameAndVariant += "_" + currentArch.ArchVariant |
| 1592 | } |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1593 | |
| 1594 | var dirName string |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 1595 | if isNdk { |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1596 | dirName = "ndk" |
Hsin-Yi Chen | 5348964 | 2019-07-31 17:10:45 +0800 | [diff] [blame] | 1597 | } else if isLlndkOrVndk { |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1598 | dirName = "vndk" |
Logan Chien | 41eabe6 | 2019-04-10 13:33:58 +0800 | [diff] [blame] | 1599 | } else { |
| 1600 | dirName = "platform" // opt-in libs |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 1601 | } |
Logan Chien | 5237bed | 2018-07-11 17:15:57 +0800 | [diff] [blame] | 1602 | |
Jayant Chowdhary | 34ce67d | 2018-03-08 11:00:50 -0800 | [diff] [blame] | 1603 | binderBitness := ctx.DeviceConfig().BinderBitness() |
Logan Chien | 7eefdc4 | 2018-07-11 18:10:41 +0800 | [diff] [blame] | 1604 | |
| 1605 | var ext string |
| 1606 | if isGzip { |
| 1607 | ext = ".lsdump.gz" |
| 1608 | } else { |
| 1609 | ext = ".lsdump" |
| 1610 | } |
| 1611 | |
| 1612 | return ExistentPathForSource(ctx, "prebuilts", "abi-dumps", dirName, |
| 1613 | version, binderBitness, archNameAndVariant, "source-based", |
| 1614 | fileName+ext) |
Jayant Chowdhary | 3e231fd | 2017-02-08 13:45:53 -0800 | [diff] [blame] | 1615 | } |
| 1616 | |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1617 | // PathForBazelOut returns a Path representing the paths... under an output directory dedicated to |
| 1618 | // bazel-owned outputs. |
| 1619 | func PathForBazelOut(ctx PathContext, paths ...string) BazelOutPath { |
| 1620 | execRootPathComponents := append([]string{"execroot", "__main__"}, paths...) |
| 1621 | execRootPath := filepath.Join(execRootPathComponents...) |
| 1622 | validatedExecRootPath, err := validatePath(execRootPath) |
| 1623 | if err != nil { |
| 1624 | reportPathError(ctx, err) |
| 1625 | } |
| 1626 | |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1627 | outputPath := OutputPath{basePath{"", ""}, |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1628 | ctx.Config().buildDir, |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 1629 | ctx.Config().BazelContext.OutputBase()} |
| 1630 | |
| 1631 | return BazelOutPath{ |
| 1632 | OutputPath: outputPath.withRel(validatedExecRootPath), |
| 1633 | } |
| 1634 | } |
| 1635 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1636 | // PathForModuleOut returns a Path representing the paths... under the module's |
| 1637 | // output directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1638 | func PathForModuleOut(ctx ModuleOutPathContext, paths ...string) ModuleOutPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1639 | p, err := validatePath(paths...) |
| 1640 | if err != nil { |
| 1641 | reportPathError(ctx, err) |
| 1642 | } |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1643 | return ModuleOutPath{ |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1644 | OutputPath: pathForModuleOut(ctx).withRel(p), |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1645 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | // ModuleGenPath is a Path representing the 'gen' directory in a module's output |
| 1649 | // directory. Mainly used for generated sources. |
| 1650 | type ModuleGenPath struct { |
| 1651 | ModuleOutPath |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1654 | func (p ModuleGenPath) RelativeToTop() Path { |
| 1655 | p.OutputPath = p.outputPathRelativeToTop() |
| 1656 | return p |
| 1657 | } |
| 1658 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1659 | var _ Path = ModuleGenPath{} |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1660 | var _ WritablePath = ModuleGenPath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1661 | var _ genPathProvider = ModuleGenPath{} |
| 1662 | var _ objPathProvider = ModuleGenPath{} |
| 1663 | |
| 1664 | // PathForModuleGen returns a Path representing the paths... under the module's |
| 1665 | // `gen' directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1666 | func PathForModuleGen(ctx ModuleOutPathContext, paths ...string) ModuleGenPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1667 | p, err := validatePath(paths...) |
| 1668 | if err != nil { |
| 1669 | reportPathError(ctx, err) |
| 1670 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1671 | return ModuleGenPath{ |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1672 | ModuleOutPath: ModuleOutPath{ |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1673 | OutputPath: pathForModuleOut(ctx).withRel("gen").withRel(p), |
Colin Cross | 702e0f8 | 2017-10-18 17:27:54 -0700 | [diff] [blame] | 1674 | }, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1675 | } |
| 1676 | } |
| 1677 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1678 | func (p ModuleGenPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1679 | // TODO: make a different path for local vs remote generated files? |
Dan Willemsen | 21ec490 | 2016-11-02 20:43:13 -0700 | [diff] [blame] | 1680 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1683 | func (p ModuleGenPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1684 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 1685 | } |
| 1686 | |
| 1687 | // ModuleObjPath is a Path representing the 'obj' directory in a module's output |
| 1688 | // directory. Used for compiled objects. |
| 1689 | type ModuleObjPath struct { |
| 1690 | ModuleOutPath |
| 1691 | } |
| 1692 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1693 | func (p ModuleObjPath) RelativeToTop() Path { |
| 1694 | p.OutputPath = p.outputPathRelativeToTop() |
| 1695 | return p |
| 1696 | } |
| 1697 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1698 | var _ Path = ModuleObjPath{} |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1699 | var _ WritablePath = ModuleObjPath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1700 | |
| 1701 | // PathForModuleObj returns a Path representing the paths... under the module's |
| 1702 | // 'obj' directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1703 | func PathForModuleObj(ctx ModuleOutPathContext, pathComponents ...string) ModuleObjPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1704 | p, err := validatePath(pathComponents...) |
| 1705 | if err != nil { |
| 1706 | reportPathError(ctx, err) |
| 1707 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1708 | return ModuleObjPath{PathForModuleOut(ctx, "obj", p)} |
| 1709 | } |
| 1710 | |
| 1711 | // ModuleResPath is a a Path representing the 'res' directory in a module's |
| 1712 | // output directory. |
| 1713 | type ModuleResPath struct { |
| 1714 | ModuleOutPath |
| 1715 | } |
| 1716 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1717 | func (p ModuleResPath) RelativeToTop() Path { |
| 1718 | p.OutputPath = p.outputPathRelativeToTop() |
| 1719 | return p |
| 1720 | } |
| 1721 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1722 | var _ Path = ModuleResPath{} |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1723 | var _ WritablePath = ModuleResPath{} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1724 | |
| 1725 | // PathForModuleRes returns a Path representing the paths... under the module's |
| 1726 | // 'res' directory. |
Liz Kammer | a830f3a | 2020-11-10 10:50:34 -0800 | [diff] [blame] | 1727 | func PathForModuleRes(ctx ModuleOutPathContext, pathComponents ...string) ModuleResPath { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1728 | p, err := validatePath(pathComponents...) |
| 1729 | if err != nil { |
| 1730 | reportPathError(ctx, err) |
| 1731 | } |
| 1732 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1733 | return ModuleResPath{PathForModuleOut(ctx, "res", p)} |
| 1734 | } |
| 1735 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1736 | // InstallPath is a Path representing a installed file path rooted from the build directory |
| 1737 | type InstallPath struct { |
| 1738 | basePath |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1739 | |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1740 | // The soong build directory, i.e. Config.BuildDir() |
| 1741 | buildDir string |
| 1742 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1743 | // partitionDir is the part of the InstallPath that is automatically determined according to the context. |
| 1744 | // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules. |
| 1745 | partitionDir string |
| 1746 | |
| 1747 | // makePath indicates whether this path is for Soong (false) or Make (true). |
| 1748 | makePath bool |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1751 | // Will panic if called from outside a test environment. |
| 1752 | func ensureTestOnly() { |
Martin Stjernholm | 32312eb | 2021-03-27 18:54:49 +0000 | [diff] [blame] | 1753 | if PrefixInList(os.Args, "-test.") { |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 1754 | return |
| 1755 | } |
Martin Stjernholm | 32312eb | 2021-03-27 18:54:49 +0000 | [diff] [blame] | 1756 | 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] | 1757 | } |
| 1758 | |
| 1759 | func (p InstallPath) RelativeToTop() Path { |
| 1760 | ensureTestOnly() |
| 1761 | p.buildDir = OutSoongDir |
| 1762 | return p |
| 1763 | } |
| 1764 | |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1765 | func (p InstallPath) getBuildDir() string { |
| 1766 | return p.buildDir |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 1769 | func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { |
| 1770 | panic("Not implemented") |
| 1771 | } |
| 1772 | |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 1773 | var _ Path = InstallPath{} |
| 1774 | var _ WritablePath = InstallPath{} |
| 1775 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1776 | func (p InstallPath) writablePath() {} |
| 1777 | |
| 1778 | func (p InstallPath) String() string { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1779 | if p.makePath { |
| 1780 | // Make path starts with out/ instead of out/soong. |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1781 | return filepath.Join(p.buildDir, "../", p.path) |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1782 | } else { |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1783 | return filepath.Join(p.buildDir, p.path) |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | // PartitionDir returns the path to the partition where the install path is rooted at. It is |
| 1788 | // out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules. |
| 1789 | // The ./soong is dropped if the install path is for Make. |
| 1790 | func (p InstallPath) PartitionDir() string { |
| 1791 | if p.makePath { |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1792 | return filepath.Join(p.buildDir, "../", p.partitionDir) |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1793 | } else { |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1794 | return filepath.Join(p.buildDir, p.partitionDir) |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1795 | } |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | // Join creates a new InstallPath with paths... joined with the current path. The |
| 1799 | // provided paths... may not use '..' to escape from the current path. |
| 1800 | func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath { |
| 1801 | path, err := validatePath(paths...) |
| 1802 | if err != nil { |
| 1803 | reportPathError(ctx, err) |
| 1804 | } |
| 1805 | return p.withRel(path) |
| 1806 | } |
| 1807 | |
| 1808 | func (p InstallPath) withRel(rel string) InstallPath { |
| 1809 | p.basePath = p.basePath.withRel(rel) |
| 1810 | return p |
| 1811 | } |
| 1812 | |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1813 | // ToMakePath returns a new InstallPath that points to Make's install directory instead of Soong's, |
| 1814 | // i.e. out/ instead of out/soong/. |
| 1815 | func (p InstallPath) ToMakePath() InstallPath { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1816 | p.makePath = true |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1817 | return p |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1818 | } |
| 1819 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1820 | // PathForModuleInstall returns a Path representing the install path for the |
| 1821 | // module appended with paths... |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1822 | func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath { |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1823 | os := ctx.Os() |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1824 | arch := ctx.Arch().ArchType |
| 1825 | forceOS, forceArch := ctx.InstallForceOS() |
| 1826 | if forceOS != nil { |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1827 | os = *forceOS |
| 1828 | } |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1829 | if forceArch != nil { |
| 1830 | arch = *forceArch |
| 1831 | } |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1832 | partition := modulePartition(ctx, os) |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1833 | |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1834 | ret := pathForInstall(ctx, os, arch, partition, ctx.Debug(), pathComponents...) |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1835 | |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 1836 | if ctx.InstallBypassMake() && ctx.Config().KatiEnabled() { |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1837 | ret = ret.ToMakePath() |
| 1838 | } |
| 1839 | |
| 1840 | return ret |
| 1841 | } |
| 1842 | |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1843 | func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string, debug bool, |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1844 | pathComponents ...string) InstallPath { |
| 1845 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1846 | var partionPaths []string |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1847 | |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1848 | if os.Class == Device { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1849 | partionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1850 | } else { |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1851 | osName := os.String() |
| 1852 | if os == Linux { |
| 1853 | // instead of linux_glibc |
| 1854 | osName = "linux" |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 1855 | } |
Jiyong Park | 87788b5 | 2020-09-01 12:37:45 +0900 | [diff] [blame] | 1856 | // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH) |
| 1857 | // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem |
| 1858 | // to have a plan to fix it (see the comment in build/make/core/envsetup.mk). |
| 1859 | // Let's keep using x86 for the existing cases until we have a need to support |
| 1860 | // other architectures. |
| 1861 | archName := arch.String() |
| 1862 | if os.Class == Host && (arch == X86_64 || arch == Common) { |
| 1863 | archName = "x86" |
| 1864 | } |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1865 | partionPaths = []string{"host", osName + "-" + archName, partition} |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1866 | } |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1867 | if debug { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1868 | partionPaths = append([]string{"debug"}, partionPaths...) |
Dan Willemsen | 782a2d1 | 2015-12-21 14:55:28 -0800 | [diff] [blame] | 1869 | } |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1870 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1871 | partionPath, err := validatePath(partionPaths...) |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1872 | if err != nil { |
| 1873 | reportPathError(ctx, err) |
| 1874 | } |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1875 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1876 | base := InstallPath{ |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1877 | basePath: basePath{partionPath, ""}, |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1878 | buildDir: ctx.Config().buildDir, |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1879 | partitionDir: partionPath, |
| 1880 | makePath: false, |
| 1881 | } |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 1882 | |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1883 | return base.Join(ctx, pathComponents...) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1884 | } |
| 1885 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 1886 | func pathForNdkOrSdkInstall(ctx PathContext, prefix string, paths []string) InstallPath { |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1887 | base := InstallPath{ |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 1888 | basePath: basePath{prefix, ""}, |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 1889 | buildDir: ctx.Config().buildDir, |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1890 | partitionDir: prefix, |
| 1891 | makePath: false, |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1892 | } |
Jiyong Park | 957bcd9 | 2020-10-20 18:23:33 +0900 | [diff] [blame] | 1893 | return base.Join(ctx, paths...) |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1894 | } |
| 1895 | |
Nicolas Geoffray | 1228e9c | 2020-02-27 13:45:35 +0000 | [diff] [blame] | 1896 | func PathForNdkInstall(ctx PathContext, paths ...string) InstallPath { |
| 1897 | return pathForNdkOrSdkInstall(ctx, "ndk", paths) |
| 1898 | } |
| 1899 | |
| 1900 | func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath { |
| 1901 | return pathForNdkOrSdkInstall(ctx, "mainline-sdks", paths) |
| 1902 | } |
| 1903 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 1904 | func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1905 | rel := Rel(ctx, PathForOutput(ctx, "target", "product", ctx.Config().DeviceName()).String(), path.String()) |
| 1906 | |
| 1907 | return "/" + rel |
| 1908 | } |
| 1909 | |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1910 | func modulePartition(ctx ModuleInstallPathContext, os OsType) string { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1911 | var partition string |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1912 | if ctx.InstallInTestcases() { |
| 1913 | // "testcases" install directory can be used for host or device modules. |
Jaewoong Jung | 0949f31 | 2019-09-11 10:25:18 -0700 | [diff] [blame] | 1914 | partition = "testcases" |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1915 | } else if os.Class == Device { |
| 1916 | if ctx.InstallInData() { |
| 1917 | partition = "data" |
| 1918 | } else if ctx.InstallInRamdisk() { |
| 1919 | if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() { |
| 1920 | partition = "recovery/root/first_stage_ramdisk" |
| 1921 | } else { |
| 1922 | partition = "ramdisk" |
| 1923 | } |
| 1924 | if !ctx.InstallInRoot() { |
| 1925 | partition += "/system" |
| 1926 | } |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 1927 | } else if ctx.InstallInVendorRamdisk() { |
Yifan Hong | 39143a9 | 2020-10-26 12:43:12 -0700 | [diff] [blame] | 1928 | // The module is only available after switching root into |
| 1929 | // /first_stage_ramdisk. To expose the module before switching root |
| 1930 | // on a device without a dedicated recovery partition, install the |
| 1931 | // recovery variant. |
Yifan Hong | dd8dacc | 2020-10-21 15:40:17 -0700 | [diff] [blame] | 1932 | if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() { |
Petri Gynther | ac22956 | 2021-03-02 23:44:02 -0800 | [diff] [blame] | 1933 | partition = "vendor_ramdisk/first_stage_ramdisk" |
Yifan Hong | dd8dacc | 2020-10-21 15:40:17 -0700 | [diff] [blame] | 1934 | } else { |
Petri Gynther | ac22956 | 2021-03-02 23:44:02 -0800 | [diff] [blame] | 1935 | partition = "vendor_ramdisk" |
Yifan Hong | dd8dacc | 2020-10-21 15:40:17 -0700 | [diff] [blame] | 1936 | } |
| 1937 | if !ctx.InstallInRoot() { |
| 1938 | partition += "/system" |
| 1939 | } |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1940 | } else if ctx.InstallInRecovery() { |
| 1941 | if ctx.InstallInRoot() { |
| 1942 | partition = "recovery/root" |
| 1943 | } else { |
| 1944 | // the layout of recovery partion is the same as that of system partition |
| 1945 | partition = "recovery/root/system" |
| 1946 | } |
| 1947 | } else if ctx.SocSpecific() { |
| 1948 | partition = ctx.DeviceConfig().VendorPath() |
| 1949 | } else if ctx.DeviceSpecific() { |
| 1950 | partition = ctx.DeviceConfig().OdmPath() |
| 1951 | } else if ctx.ProductSpecific() { |
| 1952 | partition = ctx.DeviceConfig().ProductPath() |
| 1953 | } else if ctx.SystemExtSpecific() { |
| 1954 | partition = ctx.DeviceConfig().SystemExtPath() |
| 1955 | } else if ctx.InstallInRoot() { |
| 1956 | partition = "root" |
Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1957 | } else { |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1958 | partition = "system" |
Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1959 | } |
Colin Cross | 6e35940 | 2020-02-10 15:29:54 -0800 | [diff] [blame] | 1960 | if ctx.InstallInSanitizerDir() { |
| 1961 | partition = "data/asan/" + partition |
Yifan Hong | 82db735 | 2020-01-21 16:12:26 -0800 | [diff] [blame] | 1962 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1963 | } |
| 1964 | return partition |
| 1965 | } |
| 1966 | |
Colin Cross | 609c49a | 2020-02-13 13:20:11 -0800 | [diff] [blame] | 1967 | type InstallPaths []InstallPath |
| 1968 | |
| 1969 | // Paths returns the InstallPaths as a Paths |
| 1970 | func (p InstallPaths) Paths() Paths { |
| 1971 | if p == nil { |
| 1972 | return nil |
| 1973 | } |
| 1974 | ret := make(Paths, len(p)) |
| 1975 | for i, path := range p { |
| 1976 | ret[i] = path |
| 1977 | } |
| 1978 | return ret |
| 1979 | } |
| 1980 | |
| 1981 | // Strings returns the string forms of the install paths. |
| 1982 | func (p InstallPaths) Strings() []string { |
| 1983 | if p == nil { |
| 1984 | return nil |
| 1985 | } |
| 1986 | ret := make([]string, len(p)) |
| 1987 | for i, path := range p { |
| 1988 | ret[i] = path.String() |
| 1989 | } |
| 1990 | return ret |
| 1991 | } |
| 1992 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1993 | // validateSafePath validates a path that we trust (may contain ninja variables). |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1994 | // Ensures that each path component does not attempt to leave its component. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1995 | func validateSafePath(pathComponents ...string) (string, error) { |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 1996 | for _, path := range pathComponents { |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 1997 | path := filepath.Clean(path) |
| 1998 | if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 1999 | return "", fmt.Errorf("Path is outside directory: %s", path) |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 2000 | } |
| 2001 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 2002 | // TODO: filepath.Join isn't necessarily correct with embedded ninja |
| 2003 | // variables. '..' may remove the entire ninja variable, even if it |
| 2004 | // will be expanded to multiple nested directories. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 2005 | return filepath.Join(pathComponents...), nil |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 2008 | // validatePath validates that a path does not include ninja variables, and that |
| 2009 | // each path component does not attempt to leave its component. Returns a joined |
| 2010 | // version of each path component. |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 2011 | func validatePath(pathComponents ...string) (string, error) { |
Jeff Gaston | 734e380 | 2017-04-10 15:47:24 -0700 | [diff] [blame] | 2012 | for _, path := range pathComponents { |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 2013 | if strings.Contains(path, "$") { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 2014 | return "", fmt.Errorf("Path contains invalid character($): %s", path) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 2015 | } |
| 2016 | } |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 2017 | return validateSafePath(pathComponents...) |
Colin Cross | 6e18ca4 | 2015-07-14 18:55:36 -0700 | [diff] [blame] | 2018 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 2019 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 2020 | func PathForPhony(ctx PathContext, phony string) WritablePath { |
| 2021 | if strings.ContainsAny(phony, "$/") { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 2022 | ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony) |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 2023 | } |
Paul Duffin | 74abc5d | 2021-03-24 09:24:59 +0000 | [diff] [blame] | 2024 | return PhonyPath{basePath{phony, ""}} |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 2025 | } |
| 2026 | |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 2027 | type PhonyPath struct { |
| 2028 | basePath |
| 2029 | } |
| 2030 | |
| 2031 | func (p PhonyPath) writablePath() {} |
| 2032 | |
Paul Duffin | d65c58b | 2021-03-24 09:22:07 +0000 | [diff] [blame] | 2033 | func (p PhonyPath) getBuildDir() string { |
| 2034 | // A phone path cannot contain any / so cannot be relative to the build directory. |
| 2035 | return "" |
Paul Duffin | 9b478b0 | 2019-12-10 13:41:51 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 2038 | func (p PhonyPath) RelativeToTop() Path { |
| 2039 | ensureTestOnly() |
| 2040 | // A phony path cannot contain any / so does not have a build directory so switching to a new |
| 2041 | // build directory has no effect so just return this path. |
| 2042 | return p |
| 2043 | } |
| 2044 | |
Hans MÃ¥nsson | d3f2bd7 | 2020-11-27 12:37:28 +0100 | [diff] [blame] | 2045 | func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath { |
| 2046 | panic("Not implemented") |
| 2047 | } |
| 2048 | |
Colin Cross | 74e3fe4 | 2017-12-11 15:51:44 -0800 | [diff] [blame] | 2049 | var _ Path = PhonyPath{} |
| 2050 | var _ WritablePath = PhonyPath{} |
| 2051 | |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 2052 | type testPath struct { |
| 2053 | basePath |
| 2054 | } |
| 2055 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 2056 | func (p testPath) RelativeToTop() Path { |
| 2057 | ensureTestOnly() |
| 2058 | return p |
| 2059 | } |
| 2060 | |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 2061 | func (p testPath) String() string { |
| 2062 | return p.path |
| 2063 | } |
| 2064 | |
Paul Duffin | 85d8f0d | 2021-03-24 10:18:18 +0000 | [diff] [blame] | 2065 | var _ Path = testPath{} |
| 2066 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2067 | // PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from |
| 2068 | // within tests. |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 2069 | func PathForTesting(paths ...string) Path { |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 2070 | p, err := validateSafePath(paths...) |
| 2071 | if err != nil { |
| 2072 | panic(err) |
| 2073 | } |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 2074 | return testPath{basePath{path: p, rel: p}} |
| 2075 | } |
| 2076 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2077 | // PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests. |
| 2078 | func PathsForTesting(strs ...string) Paths { |
Colin Cross | 5b52959 | 2017-05-09 13:34:34 -0700 | [diff] [blame] | 2079 | p := make(Paths, len(strs)) |
| 2080 | for i, s := range strs { |
| 2081 | p[i] = PathForTesting(s) |
| 2082 | } |
| 2083 | |
| 2084 | return p |
| 2085 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2086 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2087 | type testPathContext struct { |
| 2088 | config Config |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2089 | } |
| 2090 | |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2091 | func (x *testPathContext) Config() Config { return x.config } |
| 2092 | func (x *testPathContext) AddNinjaFileDeps(...string) {} |
| 2093 | |
| 2094 | // PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with |
| 2095 | // PathForOutput. |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 2096 | func PathContextForTesting(config Config) PathContext { |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2097 | return &testPathContext{ |
| 2098 | config: config, |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 2099 | } |
| 2100 | } |
| 2101 | |
Ulya Trafimovich | ccc8c85 | 2020-10-14 11:29:07 +0100 | [diff] [blame] | 2102 | type testModuleInstallPathContext struct { |
| 2103 | baseModuleContext |
| 2104 | |
| 2105 | inData bool |
| 2106 | inTestcases bool |
| 2107 | inSanitizerDir bool |
| 2108 | inRamdisk bool |
| 2109 | inVendorRamdisk bool |
| 2110 | inRecovery bool |
| 2111 | inRoot bool |
| 2112 | forceOS *OsType |
| 2113 | forceArch *ArchType |
| 2114 | } |
| 2115 | |
| 2116 | func (m testModuleInstallPathContext) Config() Config { |
| 2117 | return m.baseModuleContext.config |
| 2118 | } |
| 2119 | |
| 2120 | func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {} |
| 2121 | |
| 2122 | func (m testModuleInstallPathContext) InstallInData() bool { |
| 2123 | return m.inData |
| 2124 | } |
| 2125 | |
| 2126 | func (m testModuleInstallPathContext) InstallInTestcases() bool { |
| 2127 | return m.inTestcases |
| 2128 | } |
| 2129 | |
| 2130 | func (m testModuleInstallPathContext) InstallInSanitizerDir() bool { |
| 2131 | return m.inSanitizerDir |
| 2132 | } |
| 2133 | |
| 2134 | func (m testModuleInstallPathContext) InstallInRamdisk() bool { |
| 2135 | return m.inRamdisk |
| 2136 | } |
| 2137 | |
| 2138 | func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool { |
| 2139 | return m.inVendorRamdisk |
| 2140 | } |
| 2141 | |
| 2142 | func (m testModuleInstallPathContext) InstallInRecovery() bool { |
| 2143 | return m.inRecovery |
| 2144 | } |
| 2145 | |
| 2146 | func (m testModuleInstallPathContext) InstallInRoot() bool { |
| 2147 | return m.inRoot |
| 2148 | } |
| 2149 | |
| 2150 | func (m testModuleInstallPathContext) InstallBypassMake() bool { |
| 2151 | return false |
| 2152 | } |
| 2153 | |
| 2154 | func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) { |
| 2155 | return m.forceOS, m.forceArch |
| 2156 | } |
| 2157 | |
| 2158 | // Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is |
| 2159 | // default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are |
| 2160 | // delegated to it will panic. |
| 2161 | func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext { |
| 2162 | ctx := &testModuleInstallPathContext{} |
| 2163 | ctx.config = config |
| 2164 | ctx.os = Android |
| 2165 | return ctx |
| 2166 | } |
| 2167 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2168 | // Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if |
| 2169 | // targetPath is not inside basePath. |
| 2170 | func Rel(ctx PathContext, basePath string, targetPath string) string { |
| 2171 | rel, isRel := MaybeRel(ctx, basePath, targetPath) |
| 2172 | if !isRel { |
Ulya Trafimovich | 5ab276a | 2020-08-25 12:45:15 +0100 | [diff] [blame] | 2173 | ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2174 | return "" |
| 2175 | } |
| 2176 | return rel |
| 2177 | } |
| 2178 | |
| 2179 | // MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if |
| 2180 | // targetPath is not inside basePath. |
| 2181 | func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2182 | rel, isRel, err := maybeRelErr(basePath, targetPath) |
| 2183 | if err != nil { |
| 2184 | reportPathError(ctx, err) |
| 2185 | } |
| 2186 | return rel, isRel |
| 2187 | } |
| 2188 | |
| 2189 | func maybeRelErr(basePath string, targetPath string) (string, bool, error) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2190 | // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first. |
| 2191 | if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2192 | return "", false, nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2193 | } |
| 2194 | rel, err := filepath.Rel(basePath, targetPath) |
| 2195 | if err != nil { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2196 | return "", false, err |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2197 | } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") { |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2198 | return "", false, nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2199 | } |
Dan Willemsen | 633c502 | 2019-04-12 11:11:38 -0700 | [diff] [blame] | 2200 | return rel, true, nil |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 2201 | } |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 2202 | |
| 2203 | // Writes a file to the output directory. Attempting to write directly to the output directory |
| 2204 | // will fail due to the sandbox of the soong_build process. |
| 2205 | func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error { |
| 2206 | return ioutil.WriteFile(absolutePath(path.String()), data, perm) |
| 2207 | } |
| 2208 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 2209 | func RemoveAllOutputDir(path WritablePath) error { |
| 2210 | return os.RemoveAll(absolutePath(path.String())) |
| 2211 | } |
| 2212 | |
| 2213 | func CreateOutputDirIfNonexistent(path WritablePath, perm os.FileMode) error { |
| 2214 | dir := absolutePath(path.String()) |
| 2215 | if _, err := os.Stat(dir); os.IsNotExist(err) { |
| 2216 | return os.MkdirAll(dir, os.ModePerm) |
| 2217 | } else { |
| 2218 | return err |
| 2219 | } |
| 2220 | } |
| 2221 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 2222 | func absolutePath(path string) string { |
| 2223 | if filepath.IsAbs(path) { |
| 2224 | return path |
| 2225 | } |
| 2226 | return filepath.Join(absSrcDir, path) |
| 2227 | } |
Chris Parsons | 216e10a | 2020-07-09 17:12:52 -0400 | [diff] [blame] | 2228 | |
| 2229 | // A DataPath represents the path of a file to be used as data, for example |
| 2230 | // a test library to be installed alongside a test. |
| 2231 | // The data file should be installed (copied from `<SrcPath>`) to |
| 2232 | // `<install_root>/<RelativeInstallPath>/<filename>`, or |
| 2233 | // `<install_root>/<filename>` if RelativeInstallPath is empty. |
| 2234 | type DataPath struct { |
| 2235 | // The path of the data file that should be copied into the data directory |
| 2236 | SrcPath Path |
| 2237 | // The install path of the data file, relative to the install root. |
| 2238 | RelativeInstallPath string |
| 2239 | } |
Colin Cross | dcf71b2 | 2021-02-01 13:59:03 -0800 | [diff] [blame] | 2240 | |
| 2241 | // PathsIfNonNil returns a Paths containing only the non-nil input arguments. |
| 2242 | func PathsIfNonNil(paths ...Path) Paths { |
| 2243 | if len(paths) == 0 { |
| 2244 | // Fast path for empty argument list |
| 2245 | return nil |
| 2246 | } else if len(paths) == 1 { |
| 2247 | // Fast path for a single argument |
| 2248 | if paths[0] != nil { |
| 2249 | return paths |
| 2250 | } else { |
| 2251 | return nil |
| 2252 | } |
| 2253 | } |
| 2254 | ret := make(Paths, 0, len(paths)) |
| 2255 | for _, path := range paths { |
| 2256 | if path != nil { |
| 2257 | ret = append(ret, path) |
| 2258 | } |
| 2259 | } |
| 2260 | if len(ret) == 0 { |
| 2261 | return nil |
| 2262 | } |
| 2263 | return ret |
| 2264 | } |