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{ |
Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 43 | Command: "sed 's&{MODULE}&${name}&g;s&{EXTRA_CONFIGS}&'${extraConfigs}'&g;s&{EXTRA_TEST_RUNNER_CONFIGS}&'${extraTestRunnerConfigs}'&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"}, |
Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 45 | }, "name", "template", "extraConfigs", "outputFileName", "testInstallBase", "extraTestRunnerConfigs") |
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 | |
Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 110 | func autogenTemplate(ctx android.ModuleContext, name string, output android.WritablePath, template string, configs []Config, testRunnerConfigs []Option, outputFileName string, testInstallBase string) { |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 111 | if template == "" { |
| 112 | ctx.ModuleErrorf("Empty template") |
| 113 | } |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 114 | var configStrings []string |
| 115 | for _, config := range configs { |
| 116 | configStrings = append(configStrings, config.Config()) |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 117 | } |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 118 | extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent)) |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 119 | extraConfigs = proptools.NinjaAndShellEscape(extraConfigs) |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 120 | |
Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 121 | var testRunnerConfigStrings []string |
| 122 | for _, config := range testRunnerConfigs { |
| 123 | testRunnerConfigStrings = append(testRunnerConfigStrings, config.Config()) |
| 124 | } |
| 125 | extraTestRunnerConfigs := strings.Join(testRunnerConfigStrings, fmt.Sprintf("\\n%s%s", test_xml_indent, test_xml_indent)) |
| 126 | if len(extraTestRunnerConfigs) > 0 { |
| 127 | extraTestRunnerConfigs += fmt.Sprintf("\\n%s%s", test_xml_indent, test_xml_indent) |
| 128 | } |
| 129 | extraTestRunnerConfigs = proptools.NinjaAndShellEscape(extraTestRunnerConfigs) |
| 130 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 131 | ctx.Build(pctx, android.BuildParams{ |
| 132 | Rule: autogenTestConfig, |
| 133 | Description: "test config", |
| 134 | Output: output, |
| 135 | Args: map[string]string{ |
Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 136 | "name": name, |
| 137 | "template": template, |
| 138 | "extraConfigs": extraConfigs, |
| 139 | "outputFileName": outputFileName, |
| 140 | "testInstallBase": testInstallBase, |
| 141 | "extraTestRunnerConfigs": extraTestRunnerConfigs, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 142 | }, |
| 143 | }) |
| 144 | } |
| 145 | |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 146 | // AutoGenTestConfigOptions is used so that we can supply many optional |
| 147 | // arguments to the AutoGenTestConfig function. |
| 148 | type AutoGenTestConfigOptions struct { |
| 149 | Name string |
| 150 | OutputFileName string |
| 151 | TestConfigProp *string |
| 152 | TestConfigTemplateProp *string |
| 153 | TestSuites []string |
| 154 | Config []Config |
| 155 | OptionsForAutogenerated []Option |
Dan Shi | ec73143 | 2023-05-26 04:21:44 +0000 | [diff] [blame] | 156 | TestRunnerOptions []Option |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 157 | AutoGenConfig *bool |
| 158 | UnitTest *bool |
| 159 | TestInstallBase string |
| 160 | DeviceTemplate string |
| 161 | HostTemplate string |
| 162 | HostUnitTestTemplate string |
| 163 | } |
Tahsin Loqman | 77dc7d0 | 2022-12-19 16:27:25 +0000 | [diff] [blame] | 164 | |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 165 | func AutoGenTestConfig(ctx android.ModuleContext, options AutoGenTestConfigOptions) android.Path { |
| 166 | configs := append([]Config{}, options.Config...) |
| 167 | for _, c := range options.OptionsForAutogenerated { |
| 168 | configs = append(configs, c) |
| 169 | } |
Cole Faust | 0cec5ea | 2023-01-04 11:24:28 -0800 | [diff] [blame] | 170 | name := options.Name |
| 171 | if name == "" { |
| 172 | name = ctx.ModuleName() |
| 173 | } |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 174 | path, autogenPath := testConfigPath(ctx, options.TestConfigProp, options.TestSuites, options.AutoGenConfig, options.TestConfigTemplateProp) |
Tahsin Loqman | 77dc7d0 | 2022-12-19 16:27:25 +0000 | [diff] [blame] | 175 | if autogenPath != nil { |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 176 | templatePath := getTestConfigTemplate(ctx, options.TestConfigTemplateProp) |
Tahsin Loqman | 77dc7d0 | 2022-12-19 16:27:25 +0000 | [diff] [blame] | 177 | if templatePath.Valid() { |
Dan Shi | 9b4835c | 2023-06-15 22:58:49 +0000 | [diff] [blame] | 178 | autogenTemplate(ctx, name, autogenPath, templatePath.String(), configs, options.TestRunnerOptions, options.OutputFileName, options.TestInstallBase) |
Tahsin Loqman | 77dc7d0 | 2022-12-19 16:27:25 +0000 | [diff] [blame] | 179 | } else { |
| 180 | if ctx.Device() { |
Dan Shi | 9b4835c | 2023-06-15 22:58:49 +0000 | [diff] [blame] | 181 | autogenTemplate(ctx, name, autogenPath, options.DeviceTemplate, configs, options.TestRunnerOptions, options.OutputFileName, options.TestInstallBase) |
Tahsin Loqman | 77dc7d0 | 2022-12-19 16:27:25 +0000 | [diff] [blame] | 182 | } else { |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 183 | if Bool(options.UnitTest) { |
Dan Shi | 9b4835c | 2023-06-15 22:58:49 +0000 | [diff] [blame] | 184 | autogenTemplate(ctx, name, autogenPath, options.HostUnitTestTemplate, configs, options.TestRunnerOptions, options.OutputFileName, options.TestInstallBase) |
Tahsin Loqman | 77dc7d0 | 2022-12-19 16:27:25 +0000 | [diff] [blame] | 185 | } else { |
Dan Shi | 9b4835c | 2023-06-15 22:58:49 +0000 | [diff] [blame] | 186 | autogenTemplate(ctx, name, autogenPath, options.HostTemplate, configs, options.TestRunnerOptions, options.OutputFileName, options.TestInstallBase) |
Tahsin Loqman | 77dc7d0 | 2022-12-19 16:27:25 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | } |
| 190 | return autogenPath |
| 191 | } |
Cole Faust | 2168054 | 2022-12-07 18:18:37 -0800 | [diff] [blame] | 192 | if len(options.OptionsForAutogenerated) > 0 { |
| 193 | ctx.ModuleErrorf("Extra tradefed configurations were provided for an autogenerated xml file, but the autogenerated xml file was not used.") |
Tahsin Loqman | 77dc7d0 | 2022-12-19 16:27:25 +0000 | [diff] [blame] | 194 | } |
| 195 | return path |
| 196 | } |
| 197 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 198 | var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{ |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 199 | Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template ${extraConfigs}", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 200 | CommandDeps: []string{ |
| 201 | "${AutoGenTestConfigScript}", |
| 202 | "${EmptyTestConfig}", |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 203 | "$template", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 204 | }, |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 205 | }, "name", "template", "extraConfigs") |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 206 | |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 207 | func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 208 | 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] | 209 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp) |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 210 | var configStrings []string |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 211 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 212 | template := "${InstrumentationTestConfigTemplate}" |
| 213 | moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 214 | if moduleTemplate.Valid() { |
| 215 | template = moduleTemplate.String() |
| 216 | } |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 217 | for _, config := range configs { |
| 218 | configStrings = append(configStrings, config.Config()) |
| 219 | } |
| 220 | extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent)) |
| 221 | extraConfigs = fmt.Sprintf("--extra-configs '%s'", extraConfigs) |
| 222 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 223 | ctx.Build(pctx, android.BuildParams{ |
| 224 | Rule: autogenInstrumentationTest, |
| 225 | Description: "test config", |
| 226 | Input: manifest, |
| 227 | Output: autogenPath, |
| 228 | Args: map[string]string{ |
easoncylee | 5bcff5d | 2020-04-30 14:57:06 +0800 | [diff] [blame] | 229 | "name": ctx.ModuleName(), |
| 230 | "template": template, |
| 231 | "extraConfigs": extraConfigs, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 232 | }, |
| 233 | }) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 234 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 235 | } |
| 236 | return path |
| 237 | } |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 238 | |
| 239 | var Bool = proptools.Bool |
| 240 | var BoolDefault = proptools.BoolDefault |