blob: 9c2df6589877dc002d5c1a56aeaaee25754f9b79 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross3f40fa42015-01-30 17:27:36 -080016
17import (
Colin Cross6e18ca42015-07-14 18:55:36 -070018 "fmt"
Colin Cross988414c2020-01-11 01:11:46 +000019 "os"
Colin Cross6a745c62015-06-16 16:38:10 -070020 "path/filepath"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070021 "reflect"
Chris Wailesb2703ad2021-07-30 13:25:42 -070022 "regexp"
Colin Cross5e6cfbe2017-11-03 15:20:35 -070023 "sort"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070024 "strings"
25
26 "github.com/google/blueprint"
27 "github.com/google/blueprint/pathtools"
Colin Cross3f40fa42015-01-30 17:27:36 -080028)
29
Colin Cross988414c2020-01-11 01:11:46 +000030var absSrcDir string
31
Dan Willemsen34cc69e2015-09-23 15:26:20 -070032// PathContext is the subset of a (Module|Singleton)Context required by the
33// Path methods.
34type PathContext interface {
Colin Crossaabf6792017-11-29 00:27:14 -080035 Config() Config
Dan Willemsen7b310ee2015-12-18 15:11:17 -080036 AddNinjaFileDeps(deps ...string)
Colin Cross3f40fa42015-01-30 17:27:36 -080037}
38
Colin Cross7f19f372016-11-01 11:10:25 -070039type PathGlobContext interface {
Colin Cross662d6142022-11-03 20:38:01 -070040 PathContext
Colin Cross7f19f372016-11-01 11:10:25 -070041 GlobWithDeps(globPattern string, excludes []string) ([]string, error)
42}
43
Colin Crossaabf6792017-11-29 00:27:14 -080044var _ PathContext = SingletonContext(nil)
45var _ PathContext = ModuleContext(nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -070046
Ulya Trafimovich8640ab92020-05-11 18:06:15 +010047// "Null" path context is a minimal path context for a given config.
48type NullPathContext struct {
49 config Config
50}
51
52func (NullPathContext) AddNinjaFileDeps(...string) {}
53func (ctx NullPathContext) Config() Config { return ctx.config }
54
Liz Kammera830f3a2020-11-10 10:50:34 -080055// EarlyModulePathContext is a subset of EarlyModuleContext methods required by the
56// Path methods. These path methods can be called before any mutators have run.
57type EarlyModulePathContext interface {
Liz Kammera830f3a2020-11-10 10:50:34 -080058 PathGlobContext
59
60 ModuleDir() string
61 ModuleErrorf(fmt string, args ...interface{})
Cole Fausta963b942024-04-11 17:43:00 -070062 OtherModulePropertyErrorf(module Module, property, fmt string, args ...interface{})
Liz Kammera830f3a2020-11-10 10:50:34 -080063}
64
65var _ EarlyModulePathContext = ModuleContext(nil)
66
67// Glob globs files and directories matching globPattern relative to ModuleDir(),
68// paths in the excludes parameter will be omitted.
69func Glob(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths {
70 ret, err := ctx.GlobWithDeps(globPattern, excludes)
71 if err != nil {
72 ctx.ModuleErrorf("glob: %s", err.Error())
73 }
74 return pathsForModuleSrcFromFullPath(ctx, ret, true)
75}
76
77// GlobFiles globs *only* files (not directories) matching globPattern relative to ModuleDir().
78// Paths in the excludes parameter will be omitted.
79func GlobFiles(ctx EarlyModulePathContext, globPattern string, excludes []string) Paths {
80 ret, err := ctx.GlobWithDeps(globPattern, excludes)
81 if err != nil {
82 ctx.ModuleErrorf("glob: %s", err.Error())
83 }
84 return pathsForModuleSrcFromFullPath(ctx, ret, false)
85}
86
87// ModuleWithDepsPathContext is a subset of *ModuleContext methods required by
88// the Path methods that rely on module dependencies having been resolved.
89type ModuleWithDepsPathContext interface {
90 EarlyModulePathContext
Cole Faust55b56fe2024-08-23 12:06:11 -070091 OtherModuleProviderContext
Colin Cross648daea2024-09-12 14:35:29 -070092 VisitDirectDeps(visit func(Module))
Paul Duffin40131a32021-07-09 17:10:35 +010093 OtherModuleDependencyTag(m blueprint.Module) blueprint.DependencyTag
Cole Faust4e2bf9f2024-09-11 13:26:20 -070094 HasMutatorFinished(mutatorName string) bool
Liz Kammera830f3a2020-11-10 10:50:34 -080095}
96
97// ModuleMissingDepsPathContext is a subset of *ModuleContext methods required by
98// the Path methods that rely on module dependencies having been resolved and ability to report
99// missing dependency errors.
100type ModuleMissingDepsPathContext interface {
101 ModuleWithDepsPathContext
102 AddMissingDependencies(missingDeps []string)
103}
104
Dan Willemsen00269f22017-07-06 16:59:48 -0700105type ModuleInstallPathContext interface {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700106 BaseModuleContext
Dan Willemsen00269f22017-07-06 16:59:48 -0700107
108 InstallInData() bool
Jaewoong Jung0949f312019-09-11 10:25:18 -0700109 InstallInTestcases() bool
Dan Willemsen00269f22017-07-06 16:59:48 -0700110 InstallInSanitizerDir() bool
Yifan Hong1b3348d2020-01-21 15:53:22 -0800111 InstallInRamdisk() bool
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700112 InstallInVendorRamdisk() bool
Inseob Kim08758f02021-04-08 21:13:22 +0900113 InstallInDebugRamdisk() bool
Jiyong Parkf9332f12018-02-01 00:54:12 +0900114 InstallInRecovery() bool
Colin Cross90ba5f42019-10-02 11:10:58 -0700115 InstallInRoot() bool
Colin Crossea30d852023-11-29 16:00:16 -0800116 InstallInOdm() bool
117 InstallInProduct() bool
118 InstallInVendor() bool
Jiyong Park87788b52020-09-01 12:37:45 +0900119 InstallForceOS() (*OsType, *ArchType)
Dan Willemsen00269f22017-07-06 16:59:48 -0700120}
121
122var _ ModuleInstallPathContext = ModuleContext(nil)
123
Cole Faust11edf552023-10-13 11:32:14 -0700124type baseModuleContextToModuleInstallPathContext struct {
125 BaseModuleContext
126}
127
128func (ctx *baseModuleContextToModuleInstallPathContext) InstallInData() bool {
129 return ctx.Module().InstallInData()
130}
131
132func (ctx *baseModuleContextToModuleInstallPathContext) InstallInTestcases() bool {
133 return ctx.Module().InstallInTestcases()
134}
135
136func (ctx *baseModuleContextToModuleInstallPathContext) InstallInSanitizerDir() bool {
137 return ctx.Module().InstallInSanitizerDir()
138}
139
140func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRamdisk() bool {
141 return ctx.Module().InstallInRamdisk()
142}
143
144func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendorRamdisk() bool {
145 return ctx.Module().InstallInVendorRamdisk()
146}
147
148func (ctx *baseModuleContextToModuleInstallPathContext) InstallInDebugRamdisk() bool {
149 return ctx.Module().InstallInDebugRamdisk()
150}
151
152func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRecovery() bool {
153 return ctx.Module().InstallInRecovery()
154}
155
156func (ctx *baseModuleContextToModuleInstallPathContext) InstallInRoot() bool {
157 return ctx.Module().InstallInRoot()
158}
159
Colin Crossea30d852023-11-29 16:00:16 -0800160func (ctx *baseModuleContextToModuleInstallPathContext) InstallInOdm() bool {
161 return ctx.Module().InstallInOdm()
162}
163
164func (ctx *baseModuleContextToModuleInstallPathContext) InstallInProduct() bool {
165 return ctx.Module().InstallInProduct()
166}
167
168func (ctx *baseModuleContextToModuleInstallPathContext) InstallInVendor() bool {
169 return ctx.Module().InstallInVendor()
170}
171
Cole Faust11edf552023-10-13 11:32:14 -0700172func (ctx *baseModuleContextToModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
173 return ctx.Module().InstallForceOS()
174}
175
176var _ ModuleInstallPathContext = (*baseModuleContextToModuleInstallPathContext)(nil)
177
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700178// errorfContext is the interface containing the Errorf method matching the
179// Errorf method in blueprint.SingletonContext.
180type errorfContext interface {
181 Errorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800182}
183
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700184var _ errorfContext = blueprint.SingletonContext(nil)
185
Spandan Das59a4a2b2024-01-09 21:35:56 +0000186// ModuleErrorfContext is the interface containing the ModuleErrorf method matching
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700187// the ModuleErrorf method in blueprint.ModuleContext.
Spandan Das59a4a2b2024-01-09 21:35:56 +0000188type ModuleErrorfContext interface {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700189 ModuleErrorf(format string, args ...interface{})
Colin Cross3f40fa42015-01-30 17:27:36 -0800190}
191
Spandan Das59a4a2b2024-01-09 21:35:56 +0000192var _ ModuleErrorfContext = blueprint.ModuleContext(nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700193
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700194// reportPathError will register an error with the attached context. It
195// attempts ctx.ModuleErrorf for a better error message first, then falls
196// back to ctx.Errorf.
Colin Cross1ccfcc32018-02-22 13:54:26 -0800197func reportPathError(ctx PathContext, err error) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100198 ReportPathErrorf(ctx, "%s", err.Error())
Colin Cross1ccfcc32018-02-22 13:54:26 -0800199}
200
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100201// ReportPathErrorf will register an error with the attached context. It
Colin Cross1ccfcc32018-02-22 13:54:26 -0800202// attempts ctx.ModuleErrorf for a better error message first, then falls
203// back to ctx.Errorf.
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100204func ReportPathErrorf(ctx PathContext, format string, args ...interface{}) {
Spandan Das59a4a2b2024-01-09 21:35:56 +0000205 if mctx, ok := ctx.(ModuleErrorfContext); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700206 mctx.ModuleErrorf(format, args...)
207 } else if ectx, ok := ctx.(errorfContext); ok {
208 ectx.Errorf(format, args...)
209 } else {
210 panic(fmt.Sprintf(format, args...))
Colin Crossf2298272015-05-12 11:36:53 -0700211 }
212}
213
Colin Cross5e708052019-08-06 13:59:50 -0700214func pathContextName(ctx PathContext, module blueprint.Module) string {
215 if x, ok := ctx.(interface{ ModuleName(blueprint.Module) string }); ok {
216 return x.ModuleName(module)
217 } else if x, ok := ctx.(interface{ OtherModuleName(blueprint.Module) string }); ok {
218 return x.OtherModuleName(module)
219 }
220 return "unknown"
221}
222
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700223type Path interface {
224 // Returns the path in string form
225 String() string
226
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700227 // Ext returns the extension of the last element of the path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700228 Ext() string
Colin Cross4f6fc9c2016-10-26 10:05:25 -0700229
230 // Base returns the last element of the path
231 Base() string
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800232
233 // Rel returns the portion of the path relative to the directory it was created from. For
234 // example, Rel on a PathsForModuleSrc would return the path relative to the module source
Colin Cross0db55682017-12-05 15:36:55 -0800235 // directory, and OutputPath.Join("foo").Rel() would return "foo".
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800236 Rel() string
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000237
Colin Cross7707b242024-07-26 12:02:36 -0700238 // WithoutRel returns a new Path with no relative path, i.e. Rel() will return the same value as Base().
239 WithoutRel() Path
240
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000241 // RelativeToTop returns a new path relative to the top, it is provided solely for use in tests.
242 //
243 // It is guaranteed to always return the same type as it is called on, e.g. if called on an
244 // InstallPath then the returned value can be converted to an InstallPath.
245 //
246 // A standard build has the following structure:
247 // ../top/
248 // out/ - make install files go here.
Colin Cross3b1c6842024-07-26 11:52:57 -0700249 // out/soong - this is the outDir passed to NewTestConfig()
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000250 // ... - the source files
251 //
252 // This function converts a path so that it appears relative to the ../top/ directory, i.e.
Colin Cross3b1c6842024-07-26 11:52:57 -0700253 // * Make install paths, which have the pattern "outDir/../<path>" are converted into the top
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000254 // relative path "out/<path>"
Colin Cross3b1c6842024-07-26 11:52:57 -0700255 // * Soong install paths and other writable paths, which have the pattern "outDir/soong/<path>" are
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000256 // converted into the top relative path "out/soong/<path>".
257 // * Source paths are already relative to the top.
258 // * Phony paths are not relative to anything.
259 // * toolDepPath have an absolute but known value in so don't need making relative to anything in
260 // order to test.
261 RelativeToTop() Path
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700262}
263
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000264const (
Colin Cross3b1c6842024-07-26 11:52:57 -0700265 testOutDir = "out"
266 testOutSoongSubDir = "/soong"
267 TestOutSoongDir = testOutDir + testOutSoongSubDir
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000268)
269
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700270// WritablePath is a type of path that can be used as an output for build rules.
271type WritablePath interface {
272 Path
273
Paul Duffin9b478b02019-12-10 13:41:51 +0000274 // return the path to the build directory.
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200275 getSoongOutDir() string
Paul Duffin9b478b02019-12-10 13:41:51 +0000276
Jeff Gaston734e3802017-04-10 15:47:24 -0700277 // the writablePath method doesn't directly do anything,
278 // but it allows a struct to distinguish between whether or not it implements the WritablePath interface
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700279 writablePath()
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +0100280
281 ReplaceExtension(ctx PathContext, ext string) OutputPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700282}
283
284type genPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800285 genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath
yangbill6d032dd2024-04-18 03:05:49 +0000286 genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700287}
288type objPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800289 objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700290}
291type resPathProvider interface {
Liz Kammera830f3a2020-11-10 10:50:34 -0800292 resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700293}
294
295// GenPathWithExt derives a new file path in ctx's generated sources directory
296// from the current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800297func GenPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700298 if path, ok := p.(genPathProvider); ok {
Dan Willemsen21ec4902016-11-02 20:43:13 -0700299 return path.genPathWithExt(ctx, subdir, ext)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700300 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100301 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700302 return PathForModuleGen(ctx)
303}
304
yangbill6d032dd2024-04-18 03:05:49 +0000305// GenPathWithExtAndTrimExt derives a new file path in ctx's generated sources directory
306// from the current path, but with the new extension and trim the suffix.
307func GenPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir string, p Path, ext string, trimExt string) ModuleGenPath {
308 if path, ok := p.(genPathProvider); ok {
309 return path.genPathWithExtAndTrimExt(ctx, subdir, ext, trimExt)
310 }
311 ReportPathErrorf(ctx, "Tried to create generated file from unsupported path: %s(%s)", reflect.TypeOf(p).Name(), p)
312 return PathForModuleGen(ctx)
313}
314
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700315// ObjPathWithExt derives a new file path in ctx's object directory from the
316// current path, but with the new extension.
Liz Kammera830f3a2020-11-10 10:50:34 -0800317func ObjPathWithExt(ctx ModuleOutPathContext, subdir string, p Path, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700318 if path, ok := p.(objPathProvider); ok {
319 return path.objPathWithExt(ctx, subdir, ext)
320 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100321 ReportPathErrorf(ctx, "Tried to create object file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700322 return PathForModuleObj(ctx)
323}
324
325// ResPathWithName derives a new path in ctx's output resource directory, using
326// the current path to create the directory name, and the `name` argument for
327// the filename.
Liz Kammera830f3a2020-11-10 10:50:34 -0800328func ResPathWithName(ctx ModuleOutPathContext, p Path, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700329 if path, ok := p.(resPathProvider); ok {
330 return path.resPathWithName(ctx, name)
331 }
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100332 ReportPathErrorf(ctx, "Tried to create res file from unsupported path: %s (%s)", reflect.TypeOf(p).Name(), p)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700333 return PathForModuleRes(ctx)
334}
335
336// OptionalPath is a container that may or may not contain a valid Path.
337type OptionalPath struct {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100338 path Path // nil if invalid.
339 invalidReason string // Not applicable if path != nil. "" if the reason is unknown.
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700340}
341
Yu Liu467d7c52024-09-18 21:54:44 +0000342type optionalPathGob struct {
343 Path Path
344 InvalidReason string
345}
346
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700347// OptionalPathForPath returns an OptionalPath containing the path.
348func OptionalPathForPath(path Path) OptionalPath {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100349 return OptionalPath{path: path}
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700350}
351
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100352// InvalidOptionalPath returns an OptionalPath that is invalid with the given reason.
353func InvalidOptionalPath(reason string) OptionalPath {
354
355 return OptionalPath{invalidReason: reason}
356}
357
Yu Liu467d7c52024-09-18 21:54:44 +0000358func (p *OptionalPath) ToGob() *optionalPathGob {
359 return &optionalPathGob{
360 Path: p.path,
361 InvalidReason: p.invalidReason,
362 }
363}
364
365func (p *OptionalPath) FromGob(data *optionalPathGob) {
366 p.path = data.Path
367 p.invalidReason = data.InvalidReason
368}
369
370func (p OptionalPath) GobEncode() ([]byte, error) {
371 return blueprint.CustomGobEncode[optionalPathGob](&p)
372}
373
374func (p *OptionalPath) GobDecode(data []byte) error {
375 return blueprint.CustomGobDecode[optionalPathGob](data, p)
376}
377
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700378// Valid returns whether there is a valid path
379func (p OptionalPath) Valid() bool {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100380 return p.path != nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700381}
382
383// Path returns the Path embedded in this OptionalPath. You must be sure that
384// there is a valid path, since this method will panic if there is not.
385func (p OptionalPath) Path() Path {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100386 if p.path == nil {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100387 msg := "Requesting an invalid path"
388 if p.invalidReason != "" {
389 msg += ": " + p.invalidReason
390 }
391 panic(msg)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700392 }
393 return p.path
394}
395
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +0100396// InvalidReason returns the reason that the optional path is invalid, or "" if it is valid.
397func (p OptionalPath) InvalidReason() string {
398 if p.path != nil {
399 return ""
400 }
401 if p.invalidReason == "" {
402 return "unknown"
403 }
404 return p.invalidReason
405}
406
Paul Duffinef081852021-05-13 11:11:15 +0100407// AsPaths converts the OptionalPath into Paths.
408//
409// It returns nil if this is not valid, or a single length slice containing the Path embedded in
410// this OptionalPath.
411func (p OptionalPath) AsPaths() Paths {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100412 if p.path == nil {
Paul Duffinef081852021-05-13 11:11:15 +0100413 return nil
414 }
415 return Paths{p.path}
416}
417
Paul Duffinafdd4062021-03-30 19:44:07 +0100418// RelativeToTop returns an OptionalPath with the path that was embedded having been replaced by the
419// result of calling Path.RelativeToTop on it.
420func (p OptionalPath) RelativeToTop() OptionalPath {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100421 if p.path == nil {
Paul Duffina5b81352021-03-28 23:57:19 +0100422 return p
423 }
424 p.path = p.path.RelativeToTop()
425 return p
426}
427
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700428// String returns the string version of the Path, or "" if it isn't valid.
429func (p OptionalPath) String() string {
Martin Stjernholm2fee27f2021-09-16 14:11:12 +0100430 if p.path != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700431 return p.path.String()
432 } else {
433 return ""
Colin Crossf2298272015-05-12 11:36:53 -0700434 }
435}
Colin Cross6e18ca42015-07-14 18:55:36 -0700436
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700437// Paths is a slice of Path objects, with helpers to operate on the collection.
438type Paths []Path
439
Paul Duffin85d8f0d2021-03-24 10:18:18 +0000440// RelativeToTop creates a new Paths containing the result of calling Path.RelativeToTop on each
441// item in this slice.
442func (p Paths) RelativeToTop() Paths {
443 ensureTestOnly()
444 if p == nil {
445 return p
446 }
447 ret := make(Paths, len(p))
448 for i, path := range p {
449 ret[i] = path.RelativeToTop()
450 }
451 return ret
452}
453
Jingwen Chen40fd90a2020-06-15 05:24:19 +0000454func (paths Paths) containsPath(path Path) bool {
455 for _, p := range paths {
456 if p == path {
457 return true
458 }
459 }
460 return false
461}
462
Liz Kammer7aa52882021-02-11 09:16:14 -0500463// PathsForSource returns Paths rooted from SrcDir, *not* rooted from the module's local source
464// directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700465func PathsForSource(ctx PathContext, paths []string) Paths {
466 ret := make(Paths, len(paths))
467 for i, path := range paths {
468 ret[i] = PathForSource(ctx, path)
469 }
470 return ret
471}
472
Liz Kammer7aa52882021-02-11 09:16:14 -0500473// ExistentPathsForSources returns a list of Paths rooted from SrcDir, *not* rooted from the
474// module's local source directory, that are found in the tree. If any are not found, they are
475// 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 -0700476func ExistentPathsForSources(ctx PathGlobContext, paths []string) Paths {
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800477 ret := make(Paths, 0, len(paths))
478 for _, path := range paths {
Colin Cross32f38982018-02-22 11:47:25 -0800479 p := ExistentPathForSource(ctx, path)
Dan Willemsen7b310ee2015-12-18 15:11:17 -0800480 if p.Valid() {
481 ret = append(ret, p.Path())
482 }
483 }
484 return ret
485}
486
Liz Kammer620dea62021-04-14 17:36:10 -0400487// PathsForModuleSrc returns a Paths{} containing the resolved references in paths:
Colin Crossd079e0b2022-08-16 10:27:33 -0700488// - filepath, relative to local module directory, resolves as a filepath relative to the local
489// source directory
490// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
491// source directory.
492// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700493// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
494// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700495//
Liz Kammer620dea62021-04-14 17:36:10 -0400496// Properties passed as the paths argument must have been annotated with struct tag
Colin Cross41955e82019-05-29 14:40:35 -0700497// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000498// pathdeps mutator.
Liz Kammer620dea62021-04-14 17:36:10 -0400499// If a requested module is not found as a dependency:
Colin Crossd079e0b2022-08-16 10:27:33 -0700500// - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
Liz Kammer620dea62021-04-14 17:36:10 -0400501// missing dependencies
Colin Crossd079e0b2022-08-16 10:27:33 -0700502// - otherwise, a ModuleError is thrown.
Liz Kammera830f3a2020-11-10 10:50:34 -0800503func PathsForModuleSrc(ctx ModuleMissingDepsPathContext, paths []string) Paths {
Colin Cross8a497952019-03-05 22:25:09 -0800504 return PathsForModuleSrcExcludes(ctx, paths, nil)
505}
506
Liz Kammer619be462022-01-28 15:13:39 -0500507type SourceInput struct {
508 Context ModuleMissingDepsPathContext
509 Paths []string
510 ExcludePaths []string
511 IncludeDirs bool
512}
513
Liz Kammer620dea62021-04-14 17:36:10 -0400514// PathsForModuleSrcExcludes returns a Paths{} containing the resolved references in paths, minus
515// those listed in excludes. Elements of paths and excludes are resolved as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700516// - filepath, relative to local module directory, resolves as a filepath relative to the local
517// source directory
518// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
519// source directory. Not valid in excludes.
520// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700521// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
522// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700523//
Liz Kammer620dea62021-04-14 17:36:10 -0400524// excluding the items (similarly resolved
525// Properties passed as the paths argument must have been annotated with struct tag
526// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000527// pathdeps mutator.
Liz Kammer620dea62021-04-14 17:36:10 -0400528// If a requested module is not found as a dependency:
Colin Crossd079e0b2022-08-16 10:27:33 -0700529// - if ctx.Config().AllowMissingDependencies() is true, this module to be marked as having
Liz Kammer620dea62021-04-14 17:36:10 -0400530// missing dependencies
Colin Crossd079e0b2022-08-16 10:27:33 -0700531// - otherwise, a ModuleError is thrown.
Liz Kammera830f3a2020-11-10 10:50:34 -0800532func PathsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) Paths {
Liz Kammer619be462022-01-28 15:13:39 -0500533 return PathsRelativeToModuleSourceDir(SourceInput{
534 Context: ctx,
535 Paths: paths,
536 ExcludePaths: excludes,
537 IncludeDirs: true,
538 })
539}
540
541func PathsRelativeToModuleSourceDir(input SourceInput) Paths {
542 ret, missingDeps := PathsAndMissingDepsRelativeToModuleSourceDir(input)
543 if input.Context.Config().AllowMissingDependencies() {
544 input.Context.AddMissingDependencies(missingDeps)
Colin Crossba71a3f2019-03-18 12:12:48 -0700545 } else {
546 for _, m := range missingDeps {
Liz Kammer619be462022-01-28 15:13:39 -0500547 input.Context.ModuleErrorf(`missing dependency on %q, is the property annotated with android:"path"?`, m)
Colin Crossba71a3f2019-03-18 12:12:48 -0700548 }
549 }
550 return ret
551}
552
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +0000553// OutputPaths is a slice of OutputPath objects, with helpers to operate on the collection.
554type OutputPaths []OutputPath
555
556// Paths returns the OutputPaths as a Paths
557func (p OutputPaths) Paths() Paths {
558 if p == nil {
559 return nil
560 }
561 ret := make(Paths, len(p))
562 for i, path := range p {
563 ret[i] = path
564 }
565 return ret
566}
567
568// Strings returns the string forms of the writable paths.
569func (p OutputPaths) Strings() []string {
570 if p == nil {
571 return nil
572 }
573 ret := make([]string, len(p))
574 for i, path := range p {
575 ret[i] = path.String()
576 }
577 return ret
578}
579
Liz Kammera830f3a2020-11-10 10:50:34 -0800580// Expands Paths to a SourceFileProducer or OutputFileProducer module dependency referenced via ":name" or ":name{.tag}" syntax.
581// If the dependency is not found, a missingErrorDependency is returned.
582// If the module dependency is not a SourceFileProducer or OutputFileProducer, appropriate errors will be returned.
583func getPathsFromModuleDep(ctx ModuleWithDepsPathContext, path, moduleName, tag string) (Paths, error) {
Paul Duffind5cf92e2021-07-09 17:38:55 +0100584 module := GetModuleFromPathDep(ctx, moduleName, tag)
Liz Kammera830f3a2020-11-10 10:50:34 -0800585 if module == nil {
586 return nil, missingDependencyError{[]string{moduleName}}
587 }
Cole Fausta963b942024-04-11 17:43:00 -0700588 if aModule, ok := module.(Module); ok && !aModule.Enabled(ctx) {
Colin Crossfa65cee2021-03-22 17:05:59 -0700589 return nil, missingDependencyError{[]string{moduleName}}
590 }
mrziwange6c85812024-05-22 14:36:09 -0700591 outputFiles, err := outputFilesForModule(ctx, module, tag)
592 if outputFiles != nil && err == nil {
593 return outputFiles, nil
Liz Kammera830f3a2020-11-10 10:50:34 -0800594 } else {
mrziwange6c85812024-05-22 14:36:09 -0700595 return nil, err
Liz Kammera830f3a2020-11-10 10:50:34 -0800596 }
597}
598
Paul Duffind5cf92e2021-07-09 17:38:55 +0100599// GetModuleFromPathDep will return the module that was added as a dependency automatically for
600// properties tagged with `android:"path"` or manually using ExtractSourceDeps or
601// ExtractSourcesDeps.
602//
603// The moduleName and tag supplied to this should be the values returned from SrcIsModuleWithTag.
604// Or, if no tag is expected then the moduleName should be the value returned by SrcIsModule and
605// the tag must be "".
606//
607// If tag is "" then the returned module will be the dependency that was added for ":moduleName".
608// Otherwise, it is the dependency that was added for ":moduleName{tag}".
Paul Duffind5cf92e2021-07-09 17:38:55 +0100609func GetModuleFromPathDep(ctx ModuleWithDepsPathContext, moduleName, tag string) blueprint.Module {
Paul Duffin40131a32021-07-09 17:10:35 +0100610 var found blueprint.Module
611 // The sourceOrOutputDepTag uniquely identifies the module dependency as it contains both the
612 // module name and the tag. Dependencies added automatically for properties tagged with
613 // `android:"path"` are deduped so are guaranteed to be unique. It is possible for duplicate
614 // dependencies to be added manually using ExtractSourcesDeps or ExtractSourceDeps but even then
615 // it will always be the case that the dependencies will be identical, i.e. the same tag and same
616 // moduleName referring to the same dependency module.
617 //
618 // It does not matter whether the moduleName is a fully qualified name or if the module
619 // dependency is a prebuilt module. All that matters is the same information is supplied to
620 // create the tag here as was supplied to create the tag when the dependency was added so that
621 // this finds the matching dependency module.
622 expectedTag := sourceOrOutputDepTag(moduleName, tag)
Colin Cross648daea2024-09-12 14:35:29 -0700623 ctx.VisitDirectDeps(func(module Module) {
Paul Duffin40131a32021-07-09 17:10:35 +0100624 depTag := ctx.OtherModuleDependencyTag(module)
625 if depTag == expectedTag {
626 found = module
627 }
628 })
629 return found
Paul Duffind5cf92e2021-07-09 17:38:55 +0100630}
631
Liz Kammer620dea62021-04-14 17:36:10 -0400632// PathsAndMissingDepsForModuleSrcExcludes returns a Paths{} containing the resolved references in
633// paths, minus those listed in excludes. Elements of paths and excludes are resolved as:
Colin Crossd079e0b2022-08-16 10:27:33 -0700634// - filepath, relative to local module directory, resolves as a filepath relative to the local
635// source directory
636// - glob, relative to the local module directory, resolves as filepath(s), relative to the local
637// source directory. Not valid in excludes.
638// - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer
mrziwangd38e63d2024-07-15 13:43:37 -0700639// or set the OutputFilesProvider. These resolve as a filepath to an output filepath or generated
640// source filepath.
Colin Crossd079e0b2022-08-16 10:27:33 -0700641//
Liz Kammer620dea62021-04-14 17:36:10 -0400642// and a list of the module names of missing module dependencies are returned as the second return.
643// Properties passed as the paths argument must have been annotated with struct tag
Colin Cross41955e82019-05-29 14:40:35 -0700644// `android:"path"` so that dependencies on SourceFileProducer modules will have already been handled by the
Spandan Das950091c2023-07-19 22:26:37 +0000645// pathdeps mutator.
Liz Kammer619be462022-01-28 15:13:39 -0500646func PathsAndMissingDepsForModuleSrcExcludes(ctx ModuleMissingDepsPathContext, paths, excludes []string) (Paths, []string) {
647 return PathsAndMissingDepsRelativeToModuleSourceDir(SourceInput{
648 Context: ctx,
649 Paths: paths,
650 ExcludePaths: excludes,
651 IncludeDirs: true,
652 })
653}
654
655func PathsAndMissingDepsRelativeToModuleSourceDir(input SourceInput) (Paths, []string) {
656 prefix := pathForModuleSrc(input.Context).String()
Colin Cross8a497952019-03-05 22:25:09 -0800657
658 var expandedExcludes []string
Liz Kammer619be462022-01-28 15:13:39 -0500659 if input.ExcludePaths != nil {
660 expandedExcludes = make([]string, 0, len(input.ExcludePaths))
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700661 }
Colin Cross8a497952019-03-05 22:25:09 -0800662
Colin Crossba71a3f2019-03-18 12:12:48 -0700663 var missingExcludeDeps []string
Liz Kammer619be462022-01-28 15:13:39 -0500664 for _, e := range input.ExcludePaths {
Colin Cross41955e82019-05-29 14:40:35 -0700665 if m, t := SrcIsModuleWithTag(e); m != "" {
Liz Kammer619be462022-01-28 15:13:39 -0500666 modulePaths, err := getPathsFromModuleDep(input.Context, e, m, t)
Liz Kammera830f3a2020-11-10 10:50:34 -0800667 if m, ok := err.(missingDependencyError); ok {
668 missingExcludeDeps = append(missingExcludeDeps, m.missingDeps...)
669 } else if err != nil {
Liz Kammer619be462022-01-28 15:13:39 -0500670 reportPathError(input.Context, err)
Colin Cross8a497952019-03-05 22:25:09 -0800671 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800672 expandedExcludes = append(expandedExcludes, modulePaths.Strings()...)
Colin Cross8a497952019-03-05 22:25:09 -0800673 }
674 } else {
675 expandedExcludes = append(expandedExcludes, filepath.Join(prefix, e))
676 }
677 }
678
Liz Kammer619be462022-01-28 15:13:39 -0500679 if input.Paths == nil {
Colin Crossba71a3f2019-03-18 12:12:48 -0700680 return nil, missingExcludeDeps
Colin Cross8a497952019-03-05 22:25:09 -0800681 }
682
Colin Crossba71a3f2019-03-18 12:12:48 -0700683 var missingDeps []string
684
Liz Kammer619be462022-01-28 15:13:39 -0500685 expandedSrcFiles := make(Paths, 0, len(input.Paths))
686 for _, s := range input.Paths {
687 srcFiles, err := expandOneSrcPath(sourcePathInput{
688 context: input.Context,
689 path: s,
690 expandedExcludes: expandedExcludes,
691 includeDirs: input.IncludeDirs,
692 })
Colin Cross8a497952019-03-05 22:25:09 -0800693 if depErr, ok := err.(missingDependencyError); ok {
Colin Crossba71a3f2019-03-18 12:12:48 -0700694 missingDeps = append(missingDeps, depErr.missingDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800695 } else if err != nil {
Liz Kammer619be462022-01-28 15:13:39 -0500696 reportPathError(input.Context, err)
Colin Cross8a497952019-03-05 22:25:09 -0800697 }
698 expandedSrcFiles = append(expandedSrcFiles, srcFiles...)
699 }
Colin Crossba71a3f2019-03-18 12:12:48 -0700700
Jihoon Kang0e3a5352024-04-12 00:45:50 +0000701 // TODO: b/334169722 - Replace with an error instead of implicitly removing duplicates.
702 return FirstUniquePaths(expandedSrcFiles), append(missingDeps, missingExcludeDeps...)
Colin Cross8a497952019-03-05 22:25:09 -0800703}
704
705type missingDependencyError struct {
706 missingDeps []string
707}
708
709func (e missingDependencyError) Error() string {
710 return "missing dependencies: " + strings.Join(e.missingDeps, ", ")
711}
712
Liz Kammer619be462022-01-28 15:13:39 -0500713type sourcePathInput struct {
714 context ModuleWithDepsPathContext
715 path string
716 expandedExcludes []string
717 includeDirs bool
718}
719
Liz Kammera830f3a2020-11-10 10:50:34 -0800720// Expands one path string to Paths rooted from the module's local source
721// directory, excluding those listed in the expandedExcludes.
722// Expands globs, references to SourceFileProducer or OutputFileProducer modules using the ":name" and ":name{.tag}" syntax.
Liz Kammer619be462022-01-28 15:13:39 -0500723func expandOneSrcPath(input sourcePathInput) (Paths, error) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900724 excludePaths := func(paths Paths) Paths {
Liz Kammer619be462022-01-28 15:13:39 -0500725 if len(input.expandedExcludes) == 0 {
Jooyung Han7607dd32020-07-05 10:23:14 +0900726 return paths
727 }
728 remainder := make(Paths, 0, len(paths))
729 for _, p := range paths {
Liz Kammer619be462022-01-28 15:13:39 -0500730 if !InList(p.String(), input.expandedExcludes) {
Jooyung Han7607dd32020-07-05 10:23:14 +0900731 remainder = append(remainder, p)
732 }
733 }
734 return remainder
735 }
Liz Kammer619be462022-01-28 15:13:39 -0500736 if m, t := SrcIsModuleWithTag(input.path); m != "" {
737 modulePaths, err := getPathsFromModuleDep(input.context, input.path, m, t)
Liz Kammera830f3a2020-11-10 10:50:34 -0800738 if err != nil {
739 return nil, err
Colin Cross8a497952019-03-05 22:25:09 -0800740 } else {
Liz Kammera830f3a2020-11-10 10:50:34 -0800741 return excludePaths(modulePaths), nil
Colin Cross8a497952019-03-05 22:25:09 -0800742 }
Colin Cross8a497952019-03-05 22:25:09 -0800743 } else {
Liz Kammer619be462022-01-28 15:13:39 -0500744 p := pathForModuleSrc(input.context, input.path)
745 if pathtools.IsGlob(input.path) {
746 paths := GlobFiles(input.context, p.String(), input.expandedExcludes)
747 return PathsWithModuleSrcSubDir(input.context, paths, ""), nil
748 } else {
749 if exists, _, err := input.context.Config().fs.Exists(p.String()); err != nil {
750 ReportPathErrorf(input.context, "%s: %s", p, err.Error())
751 } else if !exists && !input.context.Config().TestAllowNonExistentPaths {
752 ReportPathErrorf(input.context, "module source path %q does not exist", p)
753 } else if !input.includeDirs {
754 if isDir, err := input.context.Config().fs.IsDir(p.String()); exists && err != nil {
755 ReportPathErrorf(input.context, "%s: %s", p, err.Error())
756 } else if isDir {
757 ReportPathErrorf(input.context, "module source path %q is a directory", p)
758 }
759 }
Colin Cross8a497952019-03-05 22:25:09 -0800760
Liz Kammer619be462022-01-28 15:13:39 -0500761 if InList(p.String(), input.expandedExcludes) {
762 return nil, nil
763 }
764 return Paths{p}, nil
Colin Cross8a497952019-03-05 22:25:09 -0800765 }
Colin Cross8a497952019-03-05 22:25:09 -0800766 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700767}
768
769// pathsForModuleSrcFromFullPath returns Paths rooted from the module's local
770// source directory, but strip the local source directory from the beginning of
Dan Willemsen540a78c2018-02-26 21:50:08 -0800771// each string. If incDirs is false, strip paths with a trailing '/' from the list.
Colin Crossfe4bc362018-09-12 10:02:13 -0700772// It intended for use in globs that only list files that exist, so it allows '$' in
773// filenames.
Liz Kammera830f3a2020-11-10 10:50:34 -0800774func pathsForModuleSrcFromFullPath(ctx EarlyModulePathContext, paths []string, incDirs bool) Paths {
Lukacs T. Berkif7e36d82021-08-16 17:05:09 +0200775 prefix := ctx.ModuleDir() + "/"
Colin Cross0f37af02017-09-27 17:42:05 -0700776 if prefix == "./" {
777 prefix = ""
778 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700779 ret := make(Paths, 0, len(paths))
780 for _, p := range paths {
Dan Willemsen540a78c2018-02-26 21:50:08 -0800781 if !incDirs && strings.HasSuffix(p, "/") {
782 continue
783 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700784 path := filepath.Clean(p)
785 if !strings.HasPrefix(path, prefix) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +0100786 ReportPathErrorf(ctx, "Path %q is not in module source directory %q", p, prefix)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700787 continue
788 }
Colin Crosse3924e12018-08-15 20:18:53 -0700789
Colin Crossfe4bc362018-09-12 10:02:13 -0700790 srcPath, err := safePathForSource(ctx, ctx.ModuleDir(), path[len(prefix):])
Colin Crosse3924e12018-08-15 20:18:53 -0700791 if err != nil {
792 reportPathError(ctx, err)
793 continue
794 }
795
Colin Cross07e51612019-03-05 12:46:40 -0800796 srcPath.basePath.rel = srcPath.path
Colin Crosse3924e12018-08-15 20:18:53 -0700797
Colin Cross07e51612019-03-05 12:46:40 -0800798 ret = append(ret, srcPath)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700799 }
800 return ret
801}
802
Liz Kammera830f3a2020-11-10 10:50:34 -0800803// PathsWithOptionalDefaultForModuleSrc returns Paths rooted from the module's local source
804// directory. If input is nil, use the default if it exists. If input is empty, returns nil.
805func PathsWithOptionalDefaultForModuleSrc(ctx ModuleMissingDepsPathContext, input []string, def string) Paths {
Colin Cross0ddae7f2019-02-07 15:30:01 -0800806 if input != nil {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700807 return PathsForModuleSrc(ctx, input)
808 }
809 // Use Glob so that if the default doesn't exist, a dependency is added so that when it
810 // is created, we're run again.
Lukacs T. Berkif7e36d82021-08-16 17:05:09 +0200811 path := filepath.Join(ctx.ModuleDir(), def)
Liz Kammera830f3a2020-11-10 10:50:34 -0800812 return Glob(ctx, path, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700813}
814
815// Strings returns the Paths in string form
816func (p Paths) Strings() []string {
817 if p == nil {
818 return nil
819 }
820 ret := make([]string, len(p))
821 for i, path := range p {
822 ret[i] = path.String()
823 }
824 return ret
825}
826
Colin Crossc0efd1d2020-07-03 11:56:24 -0700827func CopyOfPaths(paths Paths) Paths {
828 return append(Paths(nil), paths...)
829}
830
Colin Crossb6715442017-10-24 11:13:31 -0700831// FirstUniquePaths returns all unique elements of a Paths, keeping the first copy of each. It
832// modifies the Paths slice contents in place, and returns a subslice of the original slice.
Dan Willemsenfe92c962017-08-29 12:28:37 -0700833func FirstUniquePaths(list Paths) Paths {
Colin Cross27027c72020-02-28 15:34:17 -0800834 // 128 was chosen based on BenchmarkFirstUniquePaths results.
835 if len(list) > 128 {
836 return firstUniquePathsMap(list)
837 }
838 return firstUniquePathsList(list)
839}
840
Colin Crossc0efd1d2020-07-03 11:56:24 -0700841// SortedUniquePaths returns all unique elements of a Paths in sorted order. It modifies the
842// Paths slice contents in place, and returns a subslice of the original slice.
Jiyong Park33c77362020-05-29 22:00:16 +0900843func SortedUniquePaths(list Paths) Paths {
844 unique := FirstUniquePaths(list)
845 sort.Slice(unique, func(i, j int) bool {
846 return unique[i].String() < unique[j].String()
847 })
848 return unique
849}
850
Colin Cross27027c72020-02-28 15:34:17 -0800851func firstUniquePathsList(list Paths) Paths {
Dan Willemsenfe92c962017-08-29 12:28:37 -0700852 k := 0
853outer:
854 for i := 0; i < len(list); i++ {
855 for j := 0; j < k; j++ {
856 if list[i] == list[j] {
857 continue outer
858 }
859 }
860 list[k] = list[i]
861 k++
862 }
863 return list[:k]
864}
865
Colin Cross27027c72020-02-28 15:34:17 -0800866func firstUniquePathsMap(list Paths) Paths {
867 k := 0
868 seen := make(map[Path]bool, len(list))
869 for i := 0; i < len(list); i++ {
870 if seen[list[i]] {
871 continue
872 }
873 seen[list[i]] = true
874 list[k] = list[i]
875 k++
876 }
877 return list[:k]
878}
879
Colin Cross5d583952020-11-24 16:21:24 -0800880// FirstUniqueInstallPaths returns all unique elements of an InstallPaths, keeping the first copy of each. It
881// modifies the InstallPaths slice contents in place, and returns a subslice of the original slice.
882func FirstUniqueInstallPaths(list InstallPaths) InstallPaths {
883 // 128 was chosen based on BenchmarkFirstUniquePaths results.
884 if len(list) > 128 {
885 return firstUniqueInstallPathsMap(list)
886 }
887 return firstUniqueInstallPathsList(list)
888}
889
890func firstUniqueInstallPathsList(list InstallPaths) InstallPaths {
891 k := 0
892outer:
893 for i := 0; i < len(list); i++ {
894 for j := 0; j < k; j++ {
895 if list[i] == list[j] {
896 continue outer
897 }
898 }
899 list[k] = list[i]
900 k++
901 }
902 return list[:k]
903}
904
905func firstUniqueInstallPathsMap(list InstallPaths) InstallPaths {
906 k := 0
907 seen := make(map[InstallPath]bool, len(list))
908 for i := 0; i < len(list); i++ {
909 if seen[list[i]] {
910 continue
911 }
912 seen[list[i]] = true
913 list[k] = list[i]
914 k++
915 }
916 return list[:k]
917}
918
Colin Crossb6715442017-10-24 11:13:31 -0700919// LastUniquePaths returns all unique elements of a Paths, keeping the last copy of each. It
920// modifies the Paths slice contents in place, and returns a subslice of the original slice.
921func LastUniquePaths(list Paths) Paths {
922 totalSkip := 0
923 for i := len(list) - 1; i >= totalSkip; i-- {
924 skip := 0
925 for j := i - 1; j >= totalSkip; j-- {
926 if list[i] == list[j] {
927 skip++
928 } else {
929 list[j+skip] = list[j]
930 }
931 }
932 totalSkip += skip
933 }
934 return list[totalSkip:]
935}
936
Colin Crossa140bb02018-04-17 10:52:26 -0700937// ReversePaths returns a copy of a Paths in reverse order.
938func ReversePaths(list Paths) Paths {
939 if list == nil {
940 return nil
941 }
942 ret := make(Paths, len(list))
943 for i := range list {
944 ret[i] = list[len(list)-1-i]
945 }
946 return ret
947}
948
Jeff Gaston294356f2017-09-27 17:05:30 -0700949func indexPathList(s Path, list []Path) int {
950 for i, l := range list {
951 if l == s {
952 return i
953 }
954 }
955
956 return -1
957}
958
959func inPathList(p Path, list []Path) bool {
960 return indexPathList(p, list) != -1
961}
962
963func FilterPathList(list []Path, filter []Path) (remainder []Path, filtered []Path) {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000964 return FilterPathListPredicate(list, func(p Path) bool { return inPathList(p, filter) })
965}
966
967func FilterPathListPredicate(list []Path, predicate func(Path) bool) (remainder []Path, filtered []Path) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700968 for _, l := range list {
Paul Duffin57b9e1d2019-12-13 00:03:35 +0000969 if predicate(l) {
Jeff Gaston294356f2017-09-27 17:05:30 -0700970 filtered = append(filtered, l)
971 } else {
972 remainder = append(remainder, l)
973 }
974 }
975
976 return
977}
978
Colin Cross93e85952017-08-15 13:34:18 -0700979// HasExt returns true of any of the paths have extension ext, otherwise false
980func (p Paths) HasExt(ext string) bool {
981 for _, path := range p {
982 if path.Ext() == ext {
983 return true
984 }
985 }
986
987 return false
988}
989
990// FilterByExt returns the subset of the paths that have extension ext
991func (p Paths) FilterByExt(ext string) Paths {
992 ret := make(Paths, 0, len(p))
993 for _, path := range p {
994 if path.Ext() == ext {
995 ret = append(ret, path)
996 }
997 }
998 return ret
999}
1000
1001// FilterOutByExt returns the subset of the paths that do not have extension ext
1002func (p Paths) FilterOutByExt(ext string) Paths {
1003 ret := make(Paths, 0, len(p))
1004 for _, path := range p {
1005 if path.Ext() != ext {
1006 ret = append(ret, path)
1007 }
1008 }
1009 return ret
1010}
1011
Colin Cross5e6cfbe2017-11-03 15:20:35 -07001012// DirectorySortedPaths is a slice of paths that are sorted such that all files in a directory
1013// (including subdirectories) are in a contiguous subslice of the list, and can be found in
1014// O(log(N)) time using a binary search on the directory prefix.
1015type DirectorySortedPaths Paths
1016
1017func PathsToDirectorySortedPaths(paths Paths) DirectorySortedPaths {
1018 ret := append(DirectorySortedPaths(nil), paths...)
1019 sort.Slice(ret, func(i, j int) bool {
1020 return ret[i].String() < ret[j].String()
1021 })
1022 return ret
1023}
1024
1025// PathsInDirectory returns a subslice of the DirectorySortedPaths as a Paths that contains all entries
1026// that are in the specified directory and its subdirectories.
1027func (p DirectorySortedPaths) PathsInDirectory(dir string) Paths {
1028 prefix := filepath.Clean(dir) + "/"
1029 start := sort.Search(len(p), func(i int) bool {
1030 return prefix < p[i].String()
1031 })
1032
1033 ret := p[start:]
1034
1035 end := sort.Search(len(ret), func(i int) bool {
1036 return !strings.HasPrefix(ret[i].String(), prefix)
1037 })
1038
1039 ret = ret[:end]
1040
1041 return Paths(ret)
1042}
1043
Alex Humesky29e3bbe2020-11-20 21:30:13 -05001044// WritablePaths is a slice of WritablePath, used for multiple outputs.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001045type WritablePaths []WritablePath
1046
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001047// RelativeToTop creates a new WritablePaths containing the result of calling Path.RelativeToTop on
1048// each item in this slice.
1049func (p WritablePaths) RelativeToTop() WritablePaths {
1050 ensureTestOnly()
1051 if p == nil {
1052 return p
1053 }
1054 ret := make(WritablePaths, len(p))
1055 for i, path := range p {
1056 ret[i] = path.RelativeToTop().(WritablePath)
1057 }
1058 return ret
1059}
1060
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001061// Strings returns the string forms of the writable paths.
1062func (p WritablePaths) Strings() []string {
1063 if p == nil {
1064 return nil
1065 }
1066 ret := make([]string, len(p))
1067 for i, path := range p {
1068 ret[i] = path.String()
1069 }
1070 return ret
1071}
1072
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001073// Paths returns the WritablePaths as a Paths
1074func (p WritablePaths) Paths() Paths {
1075 if p == nil {
1076 return nil
1077 }
1078 ret := make(Paths, len(p))
1079 for i, path := range p {
1080 ret[i] = path
1081 }
1082 return ret
1083}
1084
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001085type basePath struct {
Paul Duffin74abc5d2021-03-24 09:24:59 +00001086 path string
1087 rel string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001088}
1089
Yu Liu467d7c52024-09-18 21:54:44 +00001090type basePathGob struct {
1091 Path string
1092 Rel string
1093}
Yu Liufa297642024-06-11 00:13:02 +00001094
Yu Liu467d7c52024-09-18 21:54:44 +00001095func (p *basePath) ToGob() *basePathGob {
1096 return &basePathGob{
1097 Path: p.path,
1098 Rel: p.rel,
1099 }
1100}
1101
1102func (p *basePath) FromGob(data *basePathGob) {
1103 p.path = data.Path
1104 p.rel = data.Rel
1105}
1106
1107func (p basePath) GobEncode() ([]byte, error) {
1108 return blueprint.CustomGobEncode[basePathGob](&p)
Yu Liufa297642024-06-11 00:13:02 +00001109}
1110
1111func (p *basePath) GobDecode(data []byte) error {
Yu Liu467d7c52024-09-18 21:54:44 +00001112 return blueprint.CustomGobDecode[basePathGob](data, p)
Yu Liufa297642024-06-11 00:13:02 +00001113}
1114
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001115func (p basePath) Ext() string {
1116 return filepath.Ext(p.path)
1117}
1118
Colin Cross4f6fc9c2016-10-26 10:05:25 -07001119func (p basePath) Base() string {
1120 return filepath.Base(p.path)
1121}
1122
Colin Crossfaeb7aa2017-02-01 14:12:44 -08001123func (p basePath) Rel() string {
1124 if p.rel != "" {
1125 return p.rel
1126 }
1127 return p.path
1128}
1129
Colin Cross0875c522017-11-28 17:34:01 -08001130func (p basePath) String() string {
1131 return p.path
1132}
1133
Colin Cross0db55682017-12-05 15:36:55 -08001134func (p basePath) withRel(rel string) basePath {
1135 p.path = filepath.Join(p.path, rel)
1136 p.rel = rel
1137 return p
1138}
1139
Colin Cross7707b242024-07-26 12:02:36 -07001140func (p basePath) withoutRel() basePath {
1141 p.rel = filepath.Base(p.path)
1142 return p
1143}
1144
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001145// SourcePath is a Path representing a file path rooted from SrcDir
1146type SourcePath struct {
1147 basePath
1148}
1149
1150var _ Path = SourcePath{}
1151
Colin Cross0db55682017-12-05 15:36:55 -08001152func (p SourcePath) withRel(rel string) SourcePath {
1153 p.basePath = p.basePath.withRel(rel)
1154 return p
1155}
1156
Colin Crossbd73d0d2024-07-26 12:00:33 -07001157func (p SourcePath) RelativeToTop() Path {
1158 ensureTestOnly()
1159 return p
1160}
1161
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001162// safePathForSource is for paths that we expect are safe -- only for use by go
1163// code that is embedding ninja variables in paths
Colin Crossfe4bc362018-09-12 10:02:13 -07001164func safePathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
1165 p, err := validateSafePath(pathComponents...)
Cole Faust483d1f72023-01-09 14:35:27 -08001166 ret := SourcePath{basePath{p, ""}}
Colin Crossfe4bc362018-09-12 10:02:13 -07001167 if err != nil {
1168 return ret, err
1169 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001170
Colin Cross7b3dcc32019-01-24 13:14:39 -08001171 // absolute path already checked by validateSafePath
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001172 // special-case api surface gen files for now
1173 if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
Mikhail Naganovab1f5182019-02-08 13:17:55 -08001174 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Cross6e18ca42015-07-14 18:55:36 -07001175 }
1176
Colin Crossfe4bc362018-09-12 10:02:13 -07001177 return ret, err
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001178}
1179
Colin Cross192e97a2018-02-22 14:21:02 -08001180// pathForSource creates a SourcePath from pathComponents, but does not check that it exists.
1181func pathForSource(ctx PathContext, pathComponents ...string) (SourcePath, error) {
Colin Crossc48c1432018-02-23 07:09:01 +00001182 p, err := validatePath(pathComponents...)
Cole Faust483d1f72023-01-09 14:35:27 -08001183 ret := SourcePath{basePath{p, ""}}
Colin Cross94a32102018-02-22 14:21:02 -08001184 if err != nil {
Colin Cross192e97a2018-02-22 14:21:02 -08001185 return ret, err
Colin Cross94a32102018-02-22 14:21:02 -08001186 }
1187
Colin Cross7b3dcc32019-01-24 13:14:39 -08001188 // absolute path already checked by validatePath
Inseob Kim5eb7ee92022-04-27 10:30:34 +09001189 // special-case for now
1190 if strings.HasPrefix(ret.String(), ctx.Config().soongOutDir) && !strings.Contains(ret.String(), ctx.Config().soongOutDir+"/.export") {
Mikhail Naganovab1f5182019-02-08 13:17:55 -08001191 return ret, fmt.Errorf("source path %q is in output", ret.String())
Colin Crossc48c1432018-02-23 07:09:01 +00001192 }
1193
Colin Cross192e97a2018-02-22 14:21:02 -08001194 return ret, nil
1195}
1196
1197// 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
Cole Faustbc65a3f2023-08-01 16:38:55 +00001242// PathForArbitraryOutput creates a path for the given components. Unlike PathForOutput,
1243// the path is relative to the root of the output folder, not the out/soong folder.
1244func PathForArbitraryOutput(ctx PathContext, pathComponents ...string) Path {
Colin Cross3b1c6842024-07-26 11:52:57 -07001245 path, err := validatePath(pathComponents...)
Cole Faustbc65a3f2023-08-01 16:38:55 +00001246 if err != nil {
1247 reportPathError(ctx, err)
1248 }
Colin Cross3b1c6842024-07-26 11:52:57 -07001249 fullPath := filepath.Join(ctx.Config().OutDir(), path)
1250 path = fullPath[len(fullPath)-len(path):]
1251 return OutputPath{basePath{path, ""}, ctx.Config().OutDir(), fullPath}
Cole Faustbc65a3f2023-08-01 16:38:55 +00001252}
1253
Spandan Dasc6c10fa2022-10-21 21:52:13 +00001254// MaybeExistentPathForSource joins the provided path components and validates that the result
1255// neither escapes the source dir nor is in the out dir.
1256// It does not validate whether the path exists.
1257func MaybeExistentPathForSource(ctx PathContext, pathComponents ...string) SourcePath {
1258 path, err := pathForSource(ctx, pathComponents...)
1259 if err != nil {
1260 reportPathError(ctx, err)
1261 }
1262
1263 if pathtools.IsGlob(path.String()) {
1264 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
1265 }
1266 return path
1267}
1268
Liz Kammer7aa52882021-02-11 09:16:14 -05001269// ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not*
1270// rooted from the module's local source directory, if the path exists, or an empty OptionalPath if
1271// it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state
1272// of the path changes.
Colin Cross662d6142022-11-03 20:38:01 -07001273func ExistentPathForSource(ctx PathGlobContext, pathComponents ...string) OptionalPath {
Colin Cross192e97a2018-02-22 14:21:02 -08001274 path, err := pathForSource(ctx, pathComponents...)
Colin Cross1ccfcc32018-02-22 13:54:26 -08001275 if err != nil {
1276 reportPathError(ctx, err)
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001277 // No need to put the error message into the returned path since it has been reported already.
Colin Cross1ccfcc32018-02-22 13:54:26 -08001278 return OptionalPath{}
1279 }
Colin Crossc48c1432018-02-23 07:09:01 +00001280
Colin Crosse3924e12018-08-15 20:18:53 -07001281 if pathtools.IsGlob(path.String()) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001282 ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
Colin Crosse3924e12018-08-15 20:18:53 -07001283 return OptionalPath{}
1284 }
1285
Colin Cross192e97a2018-02-22 14:21:02 -08001286 exists, err := existsWithDependencies(ctx, path)
Colin Crossc48c1432018-02-23 07:09:01 +00001287 if err != nil {
1288 reportPathError(ctx, err)
1289 return OptionalPath{}
1290 }
Colin Cross192e97a2018-02-22 14:21:02 -08001291 if !exists {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001292 return InvalidOptionalPath(path.String() + " does not exist")
Colin Crossc48c1432018-02-23 07:09:01 +00001293 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001294 return OptionalPathForPath(path)
1295}
1296
1297func (p SourcePath) String() string {
Cole Faust483d1f72023-01-09 14:35:27 -08001298 if p.path == "" {
1299 return "."
1300 }
1301 return p.path
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001302}
1303
Colin Cross7707b242024-07-26 12:02:36 -07001304func (p SourcePath) WithoutRel() Path {
1305 p.basePath = p.basePath.withoutRel()
1306 return p
1307}
1308
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001309// Join creates a new SourcePath with paths... joined with the current path. The
1310// provided paths... may not use '..' to escape from the current path.
1311func (p SourcePath) Join(ctx PathContext, paths ...string) SourcePath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001312 path, err := validatePath(paths...)
1313 if err != nil {
1314 reportPathError(ctx, err)
1315 }
Colin Cross0db55682017-12-05 15:36:55 -08001316 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001317}
1318
Colin Cross2fafa3e2019-03-05 12:39:51 -08001319// join is like Join but does less path validation.
1320func (p SourcePath) join(ctx PathContext, paths ...string) SourcePath {
1321 path, err := validateSafePath(paths...)
1322 if err != nil {
1323 reportPathError(ctx, err)
1324 }
1325 return p.withRel(path)
1326}
1327
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001328// OverlayPath returns the overlay for `path' if it exists. This assumes that the
1329// SourcePath is the path to a resource overlay directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001330func (p SourcePath) OverlayPath(ctx ModuleMissingDepsPathContext, path Path) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001331 var relDir string
Colin Cross07e51612019-03-05 12:46:40 -08001332 if srcPath, ok := path.(SourcePath); ok {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001333 relDir = srcPath.path
1334 } else {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001335 ReportPathErrorf(ctx, "Cannot find relative path for %s(%s)", reflect.TypeOf(path).Name(), path)
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001336 // No need to put the error message into the returned path since it has been reported already.
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001337 return OptionalPath{}
1338 }
Cole Faust483d1f72023-01-09 14:35:27 -08001339 dir := filepath.Join(p.path, relDir)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001340 // Use Glob so that we are run again if the directory is added.
Colin Cross7f19f372016-11-01 11:10:25 -07001341 if pathtools.IsGlob(dir) {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001342 ReportPathErrorf(ctx, "Path may not contain a glob: %s", dir)
Dan Willemsen7b310ee2015-12-18 15:11:17 -08001343 }
Colin Cross461b4452018-02-23 09:22:42 -08001344 paths, err := ctx.GlobWithDeps(dir, nil)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001345 if err != nil {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001346 ReportPathErrorf(ctx, "glob: %s", err.Error())
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001347 return OptionalPath{}
1348 }
1349 if len(paths) == 0 {
Martin Stjernholmc32dd1c2021-09-15 02:39:00 +01001350 return InvalidOptionalPath(dir + " does not exist")
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001351 }
Cole Faust483d1f72023-01-09 14:35:27 -08001352 return OptionalPathForPath(PathForSource(ctx, paths[0]))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001353}
1354
Colin Cross70dda7e2019-10-01 22:05:35 -07001355// OutputPath is a Path representing an intermediates file path rooted from the build directory
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001356type OutputPath struct {
1357 basePath
Paul Duffind65c58b2021-03-24 09:22:07 +00001358
Colin Cross3b1c6842024-07-26 11:52:57 -07001359 // The base out directory for this path, either Config.SoongOutDir() or Config.OutDir()
1360 outDir string
Paul Duffind65c58b2021-03-24 09:22:07 +00001361
Colin Crossd63c9a72020-01-29 16:52:50 -08001362 fullPath string
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001363}
1364
Yu Liu467d7c52024-09-18 21:54:44 +00001365type outputPathGob struct {
1366 basePath
1367 OutDir string
1368 FullPath string
1369}
Yu Liufa297642024-06-11 00:13:02 +00001370
Yu Liu467d7c52024-09-18 21:54:44 +00001371func (p *OutputPath) ToGob() *outputPathGob {
1372 return &outputPathGob{
1373 basePath: p.basePath,
1374 OutDir: p.outDir,
1375 FullPath: p.fullPath,
1376 }
1377}
1378
1379func (p *OutputPath) FromGob(data *outputPathGob) {
1380 p.basePath = data.basePath
1381 p.outDir = data.OutDir
1382 p.fullPath = data.FullPath
1383}
1384
1385func (p OutputPath) GobEncode() ([]byte, error) {
1386 return blueprint.CustomGobEncode[outputPathGob](&p)
Yu Liufa297642024-06-11 00:13:02 +00001387}
1388
1389func (p *OutputPath) GobDecode(data []byte) error {
Yu Liu467d7c52024-09-18 21:54:44 +00001390 return blueprint.CustomGobDecode[outputPathGob](data, p)
Yu Liufa297642024-06-11 00:13:02 +00001391}
1392
Colin Cross702e0f82017-10-18 17:27:54 -07001393func (p OutputPath) withRel(rel string) OutputPath {
Colin Cross0db55682017-12-05 15:36:55 -08001394 p.basePath = p.basePath.withRel(rel)
Colin Crossd63c9a72020-01-29 16:52:50 -08001395 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross702e0f82017-10-18 17:27:54 -07001396 return p
1397}
1398
Colin Cross7707b242024-07-26 12:02:36 -07001399func (p OutputPath) WithoutRel() Path {
1400 p.basePath = p.basePath.withoutRel()
Colin Cross3063b782018-08-15 11:19:12 -07001401 return p
1402}
1403
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001404func (p OutputPath) getSoongOutDir() string {
Colin Cross3b1c6842024-07-26 11:52:57 -07001405 return p.outDir
Paul Duffin9b478b02019-12-10 13:41:51 +00001406}
1407
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001408func (p OutputPath) RelativeToTop() Path {
1409 return p.outputPathRelativeToTop()
1410}
1411
1412func (p OutputPath) outputPathRelativeToTop() OutputPath {
Colin Cross3b1c6842024-07-26 11:52:57 -07001413 p.fullPath = StringPathRelativeToTop(p.outDir, p.fullPath)
1414 if strings.HasSuffix(p.outDir, testOutSoongSubDir) {
1415 p.outDir = TestOutSoongDir
1416 } else {
1417 // Handle the PathForArbitraryOutput case
1418 p.outDir = testOutDir
1419 }
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001420 return p
1421}
1422
Paul Duffin0267d492021-02-02 10:05:52 +00001423func (p OutputPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
1424 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1425}
1426
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001427var _ Path = OutputPath{}
Paul Duffin9b478b02019-12-10 13:41:51 +00001428var _ WritablePath = OutputPath{}
Paul Duffin0267d492021-02-02 10:05:52 +00001429var _ objPathProvider = OutputPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001430
Chris Parsons8f232a22020-06-23 17:37:05 -04001431// toolDepPath is a Path representing a dependency of the build tool.
1432type toolDepPath struct {
1433 basePath
1434}
1435
Colin Cross7707b242024-07-26 12:02:36 -07001436func (t toolDepPath) WithoutRel() Path {
1437 t.basePath = t.basePath.withoutRel()
1438 return t
1439}
1440
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001441func (t toolDepPath) RelativeToTop() Path {
1442 ensureTestOnly()
1443 return t
1444}
1445
Chris Parsons8f232a22020-06-23 17:37:05 -04001446var _ Path = toolDepPath{}
1447
1448// pathForBuildToolDep returns a toolDepPath representing the given path string.
1449// There is no validation for the path, as it is "trusted": It may fail
1450// normal validation checks. For example, it may be an absolute path.
1451// Only use this function to construct paths for dependencies of the build
1452// tool invocation.
1453func pathForBuildToolDep(ctx PathContext, path string) toolDepPath {
Paul Duffin74abc5d2021-03-24 09:24:59 +00001454 return toolDepPath{basePath{path, ""}}
Chris Parsons8f232a22020-06-23 17:37:05 -04001455}
1456
Jeff Gaston734e3802017-04-10 15:47:24 -07001457// PathForOutput joins the provided paths and returns an OutputPath that is
1458// validated to not escape the build dir.
1459// On error, it will return a usable, but invalid OutputPath, and report a ModuleError.
1460func PathForOutput(ctx PathContext, pathComponents ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001461 path, err := validatePath(pathComponents...)
1462 if err != nil {
1463 reportPathError(ctx, err)
1464 }
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001465 fullPath := filepath.Join(ctx.Config().soongOutDir, path)
Colin Crossd63c9a72020-01-29 16:52:50 -08001466 path = fullPath[len(fullPath)-len(path):]
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001467 return OutputPath{basePath{path, ""}, ctx.Config().soongOutDir, fullPath}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001468}
1469
Colin Cross3b1c6842024-07-26 11:52:57 -07001470// PathsForOutput returns Paths rooted from outDir
Colin Cross40e33732019-02-15 11:08:35 -08001471func PathsForOutput(ctx PathContext, paths []string) WritablePaths {
1472 ret := make(WritablePaths, len(paths))
1473 for i, path := range paths {
1474 ret[i] = PathForOutput(ctx, path)
1475 }
1476 return ret
1477}
1478
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001479func (p OutputPath) writablePath() {}
1480
1481func (p OutputPath) String() string {
Colin Crossd63c9a72020-01-29 16:52:50 -08001482 return p.fullPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001483}
1484
1485// Join creates a new OutputPath with paths... joined with the current path. The
1486// provided paths... may not use '..' to escape from the current path.
1487func (p OutputPath) Join(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001488 path, err := validatePath(paths...)
1489 if err != nil {
1490 reportPathError(ctx, err)
1491 }
Colin Cross0db55682017-12-05 15:36:55 -08001492 return p.withRel(path)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001493}
1494
Colin Cross8854a5a2019-02-11 14:14:16 -08001495// ReplaceExtension creates a new OutputPath with the extension replaced with ext.
1496func (p OutputPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1497 if strings.Contains(ext, "/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001498 ReportPathErrorf(ctx, "extension %q cannot contain /", ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001499 }
1500 ret := PathForOutput(ctx, pathtools.ReplaceExtension(p.path, ext))
Colin Cross2cdd5df2019-02-25 10:25:24 -08001501 ret.rel = pathtools.ReplaceExtension(p.rel, ext)
Colin Cross8854a5a2019-02-11 14:14:16 -08001502 return ret
1503}
1504
Colin Cross40e33732019-02-15 11:08:35 -08001505// InSameDir creates a new OutputPath from the directory of the current OutputPath joined with the elements in paths.
1506func (p OutputPath) InSameDir(ctx PathContext, paths ...string) OutputPath {
1507 path, err := validatePath(paths...)
1508 if err != nil {
1509 reportPathError(ctx, err)
1510 }
1511
1512 ret := PathForOutput(ctx, filepath.Dir(p.path), path)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001513 ret.rel = filepath.Join(filepath.Dir(p.rel), path)
Colin Cross40e33732019-02-15 11:08:35 -08001514 return ret
1515}
1516
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001517// PathForIntermediates returns an OutputPath representing the top-level
1518// intermediates directory.
1519func PathForIntermediates(ctx PathContext, paths ...string) OutputPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001520 path, err := validatePath(paths...)
1521 if err != nil {
1522 reportPathError(ctx, err)
1523 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001524 return PathForOutput(ctx, ".intermediates", path)
1525}
1526
Colin Cross07e51612019-03-05 12:46:40 -08001527var _ genPathProvider = SourcePath{}
1528var _ objPathProvider = SourcePath{}
1529var _ resPathProvider = SourcePath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001530
Colin Cross07e51612019-03-05 12:46:40 -08001531// PathForModuleSrc returns a Path representing the paths... under the
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001532// module's local source directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001533func PathForModuleSrc(ctx ModuleMissingDepsPathContext, pathComponents ...string) Path {
Paul Duffin407501b2021-07-09 16:56:35 +01001534 // Just join the components textually just to make sure that it does not corrupt a fully qualified
1535 // module reference, e.g. if the pathComponents is "://other:foo" then using filepath.Join() or
1536 // validatePath() will corrupt it, e.g. replace "//" with "/". If the path is not a module
1537 // reference then it will be validated by expandOneSrcPath anyway when it calls expandOneSrcPath.
1538 p := strings.Join(pathComponents, string(filepath.Separator))
Liz Kammer619be462022-01-28 15:13:39 -05001539 paths, err := expandOneSrcPath(sourcePathInput{context: ctx, path: p, includeDirs: true})
Colin Cross8a497952019-03-05 22:25:09 -08001540 if err != nil {
1541 if depErr, ok := err.(missingDependencyError); ok {
1542 if ctx.Config().AllowMissingDependencies() {
1543 ctx.AddMissingDependencies(depErr.missingDeps)
1544 } else {
1545 ctx.ModuleErrorf(`%s, is the property annotated with android:"path"?`, depErr.Error())
1546 }
1547 } else {
1548 reportPathError(ctx, err)
1549 }
1550 return nil
1551 } else if len(paths) == 0 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001552 ReportPathErrorf(ctx, "%q produced no files, expected exactly one", p)
Colin Cross8a497952019-03-05 22:25:09 -08001553 return nil
1554 } else if len(paths) > 1 {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01001555 ReportPathErrorf(ctx, "%q produced %d files, expected exactly one", p, len(paths))
Colin Cross8a497952019-03-05 22:25:09 -08001556 }
1557 return paths[0]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001558}
1559
Liz Kammera830f3a2020-11-10 10:50:34 -08001560func pathForModuleSrc(ctx EarlyModulePathContext, paths ...string) SourcePath {
Colin Cross07e51612019-03-05 12:46:40 -08001561 p, err := validatePath(paths...)
1562 if err != nil {
1563 reportPathError(ctx, err)
1564 }
1565
1566 path, err := pathForSource(ctx, ctx.ModuleDir(), p)
1567 if err != nil {
1568 reportPathError(ctx, err)
1569 }
1570
1571 path.basePath.rel = p
1572
1573 return path
1574}
1575
Colin Cross2fafa3e2019-03-05 12:39:51 -08001576// PathsWithModuleSrcSubDir takes a list of Paths and returns a new list of Paths where Rel() on each path
1577// will return the path relative to subDir in the module's source directory. If any input paths are not located
1578// inside subDir then a path error will be reported.
Liz Kammera830f3a2020-11-10 10:50:34 -08001579func PathsWithModuleSrcSubDir(ctx EarlyModulePathContext, paths Paths, subDir string) Paths {
Colin Cross2fafa3e2019-03-05 12:39:51 -08001580 paths = append(Paths(nil), paths...)
Colin Cross07e51612019-03-05 12:46:40 -08001581 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001582 for i, path := range paths {
1583 rel := Rel(ctx, subDirFullPath.String(), path.String())
1584 paths[i] = subDirFullPath.join(ctx, rel)
1585 }
1586 return paths
1587}
1588
1589// PathWithModuleSrcSubDir takes a Path and returns a Path where Rel() will return the path relative to subDir in the
1590// 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 -08001591func PathWithModuleSrcSubDir(ctx EarlyModulePathContext, path Path, subDir string) Path {
Colin Cross07e51612019-03-05 12:46:40 -08001592 subDirFullPath := pathForModuleSrc(ctx, subDir)
Colin Cross2fafa3e2019-03-05 12:39:51 -08001593 rel := Rel(ctx, subDirFullPath.String(), path.String())
1594 return subDirFullPath.Join(ctx, rel)
1595}
1596
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001597// OptionalPathForModuleSrc returns an OptionalPath. The OptionalPath contains a
1598// valid path if p is non-nil.
Liz Kammera830f3a2020-11-10 10:50:34 -08001599func OptionalPathForModuleSrc(ctx ModuleMissingDepsPathContext, p *string) OptionalPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001600 if p == nil {
1601 return OptionalPath{}
1602 }
1603 return OptionalPathForPath(PathForModuleSrc(ctx, *p))
1604}
1605
Liz Kammera830f3a2020-11-10 10:50:34 -08001606func (p SourcePath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001607 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001608}
1609
yangbill6d032dd2024-04-18 03:05:49 +00001610func (p SourcePath) genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath {
1611 // If Trim_extension being set, force append Output_extension without replace original extension.
1612 if trimExt != "" {
1613 if ext != "" {
1614 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt)+"."+ext)
1615 }
1616 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt))
1617 }
1618 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1619}
1620
Liz Kammera830f3a2020-11-10 10:50:34 -08001621func (p SourcePath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Colin Cross7fc17db2017-02-01 14:07:55 -08001622 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001623}
1624
Liz Kammera830f3a2020-11-10 10:50:34 -08001625func (p SourcePath) resPathWithName(ctx ModuleOutPathContext, name string) ModuleResPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001626 // TODO: Use full directory if the new ctx is not the current ctx?
1627 return PathForModuleRes(ctx, p.path, name)
1628}
1629
1630// ModuleOutPath is a Path representing a module's output directory.
1631type ModuleOutPath struct {
1632 OutputPath
1633}
1634
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001635func (p ModuleOutPath) RelativeToTop() Path {
1636 p.OutputPath = p.outputPathRelativeToTop()
1637 return p
1638}
1639
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001640var _ Path = ModuleOutPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001641var _ WritablePath = ModuleOutPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001642
Liz Kammera830f3a2020-11-10 10:50:34 -08001643func (p ModuleOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01001644 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1645}
1646
Liz Kammera830f3a2020-11-10 10:50:34 -08001647// ModuleOutPathContext Subset of ModuleContext functions necessary for output path methods.
1648type ModuleOutPathContext interface {
1649 PathContext
1650
1651 ModuleName() string
1652 ModuleDir() string
1653 ModuleSubDir() string
1654}
1655
1656func pathForModuleOut(ctx ModuleOutPathContext) OutputPath {
Inseob Kimb7e9f5f2024-06-25 17:39:52 +09001657 return PathForOutput(ctx, ".intermediates", ctx.ModuleDir(), ctx.ModuleName(), ctx.ModuleSubDir())
Colin Cross702e0f82017-10-18 17:27:54 -07001658}
1659
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001660// PathForModuleOut returns a Path representing the paths... under the module's
1661// output directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001662func PathForModuleOut(ctx ModuleOutPathContext, paths ...string) ModuleOutPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001663 p, err := validatePath(paths...)
1664 if err != nil {
1665 reportPathError(ctx, err)
1666 }
Colin Cross702e0f82017-10-18 17:27:54 -07001667 return ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001668 OutputPath: pathForModuleOut(ctx).withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001669 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001670}
1671
1672// ModuleGenPath is a Path representing the 'gen' directory in a module's output
1673// directory. Mainly used for generated sources.
1674type ModuleGenPath struct {
1675 ModuleOutPath
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001676}
1677
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001678func (p ModuleGenPath) RelativeToTop() Path {
1679 p.OutputPath = p.outputPathRelativeToTop()
1680 return p
1681}
1682
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001683var _ Path = ModuleGenPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001684var _ WritablePath = ModuleGenPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001685var _ genPathProvider = ModuleGenPath{}
1686var _ objPathProvider = ModuleGenPath{}
1687
1688// PathForModuleGen returns a Path representing the paths... under the module's
1689// `gen' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001690func PathForModuleGen(ctx ModuleOutPathContext, paths ...string) ModuleGenPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001691 p, err := validatePath(paths...)
1692 if err != nil {
1693 reportPathError(ctx, err)
1694 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001695 return ModuleGenPath{
Colin Cross702e0f82017-10-18 17:27:54 -07001696 ModuleOutPath: ModuleOutPath{
Liz Kammera830f3a2020-11-10 10:50:34 -08001697 OutputPath: pathForModuleOut(ctx).withRel("gen").withRel(p),
Colin Cross702e0f82017-10-18 17:27:54 -07001698 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001699 }
1700}
1701
Liz Kammera830f3a2020-11-10 10:50:34 -08001702func (p ModuleGenPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001703 // TODO: make a different path for local vs remote generated files?
Dan Willemsen21ec4902016-11-02 20:43:13 -07001704 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001705}
1706
yangbill6d032dd2024-04-18 03:05:49 +00001707func (p ModuleGenPath) genPathWithExtAndTrimExt(ctx ModuleOutPathContext, subdir, ext string, trimExt string) ModuleGenPath {
1708 // If Trim_extension being set, force append Output_extension without replace original extension.
1709 if trimExt != "" {
1710 if ext != "" {
1711 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt)+"."+ext)
1712 }
1713 return PathForModuleGen(ctx, subdir, strings.TrimSuffix(p.path, trimExt))
1714 }
1715 return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1716}
1717
Liz Kammera830f3a2020-11-10 10:50:34 -08001718func (p ModuleGenPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath {
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001719 return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext))
1720}
1721
1722// ModuleObjPath is a Path representing the 'obj' directory in a module's output
1723// directory. Used for compiled objects.
1724type ModuleObjPath struct {
1725 ModuleOutPath
1726}
1727
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001728func (p ModuleObjPath) RelativeToTop() Path {
1729 p.OutputPath = p.outputPathRelativeToTop()
1730 return p
1731}
1732
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001733var _ Path = ModuleObjPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001734var _ WritablePath = ModuleObjPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001735
1736// PathForModuleObj returns a Path representing the paths... under the module's
1737// 'obj' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001738func PathForModuleObj(ctx ModuleOutPathContext, pathComponents ...string) ModuleObjPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001739 p, err := validatePath(pathComponents...)
1740 if err != nil {
1741 reportPathError(ctx, err)
1742 }
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001743 return ModuleObjPath{PathForModuleOut(ctx, "obj", p)}
1744}
1745
1746// ModuleResPath is a a Path representing the 'res' directory in a module's
1747// output directory.
1748type ModuleResPath struct {
1749 ModuleOutPath
1750}
1751
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001752func (p ModuleResPath) RelativeToTop() Path {
1753 p.OutputPath = p.outputPathRelativeToTop()
1754 return p
1755}
1756
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001757var _ Path = ModuleResPath{}
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001758var _ WritablePath = ModuleResPath{}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001759
1760// PathForModuleRes returns a Path representing the paths... under the module's
1761// 'res' directory.
Liz Kammera830f3a2020-11-10 10:50:34 -08001762func PathForModuleRes(ctx ModuleOutPathContext, pathComponents ...string) ModuleResPath {
Colin Cross1ccfcc32018-02-22 13:54:26 -08001763 p, err := validatePath(pathComponents...)
1764 if err != nil {
1765 reportPathError(ctx, err)
1766 }
1767
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001768 return ModuleResPath{PathForModuleOut(ctx, "res", p)}
1769}
1770
Colin Cross70dda7e2019-10-01 22:05:35 -07001771// InstallPath is a Path representing a installed file path rooted from the build directory
1772type InstallPath struct {
1773 basePath
Colin Crossff6c33d2019-10-02 16:01:35 -07001774
Lukacs T. Berkib078ade2021-08-31 10:42:08 +02001775 // The soong build directory, i.e. Config.SoongOutDir()
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001776 soongOutDir string
Paul Duffind65c58b2021-03-24 09:22:07 +00001777
Jiyong Park957bcd92020-10-20 18:23:33 +09001778 // partitionDir is the part of the InstallPath that is automatically determined according to the context.
1779 // For example, it is host/<os>-<arch> for host modules, and target/product/<device>/<partition> for device modules.
1780 partitionDir string
1781
Colin Crossb1692a32021-10-25 15:39:01 -07001782 partition string
1783
Jiyong Park957bcd92020-10-20 18:23:33 +09001784 // makePath indicates whether this path is for Soong (false) or Make (true).
1785 makePath bool
Colin Crossc0e42d52024-02-01 16:42:36 -08001786
1787 fullPath string
Colin Cross70dda7e2019-10-01 22:05:35 -07001788}
1789
Yu Liu467d7c52024-09-18 21:54:44 +00001790type installPathGob struct {
1791 basePath
1792 SoongOutDir string
1793 PartitionDir string
1794 Partition string
1795 MakePath bool
1796 FullPath string
1797}
Yu Liu26a716d2024-08-30 23:40:32 +00001798
Yu Liu467d7c52024-09-18 21:54:44 +00001799func (p *InstallPath) ToGob() *installPathGob {
1800 return &installPathGob{
1801 basePath: p.basePath,
1802 SoongOutDir: p.soongOutDir,
1803 PartitionDir: p.partitionDir,
1804 Partition: p.partition,
1805 MakePath: p.makePath,
1806 FullPath: p.fullPath,
1807 }
1808}
1809
1810func (p *InstallPath) FromGob(data *installPathGob) {
1811 p.basePath = data.basePath
1812 p.soongOutDir = data.SoongOutDir
1813 p.partitionDir = data.PartitionDir
1814 p.partition = data.Partition
1815 p.makePath = data.MakePath
1816 p.fullPath = data.FullPath
1817}
1818
1819func (p InstallPath) GobEncode() ([]byte, error) {
1820 return blueprint.CustomGobEncode[installPathGob](&p)
Yu Liu26a716d2024-08-30 23:40:32 +00001821}
1822
1823func (p *InstallPath) GobDecode(data []byte) error {
Yu Liu467d7c52024-09-18 21:54:44 +00001824 return blueprint.CustomGobDecode[installPathGob](data, p)
Yu Liu26a716d2024-08-30 23:40:32 +00001825}
1826
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001827// Will panic if called from outside a test environment.
1828func ensureTestOnly() {
Martin Stjernholm32312eb2021-03-27 18:54:49 +00001829 if PrefixInList(os.Args, "-test.") {
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001830 return
1831 }
Martin Stjernholm32312eb2021-03-27 18:54:49 +00001832 panic(fmt.Errorf("Not in test. Command line:\n %s", strings.Join(os.Args, "\n ")))
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001833}
1834
1835func (p InstallPath) RelativeToTop() Path {
1836 ensureTestOnly()
Colin Crossc0e42d52024-02-01 16:42:36 -08001837 if p.makePath {
Colin Cross3b1c6842024-07-26 11:52:57 -07001838 p.soongOutDir = testOutDir
Colin Crossc0e42d52024-02-01 16:42:36 -08001839 } else {
Colin Cross3b1c6842024-07-26 11:52:57 -07001840 p.soongOutDir = TestOutSoongDir
Colin Crossc0e42d52024-02-01 16:42:36 -08001841 }
1842 p.fullPath = filepath.Join(p.soongOutDir, p.path)
Paul Duffin85d8f0d2021-03-24 10:18:18 +00001843 return p
1844}
1845
Colin Cross7707b242024-07-26 12:02:36 -07001846func (p InstallPath) WithoutRel() Path {
1847 p.basePath = p.basePath.withoutRel()
1848 return p
1849}
1850
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001851func (p InstallPath) getSoongOutDir() string {
1852 return p.soongOutDir
Paul Duffin9b478b02019-12-10 13:41:51 +00001853}
1854
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01001855func (p InstallPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
1856 panic("Not implemented")
1857}
1858
Paul Duffin9b478b02019-12-10 13:41:51 +00001859var _ Path = InstallPath{}
1860var _ WritablePath = InstallPath{}
1861
Colin Cross70dda7e2019-10-01 22:05:35 -07001862func (p InstallPath) writablePath() {}
1863
1864func (p InstallPath) String() string {
Colin Crossc0e42d52024-02-01 16:42:36 -08001865 return p.fullPath
Jiyong Park957bcd92020-10-20 18:23:33 +09001866}
1867
1868// PartitionDir returns the path to the partition where the install path is rooted at. It is
1869// out/soong/target/product/<device>/<partition> for device modules, and out/soong/host/<os>-<arch> for host modules.
1870// The ./soong is dropped if the install path is for Make.
1871func (p InstallPath) PartitionDir() string {
1872 if p.makePath {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001873 return filepath.Join(p.soongOutDir, "../", p.partitionDir)
Jiyong Park957bcd92020-10-20 18:23:33 +09001874 } else {
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02001875 return filepath.Join(p.soongOutDir, p.partitionDir)
Jiyong Park957bcd92020-10-20 18:23:33 +09001876 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001877}
1878
Jihoon Kangf78a8902022-09-01 22:47:07 +00001879func (p InstallPath) Partition() string {
1880 return p.partition
1881}
1882
Colin Cross70dda7e2019-10-01 22:05:35 -07001883// Join creates a new InstallPath with paths... joined with the current path. The
1884// provided paths... may not use '..' to escape from the current path.
1885func (p InstallPath) Join(ctx PathContext, paths ...string) InstallPath {
1886 path, err := validatePath(paths...)
1887 if err != nil {
1888 reportPathError(ctx, err)
1889 }
1890 return p.withRel(path)
1891}
1892
1893func (p InstallPath) withRel(rel string) InstallPath {
1894 p.basePath = p.basePath.withRel(rel)
Colin Crossc0e42d52024-02-01 16:42:36 -08001895 p.fullPath = filepath.Join(p.fullPath, rel)
Colin Cross70dda7e2019-10-01 22:05:35 -07001896 return p
1897}
1898
Colin Crossc68db4b2021-11-11 18:59:15 -08001899// Deprecated: ToMakePath is a noop, PathForModuleInstall always returns Make paths when building
1900// embedded in Make.
Colin Crossff6c33d2019-10-02 16:01:35 -07001901func (p InstallPath) ToMakePath() InstallPath {
Jiyong Park957bcd92020-10-20 18:23:33 +09001902 p.makePath = true
Colin Crossff6c33d2019-10-02 16:01:35 -07001903 return p
Colin Cross70dda7e2019-10-01 22:05:35 -07001904}
1905
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001906// PathForModuleInstall returns a Path representing the install path for the
1907// module appended with paths...
Colin Cross70dda7e2019-10-01 22:05:35 -07001908func PathForModuleInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Spandan Das5d1b9292021-06-03 19:36:41 +00001909 os, arch := osAndArch(ctx)
Cole Faust11edf552023-10-13 11:32:14 -07001910 partition := modulePartition(ctx, os.Class == Device)
Cole Faust3b703f32023-10-16 13:30:51 -07001911 return pathForInstall(ctx, os, arch, partition, pathComponents...)
Spandan Das5d1b9292021-06-03 19:36:41 +00001912}
1913
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001914// PathForHostDexInstall returns an InstallPath representing the install path for the
1915// module appended with paths...
1916func PathForHostDexInstall(ctx ModuleInstallPathContext, pathComponents ...string) InstallPath {
Cole Faust3b703f32023-10-16 13:30:51 -07001917 return pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "", pathComponents...)
Colin Cross1d0eb7a2021-11-03 14:08:20 -07001918}
1919
Spandan Das5d1b9292021-06-03 19:36:41 +00001920// PathForModuleInPartitionInstall is similar to PathForModuleInstall but partition is provided by the caller
1921func PathForModuleInPartitionInstall(ctx ModuleInstallPathContext, partition string, pathComponents ...string) InstallPath {
1922 os, arch := osAndArch(ctx)
Cole Faust3b703f32023-10-16 13:30:51 -07001923 return pathForInstall(ctx, os, arch, partition, pathComponents...)
Spandan Das5d1b9292021-06-03 19:36:41 +00001924}
1925
1926func osAndArch(ctx ModuleInstallPathContext) (OsType, ArchType) {
Colin Cross6e359402020-02-10 15:29:54 -08001927 os := ctx.Os()
Jiyong Park87788b52020-09-01 12:37:45 +09001928 arch := ctx.Arch().ArchType
1929 forceOS, forceArch := ctx.InstallForceOS()
1930 if forceOS != nil {
Colin Cross6e359402020-02-10 15:29:54 -08001931 os = *forceOS
1932 }
Jiyong Park87788b52020-09-01 12:37:45 +09001933 if forceArch != nil {
1934 arch = *forceArch
1935 }
Spandan Das5d1b9292021-06-03 19:36:41 +00001936 return os, arch
1937}
Colin Cross609c49a2020-02-13 13:20:11 -08001938
Colin Crossc0e42d52024-02-01 16:42:36 -08001939func pathForPartitionInstallDir(ctx PathContext, partition, partitionPath string, makePath bool) InstallPath {
1940 fullPath := ctx.Config().SoongOutDir()
1941 if makePath {
1942 // Make path starts with out/ instead of out/soong.
1943 fullPath = filepath.Join(fullPath, "../", partitionPath)
1944 } else {
1945 fullPath = filepath.Join(fullPath, partitionPath)
1946 }
1947
1948 return InstallPath{
1949 basePath: basePath{partitionPath, ""},
1950 soongOutDir: ctx.Config().soongOutDir,
1951 partitionDir: partitionPath,
1952 partition: partition,
1953 makePath: makePath,
1954 fullPath: fullPath,
1955 }
1956}
1957
Cole Faust3b703f32023-10-16 13:30:51 -07001958func pathForInstall(ctx PathContext, os OsType, arch ArchType, partition string,
Colin Cross609c49a2020-02-13 13:20:11 -08001959 pathComponents ...string) InstallPath {
1960
Jiyong Park97859152023-02-14 17:05:48 +09001961 var partitionPaths []string
Colin Cross609c49a2020-02-13 13:20:11 -08001962
Colin Cross6e359402020-02-10 15:29:54 -08001963 if os.Class == Device {
Jiyong Park97859152023-02-14 17:05:48 +09001964 partitionPaths = []string{"target", "product", ctx.Config().DeviceName(), partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001965 } else {
Jiyong Park87788b52020-09-01 12:37:45 +09001966 osName := os.String()
Colin Crossa9b2aac2022-06-15 17:25:51 -07001967 if os == Linux {
Jiyong Park87788b52020-09-01 12:37:45 +09001968 // instead of linux_glibc
1969 osName = "linux"
Dan Willemsen866b5632017-09-22 12:28:24 -07001970 }
Colin Crossa9b2aac2022-06-15 17:25:51 -07001971 if os == LinuxMusl && ctx.Config().UseHostMusl() {
1972 // When using musl instead of glibc, use "linux" instead of "linux_musl". When cross
1973 // compiling we will still use "linux_musl".
1974 osName = "linux"
1975 }
1976
Jiyong Park87788b52020-09-01 12:37:45 +09001977 // SOONG_HOST_OUT is set to out/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
1978 // and HOST_PREBUILT_ARCH is forcibly set to x86 even on x86_64 hosts. We don't seem
1979 // to have a plan to fix it (see the comment in build/make/core/envsetup.mk).
1980 // Let's keep using x86 for the existing cases until we have a need to support
1981 // other architectures.
1982 archName := arch.String()
1983 if os.Class == Host && (arch == X86_64 || arch == Common) {
1984 archName = "x86"
1985 }
Jiyong Park97859152023-02-14 17:05:48 +09001986 partitionPaths = []string{"host", osName + "-" + archName, partition}
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001987 }
Colin Cross70dda7e2019-10-01 22:05:35 -07001988
Jiyong Park97859152023-02-14 17:05:48 +09001989 partitionPath, err := validatePath(partitionPaths...)
Colin Cross70dda7e2019-10-01 22:05:35 -07001990 if err != nil {
1991 reportPathError(ctx, err)
1992 }
Colin Crossff6c33d2019-10-02 16:01:35 -07001993
Colin Crossc0e42d52024-02-01 16:42:36 -08001994 base := pathForPartitionInstallDir(ctx, partition, partitionPath, ctx.Config().KatiEnabled())
Jiyong Park957bcd92020-10-20 18:23:33 +09001995 return base.Join(ctx, pathComponents...)
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001996}
1997
Spandan Dasf280b232024-04-04 21:25:51 +00001998func PathForNdkInstall(ctx PathContext, paths ...string) OutputPath {
1999 return PathForOutput(ctx, append([]string{"ndk"}, paths...)...)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00002000}
2001
2002func PathForMainlineSdksInstall(ctx PathContext, paths ...string) InstallPath {
Spandan Dasf280b232024-04-04 21:25:51 +00002003 base := pathForPartitionInstallDir(ctx, "", "mainline-sdks", false)
2004 return base.Join(ctx, paths...)
Nicolas Geoffray1228e9c2020-02-27 13:45:35 +00002005}
2006
Colin Cross70dda7e2019-10-01 22:05:35 -07002007func InstallPathToOnDevicePath(ctx PathContext, path InstallPath) string {
Colin Crossb1692a32021-10-25 15:39:01 -07002008 rel := Rel(ctx, strings.TrimSuffix(path.PartitionDir(), path.partition), path.String())
Colin Cross43f08db2018-11-12 10:13:39 -08002009 return "/" + rel
2010}
2011
Cole Faust11edf552023-10-13 11:32:14 -07002012func modulePartition(ctx ModuleInstallPathContext, device bool) string {
Colin Cross43f08db2018-11-12 10:13:39 -08002013 var partition string
Colin Cross6e359402020-02-10 15:29:54 -08002014 if ctx.InstallInTestcases() {
2015 // "testcases" install directory can be used for host or device modules.
Jaewoong Jung0949f312019-09-11 10:25:18 -07002016 partition = "testcases"
Cole Faust11edf552023-10-13 11:32:14 -07002017 } else if device {
Colin Cross6e359402020-02-10 15:29:54 -08002018 if ctx.InstallInData() {
2019 partition = "data"
2020 } else if ctx.InstallInRamdisk() {
2021 if ctx.DeviceConfig().BoardUsesRecoveryAsBoot() {
2022 partition = "recovery/root/first_stage_ramdisk"
2023 } else {
2024 partition = "ramdisk"
2025 }
2026 if !ctx.InstallInRoot() {
2027 partition += "/system"
2028 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -07002029 } else if ctx.InstallInVendorRamdisk() {
Yifan Hong39143a92020-10-26 12:43:12 -07002030 // The module is only available after switching root into
2031 // /first_stage_ramdisk. To expose the module before switching root
2032 // on a device without a dedicated recovery partition, install the
2033 // recovery variant.
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002034 if ctx.DeviceConfig().BoardMoveRecoveryResourcesToVendorBoot() {
Petri Gyntherac229562021-03-02 23:44:02 -08002035 partition = "vendor_ramdisk/first_stage_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002036 } else {
Petri Gyntherac229562021-03-02 23:44:02 -08002037 partition = "vendor_ramdisk"
Yifan Hongdd8dacc2020-10-21 15:40:17 -07002038 }
2039 if !ctx.InstallInRoot() {
2040 partition += "/system"
2041 }
Inseob Kim08758f02021-04-08 21:13:22 +09002042 } else if ctx.InstallInDebugRamdisk() {
2043 partition = "debug_ramdisk"
Colin Cross6e359402020-02-10 15:29:54 -08002044 } else if ctx.InstallInRecovery() {
2045 if ctx.InstallInRoot() {
2046 partition = "recovery/root"
2047 } else {
2048 // the layout of recovery partion is the same as that of system partition
2049 partition = "recovery/root/system"
2050 }
Colin Crossea30d852023-11-29 16:00:16 -08002051 } else if ctx.SocSpecific() || ctx.InstallInVendor() {
Colin Cross6e359402020-02-10 15:29:54 -08002052 partition = ctx.DeviceConfig().VendorPath()
Colin Crossea30d852023-11-29 16:00:16 -08002053 } else if ctx.DeviceSpecific() || ctx.InstallInOdm() {
Colin Cross6e359402020-02-10 15:29:54 -08002054 partition = ctx.DeviceConfig().OdmPath()
Colin Crossea30d852023-11-29 16:00:16 -08002055 } else if ctx.ProductSpecific() || ctx.InstallInProduct() {
Colin Cross6e359402020-02-10 15:29:54 -08002056 partition = ctx.DeviceConfig().ProductPath()
2057 } else if ctx.SystemExtSpecific() {
2058 partition = ctx.DeviceConfig().SystemExtPath()
2059 } else if ctx.InstallInRoot() {
2060 partition = "root"
Yifan Hong82db7352020-01-21 16:12:26 -08002061 } else {
Colin Cross6e359402020-02-10 15:29:54 -08002062 partition = "system"
Yifan Hong82db7352020-01-21 16:12:26 -08002063 }
Colin Cross6e359402020-02-10 15:29:54 -08002064 if ctx.InstallInSanitizerDir() {
2065 partition = "data/asan/" + partition
Yifan Hong82db7352020-01-21 16:12:26 -08002066 }
Colin Cross43f08db2018-11-12 10:13:39 -08002067 }
2068 return partition
2069}
2070
Colin Cross609c49a2020-02-13 13:20:11 -08002071type InstallPaths []InstallPath
2072
2073// Paths returns the InstallPaths as a Paths
2074func (p InstallPaths) Paths() Paths {
2075 if p == nil {
2076 return nil
2077 }
2078 ret := make(Paths, len(p))
2079 for i, path := range p {
2080 ret[i] = path
2081 }
2082 return ret
2083}
2084
2085// Strings returns the string forms of the install paths.
2086func (p InstallPaths) Strings() []string {
2087 if p == nil {
2088 return nil
2089 }
2090 ret := make([]string, len(p))
2091 for i, path := range p {
2092 ret[i] = path.String()
2093 }
2094 return ret
2095}
2096
Jingwen Chen24d0c562023-02-07 09:29:36 +00002097// validatePathInternal ensures that a path does not leave its component, and
2098// optionally doesn't contain Ninja variables.
2099func validatePathInternal(allowNinjaVariables bool, pathComponents ...string) (string, error) {
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002100 initialEmpty := 0
2101 finalEmpty := 0
2102 for i, path := range pathComponents {
Jingwen Chen24d0c562023-02-07 09:29:36 +00002103 if !allowNinjaVariables && strings.Contains(path, "$") {
2104 return "", fmt.Errorf("Path contains invalid character($): %s", path)
2105 }
2106
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002107 path := filepath.Clean(path)
2108 if path == ".." || strings.HasPrefix(path, "../") || strings.HasPrefix(path, "/") {
Colin Cross1ccfcc32018-02-22 13:54:26 -08002109 return "", fmt.Errorf("Path is outside directory: %s", path)
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002110 }
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002111
2112 if i == initialEmpty && pathComponents[i] == "" {
2113 initialEmpty++
2114 }
2115 if i == finalEmpty && pathComponents[len(pathComponents)-1-i] == "" {
2116 finalEmpty++
2117 }
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002118 }
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002119 // Optimization: filepath.Join("foo", "") returns a newly allocated copy
2120 // of "foo", while filepath.Join("foo") does not. Strip out any empty
2121 // path components.
2122 if initialEmpty == len(pathComponents) {
2123 return "", nil
2124 }
2125 nonEmptyPathComponents := pathComponents[initialEmpty : len(pathComponents)-finalEmpty]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002126 // TODO: filepath.Join isn't necessarily correct with embedded ninja
2127 // variables. '..' may remove the entire ninja variable, even if it
2128 // will be expanded to multiple nested directories.
Colin Crossbf9ed3f2023-10-24 14:17:03 -07002129 return filepath.Join(nonEmptyPathComponents...), nil
Dan Willemsen34cc69e2015-09-23 15:26:20 -07002130}
2131
Jingwen Chen24d0c562023-02-07 09:29:36 +00002132// validateSafePath validates a path that we trust (may contain ninja
2133// variables). Ensures that each path component does not attempt to leave its
2134// component. Returns a joined version of each path component.
2135func validateSafePath(pathComponents ...string) (string, error) {
2136 return validatePathInternal(true, pathComponents...)
2137}
2138
Dan Willemsen80a7c2a2015-12-21 14:57:11 -08002139// validatePath validates that a path does not include ninja variables, and that
2140// each path component does not attempt to leave its component. Returns a joined
2141// version of each path component.
Colin Cross1ccfcc32018-02-22 13:54:26 -08002142func validatePath(pathComponents ...string) (string, error) {
Jingwen Chen24d0c562023-02-07 09:29:36 +00002143 return validatePathInternal(false, pathComponents...)
Colin Cross6e18ca42015-07-14 18:55:36 -07002144}
Colin Cross5b529592017-05-09 13:34:34 -07002145
Colin Cross0875c522017-11-28 17:34:01 -08002146func PathForPhony(ctx PathContext, phony string) WritablePath {
2147 if strings.ContainsAny(phony, "$/") {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002148 ReportPathErrorf(ctx, "Phony target contains invalid character ($ or /): %s", phony)
Colin Cross0875c522017-11-28 17:34:01 -08002149 }
Paul Duffin74abc5d2021-03-24 09:24:59 +00002150 return PhonyPath{basePath{phony, ""}}
Colin Cross0875c522017-11-28 17:34:01 -08002151}
2152
Colin Cross74e3fe42017-12-11 15:51:44 -08002153type PhonyPath struct {
2154 basePath
2155}
2156
2157func (p PhonyPath) writablePath() {}
2158
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +02002159func (p PhonyPath) getSoongOutDir() string {
Paul Duffind65c58b2021-03-24 09:22:07 +00002160 // A phone path cannot contain any / so cannot be relative to the build directory.
2161 return ""
Paul Duffin9b478b02019-12-10 13:41:51 +00002162}
2163
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002164func (p PhonyPath) RelativeToTop() Path {
2165 ensureTestOnly()
2166 // A phony path cannot contain any / so does not have a build directory so switching to a new
2167 // build directory has no effect so just return this path.
2168 return p
2169}
2170
Colin Cross7707b242024-07-26 12:02:36 -07002171func (p PhonyPath) WithoutRel() Path {
2172 p.basePath = p.basePath.withoutRel()
2173 return p
2174}
2175
Hans MÃ¥nssond3f2bd72020-11-27 12:37:28 +01002176func (p PhonyPath) ReplaceExtension(ctx PathContext, ext string) OutputPath {
2177 panic("Not implemented")
2178}
2179
Colin Cross74e3fe42017-12-11 15:51:44 -08002180var _ Path = PhonyPath{}
2181var _ WritablePath = PhonyPath{}
2182
Colin Cross5b529592017-05-09 13:34:34 -07002183type testPath struct {
2184 basePath
2185}
2186
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002187func (p testPath) RelativeToTop() Path {
2188 ensureTestOnly()
2189 return p
2190}
2191
Colin Cross7707b242024-07-26 12:02:36 -07002192func (p testPath) WithoutRel() Path {
2193 p.basePath = p.basePath.withoutRel()
2194 return p
2195}
2196
Colin Cross5b529592017-05-09 13:34:34 -07002197func (p testPath) String() string {
2198 return p.path
2199}
2200
Paul Duffin85d8f0d2021-03-24 10:18:18 +00002201var _ Path = testPath{}
2202
Colin Cross40e33732019-02-15 11:08:35 -08002203// PathForTesting returns a Path constructed from joining the elements of paths with '/'. It should only be used from
2204// within tests.
Colin Cross5b529592017-05-09 13:34:34 -07002205func PathForTesting(paths ...string) Path {
Colin Cross1ccfcc32018-02-22 13:54:26 -08002206 p, err := validateSafePath(paths...)
2207 if err != nil {
2208 panic(err)
2209 }
Colin Cross5b529592017-05-09 13:34:34 -07002210 return testPath{basePath{path: p, rel: p}}
2211}
2212
Sam Delmerico2351eac2022-05-24 17:10:02 +00002213func PathForTestingWithRel(path, rel string) Path {
2214 p, err := validateSafePath(path, rel)
2215 if err != nil {
2216 panic(err)
2217 }
2218 r, err := validatePath(rel)
2219 if err != nil {
2220 panic(err)
2221 }
2222 return testPath{basePath{path: p, rel: r}}
2223}
2224
Colin Cross40e33732019-02-15 11:08:35 -08002225// PathsForTesting returns a Path constructed from each element in strs. It should only be used from within tests.
2226func PathsForTesting(strs ...string) Paths {
Colin Cross5b529592017-05-09 13:34:34 -07002227 p := make(Paths, len(strs))
2228 for i, s := range strs {
2229 p[i] = PathForTesting(s)
2230 }
2231
2232 return p
2233}
Colin Cross43f08db2018-11-12 10:13:39 -08002234
Colin Cross40e33732019-02-15 11:08:35 -08002235type testPathContext struct {
2236 config Config
Colin Cross40e33732019-02-15 11:08:35 -08002237}
2238
Colin Cross40e33732019-02-15 11:08:35 -08002239func (x *testPathContext) Config() Config { return x.config }
2240func (x *testPathContext) AddNinjaFileDeps(...string) {}
2241
2242// PathContextForTesting returns a PathContext that can be used in tests, for example to create an OutputPath with
2243// PathForOutput.
Colin Cross98be1bb2019-12-13 20:41:13 -08002244func PathContextForTesting(config Config) PathContext {
Colin Cross40e33732019-02-15 11:08:35 -08002245 return &testPathContext{
2246 config: config,
Colin Cross40e33732019-02-15 11:08:35 -08002247 }
2248}
2249
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002250type testModuleInstallPathContext struct {
2251 baseModuleContext
2252
2253 inData bool
2254 inTestcases bool
2255 inSanitizerDir bool
2256 inRamdisk bool
2257 inVendorRamdisk bool
Inseob Kim08758f02021-04-08 21:13:22 +09002258 inDebugRamdisk bool
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002259 inRecovery bool
2260 inRoot bool
Colin Crossea30d852023-11-29 16:00:16 -08002261 inOdm bool
2262 inProduct bool
2263 inVendor bool
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002264 forceOS *OsType
2265 forceArch *ArchType
2266}
2267
2268func (m testModuleInstallPathContext) Config() Config {
2269 return m.baseModuleContext.config
2270}
2271
2272func (testModuleInstallPathContext) AddNinjaFileDeps(deps ...string) {}
2273
2274func (m testModuleInstallPathContext) InstallInData() bool {
2275 return m.inData
2276}
2277
2278func (m testModuleInstallPathContext) InstallInTestcases() bool {
2279 return m.inTestcases
2280}
2281
2282func (m testModuleInstallPathContext) InstallInSanitizerDir() bool {
2283 return m.inSanitizerDir
2284}
2285
2286func (m testModuleInstallPathContext) InstallInRamdisk() bool {
2287 return m.inRamdisk
2288}
2289
2290func (m testModuleInstallPathContext) InstallInVendorRamdisk() bool {
2291 return m.inVendorRamdisk
2292}
2293
Inseob Kim08758f02021-04-08 21:13:22 +09002294func (m testModuleInstallPathContext) InstallInDebugRamdisk() bool {
2295 return m.inDebugRamdisk
2296}
2297
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002298func (m testModuleInstallPathContext) InstallInRecovery() bool {
2299 return m.inRecovery
2300}
2301
2302func (m testModuleInstallPathContext) InstallInRoot() bool {
2303 return m.inRoot
2304}
2305
Colin Crossea30d852023-11-29 16:00:16 -08002306func (m testModuleInstallPathContext) InstallInOdm() bool {
2307 return m.inOdm
2308}
2309
2310func (m testModuleInstallPathContext) InstallInProduct() bool {
2311 return m.inProduct
2312}
2313
2314func (m testModuleInstallPathContext) InstallInVendor() bool {
2315 return m.inVendor
2316}
2317
Ulya Trafimovichccc8c852020-10-14 11:29:07 +01002318func (m testModuleInstallPathContext) InstallForceOS() (*OsType, *ArchType) {
2319 return m.forceOS, m.forceArch
2320}
2321
2322// Construct a minimal ModuleInstallPathContext for testing. Note that baseModuleContext is
2323// default-initialized, which leaves blueprint.baseModuleContext set to nil, so methods that are
2324// delegated to it will panic.
2325func ModuleInstallPathContextForTesting(config Config) ModuleInstallPathContext {
2326 ctx := &testModuleInstallPathContext{}
2327 ctx.config = config
2328 ctx.os = Android
2329 return ctx
2330}
2331
Colin Cross43f08db2018-11-12 10:13:39 -08002332// Rel performs the same function as filepath.Rel, but reports errors to a PathContext, and reports an error if
2333// targetPath is not inside basePath.
2334func Rel(ctx PathContext, basePath string, targetPath string) string {
2335 rel, isRel := MaybeRel(ctx, basePath, targetPath)
2336 if !isRel {
Ulya Trafimovich5ab276a2020-08-25 12:45:15 +01002337 ReportPathErrorf(ctx, "path %q is not under path %q", targetPath, basePath)
Colin Cross43f08db2018-11-12 10:13:39 -08002338 return ""
2339 }
2340 return rel
2341}
2342
2343// MaybeRel performs the same function as filepath.Rel, but reports errors to a PathContext, and returns false if
2344// targetPath is not inside basePath.
2345func MaybeRel(ctx PathContext, basePath string, targetPath string) (string, bool) {
Dan Willemsen633c5022019-04-12 11:11:38 -07002346 rel, isRel, err := maybeRelErr(basePath, targetPath)
2347 if err != nil {
2348 reportPathError(ctx, err)
2349 }
2350 return rel, isRel
2351}
2352
2353func maybeRelErr(basePath string, targetPath string) (string, bool, error) {
Colin Cross43f08db2018-11-12 10:13:39 -08002354 // filepath.Rel returns an error if one path is absolute and the other is not, handle that case first.
2355 if filepath.IsAbs(basePath) != filepath.IsAbs(targetPath) {
Dan Willemsen633c5022019-04-12 11:11:38 -07002356 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002357 }
2358 rel, err := filepath.Rel(basePath, targetPath)
2359 if err != nil {
Dan Willemsen633c5022019-04-12 11:11:38 -07002360 return "", false, err
Colin Cross43f08db2018-11-12 10:13:39 -08002361 } else if rel == ".." || strings.HasPrefix(rel, "../") || strings.HasPrefix(rel, "/") {
Dan Willemsen633c5022019-04-12 11:11:38 -07002362 return "", false, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002363 }
Dan Willemsen633c5022019-04-12 11:11:38 -07002364 return rel, true, nil
Colin Cross43f08db2018-11-12 10:13:39 -08002365}
Colin Cross988414c2020-01-11 01:11:46 +00002366
2367// Writes a file to the output directory. Attempting to write directly to the output directory
2368// will fail due to the sandbox of the soong_build process.
Chris Parsons1a12d032023-02-06 22:37:41 -05002369// Only writes the file if the file doesn't exist or if it has different contents, to prevent
2370// updating the timestamp if no changes would be made. (This is better for incremental
2371// performance.)
Colin Cross988414c2020-01-11 01:11:46 +00002372func WriteFileToOutputDir(path WritablePath, data []byte, perm os.FileMode) error {
Colin Crossd6421132021-11-09 12:32:34 -08002373 absPath := absolutePath(path.String())
2374 err := os.MkdirAll(filepath.Dir(absPath), 0777)
2375 if err != nil {
2376 return err
2377 }
Chris Parsons1a12d032023-02-06 22:37:41 -05002378 return pathtools.WriteFileIfChanged(absPath, data, perm)
Colin Cross988414c2020-01-11 01:11:46 +00002379}
2380
Liz Kammer2dd9ca42020-11-25 16:06:39 -08002381func RemoveAllOutputDir(path WritablePath) error {
2382 return os.RemoveAll(absolutePath(path.String()))
2383}
2384
2385func CreateOutputDirIfNonexistent(path WritablePath, perm os.FileMode) error {
2386 dir := absolutePath(path.String())
Liz Kammer09f947d2021-05-12 14:51:49 -04002387 return createDirIfNonexistent(dir, perm)
2388}
2389
2390func createDirIfNonexistent(dir string, perm os.FileMode) error {
Liz Kammer2dd9ca42020-11-25 16:06:39 -08002391 if _, err := os.Stat(dir); os.IsNotExist(err) {
2392 return os.MkdirAll(dir, os.ModePerm)
2393 } else {
2394 return err
2395 }
2396}
2397
Jingwen Chen78257e52021-05-21 02:34:24 +00002398// absolutePath is deliberately private so that Soong's Go plugins can't use it to find and
2399// read arbitrary files without going through the methods in the current package that track
2400// dependencies.
Colin Cross988414c2020-01-11 01:11:46 +00002401func absolutePath(path string) string {
2402 if filepath.IsAbs(path) {
2403 return path
2404 }
2405 return filepath.Join(absSrcDir, path)
2406}
Chris Parsons216e10a2020-07-09 17:12:52 -04002407
2408// A DataPath represents the path of a file to be used as data, for example
2409// a test library to be installed alongside a test.
2410// The data file should be installed (copied from `<SrcPath>`) to
2411// `<install_root>/<RelativeInstallPath>/<filename>`, or
2412// `<install_root>/<filename>` if RelativeInstallPath is empty.
2413type DataPath struct {
2414 // The path of the data file that should be copied into the data directory
2415 SrcPath Path
2416 // The install path of the data file, relative to the install root.
2417 RelativeInstallPath string
Colin Cross5c1d5fb2023-11-15 12:39:40 -08002418 // If WithoutRel is true, use SrcPath.Base() instead of SrcPath.Rel() as the filename.
2419 WithoutRel bool
Chris Parsons216e10a2020-07-09 17:12:52 -04002420}
Colin Crossdcf71b22021-02-01 13:59:03 -08002421
Colin Crossd442a0e2023-11-16 11:19:26 -08002422func (d *DataPath) ToRelativeInstallPath() string {
2423 relPath := d.SrcPath.Rel()
Colin Cross5c1d5fb2023-11-15 12:39:40 -08002424 if d.WithoutRel {
2425 relPath = d.SrcPath.Base()
2426 }
Colin Crossd442a0e2023-11-16 11:19:26 -08002427 if d.RelativeInstallPath != "" {
2428 relPath = filepath.Join(d.RelativeInstallPath, relPath)
2429 }
2430 return relPath
2431}
2432
Colin Crossdcf71b22021-02-01 13:59:03 -08002433// PathsIfNonNil returns a Paths containing only the non-nil input arguments.
2434func PathsIfNonNil(paths ...Path) Paths {
2435 if len(paths) == 0 {
2436 // Fast path for empty argument list
2437 return nil
2438 } else if len(paths) == 1 {
2439 // Fast path for a single argument
2440 if paths[0] != nil {
2441 return paths
2442 } else {
2443 return nil
2444 }
2445 }
2446 ret := make(Paths, 0, len(paths))
2447 for _, path := range paths {
2448 if path != nil {
2449 ret = append(ret, path)
2450 }
2451 }
2452 if len(ret) == 0 {
2453 return nil
2454 }
2455 return ret
2456}
Chris Wailesb2703ad2021-07-30 13:25:42 -07002457
2458var thirdPartyDirPrefixExceptions = []*regexp.Regexp{
2459 regexp.MustCompile("^vendor/[^/]*google[^/]*/"),
2460 regexp.MustCompile("^hardware/google/"),
2461 regexp.MustCompile("^hardware/interfaces/"),
2462 regexp.MustCompile("^hardware/libhardware[^/]*/"),
2463 regexp.MustCompile("^hardware/ril/"),
2464}
2465
2466func IsThirdPartyPath(path string) bool {
2467 thirdPartyDirPrefixes := []string{"external/", "vendor/", "hardware/"}
2468
2469 if HasAnyPrefix(path, thirdPartyDirPrefixes) {
2470 for _, prefix := range thirdPartyDirPrefixExceptions {
2471 if prefix.MatchString(path) {
2472 return false
2473 }
2474 }
2475 return true
2476 }
2477 return false
2478}