blob: 3d30cfa132f76a4d4f7f355199ec8daa09650d0f [file] [log] [blame]
Colin Cross303e21f2018-08-07 16:49:25 -07001// 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
15package tradefed
16
17import (
Julien Desprezeb7398e2019-02-28 08:45:28 -080018 "fmt"
Julien Desprezeb7398e2019-02-28 08:45:28 -080019 "strings"
20
Colin Cross303e21f2018-08-07 16:49:25 -070021 "github.com/google/blueprint"
Julien Desprezeb7398e2019-02-28 08:45:28 -080022 "github.com/google/blueprint/proptools"
Colin Cross303e21f2018-08-07 16:49:25 -070023
24 "android/soong/android"
25)
26
Dan Shi20ccd212019-08-27 10:37:24 -070027const test_xml_indent = " "
28
Jack He33338892018-09-19 02:21:28 -070029func getTestConfigTemplate(ctx android.ModuleContext, prop *string) android.OptionalPath {
30 return ctx.ExpandOptionalSource(prop, "test_config_template")
31}
32
Colin Cross303e21f2018-08-07 16:49:25 -070033func 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
42var autogenTestConfig = pctx.StaticRule("autogenTestConfig", blueprint.RuleParams{
Dan Shi37ee3b82019-06-06 16:23:32 -070043 Command: "sed 's&{MODULE}&${name}&g;s&{EXTRA_CONFIGS}&'${extraConfigs}'&g' $template > $out",
Colin Cross303e21f2018-08-07 16:49:25 -070044 CommandDeps: []string{"$template"},
Dan Shi37ee3b82019-06-06 16:23:32 -070045}, "name", "template", "extraConfigs")
Colin Cross303e21f2018-08-07 16:49:25 -070046
Dan Shi6ffaaa82019-09-26 11:41:36 -070047func 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 Cross303e21f2018-08-07 16:49:25 -070050 return p, nil
Dan Shi6ffaaa82019-09-26 11:41:36 -070051 } else if !android.InList("cts", testSuites) && BoolDefault(autoGenConfig, true) {
Colin Cross303e21f2018-08-07 16:49:25 -070052 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".config")
Jack He33338892018-09-19 02:21:28 -070053 return nil, outputFile
Colin Cross303e21f2018-08-07 16:49:25 -070054 } 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 Shi37ee3b82019-06-06 16:23:32 -070062type Config interface {
63 Config() string
64}
65
66type Option struct {
67 Name string
68 Value string
69}
70
71var _ Config = Option{}
72
73func (o Option) Config() string {
74 return fmt.Sprintf(`<option name="%s" value="%s" />`, o.Name, o.Value)
75}
76
nelsonli0d7111e2019-09-17 16:35:23 +080077// It can be a template of object or target_preparer.
78type Object struct {
79 // Set it as a target_preparer if object type == "target_preparer".
80 Type string
Dan Shi20ccd212019-08-27 10:37:24 -070081 Class string
82 Options []Option
Dan Shi37ee3b82019-06-06 16:23:32 -070083}
84
nelsonli0d7111e2019-09-17 16:35:23 +080085var _ Config = Object{}
Dan Shi37ee3b82019-06-06 16:23:32 -070086
nelsonli0d7111e2019-09-17 16:35:23 +080087func (ob Object) Config() string {
Dan Shi20ccd212019-08-27 10:37:24 -070088 var optionStrings []string
nelsonli0d7111e2019-09-17 16:35:23 +080089 for _, option := range ob.Options {
Dan Shi20ccd212019-08-27 10:37:24 -070090 optionStrings = append(optionStrings, option.Config())
91 }
92 var options string
nelsonli0d7111e2019-09-17 16:35:23 +080093 if len(ob.Options) == 0 {
Dan Shi20ccd212019-08-27 10:37:24 -070094 options = ""
95 } else {
96 optionDelimiter := fmt.Sprintf("\\n%s%s", test_xml_indent, test_xml_indent)
97 options = optionDelimiter + strings.Join(optionStrings, optionDelimiter)
98 }
nelsonli0d7111e2019-09-17 16:35:23 +080099 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 Shi37ee3b82019-06-06 16:23:32 -0700105}
106
107func 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 Desprezeb7398e2019-02-28 08:45:28 -0800111 }
Dan Shi20ccd212019-08-27 10:37:24 -0700112 extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent))
Dan Shi37ee3b82019-06-06 16:23:32 -0700113 extraConfigs = proptools.NinjaAndShellEscape(extraConfigs)
Julien Desprezeb7398e2019-02-28 08:45:28 -0800114
Colin Cross303e21f2018-08-07 16:49:25 -0700115 ctx.Build(pctx, android.BuildParams{
116 Rule: autogenTestConfig,
117 Description: "test config",
118 Output: output,
119 Args: map[string]string{
Julien Desprezeb7398e2019-02-28 08:45:28 -0800120 "name": ctx.ModuleName(),
121 "template": template,
Dan Shi37ee3b82019-06-06 16:23:32 -0700122 "extraConfigs": extraConfigs,
Colin Cross303e21f2018-08-07 16:49:25 -0700123 },
124 })
125}
126
Jack He33338892018-09-19 02:21:28 -0700127func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700128 testConfigTemplateProp *string, testSuites []string, config []Config, autoGenConfig *bool) android.Path {
129 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig)
Colin Cross303e21f2018-08-07 16:49:25 -0700130 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700131 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
132 if templatePath.Valid() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700133 autogenTemplate(ctx, autogenPath, templatePath.String(), config)
Colin Cross303e21f2018-08-07 16:49:25 -0700134 } else {
Jack He33338892018-09-19 02:21:28 -0700135 if ctx.Device() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700136 autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}", config)
Jack He33338892018-09-19 02:21:28 -0700137 } else {
Dan Shi37ee3b82019-06-06 16:23:32 -0700138 autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}", config)
Jack He33338892018-09-19 02:21:28 -0700139 }
Colin Cross303e21f2018-08-07 16:49:25 -0700140 }
Jack He33338892018-09-19 02:21:28 -0700141 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700142 }
143 return path
144}
145
Jack He33338892018-09-19 02:21:28 -0700146func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700147 testConfigTemplateProp *string, testSuites []string, configs []Config, autoGenConfig *bool) android.Path {
148 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig)
Colin Cross303e21f2018-08-07 16:49:25 -0700149 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700150 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
151 if templatePath.Valid() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700152 autogenTemplate(ctx, autogenPath, templatePath.String(), configs)
Jack He33338892018-09-19 02:21:28 -0700153 } else {
Dan Shi37ee3b82019-06-06 16:23:32 -0700154 autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", configs)
Jack He33338892018-09-19 02:21:28 -0700155 }
156 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700157 }
158 return path
159}
160
Dan Shi6ffaaa82019-09-26 11:41:36 -0700161func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string,
162 testSuites []string, autoGenConfig *bool) android.Path {
163 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig)
Colin Cross303e21f2018-08-07 16:49:25 -0700164 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700165 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
166 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800167 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700168 } else {
Jack He33338892018-09-19 02:21:28 -0700169 if ctx.Device() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800170 autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700171 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800172 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700173 }
Colin Cross303e21f2018-08-07 16:49:25 -0700174 }
Jack He33338892018-09-19 02:21:28 -0700175 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700176 }
177 return path
178}
179
yelinhsieh80880a32018-11-06 11:49:55 +0800180func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi6ffaaa82019-09-26 11:41:36 -0700181 testConfigTemplateProp *string, testSuites []string, autoGenConfig *bool) android.Path {
yelinhsieh80880a32018-11-06 11:49:55 +0800182
Dan Shi6ffaaa82019-09-26 11:41:36 -0700183 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites, autoGenConfig)
yelinhsieh80880a32018-11-06 11:49:55 +0800184 if autogenPath != nil {
185 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
186 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800187 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800188 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800189 autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}", nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800190 }
191 return autogenPath
192 }
193 return path
194}
195
Colin Cross303e21f2018-08-07 16:49:25 -0700196var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
Jack He33338892018-09-19 02:21:28 -0700197 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template",
Colin Cross303e21f2018-08-07 16:49:25 -0700198 CommandDeps: []string{
199 "${AutoGenTestConfigScript}",
200 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700201 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700202 },
Jack He33338892018-09-19 02:21:28 -0700203}, "name", "template")
Colin Cross303e21f2018-08-07 16:49:25 -0700204
Dan Shi6ffaaa82019-09-26 11:41:36 -0700205func 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 Cross303e21f2018-08-07 16:49:25 -0700208 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700209 template := "${InstrumentationTestConfigTemplate}"
210 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
211 if moduleTemplate.Valid() {
212 template = moduleTemplate.String()
213 }
Colin Cross303e21f2018-08-07 16:49:25 -0700214 ctx.Build(pctx, android.BuildParams{
215 Rule: autogenInstrumentationTest,
216 Description: "test config",
217 Input: manifest,
218 Output: autogenPath,
219 Args: map[string]string{
Jack He33338892018-09-19 02:21:28 -0700220 "name": ctx.ModuleName(),
221 "template": template,
Colin Cross303e21f2018-08-07 16:49:25 -0700222 },
223 })
Jack He33338892018-09-19 02:21:28 -0700224 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700225 }
226 return path
227}
Dan Shi6ffaaa82019-09-26 11:41:36 -0700228
229var Bool = proptools.Bool
230var BoolDefault = proptools.BoolDefault