blob: 1cab63022e1918e95ccbf01878da521e4c52b559 [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
186 RequiredModuleNames() []string
187 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
Colin Cross69452e12023-11-15 11:20:53 -0800215}
216
217type moduleContext struct {
218 bp blueprint.ModuleContext
219 baseModuleContext
220 packagingSpecs []PackagingSpec
221 installFiles InstallPaths
222 checkbuildFiles Paths
223 module Module
224 phonies map[string]Paths
225
226 katiInstalls []katiInstall
227 katiSymlinks []katiInstall
228
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800229 testData []DataPath
230
Colin Cross69452e12023-11-15 11:20:53 -0800231 // For tests
232 buildParams []BuildParams
233 ruleParams map[blueprint.Rule]blueprint.RuleParams
234 variables map[string]string
235}
236
237func (m *moduleContext) ninjaError(params BuildParams, err error) (PackageContext, BuildParams) {
238 return pctx, BuildParams{
239 Rule: ErrorRule,
240 Description: params.Description,
241 Output: params.Output,
242 Outputs: params.Outputs,
243 ImplicitOutput: params.ImplicitOutput,
244 ImplicitOutputs: params.ImplicitOutputs,
245 Args: map[string]string{
246 "error": err.Error(),
247 },
248 }
249}
250
251func (m *moduleContext) ModuleBuild(pctx PackageContext, params ModuleBuildParams) {
252 m.Build(pctx, BuildParams(params))
253}
254
Colin Cross69452e12023-11-15 11:20:53 -0800255// Convert build parameters from their concrete Android types into their string representations,
256// and combine the singular and plural fields of the same type (e.g. Output and Outputs).
257func convertBuildParams(params BuildParams) blueprint.BuildParams {
258 bparams := blueprint.BuildParams{
259 Rule: params.Rule,
260 Description: params.Description,
261 Deps: params.Deps,
262 Outputs: params.Outputs.Strings(),
263 ImplicitOutputs: params.ImplicitOutputs.Strings(),
Colin Cross69452e12023-11-15 11:20:53 -0800264 Inputs: params.Inputs.Strings(),
265 Implicits: params.Implicits.Strings(),
266 OrderOnly: params.OrderOnly.Strings(),
267 Validations: params.Validations.Strings(),
268 Args: params.Args,
269 Optional: !params.Default,
270 }
271
272 if params.Depfile != nil {
273 bparams.Depfile = params.Depfile.String()
274 }
275 if params.Output != nil {
276 bparams.Outputs = append(bparams.Outputs, params.Output.String())
277 }
Colin Cross69452e12023-11-15 11:20:53 -0800278 if params.ImplicitOutput != nil {
279 bparams.ImplicitOutputs = append(bparams.ImplicitOutputs, params.ImplicitOutput.String())
280 }
281 if params.Input != nil {
282 bparams.Inputs = append(bparams.Inputs, params.Input.String())
283 }
284 if params.Implicit != nil {
285 bparams.Implicits = append(bparams.Implicits, params.Implicit.String())
286 }
287 if params.Validation != nil {
288 bparams.Validations = append(bparams.Validations, params.Validation.String())
289 }
290
291 bparams.Outputs = proptools.NinjaEscapeList(bparams.Outputs)
292 bparams.ImplicitOutputs = proptools.NinjaEscapeList(bparams.ImplicitOutputs)
Colin Cross69452e12023-11-15 11:20:53 -0800293 bparams.Inputs = proptools.NinjaEscapeList(bparams.Inputs)
294 bparams.Implicits = proptools.NinjaEscapeList(bparams.Implicits)
295 bparams.OrderOnly = proptools.NinjaEscapeList(bparams.OrderOnly)
296 bparams.Validations = proptools.NinjaEscapeList(bparams.Validations)
297 bparams.Depfile = proptools.NinjaEscape(bparams.Depfile)
298
299 return bparams
300}
301
302func (m *moduleContext) Variable(pctx PackageContext, name, value string) {
303 if m.config.captureBuild {
304 m.variables[name] = value
305 }
306
307 m.bp.Variable(pctx.PackageContext, name, value)
308}
309
310func (m *moduleContext) Rule(pctx PackageContext, name string, params blueprint.RuleParams,
311 argNames ...string) blueprint.Rule {
312
313 if m.config.UseRemoteBuild() {
314 if params.Pool == nil {
315 // When USE_GOMA=true or USE_RBE=true are set and the rule is not supported by goma/RBE, restrict
316 // jobs to the local parallelism value
317 params.Pool = localPool
318 } else if params.Pool == remotePool {
319 // remotePool is a fake pool used to identify rule that are supported for remoting. If the rule's
320 // pool is the remotePool, replace with nil so that ninja runs it at NINJA_REMOTE_NUM_JOBS
321 // parallelism.
322 params.Pool = nil
323 }
324 }
325
326 rule := m.bp.Rule(pctx.PackageContext, name, params, argNames...)
327
328 if m.config.captureBuild {
329 m.ruleParams[rule] = params
330 }
331
332 return rule
333}
334
335func (m *moduleContext) Build(pctx PackageContext, params BuildParams) {
336 if params.Description != "" {
337 params.Description = "${moduleDesc}" + params.Description + "${moduleDescSuffix}"
338 }
339
340 if missingDeps := m.GetMissingDependencies(); len(missingDeps) > 0 {
341 pctx, params = m.ninjaError(params, fmt.Errorf("module %s missing dependencies: %s\n",
342 m.ModuleName(), strings.Join(missingDeps, ", ")))
343 }
344
345 if m.config.captureBuild {
346 m.buildParams = append(m.buildParams, params)
347 }
348
349 bparams := convertBuildParams(params)
Colin Cross69452e12023-11-15 11:20:53 -0800350 m.bp.Build(pctx.PackageContext, bparams)
351}
352
353func (m *moduleContext) Phony(name string, deps ...Path) {
354 addPhony(m.config, name, deps...)
355}
356
357func (m *moduleContext) GetMissingDependencies() []string {
358 var missingDeps []string
359 missingDeps = append(missingDeps, m.Module().base().commonProperties.MissingDeps...)
360 missingDeps = append(missingDeps, m.bp.GetMissingDependencies()...)
361 missingDeps = FirstUniqueStrings(missingDeps)
362 return missingDeps
363}
364
365func (m *moduleContext) GetDirectDepWithTag(name string, tag blueprint.DependencyTag) blueprint.Module {
366 module, _ := m.getDirectDepInternal(name, tag)
367 return module
368}
369
370func (m *moduleContext) ModuleSubDir() string {
371 return m.bp.ModuleSubDir()
372}
373
374func (m *moduleContext) SoongConfigTraceHash() string {
375 return m.module.base().commonProperties.SoongConfigTraceHash
376}
377
378func (m *moduleContext) InstallInData() bool {
379 return m.module.InstallInData()
380}
381
382func (m *moduleContext) InstallInTestcases() bool {
383 return m.module.InstallInTestcases()
384}
385
386func (m *moduleContext) InstallInSanitizerDir() bool {
387 return m.module.InstallInSanitizerDir()
388}
389
390func (m *moduleContext) InstallInRamdisk() bool {
391 return m.module.InstallInRamdisk()
392}
393
394func (m *moduleContext) InstallInVendorRamdisk() bool {
395 return m.module.InstallInVendorRamdisk()
396}
397
398func (m *moduleContext) InstallInDebugRamdisk() bool {
399 return m.module.InstallInDebugRamdisk()
400}
401
402func (m *moduleContext) InstallInRecovery() bool {
403 return m.module.InstallInRecovery()
404}
405
406func (m *moduleContext) InstallInRoot() bool {
407 return m.module.InstallInRoot()
408}
409
410func (m *moduleContext) InstallForceOS() (*OsType, *ArchType) {
411 return m.module.InstallForceOS()
412}
413
Colin Crossea30d852023-11-29 16:00:16 -0800414func (m *moduleContext) InstallInOdm() bool {
415 return m.module.InstallInOdm()
416}
417
418func (m *moduleContext) InstallInProduct() bool {
419 return m.module.InstallInProduct()
420}
421
Colin Cross69452e12023-11-15 11:20:53 -0800422func (m *moduleContext) InstallInVendor() bool {
423 return m.module.InstallInVendor()
424}
425
426func (m *moduleContext) skipInstall() bool {
427 if m.module.base().commonProperties.SkipInstall {
428 return true
429 }
430
431 if m.module.base().commonProperties.HideFromMake {
432 return true
433 }
434
435 // We'll need a solution for choosing which of modules with the same name in different
436 // namespaces to install. For now, reuse the list of namespaces exported to Make as the
437 // list of namespaces to install in a Soong-only build.
438 if !m.module.base().commonProperties.NamespaceExportedToMake {
439 return true
440 }
441
442 return false
443}
444
445func (m *moduleContext) InstallFile(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800446 deps ...InstallPath) InstallPath {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800447 return m.installFile(installPath, name, srcPath, deps, false, true, nil)
Colin Cross69452e12023-11-15 11:20:53 -0800448}
449
450func (m *moduleContext) InstallExecutable(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, true, true, nil)
Colin Cross69452e12023-11-15 11:20:53 -0800453}
454
455func (m *moduleContext) InstallFileWithExtraFilesZip(installPath InstallPath, name string, srcPath Path,
Colin Cross09ad3a62023-11-15 12:29:33 -0800456 extraZip Path, deps ...InstallPath) InstallPath {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800457 return m.installFile(installPath, name, srcPath, deps, false, true, &extraFilesZip{
Colin Cross69452e12023-11-15 11:20:53 -0800458 zip: extraZip,
459 dir: installPath,
460 })
461}
462
463func (m *moduleContext) PackageFile(installPath InstallPath, name string, srcPath Path) PackagingSpec {
464 fullInstallPath := installPath.Join(m, name)
465 return m.packageFile(fullInstallPath, srcPath, false)
466}
467
468func (m *moduleContext) packageFile(fullInstallPath InstallPath, srcPath Path, executable bool) PackagingSpec {
469 licenseFiles := m.Module().EffectiveLicenseFiles()
470 spec := PackagingSpec{
471 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
472 srcPath: srcPath,
473 symlinkTarget: "",
474 executable: executable,
475 effectiveLicenseFiles: &licenseFiles,
476 partition: fullInstallPath.partition,
477 }
478 m.packagingSpecs = append(m.packagingSpecs, spec)
479 return spec
480}
481
Colin Cross09ad3a62023-11-15 12:29:33 -0800482func (m *moduleContext) installFile(installPath InstallPath, name string, srcPath Path, deps []InstallPath,
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800483 executable bool, hooks bool, extraZip *extraFilesZip) InstallPath {
Colin Cross69452e12023-11-15 11:20:53 -0800484
485 fullInstallPath := installPath.Join(m, name)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800486 if hooks {
487 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, false)
488 }
Colin Cross69452e12023-11-15 11:20:53 -0800489
490 if !m.skipInstall() {
Colin Cross09ad3a62023-11-15 12:29:33 -0800491 deps = append(deps, InstallPaths(m.module.base().installFilesDepSet.ToList())...)
Colin Crossd6fd0132023-11-06 13:54:06 -0800492 deps = append(deps, m.module.base().installedInitRcPaths...)
493 deps = append(deps, m.module.base().installedVintfFragmentsPaths...)
Colin Cross69452e12023-11-15 11:20:53 -0800494
495 var implicitDeps, orderOnlyDeps Paths
496
497 if m.Host() {
498 // Installed host modules might be used during the build, depend directly on their
499 // dependencies so their timestamp is updated whenever their dependency is updated
Colin Cross09ad3a62023-11-15 12:29:33 -0800500 implicitDeps = InstallPaths(deps).Paths()
Colin Cross69452e12023-11-15 11:20:53 -0800501 } else {
Colin Cross09ad3a62023-11-15 12:29:33 -0800502 orderOnlyDeps = InstallPaths(deps).Paths()
Colin Cross69452e12023-11-15 11:20:53 -0800503 }
504
505 if m.Config().KatiEnabled() {
506 // When creating the install rule in Soong but embedding in Make, write the rule to a
507 // makefile instead of directly to the ninja file so that main.mk can add the
508 // dependencies from the `required` property that are hard to resolve in Soong.
509 m.katiInstalls = append(m.katiInstalls, katiInstall{
510 from: srcPath,
511 to: fullInstallPath,
512 implicitDeps: implicitDeps,
513 orderOnlyDeps: orderOnlyDeps,
514 executable: executable,
515 extraFiles: extraZip,
516 })
517 } else {
518 rule := Cp
519 if executable {
520 rule = CpExecutable
521 }
522
523 extraCmds := ""
524 if extraZip != nil {
525 extraCmds += fmt.Sprintf(" && ( unzip -qDD -d '%s' '%s' 2>&1 | grep -v \"zipfile is empty\"; exit $${PIPESTATUS[0]} )",
526 extraZip.dir.String(), extraZip.zip.String())
527 extraCmds += " || ( code=$$?; if [ $$code -ne 0 -a $$code -ne 1 ]; then exit $$code; fi )"
528 implicitDeps = append(implicitDeps, extraZip.zip)
529 }
530
531 m.Build(pctx, BuildParams{
532 Rule: rule,
533 Description: "install " + fullInstallPath.Base(),
534 Output: fullInstallPath,
535 Input: srcPath,
536 Implicits: implicitDeps,
537 OrderOnly: orderOnlyDeps,
538 Default: !m.Config().KatiEnabled(),
539 Args: map[string]string{
540 "extraCmds": extraCmds,
541 },
542 })
543 }
544
545 m.installFiles = append(m.installFiles, fullInstallPath)
546 }
547
548 m.packageFile(fullInstallPath, srcPath, executable)
549
550 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
551
552 return fullInstallPath
553}
554
555func (m *moduleContext) InstallSymlink(installPath InstallPath, name string, srcPath InstallPath) InstallPath {
556 fullInstallPath := installPath.Join(m, name)
557 m.module.base().hooks.runInstallHooks(m, srcPath, fullInstallPath, true)
558
559 relPath, err := filepath.Rel(path.Dir(fullInstallPath.String()), srcPath.String())
560 if err != nil {
561 panic(fmt.Sprintf("Unable to generate symlink between %q and %q: %s", fullInstallPath.Base(), srcPath.Base(), err))
562 }
563 if !m.skipInstall() {
564
565 if m.Config().KatiEnabled() {
566 // When creating the symlink rule in Soong but embedding in Make, write the rule to a
567 // makefile instead of directly to the ninja file so that main.mk can add the
568 // dependencies from the `required` property that are hard to resolve in Soong.
569 m.katiSymlinks = append(m.katiSymlinks, katiInstall{
570 from: srcPath,
571 to: fullInstallPath,
572 })
573 } else {
574 // The symlink doesn't need updating when the target is modified, but we sometimes
575 // have a dependency on a symlink to a binary instead of to the binary directly, and
576 // the mtime of the symlink must be updated when the binary is modified, so use a
577 // normal dependency here instead of an order-only dependency.
578 m.Build(pctx, BuildParams{
579 Rule: Symlink,
580 Description: "install symlink " + fullInstallPath.Base(),
581 Output: fullInstallPath,
582 Input: srcPath,
583 Default: !m.Config().KatiEnabled(),
584 Args: map[string]string{
585 "fromPath": relPath,
586 },
587 })
588 }
589
590 m.installFiles = append(m.installFiles, fullInstallPath)
591 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
592 }
593
594 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
595 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
596 srcPath: nil,
597 symlinkTarget: relPath,
598 executable: false,
599 partition: fullInstallPath.partition,
600 })
601
602 return fullInstallPath
603}
604
605// installPath/name -> absPath where absPath might be a path that is available only at runtime
606// (e.g. /apex/...)
607func (m *moduleContext) InstallAbsoluteSymlink(installPath InstallPath, name string, absPath string) InstallPath {
608 fullInstallPath := installPath.Join(m, name)
609 m.module.base().hooks.runInstallHooks(m, nil, fullInstallPath, true)
610
611 if !m.skipInstall() {
612 if m.Config().KatiEnabled() {
613 // When creating the symlink rule in Soong but embedding in Make, write the rule to a
614 // makefile instead of directly to the ninja file so that main.mk can add the
615 // dependencies from the `required` property that are hard to resolve in Soong.
616 m.katiSymlinks = append(m.katiSymlinks, katiInstall{
617 absFrom: absPath,
618 to: fullInstallPath,
619 })
620 } else {
621 m.Build(pctx, BuildParams{
622 Rule: Symlink,
623 Description: "install symlink " + fullInstallPath.Base() + " -> " + absPath,
624 Output: fullInstallPath,
625 Default: !m.Config().KatiEnabled(),
626 Args: map[string]string{
627 "fromPath": absPath,
628 },
629 })
630 }
631
632 m.installFiles = append(m.installFiles, fullInstallPath)
633 }
634
635 m.packagingSpecs = append(m.packagingSpecs, PackagingSpec{
636 relPathInPackage: Rel(m, fullInstallPath.PartitionDir(), fullInstallPath.String()),
637 srcPath: nil,
638 symlinkTarget: absPath,
639 executable: false,
640 partition: fullInstallPath.partition,
641 })
642
643 return fullInstallPath
644}
645
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800646func (m *moduleContext) InstallTestData(installPath InstallPath, data []DataPath) InstallPaths {
647 m.testData = append(m.testData, data...)
648
649 ret := make(InstallPaths, 0, len(data))
650 for _, d := range data {
651 relPath := d.ToRelativeInstallPath()
652 installed := m.installFile(installPath, relPath, d.SrcPath, nil, false, false, nil)
653 ret = append(ret, installed)
654 }
655
656 return ret
657}
658
Colin Cross69452e12023-11-15 11:20:53 -0800659func (m *moduleContext) CheckbuildFile(srcPath Path) {
660 m.checkbuildFiles = append(m.checkbuildFiles, srcPath)
661}
662
663func (m *moduleContext) blueprintModuleContext() blueprint.ModuleContext {
664 return m.bp
665}
666
667func (m *moduleContext) LicenseMetadataFile() Path {
668 return m.module.base().licenseMetadataFile
669}
670
Colin Crossd6fd0132023-11-06 13:54:06 -0800671func (m *moduleContext) ModuleInfoJSON() *ModuleInfoJSON {
672 if moduleInfoJSON := m.module.base().moduleInfoJSON; moduleInfoJSON != nil {
673 return moduleInfoJSON
674 }
675 moduleInfoJSON := &ModuleInfoJSON{}
676 m.module.base().moduleInfoJSON = moduleInfoJSON
677 return moduleInfoJSON
678}
679
Colin Cross69452e12023-11-15 11:20:53 -0800680// Returns a list of paths expanded from globs and modules referenced using ":module" syntax. The property must
681// be tagged with `android:"path" to support automatic source module dependency resolution.
682//
683// Deprecated: use PathsForModuleSrc or PathsForModuleSrcExcludes instead.
684func (m *moduleContext) ExpandSources(srcFiles, excludes []string) Paths {
685 return PathsForModuleSrcExcludes(m, srcFiles, excludes)
686}
687
688// Returns a single path expanded from globs and modules referenced using ":module" syntax. The property must
689// be tagged with `android:"path" to support automatic source module dependency resolution.
690//
691// Deprecated: use PathForModuleSrc instead.
692func (m *moduleContext) ExpandSource(srcFile, _ string) Path {
693 return PathForModuleSrc(m, srcFile)
694}
695
696// Returns an optional single path expanded from globs and modules referenced using ":module" syntax if
697// the srcFile is non-nil. The property must be tagged with `android:"path" to support automatic source module
698// dependency resolution.
699func (m *moduleContext) ExpandOptionalSource(srcFile *string, _ string) OptionalPath {
700 if srcFile != nil {
701 return OptionalPathForPath(PathForModuleSrc(m, *srcFile))
702 }
703 return OptionalPath{}
704}
705
706func (m *moduleContext) RequiredModuleNames() []string {
707 return m.module.RequiredModuleNames()
708}
709
710func (m *moduleContext) HostRequiredModuleNames() []string {
711 return m.module.HostRequiredModuleNames()
712}
713
714func (m *moduleContext) TargetRequiredModuleNames() []string {
715 return m.module.TargetRequiredModuleNames()
716}