blob: 96fa05623163257946d60dce55b03bc8bc6889dc [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))
Paul Duffin40131a32021-07-09 17:10:35 +010094 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
Cole Faust4e2bf9f2024-09-11 13:26:20 -070095 HasMutatorFinished(mutatorName string) bool
Liz Kammera830f3a2020-11-10 10:50:34 -080096}
97
98// ModuleMissingDepsPathContext is a subset of *ModuleContext methods required by
99// the Path methods that rely on module dependencies having been resolved and ability to report
100// missing dependency errors.
101type ModuleMissingDepsPathContext interface {
102 ModuleWithDepsPathContext
103 AddMissingDependencies(missingDeps []string)
104}
105
Dan Willemsen00269f22017-07-06 16:59:48 -0700106type ModuleInstallPathContext interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700107 BaseModuleContext
Dan Willemsen00269f22017-07-06 16:59:48 -0700108
109 InstallInData() bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700110 InstallInTestcases() bool
Dan Willemsen00269f22017-07-06 16:59:48 -0700111 InstallInSanitizerDir() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -0800112 InstallInRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700113 InstallInVendorRamdisk() bool
Inseob Kim08758f02021-04-08 21:13:22 +0900114 InstallInDebugRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900115 InstallInRecovery() bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700116 InstallInRoot() bool
Colin Crossea30d852023-11-29 16:00:16 -0800117 InstallInOdm() bool
118 InstallInProduct() bool
119 InstallInVendor() bool
Jiyong Park87788b52020-09-01 12:37:45 +0900120 InstallForceOS() (*OsType, *ArchType)
Dan Willemsen00269f22017-07-06 16:59:48 -0700121}
122
123var _ ModuleInstallPathContext = ModuleContext(nil)
124
Cole Faust11edf552023-10-13 11:32:14 -0700125type baseModuleContextToModuleInstallPathContext struct {
126 BaseModuleContext
127}
128
129func (ctx *baseModuleContextToModuleInstallPathContext) InstallInData() bool {
130 return ctx.Module().InstallInData()
131}
132
133func (ctx *baseModuleContextToModuleInstallPathContext) InstallInTestcases() bool {
134 return ctx.Module().InstallInTestcases()
135}
136
137func (ctx *baseModuleContextToModuleInstallPathContext) InstallInSanitizerDir() bool {
138 return ctx.Module().InstallInSanitizerDir()
139}
140
141func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRamdisk() bool {
142 return ctx.Module().InstallInRamdisk()
143}
144
145func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendorRamdisk() bool {
146 return ctx.Module().InstallInVendorRamdisk()
147}
148
149func (ctx *baseModuleContextToModuleInstallPathContext) InstallInDebugRamdisk() bool {
150 return ctx.Module().InstallInDebugRamdisk()
151}
152
153func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRecovery() bool {
154 return ctx.Module().InstallInRecovery()
155}
156
157func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRoot() bool {
158 return ctx.Module().InstallInRoot()
159}
160
Colin Crossea30d852023-11-29 16:00:16 -0800161func (ctx *baseModuleContextToModuleInstallPathContext) InstallInOdm() bool {
162 return ctx.Module().InstallInOdm()
163}
164
165func (ctx *baseModuleContextToModuleInstallPathContext) InstallInProduct() bool {
166 return ctx.Module().InstallInProduct()
167}
168
169func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendor() bool {
170 return ctx.Module().InstallInVendor()
171}
172
Cole Faust11edf552023-10-13 11:32:14 -0700173func (ctx *baseModuleContextToModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
174 return ctx.Module().InstallForceOS()
175}
176
177var _ ModuleInstallPathContext = (*baseModuleContextToModuleInstallPathContext)(nil)
178
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700179// errorfContext is the interface containing the Errorf method matching the
180// Errorf method in blueprint.SingletonContext.
181type errorfContext interface {
182 Errorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800183}
184
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700185var _ errorfContext = blueprint.SingletonContext(nil)
186
Spandan Das59a4a2b2024-01-09 21:35:56 +0000187// ModuleErrorfContext is the interface containing the ModuleErrorf method matching
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700188// the ModuleErrorf method in blueprint.ModuleContext.
Spandan Das59a4a2b2024-01-09 21:35:56 +0000189type ModuleErrorfContext interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700190 ModuleErrorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800191}
192
Spandan Das59a4a2b2024-01-09 21:35:56 +0000193var _ ModuleErrorfContext = blueprint.ModuleContext(nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700194
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700195// reportPathError will register an error with the attached context. It
196// attempts ctx.ModuleErrorf for a better error message first, then falls
197// back to ctx.Errorf.
Colin Cross1ccfcc32018-02-22 13:54:26 -0800198func reportPathError(ctx PathContext, err error) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100199 ReportPathErrorf(ctx, "%s", err.Error())
Colin Cross1ccfcc32018-02-22 13:54:26 -0800200}
201
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100202// ReportPathErrorf will register an error with the attached context. It
Colin Cross1ccfcc32018-02-22 13:54:26 -0800203// attempts ctx.ModuleErrorf for a better error message first, then falls
204// back to ctx.Errorf.
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100205func ReportPathErrorf(ctx PathContext, format string, args ...interface{}) {
Spandan Das59a4a2b2024-01-09 21:35:56 +0000206 if mctx, ok := ctx.(ModuleErrorfContext); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700207 mctx.ModuleErrorf(format, args...)
208 } else if ectx, ok := ctx.(errorfContext); ok {
209 ectx.Errorf(format, args...)
210 } else {
211 panic(fmt.Sprintf(format, args...))
Colin Crossf2298272015-05-12 11:36:53 -0700212 }
213}
214
Colin Cross5e708052019-08-06 13:59:50 -0700215func pathContextName(ctx PathContext, module blueprint.Module) string {
216 if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok {
217 return x.ModuleName(module)
218 } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok {
219 return x.OtherModuleName(module)
220 }
221 return "unknown"
222}
223
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700224type Path interface {
225 // Returns the path in string form
226 String() string
227
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700228 // Ext returns the extension of the last element of the path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700229 Ext() string
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700230
231 // Base returns the last element of the path
232 Base() string
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800233
234 // Rel returns the portion of the path relative to the directory it was created from. For
235 // example, Rel on a PathsForModuleSrc would return the path relative to the module source
Colin Cross0db55682017-12-05 15:36:55 -0800236 // directory, and OutputPath.Join("foo").Rel() would return "foo".
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800237 Rel() string
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000238
Colin Cross7707b242024-07-26 12:02:36 -0700239 // WithoutRel returns a new Path with no relative path, i.e. Rel() will return the same value as Base().
240 WithoutRel() Path
241
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000242 // RelativeToTop returns a new path relative to the top, it is provided solely for use in tests.
243 //
244 // It is guaranteed to always return the same type as it is called on, e.g. if called on an
245 // InstallPath then the returned value can be converted to an InstallPath.
246 //
247 // A standard build has the following structure:
248 // ../top/
249 // out/ - make install files go here.
Colin Cross3b1c6842024-07-26 11:52:57 -0700250 // out/soong - this is the outDir passed to NewTestConfig()
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000251 // ... - the source files
252 //
253 // This function converts a path so that it appears relative to the ../top/ directory, i.e.
Colin Cross3b1c6842024-07-26 11:52:57 -0700254 // * Make install paths, which have the pattern "outDir/../<path>" are converted into the top
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000255 // relative path "out/<path>"
Colin Cross3b1c6842024-07-26 11:52:57 -0700256 // * Soong install paths and other writable paths, which have the pattern "outDir/soong/<path>" are
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000257 // converted into the top relative path "out/soong/<path>".
258 // * Source paths are already relative to the top.
259 // * Phony paths are not relative to anything.
260 // * toolDepPath have an absolute but known value in so don't need making relative to anything in
261 // order to test.
262 RelativeToTop() Path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700263}
264
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000265const (
Colin Cross3b1c6842024-07-26 11:52:57 -0700266 testOutDir = "out"
267 testOutSoongSubDir = "/soong"
268 TestOutSoongDir = testOutDir + testOutSoongSubDir
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000269)
270
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700271// WritablePath is a type of path that can be used as an output for build rules.
272type WritablePath interface {
273 Path
274
Paul Duffin9b478b02019-12-10 13:41:51 +0000275 // return the path to the build directory.
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200276 getSoongOutDir() string
Paul Duffin9b478b02019-12-10 13:41:51 +0000277
Jeff Gaston734e3802017-04-10 15:47:24 -0700278 // the writablePath method doesn't directly do anything,
279 // but it allows a struct to distinguish between whether or not it implements the WritablePath interface
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700280 writablePath()
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +0100281
282 ReplaceExtension(ctx PathContext, ext string) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700283}
284
285type genPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800286 genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath
yangbill6d032dd2024-04-18 03:05:49 +0000287 genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700288}
289type objPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800290 objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700291}
292type resPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800293 resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700294}
295
296// GenPathWithExt derives a new file path in ctx's generated sources directory
297// from the current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800298func GenPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700299 if path, ok := p.(genPathProvider); ok {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700300 return path.genPathWithExt(ctx, subdir, ext)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700301 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100302 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700303 return PathForModuleGen(ctx)
304}
305
yangbill6d032dd2024-04-18 03:05:49 +0000306// GenPathWithExtAndTrimExt derives a new file path in ctx's generated sources directory
307// from the current path, but with the new extension and trim the suffix.
308func GenPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir string, p Path, ext string, trimExt string) ModuleGenPath {
309 if path, ok := p.(genPathProvider); ok {
310 return path.genPathWithExtAndTrimExt(ctx, subdir, ext, trimExt)
311 }
312 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
313 return PathForModuleGen(ctx)
314}
315
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700316// ObjPathWithExt derives a new file path in ctx's object directory from the
317// current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800318func ObjPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700319 if path, ok := p.(objPathProvider); ok {
320 return path.objPathWithExt(ctx, subdir, ext)
321 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100322 ReportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700323 return PathForModuleObj(ctx)
324}
325
326// ResPathWithName derives a new path in ctx's output resource directory, using
327// the current path to create the directory name, and the `name` argument for
328// the filename.
Liz Kammera830f3a2020-11-10 10:50:34 -0800329func ResPathWithName(ctx ModuleOutPathContext, p Path, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700330 if path, ok := p.(resPathProvider); ok {
331 return path.resPathWithName(ctx, name)
332 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100333 ReportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700334 return PathForModuleRes(ctx)
335}
336
337// OptionalPath is a container that may or may not contain a valid Path.
338type OptionalPath struct {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100339 path Path // nil if invalid.
340 invalidReason string // Not applicable if path != nil. "" if the reason is unknown.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700341}
342
Yu Liu467d7c52024-09-18 21:54:44 +0000343type optionalPathGob struct {
344 Path Path
345 InvalidReason string
346}
347
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700348// OptionalPathForPath returns an OptionalPath containing the path.
349func OptionalPathForPath(path Path) OptionalPath {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100350 return OptionalPath{path: path}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700351}
352
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100353// InvalidOptionalPath returns an OptionalPath that is invalid with the given reason.
354func InvalidOptionalPath(reason string) OptionalPath {
355
356 return OptionalPath{invalidReason: reason}
357}
358
Yu Liu467d7c52024-09-18 21:54:44 +0000359func (p *OptionalPath) ToGob() *optionalPathGob {
360 return &optionalPathGob{
361 Path: p.path,
362 InvalidReason: p.invalidReason,
363 }
364}
365
366func (p *OptionalPath) FromGob(data *optionalPathGob) {
367 p.path = data.Path
368 p.invalidReason = data.InvalidReason
369}
370
371func (p OptionalPath) GobEncode() ([]byte, error) {
Yu Liu3cadf7d2024-10-24 18:47:06 +0000372 return gobtools.CustomGobEncode[optionalPathGob](&p)
Yu Liu467d7c52024-09-18 21:54:44 +0000373}
374
375func (p *OptionalPath) GobDecode(data []byte) error {
Yu Liu3cadf7d2024-10-24 18:47:06 +0000376 return gobtools.CustomGobDecode[optionalPathGob](data, p)
Yu Liu467d7c52024-09-18 21:54:44 +0000377}
378
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700379// Valid returns whether there is a valid path
380func (p OptionalPath) Valid() bool {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100381 return p.path != nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700382}
383
384// Path returns the Path embedded in this OptionalPath. You must be sure that
385// there is a valid path, since this method will panic if there is not.
386func (p OptionalPath) Path() Path {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100387 if p.path == nil {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100388 msg := "Requesting an invalid path"
389 if p.invalidReason != "" {
390 msg += ": " + p.invalidReason
391 }
392 panic(msg)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700393 }
394 return p.path
395}
396
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100397// InvalidReason returns the reason that the optional path is invalid, or "" if it is valid.
398func (p OptionalPath) InvalidReason() string {
399 if p.path != nil {
400 return ""
401 }
402 if p.invalidReason == "" {
403 return "unknown"
404 }
405 return p.invalidReason
406}
407
Paul Duffinef081852021-05-13 11:11:15 +0100408// AsPaths converts the OptionalPath into Paths.
409//
410// It returns nil if this is not valid, or a single length slice containing the Path embedded in
411// this OptionalPath.
412func (p OptionalPath) AsPaths() Paths {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100413 if p.path == nil {
Paul Duffinef081852021-05-13 11:11:15 +0100414 return nil
415 }
416 return Paths{p.path}
417}
418
Paul Duffinafdd4062021-03-30 19:44:07 +0100419// RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the
420// result of calling Path.RelativeToTop on it.
421func (p OptionalPath) RelativeToTop() OptionalPath {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100422 if p.path == nil {
Paul Duffina5b81352021-03-28 23:57:19 +0100423 return p
424 }
425 p.path = p.path.RelativeToTop()
426 return p
427}
428
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700429// String returns the string version of the Path, or "" if it isn't valid.
430func (p OptionalPath) String() string {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100431 if p.path != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700432 return p.path.String()
433 } else {
434 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700435 }
436}
Colin Cross6e18ca42015-07-14 18:55:36 -0700437
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700438// Paths is a slice of Path objects, with helpers to operate on the collection.
439type Paths []Path
440
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000441// RelativeToTop creates a new Paths containing the result of calling Path.RelativeToTop on each
442// item in this slice.
443func (p Paths) RelativeToTop() Paths {
444 ensureTestOnly()
445 if p == nil {
446 return p
447 }
448 ret := make(Paths, len(p))
449 for i, path := range p {
450 ret[i] = path.RelativeToTop()
451 }
452 return ret
453}
454
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000455func (paths Paths) containsPath(path Path) bool {
456 for _, p := range paths {
457 if p == path {
458 return true
459 }
460 }
461 return false
462}
463
Liz Kammer7aa52882021-02-11 09:16:14 -0500464// PathsForSource returns Paths rooted from SrcDir, *not* rooted from the module's local source
465// directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700466func PathsForSource(ctx PathContext, paths []string) Paths {
467 ret := make(Paths, len(paths))
468 for i, path := range paths {
469 ret[i] = PathForSource(ctx, path)
470 }
471 return ret
472}
473
Liz Kammer7aa52882021-02-11 09:16:14 -0500474// ExistentPathsForSources returns a list of Paths rooted from SrcDir, *not* rooted from the
475// module's local source directory, that are found in the tree. If any are not found, they are
476// 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 -0700477func ExistentPathsForSources(ctx PathGlobContext, paths []string) Paths {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800478 ret := make(Paths, 0, len(paths))
479 for _, path := range paths {
Colin Cross32f38982018-02-22 11:47:25 -0800480 p := ExistentPathForSource(ctx, path)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800481 if p.Valid() {
482 ret = append(ret, p.Path())
483 }
484 }
485 return ret
486}
487
Liz Kammer620dea62021-04-14 17:36:10 -0400488// PathsForModuleSrc returns a Paths{} containing the resolved references in paths:
Colin Crossd079e0b2022-08-16 10:27:33 -0700489// - filepath, relative to local module directory, resolves as a filepath relative to the local
490// source directory
491// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
492// source directory.
493// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700494// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
495// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700496//
Liz Kammer620dea62021-04-14 17:36:10 -0400497// Properties passed as the paths argument must have been annotated with struct tag
Colin Cross41955e82019-05-29 14:40:35 -0700498// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000499// pathdeps mutator.
Liz Kammer620dea62021-04-14 17:36:10 -0400500// If a requested module is not found as a dependency:
Colin Crossd079e0b2022-08-16 10:27:33 -0700501// - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
Liz Kammer620dea62021-04-14 17:36:10 -0400502// missing dependencies
Colin Crossd079e0b2022-08-16 10:27:33 -0700503// - otherwise, a ModuleError is thrown.
Liz Kammera830f3a2020-11-10 10:50:34 -0800504func PathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths {
Colin Cross8a497952019-03-05 22:25:09 -0800505 return PathsForModuleSrcExcludes(ctx, paths, nil)
506}
507
Liz Kammer619be462022-01-28 15:13:39 -0500508type SourceInput struct {
509 Context ModuleMissingDepsPathContext
510 Paths []string
511 ExcludePaths []string
512 IncludeDirs bool
513}
514
Liz Kammer620dea62021-04-14 17:36:10 -0400515// PathsForModuleSrcExcludes returns a Paths{} containing the resolved references in paths, minus
516// those listed in excludes. Elements of paths and excludes are resolved as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700517// - filepath, relative to local module directory, resolves as a filepath relative to the local
518// source directory
519// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
520// source directory. Not valid in excludes.
521// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700522// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
523// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700524//
Liz Kammer620dea62021-04-14 17:36:10 -0400525// excluding the items (similarly resolved
526// Properties passed as the paths argument must have been annotated with struct tag
527// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000528// pathdeps mutator.
Liz Kammer620dea62021-04-14 17:36:10 -0400529// If a requested module is not found as a dependency:
Colin Crossd079e0b2022-08-16 10:27:33 -0700530// - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
Liz Kammer620dea62021-04-14 17:36:10 -0400531// missing dependencies
Colin Crossd079e0b2022-08-16 10:27:33 -0700532// - otherwise, a ModuleError is thrown.
Liz Kammera830f3a2020-11-10 10:50:34 -0800533func PathsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) Paths {
Liz Kammer619be462022-01-28 15:13:39 -0500534 return PathsRelativeToModuleSourceDir(SourceInput{
535 Context: ctx,
536 Paths: paths,
537 ExcludePaths: excludes,
538 IncludeDirs: true,
539 })
540}
541
542func PathsRelativeToModuleSourceDir(input SourceInput) Paths {
543 ret, missingDeps := PathsAndMissingDepsRelativeToModuleSourceDir(input)
544 if input.Context.Config().AllowMissingDependencies() {
545 input.Context.AddMissingDependencies(missingDeps)
Colin Crossba71a3f2019-03-18 12:12:48 -0700546 } else {
547 for _, m := range missingDeps {
Liz Kammer619be462022-01-28 15:13:39 -0500548 input.Context.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
Colin Crossba71a3f2019-03-18 12:12:48 -0700549 }
550 }
551 return ret
552}
553
Inseob Kim76e19852024-10-10 17:57:22 +0900554// DirectoryPathsForModuleSrcExcludes returns a Paths{} containing the resolved references in
555// directory paths. Elements of paths are resolved as:
556// - filepath, relative to local module directory, resolves as a filepath relative to the local
557// source directory
558// - other modules using the ":name" syntax. These modules must implement DirProvider.
559//
560// TODO(b/358302178): Implement DirectoryPath and change the return type.
561func DirectoryPathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths {
562 var ret Paths
563
564 for _, path := range paths {
565 if m, t := SrcIsModuleWithTag(path); m != "" {
566 module := GetModuleFromPathDep(ctx, m, t)
567 if module == nil {
568 ctx.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
569 continue
570 }
571 if t != "" {
572 ctx.ModuleErrorf("DirProvider dependency %q does not support the tag %q", module, t)
573 continue
574 }
575 mctx, ok := ctx.(OtherModuleProviderContext)
576 if !ok {
577 panic(fmt.Errorf("%s is not an OtherModuleProviderContext", ctx))
578 }
579 if dirProvider, ok := OtherModuleProvider(mctx, module, DirProvider); ok {
580 ret = append(ret, dirProvider.Dirs...)
581 } else {
582 ReportPathErrorf(ctx, "module %q does not implement DirProvider", module)
583 }
584 } else {
585 p := pathForModuleSrc(ctx, path)
586 if isDir, err := ctx.Config().fs.IsDir(p.String()); err != nil {
587 ReportPathErrorf(ctx, "%s: %s", p, err.Error())
588 } else if !isDir {
589 ReportPathErrorf(ctx, "module directory path %q is not a directory", p)
590 } else {
591 ret = append(ret, p)
592 }
593 }
594 }
595
596 seen := make(map[Path]bool, len(ret))
597 for _, path := range ret {
598 if seen[path] {
599 ReportPathErrorf(ctx, "duplicated path %q", path)
600 }
601 seen[path] = true
602 }
603 return ret
604}
605
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000606// OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection.
607type OutputPaths []OutputPath
608
609// Paths returns the OutputPaths as a Paths
610func (p OutputPaths) Paths() Paths {
611 if p == nil {
612 return nil
613 }
614 ret := make(Paths, len(p))
615 for i, path := range p {
616 ret[i] = path
617 }
618 return ret
619}
620
621// Strings returns the string forms of the writable paths.
622func (p OutputPaths) Strings() []string {
623 if p == nil {
624 return nil
625 }
626 ret := make([]string, len(p))
627 for i, path := range p {
628 ret[i] = path.String()
629 }
630 return ret
631}
632
Liz Kammera830f3a2020-11-10 10:50:34 -0800633// Expands Paths to a SourceFileProducer or OutputFileProducer module dependency referenced via ":name" or ":name{.tag}" syntax.
634// If the dependency is not found, a missingErrorDependency is returned.
635// If the module dependency is not a SourceFileProducer or OutputFileProducer, appropriate errors will be returned.
636func getPathsFromModuleDep(ctx ModuleWithDepsPathContext, path, moduleName, tag string) (Paths, error) {
Paul Duffind5cf92e2021-07-09 17:38:55 +0100637 module := GetModuleFromPathDep(ctx, moduleName, tag)
Liz Kammera830f3a2020-11-10 10:50:34 -0800638 if module == nil {
639 return nil, missingDependencyError{[]string{moduleName}}
640 }
Cole Fausta963b942024-04-11 17:43:00 -0700641 if aModule, ok := module.(Module); ok && !aModule.Enabled(ctx) {
Colin Crossfa65cee2021-03-22 17:05:59 -0700642 return nil, missingDependencyError{[]string{moduleName}}
643 }
mrziwange6c85812024-05-22 14:36:09 -0700644 outputFiles, err := outputFilesForModule(ctx, module, tag)
645 if outputFiles != nil && err == nil {
646 return outputFiles, nil
Liz Kammera830f3a2020-11-10 10:50:34 -0800647 } else {
mrziwange6c85812024-05-22 14:36:09 -0700648 return nil, err
Liz Kammera830f3a2020-11-10 10:50:34 -0800649 }
650}
651
Paul Duffind5cf92e2021-07-09 17:38:55 +0100652// GetModuleFromPathDep will return the module that was added as a dependency automatically for
653// properties tagged with `android:"path"` or manually using ExtractSourceDeps or
654// ExtractSourcesDeps.
655//
656// The moduleName and tag supplied to this should be the values returned from SrcIsModuleWithTag.
657// Or, if no tag is expected then the moduleName should be the value returned by SrcIsModule and
658// the tag must be "".
659//
660// If tag is "" then the returned module will be the dependency that was added for ":moduleName".
661// Otherwise, it is the dependency that was added for ":moduleName{tag}".
Paul Duffind5cf92e2021-07-09 17:38:55 +0100662func GetModuleFromPathDep(ctx ModuleWithDepsPathContext, moduleName, tag string) blueprint.Module {
Paul Duffin40131a32021-07-09 17:10:35 +0100663 var found blueprint.Module
664 // The sourceOrOutputDepTag uniquely identifies the module dependency as it contains both the
665 // module name and the tag. Dependencies added automatically for properties tagged with
666 // `android:"path"` are deduped so are guaranteed to be unique. It is possible for duplicate
667 // dependencies to be added manually using ExtractSourcesDeps or ExtractSourceDeps but even then
668 // it will always be the case that the dependencies will be identical, i.e. the same tag and same
669 // moduleName referring to the same dependency module.
670 //
671 // It does not matter whether the moduleName is a fully qualified name or if the module
672 // dependency is a prebuilt module. All that matters is the same information is supplied to
673 // create the tag here as was supplied to create the tag when the dependency was added so that
674 // this finds the matching dependency module.
675 expectedTag := sourceOrOutputDepTag(moduleName, tag)
Colin Cross648daea2024-09-12 14:35:29 -0700676 ctx.VisitDirectDeps(func(module Module) {
Paul Duffin40131a32021-07-09 17:10:35 +0100677 depTag := ctx.OtherModuleDependencyTag(module)
678 if depTag == expectedTag {
679 found = module
680 }
681 })
682 return found
Paul Duffind5cf92e2021-07-09 17:38:55 +0100683}
684
Liz Kammer620dea62021-04-14 17:36:10 -0400685// PathsAndMissingDepsForModuleSrcExcludes returns a Paths{} containing the resolved references in
686// paths, minus those listed in excludes. Elements of paths and excludes are resolved as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700687// - filepath, relative to local module directory, resolves as a filepath relative to the local
688// source directory
689// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
690// source directory. Not valid in excludes.
691// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700692// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
693// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700694//
Liz Kammer620dea62021-04-14 17:36:10 -0400695// and a list of the module names of missing module dependencies are returned as the second return.
696// Properties passed as the paths argument must have been annotated with struct tag
Colin Cross41955e82019-05-29 14:40:35 -0700697// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000698// pathdeps mutator.
Liz Kammer619be462022-01-28 15:13:39 -0500699func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) (Paths, []string) {
700 return PathsAndMissingDepsRelativeToModuleSourceDir(SourceInput{
701 Context: ctx,
702 Paths: paths,
703 ExcludePaths: excludes,
704 IncludeDirs: true,
705 })
706}
707
708func PathsAndMissingDepsRelativeToModuleSourceDir(input SourceInput) (Paths, []string) {
709 prefix := pathForModuleSrc(input.Context).String()
Colin Cross8a497952019-03-05 22:25:09 -0800710
711 var expandedExcludes []string
Liz Kammer619be462022-01-28 15:13:39 -0500712 if input.ExcludePaths != nil {
713 expandedExcludes = make([]string, 0, len(input.ExcludePaths))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700714 }
Colin Cross8a497952019-03-05 22:25:09 -0800715
Colin Crossba71a3f2019-03-18 12:12:48 -0700716 var missingExcludeDeps []string
Liz Kammer619be462022-01-28 15:13:39 -0500717 for _, e := range input.ExcludePaths {
Colin Cross41955e82019-05-29 14:40:35 -0700718 if m, t := SrcIsModuleWithTag(e); m != "" {
Liz Kammer619be462022-01-28 15:13:39 -0500719 modulePaths, err := getPathsFromModuleDep(input.Context, e, m, t)
Liz Kammera830f3a2020-11-10 10:50:34 -0800720 if m, ok := err.(missingDependencyError); ok {
721 missingExcludeDeps = append(missingExcludeDeps, m.missingDeps...)
722 } else if err != nil {
Liz Kammer619be462022-01-28 15:13:39 -0500723 reportPathError(input.Context, err)
Colin Cross8a497952019-03-05 22:25:09 -0800724 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800725 expandedExcludes = append(expandedExcludes, modulePaths.Strings()...)
Colin Cross8a497952019-03-05 22:25:09 -0800726 }
727 } else {
728 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
729 }
730 }
731
Liz Kammer619be462022-01-28 15:13:39 -0500732 if input.Paths == nil {
Colin Crossba71a3f2019-03-18 12:12:48 -0700733 return nil, missingExcludeDeps
Colin Cross8a497952019-03-05 22:25:09 -0800734 }
735
Colin Crossba71a3f2019-03-18 12:12:48 -0700736 var missingDeps []string
737
Liz Kammer619be462022-01-28 15:13:39 -0500738 expandedSrcFiles := make(Paths, 0, len(input.Paths))
739 for _, s := range input.Paths {
740 srcFiles, err := expandOneSrcPath(sourcePathInput{
741 context: input.Context,
742 path: s,
743 expandedExcludes: expandedExcludes,
744 includeDirs: input.IncludeDirs,
745 })
Colin Cross8a497952019-03-05 22:25:09 -0800746 if depErr, ok := err.(missingDependencyError); ok {
Colin Crossba71a3f2019-03-18 12:12:48 -0700747 missingDeps = append(missingDeps, depErr.missingDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800748 } else if err != nil {
Liz Kammer619be462022-01-28 15:13:39 -0500749 reportPathError(input.Context, err)
Colin Cross8a497952019-03-05 22:25:09 -0800750 }
751 expandedSrcFiles = append(expandedSrcFiles, srcFiles...)
752 }
Colin Crossba71a3f2019-03-18 12:12:48 -0700753
Jihoon Kang0e3a5352024-04-12 00:45:50 +0000754 // TODO: b/334169722 - Replace with an error instead of implicitly removing duplicates.
755 return FirstUniquePaths(expandedSrcFiles), append(missingDeps, missingExcludeDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800756}
757
758type missingDependencyError struct {
759 missingDeps []string
760}
761
762func (e missingDependencyError) Error() string {
763 return "missing dependencies: " + strings.Join(e.missingDeps, ", ")
764}
765
Liz Kammer619be462022-01-28 15:13:39 -0500766type sourcePathInput struct {
767 context ModuleWithDepsPathContext
768 path string
769 expandedExcludes []string
770 includeDirs bool
771}
772
Liz Kammera830f3a2020-11-10 10:50:34 -0800773// Expands one path string to Paths rooted from the module's local source
774// directory, excluding those listed in the expandedExcludes.
775// Expands globs, references to SourceFileProducer or OutputFileProducer modules using the ":name" and ":name{.tag}" syntax.
Liz Kammer619be462022-01-28 15:13:39 -0500776func expandOneSrcPath(input sourcePathInput) (Paths, error) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900777 excludePaths := func(paths Paths) Paths {
Liz Kammer619be462022-01-28 15:13:39 -0500778 if len(input.expandedExcludes) == 0 {
Jooyung Han7607dd32020-07-05 10:23:14 +0900779 return paths
780 }
781 remainder := make(Paths, 0, len(paths))
782 for _, p := range paths {
Liz Kammer619be462022-01-28 15:13:39 -0500783 if !InList(p.String(), input.expandedExcludes) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900784 remainder = append(remainder, p)
785 }
786 }
787 return remainder
788 }
Liz Kammer619be462022-01-28 15:13:39 -0500789 if m, t := SrcIsModuleWithTag(input.path); m != "" {
790 modulePaths, err := getPathsFromModuleDep(input.context, input.path, m, t)
Liz Kammera830f3a2020-11-10 10:50:34 -0800791 if err != nil {
792 return nil, err
Colin Cross8a497952019-03-05 22:25:09 -0800793 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800794 return excludePaths(modulePaths), nil
Colin Cross8a497952019-03-05 22:25:09 -0800795 }
Colin Cross8a497952019-03-05 22:25:09 -0800796 } else {
Liz Kammer619be462022-01-28 15:13:39 -0500797 p := pathForModuleSrc(input.context, input.path)
798 if pathtools.IsGlob(input.path) {
799 paths := GlobFiles(input.context, p.String(), input.expandedExcludes)
800 return PathsWithModuleSrcSubDir(input.context, paths, ""), nil
801 } else {
802 if exists, _, err := input.context.Config().fs.Exists(p.String()); err != nil {
803 ReportPathErrorf(input.context, "%s: %s", p, err.Error())
804 } else if !exists && !input.context.Config().TestAllowNonExistentPaths {
805 ReportPathErrorf(input.context, "module source path %q does not exist", p)
806 } else if !input.includeDirs {
807 if isDir, err := input.context.Config().fs.IsDir(p.String()); exists && err != nil {
808 ReportPathErrorf(input.context, "%s: %s", p, err.Error())
809 } else if isDir {
810 ReportPathErrorf(input.context, "module source path %q is a directory", p)
811 }
812 }
Colin Cross8a497952019-03-05 22:25:09 -0800813
Liz Kammer619be462022-01-28 15:13:39 -0500814 if InList(p.String(), input.expandedExcludes) {
815 return nil, nil
816 }
817 return Paths{p}, nil
Colin Cross8a497952019-03-05 22:25:09 -0800818 }
Colin Cross8a497952019-03-05 22:25:09 -0800819 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700820}
821
822// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
823// source directory, but strip the local source directory from the beginning of
Dan Willemsen540a78c2018-02-26 21:50:08 -0800824// each string. If incDirs is false, strip paths with a trailing '/' from the list.
Colin Crossfe4bc362018-09-12 10:02:13 -0700825// It intended for use in globs that only list files that exist, so it allows '$' in
826// filenames.
Liz Kammera830f3a2020-11-10 10:50:34 -0800827func pathsForModuleSrcFromFullPath(ctx EarlyModulePathContext, paths []string, incDirs bool) Paths {
Lukacs T. Berkif7e36d82021-08-16 17:05:09 +0200828 prefix := ctx.ModuleDir() + "/"
Colin Cross0f37af02017-09-27 17:42:05 -0700829 if prefix == "./" {
830 prefix = ""
831 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700832 ret := make(Paths, 0, len(paths))
833 for _, p := range paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -0800834 if !incDirs && strings.HasSuffix(p, "/") {
835 continue
836 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700837 path := filepath.Clean(p)
838 if !strings.HasPrefix(path, prefix) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100839 ReportPathErrorf(ctx, "Path %q is not in module source directory %q", p, prefix)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700840 continue
841 }
Colin Crosse3924e12018-08-15 20:18:53 -0700842
Colin Crossfe4bc362018-09-12 10:02:13 -0700843 srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
Colin Crosse3924e12018-08-15 20:18:53 -0700844 if err != nil {
845 reportPathError(ctx, err)
846 continue
847 }
848
Colin Cross07e51612019-03-05 12:46:40 -0800849 srcPath.basePath.rel = srcPath.path
Colin Crosse3924e12018-08-15 20:18:53 -0700850
Colin Cross07e51612019-03-05 12:46:40 -0800851 ret = append(ret, srcPath)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700852 }
853 return ret
854}
855
Liz Kammera830f3a2020-11-10 10:50:34 -0800856// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's local source
857// directory. If input is nil, use the default if it exists. If input is empty, returns nil.
858func PathsWithOptionalDefaultForModuleSrc(ctx ModuleMissingDepsPathContext, input []string, def string) Paths {
Colin Cross0ddae7f2019-02-07 15:30:01 -0800859 if input != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700860 return PathsForModuleSrc(ctx, input)
861 }
862 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
863 // is created, we're run again.
Lukacs T. Berkif7e36d82021-08-16 17:05:09 +0200864 path := filepath.Join(ctx.ModuleDir(), def)
Liz Kammera830f3a2020-11-10 10:50:34 -0800865 return Glob(ctx, path, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700866}
867
868// Strings returns the Paths in string form
869func (p Paths) Strings() []string {
870 if p == nil {
871 return nil
872 }
873 ret := make([]string, len(p))
874 for i, path := range p {
875 ret[i] = path.String()
876 }
877 return ret
878}
879
Colin Crossc0efd1d2020-07-03 11:56:24 -0700880func CopyOfPaths(paths Paths) Paths {
881 return append(Paths(nil), paths...)
882}
883
Colin Crossb6715442017-10-24 11:13:31 -0700884// FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
885// modifies the Paths slice contents in place, and returns a subslice of the original slice.
Dan Willemsenfe92c962017-08-29 12:28:37 -0700886func FirstUniquePaths(list Paths) Paths {
Colin Cross27027c72020-02-28 15:34:17 -0800887 // 128 was chosen based on BenchmarkFirstUniquePaths results.
888 if len(list) > 128 {
889 return firstUniquePathsMap(list)
890 }
891 return firstUniquePathsList(list)
892}
893
Colin Crossc0efd1d2020-07-03 11:56:24 -0700894// SortedUniquePaths returns all unique elements of a Paths in sorted order. It modifies the
895// Paths slice contents in place, and returns a subslice of the original slice.
Jiyong Park33c77362020-05-29 22:00:16 +0900896func SortedUniquePaths(list Paths) Paths {
897 unique := FirstUniquePaths(list)
898 sort.Slice(unique, func(i, j int) bool {
899 return unique[i].String() < unique[j].String()
900 })
901 return unique
902}
903
Colin Cross27027c72020-02-28 15:34:17 -0800904func firstUniquePathsList(list Paths) Paths {
Dan Willemsenfe92c962017-08-29 12:28:37 -0700905 k := 0
906outer:
907 for i := 0; i < len(list); i++ {
908 for j := 0; j < k; j++ {
909 if list[i] == list[j] {
910 continue outer
911 }
912 }
913 list[k] = list[i]
914 k++
915 }
916 return list[:k]
917}
918
Colin Cross27027c72020-02-28 15:34:17 -0800919func firstUniquePathsMap(list Paths) Paths {
920 k := 0
921 seen := make(map[Path]bool, len(list))
922 for i := 0; i < len(list); i++ {
923 if seen[list[i]] {
924 continue
925 }
926 seen[list[i]] = true
927 list[k] = list[i]
928 k++
929 }
930 return list[:k]
931}
932
Colin Cross5d583952020-11-24 16:21:24 -0800933// FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each. It
934// modifies the InstallPaths slice contents in place, and returns a subslice of the original slice.
935func FirstUniqueInstallPaths(list InstallPaths) InstallPaths {
936 // 128 was chosen based on BenchmarkFirstUniquePaths results.
937 if len(list) > 128 {
938 return firstUniqueInstallPathsMap(list)
939 }
940 return firstUniqueInstallPathsList(list)
941}
942
943func firstUniqueInstallPathsList(list InstallPaths) InstallPaths {
944 k := 0
945outer:
946 for i := 0; i < len(list); i++ {
947 for j := 0; j < k; j++ {
948 if list[i] == list[j] {
949 continue outer
950 }
951 }
952 list[k] = list[i]
953 k++
954 }
955 return list[:k]
956}
957
958func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths {
959 k := 0
960 seen := make(map[InstallPath]bool, len(list))
961 for i := 0; i < len(list); i++ {
962 if seen[list[i]] {
963 continue
964 }
965 seen[list[i]] = true
966 list[k] = list[i]
967 k++
968 }
969 return list[:k]
970}
971
Colin Crossb6715442017-10-24 11:13:31 -0700972// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
973// modifies the Paths slice contents in place, and returns a subslice of the original slice.
974func LastUniquePaths(list Paths) Paths {
975 totalSkip := 0
976 for i := len(list) - 1; i >= totalSkip; i-- {
977 skip := 0
978 for j := i - 1; j >= totalSkip; j-- {
979 if list[i] == list[j] {
980 skip++
981 } else {
982 list[j+skip] = list[j]
983 }
984 }
985 totalSkip += skip
986 }
987 return list[totalSkip:]
988}
989
Colin Crossa140bb02018-04-17 10:52:26 -0700990// ReversePaths returns a copy of a Paths in reverse order.
991func ReversePaths(list Paths) Paths {
992 if list == nil {
993 return nil
994 }
995 ret := make(Paths, len(list))
996 for i := range list {
997 ret[i] = list[len(list)-1-i]
998 }
999 return ret
1000}
1001
Jeff Gaston294356f2017-09-27 17:05:30 -07001002func indexPathList(s Path, list []Path) int {
1003 for i, l := range list {
1004 if l == s {
1005 return i
1006 }
1007 }
1008
1009 return -1
1010}
1011
1012func inPathList(p Path, list []Path) bool {
1013 return indexPathList(p, list) != -1
1014}
1015
1016func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
Paul Duffin57b9e1d2019-12-13 00:03:35 +00001017 return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) })
1018}
1019
1020func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001021 for _, l := range list {
Paul Duffin57b9e1d2019-12-13 00:03:35 +00001022 if predicate(l) {
Jeff Gaston294356f2017-09-27 17:05:30 -07001023 filtered = append(filtered, l)
1024 } else {
1025 remainder = append(remainder, l)
1026 }
1027 }
1028
1029 return
1030}
1031
Colin Cross93e85952017-08-15 13:34:18 -07001032// HasExt returns true of any of the paths have extension ext, otherwise false
1033func (p Paths) HasExt(ext string) bool {
1034 for _, path := range p {
1035 if path.Ext() == ext {
1036 return true
1037 }
1038 }
1039
1040 return false
1041}
1042
1043// FilterByExt returns the subset of the paths that have extension ext
1044func (p Paths) FilterByExt(ext string) Paths {
1045 ret := make(Paths, 0, len(p))
1046 for _, path := range p {
1047 if path.Ext() == ext {
1048 ret = append(ret, path)
1049 }
1050 }
1051 return ret
1052}
1053
1054// FilterOutByExt returns the subset of the paths that do not have extension ext
1055func (p Paths) FilterOutByExt(ext string) Paths {
1056 ret := make(Paths, 0, len(p))
1057 for _, path := range p {
1058 if path.Ext() != ext {
1059 ret = append(ret, path)
1060 }
1061 }
1062 return ret
1063}
1064
Colin Cross5e6cfbe2017-11-03 15:20:35 -07001065// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
1066// (including subdirectories) are in a contiguous subslice of the list, and can be found in
1067// O(log(N)) time using a binary search on the directory prefix.
1068type DirectorySortedPaths Paths
1069
1070func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
1071 ret := append(DirectorySortedPaths(nil), paths...)
1072 sort.Slice(ret, func(i, j int) bool {
1073 return ret[i].String() < ret[j].String()
1074 })
1075 return ret
1076}
1077
1078// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
1079// that are in the specified directory and its subdirectories.
1080func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
1081 prefix := filepath.Clean(dir) + "/"
1082 start := sort.Search(len(p), func(i int) bool {
1083 return prefix < p[i].String()
1084 })
1085
1086 ret := p[start:]
1087
1088 end := sort.Search(len(ret), func(i int) bool {
1089 return !strings.HasPrefix(ret[i].String(), prefix)
1090 })
1091
1092 ret = ret[:end]
1093
1094 return Paths(ret)
1095}
1096
Alex Humesky29e3bbe2020-11-20 21:30:13 -05001097// WritablePaths is a slice of WritablePath, used for multiple outputs.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001098type WritablePaths []WritablePath
1099
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001100// RelativeToTop creates a new WritablePaths containing the result of calling Path.RelativeToTop on
1101// each item in this slice.
1102func (p WritablePaths) RelativeToTop() WritablePaths {
1103 ensureTestOnly()
1104 if p == nil {
1105 return p
1106 }
1107 ret := make(WritablePaths, len(p))
1108 for i, path := range p {
1109 ret[i] = path.RelativeToTop().(WritablePath)
1110 }
1111 return ret
1112}
1113
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001114// Strings returns the string forms of the writable paths.
1115func (p WritablePaths) Strings() []string {
1116 if p == nil {
1117 return nil
1118 }
1119 ret := make([]string, len(p))
1120 for i, path := range p {
1121 ret[i] = path.String()
1122 }
1123 return ret
1124}
1125
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001126// Paths returns the WritablePaths as a Paths
1127func (p WritablePaths) Paths() Paths {
1128 if p == nil {
1129 return nil
1130 }
1131 ret := make(Paths, len(p))
1132 for i, path := range p {
1133 ret[i] = path
1134 }
1135 return ret
1136}
1137
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001138type basePath struct {
Paul Duffin74abc5d2021-03-24 09:24:59 +00001139 path string
1140 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001141}
1142
Yu Liu467d7c52024-09-18 21:54:44 +00001143type basePathGob struct {
1144 Path string
1145 Rel string
1146}
Yu Liufa297642024-06-11 00:13:02 +00001147
Yu Liu467d7c52024-09-18 21:54:44 +00001148func (p *basePath) ToGob() *basePathGob {
1149 return &basePathGob{
1150 Path: p.path,
1151 Rel: p.rel,
1152 }
1153}
1154
1155func (p *basePath) FromGob(data *basePathGob) {
1156 p.path = data.Path
1157 p.rel = data.Rel
1158}
1159
1160func (p basePath) GobEncode() ([]byte, error) {
Yu Liu3cadf7d2024-10-24 18:47:06 +00001161 return gobtools.CustomGobEncode[basePathGob](&p)
Yu Liufa297642024-06-11 00:13:02 +00001162}
1163
1164func (p *basePath) GobDecode(data []byte) error {
Yu Liu3cadf7d2024-10-24 18:47:06 +00001165 return gobtools.CustomGobDecode[basePathGob](data, p)
Yu Liufa297642024-06-11 00:13:02 +00001166}
1167
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001168func (p basePath) Ext() string {
1169 return filepath.Ext(p.path)
1170}
1171
Colin Cross4f6fc9c2016-10-26 10:05:25 -07001172func (p basePath) Base() string {
1173 return filepath.Base(p.path)
1174}
1175
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001176func (p basePath) Rel() string {
1177 if p.rel != "" {
1178 return p.rel
1179 }
1180 return p.path
1181}
1182
Colin Cross0875c522017-11-28 17:34:01 -08001183func (p basePath) String() string {
1184 return p.path
1185}
1186
Colin Cross0db55682017-12-05 15:36:55 -08001187func (p basePath) withRel(rel string) basePath {
1188 p.path = filepath.Join(p.path, rel)
1189 p.rel = rel
1190 return p
1191}
1192
Colin Cross7707b242024-07-26 12:02:36 -07001193func (p basePath) withoutRel() basePath {
1194 p.rel = filepath.Base(p.path)
1195 return p
1196}
1197
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001198// SourcePath is a Path representing a file path rooted from SrcDir
1199type SourcePath struct {
1200 basePath
1201}
1202
1203var _ Path = SourcePath{}
1204
Colin Cross0db55682017-12-05 15:36:55 -08001205func (p SourcePath) withRel(rel string) SourcePath {
1206 p.basePath = p.basePath.withRel(rel)
1207 return p
1208}
1209
Colin Crossbd73d0d2024-07-26 12:00:33 -07001210func (p SourcePath) RelativeToTop() Path {
1211 ensureTestOnly()
1212 return p
1213}
1214
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001215// safePathForSource is for paths that we expect are safe -- only for use by go
1216// code that is embedding ninja variables in paths
Colin Crossfe4bc362018-09-12 10:02:13 -07001217func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
1218 p, err := validateSafePath(pathComponents...)
Cole Faust483d1f72023-01-09 14:35:27 -08001219 ret := SourcePath{basePath{p, ""}}
Colin Crossfe4bc362018-09-12 10:02:13 -07001220 if err != nil {
1221 return ret, err
1222 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001223
Colin Cross7b3dcc32019-01-24 13:14:39 -08001224 // absolute path already checked by validateSafePath
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001225 // special-case api surface gen files for now
1226 if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
Mikhail Naganovab1f5182019-02-08 13:17:55 -08001227 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -07001228 }
1229
Colin Crossfe4bc362018-09-12 10:02:13 -07001230 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001231}
1232
Colin Cross192e97a2018-02-22 14:21:02 -08001233// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
1234func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
Colin Crossc48c1432018-02-23 07:09:01 +00001235 p, err := validatePath(pathComponents...)
Cole Faust483d1f72023-01-09 14:35:27 -08001236 ret := SourcePath{basePath{p, ""}}
Colin Cross94a32102018-02-22 14:21:02 -08001237 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -08001238 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -08001239 }
1240
Colin Cross7b3dcc32019-01-24 13:14:39 -08001241 // absolute path already checked by validatePath
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001242 // special-case for now
1243 if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
Mikhail Naganovab1f5182019-02-08 13:17:55 -08001244 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Crossc48c1432018-02-23 07:09:01 +00001245 }
1246
Colin Cross192e97a2018-02-22 14:21:02 -08001247 return ret, nil
1248}
1249
1250// existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
1251// path does not exist.
Colin Cross662d6142022-11-03 20:38:01 -07001252func existsWithDependencies(ctx PathGlobContext, path SourcePath) (exists bool, err error) {
Colin Cross192e97a2018-02-22 14:21:02 -08001253 var files []string
1254
Colin Cross662d6142022-11-03 20:38:01 -07001255 // Use glob to produce proper dependencies, even though we only want
1256 // a single file.
1257 files, err = ctx.GlobWithDeps(path.String(), nil)
Colin Cross192e97a2018-02-22 14:21:02 -08001258
1259 if err != nil {
1260 return false, fmt.Errorf("glob: %s", err.Error())
1261 }
1262
1263 return len(files) > 0, nil
1264}
1265
1266// PathForSource joins the provided path components and validates that the result
1267// neither escapes the source dir nor is in the out dir.
1268// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
1269func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
1270 path, err := pathForSource(ctx, pathComponents...)
1271 if err != nil {
1272 reportPathError(ctx, err)
1273 }
1274
Colin Crosse3924e12018-08-15 20:18:53 -07001275 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001276 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -07001277 }
1278
Liz Kammera830f3a2020-11-10 10:50:34 -08001279 if modCtx, ok := ctx.(ModuleMissingDepsPathContext); ok && ctx.Config().AllowMissingDependencies() {
Colin Cross662d6142022-11-03 20:38:01 -07001280 exists, err := existsWithDependencies(modCtx, path)
Colin Cross192e97a2018-02-22 14:21:02 -08001281 if err != nil {
1282 reportPathError(ctx, err)
1283 }
1284 if !exists {
1285 modCtx.AddMissingDependencies([]string{path.String()})
1286 }
Colin Cross988414c2020-01-11 01:11:46 +00001287 } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001288 ReportPathErrorf(ctx, "%s: %s", path, err.Error())
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001289 } else if !exists && !ctx.Config().TestAllowNonExistentPaths {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001290 ReportPathErrorf(ctx, "source path %q does not exist", path)
Colin Cross192e97a2018-02-22 14:21:02 -08001291 }
1292 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001293}
1294
Cole Faustbc65a3f2023-08-01 16:38:55 +00001295// PathForArbitraryOutput creates a path for the given components. Unlike PathForOutput,
1296// the path is relative to the root of the output folder, not the out/soong folder.
1297func PathForArbitraryOutput(ctx PathContext, pathComponents ...string) Path {
Colin Cross3b1c6842024-07-26 11:52:57 -07001298 path, err := validatePath(pathComponents...)
Cole Faustbc65a3f2023-08-01 16:38:55 +00001299 if err != nil {
1300 reportPathError(ctx, err)
1301 }
Colin Cross3b1c6842024-07-26 11:52:57 -07001302 fullPath := filepath.Join(ctx.Config().OutDir(), path)
1303 path = fullPath[len(fullPath)-len(path):]
1304 return OutputPath{basePath{path, ""}, ctx.Config().OutDir(), fullPath}
Cole Faustbc65a3f2023-08-01 16:38:55 +00001305}
1306
Spandan Dasc6c10fa2022-10-21 21:52:13 +00001307// MaybeExistentPathForSource joins the provided path components and validates that the result
1308// neither escapes the source dir nor is in the out dir.
1309// It does not validate whether the path exists.
1310func MaybeExistentPathForSource(ctx PathContext, pathComponents ...string) SourcePath {
1311 path, err := pathForSource(ctx, pathComponents...)
1312 if err != nil {
1313 reportPathError(ctx, err)
1314 }
1315
1316 if pathtools.IsGlob(path.String()) {
1317 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
1318 }
1319 return path
1320}
1321
Liz Kammer7aa52882021-02-11 09:16:14 -05001322// ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not*
1323// rooted from the module's local source directory, if the path exists, or an empty OptionalPath if
1324// it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state
1325// of the path changes.
Colin Cross662d6142022-11-03 20:38:01 -07001326func ExistentPathForSource(ctx PathGlobContext, pathComponents ...string) OptionalPath {
Colin Cross192e97a2018-02-22 14:21:02 -08001327 path, err := pathForSource(ctx, pathComponents...)
Colin Cross1ccfcc32018-02-22 13:54:26 -08001328 if err != nil {
1329 reportPathError(ctx, err)
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001330 // No need to put the error message into the returned path since it has been reported already.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001331 return OptionalPath{}
1332 }
Colin Crossc48c1432018-02-23 07:09:01 +00001333
Colin Crosse3924e12018-08-15 20:18:53 -07001334 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001335 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -07001336 return OptionalPath{}
1337 }
1338
Colin Cross192e97a2018-02-22 14:21:02 -08001339 exists, err := existsWithDependencies(ctx, path)
Colin Crossc48c1432018-02-23 07:09:01 +00001340 if err != nil {
1341 reportPathError(ctx, err)
1342 return OptionalPath{}
1343 }
Colin Cross192e97a2018-02-22 14:21:02 -08001344 if !exists {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001345 return InvalidOptionalPath(path.String() + " does not exist")
Colin Crossc48c1432018-02-23 07:09:01 +00001346 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001347 return OptionalPathForPath(path)
1348}
1349
1350func (p SourcePath) String() string {
Cole Faust483d1f72023-01-09 14:35:27 -08001351 if p.path == "" {
1352 return "."
1353 }
1354 return p.path
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001355}
1356
Colin Cross7707b242024-07-26 12:02:36 -07001357func (p SourcePath) WithoutRel() Path {
1358 p.basePath = p.basePath.withoutRel()
1359 return p
1360}
1361
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001362// Join creates a new SourcePath with paths... joined with the current path. The
1363// provided paths... may not use '..' to escape from the current path.
1364func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001365 path, err := validatePath(paths...)
1366 if err != nil {
1367 reportPathError(ctx, err)
1368 }
Colin Cross0db55682017-12-05 15:36:55 -08001369 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001370}
1371
Colin Cross2fafa3e2019-03-05 12:39:51 -08001372// join is like Join but does less path validation.
1373func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath {
1374 path, err := validateSafePath(paths...)
1375 if err != nil {
1376 reportPathError(ctx, err)
1377 }
1378 return p.withRel(path)
1379}
1380
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001381// OverlayPath returns the overlay for `path' if it exists. This assumes that the
1382// SourcePath is the path to a resource overlay directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001383func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001384 var relDir string
Colin Cross07e51612019-03-05 12:46:40 -08001385 if srcPath, ok := path.(SourcePath); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001386 relDir = srcPath.path
1387 } else {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001388 ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001389 // No need to put the error message into the returned path since it has been reported already.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001390 return OptionalPath{}
1391 }
Cole Faust483d1f72023-01-09 14:35:27 -08001392 dir := filepath.Join(p.path, relDir)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001393 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -07001394 if pathtools.IsGlob(dir) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001395 ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
Dan Willemsen7b310ee2015-12-18 15:11:17 -08001396 }
Colin Cross461b4452018-02-23 09:22:42 -08001397 paths, err := ctx.GlobWithDeps(dir, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001398 if err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001399 ReportPathErrorf(ctx, "glob: %s", err.Error())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001400 return OptionalPath{}
1401 }
1402 if len(paths) == 0 {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001403 return InvalidOptionalPath(dir + " does not exist")
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001404 }
Cole Faust483d1f72023-01-09 14:35:27 -08001405 return OptionalPathForPath(PathForSource(ctx, paths[0]))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001406}
1407
Colin Cross70dda7e2019-10-01 22:05:35 -07001408// OutputPath is a Path representing an intermediates file path rooted from the build directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001409type OutputPath struct {
1410 basePath
Paul Duffind65c58b2021-03-24 09:22:07 +00001411
Colin Cross3b1c6842024-07-26 11:52:57 -07001412 // The base out directory for this path, either Config.SoongOutDir() or Config.OutDir()
1413 outDir string
Paul Duffind65c58b2021-03-24 09:22:07 +00001414
Colin Crossd63c9a72020-01-29 16:52:50 -08001415 fullPath string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001416}
1417
Yu Liu467d7c52024-09-18 21:54:44 +00001418type outputPathGob struct {
Yu Liu5246a7e2024-10-09 20:04:52 +00001419 BasePath basePath
Yu Liu467d7c52024-09-18 21:54:44 +00001420 OutDir string
1421 FullPath string
1422}
Yu Liufa297642024-06-11 00:13:02 +00001423
Yu Liu467d7c52024-09-18 21:54:44 +00001424func (p *OutputPath) ToGob() *outputPathGob {
1425 return &outputPathGob{
Yu Liu5246a7e2024-10-09 20:04:52 +00001426 BasePath: p.basePath,
Yu Liu467d7c52024-09-18 21:54:44 +00001427 OutDir: p.outDir,
1428 FullPath: p.fullPath,
1429 }
1430}
1431
1432func (p *OutputPath) FromGob(data *outputPathGob) {
Yu Liu5246a7e2024-10-09 20:04:52 +00001433 p.basePath = data.BasePath
Yu Liu467d7c52024-09-18 21:54:44 +00001434 p.outDir = data.OutDir
1435 p.fullPath = data.FullPath
1436}
1437
1438func (p OutputPath) GobEncode() ([]byte, error) {
Yu Liu3cadf7d2024-10-24 18:47:06 +00001439 return gobtools.CustomGobEncode[outputPathGob](&p)
Yu Liufa297642024-06-11 00:13:02 +00001440}
1441
1442func (p *OutputPath) GobDecode(data []byte) error {
Yu Liu3cadf7d2024-10-24 18:47:06 +00001443 return gobtools.CustomGobDecode[outputPathGob](data, p)
Yu Liufa297642024-06-11 00:13:02 +00001444}
1445
Colin Cross702e0f82017-10-18 17:27:54 -07001446func (p OutputPath) withRel(rel string) OutputPath {
Colin Cross0db55682017-12-05 15:36:55 -08001447 p.basePath = p.basePath.withRel(rel)
Colin Crossd63c9a72020-01-29 16:52:50 -08001448 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross702e0f82017-10-18 17:27:54 -07001449 return p
1450}
1451
Colin Cross7707b242024-07-26 12:02:36 -07001452func (p OutputPath) WithoutRel() Path {
1453 p.basePath = p.basePath.withoutRel()
Colin Cross3063b782018-08-15 11:19:12 -07001454 return p
1455}
1456
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001457func (p OutputPath) getSoongOutDir() string {
Colin Cross3b1c6842024-07-26 11:52:57 -07001458 return p.outDir
Paul Duffin9b478b02019-12-10 13:41:51 +00001459}
1460
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001461func (p OutputPath) RelativeToTop() Path {
1462 return p.outputPathRelativeToTop()
1463}
1464
1465func (p OutputPath) outputPathRelativeToTop() OutputPath {
Colin Cross3b1c6842024-07-26 11:52:57 -07001466 p.fullPath = StringPathRelativeToTop(p.outDir, p.fullPath)
1467 if strings.HasSuffix(p.outDir, testOutSoongSubDir) {
1468 p.outDir = TestOutSoongDir
1469 } else {
1470 // Handle the PathForArbitraryOutput case
1471 p.outDir = testOutDir
1472 }
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001473 return p
1474}
1475
Paul Duffin0267d492021-02-02 10:05:52 +00001476func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
1477 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1478}
1479
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001480var _ Path = OutputPath{}
Paul Duffin9b478b02019-12-10 13:41:51 +00001481var _ WritablePath = OutputPath{}
Paul Duffin0267d492021-02-02 10:05:52 +00001482var _ objPathProvider = OutputPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001483
Chris Parsons8f232a22020-06-23 17:37:05 -04001484// toolDepPath is a Path representing a dependency of the build tool.
1485type toolDepPath struct {
1486 basePath
1487}
1488
Colin Cross7707b242024-07-26 12:02:36 -07001489func (t toolDepPath) WithoutRel() Path {
1490 t.basePath = t.basePath.withoutRel()
1491 return t
1492}
1493
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001494func (t toolDepPath) RelativeToTop() Path {
1495 ensureTestOnly()
1496 return t
1497}
1498
Chris Parsons8f232a22020-06-23 17:37:05 -04001499var _ Path = toolDepPath{}
1500
1501// pathForBuildToolDep returns a toolDepPath representing the given path string.
1502// There is no validation for the path, as it is "trusted": It may fail
1503// normal validation checks. For example, it may be an absolute path.
1504// Only use this function to construct paths for dependencies of the build
1505// tool invocation.
1506func pathForBuildToolDep(ctx PathContext, path string) toolDepPath {
Paul Duffin74abc5d2021-03-24 09:24:59 +00001507 return toolDepPath{basePath{path, ""}}
Chris Parsons8f232a22020-06-23 17:37:05 -04001508}
1509
Jeff Gaston734e3802017-04-10 15:47:24 -07001510// PathForOutput joins the provided paths and returns an OutputPath that is
1511// validated to not escape the build dir.
1512// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
1513func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001514 path, err := validatePath(pathComponents...)
1515 if err != nil {
1516 reportPathError(ctx, err)
1517 }
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001518 fullPath := filepath.Join(ctx.Config().soongOutDir, path)
Colin Crossd63c9a72020-01-29 16:52:50 -08001519 path = fullPath[len(fullPath)-len(path):]
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001520 return OutputPath{basePath{path, ""}, ctx.Config().soongOutDir, fullPath}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001521}
1522
Colin Cross3b1c6842024-07-26 11:52:57 -07001523// PathsForOutput returns Paths rooted from outDir
Colin Cross40e33732019-02-15 11:08:35 -08001524func PathsForOutput(ctx PathContext, paths []string) WritablePaths {
1525 ret := make(WritablePaths, len(paths))
1526 for i, path := range paths {
1527 ret[i] = PathForOutput(ctx, path)
1528 }
1529 return ret
1530}
1531
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001532func (p OutputPath) writablePath() {}
1533
1534func (p OutputPath) String() string {
Colin Crossd63c9a72020-01-29 16:52:50 -08001535 return p.fullPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001536}
1537
1538// Join creates a new OutputPath with paths... joined with the current path. The
1539// provided paths... may not use '..' to escape from the current path.
1540func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001541 path, err := validatePath(paths...)
1542 if err != nil {
1543 reportPathError(ctx, err)
1544 }
Colin Cross0db55682017-12-05 15:36:55 -08001545 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001546}
1547
Colin Cross8854a5a2019-02-11 14:14:16 -08001548// ReplaceExtension creates a new OutputPath with the extension replaced with ext.
1549func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1550 if strings.Contains(ext, "/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001551 ReportPathErrorf(ctx, "extension %q cannot contain /", ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001552 }
1553 ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext))
Colin Cross2cdd5df2019-02-25 10:25:24 -08001554 ret.rel = pathtools.ReplaceExtension(p.rel, ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001555 return ret
1556}
1557
Colin Cross40e33732019-02-15 11:08:35 -08001558// InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths.
1559func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath {
1560 path, err := validatePath(paths...)
1561 if err != nil {
1562 reportPathError(ctx, err)
1563 }
1564
1565 ret := PathForOutput(ctx, filepath.Dir(p.path), path)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001566 ret.rel = filepath.Join(filepath.Dir(p.rel), path)
Colin Cross40e33732019-02-15 11:08:35 -08001567 return ret
1568}
1569
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001570// PathForIntermediates returns an OutputPath representing the top-level
1571// intermediates directory.
1572func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001573 path, err := validatePath(paths...)
1574 if err != nil {
1575 reportPathError(ctx, err)
1576 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001577 return PathForOutput(ctx, ".intermediates", path)
1578}
1579
Colin Cross07e51612019-03-05 12:46:40 -08001580var _ genPathProvider = SourcePath{}
1581var _ objPathProvider = SourcePath{}
1582var _ resPathProvider = SourcePath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001583
Colin Cross07e51612019-03-05 12:46:40 -08001584// PathForModuleSrc returns a Path representing the paths... under the
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001585// module's local source directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001586func PathForModuleSrc(ctx ModuleMissingDepsPathContext, pathComponents ...string) Path {
Paul Duffin407501b2021-07-09 16:56:35 +01001587 // Just join the components textually just to make sure that it does not corrupt a fully qualified
1588 // module reference, e.g. if the pathComponents is "://other:foo" then using filepath.Join() or
1589 // validatePath() will corrupt it, e.g. replace "//" with "/". If the path is not a module
1590 // reference then it will be validated by expandOneSrcPath anyway when it calls expandOneSrcPath.
1591 p := strings.Join(pathComponents, string(filepath.Separator))
Liz Kammer619be462022-01-28 15:13:39 -05001592 paths, err := expandOneSrcPath(sourcePathInput{context: ctx, path: p, includeDirs: true})
Colin Cross8a497952019-03-05 22:25:09 -08001593 if err != nil {
1594 if depErr, ok := err.(missingDependencyError); ok {
1595 if ctx.Config().AllowMissingDependencies() {
1596 ctx.AddMissingDependencies(depErr.missingDeps)
1597 } else {
1598 ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error())
1599 }
1600 } else {
1601 reportPathError(ctx, err)
1602 }
1603 return nil
1604 } else if len(paths) == 0 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001605 ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p)
Colin Cross8a497952019-03-05 22:25:09 -08001606 return nil
1607 } else if len(paths) > 1 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001608 ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths))
Colin Cross8a497952019-03-05 22:25:09 -08001609 }
1610 return paths[0]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001611}
1612
Liz Kammera830f3a2020-11-10 10:50:34 -08001613func pathForModuleSrc(ctx EarlyModulePathContext, paths ...string) SourcePath {
Colin Cross07e51612019-03-05 12:46:40 -08001614 p, err := validatePath(paths...)
1615 if err != nil {
1616 reportPathError(ctx, err)
1617 }
1618
1619 path, err := pathForSource(ctx, ctx.ModuleDir(), p)
1620 if err != nil {
1621 reportPathError(ctx, err)
1622 }
1623
1624 path.basePath.rel = p
1625
1626 return path
1627}
1628
Colin Cross2fafa3e2019-03-05 12:39:51 -08001629// PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path
1630// will return the path relative to subDir in the module's source directory. If any input paths are not located
1631// inside subDir then a path error will be reported.
Liz Kammera830f3a2020-11-10 10:50:34 -08001632func PathsWithModuleSrcSubDir(ctx EarlyModulePathContext, paths Paths, subDir string) Paths {
Colin Cross2fafa3e2019-03-05 12:39:51 -08001633 paths = append(Paths(nil), paths...)
Colin Cross07e51612019-03-05 12:46:40 -08001634 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001635 for i, path := range paths {
1636 rel := Rel(ctx, subDirFullPath.String(), path.String())
1637 paths[i] = subDirFullPath.join(ctx, rel)
1638 }
1639 return paths
1640}
1641
1642// PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the
1643// 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 -08001644func PathWithModuleSrcSubDir(ctx EarlyModulePathContext, path Path, subDir string) Path {
Colin Cross07e51612019-03-05 12:46:40 -08001645 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001646 rel := Rel(ctx, subDirFullPath.String(), path.String())
1647 return subDirFullPath.Join(ctx, rel)
1648}
1649
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001650// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
1651// valid path if p is non-nil.
Liz Kammera830f3a2020-11-10 10:50:34 -08001652func OptionalPathForModuleSrc(ctx ModuleMissingDepsPathContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001653 if p == nil {
1654 return OptionalPath{}
1655 }
1656 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
1657}
1658
Liz Kammera830f3a2020-11-10 10:50:34 -08001659func (p SourcePath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001660 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001661}
1662
yangbill6d032dd2024-04-18 03:05:49 +00001663func (p SourcePath) genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath {
1664 // If Trim_extension being set, force append Output_extension without replace original extension.
1665 if trimExt != "" {
1666 if ext != "" {
1667 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt)+"."+ext)
1668 }
1669 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt))
1670 }
1671 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1672}
1673
Liz Kammera830f3a2020-11-10 10:50:34 -08001674func (p SourcePath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001675 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001676}
1677
Liz Kammera830f3a2020-11-10 10:50:34 -08001678func (p SourcePath) resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001679 // TODO: Use full directory if the new ctx is not the current ctx?
1680 return PathForModuleRes(ctx, p.path, name)
1681}
1682
1683// ModuleOutPath is a Path representing a module's output directory.
1684type ModuleOutPath struct {
1685 OutputPath
1686}
1687
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001688func (p ModuleOutPath) RelativeToTop() Path {
1689 p.OutputPath = p.outputPathRelativeToTop()
1690 return p
1691}
1692
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001693var _ Path = ModuleOutPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001694var _ WritablePath = ModuleOutPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001695
Liz Kammera830f3a2020-11-10 10:50:34 -08001696func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01001697 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1698}
1699
Liz Kammera830f3a2020-11-10 10:50:34 -08001700// ModuleOutPathContext Subset of ModuleContext functions necessary for output path methods.
1701type ModuleOutPathContext interface {
1702 PathContext
1703
1704 ModuleName() string
1705 ModuleDir() string
1706 ModuleSubDir() string
1707}
1708
1709func pathForModuleOut(ctx ModuleOutPathContext) OutputPath {
Inseob Kimb7e9f5f2024-06-25 17:39:52 +09001710 return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
Colin Cross702e0f82017-10-18 17:27:54 -07001711}
1712
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001713// PathForModuleOut returns a Path representing the paths... under the module's
1714// output directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001715func PathForModuleOut(ctx ModuleOutPathContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001716 p, err := validatePath(paths...)
1717 if err != nil {
1718 reportPathError(ctx, err)
1719 }
Colin Cross702e0f82017-10-18 17:27:54 -07001720 return ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001721 OutputPath: pathForModuleOut(ctx).withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001722 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001723}
1724
1725// ModuleGenPath is a Path representing the 'gen' directory in a module's output
1726// directory. Mainly used for generated sources.
1727type ModuleGenPath struct {
1728 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001729}
1730
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001731func (p ModuleGenPath) RelativeToTop() Path {
1732 p.OutputPath = p.outputPathRelativeToTop()
1733 return p
1734}
1735
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001736var _ Path = ModuleGenPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001737var _ WritablePath = ModuleGenPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001738var _ genPathProvider = ModuleGenPath{}
1739var _ objPathProvider = ModuleGenPath{}
1740
1741// PathForModuleGen returns a Path representing the paths... under the module's
1742// `gen' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001743func PathForModuleGen(ctx ModuleOutPathContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001744 p, err := validatePath(paths...)
1745 if err != nil {
1746 reportPathError(ctx, err)
1747 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001748 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -07001749 ModuleOutPath: ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001750 OutputPath: pathForModuleOut(ctx).withRel("gen").withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001751 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001752 }
1753}
1754
Liz Kammera830f3a2020-11-10 10:50:34 -08001755func (p ModuleGenPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001756 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -07001757 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001758}
1759
yangbill6d032dd2024-04-18 03:05:49 +00001760func (p ModuleGenPath) genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath {
1761 // If Trim_extension being set, force append Output_extension without replace original extension.
1762 if trimExt != "" {
1763 if ext != "" {
1764 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt)+"."+ext)
1765 }
1766 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt))
1767 }
1768 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1769}
1770
Liz Kammera830f3a2020-11-10 10:50:34 -08001771func (p ModuleGenPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001772 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1773}
1774
1775// ModuleObjPath is a Path representing the 'obj' directory in a module's output
1776// directory. Used for compiled objects.
1777type ModuleObjPath struct {
1778 ModuleOutPath
1779}
1780
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001781func (p ModuleObjPath) RelativeToTop() Path {
1782 p.OutputPath = p.outputPathRelativeToTop()
1783 return p
1784}
1785
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001786var _ Path = ModuleObjPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001787var _ WritablePath = ModuleObjPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001788
1789// PathForModuleObj returns a Path representing the paths... under the module's
1790// 'obj' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001791func PathForModuleObj(ctx ModuleOutPathContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001792 p, err := validatePath(pathComponents...)
1793 if err != nil {
1794 reportPathError(ctx, err)
1795 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001796 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
1797}
1798
1799// ModuleResPath is a a Path representing the 'res' directory in a module's
1800// output directory.
1801type ModuleResPath struct {
1802 ModuleOutPath
1803}
1804
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001805func (p ModuleResPath) RelativeToTop() Path {
1806 p.OutputPath = p.outputPathRelativeToTop()
1807 return p
1808}
1809
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001810var _ Path = ModuleResPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001811var _ WritablePath = ModuleResPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001812
1813// PathForModuleRes returns a Path representing the paths... under the module's
1814// 'res' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001815func PathForModuleRes(ctx ModuleOutPathContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001816 p, err := validatePath(pathComponents...)
1817 if err != nil {
1818 reportPathError(ctx, err)
1819 }
1820
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001821 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
1822}
1823
Colin Cross70dda7e2019-10-01 22:05:35 -07001824// InstallPath is a Path representing a installed file path rooted from the build directory
1825type InstallPath struct {
1826 basePath
Colin Crossff6c33d2019-10-02 16:01:35 -07001827
Lukacs T. Berkib078ade2021-08-31 10:42:08 +02001828 // The soong build directory, i.e. Config.SoongOutDir()
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001829 soongOutDir string
Paul Duffind65c58b2021-03-24 09:22:07 +00001830
Jiyong Park957bcd92020-10-20 18:23:33 +09001831 // partitionDir is the part of the InstallPath that is automatically determined according to the context.
1832 // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules.
1833 partitionDir string
1834
Colin Crossb1692a32021-10-25 15:39:01 -07001835 partition string
1836
Jiyong Park957bcd92020-10-20 18:23:33 +09001837 // makePath indicates whether this path is for Soong (false) or Make (true).
1838 makePath bool
Colin Crossc0e42d52024-02-01 16:42:36 -08001839
1840 fullPath string
Colin Cross70dda7e2019-10-01 22:05:35 -07001841}
1842
Yu Liu467d7c52024-09-18 21:54:44 +00001843type installPathGob struct {
Yu Liu5246a7e2024-10-09 20:04:52 +00001844 BasePath basePath
Yu Liu467d7c52024-09-18 21:54:44 +00001845 SoongOutDir string
1846 PartitionDir string
1847 Partition string
1848 MakePath bool
1849 FullPath string
1850}
Yu Liu26a716d2024-08-30 23:40:32 +00001851
Yu Liu467d7c52024-09-18 21:54:44 +00001852func (p *InstallPath) ToGob() *installPathGob {
1853 return &installPathGob{
Yu Liu5246a7e2024-10-09 20:04:52 +00001854 BasePath: p.basePath,
Yu Liu467d7c52024-09-18 21:54:44 +00001855 SoongOutDir: p.soongOutDir,
1856 PartitionDir: p.partitionDir,
1857 Partition: p.partition,
1858 MakePath: p.makePath,
1859 FullPath: p.fullPath,
1860 }
1861}
1862
1863func (p *InstallPath) FromGob(data *installPathGob) {
Yu Liu5246a7e2024-10-09 20:04:52 +00001864 p.basePath = data.BasePath
Yu Liu467d7c52024-09-18 21:54:44 +00001865 p.soongOutDir = data.SoongOutDir
1866 p.partitionDir = data.PartitionDir
1867 p.partition = data.Partition
1868 p.makePath = data.MakePath
1869 p.fullPath = data.FullPath
1870}
1871
1872func (p InstallPath) GobEncode() ([]byte, error) {
Yu Liu3cadf7d2024-10-24 18:47:06 +00001873 return gobtools.CustomGobEncode[installPathGob](&p)
Yu Liu26a716d2024-08-30 23:40:32 +00001874}
1875
1876func (p *InstallPath) GobDecode(data []byte) error {
Yu Liu3cadf7d2024-10-24 18:47:06 +00001877 return gobtools.CustomGobDecode[installPathGob](data, p)
Yu Liu26a716d2024-08-30 23:40:32 +00001878}
1879
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001880// Will panic if called from outside a test environment.
1881func ensureTestOnly() {
Martin Stjernholm32312eb2021-03-27 18:54:49 +00001882 if PrefixInList(os.Args, "-test.") {
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001883 return
1884 }
Martin Stjernholm32312eb2021-03-27 18:54:49 +00001885 panic(fmt.Errorf("Not in test. Command line:\n %s", strings.Join(os.Args, "\n ")))
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001886}
1887
1888func (p InstallPath) RelativeToTop() Path {
1889 ensureTestOnly()
Colin Crossc0e42d52024-02-01 16:42:36 -08001890 if p.makePath {
Colin Cross3b1c6842024-07-26 11:52:57 -07001891 p.soongOutDir = testOutDir
Colin Crossc0e42d52024-02-01 16:42:36 -08001892 } else {
Colin Cross3b1c6842024-07-26 11:52:57 -07001893 p.soongOutDir = TestOutSoongDir
Colin Crossc0e42d52024-02-01 16:42:36 -08001894 }
1895 p.fullPath = filepath.Join(p.soongOutDir, p.path)
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001896 return p
1897}
1898
Colin Cross7707b242024-07-26 12:02:36 -07001899func (p InstallPath) WithoutRel() Path {
1900 p.basePath = p.basePath.withoutRel()
1901 return p
1902}
1903
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001904func (p InstallPath) getSoongOutDir() string {
1905 return p.soongOutDir
Paul Duffin9b478b02019-12-10 13:41:51 +00001906}
1907
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001908func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1909 panic("Not implemented")
1910}
1911
Paul Duffin9b478b02019-12-10 13:41:51 +00001912var _ Path = InstallPath{}
1913var _ WritablePath = InstallPath{}
1914
Colin Cross70dda7e2019-10-01 22:05:35 -07001915func (p InstallPath) writablePath() {}
1916
1917func (p InstallPath) String() string {
Colin Crossc0e42d52024-02-01 16:42:36 -08001918 return p.fullPath
Jiyong Park957bcd92020-10-20 18:23:33 +09001919}
1920
1921// PartitionDir returns the path to the partition where the install path is rooted at. It is
1922// out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules.
1923// The ./soong is dropped if the install path is for Make.
1924func (p InstallPath) PartitionDir() string {
1925 if p.makePath {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001926 return filepath.Join(p.soongOutDir, "../", p.partitionDir)
Jiyong Park957bcd92020-10-20 18:23:33 +09001927 } else {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001928 return filepath.Join(p.soongOutDir, p.partitionDir)
Jiyong Park957bcd92020-10-20 18:23:33 +09001929 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001930}
1931
Jihoon Kangf78a8902022-09-01 22:47:07 +00001932func (p InstallPath) Partition() string {
1933 return p.partition
1934}
1935
Colin Cross70dda7e2019-10-01 22:05:35 -07001936// Join creates a new InstallPath with paths... joined with the current path. The
1937// provided paths... may not use '..' to escape from the current path.
1938func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath {
1939 path, err := validatePath(paths...)
1940 if err != nil {
1941 reportPathError(ctx, err)
1942 }
1943 return p.withRel(path)
1944}
1945
1946func (p InstallPath) withRel(rel string) InstallPath {
1947 p.basePath = p.basePath.withRel(rel)
Colin Crossc0e42d52024-02-01 16:42:36 -08001948 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross70dda7e2019-10-01 22:05:35 -07001949 return p
1950}
1951
Colin Crossc68db4b2021-11-11 18:59:15 -08001952// Deprecated: ToMakePath is a noop, PathForModuleInstall always returns Make paths when building
1953// embedded in Make.
Colin Crossff6c33d2019-10-02 16:01:35 -07001954func (p InstallPath) ToMakePath() InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001955 p.makePath = true
Colin Crossff6c33d2019-10-02 16:01:35 -07001956 return p
Colin Cross70dda7e2019-10-01 22:05:35 -07001957}
1958
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001959// PathForModuleInstall returns a Path representing the install path for the
1960// module appended with paths...
Colin Cross70dda7e2019-10-01 22:05:35 -07001961func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Spandan Das5d1b9292021-06-03 19:36:41 +00001962 os, arch := osAndArch(ctx)
Cole Faust11edf552023-10-13 11:32:14 -07001963 partition := modulePartition(ctx, os.Class == Device)
Cole Faust3b703f32023-10-16 13:30:51 -07001964 return pathForInstall(ctx, os, arch, partition, pathComponents...)
Spandan Das5d1b9292021-06-03 19:36:41 +00001965}
1966
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001967// PathForHostDexInstall returns an InstallPath representing the install path for the
1968// module appended with paths...
1969func PathForHostDexInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Cole Faust3b703f32023-10-16 13:30:51 -07001970 return pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "", pathComponents...)
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001971}
1972
Spandan Das5d1b9292021-06-03 19:36:41 +00001973// PathForModuleInPartitionInstall is similar to PathForModuleInstall but partition is provided by the caller
1974func PathForModuleInPartitionInstall(ctx ModuleInstallPathContext, partition string, pathComponents ...string) InstallPath {
1975 os, arch := osAndArch(ctx)
Cole Faust3b703f32023-10-16 13:30:51 -07001976 return pathForInstall(ctx, os, arch, partition, pathComponents...)
Spandan Das5d1b9292021-06-03 19:36:41 +00001977}
1978
1979func osAndArch(ctx ModuleInstallPathContext) (OsType, ArchType) {
Colin Cross6e359402020-02-10 15:29:54 -08001980 os := ctx.Os()
Jiyong Park87788b52020-09-01 12:37:45 +09001981 arch := ctx.Arch().ArchType
1982 forceOS, forceArch := ctx.InstallForceOS()
1983 if forceOS != nil {
Colin Cross6e359402020-02-10 15:29:54 -08001984 os = *forceOS
1985 }
Jiyong Park87788b52020-09-01 12:37:45 +09001986 if forceArch != nil {
1987 arch = *forceArch
1988 }
Spandan Das5d1b9292021-06-03 19:36:41 +00001989 return os, arch
1990}
Colin Cross609c49a2020-02-13 13:20:11 -08001991
Colin Crossc0e42d52024-02-01 16:42:36 -08001992func pathForPartitionInstallDir(ctx PathContext, partition, partitionPath string, makePath bool) InstallPath {
1993 fullPath := ctx.Config().SoongOutDir()
1994 if makePath {
1995 // Make path starts with out/ instead of out/soong.
1996 fullPath = filepath.Join(fullPath, "../", partitionPath)
1997 } else {
1998 fullPath = filepath.Join(fullPath, partitionPath)
1999 }
2000
2001 return InstallPath{
2002 basePath: basePath{partitionPath, ""},
2003 soongOutDir: ctx.Config().soongOutDir,
2004 partitionDir: partitionPath,
2005 partition: partition,
2006 makePath: makePath,
2007 fullPath: fullPath,
2008 }
2009}
2010
Cole Faust3b703f32023-10-16 13:30:51 -07002011func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string,
Colin Cross609c49a2020-02-13 13:20:11 -08002012 pathComponents ...string) InstallPath {
2013
Jiyong Park97859152023-02-14 17:05:48 +09002014 var partitionPaths []string
Colin Cross609c49a2020-02-13 13:20:11 -08002015
Colin Cross6e359402020-02-10 15:29:54 -08002016 if os.Class == Device {
Jiyong Park97859152023-02-14 17:05:48 +09002017 partitionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002018 } else {
Jiyong Park87788b52020-09-01 12:37:45 +09002019 osName := os.String()
Colin Crossa9b2aac2022-06-15 17:25:51 -07002020 if os == Linux {
Jiyong Park87788b52020-09-01 12:37:45 +09002021 // instead of linux_glibc
2022 osName = "linux"
Dan Willemsen866b5632017-09-22 12:28:24 -07002023 }
Colin Crossa9b2aac2022-06-15 17:25:51 -07002024 if os == LinuxMusl && ctx.Config().UseHostMusl() {
2025 // When using musl instead of glibc, use "linux" instead of "linux_musl". When cross
2026 // compiling we will still use "linux_musl".
2027 osName = "linux"
2028 }
2029
Jiyong Park87788b52020-09-01 12:37:45 +09002030 // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
2031 // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem
2032 // to have a plan to fix it (see the comment in build/make/core/envsetup.mk).
2033 // Let's keep using x86 for the existing cases until we have a need to support
2034 // other architectures.
2035 archName := arch.String()
2036 if os.Class == Host && (arch == X86_64 || arch == Common) {
2037 archName = "x86"
2038 }
Jiyong Park97859152023-02-14 17:05:48 +09002039 partitionPaths = []string{"host", osName + "-" + archName, partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002040 }
Colin Cross70dda7e2019-10-01 22:05:35 -07002041
Jiyong Park97859152023-02-14 17:05:48 +09002042 partitionPath, err := validatePath(partitionPaths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07002043 if err != nil {
2044 reportPathError(ctx, err)
2045 }
Colin Crossff6c33d2019-10-02 16:01:35 -07002046
Colin Crossc0e42d52024-02-01 16:42:36 -08002047 base := pathForPartitionInstallDir(ctx, partition, partitionPath, ctx.Config().KatiEnabled())
Jiyong Park957bcd92020-10-20 18:23:33 +09002048 return base.Join(ctx, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002049}
2050
Spandan Dasf280b232024-04-04 21:25:51 +00002051func PathForNdkInstall(ctx PathContext, paths ...string) OutputPath {
2052 return PathForOutput(ctx, append([]string{"ndk"}, paths...)...)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00002053}
2054
2055func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath {
Spandan Dasf280b232024-04-04 21:25:51 +00002056 base := pathForPartitionInstallDir(ctx, "", "mainline-sdks", false)
2057 return base.Join(ctx, paths...)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00002058}
2059
Weijia Heaa37c162024-11-06 19:46:03 +00002060func PathForSuiteInstall(ctx PathContext, suite string, pathComponents ...string) InstallPath {
2061 return pathForPartitionInstallDir(ctx, "test_suites", "test_suites", false).Join(ctx, suite).Join(ctx, pathComponents...)
2062}
2063
Colin Cross70dda7e2019-10-01 22:05:35 -07002064func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string {
Colin Crossb1692a32021-10-25 15:39:01 -07002065 rel := Rel(ctx, strings.TrimSuffix(path.PartitionDir(), path.partition), path.String())
Colin Cross43f08db2018-11-12 10:13:39 -08002066 return "/" + rel
2067}
2068
Cole Faust11edf552023-10-13 11:32:14 -07002069func modulePartition(ctx ModuleInstallPathContext, device bool) string {
Colin Cross43f08db2018-11-12 10:13:39 -08002070 var partition string
Colin Cross6e359402020-02-10 15:29:54 -08002071 if ctx.InstallInTestcases() {
2072 // "testcases" install directory can be used for host or device modules.
Jaewoong Jung0949f312019-09-11 10:25:18 -07002073 partition = "testcases"
Cole Faust11edf552023-10-13 11:32:14 -07002074 } else if device {
Colin Cross6e359402020-02-10 15:29:54 -08002075 if ctx.InstallInData() {
2076 partition = "data"
2077 } else if ctx.InstallInRamdisk() {
2078 if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() {
2079 partition = "recovery/root/first_stage_ramdisk"
2080 } else {
2081 partition = "ramdisk"
2082 }
2083 if !ctx.InstallInRoot() {
2084 partition += "/system"
2085 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -07002086 } else if ctx.InstallInVendorRamdisk() {
Yifan Hong39143a92020-10-26 12:43:12 -07002087 // The module is only available after switching root into
2088 // /first_stage_ramdisk. To expose the module before switching root
2089 // on a device without a dedicated recovery partition, install the
2090 // recovery variant.
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002091 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
Petri Gyntherac229562021-03-02 23:44:02 -08002092 partition = "vendor_ramdisk/first_stage_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002093 } else {
Petri Gyntherac229562021-03-02 23:44:02 -08002094 partition = "vendor_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002095 }
2096 if !ctx.InstallInRoot() {
2097 partition += "/system"
2098 }
Inseob Kim08758f02021-04-08 21:13:22 +09002099 } else if ctx.InstallInDebugRamdisk() {
2100 partition = "debug_ramdisk"
Colin Cross6e359402020-02-10 15:29:54 -08002101 } else if ctx.InstallInRecovery() {
2102 if ctx.InstallInRoot() {
2103 partition = "recovery/root"
2104 } else {
2105 // the layout of recovery partion is the same as that of system partition
2106 partition = "recovery/root/system"
2107 }
Colin Crossea30d852023-11-29 16:00:16 -08002108 } else if ctx.SocSpecific() || ctx.InstallInVendor() {
Colin Cross6e359402020-02-10 15:29:54 -08002109 partition = ctx.DeviceConfig().VendorPath()
Colin Crossea30d852023-11-29 16:00:16 -08002110 } else if ctx.DeviceSpecific() || ctx.InstallInOdm() {
Colin Cross6e359402020-02-10 15:29:54 -08002111 partition = ctx.DeviceConfig().OdmPath()
Colin Crossea30d852023-11-29 16:00:16 -08002112 } else if ctx.ProductSpecific() || ctx.InstallInProduct() {
Colin Cross6e359402020-02-10 15:29:54 -08002113 partition = ctx.DeviceConfig().ProductPath()
2114 } else if ctx.SystemExtSpecific() {
2115 partition = ctx.DeviceConfig().SystemExtPath()
2116 } else if ctx.InstallInRoot() {
2117 partition = "root"
Yifan Hong82db7352020-01-21 16:12:26 -08002118 } else {
Colin Cross6e359402020-02-10 15:29:54 -08002119 partition = "system"
Yifan Hong82db7352020-01-21 16:12:26 -08002120 }
Colin Cross6e359402020-02-10 15:29:54 -08002121 if ctx.InstallInSanitizerDir() {
2122 partition = "data/asan/" + partition
Yifan Hong82db7352020-01-21 16:12:26 -08002123 }
Colin Cross43f08db2018-11-12 10:13:39 -08002124 }
2125 return partition
2126}
2127
Colin Cross609c49a2020-02-13 13:20:11 -08002128type InstallPaths []InstallPath
2129
2130// Paths returns the InstallPaths as a Paths
2131func (p InstallPaths) Paths() Paths {
2132 if p == nil {
2133 return nil
2134 }
2135 ret := make(Paths, len(p))
2136 for i, path := range p {
2137 ret[i] = path
2138 }
2139 return ret
2140}
2141
2142// Strings returns the string forms of the install paths.
2143func (p InstallPaths) Strings() []string {
2144 if p == nil {
2145 return nil
2146 }
2147 ret := make([]string, len(p))
2148 for i, path := range p {
2149 ret[i] = path.String()
2150 }
2151 return ret
2152}
2153
Jingwen Chen24d0c562023-02-07 09:29:36 +00002154// validatePathInternal ensures that a path does not leave its component, and
2155// optionally doesn't contain Ninja variables.
2156func validatePathInternal(allowNinjaVariables bool, pathComponents ...string) (string, error) {
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002157 initialEmpty := 0
2158 finalEmpty := 0
2159 for i, path := range pathComponents {
Jingwen Chen24d0c562023-02-07 09:29:36 +00002160 if !allowNinjaVariables && strings.Contains(path, "$") {
2161 return "", fmt.Errorf("Path contains invalid character($): %s", path)
2162 }
2163
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002164 path := filepath.Clean(path)
2165 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08002166 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002167 }
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002168
2169 if i == initialEmpty && pathComponents[i] == "" {
2170 initialEmpty++
2171 }
2172 if i == finalEmpty && pathComponents[len(pathComponents)-1-i] == "" {
2173 finalEmpty++
2174 }
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002175 }
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002176 // Optimization: filepath.Join("foo", "") returns a newly allocated copy
2177 // of "foo", while filepath.Join("foo") does not. Strip out any empty
2178 // path components.
2179 if initialEmpty == len(pathComponents) {
2180 return "", nil
2181 }
2182 nonEmptyPathComponents := pathComponents[initialEmpty : len(pathComponents)-finalEmpty]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002183 // TODO: filepath.Join isn't necessarily correct with embedded ninja
2184 // variables. '..' may remove the entire ninja variable, even if it
2185 // will be expanded to multiple nested directories.
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002186 return filepath.Join(nonEmptyPathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002187}
2188
Jingwen Chen24d0c562023-02-07 09:29:36 +00002189// validateSafePath validates a path that we trust (may contain ninja
2190// variables). Ensures that each path component does not attempt to leave its
2191// component. Returns a joined version of each path component.
2192func validateSafePath(pathComponents ...string) (string, error) {
2193 return validatePathInternal(true, pathComponents...)
2194}
2195
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002196// validatePath validates that a path does not include ninja variables, and that
2197// each path component does not attempt to leave its component. Returns a joined
2198// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08002199func validatePath(pathComponents ...string) (string, error) {
Jingwen Chen24d0c562023-02-07 09:29:36 +00002200 return validatePathInternal(false, pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -07002201}
Colin Cross5b529592017-05-09 13:34:34 -07002202
Colin Cross0875c522017-11-28 17:34:01 -08002203func PathForPhony(ctx PathContext, phony string) WritablePath {
2204 if strings.ContainsAny(phony, "$/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002205 ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -08002206 }
Paul Duffin74abc5d2021-03-24 09:24:59 +00002207 return PhonyPath{basePath{phony, ""}}
Colin Cross0875c522017-11-28 17:34:01 -08002208}
2209
Colin Cross74e3fe42017-12-11 15:51:44 -08002210type PhonyPath struct {
2211 basePath
2212}
2213
2214func (p PhonyPath) writablePath() {}
2215
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02002216func (p PhonyPath) getSoongOutDir() string {
Paul Duffind65c58b2021-03-24 09:22:07 +00002217 // A phone path cannot contain any / so cannot be relative to the build directory.
2218 return ""
Paul Duffin9b478b02019-12-10 13:41:51 +00002219}
2220
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002221func (p PhonyPath) RelativeToTop() Path {
2222 ensureTestOnly()
2223 // A phony path cannot contain any / so does not have a build directory so switching to a new
2224 // build directory has no effect so just return this path.
2225 return p
2226}
2227
Colin Cross7707b242024-07-26 12:02:36 -07002228func (p PhonyPath) WithoutRel() Path {
2229 p.basePath = p.basePath.withoutRel()
2230 return p
2231}
2232
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01002233func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
2234 panic("Not implemented")
2235}
2236
Colin Cross74e3fe42017-12-11 15:51:44 -08002237var _ Path = PhonyPath{}
2238var _ WritablePath = PhonyPath{}
2239
Colin Cross5b529592017-05-09 13:34:34 -07002240type testPath struct {
2241 basePath
2242}
2243
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002244func (p testPath) RelativeToTop() Path {
2245 ensureTestOnly()
2246 return p
2247}
2248
Colin Cross7707b242024-07-26 12:02:36 -07002249func (p testPath) WithoutRel() Path {
2250 p.basePath = p.basePath.withoutRel()
2251 return p
2252}
2253
Colin Cross5b529592017-05-09 13:34:34 -07002254func (p testPath) String() string {
2255 return p.path
2256}
2257
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002258var _ Path = testPath{}
2259
Colin Cross40e33732019-02-15 11:08:35 -08002260// PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from
2261// within tests.
Colin Cross5b529592017-05-09 13:34:34 -07002262func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -08002263 p, err := validateSafePath(paths...)
2264 if err != nil {
2265 panic(err)
2266 }
Colin Cross5b529592017-05-09 13:34:34 -07002267 return testPath{basePath{path: p, rel: p}}
2268}
2269
Sam Delmerico2351eac2022-05-24 17:10:02 +00002270func PathForTestingWithRel(path, rel string) Path {
2271 p, err := validateSafePath(path, rel)
2272 if err != nil {
2273 panic(err)
2274 }
2275 r, err := validatePath(rel)
2276 if err != nil {
2277 panic(err)
2278 }
2279 return testPath{basePath{path: p, rel: r}}
2280}
2281
Colin Cross40e33732019-02-15 11:08:35 -08002282// PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests.
2283func PathsForTesting(strs ...string) Paths {
Colin Cross5b529592017-05-09 13:34:34 -07002284 p := make(Paths, len(strs))
2285 for i, s := range strs {
2286 p[i] = PathForTesting(s)
2287 }
2288
2289 return p
2290}
Colin Cross43f08db2018-11-12 10:13:39 -08002291
Colin Cross40e33732019-02-15 11:08:35 -08002292type testPathContext struct {
2293 config Config
Colin Cross40e33732019-02-15 11:08:35 -08002294}
2295
Colin Cross40e33732019-02-15 11:08:35 -08002296func (x *testPathContext) Config() Config { return x.config }
2297func (x *testPathContext) AddNinjaFileDeps(...string) {}
2298
2299// PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
2300// PathForOutput.
Colin Cross98be1bb2019-12-13 20:41:13 -08002301func PathContextForTesting(config Config) PathContext {
Colin Cross40e33732019-02-15 11:08:35 -08002302 return &testPathContext{
2303 config: config,
Colin Cross40e33732019-02-15 11:08:35 -08002304 }
2305}
2306
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002307type testModuleInstallPathContext struct {
2308 baseModuleContext
2309
2310 inData bool
2311 inTestcases bool
2312 inSanitizerDir bool
2313 inRamdisk bool
2314 inVendorRamdisk bool
Inseob Kim08758f02021-04-08 21:13:22 +09002315 inDebugRamdisk bool
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002316 inRecovery bool
2317 inRoot bool
Colin Crossea30d852023-11-29 16:00:16 -08002318 inOdm bool
2319 inProduct bool
2320 inVendor bool
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002321 forceOS *OsType
2322 forceArch *ArchType
2323}
2324
2325func (m testModuleInstallPathContext) Config() Config {
2326 return m.baseModuleContext.config
2327}
2328
2329func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {}
2330
2331func (m testModuleInstallPathContext) InstallInData() bool {
2332 return m.inData
2333}
2334
2335func (m testModuleInstallPathContext) InstallInTestcases() bool {
2336 return m.inTestcases
2337}
2338
2339func (m testModuleInstallPathContext) InstallInSanitizerDir() bool {
2340 return m.inSanitizerDir
2341}
2342
2343func (m testModuleInstallPathContext) InstallInRamdisk() bool {
2344 return m.inRamdisk
2345}
2346
2347func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool {
2348 return m.inVendorRamdisk
2349}
2350
Inseob Kim08758f02021-04-08 21:13:22 +09002351func (m testModuleInstallPathContext) InstallInDebugRamdisk() bool {
2352 return m.inDebugRamdisk
2353}
2354
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002355func (m testModuleInstallPathContext) InstallInRecovery() bool {
2356 return m.inRecovery
2357}
2358
2359func (m testModuleInstallPathContext) InstallInRoot() bool {
2360 return m.inRoot
2361}
2362
Colin Crossea30d852023-11-29 16:00:16 -08002363func (m testModuleInstallPathContext) InstallInOdm() bool {
2364 return m.inOdm
2365}
2366
2367func (m testModuleInstallPathContext) InstallInProduct() bool {
2368 return m.inProduct
2369}
2370
2371func (m testModuleInstallPathContext) InstallInVendor() bool {
2372 return m.inVendor
2373}
2374
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002375func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
2376 return m.forceOS, m.forceArch
2377}
2378
2379// Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is
2380// default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are
2381// delegated to it will panic.
2382func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext {
2383 ctx := &testModuleInstallPathContext{}
2384 ctx.config = config
2385 ctx.os = Android
2386 return ctx
2387}
2388
Colin Cross43f08db2018-11-12 10:13:39 -08002389// Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
2390// targetPath is not inside basePath.
2391func Rel(ctx PathContext, basePath string, targetPath string) string {
2392 rel, isRel := MaybeRel(ctx, basePath, targetPath)
2393 if !isRel {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002394 ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
Colin Cross43f08db2018-11-12 10:13:39 -08002395 return ""
2396 }
2397 return rel
2398}
2399
2400// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
2401// targetPath is not inside basePath.
2402func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
Dan Willemsen633c5022019-04-12 11:11:38 -07002403 rel, isRel, err := maybeRelErr(basePath, targetPath)
2404 if err != nil {
2405 reportPathError(ctx, err)
2406 }
2407 return rel, isRel
2408}
2409
2410func maybeRelErr(basePath string, targetPath string) (string, bool, error) {
Colin Cross43f08db2018-11-12 10:13:39 -08002411 // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
2412 if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
Dan Willemsen633c5022019-04-12 11:11:38 -07002413 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002414 }
2415 rel, err := filepath.Rel(basePath, targetPath)
2416 if err != nil {
Dan Willemsen633c5022019-04-12 11:11:38 -07002417 return "", false, err
Colin Cross43f08db2018-11-12 10:13:39 -08002418 } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
Dan Willemsen633c5022019-04-12 11:11:38 -07002419 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002420 }
Dan Willemsen633c5022019-04-12 11:11:38 -07002421 return rel, true, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002422}
Colin Cross988414c2020-01-11 01:11:46 +00002423
2424// Writes a file to the output directory. Attempting to write directly to the output directory
2425// will fail due to the sandbox of the soong_build process.
Chris Parsons1a12d032023-02-06 22:37:41 -05002426// Only writes the file if the file doesn't exist or if it has different contents, to prevent
2427// updating the timestamp if no changes would be made. (This is better for incremental
2428// performance.)
Colin Cross988414c2020-01-11 01:11:46 +00002429func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error {
Colin Crossd6421132021-11-09 12:32:34 -08002430 absPath := absolutePath(path.String())
2431 err := os.MkdirAll(filepath.Dir(absPath), 0777)
2432 if err != nil {
2433 return err
2434 }
Chris Parsons1a12d032023-02-06 22:37:41 -05002435 return pathtools.WriteFileIfChanged(absPath, data, perm)
Colin Cross988414c2020-01-11 01:11:46 +00002436}
2437
Liz Kammer2dd9ca42020-11-25 16:06:39 -08002438func RemoveAllOutputDir(path WritablePath) error {
2439 return os.RemoveAll(absolutePath(path.String()))
2440}
2441
2442func CreateOutputDirIfNonexistent(path WritablePath, perm os.FileMode) error {
2443 dir := absolutePath(path.String())
Liz Kammer09f947d2021-05-12 14:51:49 -04002444 return createDirIfNonexistent(dir, perm)
2445}
2446
2447func createDirIfNonexistent(dir string, perm os.FileMode) error {
Liz Kammer2dd9ca42020-11-25 16:06:39 -08002448 if _, err := os.Stat(dir); os.IsNotExist(err) {
2449 return os.MkdirAll(dir, os.ModePerm)
2450 } else {
2451 return err
2452 }
2453}
2454
Jingwen Chen78257e52021-05-21 02:34:24 +00002455// absolutePath is deliberately private so that Soong's Go plugins can't use it to find and
2456// read arbitrary files without going through the methods in the current package that track
2457// dependencies.
Colin Cross988414c2020-01-11 01:11:46 +00002458func absolutePath(path string) string {
2459 if filepath.IsAbs(path) {
2460 return path
2461 }
2462 return filepath.Join(absSrcDir, path)
2463}
Chris Parsons216e10a2020-07-09 17:12:52 -04002464
2465// A DataPath represents the path of a file to be used as data, for example
2466// a test library to be installed alongside a test.
2467// The data file should be installed (copied from `<SrcPath>`) to
2468// `<install_root>/<RelativeInstallPath>/<filename>`, or
2469// `<install_root>/<filename>` if RelativeInstallPath is empty.
2470type DataPath struct {
2471 // The path of the data file that should be copied into the data directory
2472 SrcPath Path
2473 // The install path of the data file, relative to the install root.
2474 RelativeInstallPath string
Colin Cross5c1d5fb2023-11-15 12:39:40 -08002475 // If WithoutRel is true, use SrcPath.Base() instead of SrcPath.Rel() as the filename.
2476 WithoutRel bool
Chris Parsons216e10a2020-07-09 17:12:52 -04002477}
Colin Crossdcf71b22021-02-01 13:59:03 -08002478
Colin Crossd442a0e2023-11-16 11:19:26 -08002479func (d *DataPath) ToRelativeInstallPath() string {
2480 relPath := d.SrcPath.Rel()
Colin Cross5c1d5fb2023-11-15 12:39:40 -08002481 if d.WithoutRel {
2482 relPath = d.SrcPath.Base()
2483 }
Colin Crossd442a0e2023-11-16 11:19:26 -08002484 if d.RelativeInstallPath != "" {
2485 relPath = filepath.Join(d.RelativeInstallPath, relPath)
2486 }
2487 return relPath
2488}
2489
Colin Crossdcf71b22021-02-01 13:59:03 -08002490// PathsIfNonNil returns a Paths containing only the non-nil input arguments.
2491func PathsIfNonNil(paths ...Path) Paths {
2492 if len(paths) == 0 {
2493 // Fast path for empty argument list
2494 return nil
2495 } else if len(paths) == 1 {
2496 // Fast path for a single argument
2497 if paths[0] != nil {
2498 return paths
2499 } else {
2500 return nil
2501 }
2502 }
2503 ret := make(Paths, 0, len(paths))
2504 for _, path := range paths {
2505 if path != nil {
2506 ret = append(ret, path)
2507 }
2508 }
2509 if len(ret) == 0 {
2510 return nil
2511 }
2512 return ret
2513}
Chris Wailesb2703ad2021-07-30 13:25:42 -07002514
2515var thirdPartyDirPrefixExceptions = []*regexp.Regexp{
2516 regexp.MustCompile("^vendor/[^/]*google[^/]*/"),
2517 regexp.MustCompile("^hardware/google/"),
2518 regexp.MustCompile("^hardware/interfaces/"),
2519 regexp.MustCompile("^hardware/libhardware[^/]*/"),
2520 regexp.MustCompile("^hardware/ril/"),
2521}
2522
2523func IsThirdPartyPath(path string) bool {
2524 thirdPartyDirPrefixes := []string{"external/", "vendor/", "hardware/"}
2525
2526 if HasAnyPrefix(path, thirdPartyDirPrefixes) {
2527 for _, prefix := range thirdPartyDirPrefixExceptions {
2528 if prefix.MatchString(path) {
2529 return false
2530 }
2531 }
2532 return true
2533 }
2534 return false
2535}