blob: 3fc5d01587887a3cdd16b614ef7bc0f937104073 [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"
Cole Faust5a231bd2024-02-07 09:43:59 -080024 "github.com/google/blueprint/parser"
Cole Faust9a346f62024-01-18 20:12:02 +000025 "github.com/google/blueprint/proptools"
Colin Cross69452e12023-11-15 11:20:53 -080026)
27
28// BuildParameters describes the set of potential parameters to build a Ninja rule.
29// In general, these correspond to a Ninja concept.
30type BuildParams struct {
31 // A Ninja Rule that will be written to the Ninja file. This allows factoring out common code
32 // among multiple modules to reduce repetition in the Ninja file of action requirements. A rule
33 // can contain variables that should be provided in Args.
34 Rule blueprint.Rule
35 // Deps represents the depfile format. When using RuleBuilder, this defaults to GCC when depfiles
36 // are used.
37 Deps blueprint.Deps
38 // Depfile is a writeable path that allows correct incremental builds when the inputs have not
39 // been fully specified by the Ninja rule. Ninja supports a subset of the Makefile depfile syntax.
40 Depfile WritablePath
41 // A description of the build action.
42 Description string
43 // Output is an output file of the action. When using this field, references to $out in the Ninja
44 // command will refer to this file.
45 Output WritablePath
46 // Outputs is a slice of output file of the action. When using this field, references to $out in
47 // the Ninja command will refer to these files.
48 Outputs WritablePaths
Colin Cross69452e12023-11-15 11:20:53 -080049 // ImplicitOutput is an output file generated by the action. Note: references to `$out` in the
50 // Ninja command will NOT include references to this file.
51 ImplicitOutput WritablePath
52 // ImplicitOutputs is a slice of output files generated by the action. Note: references to `$out`
53 // in the Ninja command will NOT include references to these files.
54 ImplicitOutputs WritablePaths
55 // Input is an input file to the Ninja action. When using this field, references to $in in the
56 // Ninja command will refer to this file.
57 Input Path
58 // Inputs is a slice of input files to the Ninja action. When using this field, references to $in
59 // in the Ninja command will refer to these files.
60 Inputs Paths
61 // Implicit is an input file to the Ninja action. Note: references to `$in` in the Ninja command
62 // will NOT include references to this file.
63 Implicit Path
64 // Implicits is a slice of input files to the Ninja action. Note: references to `$in` in the Ninja
65 // command will NOT include references to these files.
66 Implicits Paths
67 // OrderOnly are Ninja order-only inputs to the action. When these are out of date, the output is
68 // not rebuilt until they are built, but changes in order-only dependencies alone do not cause the
69 // output to be rebuilt.
70 OrderOnly Paths
71 // Validation is an output path for a validation action. Validation outputs imply lower
72 // non-blocking priority to building non-validation outputs.
73 Validation Path
74 // Validations is a slice of output path for a validation action. Validation outputs imply lower
75 // non-blocking priority to building non-validation outputs.
76 Validations Paths
77 // Whether to skip outputting a default target statement which will be built by Ninja when no
78 // targets are specified on Ninja's command line.
79 Default bool
80 // Args is a key value mapping for replacements of variables within the Rule
81 Args map[string]string
82}
83
84type ModuleBuildParams BuildParams
85
86type ModuleContext interface {
87 BaseModuleContext
88
89 blueprintModuleContext() blueprint.ModuleContext
90
91 // Deprecated: use ModuleContext.Build instead.
92 ModuleBuild(pctx PackageContext, params ModuleBuildParams)
93
94 // Returns a list of paths expanded from globs and modules referenced using ":module" syntax. The property must
95 // be tagged with `android:"path" to support automatic source module dependency resolution.
96 //
97 // Deprecated: use PathsForModuleSrc or PathsForModuleSrcExcludes instead.
98 ExpandSources(srcFiles, excludes []string) Paths
99
100 // Returns a single path expanded from globs and modules referenced using ":module" syntax. The property must
101 // be tagged with `android:"path" to support automatic source module dependency resolution.
102 //
103 // Deprecated: use PathForModuleSrc instead.
104 ExpandSource(srcFile, prop string) Path
105
106 ExpandOptionalSource(srcFile *string, prop string) OptionalPath
107
108 // InstallExecutable creates a rule to copy srcPath to name in the installPath directory,
109 // with the given additional dependencies. The file is marked executable after copying.
110 //
111 // The installed file will be returned by FilesToInstall(), and the PackagingSpec for the
112 // installed file will be returned by PackagingSpecs() on this module or by
113 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
114 // for which IsInstallDepNeeded returns true.
Colin Cross09ad3a62023-11-15 12:29:33 -0800115 InstallExecutable(installPath InstallPath, name string, srcPath Path, deps ...InstallPath) InstallPath
Colin Cross69452e12023-11-15 11:20:53 -0800116
117 // InstallFile creates a rule to copy srcPath to name in the installPath directory,
118 // with the given additional dependencies.
119 //
120 // The installed file will be returned by FilesToInstall(), and the PackagingSpec for the
121 // installed file will be returned by PackagingSpecs() on this module or by
122 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
123 // for which IsInstallDepNeeded returns true.
Colin Cross09ad3a62023-11-15 12:29:33 -0800124 InstallFile(installPath InstallPath, name string, srcPath Path, deps ...InstallPath) InstallPath
Colin Cross69452e12023-11-15 11:20:53 -0800125
126 // InstallFileWithExtraFilesZip creates a rule to copy srcPath to name in the installPath
127 // directory, and also unzip a zip file containing extra files to install into the same
128 // directory.
129 //
130 // The installed file will be returned by FilesToInstall(), and the PackagingSpec for the
131 // installed file will be returned by PackagingSpecs() on this module or by
132 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
133 // for which IsInstallDepNeeded returns true.
Colin Cross09ad3a62023-11-15 12:29:33 -0800134 InstallFileWithExtraFilesZip(installPath InstallPath, name string, srcPath Path, extraZip Path, deps ...InstallPath) InstallPath
Colin Cross69452e12023-11-15 11:20:53 -0800135
136 // InstallSymlink creates a rule to create a symlink from src srcPath to name in the installPath
137 // directory.
138 //
139 // The installed symlink will be returned by FilesToInstall(), and the PackagingSpec for the
140 // installed file will be returned by PackagingSpecs() on this module or by
141 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
142 // for which IsInstallDepNeeded returns true.
143 InstallSymlink(installPath InstallPath, name string, srcPath InstallPath) InstallPath
144
145 // InstallAbsoluteSymlink creates a rule to create an absolute symlink from src srcPath to name
146 // in the installPath directory.
147 //
148 // The installed symlink will be returned by FilesToInstall(), and the PackagingSpec for the
149 // installed file will be returned by PackagingSpecs() on this module or by
150 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
151 // for which IsInstallDepNeeded returns true.
152 InstallAbsoluteSymlink(installPath InstallPath, name string, absPath string) InstallPath
153
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800154 // InstallTestData creates rules to install test data (e.g. data files used during a test) into
155 // the installPath directory.
156 //
157 // The installed files will be returned by FilesToInstall(), and the PackagingSpec for the
158 // installed files will be returned by PackagingSpecs() on this module or by
159 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
160 // for which IsInstallDepNeeded returns true.
161 InstallTestData(installPath InstallPath, data []DataPath) InstallPaths
162
Colin Cross69452e12023-11-15 11:20:53 -0800163 // PackageFile creates a PackagingSpec as if InstallFile was called, but without creating
164 // the rule to copy the file. This is useful to define how a module would be packaged
165 // without installing it into the global installation directories.
166 //
167 // The created PackagingSpec for the will be returned by PackagingSpecs() on this module or by
168 // TransitivePackagingSpecs() on modules that depend on this module through dependency tags
169 // for which IsInstallDepNeeded returns true.
170 PackageFile(installPath InstallPath, name string, srcPath Path) PackagingSpec
171
172 CheckbuildFile(srcPath Path)
173
174 InstallInData() bool
175 InstallInTestcases() bool
176 InstallInSanitizerDir() bool
177 InstallInRamdisk() bool
178 InstallInVendorRamdisk() bool
179 InstallInDebugRamdisk() bool
180 InstallInRecovery() bool
181 InstallInRoot() bool
Colin Crossea30d852023-11-29 16:00:16 -0800182 InstallInOdm() bool
183 InstallInProduct() bool
Colin Cross69452e12023-11-15 11:20:53 -0800184 InstallInVendor() bool
185 InstallForceOS() (*OsType, *ArchType)
186
187 RequiredModuleNames() []string
188 HostRequiredModuleNames() []string
189 TargetRequiredModuleNames() []string
190
191 ModuleSubDir() string
192 SoongConfigTraceHash() string
193
194 Variable(pctx PackageContext, name, value string)
195 Rule(pctx PackageContext, name string, params blueprint.RuleParams, argNames ...string) blueprint.Rule
196 // Similar to blueprint.ModuleContext.Build, but takes Paths instead of []string,
197 // and performs more verification.
198 Build(pctx PackageContext, params BuildParams)
199 // Phony creates a Make-style phony rule, a rule with no commands that can depend on other
200 // phony rules or real files. Phony can be called on the same name multiple times to add
201 // additional dependencies.
202 Phony(phony string, deps ...Path)
203
204 // GetMissingDependencies returns the list of dependencies that were passed to AddDependencies or related methods,
205 // but do not exist.
206 GetMissingDependencies() []string
207
208 // LicenseMetadataFile returns the path where the license metadata for this module will be
209 // generated.
210 LicenseMetadataFile() Path
Colin Crossd6fd0132023-11-06 13:54:06 -0800211
212 // ModuleInfoJSON returns a pointer to the ModuleInfoJSON struct that can be filled out by
213 // GenerateAndroidBuildActions. If it is called then the struct will be written out and included in
214 // the module-info.json generated by Make, and Make will not generate its own data for this module.
215 ModuleInfoJSON() *ModuleInfoJSON
Cole Faust5a231bd2024-02-07 09:43:59 -0800216
217 // EvaluateConfiguration makes ModuleContext a valid proptools.ConfigurableEvaluator, so this context
218 // can be used to evaluate the final value of Configurable properties.
219 EvaluateConfiguration(parser.SelectType, string) (string, bool)
Colin Cross69452e12023-11-15 11:20:53 -0800220}
221
222type moduleContext struct {
223 bp blueprint.ModuleContext
224 baseModuleContext
225 packagingSpecs []PackagingSpec
226 installFiles InstallPaths
227 checkbuildFiles Paths
228 module Module
229 phonies map[string]Paths
230
231 katiInstalls []katiInstall
232 katiSymlinks []katiInstall
233
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800234 testData []DataPath
235
Colin Cross69452e12023-11-15 11:20:53 -0800236 // For tests
237 buildParams []BuildParams
238 ruleParams map[blueprint.Rule]blueprint.RuleParams
239 variables map[string]string
240}
241
242func (m *moduleContext) ninjaError(params BuildParams, err error) (PackageContext, BuildParams) {
243 return pctx, BuildParams{
244 Rule: ErrorRule,
245 Description: params.Description,
246 Output: params.Output,
247 Outputs: params.Outputs,
248 ImplicitOutput: params.ImplicitOutput,
249 ImplicitOutputs: params.ImplicitOutputs,
250 Args: map[string]string{
251 "error": err.Error(),
252 },
253 }
254}
255
256func (m *moduleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
257 m.Build(pctx, BuildParams(params))
258}
259
Colin Cross69452e12023-11-15 11:20:53 -0800260// Convert build parameters from their concrete Android types into their string representations,
261// and combine the singular and plural fields of the same type (e.g. Output and Outputs).
262func convertBuildParams(params BuildParams) blueprint.BuildParams {
263 bparams := blueprint.BuildParams{
264 Rule: params.Rule,
265 Description: params.Description,
266 Deps: params.Deps,
267 Outputs: params.Outputs.Strings(),
268 ImplicitOutputs: params.ImplicitOutputs.Strings(),
Colin Cross69452e12023-11-15 11:20:53 -0800269 Inputs: params.Inputs.Strings(),
270 Implicits: params.Implicits.Strings(),
271 OrderOnly: params.OrderOnly.Strings(),
272 Validations: params.Validations.Strings(),
273 Args: params.Args,
274 Optional: !params.Default,
275 }
276
277 if params.Depfile != nil {
278 bparams.Depfile = params.Depfile.String()
279 }
280 if params.Output != nil {
281 bparams.Outputs = append(bparams.Outputs, params.Output.String())
282 }
Colin Cross69452e12023-11-15 11:20:53 -0800283 if params.ImplicitOutput != nil {
284 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
285 }
286 if params.Input != nil {
287 bparams.Inputs = append(bparams.Inputs, params.Input.String())
288 }
289 if params.Implicit != nil {
290 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
291 }
292 if params.Validation != nil {
293 bparams.Validations = append(bparams.Validations, params.Validation.String())
294 }
295
296 bparams.Outputs = proptools.NinjaEscapeList(bparams.Outputs)
297 bparams.ImplicitOutputs = proptools.NinjaEscapeList(bparams.ImplicitOutputs)
Colin Cross69452e12023-11-15 11:20:53 -0800298 bparams.Inputs = proptools.NinjaEscapeList(bparams.Inputs)
299 bparams.Implicits = proptools.NinjaEscapeList(bparams.Implicits)
300 bparams.OrderOnly = proptools.NinjaEscapeList(bparams.OrderOnly)
301 bparams.Validations = proptools.NinjaEscapeList(bparams.Validations)
302 bparams.Depfile = proptools.NinjaEscape(bparams.Depfile)
303
304 return bparams
305}
306
307func (m *moduleContext) Variable(pctx PackageContext, name, value string) {
308 if m.config.captureBuild {
309 m.variables[name] = value
310 }
311
312 m.bp.Variable(pctx.PackageContext, name, value)
313}
314
315func (m *moduleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
316 argNames ...string) blueprint.Rule {
317
318 if m.config.UseRemoteBuild() {
319 if params.Pool == nil {
320 // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict
321 // jobs to the local parallelism value
322 params.Pool = localPool
323 } else if params.Pool == remotePool {
324 // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's
325 // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS
326 // parallelism.
327 params.Pool = nil
328 }
329 }
330
331 rule := m.bp.Rule(pctx.PackageContext, name, params, argNames...)
332
333 if m.config.captureBuild {
334 m.ruleParams[rule] = params
335 }
336
337 return rule
338}
339
340func (m *moduleContext) Build(pctx PackageContext, params BuildParams) {
341 if params.Description != "" {
342 params.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
343 }
344
345 if missingDeps := m.GetMissingDependencies(); len(missingDeps) > 0 {
346 pctx, params = m.ninjaError(params, fmt.Errorf("module %s missing dependencies: %s\n",
347 m.ModuleName(), strings.Join(missingDeps, ", ")))
348 }
349
350 if m.config.captureBuild {
351 m.buildParams = append(m.buildParams, params)
352 }
353
354 bparams := convertBuildParams(params)
Colin Cross69452e12023-11-15 11:20:53 -0800355 m.bp.Build(pctx.PackageContext, bparams)
356}
357
358func (m *moduleContext) Phony(name string, deps ...Path) {
359 addPhony(m.config, name, deps...)
360}
361
362func (m *moduleContext) GetMissingDependencies() []string {
363 var missingDeps []string
364 missingDeps = append(missingDeps, m.Module().base().commonProperties.MissingDeps...)
365 missingDeps = append(missingDeps, m.bp.GetMissingDependencies()...)
366 missingDeps = FirstUniqueStrings(missingDeps)
367 return missingDeps
368}
369
370func (m *moduleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
371 module, _ := m.getDirectDepInternal(name, tag)
372 return module
373}
374
375func (m *moduleContext) ModuleSubDir() string {
376 return m.bp.ModuleSubDir()
377}
378
379func (m *moduleContext) SoongConfigTraceHash() string {
380 return m.module.base().commonProperties.SoongConfigTraceHash
381}
382
383func (m *moduleContext) InstallInData() bool {
384 return m.module.InstallInData()
385}
386
387func (m *moduleContext) InstallInTestcases() bool {
388 return m.module.InstallInTestcases()
389}
390
391func (m *moduleContext) InstallInSanitizerDir() bool {
392 return m.module.InstallInSanitizerDir()
393}
394
395func (m *moduleContext) InstallInRamdisk() bool {
396 return m.module.InstallInRamdisk()
397}
398
399func (m *moduleContext) InstallInVendorRamdisk() bool {
400 return m.module.InstallInVendorRamdisk()
401}
402
403func (m *moduleContext) InstallInDebugRamdisk() bool {
404 return m.module.InstallInDebugRamdisk()
405}
406
407func (m *moduleContext) InstallInRecovery() bool {
408 return m.module.InstallInRecovery()
409}
410
411func (m *moduleContext) InstallInRoot() bool {
412 return m.module.InstallInRoot()
413}
414
415func (m *moduleContext) InstallForceOS() (*OsType, *ArchType) {
416 return m.module.InstallForceOS()
417}
418
Colin Crossea30d852023-11-29 16:00:16 -0800419func (m *moduleContext) InstallInOdm() bool {
420 return m.module.InstallInOdm()
421}
422
423func (m *moduleContext) InstallInProduct() bool {
424 return m.module.InstallInProduct()
425}
426
Colin Cross69452e12023-11-15 11:20:53 -0800427func (m *moduleContext) InstallInVendor() bool {
428 return m.module.InstallInVendor()
429}
430
431func (m *moduleContext) skipInstall() bool {
432 if m.module.base().commonProperties.SkipInstall {
433 return true
434 }
435
436 if m.module.base().commonProperties.HideFromMake {
437 return true
438 }
439
440 // We'll need a solution for choosing which of modules with the same name in different
441 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
442 // list of namespaces to install in a Soong-only build.
443 if !m.module.base().commonProperties.NamespaceExportedToMake {
444 return true
445 }
446
447 return false
448}
449
450func (m *moduleContext) InstallFile(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800451 deps ...InstallPath) InstallPath {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800452 return m.installFile(installPath, name, srcPath, deps, false, true, nil)
Colin Cross69452e12023-11-15 11:20:53 -0800453}
454
455func (m *moduleContext) InstallExecutable(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800456 deps ...InstallPath) InstallPath {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800457 return m.installFile(installPath, name, srcPath, deps, true, true, nil)
Colin Cross69452e12023-11-15 11:20:53 -0800458}
459
460func (m *moduleContext) InstallFileWithExtraFilesZip(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800461 extraZip Path, deps ...InstallPath) InstallPath {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800462 return m.installFile(installPath, name, srcPath, deps, false, true, &extraFilesZip{
Colin Cross69452e12023-11-15 11:20:53 -0800463 zip: extraZip,
464 dir: installPath,
465 })
466}
467
468func (m *moduleContext) PackageFile(installPath InstallPath, name string, srcPath Path) PackagingSpec {
469 fullInstallPath := installPath.Join(m, name)
470 return m.packageFile(fullInstallPath, srcPath, false)
471}
472
473func (m *moduleContext) packageFile(fullInstallPath InstallPath, srcPath Path, executable bool) PackagingSpec {
474 licenseFiles := m.Module().EffectiveLicenseFiles()
475 spec := PackagingSpec{
476 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
477 srcPath: srcPath,
478 symlinkTarget: "",
479 executable: executable,
480 effectiveLicenseFiles: &licenseFiles,
481 partition: fullInstallPath.partition,
482 }
483 m.packagingSpecs = append(m.packagingSpecs, spec)
484 return spec
485}
486
Colin Cross09ad3a62023-11-15 12:29:33 -0800487func (m *moduleContext) installFile(installPath InstallPath, name string, srcPath Path, deps []InstallPath,
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800488 executable bool, hooks bool, extraZip *extraFilesZip) InstallPath {
Colin Cross69452e12023-11-15 11:20:53 -0800489
490 fullInstallPath := installPath.Join(m, name)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800491 if hooks {
492 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, false)
493 }
Colin Cross69452e12023-11-15 11:20:53 -0800494
495 if !m.skipInstall() {
Colin Cross09ad3a62023-11-15 12:29:33 -0800496 deps = append(deps, InstallPaths(m.module.base().installFilesDepSet.ToList())...)
Colin Crossd6fd0132023-11-06 13:54:06 -0800497 deps = append(deps, m.module.base().installedInitRcPaths...)
498 deps = append(deps, m.module.base().installedVintfFragmentsPaths...)
Colin Cross69452e12023-11-15 11:20:53 -0800499
500 var implicitDeps, orderOnlyDeps Paths
501
502 if m.Host() {
503 // Installed host modules might be used during the build, depend directly on their
504 // dependencies so their timestamp is updated whenever their dependency is updated
Colin Cross09ad3a62023-11-15 12:29:33 -0800505 implicitDeps = InstallPaths(deps).Paths()
Colin Cross69452e12023-11-15 11:20:53 -0800506 } else {
Colin Cross09ad3a62023-11-15 12:29:33 -0800507 orderOnlyDeps = InstallPaths(deps).Paths()
Colin Cross69452e12023-11-15 11:20:53 -0800508 }
509
510 if m.Config().KatiEnabled() {
511 // When creating the install rule in Soong but embedding in Make, write the rule to a
512 // makefile instead of directly to the ninja file so that main.mk can add the
513 // dependencies from the `required` property that are hard to resolve in Soong.
514 m.katiInstalls = append(m.katiInstalls, katiInstall{
515 from: srcPath,
516 to: fullInstallPath,
517 implicitDeps: implicitDeps,
518 orderOnlyDeps: orderOnlyDeps,
519 executable: executable,
520 extraFiles: extraZip,
521 })
522 } else {
523 rule := Cp
524 if executable {
525 rule = CpExecutable
526 }
527
528 extraCmds := ""
529 if extraZip != nil {
530 extraCmds += fmt.Sprintf(" && ( unzip -qDD -d '%s' '%s' 2>&1 | grep -v \"zipfile is empty\"; exit $${PIPESTATUS[0]} )",
531 extraZip.dir.String(), extraZip.zip.String())
532 extraCmds += " || ( code=$$?; if [ $$code -ne 0 -a $$code -ne 1 ]; then exit $$code; fi )"
533 implicitDeps = append(implicitDeps, extraZip.zip)
534 }
535
536 m.Build(pctx, BuildParams{
537 Rule: rule,
538 Description: "install " + fullInstallPath.Base(),
539 Output: fullInstallPath,
540 Input: srcPath,
541 Implicits: implicitDeps,
542 OrderOnly: orderOnlyDeps,
543 Default: !m.Config().KatiEnabled(),
544 Args: map[string]string{
545 "extraCmds": extraCmds,
546 },
547 })
548 }
549
550 m.installFiles = append(m.installFiles, fullInstallPath)
551 }
552
553 m.packageFile(fullInstallPath, srcPath, executable)
554
555 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
556
557 return fullInstallPath
558}
559
560func (m *moduleContext) InstallSymlink(installPath InstallPath, name string, srcPath InstallPath) InstallPath {
561 fullInstallPath := installPath.Join(m, name)
562 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, true)
563
564 relPath, err := filepath.Rel(path.Dir(fullInstallPath.String()), srcPath.String())
565 if err != nil {
566 panic(fmt.Sprintf("Unable to generate symlink between %q and %q: %s", fullInstallPath.Base(), srcPath.Base(), err))
567 }
568 if !m.skipInstall() {
569
570 if m.Config().KatiEnabled() {
571 // When creating the symlink rule in Soong but embedding in Make, write the rule to a
572 // makefile instead of directly to the ninja file so that main.mk can add the
573 // dependencies from the `required` property that are hard to resolve in Soong.
574 m.katiSymlinks = append(m.katiSymlinks, katiInstall{
575 from: srcPath,
576 to: fullInstallPath,
577 })
578 } else {
579 // The symlink doesn't need updating when the target is modified, but we sometimes
580 // have a dependency on a symlink to a binary instead of to the binary directly, and
581 // the mtime of the symlink must be updated when the binary is modified, so use a
582 // normal dependency here instead of an order-only dependency.
583 m.Build(pctx, BuildParams{
584 Rule: Symlink,
585 Description: "install symlink " + fullInstallPath.Base(),
586 Output: fullInstallPath,
587 Input: srcPath,
588 Default: !m.Config().KatiEnabled(),
589 Args: map[string]string{
590 "fromPath": relPath,
591 },
592 })
593 }
594
595 m.installFiles = append(m.installFiles, fullInstallPath)
596 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
597 }
598
599 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
600 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
601 srcPath: nil,
602 symlinkTarget: relPath,
603 executable: false,
604 partition: fullInstallPath.partition,
605 })
606
607 return fullInstallPath
608}
609
610// installPath/name -> absPath where absPath might be a path that is available only at runtime
611// (e.g. /apex/...)
612func (m *moduleContext) InstallAbsoluteSymlink(installPath InstallPath, name string, absPath string) InstallPath {
613 fullInstallPath := installPath.Join(m, name)
614 m.module.base().hooks.runInstallHooks(m, nil, fullInstallPath, true)
615
616 if !m.skipInstall() {
617 if m.Config().KatiEnabled() {
618 // When creating the symlink rule in Soong but embedding in Make, write the rule to a
619 // makefile instead of directly to the ninja file so that main.mk can add the
620 // dependencies from the `required` property that are hard to resolve in Soong.
621 m.katiSymlinks = append(m.katiSymlinks, katiInstall{
622 absFrom: absPath,
623 to: fullInstallPath,
624 })
625 } else {
626 m.Build(pctx, BuildParams{
627 Rule: Symlink,
628 Description: "install symlink " + fullInstallPath.Base() + " -> " + absPath,
629 Output: fullInstallPath,
630 Default: !m.Config().KatiEnabled(),
631 Args: map[string]string{
632 "fromPath": absPath,
633 },
634 })
635 }
636
637 m.installFiles = append(m.installFiles, fullInstallPath)
638 }
639
640 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
641 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
642 srcPath: nil,
643 symlinkTarget: absPath,
644 executable: false,
645 partition: fullInstallPath.partition,
646 })
647
648 return fullInstallPath
649}
650
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800651func (m *moduleContext) InstallTestData(installPath InstallPath, data []DataPath) InstallPaths {
652 m.testData = append(m.testData, data...)
653
654 ret := make(InstallPaths, 0, len(data))
655 for _, d := range data {
656 relPath := d.ToRelativeInstallPath()
657 installed := m.installFile(installPath, relPath, d.SrcPath, nil, false, false, nil)
658 ret = append(ret, installed)
659 }
660
661 return ret
662}
663
Colin Cross69452e12023-11-15 11:20:53 -0800664func (m *moduleContext) CheckbuildFile(srcPath Path) {
665 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
666}
667
668func (m *moduleContext) blueprintModuleContext() blueprint.ModuleContext {
669 return m.bp
670}
671
672func (m *moduleContext) LicenseMetadataFile() Path {
673 return m.module.base().licenseMetadataFile
674}
675
Colin Crossd6fd0132023-11-06 13:54:06 -0800676func (m *moduleContext) ModuleInfoJSON() *ModuleInfoJSON {
677 if moduleInfoJSON := m.module.base().moduleInfoJSON; moduleInfoJSON != nil {
678 return moduleInfoJSON
679 }
680 moduleInfoJSON := &ModuleInfoJSON{}
681 m.module.base().moduleInfoJSON = moduleInfoJSON
682 return moduleInfoJSON
683}
684
Colin Cross69452e12023-11-15 11:20:53 -0800685// Returns a list of paths expanded from globs and modules referenced using ":module" syntax. The property must
686// be tagged with `android:"path" to support automatic source module dependency resolution.
687//
688// Deprecated: use PathsForModuleSrc or PathsForModuleSrcExcludes instead.
689func (m *moduleContext) ExpandSources(srcFiles, excludes []string) Paths {
690 return PathsForModuleSrcExcludes(m, srcFiles, excludes)
691}
692
693// Returns a single path expanded from globs and modules referenced using ":module" syntax. The property must
694// be tagged with `android:"path" to support automatic source module dependency resolution.
695//
696// Deprecated: use PathForModuleSrc instead.
697func (m *moduleContext) ExpandSource(srcFile, _ string) Path {
698 return PathForModuleSrc(m, srcFile)
699}
700
701// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
702// the srcFile is non-nil. The property must be tagged with `android:"path" to support automatic source module
703// dependency resolution.
704func (m *moduleContext) ExpandOptionalSource(srcFile *string, _ string) OptionalPath {
705 if srcFile != nil {
706 return OptionalPathForPath(PathForModuleSrc(m, *srcFile))
707 }
708 return OptionalPath{}
709}
710
711func (m *moduleContext) RequiredModuleNames() []string {
712 return m.module.RequiredModuleNames()
713}
714
715func (m *moduleContext) HostRequiredModuleNames() []string {
716 return m.module.HostRequiredModuleNames()
717}
718
719func (m *moduleContext) TargetRequiredModuleNames() []string {
720 return m.module.TargetRequiredModuleNames()
721}
Cole Faust5a231bd2024-02-07 09:43:59 -0800722
723func (m *moduleContext) EvaluateConfiguration(ty parser.SelectType, condition string) (string, bool) {
724 switch ty {
725 case parser.SelectTypeReleaseVariable:
726 if v, ok := m.Config().productVariables.BuildFlags[condition]; ok {
727 return v, true
728 }
729 return "", false
730 case parser.SelectTypeProductVariable:
731 // TODO: Might add these on a case-by-case basis
732 m.ModuleErrorf("TODO(b/323382414): Product variables are not yet supported in selects")
733 return "", false
734 case parser.SelectTypeSoongConfigVariable:
735 parts := strings.Split(condition, ":")
736 namespace := parts[0]
737 variable := parts[1]
738 if n, ok := m.Config().productVariables.VendorVars[namespace]; ok {
739 if v, ok := n[variable]; ok {
740 return v, true
741 }
742 }
743 return "", false
744 case parser.SelectTypeVariant:
745 m.ModuleErrorf("TODO(b/323382414): Variants are not yet supported in selects")
746 return "", false
747 default:
748 panic("Should be unreachable")
749 }
750}