blob: bb4878899bb8b20f9d3a170b2bde68b19ffd75db [file] [log] [blame]
Nan Zhang6d34b302017-02-04 17:47:46 -08001// Copyright 2016 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 phony
16
17import (
18 "fmt"
19 "io"
20 "strings"
21
Nan Zhang6d34b302017-02-04 17:47:46 -080022 "android/soong/android"
23)
24
25func init() {
Steven Moreland00e1e612018-07-11 15:24:31 -070026 android.RegisterModuleType("phony", PhonyFactory)
Nelson Lif3c70682023-12-20 02:37:52 +000027 android.RegisterModuleType("phony_rule", PhonyRuleFactory)
Nan Zhang6d34b302017-02-04 17:47:46 -080028}
29
30type phony struct {
31 android.ModuleBase
Sasha Smundakb6d23052019-04-01 18:37:36 -070032 requiredModuleNames []string
33 hostRequiredModuleNames []string
34 targetRequiredModuleNames []string
Nan Zhang6d34b302017-02-04 17:47:46 -080035}
36
Steven Moreland00e1e612018-07-11 15:24:31 -070037func PhonyFactory() android.Module {
Nan Zhang6d34b302017-02-04 17:47:46 -080038 module := &phony{}
39
Dan Willemsen60294ef2019-04-02 17:04:18 -070040 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibCommon)
Colin Cross36242852017-06-23 15:06:31 -070041 return module
Nan Zhang6d34b302017-02-04 17:47:46 -080042}
43
Nan Zhang6d34b302017-02-04 17:47:46 -080044func (p *phony) GenerateAndroidBuildActions(ctx android.ModuleContext) {
45 p.requiredModuleNames = ctx.RequiredModuleNames()
Sasha Smundakb6d23052019-04-01 18:37:36 -070046 p.hostRequiredModuleNames = ctx.HostRequiredModuleNames()
47 p.targetRequiredModuleNames = ctx.TargetRequiredModuleNames()
Nan Zhang6d34b302017-02-04 17:47:46 -080048}
49
Colin Crossa18e9cf2017-08-10 17:00:19 -070050func (p *phony) AndroidMk() android.AndroidMkData {
51 return android.AndroidMkData{
Colin Cross0f86d182017-08-10 17:07:28 -070052 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
Sasha Smundak5c4729d2022-12-01 10:49:23 -080053 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # phony.phony")
Colin Crossa18e9cf2017-08-10 17:00:19 -070054 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
55 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
Dan Willemsen60294ef2019-04-02 17:04:18 -070056 if p.Host() {
57 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
58 }
Sasha Smundakb6d23052019-04-01 18:37:36 -070059 if len(p.requiredModuleNames) > 0 {
60 fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES :=",
61 strings.Join(p.requiredModuleNames, " "))
62 }
63 if len(p.hostRequiredModuleNames) > 0 {
64 fmt.Fprintln(w, "LOCAL_HOST_REQUIRED_MODULES :=",
65 strings.Join(p.hostRequiredModuleNames, " "))
66 }
67 if len(p.targetRequiredModuleNames) > 0 {
68 fmt.Fprintln(w, "LOCAL_TARGET_REQUIRED_MODULES :=",
69 strings.Join(p.targetRequiredModuleNames, " "))
70 }
Colin Crossa18e9cf2017-08-10 17:00:19 -070071 fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
72 },
Nan Zhang6d34b302017-02-04 17:47:46 -080073 }
Nan Zhang6d34b302017-02-04 17:47:46 -080074}
Nelson Lif3c70682023-12-20 02:37:52 +000075
76type PhonyRule struct {
77 android.ModuleBase
78
79 properties PhonyProperties
80}
81
82type PhonyProperties struct {
83 // The Phony_deps is the set of all dependencies for this target,
84 // and it can function similarly to .PHONY in a makefile.
85 // Additionally, dependencies within it can even include genrule.
86 Phony_deps []string
87}
88
89// The phony_rule provides functionality similar to the .PHONY in a makefile.
90// It can create a phony target and include relevant dependencies associated with it.
91func PhonyRuleFactory() android.Module {
92 module := &PhonyRule{}
93 android.InitAndroidModule(module)
94 module.AddProperties(&module.properties)
95 return module
96}
97
98func (p *PhonyRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
99}
100
101func (p *PhonyRule) AndroidMk() android.AndroidMkData {
102 return android.AndroidMkData{
103 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
104 if len(p.properties.Phony_deps) > 0 {
105 depModulesStr := strings.Join(p.properties.Phony_deps, " ")
106 fmt.Fprintln(w, ".PHONY:", name)
107 fmt.Fprintln(w, name, ":", depModulesStr)
108 }
109 },
110 }
111}