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 | |
yangbill | 4f41bc2 | 2019-02-13 21:45:47 +0800 | [diff] [blame] | 47 | func testConfigPath(ctx android.ModuleContext, prop *string, testSuites []string) (path android.Path, autogenPath android.WritablePath) { |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 48 | if p := getTestConfig(ctx, prop); p != nil { |
| 49 | return p, nil |
yangbill | 4f41bc2 | 2019-02-13 21:45:47 +0800 | [diff] [blame] | 50 | } else if !android.InList("cts", testSuites) { |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 51 | outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".config") |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 52 | return nil, outputFile |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 53 | } else { |
| 54 | // CTS modules can be used for test data, so test config files must be |
| 55 | // explicitly created using AndroidTest.xml |
| 56 | // TODO(b/112602712): remove the path check |
| 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 | |
| 76 | type Preparer struct { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 77 | Class string |
| 78 | Options []Option |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | var _ Config = Preparer{} |
| 82 | |
| 83 | func (p Preparer) Config() string { |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 84 | var optionStrings []string |
| 85 | for _, option := range p.Options { |
| 86 | optionStrings = append(optionStrings, option.Config()) |
| 87 | } |
| 88 | var options string |
| 89 | if len(p.Options) == 0 { |
| 90 | options = "" |
| 91 | } else { |
| 92 | optionDelimiter := fmt.Sprintf("\\n%s%s", test_xml_indent, test_xml_indent) |
| 93 | options = optionDelimiter + strings.Join(optionStrings, optionDelimiter) |
| 94 | } |
| 95 | return fmt.Sprintf(`<target_preparer class="%s">%s\n%s</target_preparer>`, p.Class, options, test_xml_indent) |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string, configs []Config) { |
| 99 | var configStrings []string |
| 100 | for _, config := range configs { |
| 101 | configStrings = append(configStrings, config.Config()) |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 102 | } |
Dan Shi | 20ccd21 | 2019-08-27 10:37:24 -0700 | [diff] [blame] | 103 | extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent)) |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 104 | extraConfigs = proptools.NinjaAndShellEscape(extraConfigs) |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 105 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 106 | ctx.Build(pctx, android.BuildParams{ |
| 107 | Rule: autogenTestConfig, |
| 108 | Description: "test config", |
| 109 | Output: output, |
| 110 | Args: map[string]string{ |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 111 | "name": ctx.ModuleName(), |
| 112 | "template": template, |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 113 | "extraConfigs": extraConfigs, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 114 | }, |
| 115 | }) |
| 116 | } |
| 117 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 118 | func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string, |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 119 | testConfigTemplateProp *string, testSuites []string, config []Config) android.Path { |
yangbill | 4f41bc2 | 2019-02-13 21:45:47 +0800 | [diff] [blame] | 120 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 121 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 122 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 123 | if templatePath.Valid() { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 124 | autogenTemplate(ctx, autogenPath, templatePath.String(), config) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 125 | } else { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 126 | if ctx.Device() { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 127 | autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}", config) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 128 | } else { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 129 | autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}", config) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 130 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 131 | } |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 132 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 133 | } |
| 134 | return path |
| 135 | } |
| 136 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 137 | func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string, |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 138 | testConfigTemplateProp *string, testSuites []string, configs []Config) android.Path { |
yangbill | 4f41bc2 | 2019-02-13 21:45:47 +0800 | [diff] [blame] | 139 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 140 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 141 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 142 | if templatePath.Valid() { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 143 | autogenTemplate(ctx, autogenPath, templatePath.String(), configs) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 144 | } else { |
Dan Shi | 37ee3b8 | 2019-06-06 16:23:32 -0700 | [diff] [blame] | 145 | autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", configs) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 146 | } |
| 147 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 148 | } |
| 149 | return path |
| 150 | } |
| 151 | |
yangbill | 4f41bc2 | 2019-02-13 21:45:47 +0800 | [diff] [blame] | 152 | func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, testSuites []string) android.Path { |
| 153 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 154 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 155 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 156 | if templatePath.Valid() { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 157 | autogenTemplate(ctx, autogenPath, templatePath.String(), nil) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 158 | } else { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 159 | if ctx.Device() { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 160 | autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 161 | } else { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 162 | autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 163 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 164 | } |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 165 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 166 | } |
| 167 | return path |
| 168 | } |
| 169 | |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 170 | func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string, |
yangbill | 4f41bc2 | 2019-02-13 21:45:47 +0800 | [diff] [blame] | 171 | testConfigTemplateProp *string, testSuites []string) android.Path { |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 172 | |
yangbill | 4f41bc2 | 2019-02-13 21:45:47 +0800 | [diff] [blame] | 173 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites) |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 174 | if autogenPath != nil { |
| 175 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 176 | if templatePath.Valid() { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 177 | autogenTemplate(ctx, autogenPath, templatePath.String(), nil) |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 178 | } else { |
Julien Desprez | eb7398e | 2019-02-28 08:45:28 -0800 | [diff] [blame] | 179 | autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}", nil) |
yelinhsieh | 80880a3 | 2018-11-06 11:49:55 +0800 | [diff] [blame] | 180 | } |
| 181 | return autogenPath |
| 182 | } |
| 183 | return path |
| 184 | } |
| 185 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 186 | var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{ |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 187 | Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 188 | CommandDeps: []string{ |
| 189 | "${AutoGenTestConfigScript}", |
| 190 | "${EmptyTestConfig}", |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 191 | "$template", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 192 | }, |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 193 | }, "name", "template") |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 194 | |
yangbill | 4f41bc2 | 2019-02-13 21:45:47 +0800 | [diff] [blame] | 195 | func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, manifest android.Path, testSuites []string) android.Path { |
| 196 | path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 197 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 198 | template := "${InstrumentationTestConfigTemplate}" |
| 199 | moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 200 | if moduleTemplate.Valid() { |
| 201 | template = moduleTemplate.String() |
| 202 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 203 | ctx.Build(pctx, android.BuildParams{ |
| 204 | Rule: autogenInstrumentationTest, |
| 205 | Description: "test config", |
| 206 | Input: manifest, |
| 207 | Output: autogenPath, |
| 208 | Args: map[string]string{ |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 209 | "name": ctx.ModuleName(), |
| 210 | "template": template, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 211 | }, |
| 212 | }) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 213 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 214 | } |
| 215 | return path |
| 216 | } |