lin
2025-02-25 a02983e50ab34c3e7366b27cdeca427a327faebd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2018 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 suite_harness
 
import (
   "strings"
 
   "github.com/google/blueprint"
 
   "android/soong/android"
   "android/soong/java"
)
 
var pctx = android.NewPackageContext("android/soong/suite_harness")
 
func init() {
   android.RegisterModuleType("tradefed_binary_host", tradefedBinaryFactory)
 
   pctx.Import("android/soong/android")
}
 
type TradefedBinaryProperties struct {
   Short_name string
   Full_name  string
   Version    string
}
 
// tradefedBinaryFactory creates an empty module for the tradefed_binary module type,
// which is a java_binary with some additional processing in tradefedBinaryLoadHook.
func tradefedBinaryFactory() android.Module {
   props := &TradefedBinaryProperties{}
   module := java.BinaryHostFactory()
   module.AddProperties(props)
   android.AddLoadHook(module, tradefedBinaryLoadHook(props))
 
   return module
}
 
const genSuffix = "-gen"
 
// tradefedBinaryLoadHook adds extra resources and libraries to tradefed_binary modules.
func tradefedBinaryLoadHook(tfb *TradefedBinaryProperties) func(ctx android.LoadHookContext) {
   return func(ctx android.LoadHookContext) {
       genName := ctx.ModuleName() + genSuffix
 
       // Create a submodule that generates the test-suite-info.properties file
       // and copies DynamicConfig.xml if it is present.
       ctx.CreateModule(android.ModuleFactoryAdaptor(tradefedBinaryGenFactory),
           &TradefedBinaryGenProperties{
               Name:       &genName,
               Short_name: tfb.Short_name,
               Full_name:  tfb.Full_name,
               Version:    tfb.Version,
           })
 
       props := struct {
           Java_resources []string
           Libs           []string
       }{}
 
       // Add dependencies required by all tradefed_binary modules.
       props.Libs = []string{
           "tradefed",
           "loganalysis",
           "hosttestlib",
           "compatibility-host-util",
       }
 
       // Add the files generated by the submodule created above to the resources.
       props.Java_resources = []string{":" + genName}
 
       ctx.AppendProperties(&props)
 
   }
}
 
type TradefedBinaryGenProperties struct {
   Name       *string
   Short_name string
   Full_name  string
   Version    string
}
 
type tradefedBinaryGen struct {
   android.ModuleBase
 
   properties TradefedBinaryGenProperties
 
   gen android.Paths
}
 
func tradefedBinaryGenFactory() android.Module {
   tfg := &tradefedBinaryGen{}
   tfg.AddProperties(&tfg.properties)
   android.InitAndroidModule(tfg)
   return tfg
}
 
func (tfg *tradefedBinaryGen) DepsMutator(android.BottomUpMutatorContext) {}
 
var tradefedBinaryGenRule = pctx.StaticRule("tradefedBinaryGenRule", blueprint.RuleParams{
   Command: `rm -f $out && touch $out && ` +
       `echo "# This file is auto generated by Android.mk. Do not modify." >> $out && ` +
       `echo "build_number = ${buildNumber}" >> $out && ` +
       `echo "target_arch = ${arch}" >> $out && ` +
       `echo "name = ${name}" >> $out && ` +
       `echo "fullname = ${fullname}" >> $out && ` +
       `echo "version = ${version}" >> $out`,
}, "buildNumber", "arch", "name", "fullname", "version")
 
func (tfg *tradefedBinaryGen) GenerateAndroidBuildActions(ctx android.ModuleContext) {
   outputFile := android.PathForModuleOut(ctx, "test-suite-info.properties")
   ctx.Build(pctx, android.BuildParams{
       Rule:   tradefedBinaryGenRule,
       Output: outputFile,
       Args: map[string]string{
           "buildNumber": ctx.Config().BuildNumberFromFile(),
           "arch":        ctx.Config().DevicePrimaryArchType().String(),
           "name":        tfg.properties.Short_name,
           "fullname":    tfg.properties.Full_name,
           "version":     tfg.properties.Version,
       },
   })
 
   tfg.gen = append(tfg.gen, outputFile)
 
   dynamicConfig := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "DynamicConfig.xml")
   if dynamicConfig.Valid() {
       outputFile := android.PathForModuleOut(ctx, strings.TrimSuffix(ctx.ModuleName(), genSuffix)+".dynamic")
       ctx.Build(pctx, android.BuildParams{
           Rule:   android.Cp,
           Input:  dynamicConfig.Path(),
           Output: outputFile,
       })
 
       tfg.gen = append(tfg.gen, outputFile)
   }
}
 
func (tfg *tradefedBinaryGen) Srcs() android.Paths {
   return append(android.Paths(nil), tfg.gen...)
}
 
var _ android.SourceFileProducer = (*tradefedBinaryGen)(nil)