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