platform_build_soong/phony/phony.go
Nelson Li 1f96994e53 Support pyhon_rule_defaults in Soong
This change adds support for `python_rule_defaults` in Soong, allowing
different targets to reuse the same dependencies. This resolves the
issue where different dependencies could not be synchronized due to the
lack of `python_rule_defaults` in Soong.

Bug: 331886818
Test: 1. Add phony_rule_defaults module in the Android.bp
      2. Add the module in `defaults`
Change-Id: If3579ea685529858567748e6f34bcf7c5477bcfc
2024-04-01 11:40:21 +08:00

166 lines
4.8 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 phony
import (
"fmt"
"io"
"strings"
"android/soong/android"
)
func init() {
registerPhonyModuleTypes(android.InitRegistrationContext)
}
func registerPhonyModuleTypes(ctx android.RegistrationContext) {
ctx.RegisterModuleType("phony", PhonyFactory)
ctx.RegisterModuleType("phony_rule", PhonyRuleFactory)
ctx.RegisterModuleType("phony_rule_defaults", PhonyRuleDefaultsFactory)
}
var PrepareForTestWithPhony = android.FixtureRegisterWithContext(registerPhonyModuleTypes)
type phony struct {
android.ModuleBase
requiredModuleNames []string
hostRequiredModuleNames []string
targetRequiredModuleNames []string
}
func PhonyFactory() android.Module {
module := &phony{}
android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
return module
}
func (p *phony) GenerateAndroidBuildActions(ctx android.ModuleContext) {
p.requiredModuleNames = ctx.RequiredModuleNames()
p.hostRequiredModuleNames = ctx.HostRequiredModuleNames()
p.targetRequiredModuleNames = ctx.TargetRequiredModuleNames()
}
func (p *phony) AndroidMk() android.AndroidMkData {
return android.AndroidMkData{
Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # phony.phony")
fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
fmt.Fprintln(w, "LOCAL_MODULE :=", name)
if p.Host() {
fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
}
if len(p.requiredModuleNames) > 0 {
fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=",
strings.Join(p.requiredModuleNames, " "))
}
if len(p.hostRequiredModuleNames) > 0 {
fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES :=",
strings.Join(p.hostRequiredModuleNames, " "))
}
if len(p.targetRequiredModuleNames) > 0 {
fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES :=",
strings.Join(p.targetRequiredModuleNames, " "))
}
// AconfigUpdateAndroidMkData may have added elements to Extra. Process them here.
for _, extra := range data.Extra {
extra(w, nil)
}
fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
},
}
}
type PhonyRule struct {
android.ModuleBase
android.DefaultableModuleBase
properties PhonyProperties
}
type PhonyProperties struct {
// The Phony_deps is the set of all dependencies for this target,
// and it can function similarly to .PHONY in a makefile.
// Additionally, dependencies within it can even include genrule.
Phony_deps []string
}
// The phony_rule provides functionality similar to the .PHONY in a makefile.
// It can create a phony target and include relevant dependencies associated with it.
func PhonyRuleFactory() android.Module {
module := &PhonyRule{}
android.InitAndroidModule(module)
module.AddProperties(&module.properties)
android.InitDefaultableModule(module)
return module
}
func (p *PhonyRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
}
func (p *PhonyRule) AndroidMk() android.AndroidMkData {
return android.AndroidMkData{
Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
if len(p.properties.Phony_deps) > 0 {
depModulesStr := strings.Join(p.properties.Phony_deps, " ")
fmt.Fprintln(w, ".PHONY:", name)
fmt.Fprintln(w, name, ":", depModulesStr)
}
},
}
}
// PhonyRuleDefaults
type PhonyRuleDefaults struct {
android.ModuleBase
android.DefaultsModuleBase
}
// phony_rule_defaults provides a set of properties that can be inherited by other phony_rules.
//
// A module can use the properties from a phony_rule_defaults module using `defaults: ["defaults_module_name"]`. Each
// property in the defaults module that exists in the depending module will be prepended to the depending module's
// value for that property.
//
// Example:
//
// phony_rule_defaults {
// name: "add_module1_defaults",
// phony_deps: [
// "module1",
// ],
// }
//
// phony_rule {
// name: "example",
// defaults: ["add_module1_defaults"],
// }
//
// is functionally identical to:
//
// phony_rule {
// name: "example",
// phony_deps: [
// "module1",
// ],
// }
func PhonyRuleDefaultsFactory() android.Module {
module := &PhonyRuleDefaults{}
module.AddProperties(&PhonyProperties{})
android.InitDefaultsModule(module)
return module
}