blob: a46dce93173f95e86dbeb5e7737ecd8a47f6be80 [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
67 Value string
68}
69
70var _ Config = Option{}
71
72func (o Option) Config() string {
73 return fmt.Sprintf(`<option name="%s" value="%s" />`, o.Name, o.Value)
74}
75
nelsonli0d7111e2019-09-17 16:35:23 +080076// It can be a template of object or target_preparer.
77type Object struct {
78 // Set it as a target_preparer if object type == "target_preparer".
79 Type string
Dan Shi20ccd212019-08-27 10:37:24 -070080 Class string
81 Options []Option
Dan Shi37ee3b82019-06-06 16:23:32 -070082}
83
nelsonli0d7111e2019-09-17 16:35:23 +080084var _ Config = Object{}
Dan Shi37ee3b82019-06-06 16:23:32 -070085
nelsonli0d7111e2019-09-17 16:35:23 +080086func (ob Object) Config() string {
Dan Shi20ccd212019-08-27 10:37:24 -070087 var optionStrings []string
nelsonli0d7111e2019-09-17 16:35:23 +080088 for _, option := range ob.Options {
Dan Shi20ccd212019-08-27 10:37:24 -070089 optionStrings = append(optionStrings, option.Config())
90 }
91 var options string
nelsonli0d7111e2019-09-17 16:35:23 +080092 if len(ob.Options) == 0 {
Dan Shi20ccd212019-08-27 10:37:24 -070093 options = ""
94 } else {
95 optionDelimiter := fmt.Sprintf("\\n%s%s", test_xml_indent, test_xml_indent)
96 options = optionDelimiter + strings.Join(optionStrings, optionDelimiter)
97 }
nelsonli0d7111e2019-09-17 16:35:23 +080098 if ob.Type == "target_preparer" {
99 return fmt.Sprintf(`<target_preparer class="%s">%s\n%s</target_preparer>`, ob.Class, options, test_xml_indent)
100 } else {
101 return fmt.Sprintf(`<object type="%s" class="%s">%s\n%s</object>`, ob.Type, ob.Class, options, test_xml_indent)
102 }
103
Dan Shi37ee3b82019-06-06 16:23:32 -0700104}
105
106func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string, configs []Config) {
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700107 autogenTemplateWithName(ctx, ctx.ModuleName(), output, template, configs)
108}
109
110func autogenTemplateWithName(ctx android.ModuleContext, name string, output android.WritablePath, template string, configs []Config) {
Dan Shi37ee3b82019-06-06 16:23:32 -0700111 var configStrings []string
112 for _, config := range configs {
113 configStrings = append(configStrings, config.Config())
Julien Desprezeb7398e2019-02-28 08:45:28 -0800114 }
Dan Shi20ccd212019-08-27 10:37:24 -0700115 extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent))
Dan Shi37ee3b82019-06-06 16:23:32 -0700116 extraConfigs = proptools.NinjaAndShellEscape(extraConfigs)
Julien Desprezeb7398e2019-02-28 08:45:28 -0800117
Colin Cross303e21f2018-08-07 16:49:25 -0700118 ctx.Build(pctx, android.BuildParams{
119 Rule: autogenTestConfig,
120 Description: "test config",
121 Output: output,
122 Args: map[string]string{
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700123 "name": name,
Julien Desprezeb7398e2019-02-28 08:45:28 -0800124 "template": template,
Dan Shi37ee3b82019-06-06 16:23:32 -0700125 "extraConfigs": extraConfigs,
Colin Cross303e21f2018-08-07 16:49:25 -0700126 },
127 })
128}
129
Jack He33338892018-09-19 02:21:28 -0700130func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700131 testConfigTemplateProp *string, testSuites []string, config []Config, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900132 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700133 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700134 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
135 if templatePath.Valid() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700136 autogenTemplate(ctx, autogenPath, templatePath.String(), config)
Colin Cross303e21f2018-08-07 16:49:25 -0700137 } else {
Jack He33338892018-09-19 02:21:28 -0700138 if ctx.Device() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700139 autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}", config)
Jack He33338892018-09-19 02:21:28 -0700140 } else {
Dan Shi37ee3b82019-06-06 16:23:32 -0700141 autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}", config)
Jack He33338892018-09-19 02:21:28 -0700142 }
Colin Cross303e21f2018-08-07 16:49:25 -0700143 }
Jack He33338892018-09-19 02:21:28 -0700144 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700145 }
146 return path
147}
148
Jack He33338892018-09-19 02:21:28 -0700149func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700150 testConfigTemplateProp *string, testSuites []string, configs []Config, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900151 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700152 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700153 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
154 if templatePath.Valid() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700155 autogenTemplate(ctx, autogenPath, templatePath.String(), configs)
Jack He33338892018-09-19 02:21:28 -0700156 } else {
Dan Shi37ee3b82019-06-06 16:23:32 -0700157 autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", configs)
Jack He33338892018-09-19 02:21:28 -0700158 }
159 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700160 }
161 return path
162}
163
Dan Shi6ffaaa82019-09-26 11:41:36 -0700164func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string,
165 testSuites []string, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900166 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700167 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700168 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
169 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800170 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700171 } else {
Jack He33338892018-09-19 02:21:28 -0700172 if ctx.Device() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800173 autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700174 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800175 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700176 }
Colin Cross303e21f2018-08-07 16:49:25 -0700177 }
Jack He33338892018-09-19 02:21:28 -0700178 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700179 }
180 return path
181}
182
yelinhsieh80880a32018-11-06 11:49:55 +0800183func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700184 testConfigTemplateProp *string, testSuites []string, autoGenConfig *bool) android.Path {
yelinhsieh80880a32018-11-06 11:49:55 +0800185
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900186 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
yelinhsieh80880a32018-11-06 11:49:55 +0800187 if autogenPath != nil {
188 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
189 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800190 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800191 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800192 autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}", nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800193 }
194 return autogenPath
195 }
196 return path
197}
198
Chih-Hung Hsiehede57ae2019-11-22 20:22:35 -0800199func AutoGenRustTestConfig(ctx android.ModuleContext, name string, testConfigProp *string,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700200 testConfigTemplateProp *string, testSuites []string, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900201 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700202 if autogenPath != nil {
203 templatePathString := "${RustHostTestConfigTemplate}"
Chih-Hung Hsiehede57ae2019-11-22 20:22:35 -0800204 if ctx.Device() {
205 templatePathString = "${RustDeviceTestConfigTemplate}"
206 }
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700207 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
208 if templatePath.Valid() {
209 templatePathString = templatePath.String()
210 }
211 autogenTemplateWithName(ctx, name, autogenPath, templatePathString, nil)
212 return autogenPath
213 }
214 return path
215}
216
Colin Cross303e21f2018-08-07 16:49:25 -0700217var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
Jack He33338892018-09-19 02:21:28 -0700218 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template",
Colin Cross303e21f2018-08-07 16:49:25 -0700219 CommandDeps: []string{
220 "${AutoGenTestConfigScript}",
221 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700222 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700223 },
Jack He33338892018-09-19 02:21:28 -0700224}, "name", "template")
Colin Cross303e21f2018-08-07 16:49:25 -0700225
Dan Shi6ffaaa82019-09-26 11:41:36 -0700226func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string,
227 testConfigTemplateProp *string, manifest android.Path, testSuites []string, autoGenConfig *bool) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900228 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700229 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700230 template := "${InstrumentationTestConfigTemplate}"
231 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
232 if moduleTemplate.Valid() {
233 template = moduleTemplate.String()
234 }
Colin Cross303e21f2018-08-07 16:49:25 -0700235 ctx.Build(pctx, android.BuildParams{
236 Rule: autogenInstrumentationTest,
237 Description: "test config",
238 Input: manifest,
239 Output: autogenPath,
240 Args: map[string]string{
Jack He33338892018-09-19 02:21:28 -0700241 "name": ctx.ModuleName(),
242 "template": template,
Colin Cross303e21f2018-08-07 16:49:25 -0700243 },
244 })
Jack He33338892018-09-19 02:21:28 -0700245 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700246 }
247 return path
248}
Dan Shi6ffaaa82019-09-26 11:41:36 -0700249
250var Bool = proptools.Bool
251var BoolDefault = proptools.BoolDefault