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