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
15package rust
16
17import (
18	"android/soong/android"
19	"android/soong/tradefed"
20)
21
22type BenchmarkProperties struct {
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
25	// directory, but test_per_src doesn't work.
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
44}
45
46type benchmarkDecorator struct {
47	*binaryDecorator
48	Properties BenchmarkProperties
49	testConfig android.Path
50}
51
52func 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
73func init() {
74	android.RegisterModuleType("rust_benchmark", RustBenchmarkFactory)
75	android.RegisterModuleType("rust_benchmark_host", RustBenchmarkHostFactory)
76}
77
78func RustBenchmarkFactory() android.Module {
79	module, _ := NewRustBenchmark(android.HostAndDeviceSupported)
80	return module.Init()
81}
82
83func RustBenchmarkHostFactory() android.Module {
84	module, _ := NewRustBenchmark(android.HostSupported)
85	return module.Init()
86}
87
88func (benchmark *benchmarkDecorator) autoDep(ctx android.BottomUpMutatorContext) autoDep {
89	return rlibAutoDep
90}
91
92func (benchmark *benchmarkDecorator) stdLinkage(ctx *depsContext) RustLinkage {
93	return RlibLinkage
94}
95
96func (benchmark *benchmarkDecorator) compilerFlags(ctx ModuleContext, flags Flags) Flags {
97	flags = benchmark.binaryDecorator.compilerFlags(ctx, flags)
98	return flags
99}
100
101func (benchmark *benchmarkDecorator) compilerDeps(ctx DepsContext, deps Deps) Deps {
102	deps = benchmark.binaryDecorator.compilerDeps(ctx, deps)
103
104	deps.Rustlibs = append(deps.Rustlibs, "libtest")
105	deps.Rustlibs = append(deps.Rustlibs, "libcriterion")
106
107	return deps
108}
109
110func (benchmark *benchmarkDecorator) compilerProps() []interface{} {
111	return append(benchmark.binaryDecorator.compilerProps(), &benchmark.Properties)
112}
113
114func (benchmark *benchmarkDecorator) install(ctx ModuleContext) {
115	benchmark.testConfig = tradefed.AutoGenRustBenchmarkConfig(ctx,
116		benchmark.Properties.Test_config,
117		benchmark.Properties.Test_config_template,
118		benchmark.Properties.Test_suites,
119		nil,
120		benchmark.Properties.Auto_gen_config)
121
122	// default relative install path is module name
123	if !Bool(benchmark.Properties.No_named_install_directory) {
124		benchmark.baseCompiler.relative = ctx.ModuleName()
125	} else if String(benchmark.baseCompiler.Properties.Relative_install_path) == "" {
126		ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
127	}
128
129	benchmark.binaryDecorator.install(ctx)
130}
131