• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/tradefed/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			"compatibility-host-util",
84		}
85
86		// Add the files generated by the submodule created above to the resources.
87		props.Java_resources = []string{":" + genName}
88
89		ctx.AppendProperties(&props)
90
91	}
92}
93
94type TradefedBinaryGenProperties struct {
95	Name       *string
96	Short_name string
97	Full_name  string
98	Version    string
99}
100
101type tradefedBinaryGen struct {
102	android.ModuleBase
103
104	properties TradefedBinaryGenProperties
105
106	gen android.Paths
107}
108
109func tradefedBinaryGenFactory() android.Module {
110	tfg := &tradefedBinaryGen{}
111	tfg.AddProperties(&tfg.properties)
112	android.InitAndroidModule(tfg)
113	return tfg
114}
115
116func (tfg *tradefedBinaryGen) DepsMutator(android.BottomUpMutatorContext) {}
117
118var tradefedBinaryGenRule = pctx.StaticRule("tradefedBinaryGenRule", blueprint.RuleParams{
119	Command: `rm -f $out && touch $out && ` +
120		`echo "# This file is auto generated by Android.mk. Do not modify." >> $out && ` +
121		`echo "build_number = $$(cat ${buildNumberFile})" >> $out && ` +
122		`echo "target_arch = ${arch}" >> $out && ` +
123		`echo "name = ${name}" >> $out && ` +
124		`echo "fullname = ${fullname}" >> $out && ` +
125		`echo "version = ${version}" >> $out`,
126}, "buildNumberFile", "arch", "name", "fullname", "version")
127
128func (tfg *tradefedBinaryGen) GenerateAndroidBuildActions(ctx android.ModuleContext) {
129	buildNumberFile := ctx.Config().BuildNumberFile(ctx)
130	outputFile := android.PathForModuleOut(ctx, "test-suite-info.properties")
131	ctx.Build(pctx, android.BuildParams{
132		Rule:      tradefedBinaryGenRule,
133		Output:    outputFile,
134		OrderOnly: android.Paths{buildNumberFile},
135		Args: map[string]string{
136			"buildNumberFile": buildNumberFile.String(),
137			"arch":            ctx.Config().DevicePrimaryArchType().String(),
138			"name":            tfg.properties.Short_name,
139			"fullname":        tfg.properties.Full_name,
140			"version":         tfg.properties.Version,
141		},
142	})
143
144	tfg.gen = append(tfg.gen, outputFile)
145
146	dynamicConfig := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "DynamicConfig.xml")
147	if dynamicConfig.Valid() {
148		outputFile := android.PathForModuleOut(ctx, strings.TrimSuffix(ctx.ModuleName(), genSuffix)+".dynamic")
149		ctx.Build(pctx, android.BuildParams{
150			Rule:   android.Cp,
151			Input:  dynamicConfig.Path(),
152			Output: outputFile,
153		})
154
155		tfg.gen = append(tfg.gen, outputFile)
156	}
157}
158
159func (tfg *tradefedBinaryGen) Srcs() android.Paths {
160	return append(android.Paths(nil), tfg.gen...)
161}
162
163var _ android.SourceFileProducer = (*tradefedBinaryGen)(nil)
164