xref: /aosp_15_r20/build/soong/rust/benchmark.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2020 The Android Open Source Project
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage rust
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
19*333d2b36SAndroid Build Coastguard Worker	"android/soong/tradefed"
20*333d2b36SAndroid Build Coastguard Worker)
21*333d2b36SAndroid Build Coastguard Worker
22*333d2b36SAndroid Build Coastguard Workertype BenchmarkProperties struct {
23*333d2b36SAndroid Build Coastguard Worker	// Disables the creation of a test-specific directory when used with
24*333d2b36SAndroid Build Coastguard Worker	// relative_install_path. Useful if several tests need to be in the same
25*333d2b36SAndroid Build Coastguard Worker	// directory.
26*333d2b36SAndroid Build Coastguard Worker	No_named_install_directory *bool
27*333d2b36SAndroid Build Coastguard Worker
28*333d2b36SAndroid Build Coastguard Worker	// the name of the test configuration (for example "AndroidBenchmark.xml") that should be
29*333d2b36SAndroid Build Coastguard Worker	// installed with the module.
30*333d2b36SAndroid Build Coastguard Worker	Test_config *string `android:"path,arch_variant"`
31*333d2b36SAndroid Build Coastguard Worker
32*333d2b36SAndroid Build Coastguard Worker	// the name of the test configuration template (for example "AndroidBenchmarkTemplate.xml") that
33*333d2b36SAndroid Build Coastguard Worker	// should be installed with the module.
34*333d2b36SAndroid Build Coastguard Worker	Test_config_template *string `android:"path,arch_variant"`
35*333d2b36SAndroid Build Coastguard Worker
36*333d2b36SAndroid Build Coastguard Worker	// list of compatibility suites (for example "cts", "vts") that the module should be
37*333d2b36SAndroid Build Coastguard Worker	// installed into.
38*333d2b36SAndroid Build Coastguard Worker	Test_suites []string `android:"arch_variant"`
39*333d2b36SAndroid Build Coastguard Worker
40*333d2b36SAndroid Build Coastguard Worker	// Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
41*333d2b36SAndroid Build Coastguard Worker	// doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
42*333d2b36SAndroid Build Coastguard Worker	// explicitly.
43*333d2b36SAndroid Build Coastguard Worker	Auto_gen_config *bool
44*333d2b36SAndroid Build Coastguard Worker}
45*333d2b36SAndroid Build Coastguard Worker
46*333d2b36SAndroid Build Coastguard Workertype benchmarkDecorator struct {
47*333d2b36SAndroid Build Coastguard Worker	*binaryDecorator
48*333d2b36SAndroid Build Coastguard Worker	Properties BenchmarkProperties
49*333d2b36SAndroid Build Coastguard Worker	testConfig android.Path
50*333d2b36SAndroid Build Coastguard Worker}
51*333d2b36SAndroid Build Coastguard Worker
52*333d2b36SAndroid Build Coastguard Workerfunc NewRustBenchmark(hod android.HostOrDeviceSupported) (*Module, *benchmarkDecorator) {
53*333d2b36SAndroid Build Coastguard Worker	// Build both 32 and 64 targets for device benchmarks.
54*333d2b36SAndroid Build Coastguard Worker	// Cannot build both for host benchmarks yet if the benchmark depends on
55*333d2b36SAndroid Build Coastguard Worker	// something like proc-macro2 that cannot be built for both.
56*333d2b36SAndroid Build Coastguard Worker	multilib := android.MultilibBoth
57*333d2b36SAndroid Build Coastguard Worker	if hod != android.DeviceSupported && hod != android.HostAndDeviceSupported {
58*333d2b36SAndroid Build Coastguard Worker		multilib = android.MultilibFirst
59*333d2b36SAndroid Build Coastguard Worker	}
60*333d2b36SAndroid Build Coastguard Worker	module := newModule(hod, multilib)
61*333d2b36SAndroid Build Coastguard Worker
62*333d2b36SAndroid Build Coastguard Worker	benchmark := &benchmarkDecorator{
63*333d2b36SAndroid Build Coastguard Worker		binaryDecorator: &binaryDecorator{
64*333d2b36SAndroid Build Coastguard Worker			baseCompiler: NewBaseCompiler("nativebench", "nativebench64", InstallInData),
65*333d2b36SAndroid Build Coastguard Worker		},
66*333d2b36SAndroid Build Coastguard Worker	}
67*333d2b36SAndroid Build Coastguard Worker
68*333d2b36SAndroid Build Coastguard Worker	module.compiler = benchmark
69*333d2b36SAndroid Build Coastguard Worker	module.AddProperties(&benchmark.Properties)
70*333d2b36SAndroid Build Coastguard Worker	return module, benchmark
71*333d2b36SAndroid Build Coastguard Worker}
72*333d2b36SAndroid Build Coastguard Worker
73*333d2b36SAndroid Build Coastguard Workerfunc init() {
74*333d2b36SAndroid Build Coastguard Worker	android.RegisterModuleType("rust_benchmark", RustBenchmarkFactory)
75*333d2b36SAndroid Build Coastguard Worker	android.RegisterModuleType("rust_benchmark_host", RustBenchmarkHostFactory)
76*333d2b36SAndroid Build Coastguard Worker}
77*333d2b36SAndroid Build Coastguard Worker
78*333d2b36SAndroid Build Coastguard Workerfunc RustBenchmarkFactory() android.Module {
79*333d2b36SAndroid Build Coastguard Worker	module, _ := NewRustBenchmark(android.HostAndDeviceSupported)
80*333d2b36SAndroid Build Coastguard Worker	return module.Init()
81*333d2b36SAndroid Build Coastguard Worker}
82*333d2b36SAndroid Build Coastguard Worker
83*333d2b36SAndroid Build Coastguard Workerfunc RustBenchmarkHostFactory() android.Module {
84*333d2b36SAndroid Build Coastguard Worker	module, _ := NewRustBenchmark(android.HostSupported)
85*333d2b36SAndroid Build Coastguard Worker	return module.Init()
86*333d2b36SAndroid Build Coastguard Worker}
87*333d2b36SAndroid Build Coastguard Worker
88*333d2b36SAndroid Build Coastguard Workerfunc (benchmark *benchmarkDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
89*333d2b36SAndroid Build Coastguard Worker	return rlibAutoDep
90*333d2b36SAndroid Build Coastguard Worker}
91*333d2b36SAndroid Build Coastguard Worker
92*333d2b36SAndroid Build Coastguard Workerfunc (benchmark *benchmarkDecorator) stdLinkage(ctx *depsContext) RustLinkage {
93*333d2b36SAndroid Build Coastguard Worker	return RlibLinkage
94*333d2b36SAndroid Build Coastguard Worker}
95*333d2b36SAndroid Build Coastguard Worker
96*333d2b36SAndroid Build Coastguard Workerfunc (benchmark *benchmarkDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
97*333d2b36SAndroid Build Coastguard Worker	flags = benchmark.binaryDecorator.compilerFlags(ctx, flags)
98*333d2b36SAndroid Build Coastguard Worker	return flags
99*333d2b36SAndroid Build Coastguard Worker}
100*333d2b36SAndroid Build Coastguard Worker
101*333d2b36SAndroid Build Coastguard Workerfunc (benchmark *benchmarkDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
102*333d2b36SAndroid Build Coastguard Worker	deps = benchmark.binaryDecorator.compilerDeps(ctx, deps)
103*333d2b36SAndroid Build Coastguard Worker
104*333d2b36SAndroid Build Coastguard Worker	deps.Rustlibs = append(deps.Rustlibs, "libtest")
105*333d2b36SAndroid Build Coastguard Worker	deps.Rustlibs = append(deps.Rustlibs, "libcriterion")
106*333d2b36SAndroid Build Coastguard Worker
107*333d2b36SAndroid Build Coastguard Worker	return deps
108*333d2b36SAndroid Build Coastguard Worker}
109*333d2b36SAndroid Build Coastguard Worker
110*333d2b36SAndroid Build Coastguard Workerfunc (benchmark *benchmarkDecorator) compilerProps() []interface{} {
111*333d2b36SAndroid Build Coastguard Worker	return append(benchmark.binaryDecorator.compilerProps(), &benchmark.Properties)
112*333d2b36SAndroid Build Coastguard Worker}
113*333d2b36SAndroid Build Coastguard Worker
114*333d2b36SAndroid Build Coastguard Workerfunc (benchmark *benchmarkDecorator) install(ctx ModuleContext) {
115*333d2b36SAndroid Build Coastguard Worker	benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
116*333d2b36SAndroid Build Coastguard Worker		TestConfigProp:         benchmark.Properties.Test_config,
117*333d2b36SAndroid Build Coastguard Worker		TestConfigTemplateProp: benchmark.Properties.Test_config_template,
118*333d2b36SAndroid Build Coastguard Worker		TestSuites:             benchmark.Properties.Test_suites,
119*333d2b36SAndroid Build Coastguard Worker		AutoGenConfig:          benchmark.Properties.Auto_gen_config,
120*333d2b36SAndroid Build Coastguard Worker		DeviceTemplate:         "${RustDeviceBenchmarkConfigTemplate}",
121*333d2b36SAndroid Build Coastguard Worker		HostTemplate:           "${RustHostBenchmarkConfigTemplate}",
122*333d2b36SAndroid Build Coastguard Worker	})
123*333d2b36SAndroid Build Coastguard Worker
124*333d2b36SAndroid Build Coastguard Worker	// default relative install path is module name
125*333d2b36SAndroid Build Coastguard Worker	if !Bool(benchmark.Properties.No_named_install_directory) {
126*333d2b36SAndroid Build Coastguard Worker		benchmark.baseCompiler.relative = ctx.ModuleName()
127*333d2b36SAndroid Build Coastguard Worker	} else if String(benchmark.baseCompiler.Properties.Relative_install_path) == "" {
128*333d2b36SAndroid Build Coastguard Worker		ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
129*333d2b36SAndroid Build Coastguard Worker	}
130*333d2b36SAndroid Build Coastguard Worker
131*333d2b36SAndroid Build Coastguard Worker	benchmark.binaryDecorator.install(ctx)
132*333d2b36SAndroid Build Coastguard Worker}
133