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{
Bill Yangfd18c422018-10-25 05:36:13 +000039 Command: "sed 's&{MODULE}&${name}&g' $template > $out",
Colin Cross303e21f2018-08-07 16:49:25 -070040 CommandDeps: []string{"$template"},
Bill Yangfd18c422018-10-25 05:36:13 +000041}, "name", "template")
Colin Cross303e21f2018-08-07 16:49:25 -070042
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
Bill Yangfd18c422018-10-25 05:36:13 +000057func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string) {
Colin Cross303e21f2018-08-07 16:49:25 -070058 ctx.Build(pctx, android.BuildParams{
59 Rule: autogenTestConfig,
60 Description: "test config",
61 Output: output,
62 Args: map[string]string{
Bill Yangfd18c422018-10-25 05:36:13 +000063 "name": ctx.ModuleName(),
64 "template": template,
Colin Cross303e21f2018-08-07 16:49:25 -070065 },
66 })
67}
68
Jack He33338892018-09-19 02:21:28 -070069func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
Bill Yangfd18c422018-10-25 05:36:13 +000070 testConfigTemplateProp *string) android.Path {
Jack He33338892018-09-19 02:21:28 -070071 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() {
Bill Yangfd18c422018-10-25 05:36:13 +000075 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() {
Bill Yangfd18c422018-10-25 05:36:13 +000078 autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}")
Jack He33338892018-09-19 02:21:28 -070079 } else {
Bill Yangfd18c422018-10-25 05:36:13 +000080 autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}")
Jack He33338892018-09-19 02:21:28 -070081 }
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() {
Bill Yangfd18c422018-10-25 05:36:13 +000094 autogenTemplate(ctx, autogenPath, templatePath.String())
Jack He33338892018-09-19 02:21:28 -070095 } else {
Bill Yangfd18c422018-10-25 05:36:13 +000096 autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}")
Jack He33338892018-09-19 02:21:28 -070097 }
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() {
Bill Yangfd18c422018-10-25 05:36:13 +0000108 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() {
Bill Yangfd18c422018-10-25 05:36:13 +0000111 autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}")
Jack He33338892018-09-19 02:21:28 -0700112 } else {
Bill Yangfd18c422018-10-25 05:36:13 +0000113 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}")
Jack He33338892018-09-19 02:21:28 -0700114 }
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}