blob: 41cb0ccb240f3c86c1ee7254828539507a5adf42 [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
Spandan Das27ff7672024-11-06 19:23:57 +0000199 InstallInSystemDlkm() bool
200 InstallInVendorDlkm() bool
201 InstallInOdmDlkm() bool
Colin Cross69452e12023-11-15 11:20:53 -0800202 InstallForceOS() (*OsType, *ArchType)
203
Cole Fauste8a87832024-09-11 11:35:46 -0700204 RequiredModuleNames(ctx ConfigurableEvaluatorContext) []string
Colin Cross69452e12023-11-15 11:20:53 -0800205 HostRequiredModuleNames() []string
206 TargetRequiredModuleNames() []string
207
208 ModuleSubDir() string
Colin Cross69452e12023-11-15 11:20:53 -0800209
210 Variable(pctx PackageContext, name, value string)
211 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
212 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
213 // and performs more verification.
214 Build(pctx PackageContext, params BuildParams)
215 // Phony creates a Make-style phony rule, a rule with no commands that can depend on other
216 // phony rules or real files. Phony can be called on the same name multiple times to add
217 // additional dependencies.
218 Phony(phony string, deps ...Path)
219
220 // GetMissingDependencies returns the list of dependencies that were passed to AddDependencies or related methods,
221 // but do not exist.
222 GetMissingDependencies() []string
223
224 // LicenseMetadataFile returns the path where the license metadata for this module will be
225 // generated.
226 LicenseMetadataFile() Path
Colin Crossd6fd0132023-11-06 13:54:06 -0800227
228 // ModuleInfoJSON returns a pointer to the ModuleInfoJSON struct that can be filled out by
229 // GenerateAndroidBuildActions. If it is called then the struct will be written out and included in
230 // the module-info.json generated by Make, and Make will not generate its own data for this module.
231 ModuleInfoJSON() *ModuleInfoJSON
mrziwange6c85812024-05-22 14:36:09 -0700232
233 // SetOutputFiles stores the outputFiles to outputFiles property, which is used
234 // to set the OutputFilesProvider later.
235 SetOutputFiles(outputFiles Paths, tag string)
Wei Lia1aa2972024-06-21 13:08:51 -0700236
Yu Liu876b7ce2024-08-21 18:20:13 +0000237 GetOutputFiles() OutputFilesInfo
238
Yu Liubad1eef2024-08-21 22:37:35 +0000239 // SetLicenseInstallMap stores the set of dependency module:location mappings for files in an
240 // apex container for use when generation the license metadata file.
241 SetLicenseInstallMap(installMap []string)
242
Wei Lia1aa2972024-06-21 13:08:51 -0700243 // ComplianceMetadataInfo returns a ComplianceMetadataInfo instance for different module types to dump metadata,
244 // which usually happens in GenerateAndroidBuildActions() of a module type.
245 // See android.ModuleBase.complianceMetadataInfo
246 ComplianceMetadataInfo() *ComplianceMetadataInfo
Yu Liu9a993132024-08-27 23:21:06 +0000247
248 // Get the information about the containers this module belongs to.
249 getContainersInfo() ContainersInfo
250 setContainersInfo(info ContainersInfo)
251
252 setAconfigPaths(paths Paths)
Colin Cross69452e12023-11-15 11:20:53 -0800253}
254
255type moduleContext struct {
256 bp blueprint.ModuleContext
257 baseModuleContext
Colin Crossa6182ab2024-08-21 10:47:44 -0700258 packagingSpecs []PackagingSpec
259 installFiles InstallPaths
260 checkbuildFiles Paths
261 checkbuildTarget Path
262 uncheckedModule bool
263 module Module
264 phonies map[string]Paths
Yu Liu876b7ce2024-08-21 18:20:13 +0000265 // outputFiles stores the output of a module by tag and is used to set
266 // the OutputFilesProvider in GenerateBuildActions
267 outputFiles OutputFilesInfo
Colin Cross69452e12023-11-15 11:20:53 -0800268
Colin Crossa14fb6a2024-10-23 16:57:06 -0700269 TransitiveInstallFiles depset.DepSet[InstallPath]
Yu Liubad1eef2024-08-21 22:37:35 +0000270
271 // set of dependency module:location mappings used to populate the license metadata for
272 // apex containers.
273 licenseInstallMap []string
274
Yu Liuec810542024-08-26 18:09:15 +0000275 // The path to the generated license metadata file for the module.
276 licenseMetadataFile WritablePath
277
Yu Liud46e5ae2024-08-15 18:46:17 +0000278 katiInstalls katiInstalls
279 katiSymlinks katiInstalls
Yu Liu82a6d142024-08-27 19:02:29 +0000280 // katiInitRcInstalls and katiVintfInstalls track the install rules created by Soong that are
281 // allowed to have duplicates across modules and variants.
282 katiInitRcInstalls katiInstalls
283 katiVintfInstalls katiInstalls
284 initRcPaths Paths
285 vintfFragmentsPaths Paths
286 installedInitRcPaths InstallPaths
287 installedVintfFragmentsPaths InstallPaths
Colin Cross69452e12023-11-15 11:20:53 -0800288
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800289 testData []DataPath
290
Colin Cross69452e12023-11-15 11:20:53 -0800291 // For tests
292 buildParams []BuildParams
293 ruleParams map[blueprint.Rule]blueprint.RuleParams
294 variables map[string]string
Yu Liu4297ad92024-08-27 19:50:13 +0000295
296 // moduleInfoJSON can be filled out by GenerateAndroidBuildActions to write a JSON file that will
297 // be included in the final module-info.json produced by Make.
298 moduleInfoJSON *ModuleInfoJSON
Yu Liu9a993132024-08-27 23:21:06 +0000299
300 // containersInfo stores the information about the containers and the information of the
301 // apexes the module belongs to.
302 containersInfo ContainersInfo
303
304 // Merged Aconfig files for all transitive deps.
305 aconfigFilePaths Paths
306
307 // complianceMetadataInfo is for different module types to dump metadata.
308 // See android.ModuleContext interface.
309 complianceMetadataInfo *ComplianceMetadataInfo
Colin Cross69452e12023-11-15 11:20:53 -0800310}
311
Cole Faust02987bd2024-03-21 17:58:43 -0700312var _ ModuleContext = &moduleContext{}
313
Colin Cross69452e12023-11-15 11:20:53 -0800314func (m *moduleContext) ninjaError(params BuildParams, err error) (PackageContext, BuildParams) {
315 return pctx, BuildParams{
316 Rule: ErrorRule,
317 Description: params.Description,
318 Output: params.Output,
319 Outputs: params.Outputs,
320 ImplicitOutput: params.ImplicitOutput,
321 ImplicitOutputs: params.ImplicitOutputs,
322 Args: map[string]string{
323 "error": err.Error(),
324 },
325 }
326}
327
328func (m *moduleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
329 m.Build(pctx, BuildParams(params))
330}
331
Colin Cross69452e12023-11-15 11:20:53 -0800332// Convert build parameters from their concrete Android types into their string representations,
333// and combine the singular and plural fields of the same type (e.g. Output and Outputs).
334func convertBuildParams(params BuildParams) blueprint.BuildParams {
335 bparams := blueprint.BuildParams{
336 Rule: params.Rule,
337 Description: params.Description,
338 Deps: params.Deps,
339 Outputs: params.Outputs.Strings(),
340 ImplicitOutputs: params.ImplicitOutputs.Strings(),
Colin Cross69452e12023-11-15 11:20:53 -0800341 Inputs: params.Inputs.Strings(),
342 Implicits: params.Implicits.Strings(),
343 OrderOnly: params.OrderOnly.Strings(),
344 Validations: params.Validations.Strings(),
345 Args: params.Args,
346 Optional: !params.Default,
347 }
348
349 if params.Depfile != nil {
350 bparams.Depfile = params.Depfile.String()
351 }
352 if params.Output != nil {
353 bparams.Outputs = append(bparams.Outputs, params.Output.String())
354 }
Colin Cross69452e12023-11-15 11:20:53 -0800355 if params.ImplicitOutput != nil {
356 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
357 }
358 if params.Input != nil {
359 bparams.Inputs = append(bparams.Inputs, params.Input.String())
360 }
361 if params.Implicit != nil {
362 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
363 }
364 if params.Validation != nil {
365 bparams.Validations = append(bparams.Validations, params.Validation.String())
366 }
367
368 bparams.Outputs = proptools.NinjaEscapeList(bparams.Outputs)
369 bparams.ImplicitOutputs = proptools.NinjaEscapeList(bparams.ImplicitOutputs)
Colin Cross69452e12023-11-15 11:20:53 -0800370 bparams.Inputs = proptools.NinjaEscapeList(bparams.Inputs)
371 bparams.Implicits = proptools.NinjaEscapeList(bparams.Implicits)
372 bparams.OrderOnly = proptools.NinjaEscapeList(bparams.OrderOnly)
373 bparams.Validations = proptools.NinjaEscapeList(bparams.Validations)
374 bparams.Depfile = proptools.NinjaEscape(bparams.Depfile)
375
376 return bparams
377}
378
379func (m *moduleContext) Variable(pctx PackageContext, name, value string) {
380 if m.config.captureBuild {
381 m.variables[name] = value
382 }
383
384 m.bp.Variable(pctx.PackageContext, name, value)
385}
386
387func (m *moduleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
388 argNames ...string) blueprint.Rule {
389
390 if m.config.UseRemoteBuild() {
391 if params.Pool == nil {
392 // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict
393 // jobs to the local parallelism value
394 params.Pool = localPool
395 } else if params.Pool == remotePool {
396 // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's
397 // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS
398 // parallelism.
399 params.Pool = nil
400 }
401 }
402
403 rule := m.bp.Rule(pctx.PackageContext, name, params, argNames...)
404
405 if m.config.captureBuild {
406 m.ruleParams[rule] = params
407 }
408
409 return rule
410}
411
412func (m *moduleContext) Build(pctx PackageContext, params BuildParams) {
413 if params.Description != "" {
414 params.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
415 }
416
417 if missingDeps := m.GetMissingDependencies(); len(missingDeps) > 0 {
418 pctx, params = m.ninjaError(params, fmt.Errorf("module %s missing dependencies: %s\n",
419 m.ModuleName(), strings.Join(missingDeps, ", ")))
420 }
421
422 if m.config.captureBuild {
423 m.buildParams = append(m.buildParams, params)
424 }
425
426 bparams := convertBuildParams(params)
Colin Cross69452e12023-11-15 11:20:53 -0800427 m.bp.Build(pctx.PackageContext, bparams)
428}
429
430func (m *moduleContext) Phony(name string, deps ...Path) {
Yu Liu54513622024-08-19 20:00:32 +0000431 m.phonies[name] = append(m.phonies[name], deps...)
Colin Cross69452e12023-11-15 11:20:53 -0800432}
433
434func (m *moduleContext) GetMissingDependencies() []string {
435 var missingDeps []string
436 missingDeps = append(missingDeps, m.Module().base().commonProperties.MissingDeps...)
437 missingDeps = append(missingDeps, m.bp.GetMissingDependencies()...)
438 missingDeps = FirstUniqueStrings(missingDeps)
439 return missingDeps
440}
441
442func (m *moduleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
443 module, _ := m.getDirectDepInternal(name, tag)
444 return module
445}
446
447func (m *moduleContext) ModuleSubDir() string {
448 return m.bp.ModuleSubDir()
449}
450
Colin Cross69452e12023-11-15 11:20:53 -0800451func (m *moduleContext) InstallInData() bool {
452 return m.module.InstallInData()
453}
454
455func (m *moduleContext) InstallInTestcases() bool {
456 return m.module.InstallInTestcases()
457}
458
459func (m *moduleContext) InstallInSanitizerDir() bool {
460 return m.module.InstallInSanitizerDir()
461}
462
463func (m *moduleContext) InstallInRamdisk() bool {
464 return m.module.InstallInRamdisk()
465}
466
467func (m *moduleContext) InstallInVendorRamdisk() bool {
468 return m.module.InstallInVendorRamdisk()
469}
470
471func (m *moduleContext) InstallInDebugRamdisk() bool {
472 return m.module.InstallInDebugRamdisk()
473}
474
475func (m *moduleContext) InstallInRecovery() bool {
476 return m.module.InstallInRecovery()
477}
478
479func (m *moduleContext) InstallInRoot() bool {
480 return m.module.InstallInRoot()
481}
482
483func (m *moduleContext) InstallForceOS() (*OsType, *ArchType) {
484 return m.module.InstallForceOS()
485}
486
Colin Crossea30d852023-11-29 16:00:16 -0800487func (m *moduleContext) InstallInOdm() bool {
488 return m.module.InstallInOdm()
489}
490
491func (m *moduleContext) InstallInProduct() bool {
492 return m.module.InstallInProduct()
493}
494
Colin Cross69452e12023-11-15 11:20:53 -0800495func (m *moduleContext) InstallInVendor() bool {
496 return m.module.InstallInVendor()
497}
498
Spandan Das27ff7672024-11-06 19:23:57 +0000499func (m *moduleContext) InstallInSystemDlkm() bool {
500 return m.module.InstallInSystemDlkm()
501}
502
503func (m *moduleContext) InstallInVendorDlkm() bool {
504 return m.module.InstallInVendorDlkm()
505}
506
507func (m *moduleContext) InstallInOdmDlkm() bool {
508 return m.module.InstallInOdmDlkm()
509}
510
Colin Cross69452e12023-11-15 11:20:53 -0800511func (m *moduleContext) skipInstall() bool {
512 if m.module.base().commonProperties.SkipInstall {
513 return true
514 }
515
Colin Cross69452e12023-11-15 11:20:53 -0800516 // We'll need a solution for choosing which of modules with the same name in different
517 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
518 // list of namespaces to install in a Soong-only build.
519 if !m.module.base().commonProperties.NamespaceExportedToMake {
520 return true
521 }
522
523 return false
524}
525
Jiyong Park3f627e62024-05-01 16:14:38 +0900526// Tells whether this module is installed to the full install path (ex:
527// out/target/product/<name>/<partition>) or not. If this returns false, the install build rule is
528// not created and this module can only be installed to packaging modules like android_filesystem.
529func (m *moduleContext) requiresFullInstall() bool {
530 if m.skipInstall() {
531 return false
532 }
533
Spandan Das034af2c2024-10-30 21:45:09 +0000534 if m.module.base().commonProperties.HideFromMake {
535 return false
536 }
537
Jiyong Park3f627e62024-05-01 16:14:38 +0900538 if proptools.Bool(m.module.base().commonProperties.No_full_install) {
539 return false
540 }
541
542 return true
543}
544
Colin Cross69452e12023-11-15 11:20:53 -0800545func (m *moduleContext) InstallFile(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800546 deps ...InstallPath) InstallPath {
Colin Crossa6182ab2024-08-21 10:47:44 -0700547 return m.installFile(installPath, name, srcPath, deps, false, true, true, nil)
548}
549
550func (m *moduleContext) InstallFileWithoutCheckbuild(installPath InstallPath, name string, srcPath Path,
551 deps ...InstallPath) InstallPath {
552 return m.installFile(installPath, name, srcPath, deps, false, true, false, nil)
Colin Cross69452e12023-11-15 11:20:53 -0800553}
554
555func (m *moduleContext) InstallExecutable(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800556 deps ...InstallPath) InstallPath {
Colin Crossa6182ab2024-08-21 10:47:44 -0700557 return m.installFile(installPath, name, srcPath, deps, true, true, true, nil)
Colin Cross69452e12023-11-15 11:20:53 -0800558}
559
560func (m *moduleContext) InstallFileWithExtraFilesZip(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800561 extraZip Path, deps ...InstallPath) InstallPath {
Colin Crossa6182ab2024-08-21 10:47:44 -0700562 return m.installFile(installPath, name, srcPath, deps, false, true, true, &extraFilesZip{
Colin Cross69452e12023-11-15 11:20:53 -0800563 zip: extraZip,
564 dir: installPath,
565 })
566}
567
568func (m *moduleContext) PackageFile(installPath InstallPath, name string, srcPath Path) PackagingSpec {
569 fullInstallPath := installPath.Join(m, name)
570 return m.packageFile(fullInstallPath, srcPath, false)
571}
572
Colin Cross7ceb14a2024-10-30 11:40:21 -0700573func (m *moduleContext) getAconfigPaths() *Paths {
574 return &m.aconfigFilePaths
Yu Liu9a993132024-08-27 23:21:06 +0000575}
576
577func (m *moduleContext) setAconfigPaths(paths Paths) {
578 m.aconfigFilePaths = paths
Justin Yun74f3f302024-05-07 14:32:14 +0900579}
580
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000581func (m *moduleContext) getOwnerAndOverrides() (string, []string) {
582 owner := m.ModuleName()
583 overrides := slices.Clone(m.Module().base().commonProperties.Overrides)
584 if b, ok := m.Module().(OverridableModule); ok {
585 if b.GetOverriddenBy() != "" {
586 // overriding variant of base module
587 overrides = append(overrides, m.ModuleName()) // com.android.foo
588 owner = m.Module().Name() // com.company.android.foo
589 }
590 }
591 return owner, overrides
592}
593
Colin Cross69452e12023-11-15 11:20:53 -0800594func (m *moduleContext) packageFile(fullInstallPath InstallPath, srcPath Path, executable bool) PackagingSpec {
595 licenseFiles := m.Module().EffectiveLicenseFiles()
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000596 owner, overrides := m.getOwnerAndOverrides()
Colin Cross69452e12023-11-15 11:20:53 -0800597 spec := PackagingSpec{
598 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
599 srcPath: srcPath,
600 symlinkTarget: "",
601 executable: executable,
Colin Cross7ceb14a2024-10-30 11:40:21 -0700602 effectiveLicenseFiles: &licenseFiles,
Colin Cross69452e12023-11-15 11:20:53 -0800603 partition: fullInstallPath.partition,
Jiyong Park4152b192024-04-30 21:24:21 +0900604 skipInstall: m.skipInstall(),
Colin Cross7ceb14a2024-10-30 11:40:21 -0700605 aconfigPaths: m.getAconfigPaths(),
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900606 archType: m.target.Arch.ArchType,
Colin Cross7ceb14a2024-10-30 11:40:21 -0700607 overrides: &overrides,
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000608 owner: owner,
Colin Cross69452e12023-11-15 11:20:53 -0800609 }
610 m.packagingSpecs = append(m.packagingSpecs, spec)
611 return spec
612}
613
Colin Cross09ad3a62023-11-15 12:29:33 -0800614func (m *moduleContext) installFile(installPath InstallPath, name string, srcPath Path, deps []InstallPath,
Colin Crossa6182ab2024-08-21 10:47:44 -0700615 executable bool, hooks bool, checkbuild bool, extraZip *extraFilesZip) InstallPath {
Colin Cross69452e12023-11-15 11:20:53 -0800616
617 fullInstallPath := installPath.Join(m, name)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800618 if hooks {
619 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, false)
620 }
Colin Cross69452e12023-11-15 11:20:53 -0800621
Jiyong Park3f627e62024-05-01 16:14:38 +0900622 if m.requiresFullInstall() {
Yu Liubad1eef2024-08-21 22:37:35 +0000623 deps = append(deps, InstallPaths(m.TransitiveInstallFiles.ToList())...)
Yu Liu82a6d142024-08-27 19:02:29 +0000624 deps = append(deps, m.installedInitRcPaths...)
625 deps = append(deps, m.installedVintfFragmentsPaths...)
Colin Cross69452e12023-11-15 11:20:53 -0800626
627 var implicitDeps, orderOnlyDeps Paths
628
629 if m.Host() {
630 // Installed host modules might be used during the build, depend directly on their
631 // dependencies so their timestamp is updated whenever their dependency is updated
Colin Cross09ad3a62023-11-15 12:29:33 -0800632 implicitDeps = InstallPaths(deps).Paths()
Colin Cross69452e12023-11-15 11:20:53 -0800633 } else {
Colin Cross09ad3a62023-11-15 12:29:33 -0800634 orderOnlyDeps = InstallPaths(deps).Paths()
Colin Cross69452e12023-11-15 11:20:53 -0800635 }
636
637 if m.Config().KatiEnabled() {
638 // When creating the install rule in Soong but embedding in Make, write the rule to a
639 // makefile instead of directly to the ninja file so that main.mk can add the
640 // dependencies from the `required` property that are hard to resolve in Soong.
641 m.katiInstalls = append(m.katiInstalls, katiInstall{
642 from: srcPath,
643 to: fullInstallPath,
644 implicitDeps: implicitDeps,
645 orderOnlyDeps: orderOnlyDeps,
646 executable: executable,
647 extraFiles: extraZip,
648 })
649 } else {
650 rule := Cp
651 if executable {
652 rule = CpExecutable
653 }
654
655 extraCmds := ""
656 if extraZip != nil {
657 extraCmds += fmt.Sprintf(" && ( unzip -qDD -d '%s' '%s' 2>&1 | grep -v \"zipfile is empty\"; exit $${PIPESTATUS[0]} )",
658 extraZip.dir.String(), extraZip.zip.String())
659 extraCmds += " || ( code=$$?; if [ $$code -ne 0 -a $$code -ne 1 ]; then exit $$code; fi )"
660 implicitDeps = append(implicitDeps, extraZip.zip)
661 }
662
663 m.Build(pctx, BuildParams{
664 Rule: rule,
665 Description: "install " + fullInstallPath.Base(),
666 Output: fullInstallPath,
667 Input: srcPath,
668 Implicits: implicitDeps,
669 OrderOnly: orderOnlyDeps,
670 Default: !m.Config().KatiEnabled(),
671 Args: map[string]string{
672 "extraCmds": extraCmds,
673 },
674 })
675 }
676
677 m.installFiles = append(m.installFiles, fullInstallPath)
678 }
679
680 m.packageFile(fullInstallPath, srcPath, executable)
681
Colin Crossa6182ab2024-08-21 10:47:44 -0700682 if checkbuild {
683 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
684 }
Colin Cross69452e12023-11-15 11:20:53 -0800685
686 return fullInstallPath
687}
688
689func (m *moduleContext) InstallSymlink(installPath InstallPath, name string, srcPath InstallPath) InstallPath {
690 fullInstallPath := installPath.Join(m, name)
691 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, true)
692
693 relPath, err := filepath.Rel(path.Dir(fullInstallPath.String()), srcPath.String())
694 if err != nil {
695 panic(fmt.Sprintf("Unable to generate symlink between %q and %q: %s", fullInstallPath.Base(), srcPath.Base(), err))
696 }
Jiyong Park3f627e62024-05-01 16:14:38 +0900697 if m.requiresFullInstall() {
Colin Cross69452e12023-11-15 11:20:53 -0800698
699 if m.Config().KatiEnabled() {
700 // When creating the symlink rule in Soong but embedding in Make, write the rule to a
701 // makefile instead of directly to the ninja file so that main.mk can add the
702 // dependencies from the `required` property that are hard to resolve in Soong.
703 m.katiSymlinks = append(m.katiSymlinks, katiInstall{
704 from: srcPath,
705 to: fullInstallPath,
706 })
707 } else {
708 // The symlink doesn't need updating when the target is modified, but we sometimes
709 // have a dependency on a symlink to a binary instead of to the binary directly, and
710 // the mtime of the symlink must be updated when the binary is modified, so use a
711 // normal dependency here instead of an order-only dependency.
712 m.Build(pctx, BuildParams{
713 Rule: Symlink,
714 Description: "install symlink " + fullInstallPath.Base(),
715 Output: fullInstallPath,
716 Input: srcPath,
717 Default: !m.Config().KatiEnabled(),
718 Args: map[string]string{
719 "fromPath": relPath,
720 },
721 })
722 }
723
724 m.installFiles = append(m.installFiles, fullInstallPath)
Colin Cross69452e12023-11-15 11:20:53 -0800725 }
726
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000727 owner, overrides := m.getOwnerAndOverrides()
Colin Cross69452e12023-11-15 11:20:53 -0800728 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
729 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
730 srcPath: nil,
731 symlinkTarget: relPath,
732 executable: false,
733 partition: fullInstallPath.partition,
Jiyong Park4152b192024-04-30 21:24:21 +0900734 skipInstall: m.skipInstall(),
Colin Cross7ceb14a2024-10-30 11:40:21 -0700735 aconfigPaths: m.getAconfigPaths(),
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900736 archType: m.target.Arch.ArchType,
Colin Cross7ceb14a2024-10-30 11:40:21 -0700737 overrides: &overrides,
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000738 owner: owner,
Colin Cross69452e12023-11-15 11:20:53 -0800739 })
740
741 return fullInstallPath
742}
743
744// installPath/name -> absPath where absPath might be a path that is available only at runtime
745// (e.g. /apex/...)
746func (m *moduleContext) InstallAbsoluteSymlink(installPath InstallPath, name string, absPath string) InstallPath {
747 fullInstallPath := installPath.Join(m, name)
748 m.module.base().hooks.runInstallHooks(m, nil, fullInstallPath, true)
749
Jiyong Park3f627e62024-05-01 16:14:38 +0900750 if m.requiresFullInstall() {
Colin Cross69452e12023-11-15 11:20:53 -0800751 if m.Config().KatiEnabled() {
752 // When creating the symlink rule in Soong but embedding in Make, write the rule to a
753 // makefile instead of directly to the ninja file so that main.mk can add the
754 // dependencies from the `required` property that are hard to resolve in Soong.
755 m.katiSymlinks = append(m.katiSymlinks, katiInstall{
756 absFrom: absPath,
757 to: fullInstallPath,
758 })
759 } else {
760 m.Build(pctx, BuildParams{
761 Rule: Symlink,
762 Description: "install symlink " + fullInstallPath.Base() + " -> " + absPath,
763 Output: fullInstallPath,
764 Default: !m.Config().KatiEnabled(),
765 Args: map[string]string{
766 "fromPath": absPath,
767 },
768 })
769 }
770
771 m.installFiles = append(m.installFiles, fullInstallPath)
772 }
773
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000774 owner, overrides := m.getOwnerAndOverrides()
Colin Cross69452e12023-11-15 11:20:53 -0800775 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
776 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
777 srcPath: nil,
778 symlinkTarget: absPath,
779 executable: false,
780 partition: fullInstallPath.partition,
Jiyong Park4152b192024-04-30 21:24:21 +0900781 skipInstall: m.skipInstall(),
Colin Cross7ceb14a2024-10-30 11:40:21 -0700782 aconfigPaths: m.getAconfigPaths(),
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900783 archType: m.target.Arch.ArchType,
Colin Cross7ceb14a2024-10-30 11:40:21 -0700784 overrides: &overrides,
Spandan Dasc1ded7e2024-11-01 00:52:33 +0000785 owner: owner,
Colin Cross69452e12023-11-15 11:20:53 -0800786 })
787
788 return fullInstallPath
789}
790
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800791func (m *moduleContext) InstallTestData(installPath InstallPath, data []DataPath) InstallPaths {
792 m.testData = append(m.testData, data...)
793
794 ret := make(InstallPaths, 0, len(data))
795 for _, d := range data {
796 relPath := d.ToRelativeInstallPath()
Colin Crossa6182ab2024-08-21 10:47:44 -0700797 installed := m.installFile(installPath, relPath, d.SrcPath, nil, false, false, true, nil)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800798 ret = append(ret, installed)
799 }
800
801 return ret
802}
803
Colin Crossa6182ab2024-08-21 10:47:44 -0700804// CheckbuildFile specifies the output files that should be built by checkbuild.
805func (m *moduleContext) CheckbuildFile(srcPaths ...Path) {
806 m.checkbuildFiles = append(m.checkbuildFiles, srcPaths...)
807}
808
809// UncheckedModule marks the current module has having no files that should be built by checkbuild.
810func (m *moduleContext) UncheckedModule() {
811 m.uncheckedModule = true
Colin Cross69452e12023-11-15 11:20:53 -0800812}
813
Colin Cross1496fb12024-09-09 16:44:10 -0700814func (m *moduleContext) BlueprintModuleContext() blueprint.ModuleContext {
Colin Cross69452e12023-11-15 11:20:53 -0800815 return m.bp
816}
817
818func (m *moduleContext) LicenseMetadataFile() Path {
Yu Liuec810542024-08-26 18:09:15 +0000819 return m.licenseMetadataFile
Colin Cross69452e12023-11-15 11:20:53 -0800820}
821
Colin Crossd6fd0132023-11-06 13:54:06 -0800822func (m *moduleContext) ModuleInfoJSON() *ModuleInfoJSON {
Yu Liu4297ad92024-08-27 19:50:13 +0000823 if moduleInfoJSON := m.moduleInfoJSON; moduleInfoJSON != nil {
Colin Crossd6fd0132023-11-06 13:54:06 -0800824 return moduleInfoJSON
825 }
826 moduleInfoJSON := &ModuleInfoJSON{}
Yu Liu4297ad92024-08-27 19:50:13 +0000827 m.moduleInfoJSON = moduleInfoJSON
Colin Crossd6fd0132023-11-06 13:54:06 -0800828 return moduleInfoJSON
829}
830
mrziwange6c85812024-05-22 14:36:09 -0700831func (m *moduleContext) SetOutputFiles(outputFiles Paths, tag string) {
832 if tag == "" {
Yu Liu876b7ce2024-08-21 18:20:13 +0000833 if len(m.outputFiles.DefaultOutputFiles) > 0 {
mrziwange6c85812024-05-22 14:36:09 -0700834 m.ModuleErrorf("Module %s default OutputFiles cannot be overwritten", m.ModuleName())
835 }
Yu Liu876b7ce2024-08-21 18:20:13 +0000836 m.outputFiles.DefaultOutputFiles = outputFiles
mrziwange6c85812024-05-22 14:36:09 -0700837 } else {
Yu Liu876b7ce2024-08-21 18:20:13 +0000838 if m.outputFiles.TaggedOutputFiles == nil {
839 m.outputFiles.TaggedOutputFiles = make(map[string]Paths)
mrziwang57768d72024-06-06 11:31:51 -0700840 }
Yu Liu876b7ce2024-08-21 18:20:13 +0000841 if _, exists := m.outputFiles.TaggedOutputFiles[tag]; exists {
mrziwange6c85812024-05-22 14:36:09 -0700842 m.ModuleErrorf("Module %s OutputFiles at tag %s cannot be overwritten", m.ModuleName(), tag)
843 } else {
Yu Liu876b7ce2024-08-21 18:20:13 +0000844 m.outputFiles.TaggedOutputFiles[tag] = outputFiles
mrziwange6c85812024-05-22 14:36:09 -0700845 }
846 }
847}
848
Yu Liu876b7ce2024-08-21 18:20:13 +0000849func (m *moduleContext) GetOutputFiles() OutputFilesInfo {
850 return m.outputFiles
851}
852
Yu Liubad1eef2024-08-21 22:37:35 +0000853func (m *moduleContext) SetLicenseInstallMap(installMap []string) {
854 m.licenseInstallMap = append(m.licenseInstallMap, installMap...)
855}
856
Wei Lia1aa2972024-06-21 13:08:51 -0700857func (m *moduleContext) ComplianceMetadataInfo() *ComplianceMetadataInfo {
Yu Liu9a993132024-08-27 23:21:06 +0000858 if m.complianceMetadataInfo == nil {
859 m.complianceMetadataInfo = NewComplianceMetadataInfo()
Wei Lia1aa2972024-06-21 13:08:51 -0700860 }
Yu Liu9a993132024-08-27 23:21:06 +0000861 return m.complianceMetadataInfo
Wei Lia1aa2972024-06-21 13:08:51 -0700862}
863
Colin Cross69452e12023-11-15 11:20:53 -0800864// Returns a list of paths expanded from globs and modules referenced using ":module" syntax. The property must
865// be tagged with `android:"path" to support automatic source module dependency resolution.
866//
867// Deprecated: use PathsForModuleSrc or PathsForModuleSrcExcludes instead.
868func (m *moduleContext) ExpandSources(srcFiles, excludes []string) Paths {
869 return PathsForModuleSrcExcludes(m, srcFiles, excludes)
870}
871
872// Returns a single path expanded from globs and modules referenced using ":module" syntax. The property must
873// be tagged with `android:"path" to support automatic source module dependency resolution.
874//
875// Deprecated: use PathForModuleSrc instead.
876func (m *moduleContext) ExpandSource(srcFile, _ string) Path {
877 return PathForModuleSrc(m, srcFile)
878}
879
880// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
881// the srcFile is non-nil. The property must be tagged with `android:"path" to support automatic source module
882// dependency resolution.
883func (m *moduleContext) ExpandOptionalSource(srcFile *string, _ string) OptionalPath {
884 if srcFile != nil {
885 return OptionalPathForPath(PathForModuleSrc(m, *srcFile))
886 }
887 return OptionalPath{}
888}
889
Cole Fauste8a87832024-09-11 11:35:46 -0700890func (m *moduleContext) RequiredModuleNames(ctx ConfigurableEvaluatorContext) []string {
Cole Faust43ddd082024-06-17 12:32:40 -0700891 return m.module.RequiredModuleNames(ctx)
Colin Cross69452e12023-11-15 11:20:53 -0800892}
893
894func (m *moduleContext) HostRequiredModuleNames() []string {
895 return m.module.HostRequiredModuleNames()
896}
897
898func (m *moduleContext) TargetRequiredModuleNames() []string {
899 return m.module.TargetRequiredModuleNames()
900}
Yu Liu9a993132024-08-27 23:21:06 +0000901
902func (m *moduleContext) getContainersInfo() ContainersInfo {
903 return m.containersInfo
904}
905
906func (m *moduleContext) setContainersInfo(info ContainersInfo) {
907 m.containersInfo = info
908}