benchmark.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2020 The Android Open Source Project
  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 rust
  15. import (
  16. "android/soong/android"
  17. "android/soong/tradefed"
  18. )
  19. type BenchmarkProperties struct {
  20. // Disables the creation of a test-specific directory when used with
  21. // relative_install_path. Useful if several tests need to be in the same
  22. // directory, but test_per_src doesn't work.
  23. No_named_install_directory *bool
  24. // the name of the test configuration (for example "AndroidBenchmark.xml") that should be
  25. // installed with the module.
  26. Test_config *string `android:"path,arch_variant"`
  27. // the name of the test configuration template (for example "AndroidBenchmarkTemplate.xml") that
  28. // should be installed with the module.
  29. Test_config_template *string `android:"path,arch_variant"`
  30. // list of compatibility suites (for example "cts", "vts") that the module should be
  31. // installed into.
  32. Test_suites []string `android:"arch_variant"`
  33. // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
  34. // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
  35. // explicitly.
  36. Auto_gen_config *bool
  37. }
  38. type benchmarkDecorator struct {
  39. *binaryDecorator
  40. Properties BenchmarkProperties
  41. testConfig android.Path
  42. }
  43. func NewRustBenchmark(hod android.HostOrDeviceSupported) (*Module, *benchmarkDecorator) {
  44. // Build both 32 and 64 targets for device benchmarks.
  45. // Cannot build both for host benchmarks yet if the benchmark depends on
  46. // something like proc-macro2 that cannot be built for both.
  47. multilib := android.MultilibBoth
  48. if hod != android.DeviceSupported && hod != android.HostAndDeviceSupported {
  49. multilib = android.MultilibFirst
  50. }
  51. module := newModule(hod, multilib)
  52. benchmark := &benchmarkDecorator{
  53. binaryDecorator: &binaryDecorator{
  54. baseCompiler: NewBaseCompiler("nativebench", "nativebench64", InstallInData),
  55. },
  56. }
  57. module.compiler = benchmark
  58. module.AddProperties(&benchmark.Properties)
  59. return module, benchmark
  60. }
  61. func init() {
  62. android.RegisterModuleType("rust_benchmark", RustBenchmarkFactory)
  63. android.RegisterModuleType("rust_benchmark_host", RustBenchmarkHostFactory)
  64. }
  65. func RustBenchmarkFactory() android.Module {
  66. module, _ := NewRustBenchmark(android.HostAndDeviceSupported)
  67. return module.Init()
  68. }
  69. func RustBenchmarkHostFactory() android.Module {
  70. module, _ := NewRustBenchmark(android.HostSupported)
  71. return module.Init()
  72. }
  73. func (benchmark *benchmarkDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
  74. return rlibAutoDep
  75. }
  76. func (benchmark *benchmarkDecorator) stdLinkage(ctx *depsContext) RustLinkage {
  77. return RlibLinkage
  78. }
  79. func (benchmark *benchmarkDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
  80. flags = benchmark.binaryDecorator.compilerFlags(ctx, flags)
  81. return flags
  82. }
  83. func (benchmark *benchmarkDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
  84. deps = benchmark.binaryDecorator.compilerDeps(ctx, deps)
  85. deps.Rustlibs = append(deps.Rustlibs, "libtest")
  86. deps.Rustlibs = append(deps.Rustlibs, "libcriterion")
  87. return deps
  88. }
  89. func (benchmark *benchmarkDecorator) compilerProps() []interface{} {
  90. return append(benchmark.binaryDecorator.compilerProps(), &benchmark.Properties)
  91. }
  92. func (benchmark *benchmarkDecorator) install(ctx ModuleContext) {
  93. benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
  94. TestConfigProp: benchmark.Properties.Test_config,
  95. TestConfigTemplateProp: benchmark.Properties.Test_config_template,
  96. TestSuites: benchmark.Properties.Test_suites,
  97. AutoGenConfig: benchmark.Properties.Auto_gen_config,
  98. DeviceTemplate: "${RustDeviceBenchmarkConfigTemplate}",
  99. HostTemplate: "${RustHostBenchmarkConfigTemplate}",
  100. })
  101. // default relative install path is module name
  102. if !Bool(benchmark.Properties.No_named_install_directory) {
  103. benchmark.baseCompiler.relative = ctx.ModuleName()
  104. } else if String(benchmark.baseCompiler.Properties.Relative_install_path) == "" {
  105. ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
  106. }
  107. benchmark.binaryDecorator.install(ctx)
  108. }