Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package tradefed |
| 16 | |
| 17 | import ( |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 18 | "fmt" |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 19 | "strings" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | |
| 23 | "android/soong/android" |
| 24 | ) |
| 25 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 26 | func getTestConfigTemplate(ctx android.ModuleContext, prop *string) android.OptionalPath { |
| 27 | return ctx.ExpandOptionalSource(prop, "test_config_template") |
| 28 | } |
| 29 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 30 | func 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 | |
| 39 | var autogenTestConfig = pctx.StaticRule("autogenTestConfig", blueprint.RuleParams{ |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 40 | Command: "sed 's&{MODULE}&${name}&g' $template > $out &&" + |
| 41 | "${optionCmd} $out", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 42 | CommandDeps: []string{"$template"}, |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 43 | }, "name", "template", "optionCmd") |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 44 | |
| 45 | func 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 He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 50 | return nil, outputFile |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 51 | } 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 | |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 59 | func 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 Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 73 | ctx.Build(pctx, android.BuildParams{ |
| 74 | Rule: autogenTestConfig, |
| 75 | Description: "test config", |
| 76 | Output: output, |
| 77 | Args: map[string]string{ |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 78 | "name": ctx.ModuleName(), |
| 79 | "template": template, |
| 80 | "optionCmd": optionCmd, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 81 | }, |
| 82 | }) |
| 83 | } |
| 84 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 85 | func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string, |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 86 | testConfigTemplateProp *string, optionsMap map[string]string) android.Path { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 87 | path, autogenPath := testConfigPath(ctx, testConfigProp) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 88 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 89 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 90 | if templatePath.Valid() { |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 91 | autogenTemplate(ctx, autogenPath, templatePath.String(), optionsMap) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 92 | } else { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 93 | if ctx.Device() { |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 94 | autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}", optionsMap) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 95 | } else { |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 96 | autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}", optionsMap) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 97 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 98 | } |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 99 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 100 | } |
| 101 | return path |
| 102 | } |
| 103 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 104 | func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string, |
| 105 | testConfigTemplateProp *string) android.Path { |
| 106 | path, autogenPath := testConfigPath(ctx, testConfigProp) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 107 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 108 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 109 | if templatePath.Valid() { |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 110 | autogenTemplate(ctx, autogenPath, templatePath.String(), nil) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 111 | } else { |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 112 | autogenTemplate(ctx, autogenPath, "${NativeBenchmarkTestConfigTemplate}", nil) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 113 | } |
| 114 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 115 | } |
| 116 | return path |
| 117 | } |
| 118 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 119 | func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string) android.Path { |
| 120 | path, autogenPath := testConfigPath(ctx, testConfigProp) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 121 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 122 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 123 | if templatePath.Valid() { |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 124 | autogenTemplate(ctx, autogenPath, templatePath.String(), nil) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 125 | } else { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 126 | if ctx.Device() { |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 127 | autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}", nil) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 128 | } else { |
yelinhsieh | d30b940 | 2018-10-01 19:23:14 +0800 | [diff] [blame^] | 129 | autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}", nil) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 130 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 131 | } |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 132 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 133 | } |
| 134 | return path |
| 135 | } |
| 136 | |
| 137 | var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{ |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 138 | Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 139 | CommandDeps: []string{ |
| 140 | "${AutoGenTestConfigScript}", |
| 141 | "${EmptyTestConfig}", |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 142 | "$template", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 143 | }, |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 144 | }, "name", "template") |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 145 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 146 | func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, manifest android.Path) android.Path { |
| 147 | path, autogenPath := testConfigPath(ctx, testConfigProp) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 148 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 149 | template := "${InstrumentationTestConfigTemplate}" |
| 150 | moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 151 | if moduleTemplate.Valid() { |
| 152 | template = moduleTemplate.String() |
| 153 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 154 | ctx.Build(pctx, android.BuildParams{ |
| 155 | Rule: autogenInstrumentationTest, |
| 156 | Description: "test config", |
| 157 | Input: manifest, |
| 158 | Output: autogenPath, |
| 159 | Args: map[string]string{ |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 160 | "name": ctx.ModuleName(), |
| 161 | "template": template, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 162 | }, |
| 163 | }) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame] | 164 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 165 | } |
| 166 | return path |
| 167 | } |