blob: 952b02236f25badb3cb49d94d01301d636842f55 [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
Jack He33338892018-09-19 02:21:28 -070027func getTestConfigTemplate(ctx android.ModuleContext, prop *string) android.OptionalPath {
28 return ctx.ExpandOptionalSource(prop, "test_config_template")
29}
30
Colin Cross303e21f2018-08-07 16:49:25 -070031func 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
40var autogenTestConfig = pctx.StaticRule("autogenTestConfig", blueprint.RuleParams{
Dan Shi37ee3b82019-06-06 16:23:32 -070041 Command: "sed 's&{MODULE}&${name}&g;s&{EXTRA_CONFIGS}&'${extraConfigs}'&g' $template > $out",
Colin Cross303e21f2018-08-07 16:49:25 -070042 CommandDeps: []string{"$template"},
Dan Shi37ee3b82019-06-06 16:23:32 -070043}, "name", "template", "extraConfigs")
Colin Cross303e21f2018-08-07 16:49:25 -070044
yangbill4f41bc22019-02-13 21:45:47 +080045func testConfigPath(ctx android.ModuleContext, prop *string, testSuites []string) (path android.Path, autogenPath android.WritablePath) {
Colin Cross303e21f2018-08-07 16:49:25 -070046 if p := getTestConfig(ctx, prop); p != nil {
47 return p, nil
yangbill4f41bc22019-02-13 21:45:47 +080048 } else if !android.InList("cts", testSuites) {
Colin Cross303e21f2018-08-07 16:49:25 -070049 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".config")
Jack He33338892018-09-19 02:21:28 -070050 return nil, outputFile
Colin Cross303e21f2018-08-07 16:49:25 -070051 } 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 Shi37ee3b82019-06-06 16:23:32 -070059type Config interface {
60 Config() string
61}
62
63type Option struct {
64 Name string
65 Value string
66}
67
68var _ Config = Option{}
69
70func (o Option) Config() string {
71 return fmt.Sprintf(`<option name="%s" value="%s" />`, o.Name, o.Value)
72}
73
74type Preparer struct {
75 Class string
76}
77
78var _ Config = Preparer{}
79
80func (p Preparer) Config() string {
81 return fmt.Sprintf(`<target_preparer class="%s" />`, p.Class)
82}
83
84func 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 Desprezeb7398e2019-02-28 08:45:28 -080088 }
Dan Shi37ee3b82019-06-06 16:23:32 -070089 extraConfigs := strings.Join(configStrings, "\n ")
90 extraConfigs = proptools.NinjaAndShellEscape(extraConfigs)
Julien Desprezeb7398e2019-02-28 08:45:28 -080091
Colin Cross303e21f2018-08-07 16:49:25 -070092 ctx.Build(pctx, android.BuildParams{
93 Rule: autogenTestConfig,
94 Description: "test config",
95 Output: output,
96 Args: map[string]string{
Julien Desprezeb7398e2019-02-28 08:45:28 -080097 "name": ctx.ModuleName(),
98 "template": template,
Dan Shi37ee3b82019-06-06 16:23:32 -070099 "extraConfigs": extraConfigs,
Colin Cross303e21f2018-08-07 16:49:25 -0700100 },
101 })
102}
103
Jack He33338892018-09-19 02:21:28 -0700104func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi37ee3b82019-06-06 16:23:32 -0700105 testConfigTemplateProp *string, testSuites []string, config []Config) android.Path {
yangbill4f41bc22019-02-13 21:45:47 +0800106 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700107 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700108 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
109 if templatePath.Valid() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700110 autogenTemplate(ctx, autogenPath, templatePath.String(), config)
Colin Cross303e21f2018-08-07 16:49:25 -0700111 } else {
Jack He33338892018-09-19 02:21:28 -0700112 if ctx.Device() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700113 autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}", config)
Jack He33338892018-09-19 02:21:28 -0700114 } else {
Dan Shi37ee3b82019-06-06 16:23:32 -0700115 autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}", config)
Jack He33338892018-09-19 02:21:28 -0700116 }
Colin Cross303e21f2018-08-07 16:49:25 -0700117 }
Jack He33338892018-09-19 02:21:28 -0700118 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700119 }
120 return path
121}
122
Jack He33338892018-09-19 02:21:28 -0700123func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string,
Dan Shi37ee3b82019-06-06 16:23:32 -0700124 testConfigTemplateProp *string, testSuites []string, configs []Config) android.Path {
yangbill4f41bc22019-02-13 21:45:47 +0800125 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700126 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700127 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
128 if templatePath.Valid() {
Dan Shi37ee3b82019-06-06 16:23:32 -0700129 autogenTemplate(ctx, autogenPath, templatePath.String(), configs)
Jack He33338892018-09-19 02:21:28 -0700130 } else {
Dan Shi37ee3b82019-06-06 16:23:32 -0700131 autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", configs)
Jack He33338892018-09-19 02:21:28 -0700132 }
133 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700134 }
135 return path
136}
137
yangbill4f41bc22019-02-13 21:45:47 +0800138func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, testSuites []string) android.Path {
139 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700140 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700141 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
142 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800143 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700144 } else {
Jack He33338892018-09-19 02:21:28 -0700145 if ctx.Device() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800146 autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700147 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800148 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700149 }
Colin Cross303e21f2018-08-07 16:49:25 -0700150 }
Jack He33338892018-09-19 02:21:28 -0700151 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700152 }
153 return path
154}
155
yelinhsieh80880a32018-11-06 11:49:55 +0800156func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string,
yangbill4f41bc22019-02-13 21:45:47 +0800157 testConfigTemplateProp *string, testSuites []string) android.Path {
yelinhsieh80880a32018-11-06 11:49:55 +0800158
yangbill4f41bc22019-02-13 21:45:47 +0800159 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
yelinhsieh80880a32018-11-06 11:49:55 +0800160 if autogenPath != nil {
161 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
162 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800163 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800164 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800165 autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}", nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800166 }
167 return autogenPath
168 }
169 return path
170}
171
Colin Cross303e21f2018-08-07 16:49:25 -0700172var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
Jack He33338892018-09-19 02:21:28 -0700173 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template",
Colin Cross303e21f2018-08-07 16:49:25 -0700174 CommandDeps: []string{
175 "${AutoGenTestConfigScript}",
176 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700177 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700178 },
Jack He33338892018-09-19 02:21:28 -0700179}, "name", "template")
Colin Cross303e21f2018-08-07 16:49:25 -0700180
yangbill4f41bc22019-02-13 21:45:47 +0800181func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, manifest android.Path, testSuites []string) android.Path {
182 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700183 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700184 template := "${InstrumentationTestConfigTemplate}"
185 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
186 if moduleTemplate.Valid() {
187 template = moduleTemplate.String()
188 }
Colin Cross303e21f2018-08-07 16:49:25 -0700189 ctx.Build(pctx, android.BuildParams{
190 Rule: autogenInstrumentationTest,
191 Description: "test config",
192 Input: manifest,
193 Output: autogenPath,
194 Args: map[string]string{
Jack He33338892018-09-19 02:21:28 -0700195 "name": ctx.ModuleName(),
196 "template": template,
Colin Cross303e21f2018-08-07 16:49:25 -0700197 },
198 })
Jack He33338892018-09-19 02:21:28 -0700199 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700200 }
201 return path
202}