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 | |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame^] | 47 | func 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 Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 50 | return p, nil |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame^] | 51 | } else if !android.InList("cts", testSuites) && BoolDefault(autoGenConfig, true) { |
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 |
| 56 | // explicitly created using AndroidTest.xml |
| 57 | // TODO(b/112602712): remove the path check |
| 58 | return nil, nil |
| 59 | } |
| 60 | } |
| 61 | |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 62 | type Config interface { |
| 63 | Config() string |
| 64 | } |
| 65 | |
| 66 | type Option struct { |
| 67 | Name string |
| 68 | Value string |
| 69 | } |
| 70 | |
| 71 | var _ Config = Option{} |
| 72 | |
| 73 | func (o Option) Config() string { |
| 74 | return fmt.Sprintf(`<option name="%s" value="%s" />`, o.Name, o.Value) |
| 75 | } |
| 76 | |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 77 | // It can be a template of object or target_preparer. |
| 78 | type Object struct { |
| 79 | // Set it as a target_preparer if object type == "target_preparer". |
| 80 | Type string |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 81 | Class string |
| 82 | Options []Option |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 83 | } |
| 84 | |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 85 | var _ Config = Object{} |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 86 | |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 87 | func (ob Object) Config() string { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 88 | var optionStrings []string |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 89 | for _, option := range ob.Options { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 90 | optionStrings = append(optionStrings, option.Config()) |
| 91 | } |
| 92 | var options string |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 93 | if len(ob.Options) == 0 { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 94 | options = "" |
| 95 | } else { |
| 96 | optionDelimiter := fmt.Sprintf("\\n%s%s", test_xml_indent, test_xml_indent) |
| 97 | options = optionDelimiter + strings.Join(optionStrings, optionDelimiter) |
| 98 | } |
nelsonli | 0d7111e | 2019-09-17 16:35:23 +0800 | [diff] [blame] | 99 | 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 Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string, configs []Config) { |
| 108 | var configStrings []string |
| 109 | for _, config := range configs { |
| 110 | configStrings = append(configStrings, config.Config()) |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 111 | } |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 112 | extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent)) |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 113 | extraConfigs = proptools.NinjaAndShellEscape(extraConfigs) |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 114 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 115 | ctx.Build(pctx, android.BuildParams{ |
| 116 | Rule: autogenTestConfig, |
| 117 | Description: "test config", |
| 118 | Output: output, |
| 119 | Args: map[string]string{ |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 120 | "name": ctx.ModuleName(), |
| 121 | "template": template, |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 122 | "extraConfigs": extraConfigs, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 123 | }, |
| 124 | }) |
| 125 | } |
| 126 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 127 | func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string, |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame^] | 128 | testConfigTemplateProp *string, testSuites []string, config []Config, autoGenConfig *bool) android.Path { |
| 129 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 130 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 131 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 132 | if templatePath.Valid() { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 133 | autogenTemplate(ctx, autogenPath, templatePath.String(), config) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 134 | } else { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 135 | if ctx.Device() { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 136 | autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}", config) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 137 | } else { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 138 | autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}", config) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 139 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 140 | } |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 141 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 142 | } |
| 143 | return path |
| 144 | } |
| 145 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 146 | func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string, |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame^] | 147 | testConfigTemplateProp *string, testSuites []string, configs []Config, autoGenConfig *bool) android.Path { |
| 148 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 149 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 150 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 151 | if templatePath.Valid() { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 152 | autogenTemplate(ctx, autogenPath, templatePath.String(), configs) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 153 | } else { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 154 | autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", configs) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 155 | } |
| 156 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 157 | } |
| 158 | return path |
| 159 | } |
| 160 | |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame^] | 161 | func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, |
| 162 | testSuites []string, autoGenConfig *bool) android.Path { |
| 163 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 164 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 165 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 166 | if templatePath.Valid() { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 167 | autogenTemplate(ctx, autogenPath, templatePath.String(), nil) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 168 | } else { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 169 | if ctx.Device() { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 170 | autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 171 | } else { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 172 | autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 173 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 174 | } |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 175 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 176 | } |
| 177 | return path |
| 178 | } |
| 179 | |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 180 | func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string, |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame^] | 181 | testConfigTemplateProp *string, testSuites []string, autoGenConfig *bool) android.Path { |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 182 | |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame^] | 183 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig) |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 184 | if autogenPath != nil { |
| 185 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 186 | if templatePath.Valid() { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 187 | autogenTemplate(ctx, autogenPath, templatePath.String(), nil) |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 188 | } else { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 189 | autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}", nil) |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 190 | } |
| 191 | return autogenPath |
| 192 | } |
| 193 | return path |
| 194 | } |
| 195 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 196 | var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{ |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 197 | Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 198 | CommandDeps: []string{ |
| 199 | "${AutoGenTestConfigScript}", |
| 200 | "${EmptyTestConfig}", |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 201 | "$template", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 202 | }, |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 203 | }, "name", "template") |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 204 | |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame^] | 205 | func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, |
| 206 | testConfigTemplateProp *string, manifest android.Path, testSuites []string, autoGenConfig *bool) android.Path { |
| 207 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 208 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 209 | template := "${InstrumentationTestConfigTemplate}" |
| 210 | moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 211 | if moduleTemplate.Valid() { |
| 212 | template = moduleTemplate.String() |
| 213 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 214 | ctx.Build(pctx, android.BuildParams{ |
| 215 | Rule: autogenInstrumentationTest, |
| 216 | Description: "test config", |
| 217 | Input: manifest, |
| 218 | Output: autogenPath, |
| 219 | Args: map[string]string{ |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 220 | "name": ctx.ModuleName(), |
| 221 | "template": template, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 222 | }, |
| 223 | }) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 224 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 225 | } |
| 226 | return path |
| 227 | } |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame^] | 228 | |
| 229 | var Bool = proptools.Bool |
| 230 | var BoolDefault = proptools.BoolDefault |