Skip to content

Commit cf0037d

Browse files
committed
rust: Make binder-ndk-sys crate buildable via Cargo for NDK
Test: cargo build --target=aarch64-linux-android Bug: 368303574 Change-Id: I58e41d34bc16d28bb899dc8a58dce12627f7ab4e
1 parent 86e7e6b commit cf0037d

5 files changed

Lines changed: 88 additions & 2 deletions

File tree

libs/binder/rust/Android.bp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ rust_bindgen {
140140
"--raw-line",
141141
"use libc::sockaddr;",
142142
],
143+
cflags: [
144+
"-DANDROID_PLATFORM",
145+
],
143146
shared_libs: [
144147
"libbinder_ndk",
145148
],
@@ -180,6 +183,9 @@ rust_bindgen {
180183
// rustified
181184
"libbinder_ndk_bindgen_flags.txt",
182185
],
186+
cflags: [
187+
"-DANDROID_PLATFORM",
188+
],
183189
shared_libs: [
184190
"libbinder_ndk_on_trusty_mock",
185191
"libc++",

libs/binder/rust/sys/BinderBindings.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,19 @@
1515
*/
1616

1717
#include <android/binder_ibinder.h>
18+
#include <android/binder_parcel.h>
19+
#include <android/binder_status.h>
20+
21+
/* Platform only */
22+
#if defined(ANDROID_PLATFORM) || defined(__ANDROID_VENDOR__)
1823
#include <android/binder_ibinder_platform.h>
1924
#include <android/binder_manager.h>
20-
#include <android/binder_parcel.h>
2125
#include <android/binder_parcel_platform.h>
2226
#include <android/binder_process.h>
2327
#include <android/binder_rpc.h>
2428
#include <android/binder_shell.h>
2529
#include <android/binder_stability.h>
26-
#include <android/binder_status.h>
30+
#endif
2731

2832
namespace android {
2933

@@ -81,8 +85,10 @@ enum {
8185

8286
enum {
8387
FLAG_ONEWAY = FLAG_ONEWAY,
88+
#if defined(ANDROID_PLATFORM) || defined(__ANDROID_VENDOR__)
8489
FLAG_CLEAR_BUF = FLAG_CLEAR_BUF,
8590
FLAG_PRIVATE_LOCAL = FLAG_PRIVATE_LOCAL,
91+
#endif
8692
};
8793

8894
} // namespace consts

libs/binder/rust/sys/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "android-binder-ndk-sys"
3+
version = "0.1.0"
4+
edition = "2021"
5+
description = "Bindgen bindings to android binder, restricted to the NDK"
6+
license = "Apache-2.0"
7+
8+
[dependencies]
9+
10+
[lib]
11+
path = "lib.rs"
12+
13+
[build-dependencies]
14+
bindgen = "0.70.1"

libs/binder/rust/sys/build.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (C) 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
use std::env;
18+
use std::path::PathBuf;
19+
20+
fn main() {
21+
let ndk_home = PathBuf::from(env::var("ANDROID_NDK_HOME").unwrap());
22+
let toolchain = ndk_home.join("toolchains/llvm/prebuilt/linux-x86_64/");
23+
let sysroot = toolchain.join("sysroot");
24+
let bindings = bindgen::Builder::default()
25+
.clang_arg(format!("--sysroot={}", sysroot.display()))
26+
// TODO figure out what the "standard" #define is and use that instead
27+
.header("BinderBindings.hpp")
28+
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
29+
// Keep in sync with libbinder_ndk_bindgen_flags.txt
30+
.default_enum_style(bindgen::EnumVariation::Rust { non_exhaustive: true })
31+
.constified_enum("android::c_interface::consts::.*")
32+
.allowlist_type("android::c_interface::.*")
33+
.allowlist_type("AStatus")
34+
.allowlist_type("AIBinder_Class")
35+
.allowlist_type("AIBinder")
36+
.allowlist_type("AIBinder_Weak")
37+
.allowlist_type("AIBinder_DeathRecipient")
38+
.allowlist_type("AParcel")
39+
.allowlist_type("binder_status_t")
40+
.blocklist_function("vprintf")
41+
.blocklist_function("strtold")
42+
.blocklist_function("_vtlog")
43+
.blocklist_function("vscanf")
44+
.blocklist_function("vfprintf_worker")
45+
.blocklist_function("vsprintf")
46+
.blocklist_function("vsnprintf")
47+
.blocklist_function("vsnprintf_filtered")
48+
.blocklist_function("vfscanf")
49+
.blocklist_function("vsscanf")
50+
.blocklist_function("vdprintf")
51+
.blocklist_function("vasprintf")
52+
.blocklist_function("strtold_l")
53+
.allowlist_function(".*")
54+
.generate()
55+
.expect("Couldn't generate bindings");
56+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
57+
bindings.write_to_file(out_path.join("bindings.rs")).expect("Couldn't write bindings.");
58+
println!("cargo::rustc-link-lib=binder_ndk");
59+
}

libs/binder/rust/sys/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::error::Error;
2020
use std::fmt;
2121

2222
#[cfg(not(target_os = "trusty"))]
23+
#[allow(bad_style)]
2324
mod bindings {
2425
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
2526
}

0 commit comments

Comments
 (0)