1// Copyright (C) 2023 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
16package {
17    default_applicable_licenses: ["Android-Apache-2.0"],
18}
19
20cc_defaults {
21    name: "berberis_guest_agnostic_defaults",
22    cflags: [
23        "-Wall",
24        "-Werror",
25        "-Wextra",
26        "-Wmost",
27        "-Wover-aligned",
28        "-Wunused",
29        "-Wimplicit-fallthrough",
30        "-Werror=reorder-init-list",
31        "-Wdefaulted-function-deleted",
32    ],
33    // It is tempting to disable builds by default and explicitly enable only x86.
34    // Unfortunately that way disabling darwin has no effect. So instead
35    // we disable unneeded targets individually.
36    target: {
37        darwin: {
38            enabled: false,
39        },
40    },
41    arch: {
42        arm: {
43            enabled: false,
44        },
45        arm64: {
46            enabled: false,
47        },
48        riscv64: {
49            enabled: false,
50        },
51        x86_64: {
52            // TODO(b/123294366): disconnect guest arch from host arch!
53            cflags: [
54                "-DBERBERIS_GUEST_LP64=1",
55            ],
56        },
57    },
58    compile_multilib: "both",
59    // Only check our headers.
60    tidy_flags: [
61        "-header-filter=^frameworks/libs/binary_translation/",
62    ],
63    tidy_checks: [
64        "modernize*",
65        "-modernize-use-trailing-return-type",
66    ],
67}
68
69cc_defaults {
70    name: "berberis_defaults",
71    defaults: ["berberis_guest_agnostic_defaults"],
72    arch: {
73        x86_64: {
74            // TODO(b/123294366): disconnect guest arch from host arch!
75            cflags: ["-DNATIVE_BRIDGE_GUEST_ARCH_RISCV64"],
76        },
77    },
78}
79
80cc_defaults {
81    name: "berberis_defaults_64",
82    defaults: ["berberis_defaults"],
83    compile_multilib: "64",
84}
85
86cc_defaults {
87    name: "berberis_arm64_defaults",
88    defaults: ["berberis_guest_agnostic_defaults"],
89    compile_multilib: "64",
90    arch: {
91        x86_64: {
92            // TODO(b/123294366): disconnect guest arch from host arch!
93            cflags: ["-DNATIVE_BRIDGE_GUEST_ARCH_ARM64"],
94        },
95    },
96}
97
98cc_defaults {
99    name: "berberis_arm_defaults",
100    defaults: ["berberis_guest_agnostic_defaults"],
101    compile_multilib: "32",
102    arch: {
103        x86: {
104            // TODO(b/123294366): disconnect guest arch from host arch!
105            cflags: ["-DNATIVE_BRIDGE_GUEST_ARCH_ARM"],
106        },
107    },
108}
109
110cc_defaults {
111    name: "berberis_test_library_defaults",
112    defaults: ["berberis_defaults"],
113    host_supported: true,
114    shared: {
115        enabled: false,
116    },
117}
118
119cc_defaults {
120    name: "berberis_test_library_defaults_64",
121    defaults: ["berberis_test_library_defaults"],
122    compile_multilib: "64",
123}
124
125cc_library_shared {
126    name: "libberberis_riscv64",
127    defaults: ["berberis_defaults_64"],
128    host_supported: true,
129    static_libs: [
130        "libbase",
131        "libberberis_assembler",
132        "libberberis_base",
133        "libberberis_instrument",
134        "libberberis_intrinsics",
135        "libberberis_runtime_primitives",
136        "libberberis_tinyloader",
137    ],
138    whole_static_libs: [
139        "libberberis_proxy_loader",
140    ],
141    export_static_lib_headers: [
142        "libberberis_proxy_loader",
143    ],
144    shared_libs: [
145        "liblog",
146    ],
147    arch: {
148        x86_64: {
149            static_libs: [
150                "libberberis_backend_riscv64_to_x86_64",
151                "libberberis_code_gen_lib_riscv64",
152                "libberberis_guest_abi_riscv64",
153                "libberberis_heavy_optimizer_riscv64",
154                "libberberis_interpreter_riscv64",
155                "libberberis_kernel_api_riscv64",
156                "libberberis_lite_translator_riscv64_to_x86_64",
157                "libberberis_macro_assembler_riscv64_to_x86_64",
158                "libberberis_intrinsics_riscv64",
159                "libberberis_runtime_primitives_riscv64",
160            ],
161            whole_static_libs: [
162                "libberberis_guest_loader_riscv64",
163                // Proxy libraries reference symbols from guest_os_primitives,
164                // so we need to keep them all.
165                "libberberis_guest_os_primitives_riscv64",
166                // Android debuggerd reference symbols from get_cpu_state.
167                "libberberis_guest_state_riscv64",
168                "libberberis_runtime_riscv64_to_x86_64",
169            ],
170            export_static_lib_headers: [
171                "libberberis_guest_loader_riscv64",
172            ],
173        },
174    },
175    target: {
176        android: {
177            whole_static_libs: [
178                "libberberis_jni_riscv64",
179                "libberberis_native_activity_riscv64",
180                "libberberis_native_bridge_riscv64",
181            ],
182            shared_libs: [
183                "libandroid",
184                "libdl_android",
185            ],
186        },
187    },
188}
189
190cc_test_host {
191    name: "berberis_host_tests",
192    defaults: ["berberis_defaults"],
193    static_libs: [
194        "libberberis_assembler",
195        "libberberis_base",
196        "libberberis_intrinsics",
197        "libberberis_instrument",
198        "libberberis_runtime_primitives",
199        "libberberis_tinyloader",
200        "libgmock",
201    ],
202    shared_libs: [
203        "libbase",
204        "liblog",
205    ],
206    whole_static_libs: [
207        "libberberis_assembler_unit_tests",
208        "libberberis_base_unit_tests",
209        "libberberis_calling_conventions_unit_tests",
210        "libberberis_intrinsics_unit_tests",
211        "libberberis_tinyloader_unit_tests",
212        "libberberis_runtime_primitives_unit_tests",
213    ],
214    arch: {
215        x86: {
216            srcs: [
217                "base/raw_syscall_tests.cc",
218            ],
219        },
220        x86_64: {
221            srcs: [
222                "base/raw_syscall_tests.cc",
223            ],
224            static_libs: [
225                "libberberis_backend_riscv64_to_x86_64",
226                "libberberis_guest_abi_riscv64",
227                "libberberis_guest_os_primitives_riscv64",
228                "libberberis_guest_state_riscv64",
229                "libberberis_heavy_optimizer_riscv64",
230                "libberberis_interpreter_riscv64",
231                "libberberis_kernel_api_riscv64",
232                "libberberis_lite_translator_riscv64_to_x86_64",
233                "libberberis_macro_assembler_riscv64_to_x86_64",
234                "libberberis_intrinsics_riscv64",
235                "libberberis_runtime",
236                "libberberis_runtime_riscv64_to_x86_64",
237                "libberberis_runtime_primitives_riscv64",
238                "libberberis_code_gen_lib_riscv64",
239            ],
240            whole_static_libs: [
241                "libberberis_tests_main",
242                "libberberis_backend_riscv64_to_x86_64_unit_tests",
243                "libberberis_code_gen_lib_riscv64_unit_tests",
244                "libberberis_guest_abi_riscv64_unit_tests",
245                "libberberis_guest_os_primitives_unit_tests",
246                "libberberis_heavy_optimizer_riscv64_unit_tests",
247                "libberberis_interpreter_riscv64_unit_tests",
248                "libberberis_lite_translator_riscv64_to_x86_64_unit_tests",
249                "libberberis_runtime_riscv64_unit_tests",
250                "libberberis_runtime_primitives_riscv64_unit_tests",
251                "libberberis_cpu_state_riscv64_unit_test",
252            ],
253        },
254    },
255    data: [
256        "tiny_loader/tests/files/32/*",
257        "tiny_loader/tests/files/64/*",
258    ],
259    test_suites: ["device-tests"],
260    test_options: {
261        unit_test: true,
262    },
263}
264
265// The following are the dependencies of `berberis_all` for `arm_to_x86`
266// Note: When the variables `BERBERIS_PRODUCT_PACKAGES` and
267//      `BERBERIS_DEV_PRODUCT_PACKAGES`, in `berberis_config.mk` are modified,
268//      please also change the `berberis_deps_defaults`.
269phony_rule_defaults {
270    name: "berberis_deps_defaults",
271    phony_deps: [
272        // BERBERIS_PRODUCT_PACKAGES
273        "libberberis_exec_region",
274        // BERBERIS_DEV_PRODUCT_PACKAGES
275        "berberis_hello_world.native_bridge",
276        "berberis_hello_world_static.native_bridge",
277        "berberis_host_tests",
278        "berberis_ndk_program_tests",
279        "berberis_ndk_program_tests.native_bridge",
280        "dwarf_reader",
281        "libberberis_emulated_libcamera2ndk_api_checker",
282        "nogrod_unit_tests",
283        "gen_intrinsics_tests",
284    ],
285}
286
287// Note: When the variables `BERBERIS_PRODUCT_PACKAGES_RISCV64_TO_X86_64` and
288//      `BERBERIS_DEV_PRODUCT_PACKAGES_RISCV64_TO_X86_64` in
289//      `berberis_config.mk` and the variables `NATIVE_BRIDGE_PRODUCT_PACKAGES`,
290//      `NATIVE_BRIDGE_ORIG_GUEST_LIBS`, `NATIVE_BRIDGE_MODIFIED_GUEST_LIBS`
291//      in `frameworks/libs/native_bridge_support/native_bridge_support.mk` are
292//      modified, please also change the `berberis_riscv64_to_x86_64_defaults`.
293phony_rule_defaults {
294    name: "berberis_riscv64_to_x86_64_defaults",
295    phony_deps: [
296        // BERBERIS_PRODUCT_PACKAGES_RISCV64_TO_X86_64
297        "libberberis_proxy_libEGL",
298        "libberberis_proxy_libGLESv1_CM",
299        "libberberis_proxy_libGLESv2",
300        "libberberis_proxy_libGLESv3",
301        "libberberis_proxy_libOpenMAXAL",
302        "libberberis_proxy_libOpenSLES",
303        "libberberis_proxy_libaaudio",
304        "libberberis_proxy_libamidi",
305        "libberberis_proxy_libandroid",
306        "libberberis_proxy_libbinder_ndk",
307        "libberberis_proxy_libc",
308        "libberberis_proxy_libcamera2ndk",
309        "libberberis_proxy_libjnigraphics",
310        "libberberis_proxy_libmediandk",
311        "libberberis_proxy_libnativehelper",
312        "libberberis_proxy_libnativewindow",
313        "libberberis_proxy_libneuralnetworks",
314        "libberberis_proxy_libvulkan",
315        "libberberis_proxy_libwebviewchromium_plat_support",
316        "berberis_prebuilt_riscv64",
317        "berberis_program_runner_binfmt_misc_riscv64",
318        "berberis_program_runner_riscv64",
319        "libberberis_riscv64",
320        // NATIVE_BRIDGE_PRODUCT_PACKAGES
321        "libnative_bridge_vdso.native_bridge",
322        "native_bridge_guest_app_process.native_bridge",
323        "native_bridge_guest_linker.native_bridge",
324        // $(addsuffix .native_bridge,$(NATIVE_BRIDGE_ORIG_GUEST_LIBS))
325        "libandroidicu.bootstrap.native_bridge",
326        "libcompiler_rt.native_bridge",
327        "libcrypto.native_bridge",
328        "libcutils.native_bridge",
329        "libdl.bootstrap.native_bridge",
330        "libdl_android.bootstrap.native_bridge",
331        "libicu.bootstrap.native_bridge",
332        "liblog.native_bridge",
333        "libm.bootstrap.native_bridge",
334        "libsqlite.native_bridge",
335        "libssl.native_bridge",
336        "libstdc++.native_bridge",
337        "libsync.native_bridge",
338        "libutils.native_bridge",
339        "libz.native_bridge",
340        // NATIVE_BRIDGE_MODIFIED_GUEST_LIBS
341        "libnative_bridge_guest_libaaudio.native_bridge",
342        "libnative_bridge_guest_libamidi.native_bridge",
343        "libnative_bridge_guest_libandroid.native_bridge",
344        "libnative_bridge_guest_libandroid_runtime.native_bridge",
345        "libnative_bridge_guest_libbinder_ndk.native_bridge",
346        "libnative_bridge_guest_libc.native_bridge",
347        "libnative_bridge_guest_libcamera2ndk.native_bridge",
348        "libnative_bridge_guest_libEGL.native_bridge",
349        "libnative_bridge_guest_libGLESv1_CM.native_bridge",
350        "libnative_bridge_guest_libGLESv2.native_bridge",
351        "libnative_bridge_guest_libGLESv3.native_bridge",
352        "libnative_bridge_guest_libjnigraphics.native_bridge",
353        "libnative_bridge_guest_libmediandk.native_bridge",
354        "libnative_bridge_guest_libnativehelper.native_bridge",
355        "libnative_bridge_guest_libnativewindow.native_bridge",
356        "libnative_bridge_guest_libneuralnetworks.native_bridge",
357        "libnative_bridge_guest_libOpenMAXAL.native_bridge",
358        "libnative_bridge_guest_libOpenSLES.native_bridge",
359        "libnative_bridge_guest_libvulkan.native_bridge",
360        "libnative_bridge_guest_libwebviewchromium_plat_support.native_bridge",
361        // BERBERIS_DEV_PRODUCT_PACKAGES_RISCV64_TO_X86_64
362        "berberis_guest_loader_riscv64_tests",
363    ],
364}
365
366soong_config_module_type {
367    name: "berberis_phony_rule",
368    module_type: "phony_rule",
369    config_namespace: "berberis", // BUILD_BERBERIS
370    variables: ["translation_arch"],
371    properties: [
372        "defaults",
373        "enabled",
374        "phony_deps",
375    ],
376}
377
378soong_config_string_variable {
379    name: "translation_arch",
380    values: [
381        "riscv64_to_x86_64", // BUILD_BERBERIS_RISCV64_TO_X86_64
382    ],
383}
384
385berberis_phony_rule {
386    name: "berberis_all",
387    soong_config_variables: {
388        translation_arch: {
389            riscv64_to_x86_64: {
390                defaults: [
391                    "berberis_deps_defaults",
392                    "berberis_riscv64_to_x86_64_defaults",
393                ],
394                enabled: true,
395            },
396            conditions_default: {
397                enabled: false,
398            },
399        },
400    },
401}
402