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 | |
| 25 | func getTestConfig(ctx android.ModuleContext, prop *string) android.Path { |
| 26 | if p := ctx.ExpandOptionalSource(prop, "test_config"); p.Valid() { |
| 27 | return p.Path() |
| 28 | } else if p := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "AndroidTest.xml"); p.Valid() { |
| 29 | return p.Path() |
| 30 | } |
| 31 | return nil |
| 32 | } |
| 33 | |
| 34 | var autogenTestConfig = pctx.StaticRule("autogenTestConfig", blueprint.RuleParams{ |
| 35 | Command: "sed 's&{MODULE}&${name}&g' $template > $out", |
| 36 | CommandDeps: []string{"$template"}, |
| 37 | }, "name", "template") |
| 38 | |
| 39 | func testConfigPath(ctx android.ModuleContext, prop *string) (path android.Path, autogenPath android.WritablePath) { |
| 40 | if p := getTestConfig(ctx, prop); p != nil { |
| 41 | return p, nil |
| 42 | } else if !strings.HasPrefix(ctx.ModuleDir(), "cts/") { |
| 43 | outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".config") |
| 44 | |
| 45 | return outputFile, outputFile |
| 46 | } else { |
| 47 | // CTS modules can be used for test data, so test config files must be |
| 48 | // explicitly created using AndroidTest.xml |
| 49 | // TODO(b/112602712): remove the path check |
| 50 | return nil, nil |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func autogenTemplate(ctx android.ModuleContext, output android.WritablePath, template string) { |
| 55 | ctx.Build(pctx, android.BuildParams{ |
| 56 | Rule: autogenTestConfig, |
| 57 | Description: "test config", |
| 58 | Output: output, |
| 59 | Args: map[string]string{ |
| 60 | "name": ctx.ModuleName(), |
| 61 | "template": template, |
| 62 | }, |
| 63 | }) |
| 64 | } |
| 65 | |
| 66 | func AutoGenNativeTestConfig(ctx android.ModuleContext, prop *string) android.Path { |
| 67 | path, autogenPath := testConfigPath(ctx, prop) |
| 68 | if autogenPath != nil { |
| 69 | if ctx.Device() { |
| 70 | autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}") |
| 71 | } else { |
| 72 | autogenTemplate(ctx, autogenPath, "${NativeHostTestConfigTemplate}") |
| 73 | } |
| 74 | } |
| 75 | return path |
| 76 | } |
| 77 | |
| 78 | func AutoGenNativeBenchmarkTestConfig(ctx android.ModuleContext, prop *string) android.Path { |
| 79 | path, autogenPath := testConfigPath(ctx, prop) |
| 80 | if autogenPath != nil { |
| 81 | autogenTemplate(ctx, autogenPath, "${NativeTestConfigTemplate}") |
| 82 | } |
| 83 | return path |
| 84 | } |
| 85 | |
| 86 | func AutoGenJavaTestConfig(ctx android.ModuleContext, prop *string) android.Path { |
| 87 | path, autogenPath := testConfigPath(ctx, prop) |
| 88 | if autogenPath != nil { |
| 89 | if ctx.Device() { |
| 90 | autogenTemplate(ctx, autogenPath, "${JavaTestConfigTemplate}") |
| 91 | } else { |
| 92 | autogenTemplate(ctx, autogenPath, "${JavaHostTestConfigTemplate}") |
| 93 | } |
| 94 | } |
| 95 | return path |
| 96 | } |
| 97 | |
| 98 | var autogenInstrumentationTest = pctx.StaticRule("autogenInstrumentationTest", blueprint.RuleParams{ |
| 99 | Command: "${AutoGenTestConfigScript} $out $in ${EmptyTestConfig} ${InstrumentationTestConfigTemplate}", |
| 100 | CommandDeps: []string{ |
| 101 | "${AutoGenTestConfigScript}", |
| 102 | "${EmptyTestConfig}", |
| 103 | "${InstrumentationTestConfigTemplate}", |
| 104 | }, |
| 105 | }, "name") |
| 106 | |
| 107 | func AutoGenInstrumentationTestConfig(ctx android.ModuleContext, prop *string, manifest android.Path) android.Path { |
| 108 | path, autogenPath := testConfigPath(ctx, prop) |
| 109 | if autogenPath != nil { |
| 110 | ctx.Build(pctx, android.BuildParams{ |
| 111 | Rule: autogenInstrumentationTest, |
| 112 | Description: "test config", |
| 113 | Input: manifest, |
| 114 | Output: autogenPath, |
| 115 | Args: map[string]string{ |
| 116 | "name": ctx.ModuleName(), |
| 117 | }, |
| 118 | }) |
| 119 | } |
| 120 | return path |
| 121 | } |