tradefed_binary.go 4.8 KB

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