Colin Cross | 91b2979 | 2018-08-16 18:16:12 +0000 | [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 suite_harness |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | |
| 20 | "github.com/google/blueprint" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | "android/soong/java" |
| 24 | ) |
| 25 | |
| 26 | var pctx = android.NewPackageContext("android/soong/suite_harness") |
| 27 | |
| 28 | func init() { |
| 29 | android.RegisterModuleType("tradefed_binary_host", tradefedBinaryFactory) |
| 30 | |
Colin Cross | c091aa4 | 2019-04-02 16:23:14 -0700 | [diff] [blame] | 31 | pctx.Import("android/soong/android") |
Colin Cross | 91b2979 | 2018-08-16 18:16:12 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type TradefedBinaryProperties struct { |
| 35 | Short_name string |
| 36 | Full_name string |
| 37 | Version string |
Christopher Dombroski | 0f08b04 | 2020-05-19 12:02:08 -0700 | [diff] [blame] | 38 | Prepend_platform_version_name bool |
Colin Cross | 91b2979 | 2018-08-16 18:16:12 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // tradefedBinaryFactory creates an empty module for the tradefed_binary module type, |
| 42 | // which is a java_binary with some additional processing in tradefedBinaryLoadHook. |
| 43 | func tradefedBinaryFactory() android.Module { |
| 44 | props := &TradefedBinaryProperties{} |
| 45 | module := java.BinaryHostFactory() |
| 46 | module.AddProperties(props) |
| 47 | android.AddLoadHook(module, tradefedBinaryLoadHook(props)) |
| 48 | |
| 49 | return module |
| 50 | } |
| 51 | |
| 52 | const genSuffix = "-gen" |
| 53 | |
| 54 | // tradefedBinaryLoadHook adds extra resources and libraries to tradefed_binary modules. |
| 55 | func tradefedBinaryLoadHook(tfb *TradefedBinaryProperties) func(ctx android.LoadHookContext) { |
| 56 | return func(ctx android.LoadHookContext) { |
| 57 | genName := ctx.ModuleName() + genSuffix |
Christopher Dombroski | 0f08b04 | 2020-05-19 12:02:08 -0700 | [diff] [blame] | 58 | version := tfb.Version |
| 59 | if (tfb.Prepend_platform_version_name) { |
| 60 | version = ctx.Config().PlatformVersionName() + tfb.Version |
| 61 | } |
Colin Cross | 91b2979 | 2018-08-16 18:16:12 +0000 | [diff] [blame] | 62 | |
| 63 | // Create a submodule that generates the test-suite-info.properties file |
| 64 | // and copies DynamicConfig.xml if it is present. |
Colin Cross | 9a5d383 | 2019-09-25 15:12:58 -0700 | [diff] [blame] | 65 | ctx.CreateModule(tradefedBinaryGenFactory, |
Colin Cross | 91b2979 | 2018-08-16 18:16:12 +0000 | [diff] [blame] | 66 | &TradefedBinaryGenProperties{ |
| 67 | Name: &genName, |
| 68 | Short_name: tfb.Short_name, |
| 69 | Full_name: tfb.Full_name, |
Christopher Dombroski | 0f08b04 | 2020-05-19 12:02:08 -0700 | [diff] [blame] | 70 | Version: version, |
Colin Cross | 91b2979 | 2018-08-16 18:16:12 +0000 | [diff] [blame] | 71 | }) |
| 72 | |
| 73 | props := struct { |
| 74 | Java_resources []string |
| 75 | Libs []string |
| 76 | }{} |
| 77 | |
| 78 | // Add dependencies required by all tradefed_binary modules. |
| 79 | props.Libs = []string{ |
| 80 | "tradefed", |
Julien Desprez | e0ec387 | 2020-05-18 13:31:01 -0700 | [diff] [blame] | 81 | "tradefed-test-framework", |
Colin Cross | 91b2979 | 2018-08-16 18:16:12 +0000 | [diff] [blame] | 82 | "loganalysis", |
| 83 | "hosttestlib", |
| 84 | "compatibility-host-util", |
| 85 | } |
| 86 | |
| 87 | // Add the files generated by the submodule created above to the resources. |
| 88 | props.Java_resources = []string{":" + genName} |
| 89 | |
| 90 | ctx.AppendProperties(&props) |
| 91 | |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | type TradefedBinaryGenProperties struct { |
| 96 | Name *string |
| 97 | Short_name string |
| 98 | Full_name string |
| 99 | Version string |
| 100 | } |
| 101 | |
| 102 | type tradefedBinaryGen struct { |
| 103 | android.ModuleBase |
| 104 | |
| 105 | properties TradefedBinaryGenProperties |
| 106 | |
| 107 | gen android.Paths |
| 108 | } |
| 109 | |
| 110 | func tradefedBinaryGenFactory() android.Module { |
| 111 | tfg := &tradefedBinaryGen{} |
| 112 | tfg.AddProperties(&tfg.properties) |
| 113 | android.InitAndroidModule(tfg) |
| 114 | return tfg |
| 115 | } |
| 116 | |
| 117 | func (tfg *tradefedBinaryGen) DepsMutator(android.BottomUpMutatorContext) {} |
| 118 | |
| 119 | var tradefedBinaryGenRule = pctx.StaticRule("tradefedBinaryGenRule", blueprint.RuleParams{ |
| 120 | Command: `rm -f $out && touch $out && ` + |
| 121 | `echo "# This file is auto generated by Android.mk. Do not modify." >> $out && ` + |
Colin Cross | dae4c15 | 2020-02-23 11:01:02 -0800 | [diff] [blame] | 122 | `echo "build_number = $$(cat ${buildNumberFile})" >> $out && ` + |
Colin Cross | 91b2979 | 2018-08-16 18:16:12 +0000 | [diff] [blame] | 123 | `echo "target_arch = ${arch}" >> $out && ` + |
| 124 | `echo "name = ${name}" >> $out && ` + |
| 125 | `echo "fullname = ${fullname}" >> $out && ` + |
| 126 | `echo "version = ${version}" >> $out`, |
Colin Cross | dae4c15 | 2020-02-23 11:01:02 -0800 | [diff] [blame] | 127 | }, "buildNumberFile", "arch", "name", "fullname", "version") |
Colin Cross | 91b2979 | 2018-08-16 18:16:12 +0000 | [diff] [blame] | 128 | |
| 129 | func (tfg *tradefedBinaryGen) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | dae4c15 | 2020-02-23 11:01:02 -0800 | [diff] [blame] | 130 | buildNumberFile := ctx.Config().BuildNumberFile(ctx) |
Colin Cross | 91b2979 | 2018-08-16 18:16:12 +0000 | [diff] [blame] | 131 | outputFile := android.PathForModuleOut(ctx, "test-suite-info.properties") |
| 132 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | dae4c15 | 2020-02-23 11:01:02 -0800 | [diff] [blame] | 133 | Rule: tradefedBinaryGenRule, |
| 134 | Output: outputFile, |
| 135 | OrderOnly: android.Paths{buildNumberFile}, |
Colin Cross | 91b2979 | 2018-08-16 18:16:12 +0000 | [diff] [blame] | 136 | Args: map[string]string{ |
Colin Cross | dae4c15 | 2020-02-23 11:01:02 -0800 | [diff] [blame] | 137 | "buildNumberFile": buildNumberFile.String(), |
| 138 | "arch": ctx.Config().DevicePrimaryArchType().String(), |
| 139 | "name": tfg.properties.Short_name, |
| 140 | "fullname": tfg.properties.Full_name, |
| 141 | "version": tfg.properties.Version, |
Colin Cross | 91b2979 | 2018-08-16 18:16:12 +0000 | [diff] [blame] | 142 | }, |
| 143 | }) |
| 144 | |
| 145 | tfg.gen = append(tfg.gen, outputFile) |
| 146 | |
| 147 | dynamicConfig := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "DynamicConfig.xml") |
| 148 | if dynamicConfig.Valid() { |
| 149 | outputFile := android.PathForModuleOut(ctx, strings.TrimSuffix(ctx.ModuleName(), genSuffix)+".dynamic") |
| 150 | ctx.Build(pctx, android.BuildParams{ |
| 151 | Rule: android.Cp, |
| 152 | Input: dynamicConfig.Path(), |
| 153 | Output: outputFile, |
| 154 | }) |
| 155 | |
| 156 | tfg.gen = append(tfg.gen, outputFile) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func (tfg *tradefedBinaryGen) Srcs() android.Paths { |
| 161 | return append(android.Paths(nil), tfg.gen...) |
| 162 | } |
| 163 | |
| 164 | var _ android.SourceFileProducer = (*tradefedBinaryGen)(nil) |