blob: 596284bebbd84a99d5bb72e8ca5092154f0d156e [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{
Dan Shi37ee3b82019-06-06 16:23:32 -070043 Command: "sed 's&{MODULE}&${name}&g;s&{EXTRA_CONFIGS}&'${extraConfigs}'&g' $template > $out",
Colin Cross303e21f2018-08-07 16:49:25 -070044 CommandDeps: []string{"$template"},
Dan Shi37ee3b82019-06-06 16:23:32 -070045}, "name", "template", "extraConfigs")
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
110func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string, configs []Config) {
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700111 autogenTemplateWithName(ctx, ctx.ModuleName(), output, template, configs)
112}
113
114func autogenTemplateWithName(ctx android.ModuleContext, name string, output android.WritablePath, template string, configs []Config) {
Dan Shi37ee3b82019-06-06 16:23:32 -0700115 var configStrings []string
116 for _, config := range configs {
117 configStrings = append(configStrings, config.Config())
Julien Desprezeb7398e2019-02-28 08:45:28 -0800118 }
Dan Shi20ccd212019-08-27 10:37:24 -0700119 extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent))
Dan Shi37ee3b82019-06-06 16:23:32 -0700120 extraConfigs = proptools.NinjaAndShellEscape(extraConfigs)
Julien Desprezeb7398e2019-02-28 08:45:28 -0800121
Colin Cross303e21f2018-08-07 16:49:25 -0700122 ctx.Build(pctx, android.BuildParams{
123 Rule: autogenTestConfig,
124 Description: "test config",
125 Output: output,
126 Args: map[string]string{
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700127 "name": name,
Julien Desprezeb7398e2019-02-28 08:45:28 -0800128 "template": template,
Dan Shi37ee3b82019-06-06 16:23:32 -0700129 "extraConfigs": extraConfigs,
Colin Cross303e21f2018-08-07 16:49:25 -0700130 },
131 })
132}
133
Jack He33338892018-09-19 02:21:28 -0700134func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700135 testConfigTemplateProp *string, testSuites []string, config []Config, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900136 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700137 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700138 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
139 if templatePath.Valid() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700140 autogenTemplate(ctx, autogenPath, templatePath.String(), config)
Colin Cross303e21f2018-08-07 16:49:25 -0700141 } else {
Jack He33338892018-09-19 02:21:28 -0700142 if ctx.Device() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700143 autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}", config)
Jack He33338892018-09-19 02:21:28 -0700144 } else {
Dan Shi37ee3b82019-06-06 16:23:32 -0700145 autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}", config)
Jack He33338892018-09-19 02:21:28 -0700146 }
Colin Cross303e21f2018-08-07 16:49:25 -0700147 }
Jack He33338892018-09-19 02:21:28 -0700148 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700149 }
150 return path
151}
152
Jack He33338892018-09-19 02:21:28 -0700153func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700154 testConfigTemplateProp *string, testSuites []string, configs []Config, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900155 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700156 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700157 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
158 if templatePath.Valid() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700159 autogenTemplate(ctx, autogenPath, templatePath.String(), configs)
Jack He33338892018-09-19 02:21:28 -0700160 } else {
Dan Shi37ee3b82019-06-06 16:23:32 -0700161 autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", configs)
Jack He33338892018-09-19 02:21:28 -0700162 }
163 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700164 }
165 return path
166}
167
Dan Shi6ffaaa82019-09-26 11:41:36 -0700168func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string,
169 testSuites []string, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900170 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700171 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700172 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
173 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800174 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700175 } else {
Jack He33338892018-09-19 02:21:28 -0700176 if ctx.Device() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800177 autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700178 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800179 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700180 }
Colin Cross303e21f2018-08-07 16:49:25 -0700181 }
Jack He33338892018-09-19 02:21:28 -0700182 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700183 }
184 return path
185}
186
yelinhsieh80880a32018-11-06 11:49:55 +0800187func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700188 testConfigTemplateProp *string, testSuites []string, autoGenConfig *bool) android.Path {
yelinhsieh80880a32018-11-06 11:49:55 +0800189
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900190 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
yelinhsieh80880a32018-11-06 11:49:55 +0800191 if autogenPath != nil {
192 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
193 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800194 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800195 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800196 autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}", nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800197 }
198 return autogenPath
199 }
200 return path
201}
202
Chih-Hung Hsiehede57ae2019-11-22 20:22:35 -0800203func AutoGenRustTestConfig(ctx android.ModuleContext, name string, testConfigProp *string,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700204 testConfigTemplateProp *string, testSuites []string, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900205 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700206 if autogenPath != nil {
207 templatePathString := "${RustHostTestConfigTemplate}"
Chih-Hung Hsiehede57ae2019-11-22 20:22:35 -0800208 if ctx.Device() {
209 templatePathString = "${RustDeviceTestConfigTemplate}"
210 }
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700211 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
212 if templatePath.Valid() {
213 templatePathString = templatePath.String()
214 }
215 autogenTemplateWithName(ctx, name, autogenPath, templatePathString, nil)
216 return autogenPath
217 }
218 return path
219}
220
Colin Cross303e21f2018-08-07 16:49:25 -0700221var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
Jack He33338892018-09-19 02:21:28 -0700222 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template",
Colin Cross303e21f2018-08-07 16:49:25 -0700223 CommandDeps: []string{
224 "${AutoGenTestConfigScript}",
225 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700226 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700227 },
Jack He33338892018-09-19 02:21:28 -0700228}, "name", "template")
Colin Cross303e21f2018-08-07 16:49:25 -0700229
Dan Shi6ffaaa82019-09-26 11:41:36 -0700230func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string,
231 testConfigTemplateProp *string, manifest android.Path, testSuites []string, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900232 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700233 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700234 template := "${InstrumentationTestConfigTemplate}"
235 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
236 if moduleTemplate.Valid() {
237 template = moduleTemplate.String()
238 }
Colin Cross303e21f2018-08-07 16:49:25 -0700239 ctx.Build(pctx, android.BuildParams{
240 Rule: autogenInstrumentationTest,
241 Description: "test config",
242 Input: manifest,
243 Output: autogenPath,
244 Args: map[string]string{
Jack He33338892018-09-19 02:21:28 -0700245 "name": ctx.ModuleName(),
246 "template": template,
Colin Cross303e21f2018-08-07 16:49:25 -0700247 },
248 })
Jack He33338892018-09-19 02:21:28 -0700249 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700250 }
251 return path
252}
Dan Shi6ffaaa82019-09-26 11:41:36 -0700253
254var Bool = proptools.Bool
255var BoolDefault = proptools.BoolDefault