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 ( |
| 18 | "strings" |
| 19 | |
| 20 | "github.com/google/blueprint" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 25 | func getTestConfigTemplate(ctx android.ModuleContext, prop *string) android.OptionalPath { |
| 26 | return ctx.ExpandOptionalSource(prop, "test_config_template") |
| 27 | } |
| 28 | |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 29 | func 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 | |
| 38 | var autogenTestConfig = pctx.StaticRule("autogenTestConfig", blueprint.RuleParams{ |
| 39 | Command: "sed 's&{MODULE}&${name}&g' $template > $out", |
| 40 | CommandDeps: []string{"$template"}, |
| 41 | }, "name", "template") |
| 42 | |
| 43 | func 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 He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 48 | return nil, outputFile |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 49 | } 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 | |
| 57 | func 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 He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 69 | func AutoGenNativeTestConfig(ctx android.ModuleContext, testConfigProp *string, |
| 70 | testConfigTemplateProp *string) android.Path { |
| 71 | path, autogenPath := testConfigPath(ctx, testConfigProp) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 72 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 73 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 74 | if templatePath.Valid() { |
| 75 | autogenTemplate(ctx, autogenPath, templatePath.String()) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 76 | } else { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 77 | if ctx.Device() { |
| 78 | autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}") |
| 79 | } else { |
| 80 | autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}") |
| 81 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 82 | } |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 83 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 84 | } |
| 85 | return path |
| 86 | } |
| 87 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 88 | func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, testConfigProp *string, |
| 89 | testConfigTemplateProp *string) android.Path { |
| 90 | path, autogenPath := testConfigPath(ctx, testConfigProp) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 91 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 92 | 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 Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 99 | } |
| 100 | return path |
| 101 | } |
| 102 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 103 | func AutoGenJavaTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string) android.Path { |
| 104 | path, autogenPath := testConfigPath(ctx, testConfigProp) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 105 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 106 | templatePath := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 107 | if templatePath.Valid() { |
| 108 | autogenTemplate(ctx, autogenPath, templatePath.String()) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 109 | } else { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 110 | if ctx.Device() { |
| 111 | autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}") |
| 112 | } else { |
| 113 | autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}") |
| 114 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 115 | } |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 116 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 117 | } |
| 118 | return path |
| 119 | } |
| 120 | |
| 121 | var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{ |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 122 | Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} $template", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 123 | CommandDeps: []string{ |
| 124 | "${AutoGenTestConfigScript}", |
| 125 | "${EmptyTestConfig}", |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 126 | "$template", |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 127 | }, |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 128 | }, "name", "template") |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 129 | |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 130 | func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, testConfigProp *string, testConfigTemplateProp *string, manifest android.Path) android.Path { |
| 131 | path, autogenPath := testConfigPath(ctx, testConfigProp) |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 132 | if autogenPath != nil { |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 133 | template := "${InstrumentationTestConfigTemplate}" |
| 134 | moduleTemplate := getTestConfigTemplate(ctx, testConfigTemplateProp) |
| 135 | if moduleTemplate.Valid() { |
| 136 | template = moduleTemplate.String() |
| 137 | } |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 138 | ctx.Build(pctx, android.BuildParams{ |
| 139 | Rule: autogenInstrumentationTest, |
| 140 | Description: "test config", |
| 141 | Input: manifest, |
| 142 | Output: autogenPath, |
| 143 | Args: map[string]string{ |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 144 | "name": ctx.ModuleName(), |
| 145 | "template": template, |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 146 | }, |
| 147 | }) |
Jack He | 3333889 | 2018-09-19 02:21:28 -0700 | [diff] [blame^] | 148 | return autogenPath |
Colin Cross | 303e21f | 2018-08-07 16:49:25 -0700 | [diff] [blame] | 149 | } |
| 150 | return path |
| 151 | } |