1// Copyright (C) 2019 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/bloaty"
20	"android/soong/cc"
21)
22
23// Preparer that will define all cc module types and a limited set of mutators and singletons that
24// make those module types usable.
25var PrepareForTestWithRustBuildComponents = android.GroupFixturePreparers(
26	android.FixtureRegisterWithContext(registerRequiredBuildComponentsForTest),
27)
28
29// The directory in which rust test default modules will be defined.
30//
31// Placing them here ensures that their location does not conflict with default test modules
32// defined by other packages.
33const rustDefaultsDir = "defaults/rust/"
34
35// Preparer that will define default rust modules, e.g. standard prebuilt modules.
36var PrepareForTestWithRustDefaultModules = android.GroupFixturePreparers(
37	cc.PrepareForTestWithCcDefaultModules,
38	bloaty.PrepareForTestWithBloatyDefaultModules,
39	PrepareForTestWithRustBuildComponents,
40	android.FixtureAddTextFile(rustDefaultsDir+"Android.bp", GatherRequiredDepsForTest()),
41)
42
43// Preparer that will allow use of all rust modules fully.
44var PrepareForIntegrationTestWithRust = android.GroupFixturePreparers(
45	PrepareForTestWithRustDefaultModules,
46	cc.PrepareForIntegrationTestWithCc,
47)
48
49func GatherRequiredDepsForTest() string {
50	bp := `
51		rust_prebuilt_library {
52			name: "libstd",
53			crate_name: "std",
54			rlib: {
55				srcs: ["libstd/libstd.rlib"],
56			},
57			dylib: {
58				srcs: ["libstd/libstd.so"],
59			},
60			host_supported: true,
61			sysroot: true,
62		}
63		rust_prebuilt_library {
64			name: "libcore.sysroot",
65			crate_name: "core",
66			rlib: {
67				srcs: ["libcore/libcore.rlib"],
68			},
69			dylib: {
70				srcs: ["libcore/libcore.so"],
71			},
72			host_supported: true,
73			sysroot: true,
74		}
75		//////////////////////////////
76		// Device module requirements
77
78		cc_library {
79			name: "liblog",
80			no_libcrt: true,
81			nocrt: true,
82			system_shared_libs: [],
83			apex_available: ["//apex_available:platform", "//apex_available:anyapex"],
84			min_sdk_version: "29",
85			vendor_available: true,
86			host_supported: true,
87			recovery_available: true,
88			llndk: {
89				symbol_file: "liblog.map.txt",
90			},
91		}
92		cc_library {
93			name: "libprotobuf-cpp-full",
94			no_libcrt: true,
95			nocrt: true,
96			system_shared_libs: [],
97			export_include_dirs: ["libprotobuf-cpp-full-includes"],
98		}
99		cc_library {
100			name: "libclang_rt.asan",
101			no_libcrt: true,
102			nocrt: true,
103			system_shared_libs: [],
104		}
105		cc_library {
106			name: "libclang_rt.hwasan_static",
107			no_libcrt: true,
108			nocrt: true,
109			system_shared_libs: [],
110		}
111		rust_library {
112			name: "libstd",
113			crate_name: "std",
114			srcs: ["foo.rs"],
115			no_stdlibs: true,
116			product_available: true,
117			host_supported: true,
118			vendor_available: true,
119			vendor_ramdisk_available: true,
120			recovery_available: true,
121			native_coverage: false,
122			sysroot: true,
123			apex_available: ["//apex_available:platform", "//apex_available:anyapex"],
124			min_sdk_version: "29",
125		}
126		rust_library {
127			name: "libtest",
128			crate_name: "test",
129			srcs: ["foo.rs"],
130			host_supported: true,
131			vendor_available: true,
132			vendor_ramdisk_available: true,
133			recovery_available: true,
134			native_coverage: false,
135			apex_available: ["//apex_available:platform", "//apex_available:anyapex"],
136			min_sdk_version: "29",
137		}
138		rust_library {
139			name: "libprotobuf",
140			crate_name: "protobuf",
141			srcs: ["foo.rs"],
142			host_supported: true,
143		}
144		rust_library {
145			name: "libgrpcio",
146			crate_name: "grpcio",
147			srcs: ["foo.rs"],
148			host_supported: true,
149		}
150		rust_library {
151			name: "libfutures",
152			crate_name: "futures",
153			srcs: ["foo.rs"],
154			host_supported: true,
155		}
156		rust_library {
157			name: "liblibfuzzer_sys",
158			crate_name: "libfuzzer_sys",
159			srcs:["foo.rs"],
160			host_supported: true,
161		}
162		rust_library {
163			name: "libcriterion",
164			crate_name: "criterion",
165			srcs:["foo.rs"],
166			host_supported: true,
167		}
168`
169	return bp
170}
171
172func registerRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
173	ctx.RegisterModuleType("rust_benchmark", RustBenchmarkFactory)
174	ctx.RegisterModuleType("rust_benchmark_host", RustBenchmarkHostFactory)
175	ctx.RegisterModuleType("rust_binary", RustBinaryFactory)
176	ctx.RegisterModuleType("rust_binary_host", RustBinaryHostFactory)
177	ctx.RegisterModuleType("rust_bindgen", RustBindgenFactory)
178	ctx.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory)
179	ctx.RegisterModuleType("rust_test", RustTestFactory)
180	ctx.RegisterModuleType("rust_test_host", RustTestHostFactory)
181	ctx.RegisterModuleType("rust_library", RustLibraryFactory)
182	ctx.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
183	ctx.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
184	ctx.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
185	ctx.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
186	ctx.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
187	ctx.RegisterModuleType("rust_fuzz", RustFuzzFactory)
188	ctx.RegisterModuleType("rust_fuzz_host", RustFuzzHostFactory)
189	ctx.RegisterModuleType("rust_ffi", RustFFIFactory)
190	ctx.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
191	ctx.RegisterModuleType("rust_ffi_rlib", RustFFIRlibFactory)
192	ctx.RegisterModuleType("rust_ffi_static", RustFFIStaticRlibFactory)
193	ctx.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
194	ctx.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
195	ctx.RegisterModuleType("rust_ffi_host_rlib", RustFFIRlibHostFactory)
196	ctx.RegisterModuleType("rust_ffi_host_static", RustFFIStaticRlibHostFactory)
197	ctx.RegisterModuleType("rust_proc_macro", ProcMacroFactory)
198	ctx.RegisterModuleType("rust_protobuf", RustProtobufFactory)
199	ctx.RegisterModuleType("rust_protobuf_host", RustProtobufHostFactory)
200	ctx.RegisterModuleType("rust_prebuilt_library", PrebuiltLibraryFactory)
201	ctx.RegisterModuleType("rust_prebuilt_dylib", PrebuiltDylibFactory)
202	ctx.RegisterModuleType("rust_prebuilt_rlib", PrebuiltRlibFactory)
203	ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
204		// rust mutators
205		ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
206		ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
207		ctx.BottomUp("rust_begin", BeginMutator).Parallel()
208	})
209	ctx.RegisterParallelSingletonType("rust_project_generator", rustProjectGeneratorSingleton)
210	ctx.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
211	ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
212		ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
213	})
214}
215