blob: 85984588b198cbb22f1db9d91da6232b309d15ca [file] [log] [blame]
Colin Cross91b29792018-08-16 18:16:12 +00001// Copyright 2018 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 suite_harness
16
17import (
18 "strings"
19
20 "github.com/google/blueprint"
21
22 "android/soong/android"
23 "android/soong/java"
24)
25
26var pctx = android.NewPackageContext("android/soong/suite_harness")
27
28func init() {
29 android.RegisterModuleType("tradefed_binary_host", tradefedBinaryFactory)
30
Colin Crossc091aa42019-04-02 16:23:14 -070031 pctx.Import("android/soong/android")
Colin Cross91b29792018-08-16 18:16:12 +000032}
33
34type TradefedBinaryProperties struct {
35 Short_name string
36 Full_name string
37 Version string
Christopher Dombroski0f08b042020-05-19 12:02:08 -070038 Prepend_platform_version_name bool
Colin Cross91b29792018-08-16 18:16:12 +000039}
40
41// tradefedBinaryFactory creates an empty module for the tradefed_binary module type,
42// which is a java_binary with some additional processing in tradefedBinaryLoadHook.
43func tradefedBinaryFactory() android.Module {
44 props := &TradefedBinaryProperties{}
45 module := java.BinaryHostFactory()
46 module.AddProperties(props)
47 android.AddLoadHook(module, tradefedBinaryLoadHook(props))
48
49 return module
50}
51
52const genSuffix = "-gen"
53
54// tradefedBinaryLoadHook adds extra resources and libraries to tradefed_binary modules.
55func tradefedBinaryLoadHook(tfb *TradefedBinaryProperties) func(ctx android.LoadHookContext) {
56 return func(ctx android.LoadHookContext) {
57 genName := ctx.ModuleName() + genSuffix
Christopher Dombroski0f08b042020-05-19 12:02:08 -070058 version := tfb.Version
59 if (tfb.Prepend_platform_version_name) {
60 version = ctx.Config().PlatformVersionName() + tfb.Version
61 }
Colin Cross91b29792018-08-16 18:16:12 +000062
63 // Create a submodule that generates the test-suite-info.properties file
64 // and copies DynamicConfig.xml if it is present.
Colin Cross9a5d3832019-09-25 15:12:58 -070065 ctx.CreateModule(tradefedBinaryGenFactory,
Colin Cross91b29792018-08-16 18:16:12 +000066 &TradefedBinaryGenProperties{
67 Name: &genName,
68 Short_name: tfb.Short_name,
69 Full_name: tfb.Full_name,
Christopher Dombroski0f08b042020-05-19 12:02:08 -070070 Version: version,
Colin Cross91b29792018-08-16 18:16:12 +000071 })
72
73 props := struct {
74 Java_resources []string
75 Libs []string
76 }{}
77
78 // Add dependencies required by all tradefed_binary modules.
79 props.Libs = []string{
80 "tradefed",
Julien Despreze0ec3872020-05-18 13:31:01 -070081 "tradefed-test-framework",
Colin Cross91b29792018-08-16 18:16:12 +000082 "loganalysis",
83 "hosttestlib",
84 "compatibility-host-util",
85 }
86
87 // Add the files generated by the submodule created above to the resources.
88 props.Java_resources = []string{":" + genName}
89
90 ctx.AppendProperties(&props)
91
92 }
93}
94
95type TradefedBinaryGenProperties struct {
96 Name *string
97 Short_name string
98 Full_name string
99 Version string
100}
101
102type tradefedBinaryGen struct {
103 android.ModuleBase
104
105 properties TradefedBinaryGenProperties
106
107 gen android.Paths
108}
109
110func tradefedBinaryGenFactory() android.Module {
111 tfg := &tradefedBinaryGen{}
112 tfg.AddProperties(&tfg.properties)
113 android.InitAndroidModule(tfg)
114 return tfg
115}
116
117func (tfg *tradefedBinaryGen) DepsMutator(android.BottomUpMutatorContext) {}
118
119var tradefedBinaryGenRule = pctx.StaticRule("tradefedBinaryGenRule", blueprint.RuleParams{
120 Command: `rm -f $out && touch $out && ` +
121 `echo "# This file is auto generated by Android.mk. Do not modify." >> $out && ` +
Colin Crossdae4c152020-02-23 11:01:02 -0800122 `echo "build_number = $$(cat ${buildNumberFile})" >> $out && ` +
Colin Cross91b29792018-08-16 18:16:12 +0000123 `echo "target_arch = ${arch}" >> $out && ` +
124 `echo "name = ${name}" >> $out && ` +
125 `echo "fullname = ${fullname}" >> $out && ` +
126 `echo "version = ${version}" >> $out`,
Colin Crossdae4c152020-02-23 11:01:02 -0800127}, "buildNumberFile", "arch", "name", "fullname", "version")
Colin Cross91b29792018-08-16 18:16:12 +0000128
129func (tfg *tradefedBinaryGen) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Crossdae4c152020-02-23 11:01:02 -0800130 buildNumberFile := ctx.Config().BuildNumberFile(ctx)
Colin Cross91b29792018-08-16 18:16:12 +0000131 outputFile := android.PathForModuleOut(ctx, "test-suite-info.properties")
132 ctx.Build(pctx, android.BuildParams{
Colin Crossdae4c152020-02-23 11:01:02 -0800133 Rule: tradefedBinaryGenRule,
134 Output: outputFile,
135 OrderOnly: android.Paths{buildNumberFile},
Colin Cross91b29792018-08-16 18:16:12 +0000136 Args: map[string]string{
Colin Crossdae4c152020-02-23 11:01:02 -0800137 "buildNumberFile": buildNumberFile.String(),
138 "arch": ctx.Config().DevicePrimaryArchType().String(),
139 "name": tfg.properties.Short_name,
140 "fullname": tfg.properties.Full_name,
141 "version": tfg.properties.Version,
Colin Cross91b29792018-08-16 18:16:12 +0000142 },
143 })
144
145 tfg.gen = append(tfg.gen, outputFile)
146
147 dynamicConfig := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "DynamicConfig.xml")
148 if dynamicConfig.Valid() {
149 outputFile := android.PathForModuleOut(ctx, strings.TrimSuffix(ctx.ModuleName(), genSuffix)+".dynamic")
150 ctx.Build(pctx, android.BuildParams{
151 Rule: android.Cp,
152 Input: dynamicConfig.Path(),
153 Output: outputFile,
154 })
155
156 tfg.gen = append(tfg.gen, outputFile)
157 }
158}
159
160func (tfg *tradefedBinaryGen) Srcs() android.Paths {
161 return append(android.Paths(nil), tfg.gen...)
162}
163
164var _ android.SourceFileProducer = (*tradefedBinaryGen)(nil)