blob: 5fb85c3ca69fa78c8142251e4b7e8bc9f421f4a9 [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 (
yelinhsiehd30b9402018-10-01 19:23:14 +080018 "fmt"
Colin Cross303e21f2018-08-07 16:49:25 -070019 "strings"
20
21 "github.com/google/blueprint"
22
23 "android/soong/android"
24)
25
Jack He33338892018-09-19 02:21:28 -070026func getTestConfigTemplate(ctx android.ModuleContext, prop *string) android.OptionalPath {
27 return ctx.ExpandOptionalSource(prop, "test_config_template")
28}
29
Colin Cross303e21f2018-08-07 16:49:25 -070030func getTestConfig(ctx android.ModuleContext, prop *string) android.Path {
31 if p := ctx.ExpandOptionalSource(prop, "test_config"); p.Valid() {
32 return p.Path()
33 } else if p := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "AndroidTest.xml"); p.Valid() {
34 return p.Path()
35 }
36 return nil
37}
38
39var autogenTestConfig = pctx.StaticRule("autogenTestConfig", blueprint.RuleParams{
yelinhsiehd30b9402018-10-01 19:23:14 +080040 Command: "sed 's&{MODULE}&${name}&g' $template > $out &&" +
41 "${optionCmd} $out",
Colin Cross303e21f2018-08-07 16:49:25 -070042 CommandDeps: []string{"$template"},
yelinhsiehd30b9402018-10-01 19:23:14 +080043}, "name", "template", "optionCmd")
Colin Cross303e21f2018-08-07 16:49:25 -070044
45func testConfigPath(ctx android.ModuleContext, prop *string) (path android.Path, autogenPath android.WritablePath) {
46 if p := getTestConfig(ctx, prop); p != nil {
47 return p, nil
48 } else if !strings.HasPrefix(ctx.ModuleDir(), "cts/") {
49 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
yelinhsiehd30b9402018-10-01 19:23:14 +080059func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string, optionsMap map[string]string) {
60 // If no test option found, delete {UID_OPTION} line.
61 // If found, replace it with corresponding options format.
62 optionCmd := "sed -i '/{UID_OPTION}/d'"
63 if optionsMap != nil {
64 //Append options
65 var options []string
66 for optionName, value := range optionsMap {
67 if value != "" {
68 options = append(options, fmt.Sprintf("<option name=\"%s\" value=\"%s\" />", optionName, value))
69 }
70 }
71 optionCmd = fmt.Sprintf("sed -i 's&{UID_OPTION}&%s&g'", strings.Join(options, "\\n "))
72 }
Colin Cross303e21f2018-08-07 16:49:25 -070073 ctx.Build(pctx, android.BuildParams{
74 Rule: autogenTestConfig,
75 Description: "test config",
76 Output: output,
77 Args: map[string]string{
yelinhsiehd30b9402018-10-01 19:23:14 +080078 "name": ctx.ModuleName(),
79 "template": template,
80 "optionCmd": optionCmd,
Colin Cross303e21f2018-08-07 16:49:25 -070081 },
82 })
83}
84
Jack He33338892018-09-19 02:21:28 -070085func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string,
yelinhsiehd30b9402018-10-01 19:23:14 +080086 testConfigTemplateProp *string, optionsMap map[string]string) android.Path {
Jack He33338892018-09-19 02:21:28 -070087 path, autogenPath := testConfigPath(ctx, testConfigProp)
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() {
yelinhsiehd30b9402018-10-01 19:23:14 +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() {
yelinhsiehd30b9402018-10-01 19:23:14 +080094 autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}", optionsMap)
Jack He33338892018-09-19 02:21:28 -070095 } else {
yelinhsiehd30b9402018-10-01 19:23:14 +080096 autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}", optionsMap)
Jack He33338892018-09-19 02:21:28 -070097 }
Colin Cross303e21f2018-08-07 16:49:25 -070098 }
Jack He33338892018-09-19 02:21:28 -070099 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700100 }
101 return path
102}
103
Jack He33338892018-09-19 02:21:28 -0700104func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string,
105 testConfigTemplateProp *string) android.Path {
106 path, autogenPath := testConfigPath(ctx, testConfigProp)
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() {
yelinhsiehd30b9402018-10-01 19:23:14 +0800110 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
Jack He33338892018-09-19 02:21:28 -0700111 } else {
yelinhsiehd30b9402018-10-01 19:23:14 +0800112 autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700113 }
114 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700115 }
116 return path
117}
118
Jack He33338892018-09-19 02:21:28 -0700119func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string) android.Path {
120 path, autogenPath := testConfigPath(ctx, testConfigProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700121 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700122 templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp)
123 if templatePath.Valid() {
yelinhsiehd30b9402018-10-01 19:23:14 +0800124 autogenTemplate(ctx, autogenPath, templatePath.String(), nil)
Colin Cross303e21f2018-08-07 16:49:25 -0700125 } else {
Jack He33338892018-09-19 02:21:28 -0700126 if ctx.Device() {
yelinhsiehd30b9402018-10-01 19:23:14 +0800127 autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700128 } else {
yelinhsiehd30b9402018-10-01 19:23:14 +0800129 autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil)
Jack He33338892018-09-19 02:21:28 -0700130 }
Colin Cross303e21f2018-08-07 16:49:25 -0700131 }
Jack He33338892018-09-19 02:21:28 -0700132 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700133 }
134 return path
135}
136
137var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{
Jack He33338892018-09-19 02:21:28 -0700138 Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template",
Colin Cross303e21f2018-08-07 16:49:25 -0700139 CommandDeps: []string{
140 "${AutoGenTestConfigScript}",
141 "${EmptyTestConfig}",
Jack He33338892018-09-19 02:21:28 -0700142 "$template",
Colin Cross303e21f2018-08-07 16:49:25 -0700143 },
Jack He33338892018-09-19 02:21:28 -0700144}, "name", "template")
Colin Cross303e21f2018-08-07 16:49:25 -0700145
Jack He33338892018-09-19 02:21:28 -0700146func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, manifest android.Path) android.Path {
147 path, autogenPath := testConfigPath(ctx, testConfigProp)
Colin Cross303e21f2018-08-07 16:49:25 -0700148 if autogenPath != nil {
Jack He33338892018-09-19 02:21:28 -0700149 template := "${InstrumentationTestConfigTemplate}"
150 moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp)
151 if moduleTemplate.Valid() {
152 template = moduleTemplate.String()
153 }
Colin Cross303e21f2018-08-07 16:49:25 -0700154 ctx.Build(pctx, android.BuildParams{
155 Rule: autogenInstrumentationTest,
156 Description: "test config",
157 Input: manifest,
158 Output: autogenPath,
159 Args: map[string]string{
Jack He33338892018-09-19 02:21:28 -0700160 "name": ctx.ModuleName(),
161 "template": template,
Colin Cross303e21f2018-08-07 16:49:25 -0700162 },
163 })
Jack He33338892018-09-19 02:21:28 -0700164 return autogenPath
Colin Cross303e21f2018-08-07 16:49:25 -0700165 }
166 return path
167}