blob: a944c48db1e8eec96740f9a63abd9bffbff30634 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross6e18ca42015-07-14 18:55:36 -070018 "fmt"
Colin Cross988414c2020-01-11 01:11:46 +000019 "os"
Colin Cross6a745c62015-06-16 16:38:10 -070020 "path/filepath"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070021 "reflect"
Chris Wailesb2703ad2021-07-30 13:25:42 -070022 "regexp"
Colin Cross5e6cfbe2017-11-03 15:20:35 -070023 "sort"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070024 "strings"
25
26 "github.com/google/blueprint"
Yu Liu3cadf7d2024-10-24 18:47:06 +000027 "github.com/google/blueprint/gobtools"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070028 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080029)
30
Colin Cross988414c2020-01-11 01:11:46 +000031var absSrcDir string
32
Dan Willemsen34cc69e2015-09-23 15:26:20 -070033// PathContext is the subset of a (Module|Singleton)Context required by the
34// Path methods.
35type PathContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -080036 Config() Config
Dan Willemsen7b310ee2015-12-18 15:11:17 -080037 AddNinjaFileDeps(deps ...string)
Colin Cross3f40fa42015-01-30 17:27:36 -080038}
39
Colin Cross7f19f372016-11-01 11:10:25 -070040type PathGlobContext interface {
Colin Cross662d6142022-11-03 20:38:01 -070041 PathContext
Colin Cross7f19f372016-11-01 11:10:25 -070042 GlobWithDeps(globPattern string, excludes []string) ([]string, error)
43}
44
Colin Crossaabf6792017-11-29 00:27:14 -080045var _ PathContext = SingletonContext(nil)
46var _ PathContext = ModuleContext(nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -070047
Ulya Trafimovich8640ab92020-05-11 18:06:15 +010048// "Null" path context is a minimal path context for a given config.
49type NullPathContext struct {
50 config Config
51}
52
53func (NullPathContext) AddNinjaFileDeps(...string) {}
54func (ctx NullPathContext) Config() Config { return ctx.config }
55
Liz Kammera830f3a2020-11-10 10:50:34 -080056// EarlyModulePathContext is a subset of EarlyModuleContext methods required by the
57// Path methods. These path methods can be called before any mutators have run.
58type EarlyModulePathContext interface {
Liz Kammera830f3a2020-11-10 10:50:34 -080059 PathGlobContext
60
61 ModuleDir() string
62 ModuleErrorf(fmt string, args ...interface{})
Cole Fausta963b942024-04-11 17:43:00 -070063 OtherModulePropertyErrorf(module Module, property, fmt string, args ...interface{})
Liz Kammera830f3a2020-11-10 10:50:34 -080064}
65
66var _ EarlyModulePathContext = ModuleContext(nil)
67
68// Glob globs files and directories matching globPattern relative to ModuleDir(),
69// paths in the excludes parameter will be omitted.
70func Glob(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths {
71 ret, err := ctx.GlobWithDeps(globPattern, excludes)
72 if err != nil {
73 ctx.ModuleErrorf("glob: %s", err.Error())
74 }
75 return pathsForModuleSrcFromFullPath(ctx, ret, true)
76}
77
78// GlobFiles globs *only* files (not directories) matching globPattern relative to ModuleDir().
79// Paths in the excludes parameter will be omitted.
80func GlobFiles(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths {
81 ret, err := ctx.GlobWithDeps(globPattern, excludes)
82 if err != nil {
83 ctx.ModuleErrorf("glob: %s", err.Error())
84 }
85 return pathsForModuleSrcFromFullPath(ctx, ret, false)
86}
87
88// ModuleWithDepsPathContext is a subset of *ModuleContext methods required by
89// the Path methods that rely on module dependencies having been resolved.
90type ModuleWithDepsPathContext interface {
91 EarlyModulePathContext
Cole Faust55b56fe2024-08-23 12:06:11 -070092 OtherModuleProviderContext
Colin Cross648daea2024-09-12 14:35:29 -070093 VisitDirectDeps(visit func(Module))
Yu Liud3228ac2024-11-08 23:11:47 +000094 VisitDirectDepsProxy(visit func(ModuleProxy))
95 VisitDirectDepsProxyWithTag(tag blueprint.DependencyTag, visit func(ModuleProxy))
Paul Duffin40131a32021-07-09 17:10:35 +010096 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
Cole Faust4e2bf9f2024-09-11 13:26:20 -070097 HasMutatorFinished(mutatorName string) bool
Liz Kammera830f3a2020-11-10 10:50:34 -080098}
99
100// ModuleMissingDepsPathContext is a subset of *ModuleContext methods required by
101// the Path methods that rely on module dependencies having been resolved and ability to report
102// missing dependency errors.
103type ModuleMissingDepsPathContext interface {
104 ModuleWithDepsPathContext
105 AddMissingDependencies(missingDeps []string)
106}
107
Dan Willemsen00269f22017-07-06 16:59:48 -0700108type ModuleInstallPathContext interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700109 BaseModuleContext
Dan Willemsen00269f22017-07-06 16:59:48 -0700110
111 InstallInData() bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700112 InstallInTestcases() bool
Dan Willemsen00269f22017-07-06 16:59:48 -0700113 InstallInSanitizerDir() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -0800114 InstallInRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700115 InstallInVendorRamdisk() bool
Inseob Kim08758f02021-04-08 21:13:22 +0900116 InstallInDebugRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900117 InstallInRecovery() bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700118 InstallInRoot() bool
Colin Crossea30d852023-11-29 16:00:16 -0800119 InstallInOdm() bool
120 InstallInProduct() bool
121 InstallInVendor() bool
Spandan Das27ff7672024-11-06 19:23:57 +0000122 InstallInSystemDlkm() bool
123 InstallInVendorDlkm() bool
124 InstallInOdmDlkm() bool
Jiyong Park87788b52020-09-01 12:37:45 +0900125 InstallForceOS() (*OsType, *ArchType)
Dan Willemsen00269f22017-07-06 16:59:48 -0700126}
127
128var _ ModuleInstallPathContext = ModuleContext(nil)
129
Cole Faust11edf552023-10-13 11:32:14 -0700130type baseModuleContextToModuleInstallPathContext struct {
131 BaseModuleContext
132}
133
134func (ctx *baseModuleContextToModuleInstallPathContext) InstallInData() bool {
135 return ctx.Module().InstallInData()
136}
137
138func (ctx *baseModuleContextToModuleInstallPathContext) InstallInTestcases() bool {
139 return ctx.Module().InstallInTestcases()
140}
141
142func (ctx *baseModuleContextToModuleInstallPathContext) InstallInSanitizerDir() bool {
143 return ctx.Module().InstallInSanitizerDir()
144}
145
146func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRamdisk() bool {
147 return ctx.Module().InstallInRamdisk()
148}
149
150func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendorRamdisk() bool {
151 return ctx.Module().InstallInVendorRamdisk()
152}
153
154func (ctx *baseModuleContextToModuleInstallPathContext) InstallInDebugRamdisk() bool {
155 return ctx.Module().InstallInDebugRamdisk()
156}
157
158func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRecovery() bool {
159 return ctx.Module().InstallInRecovery()
160}
161
162func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRoot() bool {
163 return ctx.Module().InstallInRoot()
164}
165
Colin Crossea30d852023-11-29 16:00:16 -0800166func (ctx *baseModuleContextToModuleInstallPathContext) InstallInOdm() bool {
167 return ctx.Module().InstallInOdm()
168}
169
170func (ctx *baseModuleContextToModuleInstallPathContext) InstallInProduct() bool {
171 return ctx.Module().InstallInProduct()
172}
173
174func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendor() bool {
175 return ctx.Module().InstallInVendor()
176}
177
Spandan Das27ff7672024-11-06 19:23:57 +0000178func (ctx *baseModuleContextToModuleInstallPathContext) InstallInSystemDlkm() bool {
179 return ctx.Module().InstallInSystemDlkm()
180}
181
182func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendorDlkm() bool {
183 return ctx.Module().InstallInVendorDlkm()
184}
185
186func (ctx *baseModuleContextToModuleInstallPathContext) InstallInOdmDlkm() bool {
187 return ctx.Module().InstallInOdmDlkm()
188}
189
Cole Faust11edf552023-10-13 11:32:14 -0700190func (ctx *baseModuleContextToModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
191 return ctx.Module().InstallForceOS()
192}
193
194var _ ModuleInstallPathContext = (*baseModuleContextToModuleInstallPathContext)(nil)
195
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700196// errorfContext is the interface containing the Errorf method matching the
197// Errorf method in blueprint.SingletonContext.
198type errorfContext interface {
199 Errorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800200}
201
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700202var _ errorfContext = blueprint.SingletonContext(nil)
203
Spandan Das59a4a2b2024-01-09 21:35:56 +0000204// ModuleErrorfContext is the interface containing the ModuleErrorf method matching
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700205// the ModuleErrorf method in blueprint.ModuleContext.
Spandan Das59a4a2b2024-01-09 21:35:56 +0000206type ModuleErrorfContext interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700207 ModuleErrorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800208}
209
Spandan Das59a4a2b2024-01-09 21:35:56 +0000210var _ ModuleErrorfContext = blueprint.ModuleContext(nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700211
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700212// reportPathError will register an error with the attached context. It
213// attempts ctx.ModuleErrorf for a better error message first, then falls
214// back to ctx.Errorf.
Colin Cross1ccfcc32018-02-22 13:54:26 -0800215func reportPathError(ctx PathContext, err error) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100216 ReportPathErrorf(ctx, "%s", err.Error())
Colin Cross1ccfcc32018-02-22 13:54:26 -0800217}
218
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100219// ReportPathErrorf will register an error with the attached context. It
Colin Cross1ccfcc32018-02-22 13:54:26 -0800220// attempts ctx.ModuleErrorf for a better error message first, then falls
221// back to ctx.Errorf.
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100222func ReportPathErrorf(ctx PathContext, format string, args ...interface{}) {
Spandan Das59a4a2b2024-01-09 21:35:56 +0000223 if mctx, ok := ctx.(ModuleErrorfContext); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700224 mctx.ModuleErrorf(format, args...)
225 } else if ectx, ok := ctx.(errorfContext); ok {
226 ectx.Errorf(format, args...)
227 } else {
228 panic(fmt.Sprintf(format, args...))
Colin Crossf2298272015-05-12 11:36:53 -0700229 }
230}
231
Yu Liu2a815b62025-02-21 20:46:25 +0000232// TODO(b/397766191): Change the signature to take ModuleProxy
233// Please only access the module's internal data through providers.
Colin Cross5e708052019-08-06 13:59:50 -0700234func pathContextName(ctx PathContext, module blueprint.Module) string {
235 if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok {
236 return x.ModuleName(module)
237 } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok {
238 return x.OtherModuleName(module)
239 }
240 return "unknown"
241}
242
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700243type Path interface {
244 // Returns the path in string form
245 String() string
246
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700247 // Ext returns the extension of the last element of the path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700248 Ext() string
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700249
250 // Base returns the last element of the path
251 Base() string
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800252
253 // Rel returns the portion of the path relative to the directory it was created from. For
254 // example, Rel on a PathsForModuleSrc would return the path relative to the module source
Colin Cross0db55682017-12-05 15:36:55 -0800255 // directory, and OutputPath.Join("foo").Rel() would return "foo".
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800256 Rel() string
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000257
Colin Cross7707b242024-07-26 12:02:36 -0700258 // WithoutRel returns a new Path with no relative path, i.e. Rel() will return the same value as Base().
259 WithoutRel() Path
260
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000261 // RelativeToTop returns a new path relative to the top, it is provided solely for use in tests.
262 //
263 // It is guaranteed to always return the same type as it is called on, e.g. if called on an
264 // InstallPath then the returned value can be converted to an InstallPath.
265 //
266 // A standard build has the following structure:
267 // ../top/
268 // out/ - make install files go here.
Colin Cross3b1c6842024-07-26 11:52:57 -0700269 // out/soong - this is the outDir passed to NewTestConfig()
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000270 // ... - the source files
271 //
272 // This function converts a path so that it appears relative to the ../top/ directory, i.e.
Colin Cross3b1c6842024-07-26 11:52:57 -0700273 // * Make install paths, which have the pattern "outDir/../<path>" are converted into the top
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000274 // relative path "out/<path>"
Colin Cross3b1c6842024-07-26 11:52:57 -0700275 // * Soong install paths and other writable paths, which have the pattern "outDir/soong/<path>" are
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000276 // converted into the top relative path "out/soong/<path>".
277 // * Source paths are already relative to the top.
278 // * Phony paths are not relative to anything.
279 // * toolDepPath have an absolute but known value in so don't need making relative to anything in
280 // order to test.
281 RelativeToTop() Path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700282}
283
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000284const (
Colin Cross3b1c6842024-07-26 11:52:57 -0700285 testOutDir = "out"
286 testOutSoongSubDir = "/soong"
287 TestOutSoongDir = testOutDir + testOutSoongSubDir
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000288)
289
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700290// WritablePath is a type of path that can be used as an output for build rules.
291type WritablePath interface {
292 Path
293
Paul Duffin9b478b02019-12-10 13:41:51 +0000294 // return the path to the build directory.
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200295 getSoongOutDir() string
Paul Duffin9b478b02019-12-10 13:41:51 +0000296
Jeff Gaston734e3802017-04-10 15:47:24 -0700297 // the writablePath method doesn't directly do anything,
298 // but it allows a struct to distinguish between whether or not it implements the WritablePath interface
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700299 writablePath()
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +0100300
301 ReplaceExtension(ctx PathContext, ext string) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700302}
303
304type genPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800305 genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath
yangbill6d032dd2024-04-18 03:05:49 +0000306 genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700307}
308type objPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800309 objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700310}
311type resPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800312 resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700313}
314
315// GenPathWithExt derives a new file path in ctx's generated sources directory
316// from the current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800317func GenPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700318 if path, ok := p.(genPathProvider); ok {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700319 return path.genPathWithExt(ctx, subdir, ext)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700320 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100321 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700322 return PathForModuleGen(ctx)
323}
324
yangbill6d032dd2024-04-18 03:05:49 +0000325// GenPathWithExtAndTrimExt derives a new file path in ctx's generated sources directory
326// from the current path, but with the new extension and trim the suffix.
327func GenPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir string, p Path, ext string, trimExt string) ModuleGenPath {
328 if path, ok := p.(genPathProvider); ok {
329 return path.genPathWithExtAndTrimExt(ctx, subdir, ext, trimExt)
330 }
331 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
332 return PathForModuleGen(ctx)
333}
334
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700335// ObjPathWithExt derives a new file path in ctx's object directory from the
336// current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800337func ObjPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700338 if path, ok := p.(objPathProvider); ok {
339 return path.objPathWithExt(ctx, subdir, ext)
340 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100341 ReportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700342 return PathForModuleObj(ctx)
343}
344
345// ResPathWithName derives a new path in ctx's output resource directory, using
346// the current path to create the directory name, and the `name` argument for
347// the filename.
Liz Kammera830f3a2020-11-10 10:50:34 -0800348func ResPathWithName(ctx ModuleOutPathContext, p Path, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700349 if path, ok := p.(resPathProvider); ok {
350 return path.resPathWithName(ctx, name)
351 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100352 ReportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700353 return PathForModuleRes(ctx)
354}
355
356// OptionalPath is a container that may or may not contain a valid Path.
357type OptionalPath struct {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100358 path Path // nil if invalid.
359 invalidReason string // Not applicable if path != nil. "" if the reason is unknown.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700360}
361
Yu Liu467d7c52024-09-18 21:54:44 +0000362type optionalPathGob struct {
363 Path Path
364 InvalidReason string
365}
366
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700367// OptionalPathForPath returns an OptionalPath containing the path.
368func OptionalPathForPath(path Path) OptionalPath {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100369 return OptionalPath{path: path}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700370}
371
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100372// InvalidOptionalPath returns an OptionalPath that is invalid with the given reason.
373func InvalidOptionalPath(reason string) OptionalPath {
374
375 return OptionalPath{invalidReason: reason}
376}
377
Yu Liu467d7c52024-09-18 21:54:44 +0000378func (p *OptionalPath) ToGob() *optionalPathGob {
379 return &optionalPathGob{
380 Path: p.path,
381 InvalidReason: p.invalidReason,
382 }
383}
384
385func (p *OptionalPath) FromGob(data *optionalPathGob) {
386 p.path = data.Path
387 p.invalidReason = data.InvalidReason
388}
389
390func (p OptionalPath) GobEncode() ([]byte, error) {
Yu Liu3cadf7d2024-10-24 18:47:06 +0000391 return gobtools.CustomGobEncode[optionalPathGob](&p)
Yu Liu467d7c52024-09-18 21:54:44 +0000392}
393
394func (p *OptionalPath) GobDecode(data []byte) error {
Yu Liu3cadf7d2024-10-24 18:47:06 +0000395 return gobtools.CustomGobDecode[optionalPathGob](data, p)
Yu Liu467d7c52024-09-18 21:54:44 +0000396}
397
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700398// Valid returns whether there is a valid path
399func (p OptionalPath) Valid() bool {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100400 return p.path != nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700401}
402
403// Path returns the Path embedded in this OptionalPath. You must be sure that
404// there is a valid path, since this method will panic if there is not.
405func (p OptionalPath) Path() Path {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100406 if p.path == nil {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100407 msg := "Requesting an invalid path"
408 if p.invalidReason != "" {
409 msg += ": " + p.invalidReason
410 }
411 panic(msg)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700412 }
413 return p.path
414}
415
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100416// InvalidReason returns the reason that the optional path is invalid, or "" if it is valid.
417func (p OptionalPath) InvalidReason() string {
418 if p.path != nil {
419 return ""
420 }
421 if p.invalidReason == "" {
422 return "unknown"
423 }
424 return p.invalidReason
425}
426
Paul Duffinef081852021-05-13 11:11:15 +0100427// AsPaths converts the OptionalPath into Paths.
428//
429// It returns nil if this is not valid, or a single length slice containing the Path embedded in
430// this OptionalPath.
431func (p OptionalPath) AsPaths() Paths {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100432 if p.path == nil {
Paul Duffinef081852021-05-13 11:11:15 +0100433 return nil
434 }
435 return Paths{p.path}
436}
437
Paul Duffinafdd4062021-03-30 19:44:07 +0100438// RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the
439// result of calling Path.RelativeToTop on it.
440func (p OptionalPath) RelativeToTop() OptionalPath {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100441 if p.path == nil {
Paul Duffina5b81352021-03-28 23:57:19 +0100442 return p
443 }
444 p.path = p.path.RelativeToTop()
445 return p
446}
447
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700448// String returns the string version of the Path, or "" if it isn't valid.
449func (p OptionalPath) String() string {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100450 if p.path != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700451 return p.path.String()
452 } else {
453 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700454 }
455}
Colin Cross6e18ca42015-07-14 18:55:36 -0700456
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700457// Paths is a slice of Path objects, with helpers to operate on the collection.
458type Paths []Path
459
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000460// RelativeToTop creates a new Paths containing the result of calling Path.RelativeToTop on each
461// item in this slice.
462func (p Paths) RelativeToTop() Paths {
463 ensureTestOnly()
464 if p == nil {
465 return p
466 }
467 ret := make(Paths, len(p))
468 for i, path := range p {
469 ret[i] = path.RelativeToTop()
470 }
471 return ret
472}
473
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000474func (paths Paths) containsPath(path Path) bool {
475 for _, p := range paths {
476 if p == path {
477 return true
478 }
479 }
480 return false
481}
482
Liz Kammer7aa52882021-02-11 09:16:14 -0500483// PathsForSource returns Paths rooted from SrcDir, *not* rooted from the module's local source
484// directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700485func PathsForSource(ctx PathContext, paths []string) Paths {
486 ret := make(Paths, len(paths))
487 for i, path := range paths {
488 ret[i] = PathForSource(ctx, path)
489 }
490 return ret
491}
492
Liz Kammer7aa52882021-02-11 09:16:14 -0500493// ExistentPathsForSources returns a list of Paths rooted from SrcDir, *not* rooted from the
494// module's local source directory, that are found in the tree. If any are not found, they are
495// omitted from the list, and dependencies are added so that we're re-run when they are added.
Colin Cross662d6142022-11-03 20:38:01 -0700496func ExistentPathsForSources(ctx PathGlobContext, paths []string) Paths {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800497 ret := make(Paths, 0, len(paths))
498 for _, path := range paths {
Colin Cross32f38982018-02-22 11:47:25 -0800499 p := ExistentPathForSource(ctx, path)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800500 if p.Valid() {
501 ret = append(ret, p.Path())
502 }
503 }
504 return ret
505}
506
Liz Kammer620dea62021-04-14 17:36:10 -0400507// PathsForModuleSrc returns a Paths{} containing the resolved references in paths:
Colin Crossd079e0b2022-08-16 10:27:33 -0700508// - filepath, relative to local module directory, resolves as a filepath relative to the local
509// source directory
510// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
511// source directory.
512// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700513// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
514// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700515//
Liz Kammer620dea62021-04-14 17:36:10 -0400516// Properties passed as the paths argument must have been annotated with struct tag
Colin Cross41955e82019-05-29 14:40:35 -0700517// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000518// pathdeps mutator.
Liz Kammer620dea62021-04-14 17:36:10 -0400519// If a requested module is not found as a dependency:
Colin Crossd079e0b2022-08-16 10:27:33 -0700520// - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
Liz Kammer620dea62021-04-14 17:36:10 -0400521// missing dependencies
Colin Crossd079e0b2022-08-16 10:27:33 -0700522// - otherwise, a ModuleError is thrown.
Liz Kammera830f3a2020-11-10 10:50:34 -0800523func PathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths {
Colin Cross8a497952019-03-05 22:25:09 -0800524 return PathsForModuleSrcExcludes(ctx, paths, nil)
525}
526
Liz Kammer619be462022-01-28 15:13:39 -0500527type SourceInput struct {
528 Context ModuleMissingDepsPathContext
529 Paths []string
530 ExcludePaths []string
531 IncludeDirs bool
532}
533
Liz Kammer620dea62021-04-14 17:36:10 -0400534// PathsForModuleSrcExcludes returns a Paths{} containing the resolved references in paths, minus
535// those listed in excludes. Elements of paths and excludes are resolved as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700536// - filepath, relative to local module directory, resolves as a filepath relative to the local
537// source directory
538// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
539// source directory. Not valid in excludes.
540// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700541// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
542// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700543//
Liz Kammer620dea62021-04-14 17:36:10 -0400544// excluding the items (similarly resolved
545// Properties passed as the paths argument must have been annotated with struct tag
546// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000547// pathdeps mutator.
Liz Kammer620dea62021-04-14 17:36:10 -0400548// If a requested module is not found as a dependency:
Colin Crossd079e0b2022-08-16 10:27:33 -0700549// - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
Liz Kammer620dea62021-04-14 17:36:10 -0400550// missing dependencies
Colin Crossd079e0b2022-08-16 10:27:33 -0700551// - otherwise, a ModuleError is thrown.
Liz Kammera830f3a2020-11-10 10:50:34 -0800552func PathsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) Paths {
Liz Kammer619be462022-01-28 15:13:39 -0500553 return PathsRelativeToModuleSourceDir(SourceInput{
554 Context: ctx,
555 Paths: paths,
556 ExcludePaths: excludes,
557 IncludeDirs: true,
558 })
559}
560
561func PathsRelativeToModuleSourceDir(input SourceInput) Paths {
562 ret, missingDeps := PathsAndMissingDepsRelativeToModuleSourceDir(input)
563 if input.Context.Config().AllowMissingDependencies() {
564 input.Context.AddMissingDependencies(missingDeps)
Colin Crossba71a3f2019-03-18 12:12:48 -0700565 } else {
566 for _, m := range missingDeps {
Liz Kammer619be462022-01-28 15:13:39 -0500567 input.Context.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
Colin Crossba71a3f2019-03-18 12:12:48 -0700568 }
569 }
570 return ret
571}
572
Inseob Kim93036a52024-10-25 17:02:21 +0900573type directoryPath struct {
574 basePath
575}
576
577func (d *directoryPath) String() string {
578 return d.basePath.String()
579}
580
581func (d *directoryPath) base() basePath {
582 return d.basePath
583}
584
585// DirectoryPath represents a source path for directories. Incompatible with Path by design.
586type DirectoryPath interface {
587 String() string
588 base() basePath
589}
590
591var _ DirectoryPath = (*directoryPath)(nil)
592
593type DirectoryPaths []DirectoryPath
594
Inseob Kim76e19852024-10-10 17:57:22 +0900595// DirectoryPathsForModuleSrcExcludes returns a Paths{} containing the resolved references in
596// directory paths. Elements of paths are resolved as:
597// - filepath, relative to local module directory, resolves as a filepath relative to the local
598// source directory
599// - other modules using the ":name" syntax. These modules must implement DirProvider.
Inseob Kim93036a52024-10-25 17:02:21 +0900600func DirectoryPathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) DirectoryPaths {
601 var ret DirectoryPaths
Inseob Kim76e19852024-10-10 17:57:22 +0900602
603 for _, path := range paths {
604 if m, t := SrcIsModuleWithTag(path); m != "" {
Yu Liud3228ac2024-11-08 23:11:47 +0000605 module := GetModuleProxyFromPathDep(ctx, m, t)
Inseob Kim76e19852024-10-10 17:57:22 +0900606 if module == nil {
607 ctx.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
608 continue
609 }
610 if t != "" {
611 ctx.ModuleErrorf("DirProvider dependency %q does not support the tag %q", module, t)
612 continue
613 }
614 mctx, ok := ctx.(OtherModuleProviderContext)
615 if !ok {
616 panic(fmt.Errorf("%s is not an OtherModuleProviderContext", ctx))
617 }
Yu Liud3228ac2024-11-08 23:11:47 +0000618 if dirProvider, ok := OtherModuleProvider(mctx, *module, DirProvider); ok {
Inseob Kim76e19852024-10-10 17:57:22 +0900619 ret = append(ret, dirProvider.Dirs...)
620 } else {
621 ReportPathErrorf(ctx, "module %q does not implement DirProvider", module)
622 }
623 } else {
624 p := pathForModuleSrc(ctx, path)
625 if isDir, err := ctx.Config().fs.IsDir(p.String()); err != nil {
626 ReportPathErrorf(ctx, "%s: %s", p, err.Error())
627 } else if !isDir {
628 ReportPathErrorf(ctx, "module directory path %q is not a directory", p)
629 } else {
Inseob Kim93036a52024-10-25 17:02:21 +0900630 ret = append(ret, &directoryPath{basePath{path: p.path, rel: p.rel}})
Inseob Kim76e19852024-10-10 17:57:22 +0900631 }
632 }
633 }
634
Inseob Kim93036a52024-10-25 17:02:21 +0900635 seen := make(map[DirectoryPath]bool, len(ret))
Inseob Kim76e19852024-10-10 17:57:22 +0900636 for _, path := range ret {
637 if seen[path] {
638 ReportPathErrorf(ctx, "duplicated path %q", path)
639 }
640 seen[path] = true
641 }
642 return ret
643}
644
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000645// OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection.
646type OutputPaths []OutputPath
647
648// Paths returns the OutputPaths as a Paths
649func (p OutputPaths) Paths() Paths {
650 if p == nil {
651 return nil
652 }
653 ret := make(Paths, len(p))
654 for i, path := range p {
655 ret[i] = path
656 }
657 return ret
658}
659
660// Strings returns the string forms of the writable paths.
661func (p OutputPaths) Strings() []string {
662 if p == nil {
663 return nil
664 }
665 ret := make([]string, len(p))
666 for i, path := range p {
667 ret[i] = path.String()
668 }
669 return ret
670}
671
Liz Kammera830f3a2020-11-10 10:50:34 -0800672// Expands Paths to a SourceFileProducer or OutputFileProducer module dependency referenced via ":name" or ":name{.tag}" syntax.
673// If the dependency is not found, a missingErrorDependency is returned.
674// If the module dependency is not a SourceFileProducer or OutputFileProducer, appropriate errors will be returned.
675func getPathsFromModuleDep(ctx ModuleWithDepsPathContext, path, moduleName, tag string) (Paths, error) {
Yu Liud3228ac2024-11-08 23:11:47 +0000676 module := GetModuleProxyFromPathDep(ctx, moduleName, tag)
Liz Kammera830f3a2020-11-10 10:50:34 -0800677 if module == nil {
678 return nil, missingDependencyError{[]string{moduleName}}
679 }
Yu Liub5275322024-11-13 18:40:43 +0000680 if !OtherModuleProviderOrDefault(ctx, *module, CommonModuleInfoKey).Enabled {
Colin Crossfa65cee2021-03-22 17:05:59 -0700681 return nil, missingDependencyError{[]string{moduleName}}
682 }
Yu Liud3228ac2024-11-08 23:11:47 +0000683
684 outputFiles, err := outputFilesForModule(ctx, *module, tag)
mrziwange6c85812024-05-22 14:36:09 -0700685 if outputFiles != nil && err == nil {
686 return outputFiles, nil
Liz Kammera830f3a2020-11-10 10:50:34 -0800687 } else {
mrziwange6c85812024-05-22 14:36:09 -0700688 return nil, err
Liz Kammera830f3a2020-11-10 10:50:34 -0800689 }
690}
691
Yu Liud3228ac2024-11-08 23:11:47 +0000692// GetModuleProxyFromPathDep will return the module that was added as a dependency automatically for
Paul Duffind5cf92e2021-07-09 17:38:55 +0100693// properties tagged with `android:"path"` or manually using ExtractSourceDeps or
694// ExtractSourcesDeps.
695//
696// The moduleName and tag supplied to this should be the values returned from SrcIsModuleWithTag.
697// Or, if no tag is expected then the moduleName should be the value returned by SrcIsModule and
698// the tag must be "".
699//
700// If tag is "" then the returned module will be the dependency that was added for ":moduleName".
701// Otherwise, it is the dependency that was added for ":moduleName{tag}".
Yu Liud3228ac2024-11-08 23:11:47 +0000702func GetModuleProxyFromPathDep(ctx ModuleWithDepsPathContext, moduleName, tag string) *ModuleProxy {
703 var found *ModuleProxy
704 // The sourceOrOutputDepTag uniquely identifies the module dependency as it contains both the
705 // module name and the tag. Dependencies added automatically for properties tagged with
706 // `android:"path"` are deduped so are guaranteed to be unique. It is possible for duplicate
707 // dependencies to be added manually using ExtractSourcesDeps or ExtractSourceDeps but even then
708 // it will always be the case that the dependencies will be identical, i.e. the same tag and same
709 // moduleName referring to the same dependency module.
710 //
711 // It does not matter whether the moduleName is a fully qualified name or if the module
712 // dependency is a prebuilt module. All that matters is the same information is supplied to
713 // create the tag here as was supplied to create the tag when the dependency was added so that
714 // this finds the matching dependency module.
715 expectedTag := sourceOrOutputDepTag(moduleName, tag)
716 ctx.VisitDirectDepsProxyWithTag(expectedTag, func(module ModuleProxy) {
717 found = &module
718 })
719 return found
720}
721
722// Deprecated: use GetModuleProxyFromPathDep
Paul Duffind5cf92e2021-07-09 17:38:55 +0100723func GetModuleFromPathDep(ctx ModuleWithDepsPathContext, moduleName, tag string) blueprint.Module {
Paul Duffin40131a32021-07-09 17:10:35 +0100724 var found blueprint.Module
725 // The sourceOrOutputDepTag uniquely identifies the module dependency as it contains both the
726 // module name and the tag. Dependencies added automatically for properties tagged with
727 // `android:"path"` are deduped so are guaranteed to be unique. It is possible for duplicate
728 // dependencies to be added manually using ExtractSourcesDeps or ExtractSourceDeps but even then
729 // it will always be the case that the dependencies will be identical, i.e. the same tag and same
730 // moduleName referring to the same dependency module.
731 //
732 // It does not matter whether the moduleName is a fully qualified name or if the module
733 // dependency is a prebuilt module. All that matters is the same information is supplied to
734 // create the tag here as was supplied to create the tag when the dependency was added so that
735 // this finds the matching dependency module.
736 expectedTag := sourceOrOutputDepTag(moduleName, tag)
Colin Cross648daea2024-09-12 14:35:29 -0700737 ctx.VisitDirectDeps(func(module Module) {
Paul Duffin40131a32021-07-09 17:10:35 +0100738 depTag := ctx.OtherModuleDependencyTag(module)
739 if depTag == expectedTag {
740 found = module
741 }
742 })
743 return found
Paul Duffind5cf92e2021-07-09 17:38:55 +0100744}
745
Liz Kammer620dea62021-04-14 17:36:10 -0400746// PathsAndMissingDepsForModuleSrcExcludes returns a Paths{} containing the resolved references in
747// paths, minus those listed in excludes. Elements of paths and excludes are resolved as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700748// - filepath, relative to local module directory, resolves as a filepath relative to the local
749// source directory
750// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
751// source directory. Not valid in excludes.
752// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700753// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
754// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700755//
Liz Kammer620dea62021-04-14 17:36:10 -0400756// and a list of the module names of missing module dependencies are returned as the second return.
757// Properties passed as the paths argument must have been annotated with struct tag
Colin Cross41955e82019-05-29 14:40:35 -0700758// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000759// pathdeps mutator.
Liz Kammer619be462022-01-28 15:13:39 -0500760func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) (Paths, []string) {
761 return PathsAndMissingDepsRelativeToModuleSourceDir(SourceInput{
762 Context: ctx,
763 Paths: paths,
764 ExcludePaths: excludes,
765 IncludeDirs: true,
766 })
767}
768
769func PathsAndMissingDepsRelativeToModuleSourceDir(input SourceInput) (Paths, []string) {
770 prefix := pathForModuleSrc(input.Context).String()
Colin Cross8a497952019-03-05 22:25:09 -0800771
772 var expandedExcludes []string
Liz Kammer619be462022-01-28 15:13:39 -0500773 if input.ExcludePaths != nil {
774 expandedExcludes = make([]string, 0, len(input.ExcludePaths))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700775 }
Colin Cross8a497952019-03-05 22:25:09 -0800776
Colin Crossba71a3f2019-03-18 12:12:48 -0700777 var missingExcludeDeps []string
Liz Kammer619be462022-01-28 15:13:39 -0500778 for _, e := range input.ExcludePaths {
Colin Cross41955e82019-05-29 14:40:35 -0700779 if m, t := SrcIsModuleWithTag(e); m != "" {
Liz Kammer619be462022-01-28 15:13:39 -0500780 modulePaths, err := getPathsFromModuleDep(input.Context, e, m, t)
Liz Kammera830f3a2020-11-10 10:50:34 -0800781 if m, ok := err.(missingDependencyError); ok {
782 missingExcludeDeps = append(missingExcludeDeps, m.missingDeps...)
783 } else if err != nil {
Liz Kammer619be462022-01-28 15:13:39 -0500784 reportPathError(input.Context, err)
Colin Cross8a497952019-03-05 22:25:09 -0800785 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800786 expandedExcludes = append(expandedExcludes, modulePaths.Strings()...)
Colin Cross8a497952019-03-05 22:25:09 -0800787 }
788 } else {
789 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
790 }
791 }
792
Liz Kammer619be462022-01-28 15:13:39 -0500793 if input.Paths == nil {
Colin Crossba71a3f2019-03-18 12:12:48 -0700794 return nil, missingExcludeDeps
Colin Cross8a497952019-03-05 22:25:09 -0800795 }
796
Colin Crossba71a3f2019-03-18 12:12:48 -0700797 var missingDeps []string
798
Liz Kammer619be462022-01-28 15:13:39 -0500799 expandedSrcFiles := make(Paths, 0, len(input.Paths))
800 for _, s := range input.Paths {
801 srcFiles, err := expandOneSrcPath(sourcePathInput{
802 context: input.Context,
803 path: s,
804 expandedExcludes: expandedExcludes,
805 includeDirs: input.IncludeDirs,
806 })
Colin Cross8a497952019-03-05 22:25:09 -0800807 if depErr, ok := err.(missingDependencyError); ok {
Colin Crossba71a3f2019-03-18 12:12:48 -0700808 missingDeps = append(missingDeps, depErr.missingDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800809 } else if err != nil {
Liz Kammer619be462022-01-28 15:13:39 -0500810 reportPathError(input.Context, err)
Colin Cross8a497952019-03-05 22:25:09 -0800811 }
812 expandedSrcFiles = append(expandedSrcFiles, srcFiles...)
813 }
Colin Crossba71a3f2019-03-18 12:12:48 -0700814
Jihoon Kang0e3a5352024-04-12 00:45:50 +0000815 // TODO: b/334169722 - Replace with an error instead of implicitly removing duplicates.
816 return FirstUniquePaths(expandedSrcFiles), append(missingDeps, missingExcludeDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800817}
818
819type missingDependencyError struct {
820 missingDeps []string
821}
822
823func (e missingDependencyError) Error() string {
824 return "missing dependencies: " + strings.Join(e.missingDeps, ", ")
825}
826
Liz Kammer619be462022-01-28 15:13:39 -0500827type sourcePathInput struct {
828 context ModuleWithDepsPathContext
829 path string
830 expandedExcludes []string
831 includeDirs bool
832}
833
Liz Kammera830f3a2020-11-10 10:50:34 -0800834// Expands one path string to Paths rooted from the module's local source
835// directory, excluding those listed in the expandedExcludes.
836// Expands globs, references to SourceFileProducer or OutputFileProducer modules using the ":name" and ":name{.tag}" syntax.
Liz Kammer619be462022-01-28 15:13:39 -0500837func expandOneSrcPath(input sourcePathInput) (Paths, error) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900838 excludePaths := func(paths Paths) Paths {
Liz Kammer619be462022-01-28 15:13:39 -0500839 if len(input.expandedExcludes) == 0 {
Jooyung Han7607dd32020-07-05 10:23:14 +0900840 return paths
841 }
842 remainder := make(Paths, 0, len(paths))
843 for _, p := range paths {
Liz Kammer619be462022-01-28 15:13:39 -0500844 if !InList(p.String(), input.expandedExcludes) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900845 remainder = append(remainder, p)
846 }
847 }
848 return remainder
849 }
Liz Kammer619be462022-01-28 15:13:39 -0500850 if m, t := SrcIsModuleWithTag(input.path); m != "" {
851 modulePaths, err := getPathsFromModuleDep(input.context, input.path, m, t)
Liz Kammera830f3a2020-11-10 10:50:34 -0800852 if err != nil {
853 return nil, err
Colin Cross8a497952019-03-05 22:25:09 -0800854 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800855 return excludePaths(modulePaths), nil
Colin Cross8a497952019-03-05 22:25:09 -0800856 }
Colin Cross8a497952019-03-05 22:25:09 -0800857 } else {
Liz Kammer619be462022-01-28 15:13:39 -0500858 p := pathForModuleSrc(input.context, input.path)
859 if pathtools.IsGlob(input.path) {
860 paths := GlobFiles(input.context, p.String(), input.expandedExcludes)
861 return PathsWithModuleSrcSubDir(input.context, paths, ""), nil
862 } else {
863 if exists, _, err := input.context.Config().fs.Exists(p.String()); err != nil {
864 ReportPathErrorf(input.context, "%s: %s", p, err.Error())
865 } else if !exists && !input.context.Config().TestAllowNonExistentPaths {
866 ReportPathErrorf(input.context, "module source path %q does not exist", p)
867 } else if !input.includeDirs {
868 if isDir, err := input.context.Config().fs.IsDir(p.String()); exists && err != nil {
869 ReportPathErrorf(input.context, "%s: %s", p, err.Error())
870 } else if isDir {
871 ReportPathErrorf(input.context, "module source path %q is a directory", p)
872 }
873 }
Colin Cross8a497952019-03-05 22:25:09 -0800874
Liz Kammer619be462022-01-28 15:13:39 -0500875 if InList(p.String(), input.expandedExcludes) {
876 return nil, nil
877 }
878 return Paths{p}, nil
Colin Cross8a497952019-03-05 22:25:09 -0800879 }
Colin Cross8a497952019-03-05 22:25:09 -0800880 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700881}
882
883// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
884// source directory, but strip the local source directory from the beginning of
Dan Willemsen540a78c2018-02-26 21:50:08 -0800885// each string. If incDirs is false, strip paths with a trailing '/' from the list.
Colin Crossfe4bc362018-09-12 10:02:13 -0700886// It intended for use in globs that only list files that exist, so it allows '$' in
887// filenames.
Liz Kammera830f3a2020-11-10 10:50:34 -0800888func pathsForModuleSrcFromFullPath(ctx EarlyModulePathContext, paths []string, incDirs bool) Paths {
Lukacs T. Berkif7e36d82021-08-16 17:05:09 +0200889 prefix := ctx.ModuleDir() + "/"
Colin Cross0f37af02017-09-27 17:42:05 -0700890 if prefix == "./" {
891 prefix = ""
892 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700893 ret := make(Paths, 0, len(paths))
894 for _, p := range paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -0800895 if !incDirs && strings.HasSuffix(p, "/") {
896 continue
897 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700898 path := filepath.Clean(p)
899 if !strings.HasPrefix(path, prefix) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100900 ReportPathErrorf(ctx, "Path %q is not in module source directory %q", p, prefix)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700901 continue
902 }
Colin Crosse3924e12018-08-15 20:18:53 -0700903
Colin Crossfe4bc362018-09-12 10:02:13 -0700904 srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
Colin Crosse3924e12018-08-15 20:18:53 -0700905 if err != nil {
906 reportPathError(ctx, err)
907 continue
908 }
909
Colin Cross07e51612019-03-05 12:46:40 -0800910 srcPath.basePath.rel = srcPath.path
Colin Crosse3924e12018-08-15 20:18:53 -0700911
Colin Cross07e51612019-03-05 12:46:40 -0800912 ret = append(ret, srcPath)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700913 }
914 return ret
915}
916
Liz Kammera830f3a2020-11-10 10:50:34 -0800917// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's local source
918// directory. If input is nil, use the default if it exists. If input is empty, returns nil.
919func PathsWithOptionalDefaultForModuleSrc(ctx ModuleMissingDepsPathContext, input []string, def string) Paths {
Colin Cross0ddae7f2019-02-07 15:30:01 -0800920 if input != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700921 return PathsForModuleSrc(ctx, input)
922 }
923 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
924 // is created, we're run again.
Lukacs T. Berkif7e36d82021-08-16 17:05:09 +0200925 path := filepath.Join(ctx.ModuleDir(), def)
Liz Kammera830f3a2020-11-10 10:50:34 -0800926 return Glob(ctx, path, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700927}
928
929// Strings returns the Paths in string form
930func (p Paths) Strings() []string {
931 if p == nil {
932 return nil
933 }
934 ret := make([]string, len(p))
935 for i, path := range p {
936 ret[i] = path.String()
937 }
938 return ret
939}
940
Colin Crossc0efd1d2020-07-03 11:56:24 -0700941func CopyOfPaths(paths Paths) Paths {
942 return append(Paths(nil), paths...)
943}
944
Colin Crossb6715442017-10-24 11:13:31 -0700945// FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
946// modifies the Paths slice contents in place, and returns a subslice of the original slice.
Dan Willemsenfe92c962017-08-29 12:28:37 -0700947func FirstUniquePaths(list Paths) Paths {
Colin Cross27027c72020-02-28 15:34:17 -0800948 // 128 was chosen based on BenchmarkFirstUniquePaths results.
949 if len(list) > 128 {
950 return firstUniquePathsMap(list)
951 }
952 return firstUniquePathsList(list)
953}
954
Colin Crossc0efd1d2020-07-03 11:56:24 -0700955// SortedUniquePaths returns all unique elements of a Paths in sorted order. It modifies the
956// Paths slice contents in place, and returns a subslice of the original slice.
Jiyong Park33c77362020-05-29 22:00:16 +0900957func SortedUniquePaths(list Paths) Paths {
958 unique := FirstUniquePaths(list)
959 sort.Slice(unique, func(i, j int) bool {
960 return unique[i].String() < unique[j].String()
961 })
962 return unique
963}
964
Colin Cross27027c72020-02-28 15:34:17 -0800965func firstUniquePathsList(list Paths) Paths {
Dan Willemsenfe92c962017-08-29 12:28:37 -0700966 k := 0
967outer:
968 for i := 0; i < len(list); i++ {
969 for j := 0; j < k; j++ {
970 if list[i] == list[j] {
971 continue outer
972 }
973 }
974 list[k] = list[i]
975 k++
976 }
977 return list[:k]
978}
979
Colin Cross27027c72020-02-28 15:34:17 -0800980func firstUniquePathsMap(list Paths) Paths {
981 k := 0
982 seen := make(map[Path]bool, len(list))
983 for i := 0; i < len(list); i++ {
984 if seen[list[i]] {
985 continue
986 }
987 seen[list[i]] = true
988 list[k] = list[i]
989 k++
990 }
991 return list[:k]
992}
993
Colin Cross5d583952020-11-24 16:21:24 -0800994// FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each. It
995// modifies the InstallPaths slice contents in place, and returns a subslice of the original slice.
996func FirstUniqueInstallPaths(list InstallPaths) InstallPaths {
997 // 128 was chosen based on BenchmarkFirstUniquePaths results.
998 if len(list) > 128 {
999 return firstUniqueInstallPathsMap(list)
1000 }
1001 return firstUniqueInstallPathsList(list)
1002}
1003
1004func firstUniqueInstallPathsList(list InstallPaths) InstallPaths {
1005 k := 0
1006outer:
1007 for i := 0; i < len(list); i++ {
1008 for j := 0; j < k; j++ {
1009 if list[i] == list[j] {
1010 continue outer
1011 }
1012 }
1013 list[k] = list[i]
1014 k++
1015 }
1016 return list[:k]
1017}
1018
1019func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths {
1020 k := 0
1021 seen := make(map[InstallPath]bool, len(list))
1022 for i := 0; i < len(list); i++ {
1023 if seen[list[i]] {
1024 continue
1025 }
1026 seen[list[i]] = true
1027 list[k] = list[i]
1028 k++
1029 }
1030 return list[:k]
1031}
1032
Colin Crossb6715442017-10-24 11:13:31 -07001033// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
1034// modifies the Paths slice contents in place, and returns a subslice of the original slice.
1035func LastUniquePaths(list Paths) Paths {
1036 totalSkip := 0
1037 for i := len(list) - 1; i >= totalSkip; i-- {
1038 skip := 0
1039 for j := i - 1; j >= totalSkip; j-- {
1040 if list[i] == list[j] {
1041 skip++
1042 } else {
1043 list[j+skip] = list[j]
1044 }
1045 }
1046 totalSkip += skip
1047 }
1048 return list[totalSkip:]
1049}
1050
Colin Crossa140bb02018-04-17 10:52:26 -07001051// ReversePaths returns a copy of a Paths in reverse order.
1052func ReversePaths(list Paths) Paths {
1053 if list == nil {
1054 return nil
1055 }
1056 ret := make(Paths, len(list))
1057 for i := range list {
1058 ret[i] = list[len(list)-1-i]
1059 }
1060 return ret
1061}
1062
Jeff Gaston294356f2017-09-27 17:05:30 -07001063func indexPathList(s Path, list []Path) int {
1064 for i, l := range list {
1065 if l == s {
1066 return i
1067 }
1068 }
1069
1070 return -1
1071}
1072
1073func inPathList(p Path, list []Path) bool {
1074 return indexPathList(p, list) != -1
1075}
1076
1077func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
Paul Duffin57b9e1d2019-12-13 00:03:35 +00001078 return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) })
1079}
1080
1081func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001082 for _, l := range list {
Paul Duffin57b9e1d2019-12-13 00:03:35 +00001083 if predicate(l) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001084 filtered = append(filtered, l)
1085 } else {
1086 remainder = append(remainder, l)
1087 }
1088 }
1089
1090 return
1091}
1092
Colin Cross93e85952017-08-15 13:34:18 -07001093// HasExt returns true of any of the paths have extension ext, otherwise false
1094func (p Paths) HasExt(ext string) bool {
1095 for _, path := range p {
1096 if path.Ext() == ext {
1097 return true
1098 }
1099 }
1100
1101 return false
1102}
1103
1104// FilterByExt returns the subset of the paths that have extension ext
1105func (p Paths) FilterByExt(ext string) Paths {
1106 ret := make(Paths, 0, len(p))
1107 for _, path := range p {
1108 if path.Ext() == ext {
1109 ret = append(ret, path)
1110 }
1111 }
1112 return ret
1113}
1114
1115// FilterOutByExt returns the subset of the paths that do not have extension ext
1116func (p Paths) FilterOutByExt(ext string) Paths {
1117 ret := make(Paths, 0, len(p))
1118 for _, path := range p {
1119 if path.Ext() != ext {
1120 ret = append(ret, path)
1121 }
1122 }
1123 return ret
1124}
1125
Colin Cross5e6cfbe2017-11-03 15:20:35 -07001126// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
1127// (including subdirectories) are in a contiguous subslice of the list, and can be found in
1128// O(log(N)) time using a binary search on the directory prefix.
1129type DirectorySortedPaths Paths
1130
1131func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
1132 ret := append(DirectorySortedPaths(nil), paths...)
1133 sort.Slice(ret, func(i, j int) bool {
1134 return ret[i].String() < ret[j].String()
1135 })
1136 return ret
1137}
1138
1139// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
1140// that are in the specified directory and its subdirectories.
1141func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
1142 prefix := filepath.Clean(dir) + "/"
1143 start := sort.Search(len(p), func(i int) bool {
1144 return prefix < p[i].String()
1145 })
1146
1147 ret := p[start:]
1148
1149 end := sort.Search(len(ret), func(i int) bool {
1150 return !strings.HasPrefix(ret[i].String(), prefix)
1151 })
1152
1153 ret = ret[:end]
1154
1155 return Paths(ret)
1156}
1157
Alex Humesky29e3bbe2020-11-20 21:30:13 -05001158// WritablePaths is a slice of WritablePath, used for multiple outputs.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001159type WritablePaths []WritablePath
1160
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001161// RelativeToTop creates a new WritablePaths containing the result of calling Path.RelativeToTop on
1162// each item in this slice.
1163func (p WritablePaths) RelativeToTop() WritablePaths {
1164 ensureTestOnly()
1165 if p == nil {
1166 return p
1167 }
1168 ret := make(WritablePaths, len(p))
1169 for i, path := range p {
1170 ret[i] = path.RelativeToTop().(WritablePath)
1171 }
1172 return ret
1173}
1174
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001175// Strings returns the string forms of the writable paths.
1176func (p WritablePaths) Strings() []string {
1177 if p == nil {
1178 return nil
1179 }
1180 ret := make([]string, len(p))
1181 for i, path := range p {
1182 ret[i] = path.String()
1183 }
1184 return ret
1185}
1186
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001187// Paths returns the WritablePaths as a Paths
1188func (p WritablePaths) Paths() Paths {
1189 if p == nil {
1190 return nil
1191 }
1192 ret := make(Paths, len(p))
1193 for i, path := range p {
1194 ret[i] = path
1195 }
1196 return ret
1197}
1198
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001199type basePath struct {
Paul Duffin74abc5d2021-03-24 09:24:59 +00001200 path string
1201 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001202}
1203
Yu Liu467d7c52024-09-18 21:54:44 +00001204type basePathGob struct {
1205 Path string
1206 Rel string
1207}
Yu Liufa297642024-06-11 00:13:02 +00001208
Yu Liu467d7c52024-09-18 21:54:44 +00001209func (p *basePath) ToGob() *basePathGob {
1210 return &basePathGob{
1211 Path: p.path,
1212 Rel: p.rel,
1213 }
1214}
1215
1216func (p *basePath) FromGob(data *basePathGob) {
1217 p.path = data.Path
1218 p.rel = data.Rel
1219}
1220
1221func (p basePath) GobEncode() ([]byte, error) {
Yu Liu3cadf7d2024-10-24 18:47:06 +00001222 return gobtools.CustomGobEncode[basePathGob](&p)
Yu Liufa297642024-06-11 00:13:02 +00001223}
1224
1225func (p *basePath) GobDecode(data []byte) error {
Yu Liu3cadf7d2024-10-24 18:47:06 +00001226 return gobtools.CustomGobDecode[basePathGob](data, p)
Yu Liufa297642024-06-11 00:13:02 +00001227}
1228
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001229func (p basePath) Ext() string {
1230 return filepath.Ext(p.path)
1231}
1232
Colin Cross4f6fc9c2016-10-26 10:05:25 -07001233func (p basePath) Base() string {
1234 return filepath.Base(p.path)
1235}
1236
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001237func (p basePath) Rel() string {
1238 if p.rel != "" {
1239 return p.rel
1240 }
1241 return p.path
1242}
1243
Colin Cross0875c522017-11-28 17:34:01 -08001244func (p basePath) String() string {
1245 return p.path
1246}
1247
Colin Cross0db55682017-12-05 15:36:55 -08001248func (p basePath) withRel(rel string) basePath {
1249 p.path = filepath.Join(p.path, rel)
1250 p.rel = rel
1251 return p
1252}
1253
Colin Cross7707b242024-07-26 12:02:36 -07001254func (p basePath) withoutRel() basePath {
1255 p.rel = filepath.Base(p.path)
1256 return p
1257}
1258
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001259// SourcePath is a Path representing a file path rooted from SrcDir
1260type SourcePath struct {
1261 basePath
1262}
1263
1264var _ Path = SourcePath{}
1265
Colin Cross0db55682017-12-05 15:36:55 -08001266func (p SourcePath) withRel(rel string) SourcePath {
1267 p.basePath = p.basePath.withRel(rel)
1268 return p
1269}
1270
Colin Crossbd73d0d2024-07-26 12:00:33 -07001271func (p SourcePath) RelativeToTop() Path {
1272 ensureTestOnly()
1273 return p
1274}
1275
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001276// safePathForSource is for paths that we expect are safe -- only for use by go
1277// code that is embedding ninja variables in paths
Colin Crossfe4bc362018-09-12 10:02:13 -07001278func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
1279 p, err := validateSafePath(pathComponents...)
Cole Faust483d1f72023-01-09 14:35:27 -08001280 ret := SourcePath{basePath{p, ""}}
Colin Crossfe4bc362018-09-12 10:02:13 -07001281 if err != nil {
1282 return ret, err
1283 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001284
Colin Cross7b3dcc32019-01-24 13:14:39 -08001285 // absolute path already checked by validateSafePath
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001286 // special-case api surface gen files for now
1287 if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
Mikhail Naganovab1f5182019-02-08 13:17:55 -08001288 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -07001289 }
1290
Colin Crossfe4bc362018-09-12 10:02:13 -07001291 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001292}
1293
Colin Cross192e97a2018-02-22 14:21:02 -08001294// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
1295func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
Colin Crossc48c1432018-02-23 07:09:01 +00001296 p, err := validatePath(pathComponents...)
Cole Faust483d1f72023-01-09 14:35:27 -08001297 ret := SourcePath{basePath{p, ""}}
Colin Cross94a32102018-02-22 14:21:02 -08001298 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -08001299 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -08001300 }
1301
Colin Cross7b3dcc32019-01-24 13:14:39 -08001302 // absolute path already checked by validatePath
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001303 // special-case for now
1304 if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
Mikhail Naganovab1f5182019-02-08 13:17:55 -08001305 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Crossc48c1432018-02-23 07:09:01 +00001306 }
1307
Colin Cross192e97a2018-02-22 14:21:02 -08001308 return ret, nil
1309}
1310
1311// existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
1312// path does not exist.
Colin Cross662d6142022-11-03 20:38:01 -07001313func existsWithDependencies(ctx PathGlobContext, path SourcePath) (exists bool, err error) {
Colin Cross192e97a2018-02-22 14:21:02 -08001314 var files []string
1315
Colin Cross662d6142022-11-03 20:38:01 -07001316 // Use glob to produce proper dependencies, even though we only want
1317 // a single file.
1318 files, err = ctx.GlobWithDeps(path.String(), nil)
Colin Cross192e97a2018-02-22 14:21:02 -08001319
1320 if err != nil {
1321 return false, fmt.Errorf("glob: %s", err.Error())
1322 }
1323
1324 return len(files) > 0, nil
1325}
1326
1327// PathForSource joins the provided path components and validates that the result
1328// neither escapes the source dir nor is in the out dir.
1329// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
1330func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
1331 path, err := pathForSource(ctx, pathComponents...)
1332 if err != nil {
1333 reportPathError(ctx, err)
1334 }
1335
Colin Crosse3924e12018-08-15 20:18:53 -07001336 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001337 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -07001338 }
1339
Liz Kammera830f3a2020-11-10 10:50:34 -08001340 if modCtx, ok := ctx.(ModuleMissingDepsPathContext); ok && ctx.Config().AllowMissingDependencies() {
Colin Cross662d6142022-11-03 20:38:01 -07001341 exists, err := existsWithDependencies(modCtx, path)
Colin Cross192e97a2018-02-22 14:21:02 -08001342 if err != nil {
1343 reportPathError(ctx, err)
1344 }
1345 if !exists {
1346 modCtx.AddMissingDependencies([]string{path.String()})
1347 }
Colin Cross988414c2020-01-11 01:11:46 +00001348 } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001349 ReportPathErrorf(ctx, "%s: %s", path, err.Error())
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001350 } else if !exists && !ctx.Config().TestAllowNonExistentPaths {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001351 ReportPathErrorf(ctx, "source path %q does not exist", path)
Colin Cross192e97a2018-02-22 14:21:02 -08001352 }
1353 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001354}
1355
Cole Faustbc65a3f2023-08-01 16:38:55 +00001356// PathForArbitraryOutput creates a path for the given components. Unlike PathForOutput,
1357// the path is relative to the root of the output folder, not the out/soong folder.
Cole Faustaddc0dd2024-12-16 16:19:17 -08001358func PathForArbitraryOutput(ctx PathContext, pathComponents ...string) WritablePath {
Colin Cross3b1c6842024-07-26 11:52:57 -07001359 path, err := validatePath(pathComponents...)
Cole Faustbc65a3f2023-08-01 16:38:55 +00001360 if err != nil {
1361 reportPathError(ctx, err)
1362 }
Colin Cross3b1c6842024-07-26 11:52:57 -07001363 fullPath := filepath.Join(ctx.Config().OutDir(), path)
1364 path = fullPath[len(fullPath)-len(path):]
1365 return OutputPath{basePath{path, ""}, ctx.Config().OutDir(), fullPath}
Cole Faustbc65a3f2023-08-01 16:38:55 +00001366}
1367
Spandan Dasc6c10fa2022-10-21 21:52:13 +00001368// MaybeExistentPathForSource joins the provided path components and validates that the result
1369// neither escapes the source dir nor is in the out dir.
1370// It does not validate whether the path exists.
1371func MaybeExistentPathForSource(ctx PathContext, pathComponents ...string) SourcePath {
1372 path, err := pathForSource(ctx, pathComponents...)
1373 if err != nil {
1374 reportPathError(ctx, err)
1375 }
1376
1377 if pathtools.IsGlob(path.String()) {
1378 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
1379 }
1380 return path
1381}
1382
Liz Kammer7aa52882021-02-11 09:16:14 -05001383// ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not*
1384// rooted from the module's local source directory, if the path exists, or an empty OptionalPath if
1385// it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state
1386// of the path changes.
Colin Cross662d6142022-11-03 20:38:01 -07001387func ExistentPathForSource(ctx PathGlobContext, pathComponents ...string) OptionalPath {
Colin Cross192e97a2018-02-22 14:21:02 -08001388 path, err := pathForSource(ctx, pathComponents...)
Colin Cross1ccfcc32018-02-22 13:54:26 -08001389 if err != nil {
1390 reportPathError(ctx, err)
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001391 // No need to put the error message into the returned path since it has been reported already.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001392 return OptionalPath{}
1393 }
Colin Crossc48c1432018-02-23 07:09:01 +00001394
Colin Crosse3924e12018-08-15 20:18:53 -07001395 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001396 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -07001397 return OptionalPath{}
1398 }
1399
Colin Cross192e97a2018-02-22 14:21:02 -08001400 exists, err := existsWithDependencies(ctx, path)
Colin Crossc48c1432018-02-23 07:09:01 +00001401 if err != nil {
1402 reportPathError(ctx, err)
1403 return OptionalPath{}
1404 }
Colin Cross192e97a2018-02-22 14:21:02 -08001405 if !exists {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001406 return InvalidOptionalPath(path.String() + " does not exist")
Colin Crossc48c1432018-02-23 07:09:01 +00001407 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001408 return OptionalPathForPath(path)
1409}
1410
1411func (p SourcePath) String() string {
Cole Faust483d1f72023-01-09 14:35:27 -08001412 if p.path == "" {
1413 return "."
1414 }
1415 return p.path
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001416}
1417
Colin Cross7707b242024-07-26 12:02:36 -07001418func (p SourcePath) WithoutRel() Path {
1419 p.basePath = p.basePath.withoutRel()
1420 return p
1421}
1422
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001423// Join creates a new SourcePath with paths... joined with the current path. The
1424// provided paths... may not use '..' to escape from the current path.
1425func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001426 path, err := validatePath(paths...)
1427 if err != nil {
1428 reportPathError(ctx, err)
1429 }
Colin Cross0db55682017-12-05 15:36:55 -08001430 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001431}
1432
Colin Cross2fafa3e2019-03-05 12:39:51 -08001433// join is like Join but does less path validation.
1434func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath {
1435 path, err := validateSafePath(paths...)
1436 if err != nil {
1437 reportPathError(ctx, err)
1438 }
1439 return p.withRel(path)
1440}
1441
Colin Cross70dda7e2019-10-01 22:05:35 -07001442// OutputPath is a Path representing an intermediates file path rooted from the build directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001443type OutputPath struct {
1444 basePath
Paul Duffind65c58b2021-03-24 09:22:07 +00001445
Colin Cross3b1c6842024-07-26 11:52:57 -07001446 // The base out directory for this path, either Config.SoongOutDir() or Config.OutDir()
1447 outDir string
Paul Duffind65c58b2021-03-24 09:22:07 +00001448
Colin Crossd63c9a72020-01-29 16:52:50 -08001449 fullPath string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001450}
1451
Yu Liu467d7c52024-09-18 21:54:44 +00001452type outputPathGob struct {
Yu Liu5246a7e2024-10-09 20:04:52 +00001453 BasePath basePath
Yu Liu467d7c52024-09-18 21:54:44 +00001454 OutDir string
1455 FullPath string
1456}
Yu Liufa297642024-06-11 00:13:02 +00001457
Yu Liu467d7c52024-09-18 21:54:44 +00001458func (p *OutputPath) ToGob() *outputPathGob {
1459 return &outputPathGob{
Yu Liu5246a7e2024-10-09 20:04:52 +00001460 BasePath: p.basePath,
Yu Liu467d7c52024-09-18 21:54:44 +00001461 OutDir: p.outDir,
1462 FullPath: p.fullPath,
1463 }
1464}
1465
1466func (p *OutputPath) FromGob(data *outputPathGob) {
Yu Liu5246a7e2024-10-09 20:04:52 +00001467 p.basePath = data.BasePath
Yu Liu467d7c52024-09-18 21:54:44 +00001468 p.outDir = data.OutDir
1469 p.fullPath = data.FullPath
1470}
1471
1472func (p OutputPath) GobEncode() ([]byte, error) {
Yu Liu3cadf7d2024-10-24 18:47:06 +00001473 return gobtools.CustomGobEncode[outputPathGob](&p)
Yu Liufa297642024-06-11 00:13:02 +00001474}
1475
1476func (p *OutputPath) GobDecode(data []byte) error {
Yu Liu3cadf7d2024-10-24 18:47:06 +00001477 return gobtools.CustomGobDecode[outputPathGob](data, p)
Yu Liufa297642024-06-11 00:13:02 +00001478}
1479
Colin Cross702e0f82017-10-18 17:27:54 -07001480func (p OutputPath) withRel(rel string) OutputPath {
Colin Cross0db55682017-12-05 15:36:55 -08001481 p.basePath = p.basePath.withRel(rel)
Colin Crossd63c9a72020-01-29 16:52:50 -08001482 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross702e0f82017-10-18 17:27:54 -07001483 return p
1484}
1485
Colin Cross7707b242024-07-26 12:02:36 -07001486func (p OutputPath) WithoutRel() Path {
1487 p.basePath = p.basePath.withoutRel()
Colin Cross3063b782018-08-15 11:19:12 -07001488 return p
1489}
1490
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001491func (p OutputPath) getSoongOutDir() string {
Colin Cross3b1c6842024-07-26 11:52:57 -07001492 return p.outDir
Paul Duffin9b478b02019-12-10 13:41:51 +00001493}
1494
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001495func (p OutputPath) RelativeToTop() Path {
1496 return p.outputPathRelativeToTop()
1497}
1498
1499func (p OutputPath) outputPathRelativeToTop() OutputPath {
Colin Cross3b1c6842024-07-26 11:52:57 -07001500 p.fullPath = StringPathRelativeToTop(p.outDir, p.fullPath)
1501 if strings.HasSuffix(p.outDir, testOutSoongSubDir) {
1502 p.outDir = TestOutSoongDir
1503 } else {
1504 // Handle the PathForArbitraryOutput case
1505 p.outDir = testOutDir
1506 }
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001507 return p
1508}
1509
Paul Duffin0267d492021-02-02 10:05:52 +00001510func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
1511 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1512}
1513
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001514var _ Path = OutputPath{}
Paul Duffin9b478b02019-12-10 13:41:51 +00001515var _ WritablePath = OutputPath{}
Paul Duffin0267d492021-02-02 10:05:52 +00001516var _ objPathProvider = OutputPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001517
Chris Parsons8f232a22020-06-23 17:37:05 -04001518// toolDepPath is a Path representing a dependency of the build tool.
1519type toolDepPath struct {
1520 basePath
1521}
1522
Colin Cross7707b242024-07-26 12:02:36 -07001523func (t toolDepPath) WithoutRel() Path {
1524 t.basePath = t.basePath.withoutRel()
1525 return t
1526}
1527
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001528func (t toolDepPath) RelativeToTop() Path {
1529 ensureTestOnly()
1530 return t
1531}
1532
Chris Parsons8f232a22020-06-23 17:37:05 -04001533var _ Path = toolDepPath{}
1534
1535// pathForBuildToolDep returns a toolDepPath representing the given path string.
1536// There is no validation for the path, as it is "trusted": It may fail
1537// normal validation checks. For example, it may be an absolute path.
1538// Only use this function to construct paths for dependencies of the build
1539// tool invocation.
1540func pathForBuildToolDep(ctx PathContext, path string) toolDepPath {
Paul Duffin74abc5d2021-03-24 09:24:59 +00001541 return toolDepPath{basePath{path, ""}}
Chris Parsons8f232a22020-06-23 17:37:05 -04001542}
1543
Jeff Gaston734e3802017-04-10 15:47:24 -07001544// PathForOutput joins the provided paths and returns an OutputPath that is
1545// validated to not escape the build dir.
1546// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
1547func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001548 path, err := validatePath(pathComponents...)
1549 if err != nil {
1550 reportPathError(ctx, err)
1551 }
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001552 fullPath := filepath.Join(ctx.Config().soongOutDir, path)
Colin Crossd63c9a72020-01-29 16:52:50 -08001553 path = fullPath[len(fullPath)-len(path):]
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001554 return OutputPath{basePath{path, ""}, ctx.Config().soongOutDir, fullPath}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001555}
1556
Colin Cross3b1c6842024-07-26 11:52:57 -07001557// PathsForOutput returns Paths rooted from outDir
Colin Cross40e33732019-02-15 11:08:35 -08001558func PathsForOutput(ctx PathContext, paths []string) WritablePaths {
1559 ret := make(WritablePaths, len(paths))
1560 for i, path := range paths {
1561 ret[i] = PathForOutput(ctx, path)
1562 }
1563 return ret
1564}
1565
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001566func (p OutputPath) writablePath() {}
1567
1568func (p OutputPath) String() string {
Colin Crossd63c9a72020-01-29 16:52:50 -08001569 return p.fullPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001570}
1571
1572// Join creates a new OutputPath with paths... joined with the current path. The
1573// provided paths... may not use '..' to escape from the current path.
1574func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001575 path, err := validatePath(paths...)
1576 if err != nil {
1577 reportPathError(ctx, err)
1578 }
Colin Cross0db55682017-12-05 15:36:55 -08001579 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001580}
1581
Colin Cross8854a5a2019-02-11 14:14:16 -08001582// ReplaceExtension creates a new OutputPath with the extension replaced with ext.
1583func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1584 if strings.Contains(ext, "/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001585 ReportPathErrorf(ctx, "extension %q cannot contain /", ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001586 }
1587 ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext))
Colin Cross2cdd5df2019-02-25 10:25:24 -08001588 ret.rel = pathtools.ReplaceExtension(p.rel, ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001589 return ret
1590}
1591
Colin Cross40e33732019-02-15 11:08:35 -08001592// InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths.
1593func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath {
1594 path, err := validatePath(paths...)
1595 if err != nil {
1596 reportPathError(ctx, err)
1597 }
1598
1599 ret := PathForOutput(ctx, filepath.Dir(p.path), path)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001600 ret.rel = filepath.Join(filepath.Dir(p.rel), path)
Colin Cross40e33732019-02-15 11:08:35 -08001601 return ret
1602}
1603
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001604// PathForIntermediates returns an OutputPath representing the top-level
1605// intermediates directory.
1606func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001607 path, err := validatePath(paths...)
1608 if err != nil {
1609 reportPathError(ctx, err)
1610 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001611 return PathForOutput(ctx, ".intermediates", path)
1612}
1613
Colin Cross07e51612019-03-05 12:46:40 -08001614var _ genPathProvider = SourcePath{}
1615var _ objPathProvider = SourcePath{}
1616var _ resPathProvider = SourcePath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001617
Colin Cross07e51612019-03-05 12:46:40 -08001618// PathForModuleSrc returns a Path representing the paths... under the
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001619// module's local source directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001620func PathForModuleSrc(ctx ModuleMissingDepsPathContext, pathComponents ...string) Path {
Paul Duffin407501b2021-07-09 16:56:35 +01001621 // Just join the components textually just to make sure that it does not corrupt a fully qualified
1622 // module reference, e.g. if the pathComponents is "://other:foo" then using filepath.Join() or
1623 // validatePath() will corrupt it, e.g. replace "//" with "/". If the path is not a module
1624 // reference then it will be validated by expandOneSrcPath anyway when it calls expandOneSrcPath.
1625 p := strings.Join(pathComponents, string(filepath.Separator))
Liz Kammer619be462022-01-28 15:13:39 -05001626 paths, err := expandOneSrcPath(sourcePathInput{context: ctx, path: p, includeDirs: true})
Colin Cross8a497952019-03-05 22:25:09 -08001627 if err != nil {
1628 if depErr, ok := err.(missingDependencyError); ok {
1629 if ctx.Config().AllowMissingDependencies() {
1630 ctx.AddMissingDependencies(depErr.missingDeps)
1631 } else {
1632 ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error())
1633 }
1634 } else {
1635 reportPathError(ctx, err)
1636 }
1637 return nil
1638 } else if len(paths) == 0 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001639 ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p)
Colin Cross8a497952019-03-05 22:25:09 -08001640 return nil
1641 } else if len(paths) > 1 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001642 ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths))
Colin Cross8a497952019-03-05 22:25:09 -08001643 }
1644 return paths[0]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001645}
1646
Liz Kammera830f3a2020-11-10 10:50:34 -08001647func pathForModuleSrc(ctx EarlyModulePathContext, paths ...string) SourcePath {
Colin Cross07e51612019-03-05 12:46:40 -08001648 p, err := validatePath(paths...)
1649 if err != nil {
1650 reportPathError(ctx, err)
1651 }
1652
1653 path, err := pathForSource(ctx, ctx.ModuleDir(), p)
1654 if err != nil {
1655 reportPathError(ctx, err)
1656 }
1657
1658 path.basePath.rel = p
1659
1660 return path
1661}
1662
Colin Cross2fafa3e2019-03-05 12:39:51 -08001663// PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path
1664// will return the path relative to subDir in the module's source directory. If any input paths are not located
1665// inside subDir then a path error will be reported.
Liz Kammera830f3a2020-11-10 10:50:34 -08001666func PathsWithModuleSrcSubDir(ctx EarlyModulePathContext, paths Paths, subDir string) Paths {
Colin Cross2fafa3e2019-03-05 12:39:51 -08001667 paths = append(Paths(nil), paths...)
Colin Cross07e51612019-03-05 12:46:40 -08001668 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001669 for i, path := range paths {
1670 rel := Rel(ctx, subDirFullPath.String(), path.String())
1671 paths[i] = subDirFullPath.join(ctx, rel)
1672 }
1673 return paths
1674}
1675
1676// PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the
1677// module's source directory. If the input path is not located inside subDir then a path error will be reported.
Liz Kammera830f3a2020-11-10 10:50:34 -08001678func PathWithModuleSrcSubDir(ctx EarlyModulePathContext, path Path, subDir string) Path {
Colin Cross07e51612019-03-05 12:46:40 -08001679 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001680 rel := Rel(ctx, subDirFullPath.String(), path.String())
1681 return subDirFullPath.Join(ctx, rel)
1682}
1683
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001684// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
1685// valid path if p is non-nil.
Liz Kammera830f3a2020-11-10 10:50:34 -08001686func OptionalPathForModuleSrc(ctx ModuleMissingDepsPathContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001687 if p == nil {
1688 return OptionalPath{}
1689 }
1690 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
1691}
1692
Liz Kammera830f3a2020-11-10 10:50:34 -08001693func (p SourcePath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001694 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001695}
1696
yangbill6d032dd2024-04-18 03:05:49 +00001697func (p SourcePath) genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath {
1698 // If Trim_extension being set, force append Output_extension without replace original extension.
1699 if trimExt != "" {
1700 if ext != "" {
1701 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt)+"."+ext)
1702 }
1703 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt))
1704 }
1705 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1706}
1707
Liz Kammera830f3a2020-11-10 10:50:34 -08001708func (p SourcePath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001709 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001710}
1711
Liz Kammera830f3a2020-11-10 10:50:34 -08001712func (p SourcePath) resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001713 // TODO: Use full directory if the new ctx is not the current ctx?
1714 return PathForModuleRes(ctx, p.path, name)
1715}
1716
1717// ModuleOutPath is a Path representing a module's output directory.
1718type ModuleOutPath struct {
1719 OutputPath
1720}
1721
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001722func (p ModuleOutPath) RelativeToTop() Path {
1723 p.OutputPath = p.outputPathRelativeToTop()
1724 return p
1725}
1726
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001727var _ Path = ModuleOutPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001728var _ WritablePath = ModuleOutPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001729
Liz Kammera830f3a2020-11-10 10:50:34 -08001730func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01001731 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1732}
1733
Liz Kammera830f3a2020-11-10 10:50:34 -08001734// ModuleOutPathContext Subset of ModuleContext functions necessary for output path methods.
1735type ModuleOutPathContext interface {
1736 PathContext
1737
1738 ModuleName() string
1739 ModuleDir() string
1740 ModuleSubDir() string
1741}
1742
1743func pathForModuleOut(ctx ModuleOutPathContext) OutputPath {
Inseob Kimb7e9f5f2024-06-25 17:39:52 +09001744 return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
Colin Cross702e0f82017-10-18 17:27:54 -07001745}
1746
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001747// PathForModuleOut returns a Path representing the paths... under the module's
1748// output directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001749func PathForModuleOut(ctx ModuleOutPathContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001750 p, err := validatePath(paths...)
1751 if err != nil {
1752 reportPathError(ctx, err)
1753 }
Colin Cross702e0f82017-10-18 17:27:54 -07001754 return ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001755 OutputPath: pathForModuleOut(ctx).withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001756 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001757}
1758
1759// ModuleGenPath is a Path representing the 'gen' directory in a module's output
1760// directory. Mainly used for generated sources.
1761type ModuleGenPath struct {
1762 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001763}
1764
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001765func (p ModuleGenPath) RelativeToTop() Path {
1766 p.OutputPath = p.outputPathRelativeToTop()
1767 return p
1768}
1769
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001770var _ Path = ModuleGenPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001771var _ WritablePath = ModuleGenPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001772var _ genPathProvider = ModuleGenPath{}
1773var _ objPathProvider = ModuleGenPath{}
1774
1775// PathForModuleGen returns a Path representing the paths... under the module's
1776// `gen' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001777func PathForModuleGen(ctx ModuleOutPathContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001778 p, err := validatePath(paths...)
1779 if err != nil {
1780 reportPathError(ctx, err)
1781 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001782 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -07001783 ModuleOutPath: ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001784 OutputPath: pathForModuleOut(ctx).withRel("gen").withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001785 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001786 }
1787}
1788
Liz Kammera830f3a2020-11-10 10:50:34 -08001789func (p ModuleGenPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001790 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -07001791 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001792}
1793
yangbill6d032dd2024-04-18 03:05:49 +00001794func (p ModuleGenPath) genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath {
1795 // If Trim_extension being set, force append Output_extension without replace original extension.
1796 if trimExt != "" {
1797 if ext != "" {
1798 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt)+"."+ext)
1799 }
1800 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt))
1801 }
1802 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1803}
1804
Liz Kammera830f3a2020-11-10 10:50:34 -08001805func (p ModuleGenPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001806 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1807}
1808
1809// ModuleObjPath is a Path representing the 'obj' directory in a module's output
1810// directory. Used for compiled objects.
1811type ModuleObjPath struct {
1812 ModuleOutPath
1813}
1814
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001815func (p ModuleObjPath) RelativeToTop() Path {
1816 p.OutputPath = p.outputPathRelativeToTop()
1817 return p
1818}
1819
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001820var _ Path = ModuleObjPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001821var _ WritablePath = ModuleObjPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001822
1823// PathForModuleObj returns a Path representing the paths... under the module's
1824// 'obj' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001825func PathForModuleObj(ctx ModuleOutPathContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001826 p, err := validatePath(pathComponents...)
1827 if err != nil {
1828 reportPathError(ctx, err)
1829 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001830 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
1831}
1832
1833// ModuleResPath is a a Path representing the 'res' directory in a module's
1834// output directory.
1835type ModuleResPath struct {
1836 ModuleOutPath
1837}
1838
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001839func (p ModuleResPath) RelativeToTop() Path {
1840 p.OutputPath = p.outputPathRelativeToTop()
1841 return p
1842}
1843
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001844var _ Path = ModuleResPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001845var _ WritablePath = ModuleResPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001846
1847// PathForModuleRes returns a Path representing the paths... under the module's
1848// 'res' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001849func PathForModuleRes(ctx ModuleOutPathContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001850 p, err := validatePath(pathComponents...)
1851 if err != nil {
1852 reportPathError(ctx, err)
1853 }
1854
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001855 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
1856}
1857
Colin Cross70dda7e2019-10-01 22:05:35 -07001858// InstallPath is a Path representing a installed file path rooted from the build directory
1859type InstallPath struct {
1860 basePath
Colin Crossff6c33d2019-10-02 16:01:35 -07001861
Lukacs T. Berkib078ade2021-08-31 10:42:08 +02001862 // The soong build directory, i.e. Config.SoongOutDir()
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001863 soongOutDir string
Paul Duffind65c58b2021-03-24 09:22:07 +00001864
Jiyong Park957bcd92020-10-20 18:23:33 +09001865 // partitionDir is the part of the InstallPath that is automatically determined according to the context.
1866 // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules.
1867 partitionDir string
1868
Colin Crossb1692a32021-10-25 15:39:01 -07001869 partition string
1870
Jiyong Park957bcd92020-10-20 18:23:33 +09001871 // makePath indicates whether this path is for Soong (false) or Make (true).
1872 makePath bool
Colin Crossc0e42d52024-02-01 16:42:36 -08001873
1874 fullPath string
Colin Cross70dda7e2019-10-01 22:05:35 -07001875}
1876
Yu Liu467d7c52024-09-18 21:54:44 +00001877type installPathGob struct {
Yu Liu5246a7e2024-10-09 20:04:52 +00001878 BasePath basePath
Yu Liu467d7c52024-09-18 21:54:44 +00001879 SoongOutDir string
1880 PartitionDir string
1881 Partition string
1882 MakePath bool
1883 FullPath string
1884}
Yu Liu26a716d2024-08-30 23:40:32 +00001885
Yu Liu467d7c52024-09-18 21:54:44 +00001886func (p *InstallPath) ToGob() *installPathGob {
1887 return &installPathGob{
Yu Liu5246a7e2024-10-09 20:04:52 +00001888 BasePath: p.basePath,
Yu Liu467d7c52024-09-18 21:54:44 +00001889 SoongOutDir: p.soongOutDir,
1890 PartitionDir: p.partitionDir,
1891 Partition: p.partition,
1892 MakePath: p.makePath,
1893 FullPath: p.fullPath,
1894 }
1895}
1896
1897func (p *InstallPath) FromGob(data *installPathGob) {
Yu Liu5246a7e2024-10-09 20:04:52 +00001898 p.basePath = data.BasePath
Yu Liu467d7c52024-09-18 21:54:44 +00001899 p.soongOutDir = data.SoongOutDir
1900 p.partitionDir = data.PartitionDir
1901 p.partition = data.Partition
1902 p.makePath = data.MakePath
1903 p.fullPath = data.FullPath
1904}
1905
1906func (p InstallPath) GobEncode() ([]byte, error) {
Yu Liu3cadf7d2024-10-24 18:47:06 +00001907 return gobtools.CustomGobEncode[installPathGob](&p)
Yu Liu26a716d2024-08-30 23:40:32 +00001908}
1909
1910func (p *InstallPath) GobDecode(data []byte) error {
Yu Liu3cadf7d2024-10-24 18:47:06 +00001911 return gobtools.CustomGobDecode[installPathGob](data, p)
Yu Liu26a716d2024-08-30 23:40:32 +00001912}
1913
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001914// Will panic if called from outside a test environment.
1915func ensureTestOnly() {
Martin Stjernholm32312eb2021-03-27 18:54:49 +00001916 if PrefixInList(os.Args, "-test.") {
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001917 return
1918 }
Martin Stjernholm32312eb2021-03-27 18:54:49 +00001919 panic(fmt.Errorf("Not in test. Command line:\n %s", strings.Join(os.Args, "\n ")))
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001920}
1921
1922func (p InstallPath) RelativeToTop() Path {
1923 ensureTestOnly()
Colin Crossc0e42d52024-02-01 16:42:36 -08001924 if p.makePath {
Colin Cross3b1c6842024-07-26 11:52:57 -07001925 p.soongOutDir = testOutDir
Colin Crossc0e42d52024-02-01 16:42:36 -08001926 } else {
Colin Cross3b1c6842024-07-26 11:52:57 -07001927 p.soongOutDir = TestOutSoongDir
Colin Crossc0e42d52024-02-01 16:42:36 -08001928 }
1929 p.fullPath = filepath.Join(p.soongOutDir, p.path)
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001930 return p
1931}
1932
Colin Cross7707b242024-07-26 12:02:36 -07001933func (p InstallPath) WithoutRel() Path {
1934 p.basePath = p.basePath.withoutRel()
1935 return p
1936}
1937
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001938func (p InstallPath) getSoongOutDir() string {
1939 return p.soongOutDir
Paul Duffin9b478b02019-12-10 13:41:51 +00001940}
1941
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001942func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1943 panic("Not implemented")
1944}
1945
Paul Duffin9b478b02019-12-10 13:41:51 +00001946var _ Path = InstallPath{}
1947var _ WritablePath = InstallPath{}
1948
Colin Cross70dda7e2019-10-01 22:05:35 -07001949func (p InstallPath) writablePath() {}
1950
1951func (p InstallPath) String() string {
Colin Crossc0e42d52024-02-01 16:42:36 -08001952 return p.fullPath
Jiyong Park957bcd92020-10-20 18:23:33 +09001953}
1954
1955// PartitionDir returns the path to the partition where the install path is rooted at. It is
1956// out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules.
1957// The ./soong is dropped if the install path is for Make.
1958func (p InstallPath) PartitionDir() string {
1959 if p.makePath {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001960 return filepath.Join(p.soongOutDir, "../", p.partitionDir)
Jiyong Park957bcd92020-10-20 18:23:33 +09001961 } else {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001962 return filepath.Join(p.soongOutDir, p.partitionDir)
Jiyong Park957bcd92020-10-20 18:23:33 +09001963 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001964}
1965
Jihoon Kangf78a8902022-09-01 22:47:07 +00001966func (p InstallPath) Partition() string {
1967 return p.partition
1968}
1969
Colin Cross70dda7e2019-10-01 22:05:35 -07001970// Join creates a new InstallPath with paths... joined with the current path. The
1971// provided paths... may not use '..' to escape from the current path.
1972func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath {
1973 path, err := validatePath(paths...)
1974 if err != nil {
1975 reportPathError(ctx, err)
1976 }
1977 return p.withRel(path)
1978}
1979
1980func (p InstallPath) withRel(rel string) InstallPath {
1981 p.basePath = p.basePath.withRel(rel)
Colin Crossc0e42d52024-02-01 16:42:36 -08001982 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross70dda7e2019-10-01 22:05:35 -07001983 return p
1984}
1985
Colin Crossc68db4b2021-11-11 18:59:15 -08001986// Deprecated: ToMakePath is a noop, PathForModuleInstall always returns Make paths when building
1987// embedded in Make.
Colin Crossff6c33d2019-10-02 16:01:35 -07001988func (p InstallPath) ToMakePath() InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001989 p.makePath = true
Colin Crossff6c33d2019-10-02 16:01:35 -07001990 return p
Colin Cross70dda7e2019-10-01 22:05:35 -07001991}
1992
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001993// PathForModuleInstall returns a Path representing the install path for the
1994// module appended with paths...
Colin Cross70dda7e2019-10-01 22:05:35 -07001995func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Spandan Das5d1b9292021-06-03 19:36:41 +00001996 os, arch := osAndArch(ctx)
Cole Faust11edf552023-10-13 11:32:14 -07001997 partition := modulePartition(ctx, os.Class == Device)
Cole Faust3b703f32023-10-16 13:30:51 -07001998 return pathForInstall(ctx, os, arch, partition, pathComponents...)
Spandan Das5d1b9292021-06-03 19:36:41 +00001999}
2000
Colin Cross1d0eb7a2021-11-03 14:08:20 -07002001// PathForHostDexInstall returns an InstallPath representing the install path for the
2002// module appended with paths...
2003func PathForHostDexInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Cole Faust3b703f32023-10-16 13:30:51 -07002004 return pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "", pathComponents...)
Colin Cross1d0eb7a2021-11-03 14:08:20 -07002005}
2006
Spandan Das5d1b9292021-06-03 19:36:41 +00002007// PathForModuleInPartitionInstall is similar to PathForModuleInstall but partition is provided by the caller
2008func PathForModuleInPartitionInstall(ctx ModuleInstallPathContext, partition string, pathComponents ...string) InstallPath {
2009 os, arch := osAndArch(ctx)
Cole Faust3b703f32023-10-16 13:30:51 -07002010 return pathForInstall(ctx, os, arch, partition, pathComponents...)
Spandan Das5d1b9292021-06-03 19:36:41 +00002011}
2012
2013func osAndArch(ctx ModuleInstallPathContext) (OsType, ArchType) {
Colin Cross6e359402020-02-10 15:29:54 -08002014 os := ctx.Os()
Jiyong Park87788b52020-09-01 12:37:45 +09002015 arch := ctx.Arch().ArchType
2016 forceOS, forceArch := ctx.InstallForceOS()
2017 if forceOS != nil {
Colin Cross6e359402020-02-10 15:29:54 -08002018 os = *forceOS
2019 }
Jiyong Park87788b52020-09-01 12:37:45 +09002020 if forceArch != nil {
2021 arch = *forceArch
2022 }
Spandan Das5d1b9292021-06-03 19:36:41 +00002023 return os, arch
2024}
Colin Cross609c49a2020-02-13 13:20:11 -08002025
Colin Crossc0e42d52024-02-01 16:42:36 -08002026func pathForPartitionInstallDir(ctx PathContext, partition, partitionPath string, makePath bool) InstallPath {
2027 fullPath := ctx.Config().SoongOutDir()
2028 if makePath {
2029 // Make path starts with out/ instead of out/soong.
2030 fullPath = filepath.Join(fullPath, "../", partitionPath)
2031 } else {
2032 fullPath = filepath.Join(fullPath, partitionPath)
2033 }
2034
2035 return InstallPath{
2036 basePath: basePath{partitionPath, ""},
2037 soongOutDir: ctx.Config().soongOutDir,
2038 partitionDir: partitionPath,
2039 partition: partition,
2040 makePath: makePath,
2041 fullPath: fullPath,
2042 }
2043}
2044
Cole Faust3b703f32023-10-16 13:30:51 -07002045func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string,
Colin Cross609c49a2020-02-13 13:20:11 -08002046 pathComponents ...string) InstallPath {
2047
Jiyong Park97859152023-02-14 17:05:48 +09002048 var partitionPaths []string
Colin Cross609c49a2020-02-13 13:20:11 -08002049
Colin Cross6e359402020-02-10 15:29:54 -08002050 if os.Class == Device {
Jiyong Park97859152023-02-14 17:05:48 +09002051 partitionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002052 } else {
Jiyong Park87788b52020-09-01 12:37:45 +09002053 osName := os.String()
Colin Crossa9b2aac2022-06-15 17:25:51 -07002054 if os == Linux {
Jiyong Park87788b52020-09-01 12:37:45 +09002055 // instead of linux_glibc
2056 osName = "linux"
Dan Willemsen866b5632017-09-22 12:28:24 -07002057 }
Colin Crossa9b2aac2022-06-15 17:25:51 -07002058 if os == LinuxMusl && ctx.Config().UseHostMusl() {
2059 // When using musl instead of glibc, use "linux" instead of "linux_musl". When cross
2060 // compiling we will still use "linux_musl".
2061 osName = "linux"
2062 }
2063
Jiyong Park87788b52020-09-01 12:37:45 +09002064 // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
2065 // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem
2066 // to have a plan to fix it (see the comment in build/make/core/envsetup.mk).
2067 // Let's keep using x86 for the existing cases until we have a need to support
2068 // other architectures.
2069 archName := arch.String()
2070 if os.Class == Host && (arch == X86_64 || arch == Common) {
2071 archName = "x86"
2072 }
Jiyong Park97859152023-02-14 17:05:48 +09002073 partitionPaths = []string{"host", osName + "-" + archName, partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002074 }
Colin Cross70dda7e2019-10-01 22:05:35 -07002075
Jiyong Park97859152023-02-14 17:05:48 +09002076 partitionPath, err := validatePath(partitionPaths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07002077 if err != nil {
2078 reportPathError(ctx, err)
2079 }
Colin Crossff6c33d2019-10-02 16:01:35 -07002080
Cole Faust6b7075f2024-12-17 10:42:42 -08002081 base := pathForPartitionInstallDir(ctx, partition, partitionPath, true)
Jiyong Park957bcd92020-10-20 18:23:33 +09002082 return base.Join(ctx, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002083}
2084
Spandan Dasf280b232024-04-04 21:25:51 +00002085func PathForNdkInstall(ctx PathContext, paths ...string) OutputPath {
2086 return PathForOutput(ctx, append([]string{"ndk"}, paths...)...)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00002087}
2088
2089func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath {
Spandan Dasf280b232024-04-04 21:25:51 +00002090 base := pathForPartitionInstallDir(ctx, "", "mainline-sdks", false)
2091 return base.Join(ctx, paths...)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00002092}
2093
Weijia Heaa37c162024-11-06 19:46:03 +00002094func PathForSuiteInstall(ctx PathContext, suite string, pathComponents ...string) InstallPath {
2095 return pathForPartitionInstallDir(ctx, "test_suites", "test_suites", false).Join(ctx, suite).Join(ctx, pathComponents...)
2096}
2097
Colin Cross70dda7e2019-10-01 22:05:35 -07002098func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string {
Colin Crossb1692a32021-10-25 15:39:01 -07002099 rel := Rel(ctx, strings.TrimSuffix(path.PartitionDir(), path.partition), path.String())
Colin Cross43f08db2018-11-12 10:13:39 -08002100 return "/" + rel
2101}
2102
Cole Faust11edf552023-10-13 11:32:14 -07002103func modulePartition(ctx ModuleInstallPathContext, device bool) string {
Colin Cross43f08db2018-11-12 10:13:39 -08002104 var partition string
Colin Cross6e359402020-02-10 15:29:54 -08002105 if ctx.InstallInTestcases() {
2106 // "testcases" install directory can be used for host or device modules.
Jaewoong Jung0949f312019-09-11 10:25:18 -07002107 partition = "testcases"
Cole Faust11edf552023-10-13 11:32:14 -07002108 } else if device {
Colin Cross6e359402020-02-10 15:29:54 -08002109 if ctx.InstallInData() {
2110 partition = "data"
2111 } else if ctx.InstallInRamdisk() {
2112 if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() {
2113 partition = "recovery/root/first_stage_ramdisk"
2114 } else {
2115 partition = "ramdisk"
2116 }
2117 if !ctx.InstallInRoot() {
2118 partition += "/system"
2119 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -07002120 } else if ctx.InstallInVendorRamdisk() {
Yifan Hong39143a92020-10-26 12:43:12 -07002121 // The module is only available after switching root into
2122 // /first_stage_ramdisk. To expose the module before switching root
2123 // on a device without a dedicated recovery partition, install the
2124 // recovery variant.
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002125 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
Petri Gyntherac229562021-03-02 23:44:02 -08002126 partition = "vendor_ramdisk/first_stage_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002127 } else {
Petri Gyntherac229562021-03-02 23:44:02 -08002128 partition = "vendor_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002129 }
2130 if !ctx.InstallInRoot() {
2131 partition += "/system"
2132 }
Inseob Kim08758f02021-04-08 21:13:22 +09002133 } else if ctx.InstallInDebugRamdisk() {
2134 partition = "debug_ramdisk"
Colin Cross6e359402020-02-10 15:29:54 -08002135 } else if ctx.InstallInRecovery() {
2136 if ctx.InstallInRoot() {
2137 partition = "recovery/root"
2138 } else {
2139 // the layout of recovery partion is the same as that of system partition
2140 partition = "recovery/root/system"
2141 }
Colin Crossea30d852023-11-29 16:00:16 -08002142 } else if ctx.SocSpecific() || ctx.InstallInVendor() {
Colin Cross6e359402020-02-10 15:29:54 -08002143 partition = ctx.DeviceConfig().VendorPath()
Colin Crossea30d852023-11-29 16:00:16 -08002144 } else if ctx.DeviceSpecific() || ctx.InstallInOdm() {
Colin Cross6e359402020-02-10 15:29:54 -08002145 partition = ctx.DeviceConfig().OdmPath()
Colin Crossea30d852023-11-29 16:00:16 -08002146 } else if ctx.ProductSpecific() || ctx.InstallInProduct() {
Colin Cross6e359402020-02-10 15:29:54 -08002147 partition = ctx.DeviceConfig().ProductPath()
2148 } else if ctx.SystemExtSpecific() {
2149 partition = ctx.DeviceConfig().SystemExtPath()
2150 } else if ctx.InstallInRoot() {
2151 partition = "root"
Spandan Das27ff7672024-11-06 19:23:57 +00002152 } else if ctx.InstallInSystemDlkm() {
2153 partition = ctx.DeviceConfig().SystemDlkmPath()
2154 } else if ctx.InstallInVendorDlkm() {
2155 partition = ctx.DeviceConfig().VendorDlkmPath()
2156 } else if ctx.InstallInOdmDlkm() {
2157 partition = ctx.DeviceConfig().OdmDlkmPath()
Yifan Hong82db7352020-01-21 16:12:26 -08002158 } else {
Colin Cross6e359402020-02-10 15:29:54 -08002159 partition = "system"
Yifan Hong82db7352020-01-21 16:12:26 -08002160 }
Colin Cross6e359402020-02-10 15:29:54 -08002161 if ctx.InstallInSanitizerDir() {
2162 partition = "data/asan/" + partition
Yifan Hong82db7352020-01-21 16:12:26 -08002163 }
Colin Cross43f08db2018-11-12 10:13:39 -08002164 }
2165 return partition
2166}
2167
Colin Cross609c49a2020-02-13 13:20:11 -08002168type InstallPaths []InstallPath
2169
2170// Paths returns the InstallPaths as a Paths
2171func (p InstallPaths) Paths() Paths {
2172 if p == nil {
2173 return nil
2174 }
2175 ret := make(Paths, len(p))
2176 for i, path := range p {
2177 ret[i] = path
2178 }
2179 return ret
2180}
2181
2182// Strings returns the string forms of the install paths.
2183func (p InstallPaths) Strings() []string {
2184 if p == nil {
2185 return nil
2186 }
2187 ret := make([]string, len(p))
2188 for i, path := range p {
2189 ret[i] = path.String()
2190 }
2191 return ret
2192}
2193
Jingwen Chen24d0c562023-02-07 09:29:36 +00002194// validatePathInternal ensures that a path does not leave its component, and
2195// optionally doesn't contain Ninja variables.
2196func validatePathInternal(allowNinjaVariables bool, pathComponents ...string) (string, error) {
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002197 initialEmpty := 0
2198 finalEmpty := 0
2199 for i, path := range pathComponents {
Jingwen Chen24d0c562023-02-07 09:29:36 +00002200 if !allowNinjaVariables && strings.Contains(path, "$") {
2201 return "", fmt.Errorf("Path contains invalid character($): %s", path)
2202 }
2203
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002204 path := filepath.Clean(path)
2205 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08002206 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002207 }
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002208
2209 if i == initialEmpty && pathComponents[i] == "" {
2210 initialEmpty++
2211 }
2212 if i == finalEmpty && pathComponents[len(pathComponents)-1-i] == "" {
2213 finalEmpty++
2214 }
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002215 }
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002216 // Optimization: filepath.Join("foo", "") returns a newly allocated copy
2217 // of "foo", while filepath.Join("foo") does not. Strip out any empty
2218 // path components.
2219 if initialEmpty == len(pathComponents) {
2220 return "", nil
2221 }
2222 nonEmptyPathComponents := pathComponents[initialEmpty : len(pathComponents)-finalEmpty]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002223 // TODO: filepath.Join isn't necessarily correct with embedded ninja
2224 // variables. '..' may remove the entire ninja variable, even if it
2225 // will be expanded to multiple nested directories.
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002226 return filepath.Join(nonEmptyPathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002227}
2228
Jingwen Chen24d0c562023-02-07 09:29:36 +00002229// validateSafePath validates a path that we trust (may contain ninja
2230// variables). Ensures that each path component does not attempt to leave its
2231// component. Returns a joined version of each path component.
2232func validateSafePath(pathComponents ...string) (string, error) {
2233 return validatePathInternal(true, pathComponents...)
2234}
2235
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002236// validatePath validates that a path does not include ninja variables, and that
2237// each path component does not attempt to leave its component. Returns a joined
2238// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08002239func validatePath(pathComponents ...string) (string, error) {
Jingwen Chen24d0c562023-02-07 09:29:36 +00002240 return validatePathInternal(false, pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -07002241}
Colin Cross5b529592017-05-09 13:34:34 -07002242
Colin Cross0875c522017-11-28 17:34:01 -08002243func PathForPhony(ctx PathContext, phony string) WritablePath {
2244 if strings.ContainsAny(phony, "$/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002245 ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -08002246 }
Paul Duffin74abc5d2021-03-24 09:24:59 +00002247 return PhonyPath{basePath{phony, ""}}
Colin Cross0875c522017-11-28 17:34:01 -08002248}
2249
Colin Cross74e3fe42017-12-11 15:51:44 -08002250type PhonyPath struct {
2251 basePath
2252}
2253
2254func (p PhonyPath) writablePath() {}
2255
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02002256func (p PhonyPath) getSoongOutDir() string {
Paul Duffind65c58b2021-03-24 09:22:07 +00002257 // A phone path cannot contain any / so cannot be relative to the build directory.
2258 return ""
Paul Duffin9b478b02019-12-10 13:41:51 +00002259}
2260
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002261func (p PhonyPath) RelativeToTop() Path {
2262 ensureTestOnly()
2263 // A phony path cannot contain any / so does not have a build directory so switching to a new
2264 // build directory has no effect so just return this path.
2265 return p
2266}
2267
Colin Cross7707b242024-07-26 12:02:36 -07002268func (p PhonyPath) WithoutRel() Path {
2269 p.basePath = p.basePath.withoutRel()
2270 return p
2271}
2272
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01002273func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
2274 panic("Not implemented")
2275}
2276
Colin Cross74e3fe42017-12-11 15:51:44 -08002277var _ Path = PhonyPath{}
2278var _ WritablePath = PhonyPath{}
2279
Colin Cross5b529592017-05-09 13:34:34 -07002280type testPath struct {
2281 basePath
2282}
2283
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002284func (p testPath) RelativeToTop() Path {
2285 ensureTestOnly()
2286 return p
2287}
2288
Colin Cross7707b242024-07-26 12:02:36 -07002289func (p testPath) WithoutRel() Path {
2290 p.basePath = p.basePath.withoutRel()
2291 return p
2292}
2293
Colin Cross5b529592017-05-09 13:34:34 -07002294func (p testPath) String() string {
2295 return p.path
2296}
2297
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002298var _ Path = testPath{}
2299
Colin Cross40e33732019-02-15 11:08:35 -08002300// PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from
2301// within tests.
Colin Cross5b529592017-05-09 13:34:34 -07002302func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -08002303 p, err := validateSafePath(paths...)
2304 if err != nil {
2305 panic(err)
2306 }
Colin Cross5b529592017-05-09 13:34:34 -07002307 return testPath{basePath{path: p, rel: p}}
2308}
2309
Sam Delmerico2351eac2022-05-24 17:10:02 +00002310func PathForTestingWithRel(path, rel string) Path {
2311 p, err := validateSafePath(path, rel)
2312 if err != nil {
2313 panic(err)
2314 }
2315 r, err := validatePath(rel)
2316 if err != nil {
2317 panic(err)
2318 }
2319 return testPath{basePath{path: p, rel: r}}
2320}
2321
Colin Cross40e33732019-02-15 11:08:35 -08002322// PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests.
2323func PathsForTesting(strs ...string) Paths {
Colin Cross5b529592017-05-09 13:34:34 -07002324 p := make(Paths, len(strs))
2325 for i, s := range strs {
2326 p[i] = PathForTesting(s)
2327 }
2328
2329 return p
2330}
Colin Cross43f08db2018-11-12 10:13:39 -08002331
Colin Cross40e33732019-02-15 11:08:35 -08002332type testPathContext struct {
2333 config Config
Colin Cross40e33732019-02-15 11:08:35 -08002334}
2335
Colin Cross40e33732019-02-15 11:08:35 -08002336func (x *testPathContext) Config() Config { return x.config }
2337func (x *testPathContext) AddNinjaFileDeps(...string) {}
2338
2339// PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
2340// PathForOutput.
Colin Cross98be1bb2019-12-13 20:41:13 -08002341func PathContextForTesting(config Config) PathContext {
Colin Cross40e33732019-02-15 11:08:35 -08002342 return &testPathContext{
2343 config: config,
Colin Cross40e33732019-02-15 11:08:35 -08002344 }
2345}
2346
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002347type testModuleInstallPathContext struct {
2348 baseModuleContext
2349
2350 inData bool
2351 inTestcases bool
2352 inSanitizerDir bool
2353 inRamdisk bool
2354 inVendorRamdisk bool
Inseob Kim08758f02021-04-08 21:13:22 +09002355 inDebugRamdisk bool
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002356 inRecovery bool
2357 inRoot bool
Colin Crossea30d852023-11-29 16:00:16 -08002358 inOdm bool
2359 inProduct bool
2360 inVendor bool
Spandan Das27ff7672024-11-06 19:23:57 +00002361 inSystemDlkm bool
2362 inVendorDlkm bool
2363 inOdmDlkm bool
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002364 forceOS *OsType
2365 forceArch *ArchType
2366}
2367
2368func (m testModuleInstallPathContext) Config() Config {
2369 return m.baseModuleContext.config
2370}
2371
2372func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {}
2373
2374func (m testModuleInstallPathContext) InstallInData() bool {
2375 return m.inData
2376}
2377
2378func (m testModuleInstallPathContext) InstallInTestcases() bool {
2379 return m.inTestcases
2380}
2381
2382func (m testModuleInstallPathContext) InstallInSanitizerDir() bool {
2383 return m.inSanitizerDir
2384}
2385
2386func (m testModuleInstallPathContext) InstallInRamdisk() bool {
2387 return m.inRamdisk
2388}
2389
2390func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool {
2391 return m.inVendorRamdisk
2392}
2393
Inseob Kim08758f02021-04-08 21:13:22 +09002394func (m testModuleInstallPathContext) InstallInDebugRamdisk() bool {
2395 return m.inDebugRamdisk
2396}
2397
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002398func (m testModuleInstallPathContext) InstallInRecovery() bool {
2399 return m.inRecovery
2400}
2401
2402func (m testModuleInstallPathContext) InstallInRoot() bool {
2403 return m.inRoot
2404}
2405
Colin Crossea30d852023-11-29 16:00:16 -08002406func (m testModuleInstallPathContext) InstallInOdm() bool {
2407 return m.inOdm
2408}
2409
2410func (m testModuleInstallPathContext) InstallInProduct() bool {
2411 return m.inProduct
2412}
2413
2414func (m testModuleInstallPathContext) InstallInVendor() bool {
2415 return m.inVendor
2416}
2417
Spandan Das27ff7672024-11-06 19:23:57 +00002418func (m testModuleInstallPathContext) InstallInSystemDlkm() bool {
2419 return m.inSystemDlkm
2420}
2421
2422func (m testModuleInstallPathContext) InstallInVendorDlkm() bool {
2423 return m.inVendorDlkm
2424}
2425
2426func (m testModuleInstallPathContext) InstallInOdmDlkm() bool {
2427 return m.inOdmDlkm
2428}
2429
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002430func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
2431 return m.forceOS, m.forceArch
2432}
2433
2434// Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is
2435// default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are
2436// delegated to it will panic.
2437func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext {
2438 ctx := &testModuleInstallPathContext{}
2439 ctx.config = config
2440 ctx.os = Android
2441 return ctx
2442}
2443
Colin Cross43f08db2018-11-12 10:13:39 -08002444// Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
2445// targetPath is not inside basePath.
2446func Rel(ctx PathContext, basePath string, targetPath string) string {
2447 rel, isRel := MaybeRel(ctx, basePath, targetPath)
2448 if !isRel {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002449 ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
Colin Cross43f08db2018-11-12 10:13:39 -08002450 return ""
2451 }
2452 return rel
2453}
2454
2455// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
2456// targetPath is not inside basePath.
2457func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
Dan Willemsen633c5022019-04-12 11:11:38 -07002458 rel, isRel, err := maybeRelErr(basePath, targetPath)
2459 if err != nil {
2460 reportPathError(ctx, err)
2461 }
2462 return rel, isRel
2463}
2464
2465func maybeRelErr(basePath string, targetPath string) (string, bool, error) {
Colin Cross43f08db2018-11-12 10:13:39 -08002466 // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
2467 if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
Dan Willemsen633c5022019-04-12 11:11:38 -07002468 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002469 }
2470 rel, err := filepath.Rel(basePath, targetPath)
2471 if err != nil {
Dan Willemsen633c5022019-04-12 11:11:38 -07002472 return "", false, err
Colin Cross43f08db2018-11-12 10:13:39 -08002473 } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
Dan Willemsen633c5022019-04-12 11:11:38 -07002474 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002475 }
Dan Willemsen633c5022019-04-12 11:11:38 -07002476 return rel, true, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002477}
Colin Cross988414c2020-01-11 01:11:46 +00002478
2479// Writes a file to the output directory. Attempting to write directly to the output directory
2480// will fail due to the sandbox of the soong_build process.
Chris Parsons1a12d032023-02-06 22:37:41 -05002481// Only writes the file if the file doesn't exist or if it has different contents, to prevent
2482// updating the timestamp if no changes would be made. (This is better for incremental
2483// performance.)
Colin Cross988414c2020-01-11 01:11:46 +00002484func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error {
Colin Crossd6421132021-11-09 12:32:34 -08002485 absPath := absolutePath(path.String())
2486 err := os.MkdirAll(filepath.Dir(absPath), 0777)
2487 if err != nil {
2488 return err
2489 }
Chris Parsons1a12d032023-02-06 22:37:41 -05002490 return pathtools.WriteFileIfChanged(absPath, data, perm)
Colin Cross988414c2020-01-11 01:11:46 +00002491}
2492
Liz Kammer2dd9ca42020-11-25 16:06:39 -08002493func RemoveAllOutputDir(path WritablePath) error {
2494 return os.RemoveAll(absolutePath(path.String()))
2495}
2496
2497func CreateOutputDirIfNonexistent(path WritablePath, perm os.FileMode) error {
2498 dir := absolutePath(path.String())
Liz Kammer09f947d2021-05-12 14:51:49 -04002499 return createDirIfNonexistent(dir, perm)
2500}
2501
2502func createDirIfNonexistent(dir string, perm os.FileMode) error {
Liz Kammer2dd9ca42020-11-25 16:06:39 -08002503 if _, err := os.Stat(dir); os.IsNotExist(err) {
2504 return os.MkdirAll(dir, os.ModePerm)
2505 } else {
2506 return err
2507 }
2508}
2509
Jingwen Chen78257e52021-05-21 02:34:24 +00002510// absolutePath is deliberately private so that Soong's Go plugins can't use it to find and
2511// read arbitrary files without going through the methods in the current package that track
2512// dependencies.
Colin Cross988414c2020-01-11 01:11:46 +00002513func absolutePath(path string) string {
2514 if filepath.IsAbs(path) {
2515 return path
2516 }
2517 return filepath.Join(absSrcDir, path)
2518}
Chris Parsons216e10a2020-07-09 17:12:52 -04002519
2520// A DataPath represents the path of a file to be used as data, for example
2521// a test library to be installed alongside a test.
2522// The data file should be installed (copied from `<SrcPath>`) to
2523// `<install_root>/<RelativeInstallPath>/<filename>`, or
2524// `<install_root>/<filename>` if RelativeInstallPath is empty.
2525type DataPath struct {
2526 // The path of the data file that should be copied into the data directory
2527 SrcPath Path
2528 // The install path of the data file, relative to the install root.
2529 RelativeInstallPath string
Colin Cross5c1d5fb2023-11-15 12:39:40 -08002530 // If WithoutRel is true, use SrcPath.Base() instead of SrcPath.Rel() as the filename.
2531 WithoutRel bool
Chris Parsons216e10a2020-07-09 17:12:52 -04002532}
Colin Crossdcf71b22021-02-01 13:59:03 -08002533
Colin Crossd442a0e2023-11-16 11:19:26 -08002534func (d *DataPath) ToRelativeInstallPath() string {
2535 relPath := d.SrcPath.Rel()
Colin Cross5c1d5fb2023-11-15 12:39:40 -08002536 if d.WithoutRel {
2537 relPath = d.SrcPath.Base()
2538 }
Colin Crossd442a0e2023-11-16 11:19:26 -08002539 if d.RelativeInstallPath != "" {
2540 relPath = filepath.Join(d.RelativeInstallPath, relPath)
2541 }
2542 return relPath
2543}
2544
Colin Crossdcf71b22021-02-01 13:59:03 -08002545// PathsIfNonNil returns a Paths containing only the non-nil input arguments.
2546func PathsIfNonNil(paths ...Path) Paths {
2547 if len(paths) == 0 {
2548 // Fast path for empty argument list
2549 return nil
2550 } else if len(paths) == 1 {
2551 // Fast path for a single argument
2552 if paths[0] != nil {
2553 return paths
2554 } else {
2555 return nil
2556 }
2557 }
2558 ret := make(Paths, 0, len(paths))
2559 for _, path := range paths {
2560 if path != nil {
2561 ret = append(ret, path)
2562 }
2563 }
2564 if len(ret) == 0 {
2565 return nil
2566 }
2567 return ret
2568}
Chris Wailesb2703ad2021-07-30 13:25:42 -07002569
2570var thirdPartyDirPrefixExceptions = []*regexp.Regexp{
2571 regexp.MustCompile("^vendor/[^/]*google[^/]*/"),
2572 regexp.MustCompile("^hardware/google/"),
2573 regexp.MustCompile("^hardware/interfaces/"),
2574 regexp.MustCompile("^hardware/libhardware[^/]*/"),
2575 regexp.MustCompile("^hardware/ril/"),
2576}
2577
2578func IsThirdPartyPath(path string) bool {
2579 thirdPartyDirPrefixes := []string{"external/", "vendor/", "hardware/"}
2580
2581 if HasAnyPrefix(path, thirdPartyDirPrefixes) {
2582 for _, prefix := range thirdPartyDirPrefixExceptions {
2583 if prefix.MatchString(path) {
2584 return false
2585 }
2586 }
2587 return true
2588 }
2589 return false
2590}
Jihoon Kangf27c3a52024-11-12 21:27:09 +00002591
2592// ToRelativeSourcePath converts absolute source path to the path relative to the source root.
2593// This throws an error if the input path is outside of the source root and cannot be converted
2594// to the relative path.
2595// This should be rarely used given that the source path is relative in Soong.
2596func ToRelativeSourcePath(ctx PathContext, path string) string {
2597 ret := path
2598 if filepath.IsAbs(path) {
2599 relPath, err := filepath.Rel(absSrcDir, path)
2600 if err != nil || strings.HasPrefix(relPath, "..") {
2601 ReportPathErrorf(ctx, "%s is outside of the source root", path)
2602 }
2603 ret = relPath
2604 }
2605 return ret
2606}