blob: 131fdc44ca79df4b2cee53dd26f3d5efeae623e1 [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 (
18 "strings"
19
20 "github.com/google/blueprint"
21
22 "android/soong/android"
23)
24
Jack He33338892018-09-19 02:21:28 -070025func getTestConfigTemplate(ctx android.ModuleContext, prop *string) android.OptionalPath {
26 return ctx.ExpandOptionalSource(prop, "test_config_template")
27}
28
Colin Cross303e21f2018-08-07 16:49:25 -070029func getTestConfig(ctx android.ModuleContext, prop *string) android.Path {
30 if p := ctx.ExpandOptionalSource(prop, "test_config"); p.Valid() {
31 return p.Path()
32 } else if p := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "AndroidTest.xml"); p.Valid() {
33 return p.Path()
34 }
35 return nil
36}
37
38var autogenTestConfig = pctx.StaticRule("autogenTestConfig", blueprint.RuleParams{
39 Command: "sed 's&{MODULE}&${name}&g' $template > $out",
40 CommandDeps: []string{"$template"},
41}, "name", "template")
42
43func testConfigPath(ctx android.ModuleContext, prop *string) (path android.Path, autogenPath android.WritablePath) {
44 if p := getTestConfig(ctx, prop); p != nil {
45 return p, nil
46 } else if !strings.HasPrefix(ctx.ModuleDir(), "cts/") {
47 outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".config")
Jack He33338892018-09-19 02:21:28 -070048 return nil, outputFile
Colin Cross303e21f2018-08-07 16:49:25 -070049 } else {
50 // CTS modules can be used for test data, so test config files must be
51 // explicitly created using AndroidTest.xml
52 // TODO(b/112602712): remove the path check
53 return nil, nil
54 }
55}
56
57func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string) {
58 ctx.Build(pctx, android.BuildParams{
59 Rule: autogenTestConfig,
60 Description: "test config",
61 Output: output,
62 Args: map[string]string{
63 "name": ctx.ModuleName(),
64 "template": template,
65 },
66 })
67}
68
Jack He33338892018-09-19 02:21:28 -070069func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
70 testConfigTemplateProp *string) android.Path {
71 path, autogenPath := testConfigPath(ctx, testConfigProp)
Colin Cross303e21f2018-08-07 16:49:25 -070072 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -070073 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
74 if templatePath.Valid() {
75 autogenTemplate(ctx, autogenPath, templatePath.String())
Colin Cross303e21f2018-08-07 16:49:25 -070076 } else {
Jack He33338892018-09-19 02:21:28 -070077 if ctx.Device() {
78 autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}")
79 } else {
80 autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}")
81 }
Colin Cross303e21f2018-08-07 16:49:25 -070082 }
Jack He33338892018-09-19 02:21:28 -070083 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -070084 }
85 return path
86}
87
Jack He33338892018-09-19 02:21:28 -070088func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string,
89 testConfigTemplateProp *string) android.Path {
90 path, autogenPath := testConfigPath(ctx, testConfigProp)
Colin Cross303e21f2018-08-07 16:49:25 -070091 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -070092 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
93 if templatePath.Valid() {
94 autogenTemplate(ctx, autogenPath, templatePath.String())
95 } else {
96 autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}")
97 }
98 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -070099 }
100 return path
101}
102
Jack He33338892018-09-19 02:21:28 -0700103func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string) android.Path {
104 path, autogenPath := testConfigPath(ctx, testConfigProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700105 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700106 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
107 if templatePath.Valid() {
108 autogenTemplate(ctx, autogenPath, templatePath.String())
Colin Cross303e21f2018-08-07 16:49:25 -0700109 } else {
Jack He33338892018-09-19 02:21:28 -0700110 if ctx.Device() {
111 autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}")
112 } else {
113 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}")
114 }
Colin Cross303e21f2018-08-07 16:49:25 -0700115 }
Jack He33338892018-09-19 02:21:28 -0700116 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700117 }
118 return path
119}
120
121var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
Jack He33338892018-09-19 02:21:28 -0700122 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template",
Colin Cross303e21f2018-08-07 16:49:25 -0700123 CommandDeps: []string{
124 "${AutoGenTestConfigScript}",
125 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700126 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700127 },
Jack He33338892018-09-19 02:21:28 -0700128}, "name", "template")
Colin Cross303e21f2018-08-07 16:49:25 -0700129
Jack He33338892018-09-19 02:21:28 -0700130func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, manifest android.Path) android.Path {
131 path, autogenPath := testConfigPath(ctx, testConfigProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700132 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700133 template := "${InstrumentationTestConfigTemplate}"
134 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
135 if moduleTemplate.Valid() {
136 template = moduleTemplate.String()
137 }
Colin Cross303e21f2018-08-07 16:49:25 -0700138 ctx.Build(pctx, android.BuildParams{
139 Rule: autogenInstrumentationTest,
140 Description: "test config",
141 Input: manifest,
142 Output: autogenPath,
143 Args: map[string]string{
Jack He33338892018-09-19 02:21:28 -0700144 "name": ctx.ModuleName(),
145 "template": template,
Colin Cross303e21f2018-08-07 16:49:25 -0700146 },
147 })
Jack He33338892018-09-19 02:21:28 -0700148 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700149 }
150 return path
151}