blob: c35d8b91403cf0b9153bd0514e4c648866faae1f [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
Dan Shi6ffaaa82019-09-26 11:41:36 -070047func testConfigPath(ctx android.ModuleContext, prop *string, testSuites []string, autoGenConfig *bool) (path android.Path, autogenPath android.WritablePath) {
48 p := getTestConfig(ctx, prop)
49 if !Bool(autoGenConfig) && p != nil {
Colin Cross303e21f2018-08-07 16:49:25 -070050 return p, nil
Dan Shi6ffaaa82019-09-26 11:41:36 -070051 } else if !android.InList("cts", testSuites) && BoolDefault(autoGenConfig, true) {
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
56 // explicitly created using AndroidTest.xml
57 // TODO(b/112602712): remove the path check
58 return nil, nil
59 }
60}
61
Dan Shi37ee3b82019-06-06 16:23:32 -070062type Config interface {
63 Config() string
64}
65
66type Option struct {
67 Name string
68 Value string
69}
70
71var _ Config = Option{}
72
73func (o Option) Config() string {
74 return fmt.Sprintf(`<option name="%s" value="%s" />`, o.Name, o.Value)
75}
76
nelsonli0d7111e2019-09-17 16:35:23 +080077// It can be a template of object or target_preparer.
78type Object struct {
79 // Set it as a target_preparer if object type == "target_preparer".
80 Type string
Dan Shi20ccd212019-08-27 10:37:24 -070081 Class string
82 Options []Option
Dan Shi37ee3b82019-06-06 16:23:32 -070083}
84
nelsonli0d7111e2019-09-17 16:35:23 +080085var _ Config = Object{}
Dan Shi37ee3b82019-06-06 16:23:32 -070086
nelsonli0d7111e2019-09-17 16:35:23 +080087func (ob Object) Config() string {
Dan Shi20ccd212019-08-27 10:37:24 -070088 var optionStrings []string
nelsonli0d7111e2019-09-17 16:35:23 +080089 for _, option := range ob.Options {
Dan Shi20ccd212019-08-27 10:37:24 -070090 optionStrings = append(optionStrings, option.Config())
91 }
92 var options string
nelsonli0d7111e2019-09-17 16:35:23 +080093 if len(ob.Options) == 0 {
Dan Shi20ccd212019-08-27 10:37:24 -070094 options = ""
95 } else {
96 optionDelimiter := fmt.Sprintf("\\n%s%s", test_xml_indent, test_xml_indent)
97 options = optionDelimiter + strings.Join(optionStrings, optionDelimiter)
98 }
nelsonli0d7111e2019-09-17 16:35:23 +080099 if ob.Type == "target_preparer" {
100 return fmt.Sprintf(`<target_preparer class="%s">%s\n%s</target_preparer>`, ob.Class, options, test_xml_indent)
101 } else {
102 return fmt.Sprintf(`<object type="%s" class="%s">%s\n%s</object>`, ob.Type, ob.Class, options, test_xml_indent)
103 }
104
Dan Shi37ee3b82019-06-06 16:23:32 -0700105}
106
107func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string, configs []Config) {
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700108 autogenTemplateWithName(ctx, ctx.ModuleName(), output, template, configs)
109}
110
111func autogenTemplateWithName(ctx android.ModuleContext, name string, output android.WritablePath, template string, configs []Config) {
Dan Shi37ee3b82019-06-06 16:23:32 -0700112 var configStrings []string
113 for _, config := range configs {
114 configStrings = append(configStrings, config.Config())
Julien Desprezeb7398e2019-02-28 08:45:28 -0800115 }
Dan Shi20ccd212019-08-27 10:37:24 -0700116 extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent))
Dan Shi37ee3b82019-06-06 16:23:32 -0700117 extraConfigs = proptools.NinjaAndShellEscape(extraConfigs)
Julien Desprezeb7398e2019-02-28 08:45:28 -0800118
Colin Cross303e21f2018-08-07 16:49:25 -0700119 ctx.Build(pctx, android.BuildParams{
120 Rule: autogenTestConfig,
121 Description: "test config",
122 Output: output,
123 Args: map[string]string{
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700124 "name": name,
Julien Desprezeb7398e2019-02-28 08:45:28 -0800125 "template": template,
Dan Shi37ee3b82019-06-06 16:23:32 -0700126 "extraConfigs": extraConfigs,
Colin Cross303e21f2018-08-07 16:49:25 -0700127 },
128 })
129}
130
Jack He33338892018-09-19 02:21:28 -0700131func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700132 testConfigTemplateProp *string, testSuites []string, config []Config, autoGenConfig *bool) android.Path {
133 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig)
Colin Cross303e21f2018-08-07 16:49:25 -0700134 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700135 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
136 if templatePath.Valid() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700137 autogenTemplate(ctx, autogenPath, templatePath.String(), config)
Colin Cross303e21f2018-08-07 16:49:25 -0700138 } else {
Jack He33338892018-09-19 02:21:28 -0700139 if ctx.Device() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700140 autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}", config)
Jack He33338892018-09-19 02:21:28 -0700141 } else {
Dan Shi37ee3b82019-06-06 16:23:32 -0700142 autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}", config)
Jack He33338892018-09-19 02:21:28 -0700143 }
Colin Cross303e21f2018-08-07 16:49:25 -0700144 }
Jack He33338892018-09-19 02:21:28 -0700145 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700146 }
147 return path
148}
149
Jack He33338892018-09-19 02:21:28 -0700150func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700151 testConfigTemplateProp *string, testSuites []string, configs []Config, autoGenConfig *bool) android.Path {
152 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig)
Colin Cross303e21f2018-08-07 16:49:25 -0700153 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700154 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
155 if templatePath.Valid() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700156 autogenTemplate(ctx, autogenPath, templatePath.String(), configs)
Jack He33338892018-09-19 02:21:28 -0700157 } else {
Dan Shi37ee3b82019-06-06 16:23:32 -0700158 autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", configs)
Jack He33338892018-09-19 02:21:28 -0700159 }
160 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700161 }
162 return path
163}
164
Dan Shi6ffaaa82019-09-26 11:41:36 -0700165func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string,
166 testSuites []string, autoGenConfig *bool) android.Path {
167 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig)
Colin Cross303e21f2018-08-07 16:49:25 -0700168 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700169 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
170 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800171 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700172 } else {
Jack He33338892018-09-19 02:21:28 -0700173 if ctx.Device() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800174 autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700175 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800176 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700177 }
Colin Cross303e21f2018-08-07 16:49:25 -0700178 }
Jack He33338892018-09-19 02:21:28 -0700179 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700180 }
181 return path
182}
183
yelinhsieh80880a32018-11-06 11:49:55 +0800184func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700185 testConfigTemplateProp *string, testSuites []string, autoGenConfig *bool) android.Path {
yelinhsieh80880a32018-11-06 11:49:55 +0800186
Dan Shi6ffaaa82019-09-26 11:41:36 -0700187 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig)
yelinhsieh80880a32018-11-06 11:49:55 +0800188 if autogenPath != nil {
189 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
190 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800191 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800192 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800193 autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}", nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800194 }
195 return autogenPath
196 }
197 return path
198}
199
Chih-Hung Hsiehede57ae2019-11-22 20:22:35 -0800200func AutoGenRustTestConfig(ctx android.ModuleContext, name string, testConfigProp *string,
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700201 testConfigTemplateProp *string, testSuites []string, autoGenConfig *bool) android.Path {
202 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig)
203 if autogenPath != nil {
204 templatePathString := "${RustHostTestConfigTemplate}"
Chih-Hung Hsiehede57ae2019-11-22 20:22:35 -0800205 if ctx.Device() {
206 templatePathString = "${RustDeviceTestConfigTemplate}"
207 }
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700208 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
209 if templatePath.Valid() {
210 templatePathString = templatePath.String()
211 }
212 autogenTemplateWithName(ctx, name, autogenPath, templatePathString, nil)
213 return autogenPath
214 }
215 return path
216}
217
Colin Cross303e21f2018-08-07 16:49:25 -0700218var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
Jack He33338892018-09-19 02:21:28 -0700219 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template",
Colin Cross303e21f2018-08-07 16:49:25 -0700220 CommandDeps: []string{
221 "${AutoGenTestConfigScript}",
222 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700223 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700224 },
Jack He33338892018-09-19 02:21:28 -0700225}, "name", "template")
Colin Cross303e21f2018-08-07 16:49:25 -0700226
Dan Shi6ffaaa82019-09-26 11:41:36 -0700227func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string,
228 testConfigTemplateProp *string, manifest android.Path, testSuites []string, autoGenConfig *bool) android.Path {
229 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig)
Colin Cross303e21f2018-08-07 16:49:25 -0700230 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700231 template := "${InstrumentationTestConfigTemplate}"
232 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
233 if moduleTemplate.Valid() {
234 template = moduleTemplate.String()
235 }
Colin Cross303e21f2018-08-07 16:49:25 -0700236 ctx.Build(pctx, android.BuildParams{
237 Rule: autogenInstrumentationTest,
238 Description: "test config",
239 Input: manifest,
240 Output: autogenPath,
241 Args: map[string]string{
Jack He33338892018-09-19 02:21:28 -0700242 "name": ctx.ModuleName(),
243 "template": template,
Colin Cross303e21f2018-08-07 16:49:25 -0700244 },
245 })
Jack He33338892018-09-19 02:21:28 -0700246 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700247 }
248 return path
249}
Dan Shi6ffaaa82019-09-26 11:41:36 -0700250
251var Bool = proptools.Bool
252var BoolDefault = proptools.BoolDefault