blob: cfa7164bba6e3c202c80be792193756be2eb4080 [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 (
Colin Cross303e21f2018-08-07 16:49:25 -070018 "github.com/google/blueprint"
19
20 "android/soong/android"
21)
22
Jack He33338892018-09-19 02:21:28 -070023func getTestConfigTemplate(ctx android.ModuleContext, prop *string) android.OptionalPath {
24 return ctx.ExpandOptionalSource(prop, "test_config_template")
25}
26
Colin Cross303e21f2018-08-07 16:49:25 -070027func getTestConfig(ctx android.ModuleContext, prop *string) android.Path {
28 if p := ctx.ExpandOptionalSource(prop, "test_config"); p.Valid() {
29 return p.Path()
30 } else if p := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "AndroidTest.xml"); p.Valid() {
31 return p.Path()
32 }
33 return nil
34}
35
36var autogenTestConfig = pctx.StaticRule("autogenTestConfig", blueprint.RuleParams{
Bill Yangfd18c422018-10-25 05:36:13 +000037 Command: "sed 's&{MODULE}&${name}&g' $template > $out",
Colin Cross303e21f2018-08-07 16:49:25 -070038 CommandDeps: []string{"$template"},
Bill Yangfd18c422018-10-25 05:36:13 +000039}, "name", "template")
Colin Cross303e21f2018-08-07 16:49:25 -070040
yangbill4f41bc22019-02-13 21:45:47 +080041func testConfigPath(ctx android.ModuleContext, prop *string, testSuites []string) (path android.Path, autogenPath android.WritablePath) {
Colin Cross303e21f2018-08-07 16:49:25 -070042 if p := getTestConfig(ctx, prop); p != nil {
43 return p, nil
yangbill4f41bc22019-02-13 21:45:47 +080044 } else if !android.InList("cts", testSuites) {
Colin Cross303e21f2018-08-07 16:49:25 -070045 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".config")
Jack He33338892018-09-19 02:21:28 -070046 return nil, outputFile
Colin Cross303e21f2018-08-07 16:49:25 -070047 } else {
48 // CTS modules can be used for test data, so test config files must be
49 // explicitly created using AndroidTest.xml
50 // TODO(b/112602712): remove the path check
51 return nil, nil
52 }
53}
54
Bill Yangfd18c422018-10-25 05:36:13 +000055func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string) {
Colin Cross303e21f2018-08-07 16:49:25 -070056 ctx.Build(pctx, android.BuildParams{
57 Rule: autogenTestConfig,
58 Description: "test config",
59 Output: output,
60 Args: map[string]string{
Bill Yangfd18c422018-10-25 05:36:13 +000061 "name": ctx.ModuleName(),
62 "template": template,
Colin Cross303e21f2018-08-07 16:49:25 -070063 },
64 })
65}
66
Jack He33338892018-09-19 02:21:28 -070067func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
yangbill4f41bc22019-02-13 21:45:47 +080068 testConfigTemplateProp *string, testSuites []string) android.Path {
69 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -070070 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -070071 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
72 if templatePath.Valid() {
Bill Yangfd18c422018-10-25 05:36:13 +000073 autogenTemplate(ctx, autogenPath, templatePath.String())
Colin Cross303e21f2018-08-07 16:49:25 -070074 } else {
Jack He33338892018-09-19 02:21:28 -070075 if ctx.Device() {
Bill Yangfd18c422018-10-25 05:36:13 +000076 autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}")
Jack He33338892018-09-19 02:21:28 -070077 } else {
Bill Yangfd18c422018-10-25 05:36:13 +000078 autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}")
Jack He33338892018-09-19 02:21:28 -070079 }
Colin Cross303e21f2018-08-07 16:49:25 -070080 }
Jack He33338892018-09-19 02:21:28 -070081 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -070082 }
83 return path
84}
85
Jack He33338892018-09-19 02:21:28 -070086func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string,
yangbill4f41bc22019-02-13 21:45:47 +080087 testConfigTemplateProp *string, testSuites []string) android.Path {
88 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -070089 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -070090 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
91 if templatePath.Valid() {
Bill Yangfd18c422018-10-25 05:36:13 +000092 autogenTemplate(ctx, autogenPath, templatePath.String())
Jack He33338892018-09-19 02:21:28 -070093 } else {
Bill Yangfd18c422018-10-25 05:36:13 +000094 autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}")
Jack He33338892018-09-19 02:21:28 -070095 }
96 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -070097 }
98 return path
99}
100
yangbill4f41bc22019-02-13 21:45:47 +0800101func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, testSuites []string) android.Path {
102 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700103 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700104 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
105 if templatePath.Valid() {
Bill Yangfd18c422018-10-25 05:36:13 +0000106 autogenTemplate(ctx, autogenPath, templatePath.String())
Colin Cross303e21f2018-08-07 16:49:25 -0700107 } else {
Jack He33338892018-09-19 02:21:28 -0700108 if ctx.Device() {
Bill Yangfd18c422018-10-25 05:36:13 +0000109 autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}")
Jack He33338892018-09-19 02:21:28 -0700110 } else {
Bill Yangfd18c422018-10-25 05:36:13 +0000111 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}")
Jack He33338892018-09-19 02:21:28 -0700112 }
Colin Cross303e21f2018-08-07 16:49:25 -0700113 }
Jack He33338892018-09-19 02:21:28 -0700114 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700115 }
116 return path
117}
118
yelinhsieh80880a32018-11-06 11:49:55 +0800119func AutoGenPythonBinaryHostTestConfig(ctx android.ModuleContext, testConfigProp *string,
yangbill4f41bc22019-02-13 21:45:47 +0800120 testConfigTemplateProp *string, testSuites []string) android.Path {
yelinhsieh80880a32018-11-06 11:49:55 +0800121
yangbill4f41bc22019-02-13 21:45:47 +0800122 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
yelinhsieh80880a32018-11-06 11:49:55 +0800123 if autogenPath != nil {
124 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
125 if templatePath.Valid() {
126 autogenTemplate(ctx, autogenPath, templatePath.String())
127 } else {
128 autogenTemplate(ctx, autogenPath, "${PythonBinaryHostTestConfigTemplate}")
129 }
130 return autogenPath
131 }
132 return path
133}
134
Colin Cross303e21f2018-08-07 16:49:25 -0700135var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
Jack He33338892018-09-19 02:21:28 -0700136 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template",
Colin Cross303e21f2018-08-07 16:49:25 -0700137 CommandDeps: []string{
138 "${AutoGenTestConfigScript}",
139 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700140 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700141 },
Jack He33338892018-09-19 02:21:28 -0700142}, "name", "template")
Colin Cross303e21f2018-08-07 16:49:25 -0700143
yangbill4f41bc22019-02-13 21:45:47 +0800144func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, manifest android.Path, testSuites []string) android.Path {
145 path, autogenPath := testConfigPath(ctx, testConfigProp, testSuites)
Colin Cross303e21f2018-08-07 16:49:25 -0700146 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700147 template := "${InstrumentationTestConfigTemplate}"
148 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
149 if moduleTemplate.Valid() {
150 template = moduleTemplate.String()
151 }
Colin Cross303e21f2018-08-07 16:49:25 -0700152 ctx.Build(pctx, android.BuildParams{
153 Rule: autogenInstrumentationTest,
154 Description: "test config",
155 Input: manifest,
156 Output: autogenPath,
157 Args: map[string]string{
Jack He33338892018-09-19 02:21:28 -0700158 "name": ctx.ModuleName(),
159 "template": template,
Colin Cross303e21f2018-08-07 16:49:25 -0700160 },
161 })
Jack He33338892018-09-19 02:21:28 -0700162 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700163 }
164 return path
165}