Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package tradefed |
| 16 | |
| 17 | import ( |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 18 | "fmt" |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 19 | "strings" |
| 20 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 23 | |
| 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 27 | const test_xml_indent = " " |
| 28 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 29 | func getTestConfigTemplate(ctx android.ModuleContext, prop *string) android.OptionalPath { |
| 30 | return ctx.ExpandOptionalSource(prop, "test_config_template") |
| 31 | } |
| 32 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 33 | func 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 | |
| 42 | var autogenTestConfig = pctx.StaticRule("autogenTestConfig", blueprint.RuleParams{ |
yangbill | 5ec4555 | 2020-08-13 16:16:56 +0800 | [diff] [blame] | 43 | 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 Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 44 | CommandDeps: []string{"$template"}, |
yangbill | 5ec4555 | 2020-08-13 16:16:56 +0800 | [diff] [blame] | 45 | }, "name", "template", "extraConfigs", "outputFileName", "testInstallBase") |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 46 | |
Lorenzo Colitti | e29c21e | 2020-02-14 18:27:56 +0900 | [diff] [blame] | 47 | func testConfigPath(ctx android.ModuleContext, prop *string, testSuites []string, autoGenConfig *bool, testConfigTemplateProp *string) (path android.Path, autogenPath android.WritablePath) { |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 48 | p := getTestConfig(ctx, prop) |
| 49 | if !Bool(autoGenConfig) && p != nil { |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 50 | return p, nil |
Lorenzo Colitti | e29c21e | 2020-02-14 18:27:56 +0900 | [diff] [blame] | 51 | } else if BoolDefault(autoGenConfig, true) && (!android.InList("cts", testSuites) || testConfigTemplateProp != nil) { |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 52 | outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".config") |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 53 | return nil, outputFile |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 54 | } else { |
| 55 | // CTS modules can be used for test data, so test config files must be |
Lorenzo Colitti | e29c21e | 2020-02-14 18:27:56 +0900 | [diff] [blame] | 56 | // explicitly created using AndroidTest.xml or test_config_template. |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 57 | return nil, nil |
| 58 | } |
| 59 | } |
| 60 | |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 61 | type Config interface { |
| 62 | Config() string |
| 63 | } |
| 64 | |
| 65 | type Option struct { |
| 66 | Name string |
easoncylee | 1e3fdcd | 2020-04-30 10:08:33 +0800 | [diff] [blame] | 67 | Key string |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 68 | Value string |
| 69 | } |
| 70 | |
| 71 | var _ Config = Option{} |
| 72 | |
| 73 | func (o Option) Config() string { |
easoncylee | 1e3fdcd | 2020-04-30 10:08:33 +0800 | [diff] [blame] | 74 | if o.Key != "" { |
| 75 | return fmt.Sprintf(`<option name="%s" key="%s" value="%s" />`, o.Name, o.Key, o.Value) |
| 76 | } |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 77 | return fmt.Sprintf(`<option name="%s" value="%s" />`, o.Name, o.Value) |
| 78 | } |
| 79 | |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 80 | // It can be a template of object or target_preparer. |
| 81 | type Object struct { |
| 82 | // Set it as a target_preparer if object type == "target_preparer". |
| 83 | Type string |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 84 | Class string |
| 85 | Options []Option |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 86 | } |
| 87 | |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 88 | var _ Config = Object{} |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 89 | |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 90 | func (ob Object) Config() string { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 91 | var optionStrings []string |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 92 | for _, option := range ob.Options { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 93 | optionStrings = append(optionStrings, option.Config()) |
| 94 | } |
| 95 | var options string |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 96 | if len(ob.Options) == 0 { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 97 | options = "" |
| 98 | } else { |
| 99 | optionDelimiter := fmt.Sprintf("\\n%s%s", test_xml_indent, test_xml_indent) |
| 100 | options = optionDelimiter + strings.Join(optionStrings, optionDelimiter) |
| 101 | } |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 102 | 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 Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Cole Faust | 8ec823c | 2022-12-07 18:18:37 -0800 | [diff] [blame^] | 110 | // MaybeAutoGenTestConfigBuilder provides a Build() method that will either |
| 111 | // generate a AndroidTest.xml file, or use an existing user-supplied one. |
| 112 | // It used to be a bunch of separate functions for each language, but was |
| 113 | // converted to this builder pattern to have one function that accepts many |
| 114 | // optional arguments. |
| 115 | type MaybeAutoGenTestConfigBuilder struct { |
| 116 | ctx android.ModuleContext |
| 117 | name string |
| 118 | outputFileName string |
| 119 | testConfigProp *string |
| 120 | testConfigTemplateProp *string |
| 121 | testSuites []string |
| 122 | config []Config |
| 123 | configsForAutogenerated []Config |
| 124 | autoGenConfig *bool |
| 125 | unitTest *bool |
| 126 | testInstallBase string |
| 127 | deviceTemplate string |
| 128 | hostTemplate string |
| 129 | hostUnitTestTemplate string |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Cole Faust | 8ec823c | 2022-12-07 18:18:37 -0800 | [diff] [blame^] | 132 | func NewMaybeAutoGenTestConfigBuilder(ctx android.ModuleContext) *MaybeAutoGenTestConfigBuilder { |
| 133 | return &MaybeAutoGenTestConfigBuilder{ |
| 134 | ctx: ctx, |
| 135 | name: ctx.ModuleName(), |
| 136 | } |
frankfeng | c5b8749 | 2020-06-03 10:28:47 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Cole Faust | 8ec823c | 2022-12-07 18:18:37 -0800 | [diff] [blame^] | 139 | func (b *MaybeAutoGenTestConfigBuilder) SetName(name string) *MaybeAutoGenTestConfigBuilder { |
| 140 | b.name = name |
| 141 | return b |
| 142 | } |
| 143 | |
| 144 | func (b *MaybeAutoGenTestConfigBuilder) SetOutputFileName(outputFileName string) *MaybeAutoGenTestConfigBuilder { |
| 145 | b.outputFileName = outputFileName |
| 146 | return b |
| 147 | } |
| 148 | |
| 149 | func (b *MaybeAutoGenTestConfigBuilder) SetTestConfigProp(testConfigProp *string) *MaybeAutoGenTestConfigBuilder { |
| 150 | b.testConfigProp = testConfigProp |
| 151 | return b |
| 152 | } |
| 153 | |
| 154 | func (b *MaybeAutoGenTestConfigBuilder) SetTestTemplateConfigProp(testConfigTemplateProp *string) *MaybeAutoGenTestConfigBuilder { |
| 155 | b.testConfigTemplateProp = testConfigTemplateProp |
| 156 | return b |
| 157 | } |
| 158 | |
| 159 | func (b *MaybeAutoGenTestConfigBuilder) SetTestSuites(testSuites []string) *MaybeAutoGenTestConfigBuilder { |
| 160 | b.testSuites = testSuites |
| 161 | return b |
| 162 | } |
| 163 | |
| 164 | func (b *MaybeAutoGenTestConfigBuilder) SetConfig(config []Config) *MaybeAutoGenTestConfigBuilder { |
| 165 | b.config = config |
| 166 | return b |
| 167 | } |
| 168 | |
| 169 | func (b *MaybeAutoGenTestConfigBuilder) SetOptionsForAutogenerated(configsForAutogenerated []Option) *MaybeAutoGenTestConfigBuilder { |
| 170 | configs := make([]Config, 0, len(configsForAutogenerated)) |
| 171 | for _, c := range configsForAutogenerated { |
| 172 | configs = append(configs, c) |
| 173 | } |
| 174 | b.configsForAutogenerated = configs |
| 175 | return b |
| 176 | } |
| 177 | |
| 178 | func (b *MaybeAutoGenTestConfigBuilder) SetUnitTest(unitTest *bool) *MaybeAutoGenTestConfigBuilder { |
| 179 | b.unitTest = unitTest |
| 180 | return b |
| 181 | } |
| 182 | |
| 183 | func (b *MaybeAutoGenTestConfigBuilder) SetAutoGenConfig(autoGenConfig *bool) *MaybeAutoGenTestConfigBuilder { |
| 184 | b.autoGenConfig = autoGenConfig |
| 185 | return b |
| 186 | } |
| 187 | |
| 188 | func (b *MaybeAutoGenTestConfigBuilder) SetTestInstallBase(testInstallBase string) *MaybeAutoGenTestConfigBuilder { |
| 189 | b.testInstallBase = testInstallBase |
| 190 | return b |
| 191 | } |
| 192 | |
| 193 | func (b *MaybeAutoGenTestConfigBuilder) SetDeviceTemplate(deviceTemplate string) *MaybeAutoGenTestConfigBuilder { |
| 194 | b.deviceTemplate = deviceTemplate |
| 195 | return b |
| 196 | } |
| 197 | |
| 198 | func (b *MaybeAutoGenTestConfigBuilder) SetHostTemplate(hostTemplate string) *MaybeAutoGenTestConfigBuilder { |
| 199 | b.hostTemplate = hostTemplate |
| 200 | return b |
| 201 | } |
| 202 | |
| 203 | func (b *MaybeAutoGenTestConfigBuilder) SetHostUnitTestTemplate(hostUnitTestTemplate string) *MaybeAutoGenTestConfigBuilder { |
| 204 | b.hostUnitTestTemplate = hostUnitTestTemplate |
| 205 | return b |
| 206 | } |
| 207 | |
| 208 | func (b *MaybeAutoGenTestConfigBuilder) Build() android.Path { |
| 209 | config := append(b.config, b.configsForAutogenerated...) |
| 210 | path, autogenPath := testConfigPath(b.ctx, b.testConfigProp, b.testSuites, b.autoGenConfig, b.testConfigTemplateProp) |
| 211 | if autogenPath != nil { |
| 212 | templatePath := getTestConfigTemplate(b.ctx, b.testConfigTemplateProp) |
| 213 | if templatePath.Valid() { |
| 214 | autogenTemplate(b.ctx, b.name, autogenPath, templatePath.String(), config, b.outputFileName, b.testInstallBase) |
| 215 | } else { |
| 216 | if b.ctx.Device() { |
| 217 | autogenTemplate(b.ctx, b.name, autogenPath, b.deviceTemplate, config, b.outputFileName, b.testInstallBase) |
| 218 | } else { |
| 219 | if Bool(b.unitTest) { |
| 220 | autogenTemplate(b.ctx, b.name, autogenPath, b.hostUnitTestTemplate, config, b.outputFileName, b.testInstallBase) |
| 221 | } else { |
| 222 | autogenTemplate(b.ctx, b.name, autogenPath, b.hostTemplate, config, b.outputFileName, b.testInstallBase) |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | return autogenPath |
| 227 | } |
| 228 | if len(b.configsForAutogenerated) > 0 { |
| 229 | b.ctx.ModuleErrorf("Extra tradefed configurations were provided for an autogenerated xml file, but the autogenerated xml file was not used.") |
| 230 | } |
| 231 | return path |
| 232 | } |
| 233 | |
| 234 | func autogenTemplate(ctx android.ModuleContext, name string, output android.WritablePath, template string, configs []Config, outputFileName string, testInstallBase string) { |
| 235 | if template == "" { |
| 236 | ctx.ModuleErrorf("Empty template") |
| 237 | } |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 238 | var configStrings []string |
| 239 | for _, config := range configs { |
| 240 | configStrings = append(configStrings, config.Config()) |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 241 | } |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 242 | extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent)) |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 243 | extraConfigs = proptools.NinjaAndShellEscape(extraConfigs) |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 244 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 245 | ctx.Build(pctx, android.BuildParams{ |
| 246 | Rule: autogenTestConfig, |
| 247 | Description: "test config", |
| 248 | Output: output, |
| 249 | Args: map[string]string{ |
yangbill | 5ec4555 | 2020-08-13 16:16:56 +0800 | [diff] [blame] | 250 | "name": name, |
| 251 | "template": template, |
| 252 | "extraConfigs": extraConfigs, |
| 253 | "outputFileName": outputFileName, |
| 254 | "testInstallBase": testInstallBase, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 255 | }, |
| 256 | }) |
| 257 | } |
| 258 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 259 | var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{ |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 260 | Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template ${extraConfigs}", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 261 | CommandDeps: []string{ |
| 262 | "${AutoGenTestConfigScript}", |
| 263 | "${EmptyTestConfig}", |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 264 | "$template", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 265 | }, |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 266 | }, "name", "template", "extraConfigs") |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 267 | |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 268 | func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 269 | testConfigTemplateProp *string, manifest android.Path, testSuites []string, autoGenConfig *bool, configs []Config) android.Path { |
Lorenzo Colitti | e29c21e | 2020-02-14 18:27:56 +0900 | [diff] [blame] | 270 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp) |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 271 | var configStrings []string |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 272 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 273 | template := "${InstrumentationTestConfigTemplate}" |
| 274 | moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 275 | if moduleTemplate.Valid() { |
| 276 | template = moduleTemplate.String() |
| 277 | } |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 278 | for _, config := range configs { |
| 279 | configStrings = append(configStrings, config.Config()) |
| 280 | } |
| 281 | extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent)) |
| 282 | extraConfigs = fmt.Sprintf("--extra-configs '%s'", extraConfigs) |
| 283 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 284 | ctx.Build(pctx, android.BuildParams{ |
| 285 | Rule: autogenInstrumentationTest, |
| 286 | Description: "test config", |
| 287 | Input: manifest, |
| 288 | Output: autogenPath, |
| 289 | Args: map[string]string{ |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 290 | "name": ctx.ModuleName(), |
| 291 | "template": template, |
| 292 | "extraConfigs": extraConfigs, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 293 | }, |
| 294 | }) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 295 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 296 | } |
| 297 | return path |
| 298 | } |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 299 | |
| 300 | var Bool = proptools.Bool |
| 301 | var BoolDefault = proptools.BoolDefault |