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 | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 43 | Command: "sed 's&{MODULE}&${name}&g;s&{EXTRA_CONFIGS}&'${extraConfigs}'&g' $template > $out", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 44 | CommandDeps: []string{"$template"}, |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 45 | }, "name", "template", "extraConfigs") |
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 |
| 67 | Value string |
| 68 | } |
| 69 | |
| 70 | var _ Config = Option{} |
| 71 | |
| 72 | func (o Option) Config() string { |
| 73 | return fmt.Sprintf(`<option name="%s" value="%s" />`, o.Name, o.Value) |
| 74 | } |
| 75 | |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 76 | // It can be a template of object or target_preparer. |
| 77 | type Object struct { |
| 78 | // Set it as a target_preparer if object type == "target_preparer". |
| 79 | Type string |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 80 | Class string |
| 81 | Options []Option |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 82 | } |
| 83 | |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 84 | var _ Config = Object{} |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 85 | |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 86 | func (ob Object) Config() string { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 87 | var optionStrings []string |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 88 | for _, option := range ob.Options { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 89 | optionStrings = append(optionStrings, option.Config()) |
| 90 | } |
| 91 | var options string |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 92 | if len(ob.Options) == 0 { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 93 | options = "" |
| 94 | } else { |
| 95 | optionDelimiter := fmt.Sprintf("\\n%s%s", test_xml_indent, test_xml_indent) |
| 96 | options = optionDelimiter + strings.Join(optionStrings, optionDelimiter) |
| 97 | } |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 98 | 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 Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string, configs []Config) { |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 107 | autogenTemplateWithName(ctx, ctx.ModuleName(), output, template, configs) |
| 108 | } |
| 109 | |
| 110 | func autogenTemplateWithName(ctx android.ModuleContext, name string, output android.WritablePath, template string, configs []Config) { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 111 | var configStrings []string |
| 112 | for _, config := range configs { |
| 113 | configStrings = append(configStrings, config.Config()) |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 114 | } |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 115 | extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent)) |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 116 | extraConfigs = proptools.NinjaAndShellEscape(extraConfigs) |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 117 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 118 | ctx.Build(pctx, android.BuildParams{ |
| 119 | Rule: autogenTestConfig, |
| 120 | Description: "test config", |
| 121 | Output: output, |
| 122 | Args: map[string]string{ |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 123 | "name": name, |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 124 | "template": template, |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 125 | "extraConfigs": extraConfigs, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 126 | }, |
| 127 | }) |
| 128 | } |
| 129 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 130 | func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string, |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 131 | testConfigTemplateProp *string, testSuites []string, config []Config, autoGenConfig *bool) android.Path { |
Lorenzo Colitti | e29c21e | 2020-02-14 18:27:56 +0900 | [diff] [blame^] | 132 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 133 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 134 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 135 | if templatePath.Valid() { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 136 | autogenTemplate(ctx, autogenPath, templatePath.String(), config) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 137 | } else { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 138 | if ctx.Device() { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 139 | autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}", config) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 140 | } else { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 141 | autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}", config) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 142 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 143 | } |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 144 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 145 | } |
| 146 | return path |
| 147 | } |
| 148 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 149 | func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string, |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 150 | testConfigTemplateProp *string, testSuites []string, configs []Config, autoGenConfig *bool) android.Path { |
Lorenzo Colitti | e29c21e | 2020-02-14 18:27:56 +0900 | [diff] [blame^] | 151 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 152 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 153 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 154 | if templatePath.Valid() { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 155 | autogenTemplate(ctx, autogenPath, templatePath.String(), configs) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 156 | } else { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 157 | autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", configs) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 158 | } |
| 159 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 160 | } |
| 161 | return path |
| 162 | } |
| 163 | |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 164 | func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, |
| 165 | testSuites []string, autoGenConfig *bool) android.Path { |
Lorenzo Colitti | e29c21e | 2020-02-14 18:27:56 +0900 | [diff] [blame^] | 166 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 167 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 168 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 169 | if templatePath.Valid() { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 170 | autogenTemplate(ctx, autogenPath, templatePath.String(), nil) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 171 | } else { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 172 | if ctx.Device() { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 173 | autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 174 | } else { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 175 | autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 176 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 177 | } |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 178 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 179 | } |
| 180 | return path |
| 181 | } |
| 182 | |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 183 | func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string, |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 184 | testConfigTemplateProp *string, testSuites []string, autoGenConfig *bool) android.Path { |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 185 | |
Lorenzo Colitti | e29c21e | 2020-02-14 18:27:56 +0900 | [diff] [blame^] | 186 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp) |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 187 | if autogenPath != nil { |
| 188 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 189 | if templatePath.Valid() { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 190 | autogenTemplate(ctx, autogenPath, templatePath.String(), nil) |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 191 | } else { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 192 | autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}", nil) |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 193 | } |
| 194 | return autogenPath |
| 195 | } |
| 196 | return path |
| 197 | } |
| 198 | |
Chih-Hung Hsieh | ede57ae | 2019-11-22 20:22:35 -0800 | [diff] [blame] | 199 | func AutoGenRustTestConfig(ctx android.ModuleContext, name string, testConfigProp *string, |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 200 | testConfigTemplateProp *string, testSuites []string, autoGenConfig *bool) android.Path { |
Lorenzo Colitti | e29c21e | 2020-02-14 18:27:56 +0900 | [diff] [blame^] | 201 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp) |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 202 | if autogenPath != nil { |
| 203 | templatePathString := "${RustHostTestConfigTemplate}" |
Chih-Hung Hsieh | ede57ae | 2019-11-22 20:22:35 -0800 | [diff] [blame] | 204 | if ctx.Device() { |
| 205 | templatePathString = "${RustDeviceTestConfigTemplate}" |
| 206 | } |
Chih-Hung Hsieh | 41805be | 2019-10-31 20:56:47 -0700 | [diff] [blame] | 207 | 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 Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 217 | var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{ |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 218 | Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 219 | CommandDeps: []string{ |
| 220 | "${AutoGenTestConfigScript}", |
| 221 | "${EmptyTestConfig}", |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 222 | "$template", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 223 | }, |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 224 | }, "name", "template") |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 225 | |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 226 | func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, |
| 227 | testConfigTemplateProp *string, manifest android.Path, testSuites []string, autoGenConfig *bool) android.Path { |
Lorenzo Colitti | e29c21e | 2020-02-14 18:27:56 +0900 | [diff] [blame^] | 228 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig, testConfigTemplateProp) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 229 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 230 | template := "${InstrumentationTestConfigTemplate}" |
| 231 | moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 232 | if moduleTemplate.Valid() { |
| 233 | template = moduleTemplate.String() |
| 234 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 235 | ctx.Build(pctx, android.BuildParams{ |
| 236 | Rule: autogenInstrumentationTest, |
| 237 | Description: "test config", |
| 238 | Input: manifest, |
| 239 | Output: autogenPath, |
| 240 | Args: map[string]string{ |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 241 | "name": ctx.ModuleName(), |
| 242 | "template": template, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 243 | }, |
| 244 | }) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 245 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 246 | } |
| 247 | return path |
| 248 | } |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 249 | |
| 250 | var Bool = proptools.Bool |
| 251 | var BoolDefault = proptools.BoolDefault |