blob: 1f5e706ec79bb25ceb8be8ebbdf3c5ca86fd95da [file] [log] [blame]
Colin Cross69452e12023-11-15 11:20:53 -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
15package android
16
17import (
18 "fmt"
Colin Cross7ceb14a2024-10-30 11:40:21 -070019 "github.com/google/blueprint/depset"
Colin Cross69452e12023-11-15 11:20:53 -080020 "path"
21 "path/filepath"
Spandan Dasc1ded7e2024-11-01 00:52:33 +000022 "slices"
Colin Cross69452e12023-11-15 11:20:53 -080023 "strings"
Cole Faust9a346f62024-01-18 20:12:02 +000024
25 "github.com/google/blueprint"
26 "github.com/google/blueprint/proptools"
Colin Cross69452e12023-11-15 11:20:53 -080027)
28
29// BuildParameters describes the set of potential parameters to build a Ninja rule.
30// In general, these correspond to a Ninja concept.
31type BuildParams struct {
32 // A Ninja Rule that will be written to the Ninja file. This allows factoring out common code
33 // among multiple modules to reduce repetition in the Ninja file of action requirements. A rule
34 // can contain variables that should be provided in Args.
35 Rule blueprint.Rule
36 // Deps represents the depfile format. When using RuleBuilder, this defaults to GCC when depfiles
37 // are used.
38 Deps blueprint.Deps
39 // Depfile is a writeable path that allows correct incremental builds when the inputs have not
40 // been fully specified by the Ninja rule. Ninja supports a subset of the Makefile depfile syntax.
41 Depfile WritablePath
42 // A description of the build action.
43 Description string
44 // Output is an output file of the action. When using this field, references to $out in the Ninja
45 // command will refer to this file.
46 Output WritablePath
47 // Outputs is a slice of output file of the action. When using this field, references to $out in
48 // the Ninja command will refer to these files.
49 Outputs WritablePaths
Colin Cross69452e12023-11-15 11:20:53 -080050 // ImplicitOutput is an output file generated by the action. Note: references to `$out` in the
51 // Ninja command will NOT include references to this file.
52 ImplicitOutput WritablePath
53 // ImplicitOutputs is a slice of output files generated by the action. Note: references to `$out`
54 // in the Ninja command will NOT include references to these files.
55 ImplicitOutputs WritablePaths
56 // Input is an input file to the Ninja action. When using this field, references to $in in the
57 // Ninja command will refer to this file.
58 Input Path
59 // Inputs is a slice of input files to the Ninja action. When using this field, references to $in
60 // in the Ninja command will refer to these files.
61 Inputs Paths
62 // Implicit is an input file to the Ninja action. Note: references to `$in` in the Ninja command
63 // will NOT include references to this file.
64 Implicit Path
65 // Implicits is a slice of input files to the Ninja action. Note: references to `$in` in the Ninja
66 // command will NOT include references to these files.
67 Implicits Paths
68 // OrderOnly are Ninja order-only inputs to the action. When these are out of date, the output is
69 // not rebuilt until they are built, but changes in order-only dependencies alone do not cause the
70 // output to be rebuilt.
71 OrderOnly Paths
72 // Validation is an output path for a validation action. Validation outputs imply lower
73 // non-blocking priority to building non-validation outputs.
74 Validation Path
75 // Validations is a slice of output path for a validation action. Validation outputs imply lower
76 // non-blocking priority to building non-validation outputs.
77 Validations Paths
78 // Whether to skip outputting a default target statement which will be built by Ninja when no
79 // targets are specified on Ninja's command line.
80 Default bool
81 // Args is a key value mapping for replacements of variables within the Rule
82 Args map[string]string
83}
84
85type ModuleBuildParams BuildParams
86
87type ModuleContext interface {
88 BaseModuleContext
89
Colin Cross1496fb12024-09-09 16:44:10 -070090 // BlueprintModuleContext returns the blueprint.ModuleContext that the ModuleContext wraps. It may only be
91 // used by the golang module types that need to call into the bootstrap module types.
92 BlueprintModuleContext() blueprint.ModuleContext
Colin Cross69452e12023-11-15 11:20:53 -080093
94 // Deprecated: use ModuleContext.Build instead.
95 ModuleBuild(pctx PackageContext, params ModuleBuildParams)
96
97 // Returns a list of paths expanded from globs and modules referenced using ":module" syntax. The property must
98 // be tagged with `android:"path" to support automatic source module dependency resolution.
99 //
100 // Deprecated: use PathsForModuleSrc or PathsForModuleSrcExcludes instead.
101 ExpandSources(srcFiles, excludes []string) Paths
102
103 // Returns a single path expanded from globs and modules referenced using ":module" syntax. The property must
104 // be tagged with `android:"path" to support automatic source module dependency resolution.
105 //
106 // Deprecated: use PathForModuleSrc instead.
107 ExpandSource(srcFile, prop string) Path
108
109 ExpandOptionalSource(srcFile *string, prop string) OptionalPath
110
111 // InstallExecutable creates a rule to copy srcPath to name in the installPath directory,
112 // with the given additional dependencies. The file is marked executable after copying.
113 //
Yu Liubad1eef2024-08-21 22:37:35 +0000114 // The installed file can be accessed by InstallFilesInfo.InstallFiles, and the PackagingSpec
115 // for the installed file can be accessed by InstallFilesInfo.PackagingSpecs on this module
116 // or by InstallFilesInfo.TransitivePackagingSpecs on modules that depend on this module through
117 // dependency tags for which IsInstallDepNeeded returns true.
Colin Cross09ad3a62023-11-15 12:29:33 -0800118 InstallExecutable(installPath InstallPath, name string, srcPath Path, deps ...InstallPath) InstallPath
Colin Cross69452e12023-11-15 11:20:53 -0800119
120 // InstallFile creates a rule to copy srcPath to name in the installPath directory,
121 // with the given additional dependencies.
122 //
Yu Liubad1eef2024-08-21 22:37:35 +0000123 // The installed file can be accessed by InstallFilesInfo.InstallFiles, and the PackagingSpec
124 // for the installed file can be accessed by InstallFilesInfo.PackagingSpecs on this module
125 // or by InstallFilesInfo.TransitivePackagingSpecs on modules that depend on this module through
126 // dependency tags for which IsInstallDepNeeded returns true.
Colin Cross09ad3a62023-11-15 12:29:33 -0800127 InstallFile(installPath InstallPath, name string, srcPath Path, deps ...InstallPath) InstallPath
Colin Cross69452e12023-11-15 11:20:53 -0800128
Colin Crossa6182ab2024-08-21 10:47:44 -0700129 // InstallFileWithoutCheckbuild creates a rule to copy srcPath to name in the installPath directory,
130 // with the given additional dependencies, but does not add the file to the list of files to build
131 // during `m checkbuild`.
132 //
133 // The installed file will be returned by FilesToInstall(), and the PackagingSpec for the
134 // installed file will be returned by PackagingSpecs() on this module or by
135 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
136 // for which IsInstallDepNeeded returns true.
137 InstallFileWithoutCheckbuild(installPath InstallPath, name string, srcPath Path, deps ...InstallPath) InstallPath
138
Colin Cross69452e12023-11-15 11:20:53 -0800139 // InstallFileWithExtraFilesZip creates a rule to copy srcPath to name in the installPath
140 // directory, and also unzip a zip file containing extra files to install into the same
141 // directory.
142 //
Yu Liubad1eef2024-08-21 22:37:35 +0000143 // The installed file can be accessed by InstallFilesInfo.InstallFiles, and the PackagingSpec
144 // for the installed file can be accessed by InstallFilesInfo.PackagingSpecs on this module
145 // or by InstallFilesInfo.TransitivePackagingSpecs on modules that depend on this module through
146 // dependency tags for which IsInstallDepNeeded returns true.
Colin Cross09ad3a62023-11-15 12:29:33 -0800147 InstallFileWithExtraFilesZip(installPath InstallPath, name string, srcPath Path, extraZip Path, deps ...InstallPath) InstallPath
Colin Cross69452e12023-11-15 11:20:53 -0800148
149 // InstallSymlink creates a rule to create a symlink from src srcPath to name in the installPath
150 // directory.
151 //
Yu Liubad1eef2024-08-21 22:37:35 +0000152 // The installed symlink can be accessed by InstallFilesInfo.InstallFiles, and the PackagingSpec
153 // for the installed file can be accessed by InstallFilesInfo.PackagingSpecs on this module
154 // or by InstallFilesInfo.TransitivePackagingSpecs on modules that depend on this module through
155 // dependency tags for which IsInstallDepNeeded returns true.
Colin Cross69452e12023-11-15 11:20:53 -0800156 InstallSymlink(installPath InstallPath, name string, srcPath InstallPath) InstallPath
157
158 // InstallAbsoluteSymlink creates a rule to create an absolute symlink from src srcPath to name
159 // in the installPath directory.
160 //
Yu Liubad1eef2024-08-21 22:37:35 +0000161 // The installed symlink can be accessed by InstallFilesInfo.InstallFiles, and the PackagingSpec
162 // for the installed file can be accessed by InstallFilesInfo.PackagingSpecs on this module
163 // or by InstallFilesInfo.TransitivePackagingSpecs on modules that depend on this module through
164 // dependency tags for which IsInstallDepNeeded returns true.
Colin Cross69452e12023-11-15 11:20:53 -0800165 InstallAbsoluteSymlink(installPath InstallPath, name string, absPath string) InstallPath
166
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800167 // InstallTestData creates rules to install test data (e.g. data files used during a test) into
168 // the installPath directory.
169 //
Yu Liubad1eef2024-08-21 22:37:35 +0000170 // The installed files can be accessed by InstallFilesInfo.InstallFiles, and the PackagingSpec
171 // for the installed files can be accessed by InstallFilesInfo.PackagingSpecs on this module
172 // or by InstallFilesInfo.TransitivePackagingSpecs on modules that depend on this module through
173 // dependency tags for which IsInstallDepNeeded returns true.
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800174 InstallTestData(installPath InstallPath, data []DataPath) InstallPaths
175
Colin Cross69452e12023-11-15 11:20:53 -0800176 // PackageFile creates a PackagingSpec as if InstallFile was called, but without creating
177 // the rule to copy the file. This is useful to define how a module would be packaged
178 // without installing it into the global installation directories.
179 //
Yu Liubad1eef2024-08-21 22:37:35 +0000180 // The created PackagingSpec can be accessed by InstallFilesInfo.PackagingSpecs on this module
181 // or by InstallFilesInfo.TransitivePackagingSpecs on modules that depend on this module through
182 // dependency tags for which IsInstallDepNeeded returns true.
Colin Cross69452e12023-11-15 11:20:53 -0800183 PackageFile(installPath InstallPath, name string, srcPath Path) PackagingSpec
184
Colin Crossa6182ab2024-08-21 10:47:44 -0700185 CheckbuildFile(srcPaths ...Path)
186 UncheckedModule()
Colin Cross69452e12023-11-15 11:20:53 -0800187
188 InstallInData() bool
189 InstallInTestcases() bool
190 InstallInSanitizerDir() bool
191 InstallInRamdisk() bool
192 InstallInVendorRamdisk() bool
193 InstallInDebugRamdisk() bool
194 InstallInRecovery() bool
195 InstallInRoot() bool
Colin Crossea30d852023-11-29 16:00:16 -0800196 InstallInOdm() bool
197 InstallInProduct() bool
Colin Cross69452e12023-11-15 11:20:53 -0800198 InstallInVendor() bool
199 InstallForceOS() (*OsType, *ArchType)
200
Cole Fauste8a87832024-09-11 11:35:46 -0700201 RequiredModuleNames(ctx ConfigurableEvaluatorContext) []string
Colin Cross69452e12023-11-15 11:20:53 -0800202 HostRequiredModuleNames() []string
203 TargetRequiredModuleNames() []string
204
205 ModuleSubDir() string
Colin Cross69452e12023-11-15 11:20:53 -0800206
207 Variable(pctx PackageContext, name, value string)
208 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
209 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
210 // and performs more verification.
211 Build(pctx PackageContext, params BuildParams)
212 // Phony creates a Make-style phony rule, a rule with no commands that can depend on other
213 // phony rules or real files. Phony can be called on the same name multiple times to add
214 // additional dependencies.
215 Phony(phony string, deps ...Path)
216
217 // GetMissingDependencies returns the list of dependencies that were passed to AddDependencies or related methods,
218 // but do not exist.
219 GetMissingDependencies() []string
220
221 // LicenseMetadataFile returns the path where the license metadata for this module will be
222 // generated.
223 LicenseMetadataFile() Path
Colin Crossd6fd0132023-11-06 13:54:06 -0800224
225 // ModuleInfoJSON returns a pointer to the ModuleInfoJSON struct that can be filled out by
226 // GenerateAndroidBuildActions. If it is called then the struct will be written out and included in
227 // the module-info.json generated by Make, and Make will not generate its own data for this module.
228 ModuleInfoJSON() *ModuleInfoJSON
mrziwange6c85812024-05-22 14:36:09 -0700229
230 // SetOutputFiles stores the outputFiles to outputFiles property, which is used
231 // to set the OutputFilesProvider later.
232 SetOutputFiles(outputFiles Paths, tag string)
Wei Lia1aa2972024-06-21 13:08:51 -0700233
Yu Liu876b7ce2024-08-21 18:20:13 +0000234 GetOutputFiles() OutputFilesInfo
235
Yu Liubad1eef2024-08-21 22:37:35 +0000236 // SetLicenseInstallMap stores the set of dependency module:location mappings for files in an
237 // apex container for use when generation the license metadata file.
238 SetLicenseInstallMap(installMap []string)
239
Wei Lia1aa2972024-06-21 13:08:51 -0700240 // ComplianceMetadataInfo returns a ComplianceMetadataInfo instance for different module types to dump metadata,
241 // which usually happens in GenerateAndroidBuildActions() of a module type.
242 // See android.ModuleBase.complianceMetadataInfo
243 ComplianceMetadataInfo() *ComplianceMetadataInfo
Yu Liu9a993132024-08-27 23:21:06 +0000244
245 // Get the information about the containers this module belongs to.
246 getContainersInfo() ContainersInfo
247 setContainersInfo(info ContainersInfo)
248
249 setAconfigPaths(paths Paths)
Colin Cross69452e12023-11-15 11:20:53 -0800250}
251
252type moduleContext struct {
253 bp blueprint.ModuleContext
254 baseModuleContext
Colin Crossa6182ab2024-08-21 10:47:44 -0700255 packagingSpecs []PackagingSpec
256 installFiles InstallPaths
257 checkbuildFiles Paths
258 checkbuildTarget Path
259 uncheckedModule bool
260 module Module
261 phonies map[string]Paths
Yu Liu876b7ce2024-08-21 18:20:13 +0000262 // outputFiles stores the output of a module by tag and is used to set
263 // the OutputFilesProvider in GenerateBuildActions
264 outputFiles OutputFilesInfo
Colin Cross69452e12023-11-15 11:20:53 -0800265
Colin Crossa14fb6a2024-10-23 16:57:06 -0700266 TransitiveInstallFiles depset.DepSet[InstallPath]
Yu Liubad1eef2024-08-21 22:37:35 +0000267
268 // set of dependency module:location mappings used to populate the license metadata for
269 // apex containers.
270 licenseInstallMap []string
271
Yu Liuec810542024-08-26 18:09:15 +0000272 // The path to the generated license metadata file for the module.
273 licenseMetadataFile WritablePath
274
Yu Liud46e5ae2024-08-15 18:46:17 +0000275 katiInstalls katiInstalls
276 katiSymlinks katiInstalls
Yu Liu82a6d142024-08-27 19:02:29 +0000277 // katiInitRcInstalls and katiVintfInstalls track the install rules created by Soong that are
278 // allowed to have duplicates across modules and variants.
279 katiInitRcInstalls katiInstalls
280 katiVintfInstalls katiInstalls
281 initRcPaths Paths
282 vintfFragmentsPaths Paths
283 installedInitRcPaths InstallPaths
284 installedVintfFragmentsPaths InstallPaths
Colin Cross69452e12023-11-15 11:20:53 -0800285
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800286 testData []DataPath
287
Colin Cross69452e12023-11-15 11:20:53 -0800288 // For tests
289 buildParams []BuildParams
290 ruleParams map[blueprint.Rule]blueprint.RuleParams
291 variables map[string]string
Yu Liu4297ad92024-08-27 19:50:13 +0000292
293 // moduleInfoJSON can be filled out by GenerateAndroidBuildActions to write a JSON file that will
294 // be included in the final module-info.json produced by Make.
295 moduleInfoJSON *ModuleInfoJSON
Yu Liu9a993132024-08-27 23:21:06 +0000296
297 // containersInfo stores the information about the containers and the information of the
298 // apexes the module belongs to.
299 containersInfo ContainersInfo
300
301 // Merged Aconfig files for all transitive deps.
302 aconfigFilePaths Paths
303
304 // complianceMetadataInfo is for different module types to dump metadata.
305 // See android.ModuleContext interface.
306 complianceMetadataInfo *ComplianceMetadataInfo
Colin Cross69452e12023-11-15 11:20:53 -0800307}
308
Cole Faust02987bd2024-03-21 17:58:43 -0700309var _ ModuleContext = &moduleContext{}
310
Colin Cross69452e12023-11-15 11:20:53 -0800311func (m *moduleContext) ninjaError(params BuildParams, err error) (PackageContext, BuildParams) {
312 return pctx, BuildParams{
313 Rule: ErrorRule,
314 Description: params.Description,
315 Output: params.Output,
316 Outputs: params.Outputs,
317 ImplicitOutput: params.ImplicitOutput,
318 ImplicitOutputs: params.ImplicitOutputs,
319 Args: map[string]string{
320 "error": err.Error(),
321 },
322 }
323}
324
325func (m *moduleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
326 m.Build(pctx, BuildParams(params))
327}
328
Colin Cross69452e12023-11-15 11:20:53 -0800329// Convert build parameters from their concrete Android types into their string representations,
330// and combine the singular and plural fields of the same type (e.g. Output and Outputs).
331func convertBuildParams(params BuildParams) blueprint.BuildParams {
332 bparams := blueprint.BuildParams{
333 Rule: params.Rule,
334 Description: params.Description,
335 Deps: params.Deps,
336 Outputs: params.Outputs.Strings(),
337 ImplicitOutputs: params.ImplicitOutputs.Strings(),
Colin Cross69452e12023-11-15 11:20:53 -0800338 Inputs: params.Inputs.Strings(),
339 Implicits: params.Implicits.Strings(),
340 OrderOnly: params.OrderOnly.Strings(),
341 Validations: params.Validations.Strings(),
342 Args: params.Args,
343 Optional: !params.Default,
344 }
345
346 if params.Depfile != nil {
347 bparams.Depfile = params.Depfile.String()
348 }
349 if params.Output != nil {
350 bparams.Outputs = append(bparams.Outputs, params.Output.String())
351 }
Colin Cross69452e12023-11-15 11:20:53 -0800352 if params.ImplicitOutput != nil {
353 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
354 }
355 if params.Input != nil {
356 bparams.Inputs = append(bparams.Inputs, params.Input.String())
357 }
358 if params.Implicit != nil {
359 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
360 }
361 if params.Validation != nil {
362 bparams.Validations = append(bparams.Validations, params.Validation.String())
363 }
364
365 bparams.Outputs = proptools.NinjaEscapeList(bparams.Outputs)
366 bparams.ImplicitOutputs = proptools.NinjaEscapeList(bparams.ImplicitOutputs)
Colin Cross69452e12023-11-15 11:20:53 -0800367 bparams.Inputs = proptools.NinjaEscapeList(bparams.Inputs)
368 bparams.Implicits = proptools.NinjaEscapeList(bparams.Implicits)
369 bparams.OrderOnly = proptools.NinjaEscapeList(bparams.OrderOnly)
370 bparams.Validations = proptools.NinjaEscapeList(bparams.Validations)
371 bparams.Depfile = proptools.NinjaEscape(bparams.Depfile)
372
373 return bparams
374}
375
376func (m *moduleContext) Variable(pctx PackageContext, name, value string) {
377 if m.config.captureBuild {
378 m.variables[name] = value
379 }
380
381 m.bp.Variable(pctx.PackageContext, name, value)
382}
383
384func (m *moduleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
385 argNames ...string) blueprint.Rule {
386
387 if m.config.UseRemoteBuild() {
388 if params.Pool == nil {
389 // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict
390 // jobs to the local parallelism value
391 params.Pool = localPool
392 } else if params.Pool == remotePool {
393 // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's
394 // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS
395 // parallelism.
396 params.Pool = nil
397 }
398 }
399
400 rule := m.bp.Rule(pctx.PackageContext, name, params, argNames...)
401
402 if m.config.captureBuild {
403 m.ruleParams[rule] = params
404 }
405
406 return rule
407}
408
409func (m *moduleContext) Build(pctx PackageContext, params BuildParams) {
410 if params.Description != "" {
411 params.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
412 }
413
414 if missingDeps := m.GetMissingDependencies(); len(missingDeps) > 0 {
415 pctx, params = m.ninjaError(params, fmt.Errorf("module %s missing dependencies: %s\n",
416 m.ModuleName(), strings.Join(missingDeps, ", ")))
417 }
418
419 if m.config.captureBuild {
420 m.buildParams = append(m.buildParams, params)
421 }
422
423 bparams := convertBuildParams(params)
Colin Cross69452e12023-11-15 11:20:53 -0800424 m.bp.Build(pctx.PackageContext, bparams)
425}
426
427func (m *moduleContext) Phony(name string, deps ...Path) {
Yu Liu54513622024-08-19 20:00:32 +0000428 m.phonies[name] = append(m.phonies[name], deps...)
Colin Cross69452e12023-11-15 11:20:53 -0800429}
430
431func (m *moduleContext) GetMissingDependencies() []string {
432 var missingDeps []string
433 missingDeps = append(missingDeps, m.Module().base().commonProperties.MissingDeps...)
434 missingDeps = append(missingDeps, m.bp.GetMissingDependencies()...)
435 missingDeps = FirstUniqueStrings(missingDeps)
436 return missingDeps
437}
438
439func (m *moduleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
440 module, _ := m.getDirectDepInternal(name, tag)
441 return module
442}
443
444func (m *moduleContext) ModuleSubDir() string {
445 return m.bp.ModuleSubDir()
446}
447
Colin Cross69452e12023-11-15 11:20:53 -0800448func (m *moduleContext) InstallInData() bool {
449 return m.module.InstallInData()
450}
451
452func (m *moduleContext) InstallInTestcases() bool {
453 return m.module.InstallInTestcases()
454}
455
456func (m *moduleContext) InstallInSanitizerDir() bool {
457 return m.module.InstallInSanitizerDir()
458}
459
460func (m *moduleContext) InstallInRamdisk() bool {
461 return m.module.InstallInRamdisk()
462}
463
464func (m *moduleContext) InstallInVendorRamdisk() bool {
465 return m.module.InstallInVendorRamdisk()
466}
467
468func (m *moduleContext) InstallInDebugRamdisk() bool {
469 return m.module.InstallInDebugRamdisk()
470}
471
472func (m *moduleContext) InstallInRecovery() bool {
473 return m.module.InstallInRecovery()
474}
475
476func (m *moduleContext) InstallInRoot() bool {
477 return m.module.InstallInRoot()
478}
479
480func (m *moduleContext) InstallForceOS() (*OsType, *ArchType) {
481 return m.module.InstallForceOS()
482}
483
Colin Crossea30d852023-11-29 16:00:16 -0800484func (m *moduleContext) InstallInOdm() bool {
485 return m.module.InstallInOdm()
486}
487
488func (m *moduleContext) InstallInProduct() bool {
489 return m.module.InstallInProduct()
490}
491
Colin Cross69452e12023-11-15 11:20:53 -0800492func (m *moduleContext) InstallInVendor() bool {
493 return m.module.InstallInVendor()
494}
495
496func (m *moduleContext) skipInstall() bool {
497 if m.module.base().commonProperties.SkipInstall {
498 return true
499 }
500
Colin Cross69452e12023-11-15 11:20:53 -0800501 // We'll need a solution for choosing which of modules with the same name in different
502 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
503 // list of namespaces to install in a Soong-only build.
504 if !m.module.base().commonProperties.NamespaceExportedToMake {
505 return true
506 }
507
508 return false
509}
510
Jiyong Park3f627e62024-05-01 16:14:38 +0900511// Tells whether this module is installed to the full install path (ex:
512// out/target/product/<name>/<partition>) or not. If this returns false, the install build rule is
513// not created and this module can only be installed to packaging modules like android_filesystem.
514func (m *moduleContext) requiresFullInstall() bool {
515 if m.skipInstall() {
516 return false
517 }
518
Spandan Das034af2c2024-10-30 21:45:09 +0000519 if m.module.base().commonProperties.HideFromMake {
520 return false
521 }
522
Jiyong Park3f627e62024-05-01 16:14:38 +0900523 if proptools.Bool(m.module.base().commonProperties.No_full_install) {
524 return false
525 }
526
527 return true
528}
529
Colin Cross69452e12023-11-15 11:20:53 -0800530func (m *moduleContext) InstallFile(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800531 deps ...InstallPath) InstallPath {
Colin Crossa6182ab2024-08-21 10:47:44 -0700532 return m.installFile(installPath, name, srcPath, deps, false, true, true, nil)
533}
534
535func (m *moduleContext) InstallFileWithoutCheckbuild(installPath InstallPath, name string, srcPath Path,
536 deps ...InstallPath) InstallPath {
537 return m.installFile(installPath, name, srcPath, deps, false, true, false, nil)
Colin Cross69452e12023-11-15 11:20:53 -0800538}
539
540func (m *moduleContext) InstallExecutable(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800541 deps ...InstallPath) InstallPath {
Colin Crossa6182ab2024-08-21 10:47:44 -0700542 return m.installFile(installPath, name, srcPath, deps, true, true, true, nil)
Colin Cross69452e12023-11-15 11:20:53 -0800543}
544
545func (m *moduleContext) InstallFileWithExtraFilesZip(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800546 extraZip Path, deps ...InstallPath) InstallPath {
Colin Crossa6182ab2024-08-21 10:47:44 -0700547 return m.installFile(installPath, name, srcPath, deps, false, true, true, &extraFilesZip{
Colin Cross69452e12023-11-15 11:20:53 -0800548 zip: extraZip,
549 dir: installPath,
550 })
551}
552
553func (m *moduleContext) PackageFile(installPath InstallPath, name string, srcPath Path) PackagingSpec {
554 fullInstallPath := installPath.Join(m, name)
555 return m.packageFile(fullInstallPath, srcPath, false)
556}
557
Colin Cross7ceb14a2024-10-30 11:40:21 -0700558func (m *moduleContext) getAconfigPaths() *Paths {
559 return &m.aconfigFilePaths
Yu Liu9a993132024-08-27 23:21:06 +0000560}
561
562func (m *moduleContext) setAconfigPaths(paths Paths) {
563 m.aconfigFilePaths = paths
Justin Yun74f3f302024-05-07 14:32:14 +0900564}
565
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000566func (m *moduleContext) getOwnerAndOverrides() (string, []string) {
567 owner := m.ModuleName()
568 overrides := slices.Clone(m.Module().base().commonProperties.Overrides)
569 if b, ok := m.Module().(OverridableModule); ok {
570 if b.GetOverriddenBy() != "" {
571 // overriding variant of base module
572 overrides = append(overrides, m.ModuleName()) // com.android.foo
573 owner = m.Module().Name() // com.company.android.foo
574 }
575 }
576 return owner, overrides
577}
578
Colin Cross69452e12023-11-15 11:20:53 -0800579func (m *moduleContext) packageFile(fullInstallPath InstallPath, srcPath Path, executable bool) PackagingSpec {
580 licenseFiles := m.Module().EffectiveLicenseFiles()
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000581 owner, overrides := m.getOwnerAndOverrides()
Colin Cross69452e12023-11-15 11:20:53 -0800582 spec := PackagingSpec{
583 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
584 srcPath: srcPath,
585 symlinkTarget: "",
586 executable: executable,
Colin Cross7ceb14a2024-10-30 11:40:21 -0700587 effectiveLicenseFiles: &licenseFiles,
Colin Cross69452e12023-11-15 11:20:53 -0800588 partition: fullInstallPath.partition,
Jiyong Park4152b192024-04-30 21:24:21 +0900589 skipInstall: m.skipInstall(),
Colin Cross7ceb14a2024-10-30 11:40:21 -0700590 aconfigPaths: m.getAconfigPaths(),
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900591 archType: m.target.Arch.ArchType,
Colin Cross7ceb14a2024-10-30 11:40:21 -0700592 overrides: &overrides,
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000593 owner: owner,
Colin Cross69452e12023-11-15 11:20:53 -0800594 }
595 m.packagingSpecs = append(m.packagingSpecs, spec)
596 return spec
597}
598
Colin Cross09ad3a62023-11-15 12:29:33 -0800599func (m *moduleContext) installFile(installPath InstallPath, name string, srcPath Path, deps []InstallPath,
Colin Crossa6182ab2024-08-21 10:47:44 -0700600 executable bool, hooks bool, checkbuild bool, extraZip *extraFilesZip) InstallPath {
Colin Cross69452e12023-11-15 11:20:53 -0800601
602 fullInstallPath := installPath.Join(m, name)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800603 if hooks {
604 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, false)
605 }
Colin Cross69452e12023-11-15 11:20:53 -0800606
Jiyong Park3f627e62024-05-01 16:14:38 +0900607 if m.requiresFullInstall() {
Yu Liubad1eef2024-08-21 22:37:35 +0000608 deps = append(deps, InstallPaths(m.TransitiveInstallFiles.ToList())...)
Yu Liu82a6d142024-08-27 19:02:29 +0000609 deps = append(deps, m.installedInitRcPaths...)
610 deps = append(deps, m.installedVintfFragmentsPaths...)
Colin Cross69452e12023-11-15 11:20:53 -0800611
612 var implicitDeps, orderOnlyDeps Paths
613
614 if m.Host() {
615 // Installed host modules might be used during the build, depend directly on their
616 // dependencies so their timestamp is updated whenever their dependency is updated
Colin Cross09ad3a62023-11-15 12:29:33 -0800617 implicitDeps = InstallPaths(deps).Paths()
Colin Cross69452e12023-11-15 11:20:53 -0800618 } else {
Colin Cross09ad3a62023-11-15 12:29:33 -0800619 orderOnlyDeps = InstallPaths(deps).Paths()
Colin Cross69452e12023-11-15 11:20:53 -0800620 }
621
622 if m.Config().KatiEnabled() {
623 // When creating the install rule in Soong but embedding in Make, write the rule to a
624 // makefile instead of directly to the ninja file so that main.mk can add the
625 // dependencies from the `required` property that are hard to resolve in Soong.
626 m.katiInstalls = append(m.katiInstalls, katiInstall{
627 from: srcPath,
628 to: fullInstallPath,
629 implicitDeps: implicitDeps,
630 orderOnlyDeps: orderOnlyDeps,
631 executable: executable,
632 extraFiles: extraZip,
633 })
634 } else {
635 rule := Cp
636 if executable {
637 rule = CpExecutable
638 }
639
640 extraCmds := ""
641 if extraZip != nil {
642 extraCmds += fmt.Sprintf(" && ( unzip -qDD -d '%s' '%s' 2>&1 | grep -v \"zipfile is empty\"; exit $${PIPESTATUS[0]} )",
643 extraZip.dir.String(), extraZip.zip.String())
644 extraCmds += " || ( code=$$?; if [ $$code -ne 0 -a $$code -ne 1 ]; then exit $$code; fi )"
645 implicitDeps = append(implicitDeps, extraZip.zip)
646 }
647
648 m.Build(pctx, BuildParams{
649 Rule: rule,
650 Description: "install " + fullInstallPath.Base(),
651 Output: fullInstallPath,
652 Input: srcPath,
653 Implicits: implicitDeps,
654 OrderOnly: orderOnlyDeps,
655 Default: !m.Config().KatiEnabled(),
656 Args: map[string]string{
657 "extraCmds": extraCmds,
658 },
659 })
660 }
661
662 m.installFiles = append(m.installFiles, fullInstallPath)
663 }
664
665 m.packageFile(fullInstallPath, srcPath, executable)
666
Colin Crossa6182ab2024-08-21 10:47:44 -0700667 if checkbuild {
668 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
669 }
Colin Cross69452e12023-11-15 11:20:53 -0800670
671 return fullInstallPath
672}
673
674func (m *moduleContext) InstallSymlink(installPath InstallPath, name string, srcPath InstallPath) InstallPath {
675 fullInstallPath := installPath.Join(m, name)
676 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, true)
677
678 relPath, err := filepath.Rel(path.Dir(fullInstallPath.String()), srcPath.String())
679 if err != nil {
680 panic(fmt.Sprintf("Unable to generate symlink between %q and %q: %s", fullInstallPath.Base(), srcPath.Base(), err))
681 }
Jiyong Park3f627e62024-05-01 16:14:38 +0900682 if m.requiresFullInstall() {
Colin Cross69452e12023-11-15 11:20:53 -0800683
684 if m.Config().KatiEnabled() {
685 // When creating the symlink rule in Soong but embedding in Make, write the rule to a
686 // makefile instead of directly to the ninja file so that main.mk can add the
687 // dependencies from the `required` property that are hard to resolve in Soong.
688 m.katiSymlinks = append(m.katiSymlinks, katiInstall{
689 from: srcPath,
690 to: fullInstallPath,
691 })
692 } else {
693 // The symlink doesn't need updating when the target is modified, but we sometimes
694 // have a dependency on a symlink to a binary instead of to the binary directly, and
695 // the mtime of the symlink must be updated when the binary is modified, so use a
696 // normal dependency here instead of an order-only dependency.
697 m.Build(pctx, BuildParams{
698 Rule: Symlink,
699 Description: "install symlink " + fullInstallPath.Base(),
700 Output: fullInstallPath,
701 Input: srcPath,
702 Default: !m.Config().KatiEnabled(),
703 Args: map[string]string{
704 "fromPath": relPath,
705 },
706 })
707 }
708
709 m.installFiles = append(m.installFiles, fullInstallPath)
Colin Cross69452e12023-11-15 11:20:53 -0800710 }
711
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000712 owner, overrides := m.getOwnerAndOverrides()
Colin Cross69452e12023-11-15 11:20:53 -0800713 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
714 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
715 srcPath: nil,
716 symlinkTarget: relPath,
717 executable: false,
718 partition: fullInstallPath.partition,
Jiyong Park4152b192024-04-30 21:24:21 +0900719 skipInstall: m.skipInstall(),
Colin Cross7ceb14a2024-10-30 11:40:21 -0700720 aconfigPaths: m.getAconfigPaths(),
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900721 archType: m.target.Arch.ArchType,
Colin Cross7ceb14a2024-10-30 11:40:21 -0700722 overrides: &overrides,
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000723 owner: owner,
Colin Cross69452e12023-11-15 11:20:53 -0800724 })
725
726 return fullInstallPath
727}
728
729// installPath/name -> absPath where absPath might be a path that is available only at runtime
730// (e.g. /apex/...)
731func (m *moduleContext) InstallAbsoluteSymlink(installPath InstallPath, name string, absPath string) InstallPath {
732 fullInstallPath := installPath.Join(m, name)
733 m.module.base().hooks.runInstallHooks(m, nil, fullInstallPath, true)
734
Jiyong Park3f627e62024-05-01 16:14:38 +0900735 if m.requiresFullInstall() {
Colin Cross69452e12023-11-15 11:20:53 -0800736 if m.Config().KatiEnabled() {
737 // When creating the symlink rule in Soong but embedding in Make, write the rule to a
738 // makefile instead of directly to the ninja file so that main.mk can add the
739 // dependencies from the `required` property that are hard to resolve in Soong.
740 m.katiSymlinks = append(m.katiSymlinks, katiInstall{
741 absFrom: absPath,
742 to: fullInstallPath,
743 })
744 } else {
745 m.Build(pctx, BuildParams{
746 Rule: Symlink,
747 Description: "install symlink " + fullInstallPath.Base() + " -> " + absPath,
748 Output: fullInstallPath,
749 Default: !m.Config().KatiEnabled(),
750 Args: map[string]string{
751 "fromPath": absPath,
752 },
753 })
754 }
755
756 m.installFiles = append(m.installFiles, fullInstallPath)
757 }
758
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000759 owner, overrides := m.getOwnerAndOverrides()
Colin Cross69452e12023-11-15 11:20:53 -0800760 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
761 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
762 srcPath: nil,
763 symlinkTarget: absPath,
764 executable: false,
765 partition: fullInstallPath.partition,
Jiyong Park4152b192024-04-30 21:24:21 +0900766 skipInstall: m.skipInstall(),
Colin Cross7ceb14a2024-10-30 11:40:21 -0700767 aconfigPaths: m.getAconfigPaths(),
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900768 archType: m.target.Arch.ArchType,
Colin Cross7ceb14a2024-10-30 11:40:21 -0700769 overrides: &overrides,
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000770 owner: owner,
Colin Cross69452e12023-11-15 11:20:53 -0800771 })
772
773 return fullInstallPath
774}
775
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800776func (m *moduleContext) InstallTestData(installPath InstallPath, data []DataPath) InstallPaths {
777 m.testData = append(m.testData, data...)
778
779 ret := make(InstallPaths, 0, len(data))
780 for _, d := range data {
781 relPath := d.ToRelativeInstallPath()
Colin Crossa6182ab2024-08-21 10:47:44 -0700782 installed := m.installFile(installPath, relPath, d.SrcPath, nil, false, false, true, nil)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800783 ret = append(ret, installed)
784 }
785
786 return ret
787}
788
Colin Crossa6182ab2024-08-21 10:47:44 -0700789// CheckbuildFile specifies the output files that should be built by checkbuild.
790func (m *moduleContext) CheckbuildFile(srcPaths ...Path) {
791 m.checkbuildFiles = append(m.checkbuildFiles, srcPaths...)
792}
793
794// UncheckedModule marks the current module has having no files that should be built by checkbuild.
795func (m *moduleContext) UncheckedModule() {
796 m.uncheckedModule = true
Colin Cross69452e12023-11-15 11:20:53 -0800797}
798
Colin Cross1496fb12024-09-09 16:44:10 -0700799func (m *moduleContext) BlueprintModuleContext() blueprint.ModuleContext {
Colin Cross69452e12023-11-15 11:20:53 -0800800 return m.bp
801}
802
803func (m *moduleContext) LicenseMetadataFile() Path {
Yu Liuec810542024-08-26 18:09:15 +0000804 return m.licenseMetadataFile
Colin Cross69452e12023-11-15 11:20:53 -0800805}
806
Colin Crossd6fd0132023-11-06 13:54:06 -0800807func (m *moduleContext) ModuleInfoJSON() *ModuleInfoJSON {
Yu Liu4297ad92024-08-27 19:50:13 +0000808 if moduleInfoJSON := m.moduleInfoJSON; moduleInfoJSON != nil {
Colin Crossd6fd0132023-11-06 13:54:06 -0800809 return moduleInfoJSON
810 }
811 moduleInfoJSON := &ModuleInfoJSON{}
Yu Liu4297ad92024-08-27 19:50:13 +0000812 m.moduleInfoJSON = moduleInfoJSON
Colin Crossd6fd0132023-11-06 13:54:06 -0800813 return moduleInfoJSON
814}
815
mrziwange6c85812024-05-22 14:36:09 -0700816func (m *moduleContext) SetOutputFiles(outputFiles Paths, tag string) {
817 if tag == "" {
Yu Liu876b7ce2024-08-21 18:20:13 +0000818 if len(m.outputFiles.DefaultOutputFiles) > 0 {
mrziwange6c85812024-05-22 14:36:09 -0700819 m.ModuleErrorf("Module %s default OutputFiles cannot be overwritten", m.ModuleName())
820 }
Yu Liu876b7ce2024-08-21 18:20:13 +0000821 m.outputFiles.DefaultOutputFiles = outputFiles
mrziwange6c85812024-05-22 14:36:09 -0700822 } else {
Yu Liu876b7ce2024-08-21 18:20:13 +0000823 if m.outputFiles.TaggedOutputFiles == nil {
824 m.outputFiles.TaggedOutputFiles = make(map[string]Paths)
mrziwang57768d72024-06-06 11:31:51 -0700825 }
Yu Liu876b7ce2024-08-21 18:20:13 +0000826 if _, exists := m.outputFiles.TaggedOutputFiles[tag]; exists {
mrziwange6c85812024-05-22 14:36:09 -0700827 m.ModuleErrorf("Module %s OutputFiles at tag %s cannot be overwritten", m.ModuleName(), tag)
828 } else {
Yu Liu876b7ce2024-08-21 18:20:13 +0000829 m.outputFiles.TaggedOutputFiles[tag] = outputFiles
mrziwange6c85812024-05-22 14:36:09 -0700830 }
831 }
832}
833
Yu Liu876b7ce2024-08-21 18:20:13 +0000834func (m *moduleContext) GetOutputFiles() OutputFilesInfo {
835 return m.outputFiles
836}
837
Yu Liubad1eef2024-08-21 22:37:35 +0000838func (m *moduleContext) SetLicenseInstallMap(installMap []string) {
839 m.licenseInstallMap = append(m.licenseInstallMap, installMap...)
840}
841
Wei Lia1aa2972024-06-21 13:08:51 -0700842func (m *moduleContext) ComplianceMetadataInfo() *ComplianceMetadataInfo {
Yu Liu9a993132024-08-27 23:21:06 +0000843 if m.complianceMetadataInfo == nil {
844 m.complianceMetadataInfo = NewComplianceMetadataInfo()
Wei Lia1aa2972024-06-21 13:08:51 -0700845 }
Yu Liu9a993132024-08-27 23:21:06 +0000846 return m.complianceMetadataInfo
Wei Lia1aa2972024-06-21 13:08:51 -0700847}
848
Colin Cross69452e12023-11-15 11:20:53 -0800849// Returns a list of paths expanded from globs and modules referenced using ":module" syntax. The property must
850// be tagged with `android:"path" to support automatic source module dependency resolution.
851//
852// Deprecated: use PathsForModuleSrc or PathsForModuleSrcExcludes instead.
853func (m *moduleContext) ExpandSources(srcFiles, excludes []string) Paths {
854 return PathsForModuleSrcExcludes(m, srcFiles, excludes)
855}
856
857// Returns a single path expanded from globs and modules referenced using ":module" syntax. The property must
858// be tagged with `android:"path" to support automatic source module dependency resolution.
859//
860// Deprecated: use PathForModuleSrc instead.
861func (m *moduleContext) ExpandSource(srcFile, _ string) Path {
862 return PathForModuleSrc(m, srcFile)
863}
864
865// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
866// the srcFile is non-nil. The property must be tagged with `android:"path" to support automatic source module
867// dependency resolution.
868func (m *moduleContext) ExpandOptionalSource(srcFile *string, _ string) OptionalPath {
869 if srcFile != nil {
870 return OptionalPathForPath(PathForModuleSrc(m, *srcFile))
871 }
872 return OptionalPath{}
873}
874
Cole Fauste8a87832024-09-11 11:35:46 -0700875func (m *moduleContext) RequiredModuleNames(ctx ConfigurableEvaluatorContext) []string {
Cole Faust43ddd082024-06-17 12:32:40 -0700876 return m.module.RequiredModuleNames(ctx)
Colin Cross69452e12023-11-15 11:20:53 -0800877}
878
879func (m *moduleContext) HostRequiredModuleNames() []string {
880 return m.module.HostRequiredModuleNames()
881}
882
883func (m *moduleContext) TargetRequiredModuleNames() []string {
884 return m.module.TargetRequiredModuleNames()
885}
Yu Liu9a993132024-08-27 23:21:06 +0000886
887func (m *moduleContext) getContainersInfo() ContainersInfo {
888 return m.containersInfo
889}
890
891func (m *moduleContext) setContainersInfo(info ContainersInfo) {
892 m.containersInfo = info
893}