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