blob: 1400345e5901c97ddef40c0db6d9a0829e9ebe73 [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
yangbill4f41bc22019-02-13 21:45:47 +080047func testConfigPath(ctx android.ModuleContext, prop *string, testSuites []string) (path android.Path, autogenPath android.WritablePath) {
Colin Cross303e21f2018-08-07 16:49:25 -070048 if p := getTestConfig(ctx, prop); p != nil {
49 return p, nil
yangbill4f41bc22019-02-13 21:45:47 +080050 } else if !android.InList("cts", testSuites) {
Colin Cross303e21f2018-08-07 16:49:25 -070051 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".config")
Jack He33338892018-09-19 02:21:28 -070052 return nil, outputFile
Colin Cross303e21f2018-08-07 16:49:25 -070053 } 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 Shi37ee3b82019-06-06 16:23:32 -070061type Config interface {
62 Config() string
63}
64
65type Option struct {
66 Name string
67 Value string
68}
69
70var _ Config = Option{}
71
72func (o Option) Config() string {
73 return fmt.Sprintf(`<option name="%s" value="%s" />`, o.Name, o.Value)
74}
75
nelsonli0d7111e2019-09-17 16:35:23 +080076// It can be a template of object or target_preparer.
77type Object struct {
78 // Set it as a target_preparer if object type == "target_preparer".
79 Type string
Dan Shi20ccd212019-08-27 10:37:24 -070080 Class string
81 Options []Option
Dan Shi37ee3b82019-06-06 16:23:32 -070082}
83
nelsonli0d7111e2019-09-17 16:35:23 +080084var _ Config = Object{}
Dan Shi37ee3b82019-06-06 16:23:32 -070085
nelsonli0d7111e2019-09-17 16:35:23 +080086func (ob Object) Config() string {
Dan Shi20ccd212019-08-27 10:37:24 -070087 var optionStrings []string
nelsonli0d7111e2019-09-17 16:35:23 +080088 for _, option := range ob.Options {
Dan Shi20ccd212019-08-27 10:37:24 -070089 optionStrings = append(optionStrings, option.Config())
90 }
91 var options string
nelsonli0d7111e2019-09-17 16:35:23 +080092 if len(ob.Options) == 0 {
Dan Shi20ccd212019-08-27 10:37:24 -070093 options = ""
94 } else {
95 optionDelimiter := fmt.Sprintf("\\n%s%s", test_xml_indent, test_xml_indent)
96 options = optionDelimiter + strings.Join(optionStrings, optionDelimiter)
97 }
nelsonli0d7111e2019-09-17 16:35:23 +080098 if ob.Type == "target_preparer" {
99 return fmt.Sprintf(`<target_preparer class="%s">%s\n%s</target_preparer>`, ob.Class, options, test_xml_indent)
100 } else {
101 return fmt.Sprintf(`<object type="%s" class="%s">%s\n%s</object>`, ob.Type, ob.Class, options, test_xml_indent)
102 }
103
Dan Shi37ee3b82019-06-06 16:23:32 -0700104}
105
106func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string, configs []Config) {
107 var configStrings []string
108 for _, config := range configs {
109 configStrings = append(configStrings, config.Config())
Julien Desprezeb7398e2019-02-28 08:45:28 -0800110 }
Dan Shi20ccd212019-08-27 10:37:24 -0700111 extraConfigs := strings.Join(configStrings, fmt.Sprintf("\\n%s", test_xml_indent))
Dan Shi37ee3b82019-06-06 16:23:32 -0700112 extraConfigs = proptools.NinjaAndShellEscape(extraConfigs)
Julien Desprezeb7398e2019-02-28 08:45:28 -0800113
Colin Cross303e21f2018-08-07 16:49:25 -0700114 ctx.Build(pctx, android.BuildParams{
115 Rule: autogenTestConfig,
116 Description: "test config",
117 Output: output,
118 Args: map[string]string{
Julien Desprezeb7398e2019-02-28 08:45:28 -0800119 "name": ctx.ModuleName(),
120 "template": template,
Dan Shi37ee3b82019-06-06 16:23:32 -0700121 "extraConfigs": extraConfigs,
Colin Cross303e21f2018-08-07 16:49:25 -0700122 },
123 })
124}
125
Jack He33338892018-09-19 02:21:28 -0700126func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi37ee3b82019-06-06 16:23:32 -0700127 testConfigTemplateProp *string, testSuites []string, config []Config) android.Path {
yangbill4f41bc22019-02-13 21:45:47 +0800128 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700129 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700130 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
131 if templatePath.Valid() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700132 autogenTemplate(ctx, autogenPath, templatePath.String(), config)
Colin Cross303e21f2018-08-07 16:49:25 -0700133 } else {
Jack He33338892018-09-19 02:21:28 -0700134 if ctx.Device() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700135 autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}", config)
Jack He33338892018-09-19 02:21:28 -0700136 } else {
Dan Shi37ee3b82019-06-06 16:23:32 -0700137 autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}", config)
Jack He33338892018-09-19 02:21:28 -0700138 }
Colin Cross303e21f2018-08-07 16:49:25 -0700139 }
Jack He33338892018-09-19 02:21:28 -0700140 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700141 }
142 return path
143}
144
Jack He33338892018-09-19 02:21:28 -0700145func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi37ee3b82019-06-06 16:23:32 -0700146 testConfigTemplateProp *string, testSuites []string, configs []Config) android.Path {
yangbill4f41bc22019-02-13 21:45:47 +0800147 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700148 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700149 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
150 if templatePath.Valid() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700151 autogenTemplate(ctx, autogenPath, templatePath.String(), configs)
Jack He33338892018-09-19 02:21:28 -0700152 } else {
Dan Shi37ee3b82019-06-06 16:23:32 -0700153 autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", configs)
Jack He33338892018-09-19 02:21:28 -0700154 }
155 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700156 }
157 return path
158}
159
yangbill4f41bc22019-02-13 21:45:47 +0800160func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, testSuites []string) android.Path {
161 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700162 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700163 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
164 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800165 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700166 } else {
Jack He33338892018-09-19 02:21:28 -0700167 if ctx.Device() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800168 autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700169 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800170 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700171 }
Colin Cross303e21f2018-08-07 16:49:25 -0700172 }
Jack He33338892018-09-19 02:21:28 -0700173 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700174 }
175 return path
176}
177
yelinhsieh80880a32018-11-06 11:49:55 +0800178func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string,
yangbill4f41bc22019-02-13 21:45:47 +0800179 testConfigTemplateProp *string, testSuites []string) android.Path {
yelinhsieh80880a32018-11-06 11:49:55 +0800180
yangbill4f41bc22019-02-13 21:45:47 +0800181 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
yelinhsieh80880a32018-11-06 11:49:55 +0800182 if autogenPath != nil {
183 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
184 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800185 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800186 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800187 autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}", nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800188 }
189 return autogenPath
190 }
191 return path
192}
193
Colin Cross303e21f2018-08-07 16:49:25 -0700194var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
Jack He33338892018-09-19 02:21:28 -0700195 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template",
Colin Cross303e21f2018-08-07 16:49:25 -0700196 CommandDeps: []string{
197 "${AutoGenTestConfigScript}",
198 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700199 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700200 },
Jack He33338892018-09-19 02:21:28 -0700201}, "name", "template")
Colin Cross303e21f2018-08-07 16:49:25 -0700202
yangbill4f41bc22019-02-13 21:45:47 +0800203func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, manifest android.Path, testSuites []string) android.Path {
204 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700205 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700206 template := "${InstrumentationTestConfigTemplate}"
207 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
208 if moduleTemplate.Valid() {
209 template = moduleTemplate.String()
210 }
Colin Cross303e21f2018-08-07 16:49:25 -0700211 ctx.Build(pctx, android.BuildParams{
212 Rule: autogenInstrumentationTest,
213 Description: "test config",
214 Input: manifest,
215 Output: autogenPath,
216 Args: map[string]string{
Jack He33338892018-09-19 02:21:28 -0700217 "name": ctx.ModuleName(),
218 "template": template,
Colin Cross303e21f2018-08-07 16:49:25 -0700219 },
220 })
Jack He33338892018-09-19 02:21:28 -0700221 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700222 }
223 return path
224}