1// 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
31	pctx.Import("android/soong/android")
32}
33
34type TradefedBinaryProperties struct {
35	Short_name string
36	Full_name  string
37	Version    string
38	Prepend_platform_version_name bool
39}
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
58		version := tfb.Version
59		if (tfb.Prepend_platform_version_name) {
60			version = ctx.Config().PlatformVersionName() + tfb.Version
61		}
62
63		// Create a submodule that generates the test-suite-info.properties file
64		// and copies DynamicConfig.xml if it is present.
65		ctx.CreateModule(tradefedBinaryGenFactory,
66			&TradefedBinaryGenProperties{
67				Name:       &genName,
68				Short_name: tfb.Short_name,
69				Full_name:  tfb.Full_name,
70				Version:    version,
71			})
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",
81			"tradefed-test-framework",
82			"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 && ` +
122		`echo "build_number = $$(cat ${buildNumberFile})" >> $out && ` +
123		`echo "target_arch = ${arch}" >> $out && ` +
124		`echo "name = ${name}" >> $out && ` +
125		`echo "fullname = ${fullname}" >> $out && ` +
126		`echo "version = ${version}" >> $out`,
127}, "buildNumberFile", "arch", "name", "fullname", "version")
128
129func (tfg *tradefedBinaryGen) GenerateAndroidBuildActions(ctx android.ModuleContext) {
130	buildNumberFile := ctx.Config().BuildNumberFile(ctx)
131	outputFile := android.PathForModuleOut(ctx, "test-suite-info.properties")
132	ctx.Build(pctx, android.BuildParams{
133		Rule:      tradefedBinaryGenRule,
134		Output:    outputFile,
135		OrderOnly: android.Paths{buildNumberFile},
136		Args: map[string]string{
137			"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,
142		},
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)
165