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