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