blob: 27d71e8287a7cefeed8b43ca46fbc9559cb9c5a4 [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,
Julien Desprez70898c42020-11-19 09:43:45 -0800191 testSuites []string, autoGenConfig *bool, unitTest *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 {
Julien Desprez70898c42020-11-19 09:43:45 -0800201 if Bool(unitTest) {
202 autogenTemplate(ctx, autogenPath, "${JavaHostUnitTestConfigTemplate}", nil, "")
203 } else {
204 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil, "")
205 }
Jack He33338892018-09-19 02:21:28 -0700206 }
Colin Cross303e21f2018-08-07 16:49:25 -0700207 }
Jack He33338892018-09-19 02:21:28 -0700208 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700209 }
210 return path
211}
212
yelinhsieh80880a32018-11-06 11:49:55 +0800213func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700214 testConfigTemplateProp *string, testSuites []string, autoGenConfig *bool) android.Path {
yelinhsieh80880a32018-11-06 11:49:55 +0800215
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900216 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
yelinhsieh80880a32018-11-06 11:49:55 +0800217 if autogenPath != nil {
218 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
219 if templatePath.Valid() {
yangbill5ec45552020-08-13 16:16:56 +0800220 autogenTemplate(ctx, autogenPath, templatePath.String(), nil, "")
yelinhsieh80880a32018-11-06 11:49:55 +0800221 } else {
yangbill5ec45552020-08-13 16:16:56 +0800222 autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}", nil, "")
yelinhsieh80880a32018-11-06 11:49:55 +0800223 }
224 return autogenPath
225 }
226 return path
227}
228
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400229func AutoGenRustTestConfig(ctx android.ModuleContext, testConfigProp *string,
230 testConfigTemplateProp *string, testSuites []string, config []Config, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900231 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700232 if autogenPath != nil {
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700233 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
234 if templatePath.Valid() {
yangbill5ec45552020-08-13 16:16:56 +0800235 autogenTemplate(ctx, autogenPath, templatePath.String(), config, "")
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400236 } else {
237 if ctx.Device() {
yangbill5ec45552020-08-13 16:16:56 +0800238 autogenTemplate(ctx, autogenPath, "${RustDeviceTestConfigTemplate}", config, "")
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400239 } else {
yangbill5ec45552020-08-13 16:16:56 +0800240 autogenTemplate(ctx, autogenPath, "${RustHostTestConfigTemplate}", config, "")
Ivan Lozanofc80fe72020-06-11 13:25:36 -0400241 }
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700242 }
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700243 return autogenPath
244 }
245 return path
246}
247
Colin Cross8eebb132020-01-29 20:07:03 -0800248func AutoGenRobolectricTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string,
249 testSuites []string, autoGenConfig *bool) android.Path {
250 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
251 if autogenPath != nil {
252 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
253 if templatePath.Valid() {
yangbill5ec45552020-08-13 16:16:56 +0800254 autogenTemplate(ctx, autogenPath, templatePath.String(), nil, "")
Colin Cross8eebb132020-01-29 20:07:03 -0800255 } else {
yangbill5ec45552020-08-13 16:16:56 +0800256 autogenTemplate(ctx, autogenPath, "${RobolectricTestConfigTemplate}", nil, "")
Colin Cross8eebb132020-01-29 20:07:03 -0800257 }
258 return autogenPath
259 }
260 return path
261}
262
Colin Cross303e21f2018-08-07 16:49:25 -0700263var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
easoncylee5bcff5d2020-04-30 14:57:06 +0800264 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template ${extraConfigs}",
Colin Cross303e21f2018-08-07 16:49:25 -0700265 CommandDeps: []string{
266 "${AutoGenTestConfigScript}",
267 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700268 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700269 },
easoncylee5bcff5d2020-04-30 14:57:06 +0800270}, "name", "template", "extraConfigs")
Colin Cross303e21f2018-08-07 16:49:25 -0700271
Dan Shi6ffaaa82019-09-26 11:41:36 -0700272func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string,
easoncylee5bcff5d2020-04-30 14:57:06 +0800273 testConfigTemplateProp *string, manifest android.Path, testSuites []string, autoGenConfig *bool, configs []Config) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900274 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
easoncylee5bcff5d2020-04-30 14:57:06 +0800275 var configStrings []string
Colin Cross303e21f2018-08-07 16:49:25 -0700276 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700277 template := "${InstrumentationTestConfigTemplate}"
278 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
279 if moduleTemplate.Valid() {
280 template = moduleTemplate.String()
281 }
easoncylee5bcff5d2020-04-30 14:57:06 +0800282 for _, config := range configs {
283 configStrings = append(configStrings, config.Config())
284 }
285 extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent))
286 extraConfigs = fmt.Sprintf("--extra-configs '%s'", extraConfigs)
287
Colin Cross303e21f2018-08-07 16:49:25 -0700288 ctx.Build(pctx, android.BuildParams{
289 Rule: autogenInstrumentationTest,
290 Description: "test config",
291 Input: manifest,
292 Output: autogenPath,
293 Args: map[string]string{
easoncylee5bcff5d2020-04-30 14:57:06 +0800294 "name": ctx.ModuleName(),
295 "template": template,
296 "extraConfigs": extraConfigs,
Colin Cross303e21f2018-08-07 16:49:25 -0700297 },
298 })
Jack He33338892018-09-19 02:21:28 -0700299 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700300 }
301 return path
302}
Dan Shi6ffaaa82019-09-26 11:41:36 -0700303
304var Bool = proptools.Bool
305var BoolDefault = proptools.BoolDefault