platform_build_soong/android/prebuilt_etc.go
Colin Cross 7228ecd5e3 Move cc.imageMutator into the android package
Prepare for making the image mutator available to all modules and
moving it between the os and arch mutators by moving it into the
android package and using an interface implemented by the module
types to control it.

Bug: 142286466
Test: No unexpected changes to out/soong/build.ninja
Change-Id: I0dcc9c7b5ec80edffade340c367f6ae4da34151b
2019-11-20 15:21:32 -08:00

257 lines
8.3 KiB
Go

// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package android
import "strconv"
// TODO(jungw): Now that it handles more than the ones in etc/, consider renaming this file.
func init() {
RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory)
RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory)
RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory)
RegisterModuleType("prebuilt_font", PrebuiltFontFactory)
RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory)
}
type prebuiltEtcProperties struct {
// Source file of this prebuilt.
Src *string `android:"path,arch_variant"`
// optional subdirectory under which this file is installed into
Sub_dir *string `android:"arch_variant"`
// optional name for the installed file. If unspecified, name of the module is used as the file name
Filename *string `android:"arch_variant"`
// when set to true, and filename property is not set, the name for the installed file
// is the same as the file name of the source file.
Filename_from_src *bool `android:"arch_variant"`
// Make this module available when building for recovery.
Recovery_available *bool
// Whether this module is directly installable to one of the partitions. Default: true.
Installable *bool
}
type PrebuiltEtcModule interface {
Module
SubDir() string
OutputFile() OutputPath
}
type PrebuiltEtc struct {
ModuleBase
properties prebuiltEtcProperties
sourceFilePath Path
outputFilePath OutputPath
// The base install location, e.g. "etc" for prebuilt_etc, "usr/share" for prebuilt_usr_share.
installDirBase string
// The base install location when soc_specific property is set to true, e.g. "firmware" for prebuilt_firmware.
socInstallDirBase string
installDirPath InstallPath
additionalDependencies *Paths
}
func (p *PrebuiltEtc) inRecovery() bool {
return p.ModuleBase.InRecovery() || p.ModuleBase.InstallInRecovery()
}
func (p *PrebuiltEtc) onlyInRecovery() bool {
return p.ModuleBase.InstallInRecovery()
}
func (p *PrebuiltEtc) InstallInRecovery() bool {
return p.inRecovery()
}
var _ ImageInterface = (*PrebuiltEtc)(nil)
func (p *PrebuiltEtc) ImageMutatorBegin(ctx BaseModuleContext) {}
func (p *PrebuiltEtc) CoreVariantNeeded(ctx BaseModuleContext) bool {
return !p.ModuleBase.InstallInRecovery()
}
func (p *PrebuiltEtc) RecoveryVariantNeeded(ctx BaseModuleContext) bool {
return Bool(p.properties.Recovery_available) || p.ModuleBase.InstallInRecovery()
}
func (p *PrebuiltEtc) ExtraImageVariations(ctx BaseModuleContext) []string {
return nil
}
func (p *PrebuiltEtc) SetImageVariation(ctx BaseModuleContext, variation string, module Module) {
}
func (p *PrebuiltEtc) DepsMutator(ctx BottomUpMutatorContext) {
if p.properties.Src == nil {
ctx.PropertyErrorf("src", "missing prebuilt source file")
}
}
func (p *PrebuiltEtc) SourceFilePath(ctx ModuleContext) Path {
return PathForModuleSrc(ctx, String(p.properties.Src))
}
func (p *PrebuiltEtc) InstallDirPath() InstallPath {
return p.installDirPath
}
// This allows other derivative modules (e.g. prebuilt_etc_xml) to perform
// additional steps (like validating the src) before the file is installed.
func (p *PrebuiltEtc) SetAdditionalDependencies(paths Paths) {
p.additionalDependencies = &paths
}
func (p *PrebuiltEtc) OutputFile() OutputPath {
return p.outputFilePath
}
func (p *PrebuiltEtc) SubDir() string {
return String(p.properties.Sub_dir)
}
func (p *PrebuiltEtc) Installable() bool {
return p.properties.Installable == nil || Bool(p.properties.Installable)
}
func (p *PrebuiltEtc) GenerateAndroidBuildActions(ctx ModuleContext) {
p.sourceFilePath = PathForModuleSrc(ctx, String(p.properties.Src))
filename := String(p.properties.Filename)
filename_from_src := Bool(p.properties.Filename_from_src)
if filename == "" {
if filename_from_src {
filename = p.sourceFilePath.Base()
} else {
filename = ctx.ModuleName()
}
} else if filename_from_src {
ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
return
}
p.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
// If soc install dir was specified and SOC specific is set, set the installDirPath to the specified
// socInstallDirBase.
installBaseDir := p.installDirBase
if ctx.SocSpecific() && p.socInstallDirBase != "" {
installBaseDir = p.socInstallDirBase
}
p.installDirPath = PathForModuleInstall(ctx, installBaseDir, String(p.properties.Sub_dir))
// This ensures that outputFilePath has the correct name for others to
// use, as the source file may have a different name.
ctx.Build(pctx, BuildParams{
Rule: Cp,
Output: p.outputFilePath,
Input: p.sourceFilePath,
})
}
func (p *PrebuiltEtc) AndroidMkEntries() AndroidMkEntries {
nameSuffix := ""
if p.inRecovery() && !p.onlyInRecovery() {
nameSuffix = ".recovery"
}
return AndroidMkEntries{
Class: "ETC",
SubName: nameSuffix,
OutputFile: OptionalPathForPath(p.outputFilePath),
ExtraEntries: []AndroidMkExtraEntriesFunc{
func(entries *AndroidMkEntries) {
entries.SetString("LOCAL_MODULE_TAGS", "optional")
entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.ToMakePath().String())
entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
entries.SetString("LOCAL_UNINSTALLABLE_MODULE", strconv.FormatBool(!p.Installable()))
if p.additionalDependencies != nil {
for _, path := range *p.additionalDependencies {
entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", path.String())
}
}
},
},
}
}
func InitPrebuiltEtcModule(p *PrebuiltEtc, dirBase string) {
p.installDirBase = dirBase
p.AddProperties(&p.properties)
}
// prebuilt_etc is for a prebuilt artifact that is installed in
// <partition>/etc/<sub_dir> directory.
func PrebuiltEtcFactory() Module {
module := &PrebuiltEtc{}
InitPrebuiltEtcModule(module, "etc")
// This module is device-only
InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
return module
}
// prebuilt_etc_host is for a host prebuilt artifact that is installed in
// $(HOST_OUT)/etc/<sub_dir> directory.
func PrebuiltEtcHostFactory() Module {
module := &PrebuiltEtc{}
InitPrebuiltEtcModule(module, "etc")
// This module is host-only
InitAndroidArchModule(module, HostSupported, MultilibCommon)
return module
}
// prebuilt_usr_share is for a prebuilt artifact that is installed in
// <partition>/usr/share/<sub_dir> directory.
func PrebuiltUserShareFactory() Module {
module := &PrebuiltEtc{}
InitPrebuiltEtcModule(module, "usr/share")
// This module is device-only
InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
return module
}
// prebuild_usr_share_host is for a host prebuilt artifact that is installed in
// $(HOST_OUT)/usr/share/<sub_dir> directory.
func PrebuiltUserShareHostFactory() Module {
module := &PrebuiltEtc{}
InitPrebuiltEtcModule(module, "usr/share")
// This module is host-only
InitAndroidArchModule(module, HostSupported, MultilibCommon)
return module
}
// prebuilt_font installs a font in <partition>/fonts directory.
func PrebuiltFontFactory() Module {
module := &PrebuiltEtc{}
InitPrebuiltEtcModule(module, "fonts")
// This module is device-only
InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
return module
}
// prebuilt_firmware installs a firmware file to <partition>/etc/firmware directory for system image.
// If soc_specific property is set to true, the firmware file is installed to the vendor <partition>/firmware
// directory for vendor image.
func PrebuiltFirmwareFactory() Module {
module := &PrebuiltEtc{}
module.socInstallDirBase = "firmware"
InitPrebuiltEtcModule(module, "etc/firmware")
// This module is device-only
InitAndroidArchModule(module, DeviceSupported, MultilibFirst)
return module
}