blob: b35f831c7236522cc6d79cda22afd5e0b63b0f0e [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
yangbill5ec45552020-08-13 16:16:56 +0800110func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string, configs []Config, testInstallBase string) {
111 autogenTemplateWithNameAndOutputFile(ctx, ctx.ModuleName(), output, template, configs, "", testInstallBase)
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700112}
113
yangbill5ec45552020-08-13 16:16:56 +0800114func autogenTemplateWithName(ctx android.ModuleContext, name string, output android.WritablePath, template string, configs []Config, testInstallBase string) {
115 autogenTemplateWithNameAndOutputFile(ctx, name, output, template, configs, "", testInstallBase)
frankfengc5b87492020-06-03 10:28:47 -0700116}
117
yangbill5ec45552020-08-13 16:16:56 +0800118func autogenTemplateWithNameAndOutputFile(ctx android.ModuleContext, name string, output android.WritablePath, template string, configs []Config, outputFileName string, testInstallBase string) {
Dan Shi37ee3b82019-06-06 16:23:32 -0700119 var configStrings []string
120 for _, config := range configs {
121 configStrings = append(configStrings, config.Config())
Julien Desprezeb7398e2019-02-28 08:45:28 -0800122 }
Dan Shi20ccd212019-08-27 10:37:24 -0700123 extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent))
Dan Shi37ee3b82019-06-06 16:23:32 -0700124 extraConfigs = proptools.NinjaAndShellEscape(extraConfigs)
Julien Desprezeb7398e2019-02-28 08:45:28 -0800125
Colin Cross303e21f2018-08-07 16:49:25 -0700126 ctx.Build(pctx, android.BuildParams{
127 Rule: autogenTestConfig,
128 Description: "test config",
129 Output: output,
130 Args: map[string]string{
yangbill5ec45552020-08-13 16:16:56 +0800131 "name": name,
132 "template": template,
133 "extraConfigs": extraConfigs,
134 "outputFileName": outputFileName,
135 "testInstallBase": testInstallBase,
Colin Cross303e21f2018-08-07 16:49:25 -0700136 },
137 })
138}
139
Jack He33338892018-09-19 02:21:28 -0700140func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
yangbill5ec45552020-08-13 16:16:56 +0800141 testConfigTemplateProp *string, testSuites []string, config []Config, autoGenConfig *bool, testInstallBase string) android.Path {
142
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900143 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700144 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700145 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
146 if templatePath.Valid() {
yangbill5ec45552020-08-13 16:16:56 +0800147 autogenTemplate(ctx, autogenPath, templatePath.String(), config, testInstallBase)
Colin Cross303e21f2018-08-07 16:49:25 -0700148 } else {
Jack He33338892018-09-19 02:21:28 -0700149 if ctx.Device() {
yangbill5ec45552020-08-13 16:16:56 +0800150 autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}", config, testInstallBase)
Jack He33338892018-09-19 02:21:28 -0700151 } else {
yangbill5ec45552020-08-13 16:16:56 +0800152 autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}", config, testInstallBase)
Jack He33338892018-09-19 02:21:28 -0700153 }
Colin Cross303e21f2018-08-07 16:49:25 -0700154 }
Jack He33338892018-09-19 02:21:28 -0700155 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700156 }
157 return path
158}
159
frankfengc5b87492020-06-03 10:28:47 -0700160func AutoGenShellTestConfig(ctx android.ModuleContext, testConfigProp *string,
161 testConfigTemplateProp *string, testSuites []string, config []Config, autoGenConfig *bool, outputFileName string) android.Path {
162 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
163 if autogenPath != nil {
164 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
165 if templatePath.Valid() {
yangbill5ec45552020-08-13 16:16:56 +0800166 autogenTemplateWithNameAndOutputFile(ctx, ctx.ModuleName(), autogenPath, templatePath.String(), config, outputFileName, "")
frankfengc5b87492020-06-03 10:28:47 -0700167 } else {
yangbill5ec45552020-08-13 16:16:56 +0800168 autogenTemplateWithNameAndOutputFile(ctx, ctx.ModuleName(), autogenPath, "${ShellTestConfigTemplate}", config, outputFileName, "")
frankfengc5b87492020-06-03 10:28:47 -0700169 }
170 return autogenPath
171 }
172 return path
173}
174
Jack He33338892018-09-19 02:21:28 -0700175func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700176 testConfigTemplateProp *string, testSuites []string, configs []Config, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900177 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700178 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700179 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
180 if templatePath.Valid() {
yangbill5ec45552020-08-13 16:16:56 +0800181 autogenTemplate(ctx, autogenPath, templatePath.String(), configs, "")
Jack He33338892018-09-19 02:21:28 -0700182 } else {
yangbill5ec45552020-08-13 16:16:56 +0800183 autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", configs, "")
Jack He33338892018-09-19 02:21:28 -0700184 }
185 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700186 }
187 return path
188}
189
Dan Shi6ffaaa82019-09-26 11:41:36 -0700190func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string,
191 testSuites []string, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900192 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700193 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700194 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
195 if templatePath.Valid() {
yangbill5ec45552020-08-13 16:16:56 +0800196 autogenTemplate(ctx, autogenPath, templatePath.String(), nil, "")
Colin Cross303e21f2018-08-07 16:49:25 -0700197 } else {
Jack He33338892018-09-19 02:21:28 -0700198 if ctx.Device() {
yangbill5ec45552020-08-13 16:16:56 +0800199 autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil, "")
Jack He33338892018-09-19 02:21:28 -0700200 } else {
yangbill5ec45552020-08-13 16:16:56 +0800201 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil, "")
Jack He33338892018-09-19 02:21:28 -0700202 }
Colin Cross303e21f2018-08-07 16:49:25 -0700203 }
Jack He33338892018-09-19 02:21:28 -0700204 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700205 }
206 return path
207}
208
yelinhsieh80880a32018-11-06 11:49:55 +0800209func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700210 testConfigTemplateProp *string, testSuites []string, autoGenConfig *bool) android.Path {
yelinhsieh80880a32018-11-06 11:49:55 +0800211
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900212 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
yelinhsieh80880a32018-11-06 11:49:55 +0800213 if autogenPath != nil {
214 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
215 if templatePath.Valid() {
yangbill5ec45552020-08-13 16:16:56 +0800216 autogenTemplate(ctx, autogenPath, templatePath.String(), nil, "")
yelinhsieh80880a32018-11-06 11:49:55 +0800217 } else {
yangbill5ec45552020-08-13 16:16:56 +0800218 autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}", nil, "")
yelinhsieh80880a32018-11-06 11:49:55 +0800219 }
220 return autogenPath
221 }
222 return path
223}
224
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400225func AutoGenRustTestConfig(ctx android.ModuleContext, testConfigProp *string,
226 testConfigTemplateProp *string, testSuites []string, config []Config, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900227 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700228 if autogenPath != nil {
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700229 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
230 if templatePath.Valid() {
yangbill5ec45552020-08-13 16:16:56 +0800231 autogenTemplate(ctx, autogenPath, templatePath.String(), config, "")
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400232 } else {
233 if ctx.Device() {
yangbill5ec45552020-08-13 16:16:56 +0800234 autogenTemplate(ctx, autogenPath, "${RustDeviceTestConfigTemplate}", config, "")
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400235 } else {
yangbill5ec45552020-08-13 16:16:56 +0800236 autogenTemplate(ctx, autogenPath, "${RustHostTestConfigTemplate}", config, "")
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400237 }
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700238 }
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700239 return autogenPath
240 }
241 return path
242}
243
Colin Cross8eebb132020-01-29 20:07:03 -0800244func AutoGenRobolectricTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string,
245 testSuites []string, autoGenConfig *bool) android.Path {
246 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
247 if autogenPath != nil {
248 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
249 if templatePath.Valid() {
yangbill5ec45552020-08-13 16:16:56 +0800250 autogenTemplate(ctx, autogenPath, templatePath.String(), nil, "")
Colin Cross8eebb132020-01-29 20:07:03 -0800251 } else {
yangbill5ec45552020-08-13 16:16:56 +0800252 autogenTemplate(ctx, autogenPath, "${RobolectricTestConfigTemplate}", nil, "")
Colin Cross8eebb132020-01-29 20:07:03 -0800253 }
254 return autogenPath
255 }
256 return path
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