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