blob: 2e16a24787548f0461d9d339e424e40b6d9844cc [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 Cross69452e12023-11-15 11:20:53 -080019 "path"
20 "path/filepath"
21 "strings"
Cole Faust9a346f62024-01-18 20:12:02 +000022
23 "github.com/google/blueprint"
24 "github.com/google/blueprint/proptools"
Colin Cross69452e12023-11-15 11:20:53 -080025)
26
27// BuildParameters describes the set of potential parameters to build a Ninja rule.
28// In general, these correspond to a Ninja concept.
29type BuildParams struct {
30 // A Ninja Rule that will be written to the Ninja file. This allows factoring out common code
31 // among multiple modules to reduce repetition in the Ninja file of action requirements. A rule
32 // can contain variables that should be provided in Args.
33 Rule blueprint.Rule
34 // Deps represents the depfile format. When using RuleBuilder, this defaults to GCC when depfiles
35 // are used.
36 Deps blueprint.Deps
37 // Depfile is a writeable path that allows correct incremental builds when the inputs have not
38 // been fully specified by the Ninja rule. Ninja supports a subset of the Makefile depfile syntax.
39 Depfile WritablePath
40 // A description of the build action.
41 Description string
42 // Output is an output file of the action. When using this field, references to $out in the Ninja
43 // command will refer to this file.
44 Output WritablePath
45 // Outputs is a slice of output file of the action. When using this field, references to $out in
46 // the Ninja command will refer to these files.
47 Outputs WritablePaths
Colin Cross69452e12023-11-15 11:20:53 -080048 // ImplicitOutput is an output file generated by the action. Note: references to `$out` in the
49 // Ninja command will NOT include references to this file.
50 ImplicitOutput WritablePath
51 // ImplicitOutputs is a slice of output files generated by the action. Note: references to `$out`
52 // in the Ninja command will NOT include references to these files.
53 ImplicitOutputs WritablePaths
54 // Input is an input file to the Ninja action. When using this field, references to $in in the
55 // Ninja command will refer to this file.
56 Input Path
57 // Inputs is a slice of input files to the Ninja action. When using this field, references to $in
58 // in the Ninja command will refer to these files.
59 Inputs Paths
60 // Implicit is an input file to the Ninja action. Note: references to `$in` in the Ninja command
61 // will NOT include references to this file.
62 Implicit Path
63 // Implicits is a slice of input files to the Ninja action. Note: references to `$in` in the Ninja
64 // command will NOT include references to these files.
65 Implicits Paths
66 // OrderOnly are Ninja order-only inputs to the action. When these are out of date, the output is
67 // not rebuilt until they are built, but changes in order-only dependencies alone do not cause the
68 // output to be rebuilt.
69 OrderOnly Paths
70 // Validation is an output path for a validation action. Validation outputs imply lower
71 // non-blocking priority to building non-validation outputs.
72 Validation Path
73 // Validations is a slice of output path for a validation action. Validation outputs imply lower
74 // non-blocking priority to building non-validation outputs.
75 Validations Paths
76 // Whether to skip outputting a default target statement which will be built by Ninja when no
77 // targets are specified on Ninja's command line.
78 Default bool
79 // Args is a key value mapping for replacements of variables within the Rule
80 Args map[string]string
81}
82
83type ModuleBuildParams BuildParams
84
85type ModuleContext interface {
86 BaseModuleContext
87
88 blueprintModuleContext() blueprint.ModuleContext
89
90 // Deprecated: use ModuleContext.Build instead.
91 ModuleBuild(pctx PackageContext, params ModuleBuildParams)
92
93 // Returns a list of paths expanded from globs and modules referenced using ":module" syntax. The property must
94 // be tagged with `android:"path" to support automatic source module dependency resolution.
95 //
96 // Deprecated: use PathsForModuleSrc or PathsForModuleSrcExcludes instead.
97 ExpandSources(srcFiles, excludes []string) Paths
98
99 // Returns a single path expanded from globs and modules referenced using ":module" syntax. The property must
100 // be tagged with `android:"path" to support automatic source module dependency resolution.
101 //
102 // Deprecated: use PathForModuleSrc instead.
103 ExpandSource(srcFile, prop string) Path
104
105 ExpandOptionalSource(srcFile *string, prop string) OptionalPath
106
107 // InstallExecutable creates a rule to copy srcPath to name in the installPath directory,
108 // with the given additional dependencies. The file is marked executable after copying.
109 //
110 // The installed file will be returned by FilesToInstall(), and the PackagingSpec for the
111 // installed file will be returned by PackagingSpecs() on this module or by
112 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
113 // for which IsInstallDepNeeded returns true.
Colin Cross09ad3a62023-11-15 12:29:33 -0800114 InstallExecutable(installPath InstallPath, name string, srcPath Path, deps ...InstallPath) InstallPath
Colin Cross69452e12023-11-15 11:20:53 -0800115
116 // InstallFile creates a rule to copy srcPath to name in the installPath directory,
117 // with the given additional dependencies.
118 //
119 // The installed file will be returned by FilesToInstall(), and the PackagingSpec for the
120 // installed file will be returned by PackagingSpecs() on this module or by
121 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
122 // for which IsInstallDepNeeded returns true.
Colin Cross09ad3a62023-11-15 12:29:33 -0800123 InstallFile(installPath InstallPath, name string, srcPath Path, deps ...InstallPath) InstallPath
Colin Cross69452e12023-11-15 11:20:53 -0800124
125 // InstallFileWithExtraFilesZip creates a rule to copy srcPath to name in the installPath
126 // directory, and also unzip a zip file containing extra files to install into the same
127 // directory.
128 //
129 // The installed file will be returned by FilesToInstall(), and the PackagingSpec for the
130 // installed file will be returned by PackagingSpecs() on this module or by
131 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
132 // for which IsInstallDepNeeded returns true.
Colin Cross09ad3a62023-11-15 12:29:33 -0800133 InstallFileWithExtraFilesZip(installPath InstallPath, name string, srcPath Path, extraZip Path, deps ...InstallPath) InstallPath
Colin Cross69452e12023-11-15 11:20:53 -0800134
135 // InstallSymlink creates a rule to create a symlink from src srcPath to name in the installPath
136 // directory.
137 //
138 // The installed symlink will be returned by FilesToInstall(), and the PackagingSpec for the
139 // installed file will be returned by PackagingSpecs() on this module or by
140 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
141 // for which IsInstallDepNeeded returns true.
142 InstallSymlink(installPath InstallPath, name string, srcPath InstallPath) InstallPath
143
144 // InstallAbsoluteSymlink creates a rule to create an absolute symlink from src srcPath to name
145 // in the installPath directory.
146 //
147 // The installed symlink will be returned by FilesToInstall(), and the PackagingSpec for the
148 // installed file will be returned by PackagingSpecs() on this module or by
149 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
150 // for which IsInstallDepNeeded returns true.
151 InstallAbsoluteSymlink(installPath InstallPath, name string, absPath string) InstallPath
152
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800153 // InstallTestData creates rules to install test data (e.g. data files used during a test) into
154 // the installPath directory.
155 //
156 // The installed files will be returned by FilesToInstall(), and the PackagingSpec for the
157 // installed files will be returned by PackagingSpecs() on this module or by
158 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
159 // for which IsInstallDepNeeded returns true.
160 InstallTestData(installPath InstallPath, data []DataPath) InstallPaths
161
Colin Cross69452e12023-11-15 11:20:53 -0800162 // PackageFile creates a PackagingSpec as if InstallFile was called, but without creating
163 // the rule to copy the file. This is useful to define how a module would be packaged
164 // without installing it into the global installation directories.
165 //
166 // The created PackagingSpec for the will be returned by PackagingSpecs() on this module or by
167 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
168 // for which IsInstallDepNeeded returns true.
169 PackageFile(installPath InstallPath, name string, srcPath Path) PackagingSpec
170
171 CheckbuildFile(srcPath Path)
172
173 InstallInData() bool
174 InstallInTestcases() bool
175 InstallInSanitizerDir() bool
176 InstallInRamdisk() bool
177 InstallInVendorRamdisk() bool
178 InstallInDebugRamdisk() bool
179 InstallInRecovery() bool
180 InstallInRoot() bool
Colin Crossea30d852023-11-29 16:00:16 -0800181 InstallInOdm() bool
182 InstallInProduct() bool
Colin Cross69452e12023-11-15 11:20:53 -0800183 InstallInVendor() bool
184 InstallForceOS() (*OsType, *ArchType)
185
Cole Faust43ddd082024-06-17 12:32:40 -0700186 RequiredModuleNames(ctx ConfigAndErrorContext) []string
Colin Cross69452e12023-11-15 11:20:53 -0800187 HostRequiredModuleNames() []string
188 TargetRequiredModuleNames() []string
189
190 ModuleSubDir() string
191 SoongConfigTraceHash() string
192
193 Variable(pctx PackageContext, name, value string)
194 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
195 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
196 // and performs more verification.
197 Build(pctx PackageContext, params BuildParams)
198 // Phony creates a Make-style phony rule, a rule with no commands that can depend on other
199 // phony rules or real files. Phony can be called on the same name multiple times to add
200 // additional dependencies.
201 Phony(phony string, deps ...Path)
202
203 // GetMissingDependencies returns the list of dependencies that were passed to AddDependencies or related methods,
204 // but do not exist.
205 GetMissingDependencies() []string
206
207 // LicenseMetadataFile returns the path where the license metadata for this module will be
208 // generated.
209 LicenseMetadataFile() Path
Colin Crossd6fd0132023-11-06 13:54:06 -0800210
211 // ModuleInfoJSON returns a pointer to the ModuleInfoJSON struct that can be filled out by
212 // GenerateAndroidBuildActions. If it is called then the struct will be written out and included in
213 // the module-info.json generated by Make, and Make will not generate its own data for this module.
214 ModuleInfoJSON() *ModuleInfoJSON
mrziwange6c85812024-05-22 14:36:09 -0700215
216 // SetOutputFiles stores the outputFiles to outputFiles property, which is used
217 // to set the OutputFilesProvider later.
218 SetOutputFiles(outputFiles Paths, tag string)
Wei Lia1aa2972024-06-21 13:08:51 -0700219
220 // ComplianceMetadataInfo returns a ComplianceMetadataInfo instance for different module types to dump metadata,
221 // which usually happens in GenerateAndroidBuildActions() of a module type.
222 // See android.ModuleBase.complianceMetadataInfo
223 ComplianceMetadataInfo() *ComplianceMetadataInfo
Colin Cross69452e12023-11-15 11:20:53 -0800224}
225
226type moduleContext struct {
227 bp blueprint.ModuleContext
228 baseModuleContext
229 packagingSpecs []PackagingSpec
230 installFiles InstallPaths
231 checkbuildFiles Paths
232 module Module
233 phonies map[string]Paths
234
235 katiInstalls []katiInstall
236 katiSymlinks []katiInstall
237
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800238 testData []DataPath
239
Colin Cross69452e12023-11-15 11:20:53 -0800240 // For tests
241 buildParams []BuildParams
242 ruleParams map[blueprint.Rule]blueprint.RuleParams
243 variables map[string]string
244}
245
Cole Faust02987bd2024-03-21 17:58:43 -0700246var _ ModuleContext = &moduleContext{}
247
Colin Cross69452e12023-11-15 11:20:53 -0800248func (m *moduleContext) ninjaError(params BuildParams, err error) (PackageContext, BuildParams) {
249 return pctx, BuildParams{
250 Rule: ErrorRule,
251 Description: params.Description,
252 Output: params.Output,
253 Outputs: params.Outputs,
254 ImplicitOutput: params.ImplicitOutput,
255 ImplicitOutputs: params.ImplicitOutputs,
256 Args: map[string]string{
257 "error": err.Error(),
258 },
259 }
260}
261
262func (m *moduleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
263 m.Build(pctx, BuildParams(params))
264}
265
Colin Cross69452e12023-11-15 11:20:53 -0800266// Convert build parameters from their concrete Android types into their string representations,
267// and combine the singular and plural fields of the same type (e.g. Output and Outputs).
268func convertBuildParams(params BuildParams) blueprint.BuildParams {
269 bparams := blueprint.BuildParams{
270 Rule: params.Rule,
271 Description: params.Description,
272 Deps: params.Deps,
273 Outputs: params.Outputs.Strings(),
274 ImplicitOutputs: params.ImplicitOutputs.Strings(),
Colin Cross69452e12023-11-15 11:20:53 -0800275 Inputs: params.Inputs.Strings(),
276 Implicits: params.Implicits.Strings(),
277 OrderOnly: params.OrderOnly.Strings(),
278 Validations: params.Validations.Strings(),
279 Args: params.Args,
280 Optional: !params.Default,
281 }
282
283 if params.Depfile != nil {
284 bparams.Depfile = params.Depfile.String()
285 }
286 if params.Output != nil {
287 bparams.Outputs = append(bparams.Outputs, params.Output.String())
288 }
Colin Cross69452e12023-11-15 11:20:53 -0800289 if params.ImplicitOutput != nil {
290 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
291 }
292 if params.Input != nil {
293 bparams.Inputs = append(bparams.Inputs, params.Input.String())
294 }
295 if params.Implicit != nil {
296 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
297 }
298 if params.Validation != nil {
299 bparams.Validations = append(bparams.Validations, params.Validation.String())
300 }
301
302 bparams.Outputs = proptools.NinjaEscapeList(bparams.Outputs)
303 bparams.ImplicitOutputs = proptools.NinjaEscapeList(bparams.ImplicitOutputs)
Colin Cross69452e12023-11-15 11:20:53 -0800304 bparams.Inputs = proptools.NinjaEscapeList(bparams.Inputs)
305 bparams.Implicits = proptools.NinjaEscapeList(bparams.Implicits)
306 bparams.OrderOnly = proptools.NinjaEscapeList(bparams.OrderOnly)
307 bparams.Validations = proptools.NinjaEscapeList(bparams.Validations)
308 bparams.Depfile = proptools.NinjaEscape(bparams.Depfile)
309
310 return bparams
311}
312
313func (m *moduleContext) Variable(pctx PackageContext, name, value string) {
314 if m.config.captureBuild {
315 m.variables[name] = value
316 }
317
318 m.bp.Variable(pctx.PackageContext, name, value)
319}
320
321func (m *moduleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
322 argNames ...string) blueprint.Rule {
323
324 if m.config.UseRemoteBuild() {
325 if params.Pool == nil {
326 // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict
327 // jobs to the local parallelism value
328 params.Pool = localPool
329 } else if params.Pool == remotePool {
330 // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's
331 // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS
332 // parallelism.
333 params.Pool = nil
334 }
335 }
336
337 rule := m.bp.Rule(pctx.PackageContext, name, params, argNames...)
338
339 if m.config.captureBuild {
340 m.ruleParams[rule] = params
341 }
342
343 return rule
344}
345
346func (m *moduleContext) Build(pctx PackageContext, params BuildParams) {
347 if params.Description != "" {
348 params.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
349 }
350
351 if missingDeps := m.GetMissingDependencies(); len(missingDeps) > 0 {
352 pctx, params = m.ninjaError(params, fmt.Errorf("module %s missing dependencies: %s\n",
353 m.ModuleName(), strings.Join(missingDeps, ", ")))
354 }
355
356 if m.config.captureBuild {
357 m.buildParams = append(m.buildParams, params)
358 }
359
360 bparams := convertBuildParams(params)
Colin Cross69452e12023-11-15 11:20:53 -0800361 m.bp.Build(pctx.PackageContext, bparams)
362}
363
364func (m *moduleContext) Phony(name string, deps ...Path) {
365 addPhony(m.config, name, deps...)
366}
367
368func (m *moduleContext) GetMissingDependencies() []string {
369 var missingDeps []string
370 missingDeps = append(missingDeps, m.Module().base().commonProperties.MissingDeps...)
371 missingDeps = append(missingDeps, m.bp.GetMissingDependencies()...)
372 missingDeps = FirstUniqueStrings(missingDeps)
373 return missingDeps
374}
375
376func (m *moduleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
377 module, _ := m.getDirectDepInternal(name, tag)
378 return module
379}
380
381func (m *moduleContext) ModuleSubDir() string {
382 return m.bp.ModuleSubDir()
383}
384
385func (m *moduleContext) SoongConfigTraceHash() string {
386 return m.module.base().commonProperties.SoongConfigTraceHash
387}
388
389func (m *moduleContext) InstallInData() bool {
390 return m.module.InstallInData()
391}
392
393func (m *moduleContext) InstallInTestcases() bool {
394 return m.module.InstallInTestcases()
395}
396
397func (m *moduleContext) InstallInSanitizerDir() bool {
398 return m.module.InstallInSanitizerDir()
399}
400
401func (m *moduleContext) InstallInRamdisk() bool {
402 return m.module.InstallInRamdisk()
403}
404
405func (m *moduleContext) InstallInVendorRamdisk() bool {
406 return m.module.InstallInVendorRamdisk()
407}
408
409func (m *moduleContext) InstallInDebugRamdisk() bool {
410 return m.module.InstallInDebugRamdisk()
411}
412
413func (m *moduleContext) InstallInRecovery() bool {
414 return m.module.InstallInRecovery()
415}
416
417func (m *moduleContext) InstallInRoot() bool {
418 return m.module.InstallInRoot()
419}
420
421func (m *moduleContext) InstallForceOS() (*OsType, *ArchType) {
422 return m.module.InstallForceOS()
423}
424
Colin Crossea30d852023-11-29 16:00:16 -0800425func (m *moduleContext) InstallInOdm() bool {
426 return m.module.InstallInOdm()
427}
428
429func (m *moduleContext) InstallInProduct() bool {
430 return m.module.InstallInProduct()
431}
432
Colin Cross69452e12023-11-15 11:20:53 -0800433func (m *moduleContext) InstallInVendor() bool {
434 return m.module.InstallInVendor()
435}
436
437func (m *moduleContext) skipInstall() bool {
438 if m.module.base().commonProperties.SkipInstall {
439 return true
440 }
441
442 if m.module.base().commonProperties.HideFromMake {
443 return true
444 }
445
446 // We'll need a solution for choosing which of modules with the same name in different
447 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
448 // list of namespaces to install in a Soong-only build.
449 if !m.module.base().commonProperties.NamespaceExportedToMake {
450 return true
451 }
452
453 return false
454}
455
Jiyong Park3f627e62024-05-01 16:14:38 +0900456// Tells whether this module is installed to the full install path (ex:
457// out/target/product/<name>/<partition>) or not. If this returns false, the install build rule is
458// not created and this module can only be installed to packaging modules like android_filesystem.
459func (m *moduleContext) requiresFullInstall() bool {
460 if m.skipInstall() {
461 return false
462 }
463
464 if proptools.Bool(m.module.base().commonProperties.No_full_install) {
465 return false
466 }
467
468 return true
469}
470
Colin Cross69452e12023-11-15 11:20:53 -0800471func (m *moduleContext) InstallFile(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800472 deps ...InstallPath) InstallPath {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800473 return m.installFile(installPath, name, srcPath, deps, false, true, nil)
Colin Cross69452e12023-11-15 11:20:53 -0800474}
475
476func (m *moduleContext) InstallExecutable(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800477 deps ...InstallPath) InstallPath {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800478 return m.installFile(installPath, name, srcPath, deps, true, true, nil)
Colin Cross69452e12023-11-15 11:20:53 -0800479}
480
481func (m *moduleContext) InstallFileWithExtraFilesZip(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800482 extraZip Path, deps ...InstallPath) InstallPath {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800483 return m.installFile(installPath, name, srcPath, deps, false, true, &extraFilesZip{
Colin Cross69452e12023-11-15 11:20:53 -0800484 zip: extraZip,
485 dir: installPath,
486 })
487}
488
489func (m *moduleContext) PackageFile(installPath InstallPath, name string, srcPath Path) PackagingSpec {
490 fullInstallPath := installPath.Join(m, name)
491 return m.packageFile(fullInstallPath, srcPath, false)
492}
493
Justin Yun74f3f302024-05-07 14:32:14 +0900494func (m *moduleContext) getAconfigPaths() *Paths {
495 return &m.module.base().aconfigFilePaths
496}
497
Colin Cross69452e12023-11-15 11:20:53 -0800498func (m *moduleContext) packageFile(fullInstallPath InstallPath, srcPath Path, executable bool) PackagingSpec {
499 licenseFiles := m.Module().EffectiveLicenseFiles()
500 spec := PackagingSpec{
501 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
502 srcPath: srcPath,
503 symlinkTarget: "",
504 executable: executable,
505 effectiveLicenseFiles: &licenseFiles,
506 partition: fullInstallPath.partition,
Jiyong Park4152b192024-04-30 21:24:21 +0900507 skipInstall: m.skipInstall(),
Justin Yun74f3f302024-05-07 14:32:14 +0900508 aconfigPaths: m.getAconfigPaths(),
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900509 archType: m.target.Arch.ArchType,
Colin Cross69452e12023-11-15 11:20:53 -0800510 }
511 m.packagingSpecs = append(m.packagingSpecs, spec)
512 return spec
513}
514
Colin Cross09ad3a62023-11-15 12:29:33 -0800515func (m *moduleContext) installFile(installPath InstallPath, name string, srcPath Path, deps []InstallPath,
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800516 executable bool, hooks bool, extraZip *extraFilesZip) InstallPath {
Colin Cross69452e12023-11-15 11:20:53 -0800517
518 fullInstallPath := installPath.Join(m, name)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800519 if hooks {
520 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, false)
521 }
Colin Cross69452e12023-11-15 11:20:53 -0800522
Jiyong Park3f627e62024-05-01 16:14:38 +0900523 if m.requiresFullInstall() {
Colin Cross09ad3a62023-11-15 12:29:33 -0800524 deps = append(deps, InstallPaths(m.module.base().installFilesDepSet.ToList())...)
Colin Crossd6fd0132023-11-06 13:54:06 -0800525 deps = append(deps, m.module.base().installedInitRcPaths...)
526 deps = append(deps, m.module.base().installedVintfFragmentsPaths...)
Colin Cross69452e12023-11-15 11:20:53 -0800527
528 var implicitDeps, orderOnlyDeps Paths
529
530 if m.Host() {
531 // Installed host modules might be used during the build, depend directly on their
532 // dependencies so their timestamp is updated whenever their dependency is updated
Colin Cross09ad3a62023-11-15 12:29:33 -0800533 implicitDeps = InstallPaths(deps).Paths()
Colin Cross69452e12023-11-15 11:20:53 -0800534 } else {
Colin Cross09ad3a62023-11-15 12:29:33 -0800535 orderOnlyDeps = InstallPaths(deps).Paths()
Colin Cross69452e12023-11-15 11:20:53 -0800536 }
537
538 if m.Config().KatiEnabled() {
539 // When creating the install rule in Soong but embedding in Make, write the rule to a
540 // makefile instead of directly to the ninja file so that main.mk can add the
541 // dependencies from the `required` property that are hard to resolve in Soong.
542 m.katiInstalls = append(m.katiInstalls, katiInstall{
543 from: srcPath,
544 to: fullInstallPath,
545 implicitDeps: implicitDeps,
546 orderOnlyDeps: orderOnlyDeps,
547 executable: executable,
548 extraFiles: extraZip,
549 })
550 } else {
551 rule := Cp
552 if executable {
553 rule = CpExecutable
554 }
555
556 extraCmds := ""
557 if extraZip != nil {
558 extraCmds += fmt.Sprintf(" && ( unzip -qDD -d '%s' '%s' 2>&1 | grep -v \"zipfile is empty\"; exit $${PIPESTATUS[0]} )",
559 extraZip.dir.String(), extraZip.zip.String())
560 extraCmds += " || ( code=$$?; if [ $$code -ne 0 -a $$code -ne 1 ]; then exit $$code; fi )"
561 implicitDeps = append(implicitDeps, extraZip.zip)
562 }
563
564 m.Build(pctx, BuildParams{
565 Rule: rule,
566 Description: "install " + fullInstallPath.Base(),
567 Output: fullInstallPath,
568 Input: srcPath,
569 Implicits: implicitDeps,
570 OrderOnly: orderOnlyDeps,
571 Default: !m.Config().KatiEnabled(),
572 Args: map[string]string{
573 "extraCmds": extraCmds,
574 },
575 })
576 }
577
578 m.installFiles = append(m.installFiles, fullInstallPath)
579 }
580
581 m.packageFile(fullInstallPath, srcPath, executable)
582
583 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
584
585 return fullInstallPath
586}
587
588func (m *moduleContext) InstallSymlink(installPath InstallPath, name string, srcPath InstallPath) InstallPath {
589 fullInstallPath := installPath.Join(m, name)
590 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, true)
591
592 relPath, err := filepath.Rel(path.Dir(fullInstallPath.String()), srcPath.String())
593 if err != nil {
594 panic(fmt.Sprintf("Unable to generate symlink between %q and %q: %s", fullInstallPath.Base(), srcPath.Base(), err))
595 }
Jiyong Park3f627e62024-05-01 16:14:38 +0900596 if m.requiresFullInstall() {
Colin Cross69452e12023-11-15 11:20:53 -0800597
598 if m.Config().KatiEnabled() {
599 // When creating the symlink rule in Soong but embedding in Make, write the rule to a
600 // makefile instead of directly to the ninja file so that main.mk can add the
601 // dependencies from the `required` property that are hard to resolve in Soong.
602 m.katiSymlinks = append(m.katiSymlinks, katiInstall{
603 from: srcPath,
604 to: fullInstallPath,
605 })
606 } else {
607 // The symlink doesn't need updating when the target is modified, but we sometimes
608 // have a dependency on a symlink to a binary instead of to the binary directly, and
609 // the mtime of the symlink must be updated when the binary is modified, so use a
610 // normal dependency here instead of an order-only dependency.
611 m.Build(pctx, BuildParams{
612 Rule: Symlink,
613 Description: "install symlink " + fullInstallPath.Base(),
614 Output: fullInstallPath,
615 Input: srcPath,
616 Default: !m.Config().KatiEnabled(),
617 Args: map[string]string{
618 "fromPath": relPath,
619 },
620 })
621 }
622
623 m.installFiles = append(m.installFiles, fullInstallPath)
624 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
625 }
626
627 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
628 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
629 srcPath: nil,
630 symlinkTarget: relPath,
631 executable: false,
632 partition: fullInstallPath.partition,
Jiyong Park4152b192024-04-30 21:24:21 +0900633 skipInstall: m.skipInstall(),
Justin Yun74f3f302024-05-07 14:32:14 +0900634 aconfigPaths: m.getAconfigPaths(),
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900635 archType: m.target.Arch.ArchType,
Colin Cross69452e12023-11-15 11:20:53 -0800636 })
637
638 return fullInstallPath
639}
640
641// installPath/name -> absPath where absPath might be a path that is available only at runtime
642// (e.g. /apex/...)
643func (m *moduleContext) InstallAbsoluteSymlink(installPath InstallPath, name string, absPath string) InstallPath {
644 fullInstallPath := installPath.Join(m, name)
645 m.module.base().hooks.runInstallHooks(m, nil, fullInstallPath, true)
646
Jiyong Park3f627e62024-05-01 16:14:38 +0900647 if m.requiresFullInstall() {
Colin Cross69452e12023-11-15 11:20:53 -0800648 if m.Config().KatiEnabled() {
649 // When creating the symlink rule in Soong but embedding in Make, write the rule to a
650 // makefile instead of directly to the ninja file so that main.mk can add the
651 // dependencies from the `required` property that are hard to resolve in Soong.
652 m.katiSymlinks = append(m.katiSymlinks, katiInstall{
653 absFrom: absPath,
654 to: fullInstallPath,
655 })
656 } else {
657 m.Build(pctx, BuildParams{
658 Rule: Symlink,
659 Description: "install symlink " + fullInstallPath.Base() + " -> " + absPath,
660 Output: fullInstallPath,
661 Default: !m.Config().KatiEnabled(),
662 Args: map[string]string{
663 "fromPath": absPath,
664 },
665 })
666 }
667
668 m.installFiles = append(m.installFiles, fullInstallPath)
669 }
670
671 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
672 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
673 srcPath: nil,
674 symlinkTarget: absPath,
675 executable: false,
676 partition: fullInstallPath.partition,
Jiyong Park4152b192024-04-30 21:24:21 +0900677 skipInstall: m.skipInstall(),
Justin Yun74f3f302024-05-07 14:32:14 +0900678 aconfigPaths: m.getAconfigPaths(),
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900679 archType: m.target.Arch.ArchType,
Colin Cross69452e12023-11-15 11:20:53 -0800680 })
681
682 return fullInstallPath
683}
684
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800685func (m *moduleContext) InstallTestData(installPath InstallPath, data []DataPath) InstallPaths {
686 m.testData = append(m.testData, data...)
687
688 ret := make(InstallPaths, 0, len(data))
689 for _, d := range data {
690 relPath := d.ToRelativeInstallPath()
691 installed := m.installFile(installPath, relPath, d.SrcPath, nil, false, false, nil)
692 ret = append(ret, installed)
693 }
694
695 return ret
696}
697
Colin Cross69452e12023-11-15 11:20:53 -0800698func (m *moduleContext) CheckbuildFile(srcPath Path) {
699 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
700}
701
702func (m *moduleContext) blueprintModuleContext() blueprint.ModuleContext {
703 return m.bp
704}
705
706func (m *moduleContext) LicenseMetadataFile() Path {
707 return m.module.base().licenseMetadataFile
708}
709
Colin Crossd6fd0132023-11-06 13:54:06 -0800710func (m *moduleContext) ModuleInfoJSON() *ModuleInfoJSON {
711 if moduleInfoJSON := m.module.base().moduleInfoJSON; moduleInfoJSON != nil {
712 return moduleInfoJSON
713 }
714 moduleInfoJSON := &ModuleInfoJSON{}
715 m.module.base().moduleInfoJSON = moduleInfoJSON
716 return moduleInfoJSON
717}
718
mrziwange6c85812024-05-22 14:36:09 -0700719func (m *moduleContext) SetOutputFiles(outputFiles Paths, tag string) {
720 if tag == "" {
721 if len(m.module.base().outputFiles.DefaultOutputFiles) > 0 {
722 m.ModuleErrorf("Module %s default OutputFiles cannot be overwritten", m.ModuleName())
723 }
724 m.module.base().outputFiles.DefaultOutputFiles = outputFiles
725 } else {
mrziwang57768d72024-06-06 11:31:51 -0700726 if m.module.base().outputFiles.TaggedOutputFiles == nil {
727 m.module.base().outputFiles.TaggedOutputFiles = make(map[string]Paths)
728 }
mrziwange6c85812024-05-22 14:36:09 -0700729 if _, exists := m.module.base().outputFiles.TaggedOutputFiles[tag]; exists {
730 m.ModuleErrorf("Module %s OutputFiles at tag %s cannot be overwritten", m.ModuleName(), tag)
731 } else {
732 m.module.base().outputFiles.TaggedOutputFiles[tag] = outputFiles
733 }
734 }
735}
736
Wei Lia1aa2972024-06-21 13:08:51 -0700737func (m *moduleContext) ComplianceMetadataInfo() *ComplianceMetadataInfo {
738 if complianceMetadataInfo := m.module.base().complianceMetadataInfo; complianceMetadataInfo != nil {
739 return complianceMetadataInfo
740 }
741 complianceMetadataInfo := NewComplianceMetadataInfo()
742 m.module.base().complianceMetadataInfo = complianceMetadataInfo
743 return complianceMetadataInfo
744}
745
Colin Cross69452e12023-11-15 11:20:53 -0800746// Returns a list of paths expanded from globs and modules referenced using ":module" syntax. The property must
747// be tagged with `android:"path" to support automatic source module dependency resolution.
748//
749// Deprecated: use PathsForModuleSrc or PathsForModuleSrcExcludes instead.
750func (m *moduleContext) ExpandSources(srcFiles, excludes []string) Paths {
751 return PathsForModuleSrcExcludes(m, srcFiles, excludes)
752}
753
754// Returns a single path expanded from globs and modules referenced using ":module" syntax. The property must
755// be tagged with `android:"path" to support automatic source module dependency resolution.
756//
757// Deprecated: use PathForModuleSrc instead.
758func (m *moduleContext) ExpandSource(srcFile, _ string) Path {
759 return PathForModuleSrc(m, srcFile)
760}
761
762// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
763// the srcFile is non-nil. The property must be tagged with `android:"path" to support automatic source module
764// dependency resolution.
765func (m *moduleContext) ExpandOptionalSource(srcFile *string, _ string) OptionalPath {
766 if srcFile != nil {
767 return OptionalPathForPath(PathForModuleSrc(m, *srcFile))
768 }
769 return OptionalPath{}
770}
771
Cole Faust43ddd082024-06-17 12:32:40 -0700772func (m *moduleContext) RequiredModuleNames(ctx ConfigAndErrorContext) []string {
773 return m.module.RequiredModuleNames(ctx)
Colin Cross69452e12023-11-15 11:20:53 -0800774}
775
776func (m *moduleContext) HostRequiredModuleNames() []string {
777 return m.module.HostRequiredModuleNames()
778}
779
780func (m *moduleContext) TargetRequiredModuleNames() []string {
781 return m.module.TargetRequiredModuleNames()
782}