blob: 6d9c2003e9205b4ad8498e9a3428ca25a65d2ba0 [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"
19 "sort"
20 "strings"
21
Colin Cross303e21f2018-08-07 16:49:25 -070022 "github.com/google/blueprint"
Julien Desprezeb7398e2019-02-28 08:45:28 -080023 "github.com/google/blueprint/proptools"
Colin Cross303e21f2018-08-07 16:49:25 -070024
25 "android/soong/android"
26)
27
Jack He33338892018-09-19 02:21:28 -070028func getTestConfigTemplate(ctx android.ModuleContext, prop *string) android.OptionalPath {
29 return ctx.ExpandOptionalSource(prop, "test_config_template")
30}
31
Colin Cross303e21f2018-08-07 16:49:25 -070032func 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
41var autogenTestConfig = pctx.StaticRule("autogenTestConfig", blueprint.RuleParams{
Julien Desprezeb7398e2019-02-28 08:45:28 -080042 Command: "sed 's&{MODULE}&${name}&g;s&{EXTRA_OPTIONS}&'${extraOptions}'&g' $template > $out",
Colin Cross303e21f2018-08-07 16:49:25 -070043 CommandDeps: []string{"$template"},
Julien Desprezeb7398e2019-02-28 08:45:28 -080044}, "name", "template", "extraOptions")
Colin Cross303e21f2018-08-07 16:49:25 -070045
yangbill4f41bc22019-02-13 21:45:47 +080046func testConfigPath(ctx android.ModuleContext, prop *string, testSuites []string) (path android.Path, autogenPath android.WritablePath) {
Colin Cross303e21f2018-08-07 16:49:25 -070047 if p := getTestConfig(ctx, prop); p != nil {
48 return p, nil
yangbill4f41bc22019-02-13 21:45:47 +080049 } else if !android.InList("cts", testSuites) {
Colin Cross303e21f2018-08-07 16:49:25 -070050 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".config")
Jack He33338892018-09-19 02:21:28 -070051 return nil, outputFile
Colin Cross303e21f2018-08-07 16:49:25 -070052 } 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 Desprezeb7398e2019-02-28 08:45:28 -080060func 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 Cross303e21f2018-08-07 16:49:25 -070072 ctx.Build(pctx, android.BuildParams{
73 Rule: autogenTestConfig,
74 Description: "test config",
75 Output: output,
76 Args: map[string]string{
Julien Desprezeb7398e2019-02-28 08:45:28 -080077 "name": ctx.ModuleName(),
78 "template": template,
79 "extraOptions": extraOptions,
Colin Cross303e21f2018-08-07 16:49:25 -070080 },
81 })
82}
83
Jack He33338892018-09-19 02:21:28 -070084func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
Julien Desprezeb7398e2019-02-28 08:45:28 -080085 testConfigTemplateProp *string, testSuites []string,
86 optionsMap map[string]string) android.Path {
yangbill4f41bc22019-02-13 21:45:47 +080087 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -070088 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -070089 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
90 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -080091 autogenTemplate(ctx, autogenPath, templatePath.String(), optionsMap)
Colin Cross303e21f2018-08-07 16:49:25 -070092 } else {
Jack He33338892018-09-19 02:21:28 -070093 if ctx.Device() {
Julien Desprezeb7398e2019-02-28 08:45:28 -080094 autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}",
95 optionsMap)
Jack He33338892018-09-19 02:21:28 -070096 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -080097 autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}",
98 optionsMap)
Jack He33338892018-09-19 02:21:28 -070099 }
Colin Cross303e21f2018-08-07 16:49:25 -0700100 }
Jack He33338892018-09-19 02:21:28 -0700101 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700102 }
103 return path
104}
105
Jack He33338892018-09-19 02:21:28 -0700106func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string,
yangbill4f41bc22019-02-13 21:45:47 +0800107 testConfigTemplateProp *string, testSuites []string) android.Path {
108 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700109 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700110 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
111 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800112 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
Jack He33338892018-09-19 02:21:28 -0700113 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800114 autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700115 }
116 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700117 }
118 return path
119}
120
yangbill4f41bc22019-02-13 21:45:47 +0800121func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, testSuites []string) android.Path {
122 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700123 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700124 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
125 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800126 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700127 } else {
Jack He33338892018-09-19 02:21:28 -0700128 if ctx.Device() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800129 autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700130 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800131 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700132 }
Colin Cross303e21f2018-08-07 16:49:25 -0700133 }
Jack He33338892018-09-19 02:21:28 -0700134 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700135 }
136 return path
137}
138
yelinhsieh80880a32018-11-06 11:49:55 +0800139func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string,
yangbill4f41bc22019-02-13 21:45:47 +0800140 testConfigTemplateProp *string, testSuites []string) android.Path {
yelinhsieh80880a32018-11-06 11:49:55 +0800141
yangbill4f41bc22019-02-13 21:45:47 +0800142 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
yelinhsieh80880a32018-11-06 11:49:55 +0800143 if autogenPath != nil {
144 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
145 if templatePath.Valid() {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800146 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800147 } else {
Julien Desprezeb7398e2019-02-28 08:45:28 -0800148 autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}", nil)
yelinhsieh80880a32018-11-06 11:49:55 +0800149 }
150 return autogenPath
151 }
152 return path
153}
154
Colin Cross303e21f2018-08-07 16:49:25 -0700155var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
Jack He33338892018-09-19 02:21:28 -0700156 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template",
Colin Cross303e21f2018-08-07 16:49:25 -0700157 CommandDeps: []string{
158 "${AutoGenTestConfigScript}",
159 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700160 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700161 },
Jack He33338892018-09-19 02:21:28 -0700162}, "name", "template")
Colin Cross303e21f2018-08-07 16:49:25 -0700163
yangbill4f41bc22019-02-13 21:45:47 +0800164func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, manifest android.Path, testSuites []string) android.Path {
165 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700166 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700167 template := "${InstrumentationTestConfigTemplate}"
168 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
169 if moduleTemplate.Valid() {
170 template = moduleTemplate.String()
171 }
Colin Cross303e21f2018-08-07 16:49:25 -0700172 ctx.Build(pctx, android.BuildParams{
173 Rule: autogenInstrumentationTest,
174 Description: "test config",
175 Input: manifest,
176 Output: autogenPath,
177 Args: map[string]string{
Jack He33338892018-09-19 02:21:28 -0700178 "name": ctx.ModuleName(),
179 "template": template,
Colin Cross303e21f2018-08-07 16:49:25 -0700180 },
181 })
Jack He33338892018-09-19 02:21:28 -0700182 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700183 }
184 return path
185}