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