blob: 236e559b5807921847db79ba2f465d5d8c9e76a4 [file] [log] [blame]
Colin Cross303e21f2018-08-07 16:49:25 -07001// 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 tradefed
16
17import (
Julien Desprezeb7398e2019-02-28 08:45:28 -080018 "fmt"
Julien Desprezeb7398e2019-02-28 08:45:28 -080019 "strings"
20
Colin Cross303e21f2018-08-07 16:49:25 -070021 "github.com/google/blueprint"
Julien Desprezeb7398e2019-02-28 08:45:28 -080022 "github.com/google/blueprint/proptools"
Colin Cross303e21f2018-08-07 16:49:25 -070023
24 "android/soong/android"
25)
26
Dan Shi20ccd212019-08-27 10:37:24 -070027const test_xml_indent = " "
28
Jack He33338892018-09-19 02:21:28 -070029func getTestConfigTemplate(ctx android.ModuleContext, prop *string) android.OptionalPath {
30 return ctx.ExpandOptionalSource(prop, "test_config_template")
31}
32
Colin Cross303e21f2018-08-07 16:49:25 -070033func getTestConfig(ctx android.ModuleContext, prop *string) android.Path {
34 if p := ctx.ExpandOptionalSource(prop, "test_config"); p.Valid() {
35 return p.Path()
36 } else if p := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "AndroidTest.xml"); p.Valid() {
37 return p.Path()
38 }
39 return nil
40}
41
42var autogenTestConfig = pctx.StaticRule("autogenTestConfig", blueprint.RuleParams{
yangbill5ec45552020-08-13 16:16:56 +080043 Command: "sed 's&{MODULE}&${name}&g;s&{EXTRA_CONFIGS}&'${extraConfigs}'&g;s&{OUTPUT_FILENAME}&'${outputFileName}'&g;s&{TEST_INSTALL_BASE}&'${testInstallBase}'&g' $template > $out",
Colin Cross303e21f2018-08-07 16:49:25 -070044 CommandDeps: []string{"$template"},
yangbill5ec45552020-08-13 16:16:56 +080045}, "name", "template", "extraConfigs", "outputFileName", "testInstallBase")
Colin Cross303e21f2018-08-07 16:49:25 -070046
Lorenzo Colittie29c21e2020-02-14 18:27:56 +090047func testConfigPath(ctx android.ModuleContext, prop *string, testSuites []string, autoGenConfig *bool, testConfigTemplateProp *string) (path android.Path, autogenPath android.WritablePath) {
Dan Shi6ffaaa82019-09-26 11:41:36 -070048 p := getTestConfig(ctx, prop)
49 if !Bool(autoGenConfig) && p != nil {
Colin Cross303e21f2018-08-07 16:49:25 -070050 return p, nil
Lorenzo Colittie29c21e2020-02-14 18:27:56 +090051 } else if BoolDefault(autoGenConfig, true) && (!android.InList("cts", testSuites) || testConfigTemplateProp != nil) {
Colin Cross303e21f2018-08-07 16:49:25 -070052 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".config")
Jack He33338892018-09-19 02:21:28 -070053 return nil, outputFile
Colin Cross303e21f2018-08-07 16:49:25 -070054 } else {
55 // CTS modules can be used for test data, so test config files must be
Lorenzo Colittie29c21e2020-02-14 18:27:56 +090056 // explicitly created using AndroidTest.xml or test_config_template.
Colin Cross303e21f2018-08-07 16:49:25 -070057 return nil, nil
58 }
59}
60
Dan Shi37ee3b82019-06-06 16:23:32 -070061type Config interface {
62 Config() string
63}
64
65type Option struct {
66 Name string
easoncylee1e3fdcd2020-04-30 10:08:33 +080067 Key string
Dan Shi37ee3b82019-06-06 16:23:32 -070068 Value string
69}
70
71var _ Config = Option{}
72
73func (o Option) Config() string {
easoncylee1e3fdcd2020-04-30 10:08:33 +080074 if o.Key != "" {
75 return fmt.Sprintf(`<option name="%s" key="%s" value="%s" />`, o.Name, o.Key, o.Value)
76 }
Dan Shi37ee3b82019-06-06 16:23:32 -070077 return fmt.Sprintf(`<option name="%s" value="%s" />`, o.Name, o.Value)
78}
79
nelsonli0d7111e2019-09-17 16:35:23 +080080// It can be a template of object or target_preparer.
81type Object struct {
82 // Set it as a target_preparer if object type == "target_preparer".
83 Type string
Dan Shi20ccd212019-08-27 10:37:24 -070084 Class string
85 Options []Option
Dan Shi37ee3b82019-06-06 16:23:32 -070086}
87
nelsonli0d7111e2019-09-17 16:35:23 +080088var _ Config = Object{}
Dan Shi37ee3b82019-06-06 16:23:32 -070089
nelsonli0d7111e2019-09-17 16:35:23 +080090func (ob Object) Config() string {
Dan Shi20ccd212019-08-27 10:37:24 -070091 var optionStrings []string
nelsonli0d7111e2019-09-17 16:35:23 +080092 for _, option := range ob.Options {
Dan Shi20ccd212019-08-27 10:37:24 -070093 optionStrings = append(optionStrings, option.Config())
94 }
95 var options string
nelsonli0d7111e2019-09-17 16:35:23 +080096 if len(ob.Options) == 0 {
Dan Shi20ccd212019-08-27 10:37:24 -070097 options = ""
98 } else {
99 optionDelimiter := fmt.Sprintf("\\n%s%s", test_xml_indent, test_xml_indent)
100 options = optionDelimiter + strings.Join(optionStrings, optionDelimiter)
101 }
nelsonli0d7111e2019-09-17 16:35:23 +0800102 if ob.Type == "target_preparer" {
103 return fmt.Sprintf(`<target_preparer class="%s">%s\n%s</target_preparer>`, ob.Class, options, test_xml_indent)
104 } else {
105 return fmt.Sprintf(`<object type="%s" class="%s">%s\n%s</object>`, ob.Type, ob.Class, options, test_xml_indent)
106 }
107
Dan Shi37ee3b82019-06-06 16:23:32 -0700108}
109
Cole Faust8ec823c2022-12-07 18:18:37 -0800110// MaybeAutoGenTestConfigBuilder provides a Build() method that will either
111// generate a AndroidTest.xml file, or use an existing user-supplied one.
112// It used to be a bunch of separate functions for each language, but was
113// converted to this builder pattern to have one function that accepts many
114// optional arguments.
115type MaybeAutoGenTestConfigBuilder struct {
116 ctx android.ModuleContext
117 name string
118 outputFileName string
119 testConfigProp *string
120 testConfigTemplateProp *string
121 testSuites []string
122 config []Config
123 configsForAutogenerated []Config
124 autoGenConfig *bool
125 unitTest *bool
126 testInstallBase string
127 deviceTemplate string
128 hostTemplate string
129 hostUnitTestTemplate string
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700130}
131
Cole Faust8ec823c2022-12-07 18:18:37 -0800132func NewMaybeAutoGenTestConfigBuilder(ctx android.ModuleContext) *MaybeAutoGenTestConfigBuilder {
133 return &MaybeAutoGenTestConfigBuilder{
134 ctx: ctx,
135 name: ctx.ModuleName(),
136 }
frankfengc5b87492020-06-03 10:28:47 -0700137}
138
Cole Faust8ec823c2022-12-07 18:18:37 -0800139func (b *MaybeAutoGenTestConfigBuilder) SetName(name string) *MaybeAutoGenTestConfigBuilder {
140 b.name = name
141 return b
142}
143
144func (b *MaybeAutoGenTestConfigBuilder) SetOutputFileName(outputFileName string) *MaybeAutoGenTestConfigBuilder {
145 b.outputFileName = outputFileName
146 return b
147}
148
149func (b *MaybeAutoGenTestConfigBuilder) SetTestConfigProp(testConfigProp *string) *MaybeAutoGenTestConfigBuilder {
150 b.testConfigProp = testConfigProp
151 return b
152}
153
154func (b *MaybeAutoGenTestConfigBuilder) SetTestTemplateConfigProp(testConfigTemplateProp *string) *MaybeAutoGenTestConfigBuilder {
155 b.testConfigTemplateProp = testConfigTemplateProp
156 return b
157}
158
159func (b *MaybeAutoGenTestConfigBuilder) SetTestSuites(testSuites []string) *MaybeAutoGenTestConfigBuilder {
160 b.testSuites = testSuites
161 return b
162}
163
164func (b *MaybeAutoGenTestConfigBuilder) SetConfig(config []Config) *MaybeAutoGenTestConfigBuilder {
165 b.config = config
166 return b
167}
168
169func (b *MaybeAutoGenTestConfigBuilder) SetOptionsForAutogenerated(configsForAutogenerated []Option) *MaybeAutoGenTestConfigBuilder {
170 configs := make([]Config, 0, len(configsForAutogenerated))
171 for _, c := range configsForAutogenerated {
172 configs = append(configs, c)
173 }
174 b.configsForAutogenerated = configs
175 return b
176}
177
178func (b *MaybeAutoGenTestConfigBuilder) SetUnitTest(unitTest *bool) *MaybeAutoGenTestConfigBuilder {
179 b.unitTest = unitTest
180 return b
181}
182
183func (b *MaybeAutoGenTestConfigBuilder) SetAutoGenConfig(autoGenConfig *bool) *MaybeAutoGenTestConfigBuilder {
184 b.autoGenConfig = autoGenConfig
185 return b
186}
187
188func (b *MaybeAutoGenTestConfigBuilder) SetTestInstallBase(testInstallBase string) *MaybeAutoGenTestConfigBuilder {
189 b.testInstallBase = testInstallBase
190 return b
191}
192
193func (b *MaybeAutoGenTestConfigBuilder) SetDeviceTemplate(deviceTemplate string) *MaybeAutoGenTestConfigBuilder {
194 b.deviceTemplate = deviceTemplate
195 return b
196}
197
198func (b *MaybeAutoGenTestConfigBuilder) SetHostTemplate(hostTemplate string) *MaybeAutoGenTestConfigBuilder {
199 b.hostTemplate = hostTemplate
200 return b
201}
202
203func (b *MaybeAutoGenTestConfigBuilder) SetHostUnitTestTemplate(hostUnitTestTemplate string) *MaybeAutoGenTestConfigBuilder {
204 b.hostUnitTestTemplate = hostUnitTestTemplate
205 return b
206}
207
208func (b *MaybeAutoGenTestConfigBuilder) Build() android.Path {
209 config := append(b.config, b.configsForAutogenerated...)
210 path, autogenPath := testConfigPath(b.ctx, b.testConfigProp, b.testSuites, b.autoGenConfig, b.testConfigTemplateProp)
211 if autogenPath != nil {
212 templatePath := getTestConfigTemplate(b.ctx, b.testConfigTemplateProp)
213 if templatePath.Valid() {
214 autogenTemplate(b.ctx, b.name, autogenPath, templatePath.String(), config, b.outputFileName, b.testInstallBase)
215 } else {
216 if b.ctx.Device() {
217 autogenTemplate(b.ctx, b.name, autogenPath, b.deviceTemplate, config, b.outputFileName, b.testInstallBase)
218 } else {
219 if Bool(b.unitTest) {
220 autogenTemplate(b.ctx, b.name, autogenPath, b.hostUnitTestTemplate, config, b.outputFileName, b.testInstallBase)
221 } else {
222 autogenTemplate(b.ctx, b.name, autogenPath, b.hostTemplate, config, b.outputFileName, b.testInstallBase)
223 }
224 }
225 }
226 return autogenPath
227 }
228 if len(b.configsForAutogenerated) > 0 {
229 b.ctx.ModuleErrorf("Extra tradefed configurations were provided for an autogenerated xml file, but the autogenerated xml file was not used.")
230 }
231 return path
232}
233
234func autogenTemplate(ctx android.ModuleContext, name string, output android.WritablePath, template string, configs []Config, outputFileName string, testInstallBase string) {
235 if template == "" {
236 ctx.ModuleErrorf("Empty template")
237 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700238 var configStrings []string
239 for _, config := range configs {
240 configStrings = append(configStrings, config.Config())
Julien Desprezeb7398e2019-02-28 08:45:28 -0800241 }
Dan Shi20ccd212019-08-27 10:37:24 -0700242 extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent))
Dan Shi37ee3b82019-06-06 16:23:32 -0700243 extraConfigs = proptools.NinjaAndShellEscape(extraConfigs)
Julien Desprezeb7398e2019-02-28 08:45:28 -0800244
Colin Cross303e21f2018-08-07 16:49:25 -0700245 ctx.Build(pctx, android.BuildParams{
246 Rule: autogenTestConfig,
247 Description: "test config",
248 Output: output,
249 Args: map[string]string{
yangbill5ec45552020-08-13 16:16:56 +0800250 "name": name,
251 "template": template,
252 "extraConfigs": extraConfigs,
253 "outputFileName": outputFileName,
254 "testInstallBase": testInstallBase,
Colin Cross303e21f2018-08-07 16:49:25 -0700255 },
256 })
257}
258
Colin Cross303e21f2018-08-07 16:49:25 -0700259var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
easoncylee5bcff5d2020-04-30 14:57:06 +0800260 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template ${extraConfigs}",
Colin Cross303e21f2018-08-07 16:49:25 -0700261 CommandDeps: []string{
262 "${AutoGenTestConfigScript}",
263 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700264 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700265 },
easoncylee5bcff5d2020-04-30 14:57:06 +0800266}, "name", "template", "extraConfigs")
Colin Cross303e21f2018-08-07 16:49:25 -0700267
Dan Shi6ffaaa82019-09-26 11:41:36 -0700268func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string,
easoncylee5bcff5d2020-04-30 14:57:06 +0800269 testConfigTemplateProp *string, manifest android.Path, testSuites []string, autoGenConfig *bool, configs []Config) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900270 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
easoncylee5bcff5d2020-04-30 14:57:06 +0800271 var configStrings []string
Colin Cross303e21f2018-08-07 16:49:25 -0700272 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700273 template := "${InstrumentationTestConfigTemplate}"
274 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
275 if moduleTemplate.Valid() {
276 template = moduleTemplate.String()
277 }
easoncylee5bcff5d2020-04-30 14:57:06 +0800278 for _, config := range configs {
279 configStrings = append(configStrings, config.Config())
280 }
281 extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent))
282 extraConfigs = fmt.Sprintf("--extra-configs '%s'", extraConfigs)
283
Colin Cross303e21f2018-08-07 16:49:25 -0700284 ctx.Build(pctx, android.BuildParams{
285 Rule: autogenInstrumentationTest,
286 Description: "test config",
287 Input: manifest,
288 Output: autogenPath,
289 Args: map[string]string{
easoncylee5bcff5d2020-04-30 14:57:06 +0800290 "name": ctx.ModuleName(),
291 "template": template,
292 "extraConfigs": extraConfigs,
Colin Cross303e21f2018-08-07 16:49:25 -0700293 },
294 })
Jack He33338892018-09-19 02:21:28 -0700295 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700296 }
297 return path
298}
Dan Shi6ffaaa82019-09-26 11:41:36 -0700299
300var Bool = proptools.Bool
301var BoolDefault = proptools.BoolDefault