blob: e93bac3edc53b97812f9694b46d1b00ec3bc8c8d [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
Cole Faust21680542022-12-07 18:18:37 -0800110func autogenTemplate(ctx android.ModuleContext, name string, output android.WritablePath, template string, configs []Config, outputFileName string, testInstallBase string) {
111 if template == "" {
112 ctx.ModuleErrorf("Empty template")
113 }
Dan Shi37ee3b82019-06-06 16:23:32 -0700114 var configStrings []string
115 for _, config := range configs {
116 configStrings = append(configStrings, config.Config())
Julien Desprezeb7398e2019-02-28 08:45:28 -0800117 }
Dan Shi20ccd212019-08-27 10:37:24 -0700118 extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent))
Dan Shi37ee3b82019-06-06 16:23:32 -0700119 extraConfigs = proptools.NinjaAndShellEscape(extraConfigs)
Julien Desprezeb7398e2019-02-28 08:45:28 -0800120
Colin Cross303e21f2018-08-07 16:49:25 -0700121 ctx.Build(pctx, android.BuildParams{
122 Rule: autogenTestConfig,
123 Description: "test config",
124 Output: output,
125 Args: map[string]string{
yangbill5ec45552020-08-13 16:16:56 +0800126 "name": name,
127 "template": template,
128 "extraConfigs": extraConfigs,
129 "outputFileName": outputFileName,
130 "testInstallBase": testInstallBase,
Colin Cross303e21f2018-08-07 16:49:25 -0700131 },
132 })
133}
134
Cole Faust21680542022-12-07 18:18:37 -0800135// AutoGenTestConfigOptions is used so that we can supply many optional
136// arguments to the AutoGenTestConfig function.
137type AutoGenTestConfigOptions struct {
138 Name string
139 OutputFileName string
140 TestConfigProp *string
141 TestConfigTemplateProp *string
142 TestSuites []string
143 Config []Config
144 OptionsForAutogenerated []Option
145 AutoGenConfig *bool
146 UnitTest *bool
147 TestInstallBase string
148 DeviceTemplate string
149 HostTemplate string
150 HostUnitTestTemplate string
151}
Tahsin Loqman77dc7d02022-12-19 16:27:25 +0000152
Cole Faust21680542022-12-07 18:18:37 -0800153func AutoGenTestConfig(ctx android.ModuleContext, options AutoGenTestConfigOptions) android.Path {
154 configs := append([]Config{}, options.Config...)
155 for _, c := range options.OptionsForAutogenerated {
156 configs = append(configs, c)
157 }
158 path, autogenPath := testConfigPath(ctx, options.TestConfigProp, options.TestSuites, options.AutoGenConfig, options.TestConfigTemplateProp)
Tahsin Loqman77dc7d02022-12-19 16:27:25 +0000159 if autogenPath != nil {
Cole Faust21680542022-12-07 18:18:37 -0800160 templatePath := getTestConfigTemplate(ctx, options.TestConfigTemplateProp)
Tahsin Loqman77dc7d02022-12-19 16:27:25 +0000161 if templatePath.Valid() {
Cole Faust21680542022-12-07 18:18:37 -0800162 autogenTemplate(ctx, options.Name, autogenPath, templatePath.String(), configs, options.OutputFileName, options.TestInstallBase)
Tahsin Loqman77dc7d02022-12-19 16:27:25 +0000163 } else {
164 if ctx.Device() {
Cole Faust21680542022-12-07 18:18:37 -0800165 autogenTemplate(ctx, options.Name, autogenPath, options.DeviceTemplate, configs, options.OutputFileName, options.TestInstallBase)
Tahsin Loqman77dc7d02022-12-19 16:27:25 +0000166 } else {
Cole Faust21680542022-12-07 18:18:37 -0800167 if Bool(options.UnitTest) {
168 autogenTemplate(ctx, options.Name, autogenPath, options.HostUnitTestTemplate, configs, options.OutputFileName, options.TestInstallBase)
Tahsin Loqman77dc7d02022-12-19 16:27:25 +0000169 } else {
Cole Faust21680542022-12-07 18:18:37 -0800170 autogenTemplate(ctx, options.Name, autogenPath, options.HostTemplate, configs, options.OutputFileName, options.TestInstallBase)
Tahsin Loqman77dc7d02022-12-19 16:27:25 +0000171 }
172 }
173 }
174 return autogenPath
175 }
Cole Faust21680542022-12-07 18:18:37 -0800176 if len(options.OptionsForAutogenerated) > 0 {
177 ctx.ModuleErrorf("Extra tradefed configurations were provided for an autogenerated xml file, but the autogenerated xml file was not used.")
Tahsin Loqman77dc7d02022-12-19 16:27:25 +0000178 }
179 return path
180}
181
Colin Cross303e21f2018-08-07 16:49:25 -0700182var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
easoncylee5bcff5d2020-04-30 14:57:06 +0800183 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template ${extraConfigs}",
Colin Cross303e21f2018-08-07 16:49:25 -0700184 CommandDeps: []string{
185 "${AutoGenTestConfigScript}",
186 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700187 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700188 },
easoncylee5bcff5d2020-04-30 14:57:06 +0800189}, "name", "template", "extraConfigs")
Colin Cross303e21f2018-08-07 16:49:25 -0700190
Dan Shi6ffaaa82019-09-26 11:41:36 -0700191func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string,
easoncylee5bcff5d2020-04-30 14:57:06 +0800192 testConfigTemplateProp *string, manifest android.Path, testSuites []string, autoGenConfig *bool, configs []Config) android.Path {
Lorenzo Colittie29c21e2020-02-14 18:27:56 +0900193 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp)
easoncylee5bcff5d2020-04-30 14:57:06 +0800194 var configStrings []string
Colin Cross303e21f2018-08-07 16:49:25 -0700195 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700196 template := "${InstrumentationTestConfigTemplate}"
197 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
198 if moduleTemplate.Valid() {
199 template = moduleTemplate.String()
200 }
easoncylee5bcff5d2020-04-30 14:57:06 +0800201 for _, config := range configs {
202 configStrings = append(configStrings, config.Config())
203 }
204 extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent))
205 extraConfigs = fmt.Sprintf("--extra-configs '%s'", extraConfigs)
206
Colin Cross303e21f2018-08-07 16:49:25 -0700207 ctx.Build(pctx, android.BuildParams{
208 Rule: autogenInstrumentationTest,
209 Description: "test config",
210 Input: manifest,
211 Output: autogenPath,
212 Args: map[string]string{
easoncylee5bcff5d2020-04-30 14:57:06 +0800213 "name": ctx.ModuleName(),
214 "template": template,
215 "extraConfigs": extraConfigs,
Colin Cross303e21f2018-08-07 16:49:25 -0700216 },
217 })
Jack He33338892018-09-19 02:21:28 -0700218 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700219 }
220 return path
221}
Dan Shi6ffaaa82019-09-26 11:41:36 -0700222
223var Bool = proptools.Bool
224var BoolDefault = proptools.BoolDefault