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)
47
48func GatherRequiredDepsForTest() string {
49	bp := `
50		rust_prebuilt_library {
51				name: "libstd_x86_64-unknown-linux-gnu",
52                                crate_name: "std",
53                                rlib: {
54                                    srcs: ["libstd.rlib"],
55                                },
56                                dylib: {
57                                    srcs: ["libstd.so"],
58                                },
59				host_supported: true,
60				sysroot: true,
61		}
62		rust_prebuilt_library {
63				name: "libtest_x86_64-unknown-linux-gnu",
64                                crate_name: "test",
65                                rlib: {
66                                    srcs: ["libtest.rlib"],
67                                },
68                                dylib: {
69                                    srcs: ["libtest.so"],
70                                },
71				host_supported: true,
72				sysroot: true,
73		}
74		rust_prebuilt_library {
75				name: "libstd_i686-unknown-linux-gnu",
76                                crate_name: "std",
77                                rlib: {
78                                    srcs: ["libstd.rlib"],
79                                },
80                                dylib: {
81                                    srcs: ["libstd.so"],
82                                },
83				host_supported: true,
84				sysroot: true,
85		}
86		rust_prebuilt_library {
87				name: "libtest_i686-unknown-linux-gnu",
88                                crate_name: "test",
89                                rlib: {
90                                    srcs: ["libtest.rlib"],
91                                },
92                                dylib: {
93                                    srcs: ["libtest.so"],
94                                },
95				host_supported: true,
96				sysroot: true,
97		}
98		rust_prebuilt_library {
99				name: "libstd_x86_64-apple-darwin",
100                                crate_name: "std",
101                                rlib: {
102                                    srcs: ["libstd.rlib"],
103                                },
104                                dylib: {
105                                    srcs: ["libstd.so"],
106                                },
107				host_supported: true,
108				sysroot: true,
109		}
110		rust_prebuilt_library {
111				name: "libtest_x86_64-apple-darwin",
112                                crate_name: "test",
113                                rlib: {
114                                    srcs: ["libtest.rlib"],
115                                },
116                                dylib: {
117                                    srcs: ["libtest.so"],
118                                },
119				host_supported: true,
120				sysroot: true,
121		}
122		//////////////////////////////
123		// Device module requirements
124
125		cc_library {
126			name: "liblog",
127			no_libcrt: true,
128			nocrt: true,
129			system_shared_libs: [],
130			apex_available: ["//apex_available:platform", "//apex_available:anyapex"],
131			min_sdk_version: "29",
132			vendor_available: true,
133		}
134		cc_library {
135			name: "libprotobuf-cpp-full",
136			no_libcrt: true,
137			nocrt: true,
138			system_shared_libs: [],
139			export_include_dirs: ["libprotobuf-cpp-full-includes"],
140		}
141		cc_library {
142			name: "libclang_rt.asan-aarch64-android",
143			no_libcrt: true,
144			nocrt: true,
145			system_shared_libs: [],
146			export_include_dirs: ["libprotobuf-cpp-full-includes"],
147		}
148		rust_library {
149			name: "libstd",
150			crate_name: "std",
151			srcs: ["foo.rs"],
152			no_stdlibs: true,
153			host_supported: true,
154			vendor_available: true,
155			vendor_ramdisk_available: true,
156			native_coverage: false,
157			sysroot: true,
158			apex_available: ["//apex_available:platform", "//apex_available:anyapex"],
159			min_sdk_version: "29",
160		}
161		rust_library {
162			name: "libtest",
163			crate_name: "test",
164			srcs: ["foo.rs"],
165			host_supported: true,
166			vendor_available: true,
167			vendor_ramdisk_available: true,
168			native_coverage: false,
169			apex_available: ["//apex_available:platform", "//apex_available:anyapex"],
170			min_sdk_version: "29",
171		}
172		rust_library {
173			name: "libprotobuf",
174			crate_name: "protobuf",
175			srcs: ["foo.rs"],
176			host_supported: true,
177		}
178		rust_library {
179			name: "libgrpcio",
180			crate_name: "grpcio",
181			srcs: ["foo.rs"],
182			host_supported: true,
183		}
184		rust_library {
185			name: "libfutures",
186			crate_name: "futures",
187			srcs: ["foo.rs"],
188			host_supported: true,
189		}
190		rust_library {
191			name: "liblibfuzzer_sys",
192			crate_name: "libfuzzer_sys",
193			srcs:["foo.rs"],
194			host_supported: true,
195		}
196		rust_library {
197			name: "libcriterion",
198			crate_name: "criterion",
199			srcs:["foo.rs"],
200			host_supported: true,
201		}
202`
203	return bp
204}
205
206func registerRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
207	ctx.RegisterModuleType("rust_benchmark", RustBenchmarkFactory)
208	ctx.RegisterModuleType("rust_benchmark_host", RustBenchmarkHostFactory)
209	ctx.RegisterModuleType("rust_binary", RustBinaryFactory)
210	ctx.RegisterModuleType("rust_binary_host", RustBinaryHostFactory)
211	ctx.RegisterModuleType("rust_bindgen", RustBindgenFactory)
212	ctx.RegisterModuleType("rust_bindgen_host", RustBindgenHostFactory)
213	ctx.RegisterModuleType("rust_test", RustTestFactory)
214	ctx.RegisterModuleType("rust_test_host", RustTestHostFactory)
215	ctx.RegisterModuleType("rust_library", RustLibraryFactory)
216	ctx.RegisterModuleType("rust_library_dylib", RustLibraryDylibFactory)
217	ctx.RegisterModuleType("rust_library_rlib", RustLibraryRlibFactory)
218	ctx.RegisterModuleType("rust_library_host", RustLibraryHostFactory)
219	ctx.RegisterModuleType("rust_library_host_dylib", RustLibraryDylibHostFactory)
220	ctx.RegisterModuleType("rust_library_host_rlib", RustLibraryRlibHostFactory)
221	ctx.RegisterModuleType("rust_fuzz", RustFuzzFactory)
222	ctx.RegisterModuleType("rust_ffi", RustFFIFactory)
223	ctx.RegisterModuleType("rust_ffi_shared", RustFFISharedFactory)
224	ctx.RegisterModuleType("rust_ffi_static", RustFFIStaticFactory)
225	ctx.RegisterModuleType("rust_ffi_host", RustFFIHostFactory)
226	ctx.RegisterModuleType("rust_ffi_host_shared", RustFFISharedHostFactory)
227	ctx.RegisterModuleType("rust_ffi_host_static", RustFFIStaticHostFactory)
228	ctx.RegisterModuleType("rust_proc_macro", ProcMacroFactory)
229	ctx.RegisterModuleType("rust_protobuf", RustProtobufFactory)
230	ctx.RegisterModuleType("rust_protobuf_host", RustProtobufHostFactory)
231	ctx.RegisterModuleType("rust_prebuilt_library", PrebuiltLibraryFactory)
232	ctx.RegisterModuleType("rust_prebuilt_dylib", PrebuiltDylibFactory)
233	ctx.RegisterModuleType("rust_prebuilt_rlib", PrebuiltRlibFactory)
234	ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
235		// rust mutators
236		ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
237		ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
238		ctx.BottomUp("rust_begin", BeginMutator).Parallel()
239	})
240	ctx.RegisterSingletonType("rust_project_generator", rustProjectGeneratorSingleton)
241}
242