blob: 65750183083089ac6b4fcaa8eb91518fa5bb4f37 [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 (
Yu Liufa297642024-06-11 00:13:02 +000018 "bytes"
19 "encoding/gob"
20 "errors"
Colin Cross6e18ca42015-07-14 18:55:36 -070021 "fmt"
Colin Cross988414c2020-01-11 01:11:46 +000022 "os"
Colin Cross6a745c62015-06-16 16:38:10 -070023 "path/filepath"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070024 "reflect"
Chris Wailesb2703ad2021-07-30 13:25:42 -070025 "regexp"
Colin Cross5e6cfbe2017-11-03 15:20:35 -070026 "sort"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070027 "strings"
28
29 "github.com/google/blueprint"
30 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080031)
32
Colin Cross988414c2020-01-11 01:11:46 +000033var absSrcDir string
34
Dan Willemsen34cc69e2015-09-23 15:26:20 -070035// PathContext is the subset of a (Module|Singleton)Context required by the
36// Path methods.
37type PathContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -080038 Config() Config
Dan Willemsen7b310ee2015-12-18 15:11:17 -080039 AddNinjaFileDeps(deps ...string)
Colin Cross3f40fa42015-01-30 17:27:36 -080040}
41
Colin Cross7f19f372016-11-01 11:10:25 -070042type PathGlobContext interface {
Colin Cross662d6142022-11-03 20:38:01 -070043 PathContext
Colin Cross7f19f372016-11-01 11:10:25 -070044 GlobWithDeps(globPattern string, excludes []string) ([]string, error)
45}
46
Colin Crossaabf6792017-11-29 00:27:14 -080047var _ PathContext = SingletonContext(nil)
48var _ PathContext = ModuleContext(nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -070049
Ulya Trafimovich8640ab92020-05-11 18:06:15 +010050// "Null" path context is a minimal path context for a given config.
51type NullPathContext struct {
52 config Config
53}
54
55func (NullPathContext) AddNinjaFileDeps(...string) {}
56func (ctx NullPathContext) Config() Config { return ctx.config }
57
Liz Kammera830f3a2020-11-10 10:50:34 -080058// EarlyModulePathContext is a subset of EarlyModuleContext methods required by the
59// Path methods. These path methods can be called before any mutators have run.
60type EarlyModulePathContext interface {
Liz Kammera830f3a2020-11-10 10:50:34 -080061 PathGlobContext
62
63 ModuleDir() string
64 ModuleErrorf(fmt string, args ...interface{})
Cole Fausta963b942024-04-11 17:43:00 -070065 OtherModulePropertyErrorf(module Module, property, fmt string, args ...interface{})
Liz Kammera830f3a2020-11-10 10:50:34 -080066}
67
68var _ EarlyModulePathContext = ModuleContext(nil)
69
70// Glob globs files and directories matching globPattern relative to ModuleDir(),
71// paths in the excludes parameter will be omitted.
72func Glob(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths {
73 ret, err := ctx.GlobWithDeps(globPattern, excludes)
74 if err != nil {
75 ctx.ModuleErrorf("glob: %s", err.Error())
76 }
77 return pathsForModuleSrcFromFullPath(ctx, ret, true)
78}
79
80// GlobFiles globs *only* files (not directories) matching globPattern relative to ModuleDir().
81// Paths in the excludes parameter will be omitted.
82func GlobFiles(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths {
83 ret, err := ctx.GlobWithDeps(globPattern, excludes)
84 if err != nil {
85 ctx.ModuleErrorf("glob: %s", err.Error())
86 }
87 return pathsForModuleSrcFromFullPath(ctx, ret, false)
88}
89
90// ModuleWithDepsPathContext is a subset of *ModuleContext methods required by
91// the Path methods that rely on module dependencies having been resolved.
92type ModuleWithDepsPathContext interface {
93 EarlyModulePathContext
Cole Faust55b56fe2024-08-23 12:06:11 -070094 OtherModuleProviderContext
Paul Duffin40131a32021-07-09 17:10:35 +010095 VisitDirectDepsBlueprint(visit func(blueprint.Module))
96 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
Cole Faust4e2bf9f2024-09-11 13:26:20 -070097 HasMutatorFinished(mutatorName string) bool
Liz Kammera830f3a2020-11-10 10:50:34 -080098}
99
100// ModuleMissingDepsPathContext is a subset of *ModuleContext methods required by
101// the Path methods that rely on module dependencies having been resolved and ability to report
102// missing dependency errors.
103type ModuleMissingDepsPathContext interface {
104 ModuleWithDepsPathContext
105 AddMissingDependencies(missingDeps []string)
106}
107
Dan Willemsen00269f22017-07-06 16:59:48 -0700108type ModuleInstallPathContext interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700109 BaseModuleContext
Dan Willemsen00269f22017-07-06 16:59:48 -0700110
111 InstallInData() bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700112 InstallInTestcases() bool
Dan Willemsen00269f22017-07-06 16:59:48 -0700113 InstallInSanitizerDir() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -0800114 InstallInRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700115 InstallInVendorRamdisk() bool
Inseob Kim08758f02021-04-08 21:13:22 +0900116 InstallInDebugRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900117 InstallInRecovery() bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700118 InstallInRoot() bool
Colin Crossea30d852023-11-29 16:00:16 -0800119 InstallInOdm() bool
120 InstallInProduct() bool
121 InstallInVendor() bool
Jiyong Park87788b52020-09-01 12:37:45 +0900122 InstallForceOS() (*OsType, *ArchType)
Dan Willemsen00269f22017-07-06 16:59:48 -0700123}
124
125var _ ModuleInstallPathContext = ModuleContext(nil)
126
Cole Faust11edf552023-10-13 11:32:14 -0700127type baseModuleContextToModuleInstallPathContext struct {
128 BaseModuleContext
129}
130
131func (ctx *baseModuleContextToModuleInstallPathContext) InstallInData() bool {
132 return ctx.Module().InstallInData()
133}
134
135func (ctx *baseModuleContextToModuleInstallPathContext) InstallInTestcases() bool {
136 return ctx.Module().InstallInTestcases()
137}
138
139func (ctx *baseModuleContextToModuleInstallPathContext) InstallInSanitizerDir() bool {
140 return ctx.Module().InstallInSanitizerDir()
141}
142
143func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRamdisk() bool {
144 return ctx.Module().InstallInRamdisk()
145}
146
147func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendorRamdisk() bool {
148 return ctx.Module().InstallInVendorRamdisk()
149}
150
151func (ctx *baseModuleContextToModuleInstallPathContext) InstallInDebugRamdisk() bool {
152 return ctx.Module().InstallInDebugRamdisk()
153}
154
155func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRecovery() bool {
156 return ctx.Module().InstallInRecovery()
157}
158
159func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRoot() bool {
160 return ctx.Module().InstallInRoot()
161}
162
Colin Crossea30d852023-11-29 16:00:16 -0800163func (ctx *baseModuleContextToModuleInstallPathContext) InstallInOdm() bool {
164 return ctx.Module().InstallInOdm()
165}
166
167func (ctx *baseModuleContextToModuleInstallPathContext) InstallInProduct() bool {
168 return ctx.Module().InstallInProduct()
169}
170
171func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendor() bool {
172 return ctx.Module().InstallInVendor()
173}
174
Cole Faust11edf552023-10-13 11:32:14 -0700175func (ctx *baseModuleContextToModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
176 return ctx.Module().InstallForceOS()
177}
178
179var _ ModuleInstallPathContext = (*baseModuleContextToModuleInstallPathContext)(nil)
180
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700181// errorfContext is the interface containing the Errorf method matching the
182// Errorf method in blueprint.SingletonContext.
183type errorfContext interface {
184 Errorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800185}
186
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700187var _ errorfContext = blueprint.SingletonContext(nil)
188
Spandan Das59a4a2b2024-01-09 21:35:56 +0000189// ModuleErrorfContext is the interface containing the ModuleErrorf method matching
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700190// the ModuleErrorf method in blueprint.ModuleContext.
Spandan Das59a4a2b2024-01-09 21:35:56 +0000191type ModuleErrorfContext interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700192 ModuleErrorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800193}
194
Spandan Das59a4a2b2024-01-09 21:35:56 +0000195var _ ModuleErrorfContext = blueprint.ModuleContext(nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700196
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700197// reportPathError will register an error with the attached context. It
198// attempts ctx.ModuleErrorf for a better error message first, then falls
199// back to ctx.Errorf.
Colin Cross1ccfcc32018-02-22 13:54:26 -0800200func reportPathError(ctx PathContext, err error) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100201 ReportPathErrorf(ctx, "%s", err.Error())
Colin Cross1ccfcc32018-02-22 13:54:26 -0800202}
203
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100204// ReportPathErrorf will register an error with the attached context. It
Colin Cross1ccfcc32018-02-22 13:54:26 -0800205// attempts ctx.ModuleErrorf for a better error message first, then falls
206// back to ctx.Errorf.
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100207func ReportPathErrorf(ctx PathContext, format string, args ...interface{}) {
Spandan Das59a4a2b2024-01-09 21:35:56 +0000208 if mctx, ok := ctx.(ModuleErrorfContext); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700209 mctx.ModuleErrorf(format, args...)
210 } else if ectx, ok := ctx.(errorfContext); ok {
211 ectx.Errorf(format, args...)
212 } else {
213 panic(fmt.Sprintf(format, args...))
Colin Crossf2298272015-05-12 11:36:53 -0700214 }
215}
216
Colin Cross5e708052019-08-06 13:59:50 -0700217func pathContextName(ctx PathContext, module blueprint.Module) string {
218 if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok {
219 return x.ModuleName(module)
220 } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok {
221 return x.OtherModuleName(module)
222 }
223 return "unknown"
224}
225
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700226type Path interface {
227 // Returns the path in string form
228 String() string
229
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700230 // Ext returns the extension of the last element of the path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700231 Ext() string
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700232
233 // Base returns the last element of the path
234 Base() string
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800235
236 // Rel returns the portion of the path relative to the directory it was created from. For
237 // example, Rel on a PathsForModuleSrc would return the path relative to the module source
Colin Cross0db55682017-12-05 15:36:55 -0800238 // directory, and OutputPath.Join("foo").Rel() would return "foo".
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800239 Rel() string
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000240
Colin Cross7707b242024-07-26 12:02:36 -0700241 // WithoutRel returns a new Path with no relative path, i.e. Rel() will return the same value as Base().
242 WithoutRel() Path
243
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000244 // RelativeToTop returns a new path relative to the top, it is provided solely for use in tests.
245 //
246 // It is guaranteed to always return the same type as it is called on, e.g. if called on an
247 // InstallPath then the returned value can be converted to an InstallPath.
248 //
249 // A standard build has the following structure:
250 // ../top/
251 // out/ - make install files go here.
Colin Cross3b1c6842024-07-26 11:52:57 -0700252 // out/soong - this is the outDir passed to NewTestConfig()
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000253 // ... - the source files
254 //
255 // This function converts a path so that it appears relative to the ../top/ directory, i.e.
Colin Cross3b1c6842024-07-26 11:52:57 -0700256 // * Make install paths, which have the pattern "outDir/../<path>" are converted into the top
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000257 // relative path "out/<path>"
Colin Cross3b1c6842024-07-26 11:52:57 -0700258 // * Soong install paths and other writable paths, which have the pattern "outDir/soong/<path>" are
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000259 // converted into the top relative path "out/soong/<path>".
260 // * Source paths are already relative to the top.
261 // * Phony paths are not relative to anything.
262 // * toolDepPath have an absolute but known value in so don't need making relative to anything in
263 // order to test.
264 RelativeToTop() Path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700265}
266
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000267const (
Colin Cross3b1c6842024-07-26 11:52:57 -0700268 testOutDir = "out"
269 testOutSoongSubDir = "/soong"
270 TestOutSoongDir = testOutDir + testOutSoongSubDir
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000271)
272
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700273// WritablePath is a type of path that can be used as an output for build rules.
274type WritablePath interface {
275 Path
276
Paul Duffin9b478b02019-12-10 13:41:51 +0000277 // return the path to the build directory.
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200278 getSoongOutDir() string
Paul Duffin9b478b02019-12-10 13:41:51 +0000279
Jeff Gaston734e3802017-04-10 15:47:24 -0700280 // the writablePath method doesn't directly do anything,
281 // but it allows a struct to distinguish between whether or not it implements the WritablePath interface
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700282 writablePath()
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +0100283
284 ReplaceExtension(ctx PathContext, ext string) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700285}
286
287type genPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800288 genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath
yangbill6d032dd2024-04-18 03:05:49 +0000289 genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700290}
291type objPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800292 objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700293}
294type resPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800295 resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700296}
297
298// GenPathWithExt derives a new file path in ctx's generated sources directory
299// from the current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800300func GenPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700301 if path, ok := p.(genPathProvider); ok {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700302 return path.genPathWithExt(ctx, subdir, ext)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700303 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100304 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700305 return PathForModuleGen(ctx)
306}
307
yangbill6d032dd2024-04-18 03:05:49 +0000308// GenPathWithExtAndTrimExt derives a new file path in ctx's generated sources directory
309// from the current path, but with the new extension and trim the suffix.
310func GenPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir string, p Path, ext string, trimExt string) ModuleGenPath {
311 if path, ok := p.(genPathProvider); ok {
312 return path.genPathWithExtAndTrimExt(ctx, subdir, ext, trimExt)
313 }
314 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
315 return PathForModuleGen(ctx)
316}
317
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700318// ObjPathWithExt derives a new file path in ctx's object directory from the
319// current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800320func ObjPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700321 if path, ok := p.(objPathProvider); ok {
322 return path.objPathWithExt(ctx, subdir, ext)
323 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100324 ReportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700325 return PathForModuleObj(ctx)
326}
327
328// ResPathWithName derives a new path in ctx's output resource directory, using
329// the current path to create the directory name, and the `name` argument for
330// the filename.
Liz Kammera830f3a2020-11-10 10:50:34 -0800331func ResPathWithName(ctx ModuleOutPathContext, p Path, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700332 if path, ok := p.(resPathProvider); ok {
333 return path.resPathWithName(ctx, name)
334 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100335 ReportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700336 return PathForModuleRes(ctx)
337}
338
339// OptionalPath is a container that may or may not contain a valid Path.
340type OptionalPath struct {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100341 path Path // nil if invalid.
342 invalidReason string // Not applicable if path != nil. "" if the reason is unknown.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700343}
344
345// OptionalPathForPath returns an OptionalPath containing the path.
346func OptionalPathForPath(path Path) OptionalPath {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100347 return OptionalPath{path: path}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700348}
349
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100350// InvalidOptionalPath returns an OptionalPath that is invalid with the given reason.
351func InvalidOptionalPath(reason string) OptionalPath {
352
353 return OptionalPath{invalidReason: reason}
354}
355
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700356// Valid returns whether there is a valid path
357func (p OptionalPath) Valid() bool {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100358 return p.path != nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700359}
360
361// Path returns the Path embedded in this OptionalPath. You must be sure that
362// there is a valid path, since this method will panic if there is not.
363func (p OptionalPath) Path() Path {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100364 if p.path == nil {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100365 msg := "Requesting an invalid path"
366 if p.invalidReason != "" {
367 msg += ": " + p.invalidReason
368 }
369 panic(msg)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700370 }
371 return p.path
372}
373
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100374// InvalidReason returns the reason that the optional path is invalid, or "" if it is valid.
375func (p OptionalPath) InvalidReason() string {
376 if p.path != nil {
377 return ""
378 }
379 if p.invalidReason == "" {
380 return "unknown"
381 }
382 return p.invalidReason
383}
384
Paul Duffinef081852021-05-13 11:11:15 +0100385// AsPaths converts the OptionalPath into Paths.
386//
387// It returns nil if this is not valid, or a single length slice containing the Path embedded in
388// this OptionalPath.
389func (p OptionalPath) AsPaths() Paths {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100390 if p.path == nil {
Paul Duffinef081852021-05-13 11:11:15 +0100391 return nil
392 }
393 return Paths{p.path}
394}
395
Paul Duffinafdd4062021-03-30 19:44:07 +0100396// RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the
397// result of calling Path.RelativeToTop on it.
398func (p OptionalPath) RelativeToTop() OptionalPath {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100399 if p.path == nil {
Paul Duffina5b81352021-03-28 23:57:19 +0100400 return p
401 }
402 p.path = p.path.RelativeToTop()
403 return p
404}
405
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700406// String returns the string version of the Path, or "" if it isn't valid.
407func (p OptionalPath) String() string {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100408 if p.path != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700409 return p.path.String()
410 } else {
411 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700412 }
413}
Colin Cross6e18ca42015-07-14 18:55:36 -0700414
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700415// Paths is a slice of Path objects, with helpers to operate on the collection.
416type Paths []Path
417
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000418// RelativeToTop creates a new Paths containing the result of calling Path.RelativeToTop on each
419// item in this slice.
420func (p Paths) RelativeToTop() Paths {
421 ensureTestOnly()
422 if p == nil {
423 return p
424 }
425 ret := make(Paths, len(p))
426 for i, path := range p {
427 ret[i] = path.RelativeToTop()
428 }
429 return ret
430}
431
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000432func (paths Paths) containsPath(path Path) bool {
433 for _, p := range paths {
434 if p == path {
435 return true
436 }
437 }
438 return false
439}
440
Liz Kammer7aa52882021-02-11 09:16:14 -0500441// PathsForSource returns Paths rooted from SrcDir, *not* rooted from the module's local source
442// directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700443func PathsForSource(ctx PathContext, paths []string) Paths {
444 ret := make(Paths, len(paths))
445 for i, path := range paths {
446 ret[i] = PathForSource(ctx, path)
447 }
448 return ret
449}
450
Liz Kammer7aa52882021-02-11 09:16:14 -0500451// ExistentPathsForSources returns a list of Paths rooted from SrcDir, *not* rooted from the
452// module's local source directory, that are found in the tree. If any are not found, they are
453// 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 -0700454func ExistentPathsForSources(ctx PathGlobContext, paths []string) Paths {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800455 ret := make(Paths, 0, len(paths))
456 for _, path := range paths {
Colin Cross32f38982018-02-22 11:47:25 -0800457 p := ExistentPathForSource(ctx, path)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800458 if p.Valid() {
459 ret = append(ret, p.Path())
460 }
461 }
462 return ret
463}
464
Liz Kammer620dea62021-04-14 17:36:10 -0400465// PathsForModuleSrc returns a Paths{} containing the resolved references in paths:
Colin Crossd079e0b2022-08-16 10:27:33 -0700466// - filepath, relative to local module directory, resolves as a filepath relative to the local
467// source directory
468// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
469// source directory.
470// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700471// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
472// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700473//
Liz Kammer620dea62021-04-14 17:36:10 -0400474// Properties passed as the paths argument must have been annotated with struct tag
Colin Cross41955e82019-05-29 14:40:35 -0700475// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000476// pathdeps mutator.
Liz Kammer620dea62021-04-14 17:36:10 -0400477// If a requested module is not found as a dependency:
Colin Crossd079e0b2022-08-16 10:27:33 -0700478// - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
Liz Kammer620dea62021-04-14 17:36:10 -0400479// missing dependencies
Colin Crossd079e0b2022-08-16 10:27:33 -0700480// - otherwise, a ModuleError is thrown.
Liz Kammera830f3a2020-11-10 10:50:34 -0800481func PathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths {
Colin Cross8a497952019-03-05 22:25:09 -0800482 return PathsForModuleSrcExcludes(ctx, paths, nil)
483}
484
Liz Kammer619be462022-01-28 15:13:39 -0500485type SourceInput struct {
486 Context ModuleMissingDepsPathContext
487 Paths []string
488 ExcludePaths []string
489 IncludeDirs bool
490}
491
Liz Kammer620dea62021-04-14 17:36:10 -0400492// PathsForModuleSrcExcludes returns a Paths{} containing the resolved references in paths, minus
493// those listed in excludes. Elements of paths and excludes are resolved as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700494// - filepath, relative to local module directory, resolves as a filepath relative to the local
495// source directory
496// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
497// source directory. Not valid in excludes.
498// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700499// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
500// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700501//
Liz Kammer620dea62021-04-14 17:36:10 -0400502// excluding the items (similarly resolved
503// Properties passed as the paths argument must have been annotated with struct tag
504// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000505// pathdeps mutator.
Liz Kammer620dea62021-04-14 17:36:10 -0400506// If a requested module is not found as a dependency:
Colin Crossd079e0b2022-08-16 10:27:33 -0700507// - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
Liz Kammer620dea62021-04-14 17:36:10 -0400508// missing dependencies
Colin Crossd079e0b2022-08-16 10:27:33 -0700509// - otherwise, a ModuleError is thrown.
Liz Kammera830f3a2020-11-10 10:50:34 -0800510func PathsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) Paths {
Liz Kammer619be462022-01-28 15:13:39 -0500511 return PathsRelativeToModuleSourceDir(SourceInput{
512 Context: ctx,
513 Paths: paths,
514 ExcludePaths: excludes,
515 IncludeDirs: true,
516 })
517}
518
519func PathsRelativeToModuleSourceDir(input SourceInput) Paths {
520 ret, missingDeps := PathsAndMissingDepsRelativeToModuleSourceDir(input)
521 if input.Context.Config().AllowMissingDependencies() {
522 input.Context.AddMissingDependencies(missingDeps)
Colin Crossba71a3f2019-03-18 12:12:48 -0700523 } else {
524 for _, m := range missingDeps {
Liz Kammer619be462022-01-28 15:13:39 -0500525 input.Context.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
Colin Crossba71a3f2019-03-18 12:12:48 -0700526 }
527 }
528 return ret
529}
530
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000531// OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection.
532type OutputPaths []OutputPath
533
534// Paths returns the OutputPaths as a Paths
535func (p OutputPaths) Paths() Paths {
536 if p == nil {
537 return nil
538 }
539 ret := make(Paths, len(p))
540 for i, path := range p {
541 ret[i] = path
542 }
543 return ret
544}
545
546// Strings returns the string forms of the writable paths.
547func (p OutputPaths) Strings() []string {
548 if p == nil {
549 return nil
550 }
551 ret := make([]string, len(p))
552 for i, path := range p {
553 ret[i] = path.String()
554 }
555 return ret
556}
557
Liz Kammera830f3a2020-11-10 10:50:34 -0800558// Expands Paths to a SourceFileProducer or OutputFileProducer module dependency referenced via ":name" or ":name{.tag}" syntax.
559// If the dependency is not found, a missingErrorDependency is returned.
560// If the module dependency is not a SourceFileProducer or OutputFileProducer, appropriate errors will be returned.
561func getPathsFromModuleDep(ctx ModuleWithDepsPathContext, path, moduleName, tag string) (Paths, error) {
Paul Duffind5cf92e2021-07-09 17:38:55 +0100562 module := GetModuleFromPathDep(ctx, moduleName, tag)
Liz Kammera830f3a2020-11-10 10:50:34 -0800563 if module == nil {
564 return nil, missingDependencyError{[]string{moduleName}}
565 }
Cole Fausta963b942024-04-11 17:43:00 -0700566 if aModule, ok := module.(Module); ok && !aModule.Enabled(ctx) {
Colin Crossfa65cee2021-03-22 17:05:59 -0700567 return nil, missingDependencyError{[]string{moduleName}}
568 }
mrziwange6c85812024-05-22 14:36:09 -0700569 outputFiles, err := outputFilesForModule(ctx, module, tag)
570 if outputFiles != nil && err == nil {
571 return outputFiles, nil
Liz Kammera830f3a2020-11-10 10:50:34 -0800572 } else {
mrziwange6c85812024-05-22 14:36:09 -0700573 return nil, err
Liz Kammera830f3a2020-11-10 10:50:34 -0800574 }
575}
576
Paul Duffind5cf92e2021-07-09 17:38:55 +0100577// GetModuleFromPathDep will return the module that was added as a dependency automatically for
578// properties tagged with `android:"path"` or manually using ExtractSourceDeps or
579// ExtractSourcesDeps.
580//
581// The moduleName and tag supplied to this should be the values returned from SrcIsModuleWithTag.
582// Or, if no tag is expected then the moduleName should be the value returned by SrcIsModule and
583// the tag must be "".
584//
585// If tag is "" then the returned module will be the dependency that was added for ":moduleName".
586// Otherwise, it is the dependency that was added for ":moduleName{tag}".
Paul Duffind5cf92e2021-07-09 17:38:55 +0100587func GetModuleFromPathDep(ctx ModuleWithDepsPathContext, moduleName, tag string) blueprint.Module {
Paul Duffin40131a32021-07-09 17:10:35 +0100588 var found blueprint.Module
589 // The sourceOrOutputDepTag uniquely identifies the module dependency as it contains both the
590 // module name and the tag. Dependencies added automatically for properties tagged with
591 // `android:"path"` are deduped so are guaranteed to be unique. It is possible for duplicate
592 // dependencies to be added manually using ExtractSourcesDeps or ExtractSourceDeps but even then
593 // it will always be the case that the dependencies will be identical, i.e. the same tag and same
594 // moduleName referring to the same dependency module.
595 //
596 // It does not matter whether the moduleName is a fully qualified name or if the module
597 // dependency is a prebuilt module. All that matters is the same information is supplied to
598 // create the tag here as was supplied to create the tag when the dependency was added so that
599 // this finds the matching dependency module.
600 expectedTag := sourceOrOutputDepTag(moduleName, tag)
601 ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) {
602 depTag := ctx.OtherModuleDependencyTag(module)
603 if depTag == expectedTag {
604 found = module
605 }
606 })
607 return found
Paul Duffind5cf92e2021-07-09 17:38:55 +0100608}
609
Liz Kammer620dea62021-04-14 17:36:10 -0400610// PathsAndMissingDepsForModuleSrcExcludes returns a Paths{} containing the resolved references in
611// paths, minus those listed in excludes. Elements of paths and excludes are resolved as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700612// - filepath, relative to local module directory, resolves as a filepath relative to the local
613// source directory
614// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
615// source directory. Not valid in excludes.
616// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700617// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
618// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700619//
Liz Kammer620dea62021-04-14 17:36:10 -0400620// and a list of the module names of missing module dependencies are returned as the second return.
621// Properties passed as the paths argument must have been annotated with struct tag
Colin Cross41955e82019-05-29 14:40:35 -0700622// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000623// pathdeps mutator.
Liz Kammer619be462022-01-28 15:13:39 -0500624func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) (Paths, []string) {
625 return PathsAndMissingDepsRelativeToModuleSourceDir(SourceInput{
626 Context: ctx,
627 Paths: paths,
628 ExcludePaths: excludes,
629 IncludeDirs: true,
630 })
631}
632
633func PathsAndMissingDepsRelativeToModuleSourceDir(input SourceInput) (Paths, []string) {
634 prefix := pathForModuleSrc(input.Context).String()
Colin Cross8a497952019-03-05 22:25:09 -0800635
636 var expandedExcludes []string
Liz Kammer619be462022-01-28 15:13:39 -0500637 if input.ExcludePaths != nil {
638 expandedExcludes = make([]string, 0, len(input.ExcludePaths))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700639 }
Colin Cross8a497952019-03-05 22:25:09 -0800640
Colin Crossba71a3f2019-03-18 12:12:48 -0700641 var missingExcludeDeps []string
Liz Kammer619be462022-01-28 15:13:39 -0500642 for _, e := range input.ExcludePaths {
Colin Cross41955e82019-05-29 14:40:35 -0700643 if m, t := SrcIsModuleWithTag(e); m != "" {
Liz Kammer619be462022-01-28 15:13:39 -0500644 modulePaths, err := getPathsFromModuleDep(input.Context, e, m, t)
Liz Kammera830f3a2020-11-10 10:50:34 -0800645 if m, ok := err.(missingDependencyError); ok {
646 missingExcludeDeps = append(missingExcludeDeps, m.missingDeps...)
647 } else if err != nil {
Liz Kammer619be462022-01-28 15:13:39 -0500648 reportPathError(input.Context, err)
Colin Cross8a497952019-03-05 22:25:09 -0800649 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800650 expandedExcludes = append(expandedExcludes, modulePaths.Strings()...)
Colin Cross8a497952019-03-05 22:25:09 -0800651 }
652 } else {
653 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
654 }
655 }
656
Liz Kammer619be462022-01-28 15:13:39 -0500657 if input.Paths == nil {
Colin Crossba71a3f2019-03-18 12:12:48 -0700658 return nil, missingExcludeDeps
Colin Cross8a497952019-03-05 22:25:09 -0800659 }
660
Colin Crossba71a3f2019-03-18 12:12:48 -0700661 var missingDeps []string
662
Liz Kammer619be462022-01-28 15:13:39 -0500663 expandedSrcFiles := make(Paths, 0, len(input.Paths))
664 for _, s := range input.Paths {
665 srcFiles, err := expandOneSrcPath(sourcePathInput{
666 context: input.Context,
667 path: s,
668 expandedExcludes: expandedExcludes,
669 includeDirs: input.IncludeDirs,
670 })
Colin Cross8a497952019-03-05 22:25:09 -0800671 if depErr, ok := err.(missingDependencyError); ok {
Colin Crossba71a3f2019-03-18 12:12:48 -0700672 missingDeps = append(missingDeps, depErr.missingDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800673 } else if err != nil {
Liz Kammer619be462022-01-28 15:13:39 -0500674 reportPathError(input.Context, err)
Colin Cross8a497952019-03-05 22:25:09 -0800675 }
676 expandedSrcFiles = append(expandedSrcFiles, srcFiles...)
677 }
Colin Crossba71a3f2019-03-18 12:12:48 -0700678
Jihoon Kang0e3a5352024-04-12 00:45:50 +0000679 // TODO: b/334169722 - Replace with an error instead of implicitly removing duplicates.
680 return FirstUniquePaths(expandedSrcFiles), append(missingDeps, missingExcludeDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800681}
682
683type missingDependencyError struct {
684 missingDeps []string
685}
686
687func (e missingDependencyError) Error() string {
688 return "missing dependencies: " + strings.Join(e.missingDeps, ", ")
689}
690
Liz Kammer619be462022-01-28 15:13:39 -0500691type sourcePathInput struct {
692 context ModuleWithDepsPathContext
693 path string
694 expandedExcludes []string
695 includeDirs bool
696}
697
Liz Kammera830f3a2020-11-10 10:50:34 -0800698// Expands one path string to Paths rooted from the module's local source
699// directory, excluding those listed in the expandedExcludes.
700// Expands globs, references to SourceFileProducer or OutputFileProducer modules using the ":name" and ":name{.tag}" syntax.
Liz Kammer619be462022-01-28 15:13:39 -0500701func expandOneSrcPath(input sourcePathInput) (Paths, error) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900702 excludePaths := func(paths Paths) Paths {
Liz Kammer619be462022-01-28 15:13:39 -0500703 if len(input.expandedExcludes) == 0 {
Jooyung Han7607dd32020-07-05 10:23:14 +0900704 return paths
705 }
706 remainder := make(Paths, 0, len(paths))
707 for _, p := range paths {
Liz Kammer619be462022-01-28 15:13:39 -0500708 if !InList(p.String(), input.expandedExcludes) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900709 remainder = append(remainder, p)
710 }
711 }
712 return remainder
713 }
Liz Kammer619be462022-01-28 15:13:39 -0500714 if m, t := SrcIsModuleWithTag(input.path); m != "" {
715 modulePaths, err := getPathsFromModuleDep(input.context, input.path, m, t)
Liz Kammera830f3a2020-11-10 10:50:34 -0800716 if err != nil {
717 return nil, err
Colin Cross8a497952019-03-05 22:25:09 -0800718 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800719 return excludePaths(modulePaths), nil
Colin Cross8a497952019-03-05 22:25:09 -0800720 }
Colin Cross8a497952019-03-05 22:25:09 -0800721 } else {
Liz Kammer619be462022-01-28 15:13:39 -0500722 p := pathForModuleSrc(input.context, input.path)
723 if pathtools.IsGlob(input.path) {
724 paths := GlobFiles(input.context, p.String(), input.expandedExcludes)
725 return PathsWithModuleSrcSubDir(input.context, paths, ""), nil
726 } else {
727 if exists, _, err := input.context.Config().fs.Exists(p.String()); err != nil {
728 ReportPathErrorf(input.context, "%s: %s", p, err.Error())
729 } else if !exists && !input.context.Config().TestAllowNonExistentPaths {
730 ReportPathErrorf(input.context, "module source path %q does not exist", p)
731 } else if !input.includeDirs {
732 if isDir, err := input.context.Config().fs.IsDir(p.String()); exists && err != nil {
733 ReportPathErrorf(input.context, "%s: %s", p, err.Error())
734 } else if isDir {
735 ReportPathErrorf(input.context, "module source path %q is a directory", p)
736 }
737 }
Colin Cross8a497952019-03-05 22:25:09 -0800738
Liz Kammer619be462022-01-28 15:13:39 -0500739 if InList(p.String(), input.expandedExcludes) {
740 return nil, nil
741 }
742 return Paths{p}, nil
Colin Cross8a497952019-03-05 22:25:09 -0800743 }
Colin Cross8a497952019-03-05 22:25:09 -0800744 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700745}
746
747// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
748// source directory, but strip the local source directory from the beginning of
Dan Willemsen540a78c2018-02-26 21:50:08 -0800749// each string. If incDirs is false, strip paths with a trailing '/' from the list.
Colin Crossfe4bc362018-09-12 10:02:13 -0700750// It intended for use in globs that only list files that exist, so it allows '$' in
751// filenames.
Liz Kammera830f3a2020-11-10 10:50:34 -0800752func pathsForModuleSrcFromFullPath(ctx EarlyModulePathContext, paths []string, incDirs bool) Paths {
Lukacs T. Berkif7e36d82021-08-16 17:05:09 +0200753 prefix := ctx.ModuleDir() + "/"
Colin Cross0f37af02017-09-27 17:42:05 -0700754 if prefix == "./" {
755 prefix = ""
756 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700757 ret := make(Paths, 0, len(paths))
758 for _, p := range paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -0800759 if !incDirs && strings.HasSuffix(p, "/") {
760 continue
761 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700762 path := filepath.Clean(p)
763 if !strings.HasPrefix(path, prefix) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100764 ReportPathErrorf(ctx, "Path %q is not in module source directory %q", p, prefix)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700765 continue
766 }
Colin Crosse3924e12018-08-15 20:18:53 -0700767
Colin Crossfe4bc362018-09-12 10:02:13 -0700768 srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
Colin Crosse3924e12018-08-15 20:18:53 -0700769 if err != nil {
770 reportPathError(ctx, err)
771 continue
772 }
773
Colin Cross07e51612019-03-05 12:46:40 -0800774 srcPath.basePath.rel = srcPath.path
Colin Crosse3924e12018-08-15 20:18:53 -0700775
Colin Cross07e51612019-03-05 12:46:40 -0800776 ret = append(ret, srcPath)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700777 }
778 return ret
779}
780
Liz Kammera830f3a2020-11-10 10:50:34 -0800781// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's local source
782// directory. If input is nil, use the default if it exists. If input is empty, returns nil.
783func PathsWithOptionalDefaultForModuleSrc(ctx ModuleMissingDepsPathContext, input []string, def string) Paths {
Colin Cross0ddae7f2019-02-07 15:30:01 -0800784 if input != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700785 return PathsForModuleSrc(ctx, input)
786 }
787 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
788 // is created, we're run again.
Lukacs T. Berkif7e36d82021-08-16 17:05:09 +0200789 path := filepath.Join(ctx.ModuleDir(), def)
Liz Kammera830f3a2020-11-10 10:50:34 -0800790 return Glob(ctx, path, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700791}
792
793// Strings returns the Paths in string form
794func (p Paths) Strings() []string {
795 if p == nil {
796 return nil
797 }
798 ret := make([]string, len(p))
799 for i, path := range p {
800 ret[i] = path.String()
801 }
802 return ret
803}
804
Colin Crossc0efd1d2020-07-03 11:56:24 -0700805func CopyOfPaths(paths Paths) Paths {
806 return append(Paths(nil), paths...)
807}
808
Colin Crossb6715442017-10-24 11:13:31 -0700809// FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
810// modifies the Paths slice contents in place, and returns a subslice of the original slice.
Dan Willemsenfe92c962017-08-29 12:28:37 -0700811func FirstUniquePaths(list Paths) Paths {
Colin Cross27027c72020-02-28 15:34:17 -0800812 // 128 was chosen based on BenchmarkFirstUniquePaths results.
813 if len(list) > 128 {
814 return firstUniquePathsMap(list)
815 }
816 return firstUniquePathsList(list)
817}
818
Colin Crossc0efd1d2020-07-03 11:56:24 -0700819// SortedUniquePaths returns all unique elements of a Paths in sorted order. It modifies the
820// Paths slice contents in place, and returns a subslice of the original slice.
Jiyong Park33c77362020-05-29 22:00:16 +0900821func SortedUniquePaths(list Paths) Paths {
822 unique := FirstUniquePaths(list)
823 sort.Slice(unique, func(i, j int) bool {
824 return unique[i].String() < unique[j].String()
825 })
826 return unique
827}
828
Colin Cross27027c72020-02-28 15:34:17 -0800829func firstUniquePathsList(list Paths) Paths {
Dan Willemsenfe92c962017-08-29 12:28:37 -0700830 k := 0
831outer:
832 for i := 0; i < len(list); i++ {
833 for j := 0; j < k; j++ {
834 if list[i] == list[j] {
835 continue outer
836 }
837 }
838 list[k] = list[i]
839 k++
840 }
841 return list[:k]
842}
843
Colin Cross27027c72020-02-28 15:34:17 -0800844func firstUniquePathsMap(list Paths) Paths {
845 k := 0
846 seen := make(map[Path]bool, len(list))
847 for i := 0; i < len(list); i++ {
848 if seen[list[i]] {
849 continue
850 }
851 seen[list[i]] = true
852 list[k] = list[i]
853 k++
854 }
855 return list[:k]
856}
857
Colin Cross5d583952020-11-24 16:21:24 -0800858// FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each. It
859// modifies the InstallPaths slice contents in place, and returns a subslice of the original slice.
860func FirstUniqueInstallPaths(list InstallPaths) InstallPaths {
861 // 128 was chosen based on BenchmarkFirstUniquePaths results.
862 if len(list) > 128 {
863 return firstUniqueInstallPathsMap(list)
864 }
865 return firstUniqueInstallPathsList(list)
866}
867
868func firstUniqueInstallPathsList(list InstallPaths) InstallPaths {
869 k := 0
870outer:
871 for i := 0; i < len(list); i++ {
872 for j := 0; j < k; j++ {
873 if list[i] == list[j] {
874 continue outer
875 }
876 }
877 list[k] = list[i]
878 k++
879 }
880 return list[:k]
881}
882
883func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths {
884 k := 0
885 seen := make(map[InstallPath]bool, len(list))
886 for i := 0; i < len(list); i++ {
887 if seen[list[i]] {
888 continue
889 }
890 seen[list[i]] = true
891 list[k] = list[i]
892 k++
893 }
894 return list[:k]
895}
896
Colin Crossb6715442017-10-24 11:13:31 -0700897// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
898// modifies the Paths slice contents in place, and returns a subslice of the original slice.
899func LastUniquePaths(list Paths) Paths {
900 totalSkip := 0
901 for i := len(list) - 1; i >= totalSkip; i-- {
902 skip := 0
903 for j := i - 1; j >= totalSkip; j-- {
904 if list[i] == list[j] {
905 skip++
906 } else {
907 list[j+skip] = list[j]
908 }
909 }
910 totalSkip += skip
911 }
912 return list[totalSkip:]
913}
914
Colin Crossa140bb02018-04-17 10:52:26 -0700915// ReversePaths returns a copy of a Paths in reverse order.
916func ReversePaths(list Paths) Paths {
917 if list == nil {
918 return nil
919 }
920 ret := make(Paths, len(list))
921 for i := range list {
922 ret[i] = list[len(list)-1-i]
923 }
924 return ret
925}
926
Jeff Gaston294356f2017-09-27 17:05:30 -0700927func indexPathList(s Path, list []Path) int {
928 for i, l := range list {
929 if l == s {
930 return i
931 }
932 }
933
934 return -1
935}
936
937func inPathList(p Path, list []Path) bool {
938 return indexPathList(p, list) != -1
939}
940
941func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000942 return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) })
943}
944
945func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700946 for _, l := range list {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000947 if predicate(l) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700948 filtered = append(filtered, l)
949 } else {
950 remainder = append(remainder, l)
951 }
952 }
953
954 return
955}
956
Colin Cross93e85952017-08-15 13:34:18 -0700957// HasExt returns true of any of the paths have extension ext, otherwise false
958func (p Paths) HasExt(ext string) bool {
959 for _, path := range p {
960 if path.Ext() == ext {
961 return true
962 }
963 }
964
965 return false
966}
967
968// FilterByExt returns the subset of the paths that have extension ext
969func (p Paths) FilterByExt(ext string) Paths {
970 ret := make(Paths, 0, len(p))
971 for _, path := range p {
972 if path.Ext() == ext {
973 ret = append(ret, path)
974 }
975 }
976 return ret
977}
978
979// FilterOutByExt returns the subset of the paths that do not have extension ext
980func (p Paths) FilterOutByExt(ext string) Paths {
981 ret := make(Paths, 0, len(p))
982 for _, path := range p {
983 if path.Ext() != ext {
984 ret = append(ret, path)
985 }
986 }
987 return ret
988}
989
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700990// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
991// (including subdirectories) are in a contiguous subslice of the list, and can be found in
992// O(log(N)) time using a binary search on the directory prefix.
993type DirectorySortedPaths Paths
994
995func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
996 ret := append(DirectorySortedPaths(nil), paths...)
997 sort.Slice(ret, func(i, j int) bool {
998 return ret[i].String() < ret[j].String()
999 })
1000 return ret
1001}
1002
1003// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
1004// that are in the specified directory and its subdirectories.
1005func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
1006 prefix := filepath.Clean(dir) + "/"
1007 start := sort.Search(len(p), func(i int) bool {
1008 return prefix < p[i].String()
1009 })
1010
1011 ret := p[start:]
1012
1013 end := sort.Search(len(ret), func(i int) bool {
1014 return !strings.HasPrefix(ret[i].String(), prefix)
1015 })
1016
1017 ret = ret[:end]
1018
1019 return Paths(ret)
1020}
1021
Alex Humesky29e3bbe2020-11-20 21:30:13 -05001022// WritablePaths is a slice of WritablePath, used for multiple outputs.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001023type WritablePaths []WritablePath
1024
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001025// RelativeToTop creates a new WritablePaths containing the result of calling Path.RelativeToTop on
1026// each item in this slice.
1027func (p WritablePaths) RelativeToTop() WritablePaths {
1028 ensureTestOnly()
1029 if p == nil {
1030 return p
1031 }
1032 ret := make(WritablePaths, len(p))
1033 for i, path := range p {
1034 ret[i] = path.RelativeToTop().(WritablePath)
1035 }
1036 return ret
1037}
1038
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001039// Strings returns the string forms of the writable paths.
1040func (p WritablePaths) Strings() []string {
1041 if p == nil {
1042 return nil
1043 }
1044 ret := make([]string, len(p))
1045 for i, path := range p {
1046 ret[i] = path.String()
1047 }
1048 return ret
1049}
1050
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001051// Paths returns the WritablePaths as a Paths
1052func (p WritablePaths) Paths() Paths {
1053 if p == nil {
1054 return nil
1055 }
1056 ret := make(Paths, len(p))
1057 for i, path := range p {
1058 ret[i] = path
1059 }
1060 return ret
1061}
1062
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001063type basePath struct {
Paul Duffin74abc5d2021-03-24 09:24:59 +00001064 path string
1065 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001066}
1067
Yu Liufa297642024-06-11 00:13:02 +00001068func (p basePath) GobEncode() ([]byte, error) {
1069 w := new(bytes.Buffer)
1070 encoder := gob.NewEncoder(w)
1071 err := errors.Join(encoder.Encode(p.path), encoder.Encode(p.rel))
1072 if err != nil {
1073 return nil, err
1074 }
1075
1076 return w.Bytes(), nil
1077}
1078
1079func (p *basePath) GobDecode(data []byte) error {
1080 r := bytes.NewBuffer(data)
1081 decoder := gob.NewDecoder(r)
1082 err := errors.Join(decoder.Decode(&p.path), decoder.Decode(&p.rel))
1083 if err != nil {
1084 return err
1085 }
1086
1087 return nil
1088}
1089
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001090func (p basePath) Ext() string {
1091 return filepath.Ext(p.path)
1092}
1093
Colin Cross4f6fc9c2016-10-26 10:05:25 -07001094func (p basePath) Base() string {
1095 return filepath.Base(p.path)
1096}
1097
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001098func (p basePath) Rel() string {
1099 if p.rel != "" {
1100 return p.rel
1101 }
1102 return p.path
1103}
1104
Colin Cross0875c522017-11-28 17:34:01 -08001105func (p basePath) String() string {
1106 return p.path
1107}
1108
Colin Cross0db55682017-12-05 15:36:55 -08001109func (p basePath) withRel(rel string) basePath {
1110 p.path = filepath.Join(p.path, rel)
1111 p.rel = rel
1112 return p
1113}
1114
Colin Cross7707b242024-07-26 12:02:36 -07001115func (p basePath) withoutRel() basePath {
1116 p.rel = filepath.Base(p.path)
1117 return p
1118}
1119
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001120// SourcePath is a Path representing a file path rooted from SrcDir
1121type SourcePath struct {
1122 basePath
1123}
1124
1125var _ Path = SourcePath{}
1126
Colin Cross0db55682017-12-05 15:36:55 -08001127func (p SourcePath) withRel(rel string) SourcePath {
1128 p.basePath = p.basePath.withRel(rel)
1129 return p
1130}
1131
Colin Crossbd73d0d2024-07-26 12:00:33 -07001132func (p SourcePath) RelativeToTop() Path {
1133 ensureTestOnly()
1134 return p
1135}
1136
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001137// safePathForSource is for paths that we expect are safe -- only for use by go
1138// code that is embedding ninja variables in paths
Colin Crossfe4bc362018-09-12 10:02:13 -07001139func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
1140 p, err := validateSafePath(pathComponents...)
Cole Faust483d1f72023-01-09 14:35:27 -08001141 ret := SourcePath{basePath{p, ""}}
Colin Crossfe4bc362018-09-12 10:02:13 -07001142 if err != nil {
1143 return ret, err
1144 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001145
Colin Cross7b3dcc32019-01-24 13:14:39 -08001146 // absolute path already checked by validateSafePath
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001147 // special-case api surface gen files for now
1148 if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
Mikhail Naganovab1f5182019-02-08 13:17:55 -08001149 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -07001150 }
1151
Colin Crossfe4bc362018-09-12 10:02:13 -07001152 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001153}
1154
Colin Cross192e97a2018-02-22 14:21:02 -08001155// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
1156func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
Colin Crossc48c1432018-02-23 07:09:01 +00001157 p, err := validatePath(pathComponents...)
Cole Faust483d1f72023-01-09 14:35:27 -08001158 ret := SourcePath{basePath{p, ""}}
Colin Cross94a32102018-02-22 14:21:02 -08001159 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -08001160 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -08001161 }
1162
Colin Cross7b3dcc32019-01-24 13:14:39 -08001163 // absolute path already checked by validatePath
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001164 // special-case for now
1165 if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
Mikhail Naganovab1f5182019-02-08 13:17:55 -08001166 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Crossc48c1432018-02-23 07:09:01 +00001167 }
1168
Colin Cross192e97a2018-02-22 14:21:02 -08001169 return ret, nil
1170}
1171
Sam Mortimere0fc32b2019-09-05 15:16:13 -07001172// pathForSourceRelaxed creates a SourcePath from pathComponents, but does not check that it exists.
1173// It differs from pathForSource in that the path is allowed to exist outside of the PathContext.
1174func pathForSourceRelaxed(ctx PathContext, pathComponents ...string) (SourcePath, error) {
1175 p := filepath.Join(pathComponents...)
1176 ret := SourcePath{basePath{p, ""}}
1177
1178 abs, err := filepath.Abs(ret.String())
1179 if err != nil {
1180 return ret, err
1181 }
1182 buildroot, err := filepath.Abs(ctx.Config().soongOutDir)
1183 if err != nil {
1184 return ret, err
1185 }
1186 if strings.HasPrefix(abs, buildroot) {
1187 return ret, fmt.Errorf("source path %s is in output", abs)
1188 }
1189
1190 if pathtools.IsGlob(ret.String()) {
1191 return ret, fmt.Errorf("path may not contain a glob: %s", ret.String())
1192 }
1193
1194 return ret, nil
1195}
1196
Colin Cross192e97a2018-02-22 14:21:02 -08001197// existsWithDependencies returns true if the path exists, and adds appropriate dependencies to rerun if the
1198// path does not exist.
Colin Cross662d6142022-11-03 20:38:01 -07001199func existsWithDependencies(ctx PathGlobContext, path SourcePath) (exists bool, err error) {
Colin Cross192e97a2018-02-22 14:21:02 -08001200 var files []string
1201
Colin Cross662d6142022-11-03 20:38:01 -07001202 // Use glob to produce proper dependencies, even though we only want
1203 // a single file.
1204 files, err = ctx.GlobWithDeps(path.String(), nil)
Colin Cross192e97a2018-02-22 14:21:02 -08001205
1206 if err != nil {
1207 return false, fmt.Errorf("glob: %s", err.Error())
1208 }
1209
1210 return len(files) > 0, nil
1211}
1212
1213// PathForSource joins the provided path components and validates that the result
1214// neither escapes the source dir nor is in the out dir.
1215// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
1216func PathForSource(ctx PathContext, pathComponents ...string) SourcePath {
1217 path, err := pathForSource(ctx, pathComponents...)
1218 if err != nil {
1219 reportPathError(ctx, err)
1220 }
1221
Colin Crosse3924e12018-08-15 20:18:53 -07001222 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001223 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -07001224 }
1225
Liz Kammera830f3a2020-11-10 10:50:34 -08001226 if modCtx, ok := ctx.(ModuleMissingDepsPathContext); ok && ctx.Config().AllowMissingDependencies() {
Colin Cross662d6142022-11-03 20:38:01 -07001227 exists, err := existsWithDependencies(modCtx, path)
Colin Cross192e97a2018-02-22 14:21:02 -08001228 if err != nil {
1229 reportPathError(ctx, err)
1230 }
1231 if !exists {
1232 modCtx.AddMissingDependencies([]string{path.String()})
1233 }
Colin Cross988414c2020-01-11 01:11:46 +00001234 } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001235 ReportPathErrorf(ctx, "%s: %s", path, err.Error())
Pedro Loureiro5d190cc2021-02-15 15:41:33 +00001236 } else if !exists && !ctx.Config().TestAllowNonExistentPaths {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001237 ReportPathErrorf(ctx, "source path %q does not exist", path)
Colin Cross192e97a2018-02-22 14:21:02 -08001238 }
1239 return path
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001240}
1241
Sam Mortimere0fc32b2019-09-05 15:16:13 -07001242// PathForSourceRelaxed joins the provided path components. Unlike PathForSource,
1243// the result is allowed to exist outside of the source dir.
1244// On error, it will return a usable, but invalid SourcePath, and report a ModuleError.
1245func PathForSourceRelaxed(ctx PathContext, pathComponents ...string) SourcePath {
1246 path, err := pathForSourceRelaxed(ctx, pathComponents...)
1247 if err != nil {
1248 reportPathError(ctx, err)
1249 }
1250
1251 if modCtx, ok := ctx.(ModuleContext); ok && ctx.Config().AllowMissingDependencies() {
1252 exists, err := existsWithDependencies(modCtx, path)
1253 if err != nil {
1254 reportPathError(ctx, err)
1255 }
1256 if !exists {
1257 modCtx.AddMissingDependencies([]string{path.String()})
1258 }
1259 } else if exists, _, err := ctx.Config().fs.Exists(path.String()); err != nil {
1260 ReportPathErrorf(ctx, "%s: %s", path, err.Error())
1261 } else if !exists {
1262 ReportPathErrorf(ctx, "source path %s does not exist", path)
1263 }
1264 return path
1265}
1266
Cole Faustbc65a3f2023-08-01 16:38:55 +00001267// PathForArbitraryOutput creates a path for the given components. Unlike PathForOutput,
1268// the path is relative to the root of the output folder, not the out/soong folder.
1269func PathForArbitraryOutput(ctx PathContext, pathComponents ...string) Path {
Colin Cross3b1c6842024-07-26 11:52:57 -07001270 path, err := validatePath(pathComponents...)
Cole Faustbc65a3f2023-08-01 16:38:55 +00001271 if err != nil {
1272 reportPathError(ctx, err)
1273 }
Colin Cross3b1c6842024-07-26 11:52:57 -07001274 fullPath := filepath.Join(ctx.Config().OutDir(), path)
1275 path = fullPath[len(fullPath)-len(path):]
1276 return OutputPath{basePath{path, ""}, ctx.Config().OutDir(), fullPath}
Cole Faustbc65a3f2023-08-01 16:38:55 +00001277}
1278
Spandan Dasc6c10fa2022-10-21 21:52:13 +00001279// MaybeExistentPathForSource joins the provided path components and validates that the result
1280// neither escapes the source dir nor is in the out dir.
1281// It does not validate whether the path exists.
1282func MaybeExistentPathForSource(ctx PathContext, pathComponents ...string) SourcePath {
1283 path, err := pathForSource(ctx, pathComponents...)
1284 if err != nil {
1285 reportPathError(ctx, err)
1286 }
1287
1288 if pathtools.IsGlob(path.String()) {
1289 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
1290 }
1291 return path
1292}
1293
Liz Kammer7aa52882021-02-11 09:16:14 -05001294// ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not*
1295// rooted from the module's local source directory, if the path exists, or an empty OptionalPath if
1296// it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state
1297// of the path changes.
Colin Cross662d6142022-11-03 20:38:01 -07001298func ExistentPathForSource(ctx PathGlobContext, pathComponents ...string) OptionalPath {
Colin Cross192e97a2018-02-22 14:21:02 -08001299 path, err := pathForSource(ctx, pathComponents...)
Colin Cross1ccfcc32018-02-22 13:54:26 -08001300 if err != nil {
1301 reportPathError(ctx, err)
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001302 // No need to put the error message into the returned path since it has been reported already.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001303 return OptionalPath{}
1304 }
Colin Crossc48c1432018-02-23 07:09:01 +00001305
Colin Crosse3924e12018-08-15 20:18:53 -07001306 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001307 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -07001308 return OptionalPath{}
1309 }
1310
Colin Cross192e97a2018-02-22 14:21:02 -08001311 exists, err := existsWithDependencies(ctx, path)
Colin Crossc48c1432018-02-23 07:09:01 +00001312 if err != nil {
1313 reportPathError(ctx, err)
1314 return OptionalPath{}
1315 }
Colin Cross192e97a2018-02-22 14:21:02 -08001316 if !exists {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001317 return InvalidOptionalPath(path.String() + " does not exist")
Colin Crossc48c1432018-02-23 07:09:01 +00001318 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001319 return OptionalPathForPath(path)
1320}
1321
1322func (p SourcePath) String() string {
Cole Faust483d1f72023-01-09 14:35:27 -08001323 if p.path == "" {
1324 return "."
1325 }
1326 return p.path
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001327}
1328
Colin Cross7707b242024-07-26 12:02:36 -07001329func (p SourcePath) WithoutRel() Path {
1330 p.basePath = p.basePath.withoutRel()
1331 return p
1332}
1333
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001334// Join creates a new SourcePath with paths... joined with the current path. The
1335// provided paths... may not use '..' to escape from the current path.
1336func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001337 path, err := validatePath(paths...)
1338 if err != nil {
1339 reportPathError(ctx, err)
1340 }
Colin Cross0db55682017-12-05 15:36:55 -08001341 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001342}
1343
Colin Cross2fafa3e2019-03-05 12:39:51 -08001344// join is like Join but does less path validation.
1345func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath {
1346 path, err := validateSafePath(paths...)
1347 if err != nil {
1348 reportPathError(ctx, err)
1349 }
1350 return p.withRel(path)
1351}
1352
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001353// OverlayPath returns the overlay for `path' if it exists. This assumes that the
1354// SourcePath is the path to a resource overlay directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001355func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001356 var relDir string
Colin Cross07e51612019-03-05 12:46:40 -08001357 if srcPath, ok := path.(SourcePath); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001358 relDir = srcPath.path
1359 } else {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001360 ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001361 // No need to put the error message into the returned path since it has been reported already.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001362 return OptionalPath{}
1363 }
Cole Faust483d1f72023-01-09 14:35:27 -08001364 dir := filepath.Join(p.path, relDir)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001365 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -07001366 if pathtools.IsGlob(dir) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001367 ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
Dan Willemsen7b310ee2015-12-18 15:11:17 -08001368 }
Colin Cross461b4452018-02-23 09:22:42 -08001369 paths, err := ctx.GlobWithDeps(dir, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001370 if err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001371 ReportPathErrorf(ctx, "glob: %s", err.Error())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001372 return OptionalPath{}
1373 }
1374 if len(paths) == 0 {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001375 return InvalidOptionalPath(dir + " does not exist")
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001376 }
Cole Faust483d1f72023-01-09 14:35:27 -08001377 return OptionalPathForPath(PathForSource(ctx, paths[0]))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001378}
1379
Colin Cross70dda7e2019-10-01 22:05:35 -07001380// OutputPath is a Path representing an intermediates file path rooted from the build directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001381type OutputPath struct {
1382 basePath
Paul Duffind65c58b2021-03-24 09:22:07 +00001383
Colin Cross3b1c6842024-07-26 11:52:57 -07001384 // The base out directory for this path, either Config.SoongOutDir() or Config.OutDir()
1385 outDir string
Paul Duffind65c58b2021-03-24 09:22:07 +00001386
Colin Crossd63c9a72020-01-29 16:52:50 -08001387 fullPath string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001388}
1389
Yu Liufa297642024-06-11 00:13:02 +00001390func (p OutputPath) GobEncode() ([]byte, error) {
1391 w := new(bytes.Buffer)
1392 encoder := gob.NewEncoder(w)
Colin Cross3b1c6842024-07-26 11:52:57 -07001393 err := errors.Join(encoder.Encode(p.basePath), encoder.Encode(p.outDir), encoder.Encode(p.fullPath))
Yu Liufa297642024-06-11 00:13:02 +00001394 if err != nil {
1395 return nil, err
1396 }
1397
1398 return w.Bytes(), nil
1399}
1400
1401func (p *OutputPath) GobDecode(data []byte) error {
1402 r := bytes.NewBuffer(data)
1403 decoder := gob.NewDecoder(r)
Colin Cross3b1c6842024-07-26 11:52:57 -07001404 err := errors.Join(decoder.Decode(&p.basePath), decoder.Decode(&p.outDir), decoder.Decode(&p.fullPath))
Yu Liufa297642024-06-11 00:13:02 +00001405 if err != nil {
1406 return err
1407 }
1408
1409 return nil
1410}
1411
Colin Cross702e0f82017-10-18 17:27:54 -07001412func (p OutputPath) withRel(rel string) OutputPath {
Colin Cross0db55682017-12-05 15:36:55 -08001413 p.basePath = p.basePath.withRel(rel)
Colin Crossd63c9a72020-01-29 16:52:50 -08001414 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross702e0f82017-10-18 17:27:54 -07001415 return p
1416}
1417
Colin Cross7707b242024-07-26 12:02:36 -07001418func (p OutputPath) WithoutRel() Path {
1419 p.basePath = p.basePath.withoutRel()
Colin Cross3063b782018-08-15 11:19:12 -07001420 return p
1421}
1422
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001423func (p OutputPath) getSoongOutDir() string {
Colin Cross3b1c6842024-07-26 11:52:57 -07001424 return p.outDir
Paul Duffin9b478b02019-12-10 13:41:51 +00001425}
1426
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001427func (p OutputPath) RelativeToTop() Path {
1428 return p.outputPathRelativeToTop()
1429}
1430
1431func (p OutputPath) outputPathRelativeToTop() OutputPath {
Colin Cross3b1c6842024-07-26 11:52:57 -07001432 p.fullPath = StringPathRelativeToTop(p.outDir, p.fullPath)
1433 if strings.HasSuffix(p.outDir, testOutSoongSubDir) {
1434 p.outDir = TestOutSoongDir
1435 } else {
1436 // Handle the PathForArbitraryOutput case
1437 p.outDir = testOutDir
1438 }
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001439 return p
1440}
1441
Paul Duffin0267d492021-02-02 10:05:52 +00001442func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
1443 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1444}
1445
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001446var _ Path = OutputPath{}
Paul Duffin9b478b02019-12-10 13:41:51 +00001447var _ WritablePath = OutputPath{}
Paul Duffin0267d492021-02-02 10:05:52 +00001448var _ objPathProvider = OutputPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001449
Chris Parsons8f232a22020-06-23 17:37:05 -04001450// toolDepPath is a Path representing a dependency of the build tool.
1451type toolDepPath struct {
1452 basePath
1453}
1454
Colin Cross7707b242024-07-26 12:02:36 -07001455func (t toolDepPath) WithoutRel() Path {
1456 t.basePath = t.basePath.withoutRel()
1457 return t
1458}
1459
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001460func (t toolDepPath) RelativeToTop() Path {
1461 ensureTestOnly()
1462 return t
1463}
1464
Chris Parsons8f232a22020-06-23 17:37:05 -04001465var _ Path = toolDepPath{}
1466
1467// pathForBuildToolDep returns a toolDepPath representing the given path string.
1468// There is no validation for the path, as it is "trusted": It may fail
1469// normal validation checks. For example, it may be an absolute path.
1470// Only use this function to construct paths for dependencies of the build
1471// tool invocation.
1472func pathForBuildToolDep(ctx PathContext, path string) toolDepPath {
Paul Duffin74abc5d2021-03-24 09:24:59 +00001473 return toolDepPath{basePath{path, ""}}
Chris Parsons8f232a22020-06-23 17:37:05 -04001474}
1475
Jeff Gaston734e3802017-04-10 15:47:24 -07001476// PathForOutput joins the provided paths and returns an OutputPath that is
1477// validated to not escape the build dir.
1478// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
1479func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001480 path, err := validatePath(pathComponents...)
1481 if err != nil {
1482 reportPathError(ctx, err)
1483 }
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001484 fullPath := filepath.Join(ctx.Config().soongOutDir, path)
Colin Crossd63c9a72020-01-29 16:52:50 -08001485 path = fullPath[len(fullPath)-len(path):]
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001486 return OutputPath{basePath{path, ""}, ctx.Config().soongOutDir, fullPath}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001487}
1488
Colin Cross3b1c6842024-07-26 11:52:57 -07001489// PathsForOutput returns Paths rooted from outDir
Colin Cross40e33732019-02-15 11:08:35 -08001490func PathsForOutput(ctx PathContext, paths []string) WritablePaths {
1491 ret := make(WritablePaths, len(paths))
1492 for i, path := range paths {
1493 ret[i] = PathForOutput(ctx, path)
1494 }
1495 return ret
1496}
1497
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001498func (p OutputPath) writablePath() {}
1499
1500func (p OutputPath) String() string {
Colin Crossd63c9a72020-01-29 16:52:50 -08001501 return p.fullPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001502}
1503
1504// Join creates a new OutputPath with paths... joined with the current path. The
1505// provided paths... may not use '..' to escape from the current path.
1506func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001507 path, err := validatePath(paths...)
1508 if err != nil {
1509 reportPathError(ctx, err)
1510 }
Colin Cross0db55682017-12-05 15:36:55 -08001511 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001512}
1513
Colin Cross8854a5a2019-02-11 14:14:16 -08001514// ReplaceExtension creates a new OutputPath with the extension replaced with ext.
1515func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1516 if strings.Contains(ext, "/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001517 ReportPathErrorf(ctx, "extension %q cannot contain /", ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001518 }
1519 ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext))
Colin Cross2cdd5df2019-02-25 10:25:24 -08001520 ret.rel = pathtools.ReplaceExtension(p.rel, ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001521 return ret
1522}
1523
Colin Cross40e33732019-02-15 11:08:35 -08001524// InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths.
1525func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath {
1526 path, err := validatePath(paths...)
1527 if err != nil {
1528 reportPathError(ctx, err)
1529 }
1530
1531 ret := PathForOutput(ctx, filepath.Dir(p.path), path)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001532 ret.rel = filepath.Join(filepath.Dir(p.rel), path)
Colin Cross40e33732019-02-15 11:08:35 -08001533 return ret
1534}
1535
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001536// PathForIntermediates returns an OutputPath representing the top-level
1537// intermediates directory.
1538func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001539 path, err := validatePath(paths...)
1540 if err != nil {
1541 reportPathError(ctx, err)
1542 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001543 return PathForOutput(ctx, ".intermediates", path)
1544}
1545
Colin Cross07e51612019-03-05 12:46:40 -08001546var _ genPathProvider = SourcePath{}
1547var _ objPathProvider = SourcePath{}
1548var _ resPathProvider = SourcePath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001549
Colin Cross07e51612019-03-05 12:46:40 -08001550// PathForModuleSrc returns a Path representing the paths... under the
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001551// module's local source directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001552func PathForModuleSrc(ctx ModuleMissingDepsPathContext, pathComponents ...string) Path {
Paul Duffin407501b2021-07-09 16:56:35 +01001553 // Just join the components textually just to make sure that it does not corrupt a fully qualified
1554 // module reference, e.g. if the pathComponents is "://other:foo" then using filepath.Join() or
1555 // validatePath() will corrupt it, e.g. replace "//" with "/". If the path is not a module
1556 // reference then it will be validated by expandOneSrcPath anyway when it calls expandOneSrcPath.
1557 p := strings.Join(pathComponents, string(filepath.Separator))
Liz Kammer619be462022-01-28 15:13:39 -05001558 paths, err := expandOneSrcPath(sourcePathInput{context: ctx, path: p, includeDirs: true})
Colin Cross8a497952019-03-05 22:25:09 -08001559 if err != nil {
1560 if depErr, ok := err.(missingDependencyError); ok {
1561 if ctx.Config().AllowMissingDependencies() {
1562 ctx.AddMissingDependencies(depErr.missingDeps)
1563 } else {
1564 ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error())
1565 }
1566 } else {
1567 reportPathError(ctx, err)
1568 }
1569 return nil
1570 } else if len(paths) == 0 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001571 ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p)
Colin Cross8a497952019-03-05 22:25:09 -08001572 return nil
1573 } else if len(paths) > 1 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001574 ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths))
Colin Cross8a497952019-03-05 22:25:09 -08001575 }
1576 return paths[0]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001577}
1578
Liz Kammera830f3a2020-11-10 10:50:34 -08001579func pathForModuleSrc(ctx EarlyModulePathContext, paths ...string) SourcePath {
Colin Cross07e51612019-03-05 12:46:40 -08001580 p, err := validatePath(paths...)
1581 if err != nil {
1582 reportPathError(ctx, err)
1583 }
1584
1585 path, err := pathForSource(ctx, ctx.ModuleDir(), p)
1586 if err != nil {
1587 reportPathError(ctx, err)
1588 }
1589
1590 path.basePath.rel = p
1591
1592 return path
1593}
1594
Colin Cross2fafa3e2019-03-05 12:39:51 -08001595// PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path
1596// will return the path relative to subDir in the module's source directory. If any input paths are not located
1597// inside subDir then a path error will be reported.
Liz Kammera830f3a2020-11-10 10:50:34 -08001598func PathsWithModuleSrcSubDir(ctx EarlyModulePathContext, paths Paths, subDir string) Paths {
Colin Cross2fafa3e2019-03-05 12:39:51 -08001599 paths = append(Paths(nil), paths...)
Colin Cross07e51612019-03-05 12:46:40 -08001600 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001601 for i, path := range paths {
1602 rel := Rel(ctx, subDirFullPath.String(), path.String())
1603 paths[i] = subDirFullPath.join(ctx, rel)
1604 }
1605 return paths
1606}
1607
1608// PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the
1609// 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 -08001610func PathWithModuleSrcSubDir(ctx EarlyModulePathContext, path Path, subDir string) Path {
Colin Cross07e51612019-03-05 12:46:40 -08001611 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001612 rel := Rel(ctx, subDirFullPath.String(), path.String())
1613 return subDirFullPath.Join(ctx, rel)
1614}
1615
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001616// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
1617// valid path if p is non-nil.
Liz Kammera830f3a2020-11-10 10:50:34 -08001618func OptionalPathForModuleSrc(ctx ModuleMissingDepsPathContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001619 if p == nil {
1620 return OptionalPath{}
1621 }
1622 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
1623}
1624
Liz Kammera830f3a2020-11-10 10:50:34 -08001625func (p SourcePath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001626 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001627}
1628
yangbill6d032dd2024-04-18 03:05:49 +00001629func (p SourcePath) genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath {
1630 // If Trim_extension being set, force append Output_extension without replace original extension.
1631 if trimExt != "" {
1632 if ext != "" {
1633 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt)+"."+ext)
1634 }
1635 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt))
1636 }
1637 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1638}
1639
Liz Kammera830f3a2020-11-10 10:50:34 -08001640func (p SourcePath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001641 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001642}
1643
Liz Kammera830f3a2020-11-10 10:50:34 -08001644func (p SourcePath) resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001645 // TODO: Use full directory if the new ctx is not the current ctx?
1646 return PathForModuleRes(ctx, p.path, name)
1647}
1648
1649// ModuleOutPath is a Path representing a module's output directory.
1650type ModuleOutPath struct {
1651 OutputPath
1652}
1653
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001654func (p ModuleOutPath) RelativeToTop() Path {
1655 p.OutputPath = p.outputPathRelativeToTop()
1656 return p
1657}
1658
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001659var _ Path = ModuleOutPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001660var _ WritablePath = ModuleOutPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001661
Liz Kammera830f3a2020-11-10 10:50:34 -08001662func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01001663 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1664}
1665
Liz Kammera830f3a2020-11-10 10:50:34 -08001666// ModuleOutPathContext Subset of ModuleContext functions necessary for output path methods.
1667type ModuleOutPathContext interface {
1668 PathContext
1669
1670 ModuleName() string
1671 ModuleDir() string
1672 ModuleSubDir() string
1673}
1674
1675func pathForModuleOut(ctx ModuleOutPathContext) OutputPath {
Inseob Kimb7e9f5f2024-06-25 17:39:52 +09001676 return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
Colin Cross702e0f82017-10-18 17:27:54 -07001677}
1678
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001679// PathForModuleOut returns a Path representing the paths... under the module's
1680// output directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001681func PathForModuleOut(ctx ModuleOutPathContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001682 p, err := validatePath(paths...)
1683 if err != nil {
1684 reportPathError(ctx, err)
1685 }
Colin Cross702e0f82017-10-18 17:27:54 -07001686 return ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001687 OutputPath: pathForModuleOut(ctx).withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001688 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001689}
1690
1691// ModuleGenPath is a Path representing the 'gen' directory in a module's output
1692// directory. Mainly used for generated sources.
1693type ModuleGenPath struct {
1694 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001695}
1696
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001697func (p ModuleGenPath) RelativeToTop() Path {
1698 p.OutputPath = p.outputPathRelativeToTop()
1699 return p
1700}
1701
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001702var _ Path = ModuleGenPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001703var _ WritablePath = ModuleGenPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001704var _ genPathProvider = ModuleGenPath{}
1705var _ objPathProvider = ModuleGenPath{}
1706
1707// PathForModuleGen returns a Path representing the paths... under the module's
1708// `gen' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001709func PathForModuleGen(ctx ModuleOutPathContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001710 p, err := validatePath(paths...)
1711 if err != nil {
1712 reportPathError(ctx, err)
1713 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001714 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -07001715 ModuleOutPath: ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001716 OutputPath: pathForModuleOut(ctx).withRel("gen").withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001717 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001718 }
1719}
1720
Liz Kammera830f3a2020-11-10 10:50:34 -08001721func (p ModuleGenPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001722 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -07001723 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001724}
1725
yangbill6d032dd2024-04-18 03:05:49 +00001726func (p ModuleGenPath) genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath {
1727 // If Trim_extension being set, force append Output_extension without replace original extension.
1728 if trimExt != "" {
1729 if ext != "" {
1730 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt)+"."+ext)
1731 }
1732 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt))
1733 }
1734 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1735}
1736
Liz Kammera830f3a2020-11-10 10:50:34 -08001737func (p ModuleGenPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001738 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1739}
1740
1741// ModuleObjPath is a Path representing the 'obj' directory in a module's output
1742// directory. Used for compiled objects.
1743type ModuleObjPath struct {
1744 ModuleOutPath
1745}
1746
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001747func (p ModuleObjPath) RelativeToTop() Path {
1748 p.OutputPath = p.outputPathRelativeToTop()
1749 return p
1750}
1751
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001752var _ Path = ModuleObjPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001753var _ WritablePath = ModuleObjPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001754
1755// PathForModuleObj returns a Path representing the paths... under the module's
1756// 'obj' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001757func PathForModuleObj(ctx ModuleOutPathContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001758 p, err := validatePath(pathComponents...)
1759 if err != nil {
1760 reportPathError(ctx, err)
1761 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001762 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
1763}
1764
1765// ModuleResPath is a a Path representing the 'res' directory in a module's
1766// output directory.
1767type ModuleResPath struct {
1768 ModuleOutPath
1769}
1770
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001771func (p ModuleResPath) RelativeToTop() Path {
1772 p.OutputPath = p.outputPathRelativeToTop()
1773 return p
1774}
1775
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001776var _ Path = ModuleResPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001777var _ WritablePath = ModuleResPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001778
1779// PathForModuleRes returns a Path representing the paths... under the module's
1780// 'res' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001781func PathForModuleRes(ctx ModuleOutPathContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001782 p, err := validatePath(pathComponents...)
1783 if err != nil {
1784 reportPathError(ctx, err)
1785 }
1786
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001787 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
1788}
1789
Colin Cross70dda7e2019-10-01 22:05:35 -07001790// InstallPath is a Path representing a installed file path rooted from the build directory
1791type InstallPath struct {
1792 basePath
Colin Crossff6c33d2019-10-02 16:01:35 -07001793
Lukacs T. Berkib078ade2021-08-31 10:42:08 +02001794 // The soong build directory, i.e. Config.SoongOutDir()
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001795 soongOutDir string
Paul Duffind65c58b2021-03-24 09:22:07 +00001796
Jiyong Park957bcd92020-10-20 18:23:33 +09001797 // partitionDir is the part of the InstallPath that is automatically determined according to the context.
1798 // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules.
1799 partitionDir string
1800
Colin Crossb1692a32021-10-25 15:39:01 -07001801 partition string
1802
Jiyong Park957bcd92020-10-20 18:23:33 +09001803 // makePath indicates whether this path is for Soong (false) or Make (true).
1804 makePath bool
Colin Crossc0e42d52024-02-01 16:42:36 -08001805
1806 fullPath string
Colin Cross70dda7e2019-10-01 22:05:35 -07001807}
1808
Yu Liu26a716d2024-08-30 23:40:32 +00001809func (p *InstallPath) GobEncode() ([]byte, error) {
1810 w := new(bytes.Buffer)
1811 encoder := gob.NewEncoder(w)
1812 err := errors.Join(encoder.Encode(p.basePath), encoder.Encode(p.soongOutDir),
1813 encoder.Encode(p.partitionDir), encoder.Encode(p.partition),
1814 encoder.Encode(p.makePath), encoder.Encode(p.fullPath))
1815 if err != nil {
1816 return nil, err
1817 }
1818
1819 return w.Bytes(), nil
1820}
1821
1822func (p *InstallPath) GobDecode(data []byte) error {
1823 r := bytes.NewBuffer(data)
1824 decoder := gob.NewDecoder(r)
1825 err := errors.Join(decoder.Decode(&p.basePath), decoder.Decode(&p.soongOutDir),
1826 decoder.Decode(&p.partitionDir), decoder.Decode(&p.partition),
1827 decoder.Decode(&p.makePath), decoder.Decode(&p.fullPath))
1828 if err != nil {
1829 return err
1830 }
1831
1832 return nil
1833}
1834
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001835// Will panic if called from outside a test environment.
1836func ensureTestOnly() {
Martin Stjernholm32312eb2021-03-27 18:54:49 +00001837 if PrefixInList(os.Args, "-test.") {
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001838 return
1839 }
Martin Stjernholm32312eb2021-03-27 18:54:49 +00001840 panic(fmt.Errorf("Not in test. Command line:\n %s", strings.Join(os.Args, "\n ")))
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001841}
1842
1843func (p InstallPath) RelativeToTop() Path {
1844 ensureTestOnly()
Colin Crossc0e42d52024-02-01 16:42:36 -08001845 if p.makePath {
Colin Cross3b1c6842024-07-26 11:52:57 -07001846 p.soongOutDir = testOutDir
Colin Crossc0e42d52024-02-01 16:42:36 -08001847 } else {
Colin Cross3b1c6842024-07-26 11:52:57 -07001848 p.soongOutDir = TestOutSoongDir
Colin Crossc0e42d52024-02-01 16:42:36 -08001849 }
1850 p.fullPath = filepath.Join(p.soongOutDir, p.path)
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001851 return p
1852}
1853
Colin Cross7707b242024-07-26 12:02:36 -07001854func (p InstallPath) WithoutRel() Path {
1855 p.basePath = p.basePath.withoutRel()
1856 return p
1857}
1858
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001859func (p InstallPath) getSoongOutDir() string {
1860 return p.soongOutDir
Paul Duffin9b478b02019-12-10 13:41:51 +00001861}
1862
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001863func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1864 panic("Not implemented")
1865}
1866
Paul Duffin9b478b02019-12-10 13:41:51 +00001867var _ Path = InstallPath{}
1868var _ WritablePath = InstallPath{}
1869
Colin Cross70dda7e2019-10-01 22:05:35 -07001870func (p InstallPath) writablePath() {}
1871
1872func (p InstallPath) String() string {
Colin Crossc0e42d52024-02-01 16:42:36 -08001873 return p.fullPath
Jiyong Park957bcd92020-10-20 18:23:33 +09001874}
1875
1876// PartitionDir returns the path to the partition where the install path is rooted at. It is
1877// out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules.
1878// The ./soong is dropped if the install path is for Make.
1879func (p InstallPath) PartitionDir() string {
1880 if p.makePath {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001881 return filepath.Join(p.soongOutDir, "../", p.partitionDir)
Jiyong Park957bcd92020-10-20 18:23:33 +09001882 } else {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001883 return filepath.Join(p.soongOutDir, p.partitionDir)
Jiyong Park957bcd92020-10-20 18:23:33 +09001884 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001885}
1886
Jihoon Kangf78a8902022-09-01 22:47:07 +00001887func (p InstallPath) Partition() string {
1888 return p.partition
1889}
1890
Colin Cross70dda7e2019-10-01 22:05:35 -07001891// Join creates a new InstallPath with paths... joined with the current path. The
1892// provided paths... may not use '..' to escape from the current path.
1893func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath {
1894 path, err := validatePath(paths...)
1895 if err != nil {
1896 reportPathError(ctx, err)
1897 }
1898 return p.withRel(path)
1899}
1900
1901func (p InstallPath) withRel(rel string) InstallPath {
1902 p.basePath = p.basePath.withRel(rel)
Colin Crossc0e42d52024-02-01 16:42:36 -08001903 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross70dda7e2019-10-01 22:05:35 -07001904 return p
1905}
1906
Colin Crossc68db4b2021-11-11 18:59:15 -08001907// Deprecated: ToMakePath is a noop, PathForModuleInstall always returns Make paths when building
1908// embedded in Make.
Colin Crossff6c33d2019-10-02 16:01:35 -07001909func (p InstallPath) ToMakePath() InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001910 p.makePath = true
Colin Crossff6c33d2019-10-02 16:01:35 -07001911 return p
Colin Cross70dda7e2019-10-01 22:05:35 -07001912}
1913
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001914// PathForModuleInstall returns a Path representing the install path for the
1915// module appended with paths...
Colin Cross70dda7e2019-10-01 22:05:35 -07001916func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Spandan Das5d1b9292021-06-03 19:36:41 +00001917 os, arch := osAndArch(ctx)
Cole Faust11edf552023-10-13 11:32:14 -07001918 partition := modulePartition(ctx, os.Class == Device)
Cole Faust3b703f32023-10-16 13:30:51 -07001919 return pathForInstall(ctx, os, arch, partition, pathComponents...)
Spandan Das5d1b9292021-06-03 19:36:41 +00001920}
1921
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001922// PathForHostDexInstall returns an InstallPath representing the install path for the
1923// module appended with paths...
1924func PathForHostDexInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Cole Faust3b703f32023-10-16 13:30:51 -07001925 return pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "", pathComponents...)
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001926}
1927
Spandan Das5d1b9292021-06-03 19:36:41 +00001928// PathForModuleInPartitionInstall is similar to PathForModuleInstall but partition is provided by the caller
1929func PathForModuleInPartitionInstall(ctx ModuleInstallPathContext, partition string, pathComponents ...string) InstallPath {
1930 os, arch := osAndArch(ctx)
Cole Faust3b703f32023-10-16 13:30:51 -07001931 return pathForInstall(ctx, os, arch, partition, pathComponents...)
Spandan Das5d1b9292021-06-03 19:36:41 +00001932}
1933
1934func osAndArch(ctx ModuleInstallPathContext) (OsType, ArchType) {
Colin Cross6e359402020-02-10 15:29:54 -08001935 os := ctx.Os()
Jiyong Park87788b52020-09-01 12:37:45 +09001936 arch := ctx.Arch().ArchType
1937 forceOS, forceArch := ctx.InstallForceOS()
1938 if forceOS != nil {
Colin Cross6e359402020-02-10 15:29:54 -08001939 os = *forceOS
1940 }
Jiyong Park87788b52020-09-01 12:37:45 +09001941 if forceArch != nil {
1942 arch = *forceArch
1943 }
Spandan Das5d1b9292021-06-03 19:36:41 +00001944 return os, arch
1945}
Colin Cross609c49a2020-02-13 13:20:11 -08001946
Colin Crossc0e42d52024-02-01 16:42:36 -08001947func pathForPartitionInstallDir(ctx PathContext, partition, partitionPath string, makePath bool) InstallPath {
1948 fullPath := ctx.Config().SoongOutDir()
1949 if makePath {
1950 // Make path starts with out/ instead of out/soong.
1951 fullPath = filepath.Join(fullPath, "../", partitionPath)
1952 } else {
1953 fullPath = filepath.Join(fullPath, partitionPath)
1954 }
1955
1956 return InstallPath{
1957 basePath: basePath{partitionPath, ""},
1958 soongOutDir: ctx.Config().soongOutDir,
1959 partitionDir: partitionPath,
1960 partition: partition,
1961 makePath: makePath,
1962 fullPath: fullPath,
1963 }
1964}
1965
Cole Faust3b703f32023-10-16 13:30:51 -07001966func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string,
Colin Cross609c49a2020-02-13 13:20:11 -08001967 pathComponents ...string) InstallPath {
1968
Jiyong Park97859152023-02-14 17:05:48 +09001969 var partitionPaths []string
Colin Cross609c49a2020-02-13 13:20:11 -08001970
Colin Cross6e359402020-02-10 15:29:54 -08001971 if os.Class == Device {
Jiyong Park97859152023-02-14 17:05:48 +09001972 partitionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001973 } else {
Jiyong Park87788b52020-09-01 12:37:45 +09001974 osName := os.String()
Colin Crossa9b2aac2022-06-15 17:25:51 -07001975 if os == Linux {
Jiyong Park87788b52020-09-01 12:37:45 +09001976 // instead of linux_glibc
1977 osName = "linux"
Dan Willemsen866b5632017-09-22 12:28:24 -07001978 }
Colin Crossa9b2aac2022-06-15 17:25:51 -07001979 if os == LinuxMusl && ctx.Config().UseHostMusl() {
1980 // When using musl instead of glibc, use "linux" instead of "linux_musl". When cross
1981 // compiling we will still use "linux_musl".
1982 osName = "linux"
1983 }
1984
Jiyong Park87788b52020-09-01 12:37:45 +09001985 // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
1986 // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem
1987 // to have a plan to fix it (see the comment in build/make/core/envsetup.mk).
1988 // Let's keep using x86 for the existing cases until we have a need to support
1989 // other architectures.
1990 archName := arch.String()
1991 if os.Class == Host && (arch == X86_64 || arch == Common) {
1992 archName = "x86"
1993 }
Jiyong Park97859152023-02-14 17:05:48 +09001994 partitionPaths = []string{"host", osName + "-" + archName, partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001995 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001996
Jiyong Park97859152023-02-14 17:05:48 +09001997 partitionPath, err := validatePath(partitionPaths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07001998 if err != nil {
1999 reportPathError(ctx, err)
2000 }
Colin Crossff6c33d2019-10-02 16:01:35 -07002001
Colin Crossc0e42d52024-02-01 16:42:36 -08002002 base := pathForPartitionInstallDir(ctx, partition, partitionPath, ctx.Config().KatiEnabled())
Jiyong Park957bcd92020-10-20 18:23:33 +09002003 return base.Join(ctx, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002004}
2005
Spandan Dasf280b232024-04-04 21:25:51 +00002006func PathForNdkInstall(ctx PathContext, paths ...string) OutputPath {
2007 return PathForOutput(ctx, append([]string{"ndk"}, paths...)...)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00002008}
2009
2010func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath {
Spandan Dasf280b232024-04-04 21:25:51 +00002011 base := pathForPartitionInstallDir(ctx, "", "mainline-sdks", false)
2012 return base.Join(ctx, paths...)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00002013}
2014
Colin Cross70dda7e2019-10-01 22:05:35 -07002015func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string {
Colin Crossb1692a32021-10-25 15:39:01 -07002016 rel := Rel(ctx, strings.TrimSuffix(path.PartitionDir(), path.partition), path.String())
Colin Cross43f08db2018-11-12 10:13:39 -08002017 return "/" + rel
2018}
2019
Cole Faust11edf552023-10-13 11:32:14 -07002020func modulePartition(ctx ModuleInstallPathContext, device bool) string {
Colin Cross43f08db2018-11-12 10:13:39 -08002021 var partition string
Colin Cross6e359402020-02-10 15:29:54 -08002022 if ctx.InstallInTestcases() {
2023 // "testcases" install directory can be used for host or device modules.
Jaewoong Jung0949f312019-09-11 10:25:18 -07002024 partition = "testcases"
Cole Faust11edf552023-10-13 11:32:14 -07002025 } else if device {
Colin Cross6e359402020-02-10 15:29:54 -08002026 if ctx.InstallInData() {
2027 partition = "data"
2028 } else if ctx.InstallInRamdisk() {
2029 if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() {
2030 partition = "recovery/root/first_stage_ramdisk"
2031 } else {
2032 partition = "ramdisk"
2033 }
2034 if !ctx.InstallInRoot() {
2035 partition += "/system"
2036 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -07002037 } else if ctx.InstallInVendorRamdisk() {
Yifan Hong39143a92020-10-26 12:43:12 -07002038 // The module is only available after switching root into
2039 // /first_stage_ramdisk. To expose the module before switching root
2040 // on a device without a dedicated recovery partition, install the
2041 // recovery variant.
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002042 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
Petri Gyntherac229562021-03-02 23:44:02 -08002043 partition = "vendor_ramdisk/first_stage_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002044 } else {
Petri Gyntherac229562021-03-02 23:44:02 -08002045 partition = "vendor_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002046 }
2047 if !ctx.InstallInRoot() {
2048 partition += "/system"
2049 }
Inseob Kim08758f02021-04-08 21:13:22 +09002050 } else if ctx.InstallInDebugRamdisk() {
2051 partition = "debug_ramdisk"
Colin Cross6e359402020-02-10 15:29:54 -08002052 } else if ctx.InstallInRecovery() {
2053 if ctx.InstallInRoot() {
2054 partition = "recovery/root"
2055 } else {
2056 // the layout of recovery partion is the same as that of system partition
2057 partition = "recovery/root/system"
2058 }
Colin Crossea30d852023-11-29 16:00:16 -08002059 } else if ctx.SocSpecific() || ctx.InstallInVendor() {
Colin Cross6e359402020-02-10 15:29:54 -08002060 partition = ctx.DeviceConfig().VendorPath()
Colin Crossea30d852023-11-29 16:00:16 -08002061 } else if ctx.DeviceSpecific() || ctx.InstallInOdm() {
Colin Cross6e359402020-02-10 15:29:54 -08002062 partition = ctx.DeviceConfig().OdmPath()
Colin Crossea30d852023-11-29 16:00:16 -08002063 } else if ctx.ProductSpecific() || ctx.InstallInProduct() {
Colin Cross6e359402020-02-10 15:29:54 -08002064 partition = ctx.DeviceConfig().ProductPath()
2065 } else if ctx.SystemExtSpecific() {
2066 partition = ctx.DeviceConfig().SystemExtPath()
2067 } else if ctx.InstallInRoot() {
2068 partition = "root"
Yifan Hong82db7352020-01-21 16:12:26 -08002069 } else {
Colin Cross6e359402020-02-10 15:29:54 -08002070 partition = "system"
Yifan Hong82db7352020-01-21 16:12:26 -08002071 }
Colin Cross6e359402020-02-10 15:29:54 -08002072 if ctx.InstallInSanitizerDir() {
2073 partition = "data/asan/" + partition
Yifan Hong82db7352020-01-21 16:12:26 -08002074 }
Colin Cross43f08db2018-11-12 10:13:39 -08002075 }
2076 return partition
2077}
2078
Colin Cross609c49a2020-02-13 13:20:11 -08002079type InstallPaths []InstallPath
2080
2081// Paths returns the InstallPaths as a Paths
2082func (p InstallPaths) Paths() Paths {
2083 if p == nil {
2084 return nil
2085 }
2086 ret := make(Paths, len(p))
2087 for i, path := range p {
2088 ret[i] = path
2089 }
2090 return ret
2091}
2092
2093// Strings returns the string forms of the install paths.
2094func (p InstallPaths) Strings() []string {
2095 if p == nil {
2096 return nil
2097 }
2098 ret := make([]string, len(p))
2099 for i, path := range p {
2100 ret[i] = path.String()
2101 }
2102 return ret
2103}
2104
Jingwen Chen24d0c562023-02-07 09:29:36 +00002105// validatePathInternal ensures that a path does not leave its component, and
2106// optionally doesn't contain Ninja variables.
2107func validatePathInternal(allowNinjaVariables bool, pathComponents ...string) (string, error) {
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002108 initialEmpty := 0
2109 finalEmpty := 0
2110 for i, path := range pathComponents {
Jingwen Chen24d0c562023-02-07 09:29:36 +00002111 if !allowNinjaVariables && strings.Contains(path, "$") {
2112 return "", fmt.Errorf("Path contains invalid character($): %s", path)
2113 }
2114
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002115 path := filepath.Clean(path)
Cosmin Tanislave79fea22024-03-20 23:16:26 -04002116 if path == ".." || strings.HasPrefix(path, "../") || i != initialEmpty && strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08002117 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002118 }
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002119
2120 if i == initialEmpty && pathComponents[i] == "" {
2121 initialEmpty++
2122 }
2123 if i == finalEmpty && pathComponents[len(pathComponents)-1-i] == "" {
2124 finalEmpty++
2125 }
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002126 }
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002127 // Optimization: filepath.Join("foo", "") returns a newly allocated copy
2128 // of "foo", while filepath.Join("foo") does not. Strip out any empty
2129 // path components.
2130 if initialEmpty == len(pathComponents) {
2131 return "", nil
2132 }
2133 nonEmptyPathComponents := pathComponents[initialEmpty : len(pathComponents)-finalEmpty]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002134 // TODO: filepath.Join isn't necessarily correct with embedded ninja
2135 // variables. '..' may remove the entire ninja variable, even if it
2136 // will be expanded to multiple nested directories.
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002137 return filepath.Join(nonEmptyPathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002138}
2139
Jingwen Chen24d0c562023-02-07 09:29:36 +00002140// validateSafePath validates a path that we trust (may contain ninja
2141// variables). Ensures that each path component does not attempt to leave its
2142// component. Returns a joined version of each path component.
2143func validateSafePath(pathComponents ...string) (string, error) {
2144 return validatePathInternal(true, pathComponents...)
2145}
2146
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002147// validatePath validates that a path does not include ninja variables, and that
2148// each path component does not attempt to leave its component. Returns a joined
2149// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08002150func validatePath(pathComponents ...string) (string, error) {
Jingwen Chen24d0c562023-02-07 09:29:36 +00002151 return validatePathInternal(false, pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -07002152}
Colin Cross5b529592017-05-09 13:34:34 -07002153
Colin Cross0875c522017-11-28 17:34:01 -08002154func PathForPhony(ctx PathContext, phony string) WritablePath {
2155 if strings.ContainsAny(phony, "$/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002156 ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -08002157 }
Paul Duffin74abc5d2021-03-24 09:24:59 +00002158 return PhonyPath{basePath{phony, ""}}
Colin Cross0875c522017-11-28 17:34:01 -08002159}
2160
Colin Cross74e3fe42017-12-11 15:51:44 -08002161type PhonyPath struct {
2162 basePath
2163}
2164
2165func (p PhonyPath) writablePath() {}
2166
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02002167func (p PhonyPath) getSoongOutDir() string {
Paul Duffind65c58b2021-03-24 09:22:07 +00002168 // A phone path cannot contain any / so cannot be relative to the build directory.
2169 return ""
Paul Duffin9b478b02019-12-10 13:41:51 +00002170}
2171
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002172func (p PhonyPath) RelativeToTop() Path {
2173 ensureTestOnly()
2174 // A phony path cannot contain any / so does not have a build directory so switching to a new
2175 // build directory has no effect so just return this path.
2176 return p
2177}
2178
Colin Cross7707b242024-07-26 12:02:36 -07002179func (p PhonyPath) WithoutRel() Path {
2180 p.basePath = p.basePath.withoutRel()
2181 return p
2182}
2183
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01002184func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
2185 panic("Not implemented")
2186}
2187
Colin Cross74e3fe42017-12-11 15:51:44 -08002188var _ Path = PhonyPath{}
2189var _ WritablePath = PhonyPath{}
2190
Colin Cross5b529592017-05-09 13:34:34 -07002191type testPath struct {
2192 basePath
2193}
2194
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002195func (p testPath) RelativeToTop() Path {
2196 ensureTestOnly()
2197 return p
2198}
2199
Colin Cross7707b242024-07-26 12:02:36 -07002200func (p testPath) WithoutRel() Path {
2201 p.basePath = p.basePath.withoutRel()
2202 return p
2203}
2204
Colin Cross5b529592017-05-09 13:34:34 -07002205func (p testPath) String() string {
2206 return p.path
2207}
2208
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002209var _ Path = testPath{}
2210
Colin Cross40e33732019-02-15 11:08:35 -08002211// PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from
2212// within tests.
Colin Cross5b529592017-05-09 13:34:34 -07002213func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -08002214 p, err := validateSafePath(paths...)
2215 if err != nil {
2216 panic(err)
2217 }
Colin Cross5b529592017-05-09 13:34:34 -07002218 return testPath{basePath{path: p, rel: p}}
2219}
2220
Sam Delmerico2351eac2022-05-24 17:10:02 +00002221func PathForTestingWithRel(path, rel string) Path {
2222 p, err := validateSafePath(path, rel)
2223 if err != nil {
2224 panic(err)
2225 }
2226 r, err := validatePath(rel)
2227 if err != nil {
2228 panic(err)
2229 }
2230 return testPath{basePath{path: p, rel: r}}
2231}
2232
Colin Cross40e33732019-02-15 11:08:35 -08002233// PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests.
2234func PathsForTesting(strs ...string) Paths {
Colin Cross5b529592017-05-09 13:34:34 -07002235 p := make(Paths, len(strs))
2236 for i, s := range strs {
2237 p[i] = PathForTesting(s)
2238 }
2239
2240 return p
2241}
Colin Cross43f08db2018-11-12 10:13:39 -08002242
Colin Cross40e33732019-02-15 11:08:35 -08002243type testPathContext struct {
2244 config Config
Colin Cross40e33732019-02-15 11:08:35 -08002245}
2246
Colin Cross40e33732019-02-15 11:08:35 -08002247func (x *testPathContext) Config() Config { return x.config }
2248func (x *testPathContext) AddNinjaFileDeps(...string) {}
2249
2250// PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
2251// PathForOutput.
Colin Cross98be1bb2019-12-13 20:41:13 -08002252func PathContextForTesting(config Config) PathContext {
Colin Cross40e33732019-02-15 11:08:35 -08002253 return &testPathContext{
2254 config: config,
Colin Cross40e33732019-02-15 11:08:35 -08002255 }
2256}
2257
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002258type testModuleInstallPathContext struct {
2259 baseModuleContext
2260
2261 inData bool
2262 inTestcases bool
2263 inSanitizerDir bool
2264 inRamdisk bool
2265 inVendorRamdisk bool
Inseob Kim08758f02021-04-08 21:13:22 +09002266 inDebugRamdisk bool
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002267 inRecovery bool
2268 inRoot bool
Colin Crossea30d852023-11-29 16:00:16 -08002269 inOdm bool
2270 inProduct bool
2271 inVendor bool
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002272 forceOS *OsType
2273 forceArch *ArchType
2274}
2275
2276func (m testModuleInstallPathContext) Config() Config {
2277 return m.baseModuleContext.config
2278}
2279
2280func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {}
2281
2282func (m testModuleInstallPathContext) InstallInData() bool {
2283 return m.inData
2284}
2285
2286func (m testModuleInstallPathContext) InstallInTestcases() bool {
2287 return m.inTestcases
2288}
2289
2290func (m testModuleInstallPathContext) InstallInSanitizerDir() bool {
2291 return m.inSanitizerDir
2292}
2293
2294func (m testModuleInstallPathContext) InstallInRamdisk() bool {
2295 return m.inRamdisk
2296}
2297
2298func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool {
2299 return m.inVendorRamdisk
2300}
2301
Inseob Kim08758f02021-04-08 21:13:22 +09002302func (m testModuleInstallPathContext) InstallInDebugRamdisk() bool {
2303 return m.inDebugRamdisk
2304}
2305
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002306func (m testModuleInstallPathContext) InstallInRecovery() bool {
2307 return m.inRecovery
2308}
2309
2310func (m testModuleInstallPathContext) InstallInRoot() bool {
2311 return m.inRoot
2312}
2313
Colin Crossea30d852023-11-29 16:00:16 -08002314func (m testModuleInstallPathContext) InstallInOdm() bool {
2315 return m.inOdm
2316}
2317
2318func (m testModuleInstallPathContext) InstallInProduct() bool {
2319 return m.inProduct
2320}
2321
2322func (m testModuleInstallPathContext) InstallInVendor() bool {
2323 return m.inVendor
2324}
2325
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002326func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
2327 return m.forceOS, m.forceArch
2328}
2329
2330// Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is
2331// default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are
2332// delegated to it will panic.
2333func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext {
2334 ctx := &testModuleInstallPathContext{}
2335 ctx.config = config
2336 ctx.os = Android
2337 return ctx
2338}
2339
Colin Cross43f08db2018-11-12 10:13:39 -08002340// Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
2341// targetPath is not inside basePath.
2342func Rel(ctx PathContext, basePath string, targetPath string) string {
2343 rel, isRel := MaybeRel(ctx, basePath, targetPath)
2344 if !isRel {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002345 ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
Colin Cross43f08db2018-11-12 10:13:39 -08002346 return ""
2347 }
2348 return rel
2349}
2350
2351// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
2352// targetPath is not inside basePath.
2353func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
Dan Willemsen633c5022019-04-12 11:11:38 -07002354 rel, isRel, err := maybeRelErr(basePath, targetPath)
2355 if err != nil {
2356 reportPathError(ctx, err)
2357 }
2358 return rel, isRel
2359}
2360
2361func maybeRelErr(basePath string, targetPath string) (string, bool, error) {
Colin Cross43f08db2018-11-12 10:13:39 -08002362 // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
2363 if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
Dan Willemsen633c5022019-04-12 11:11:38 -07002364 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002365 }
2366 rel, err := filepath.Rel(basePath, targetPath)
2367 if err != nil {
Dan Willemsen633c5022019-04-12 11:11:38 -07002368 return "", false, err
Colin Cross43f08db2018-11-12 10:13:39 -08002369 } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
Dan Willemsen633c5022019-04-12 11:11:38 -07002370 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002371 }
Dan Willemsen633c5022019-04-12 11:11:38 -07002372 return rel, true, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002373}
Colin Cross988414c2020-01-11 01:11:46 +00002374
2375// Writes a file to the output directory. Attempting to write directly to the output directory
2376// will fail due to the sandbox of the soong_build process.
Chris Parsons1a12d032023-02-06 22:37:41 -05002377// Only writes the file if the file doesn't exist or if it has different contents, to prevent
2378// updating the timestamp if no changes would be made. (This is better for incremental
2379// performance.)
Colin Cross988414c2020-01-11 01:11:46 +00002380func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error {
Colin Crossd6421132021-11-09 12:32:34 -08002381 absPath := absolutePath(path.String())
2382 err := os.MkdirAll(filepath.Dir(absPath), 0777)
2383 if err != nil {
2384 return err
2385 }
Chris Parsons1a12d032023-02-06 22:37:41 -05002386 return pathtools.WriteFileIfChanged(absPath, data, perm)
Colin Cross988414c2020-01-11 01:11:46 +00002387}
2388
Liz Kammer2dd9ca42020-11-25 16:06:39 -08002389func RemoveAllOutputDir(path WritablePath) error {
2390 return os.RemoveAll(absolutePath(path.String()))
2391}
2392
2393func CreateOutputDirIfNonexistent(path WritablePath, perm os.FileMode) error {
2394 dir := absolutePath(path.String())
Liz Kammer09f947d2021-05-12 14:51:49 -04002395 return createDirIfNonexistent(dir, perm)
2396}
2397
2398func createDirIfNonexistent(dir string, perm os.FileMode) error {
Liz Kammer2dd9ca42020-11-25 16:06:39 -08002399 if _, err := os.Stat(dir); os.IsNotExist(err) {
2400 return os.MkdirAll(dir, os.ModePerm)
2401 } else {
2402 return err
2403 }
2404}
2405
Jingwen Chen78257e52021-05-21 02:34:24 +00002406// absolutePath is deliberately private so that Soong's Go plugins can't use it to find and
2407// read arbitrary files without going through the methods in the current package that track
2408// dependencies.
Colin Cross988414c2020-01-11 01:11:46 +00002409func absolutePath(path string) string {
2410 if filepath.IsAbs(path) {
2411 return path
2412 }
2413 return filepath.Join(absSrcDir, path)
2414}
Chris Parsons216e10a2020-07-09 17:12:52 -04002415
2416// A DataPath represents the path of a file to be used as data, for example
2417// a test library to be installed alongside a test.
2418// The data file should be installed (copied from `<SrcPath>`) to
2419// `<install_root>/<RelativeInstallPath>/<filename>`, or
2420// `<install_root>/<filename>` if RelativeInstallPath is empty.
2421type DataPath struct {
2422 // The path of the data file that should be copied into the data directory
2423 SrcPath Path
2424 // The install path of the data file, relative to the install root.
2425 RelativeInstallPath string
Colin Cross5c1d5fb2023-11-15 12:39:40 -08002426 // If WithoutRel is true, use SrcPath.Base() instead of SrcPath.Rel() as the filename.
2427 WithoutRel bool
Chris Parsons216e10a2020-07-09 17:12:52 -04002428}
Colin Crossdcf71b22021-02-01 13:59:03 -08002429
Colin Crossd442a0e2023-11-16 11:19:26 -08002430func (d *DataPath) ToRelativeInstallPath() string {
2431 relPath := d.SrcPath.Rel()
Colin Cross5c1d5fb2023-11-15 12:39:40 -08002432 if d.WithoutRel {
2433 relPath = d.SrcPath.Base()
2434 }
Colin Crossd442a0e2023-11-16 11:19:26 -08002435 if d.RelativeInstallPath != "" {
2436 relPath = filepath.Join(d.RelativeInstallPath, relPath)
2437 }
2438 return relPath
2439}
2440
Colin Crossdcf71b22021-02-01 13:59:03 -08002441// PathsIfNonNil returns a Paths containing only the non-nil input arguments.
2442func PathsIfNonNil(paths ...Path) Paths {
2443 if len(paths) == 0 {
2444 // Fast path for empty argument list
2445 return nil
2446 } else if len(paths) == 1 {
2447 // Fast path for a single argument
2448 if paths[0] != nil {
2449 return paths
2450 } else {
2451 return nil
2452 }
2453 }
2454 ret := make(Paths, 0, len(paths))
2455 for _, path := range paths {
2456 if path != nil {
2457 ret = append(ret, path)
2458 }
2459 }
2460 if len(ret) == 0 {
2461 return nil
2462 }
2463 return ret
2464}
Chris Wailesb2703ad2021-07-30 13:25:42 -07002465
2466var thirdPartyDirPrefixExceptions = []*regexp.Regexp{
2467 regexp.MustCompile("^vendor/[^/]*google[^/]*/"),
2468 regexp.MustCompile("^hardware/google/"),
2469 regexp.MustCompile("^hardware/interfaces/"),
2470 regexp.MustCompile("^hardware/libhardware[^/]*/"),
2471 regexp.MustCompile("^hardware/ril/"),
2472}
2473
2474func IsThirdPartyPath(path string) bool {
2475 thirdPartyDirPrefixes := []string{"external/", "vendor/", "hardware/"}
2476
2477 if HasAnyPrefix(path, thirdPartyDirPrefixes) {
2478 for _, prefix := range thirdPartyDirPrefixExceptions {
2479 if prefix.MatchString(path) {
2480 return false
2481 }
2482 }
2483 return true
2484 }
2485 return false
2486}