Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cf0037d8 authored by Matthew Maurer's avatar Matthew Maurer
Browse files

rust: Make binder-ndk-sys crate buildable via Cargo for NDK

Test: cargo build --target=aarch64-linux-android
Bug: 368303574
Change-Id: I58e41d34bc16d28bb899dc8a58dce12627f7ab4e
parent 86e7e6be
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -140,6 +140,9 @@ rust_bindgen {
        "--raw-line",
        "use libc::sockaddr;",
    ],
    cflags: [
        "-DANDROID_PLATFORM",
    ],
    shared_libs: [
        "libbinder_ndk",
    ],
@@ -180,6 +183,9 @@ rust_bindgen {
        // rustified
        "libbinder_ndk_bindgen_flags.txt",
    ],
    cflags: [
        "-DANDROID_PLATFORM",
    ],
    shared_libs: [
        "libbinder_ndk_on_trusty_mock",
        "libc++",
+8 −2
Original line number Diff line number Diff line
@@ -15,15 +15,19 @@
 */

#include <android/binder_ibinder.h>
#include <android/binder_parcel.h>
#include <android/binder_status.h>

/* Platform only */
#if defined(ANDROID_PLATFORM) || defined(__ANDROID_VENDOR__)
#include <android/binder_ibinder_platform.h>
#include <android/binder_manager.h>
#include <android/binder_parcel.h>
#include <android/binder_parcel_platform.h>
#include <android/binder_process.h>
#include <android/binder_rpc.h>
#include <android/binder_shell.h>
#include <android/binder_stability.h>
#include <android/binder_status.h>
#endif

namespace android {

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

enum {
    FLAG_ONEWAY = FLAG_ONEWAY,
#if defined(ANDROID_PLATFORM) || defined(__ANDROID_VENDOR__)
    FLAG_CLEAR_BUF = FLAG_CLEAR_BUF,
    FLAG_PRIVATE_LOCAL = FLAG_PRIVATE_LOCAL,
#endif
};

} // namespace consts
+14 −0
Original line number Diff line number Diff line
[package]
name = "android-binder-ndk-sys"
version = "0.1.0"
edition = "2021"
description = "Bindgen bindings to android binder, restricted to the NDK"
license = "Apache-2.0"

[dependencies]

[lib]
path = "lib.rs"

[build-dependencies]
bindgen = "0.70.1"
+59 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use std::env;
use std::path::PathBuf;

fn main() {
    let ndk_home = PathBuf::from(env::var("ANDROID_NDK_HOME").unwrap());
    let toolchain = ndk_home.join("toolchains/llvm/prebuilt/linux-x86_64/");
    let sysroot = toolchain.join("sysroot");
    let bindings = bindgen::Builder::default()
        .clang_arg(format!("--sysroot={}", sysroot.display()))
        // TODO figure out what the "standard" #define is and use that instead
        .header("BinderBindings.hpp")
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        // Keep in sync with libbinder_ndk_bindgen_flags.txt
        .default_enum_style(bindgen::EnumVariation::Rust { non_exhaustive: true })
        .constified_enum("android::c_interface::consts::.*")
        .allowlist_type("android::c_interface::.*")
        .allowlist_type("AStatus")
        .allowlist_type("AIBinder_Class")
        .allowlist_type("AIBinder")
        .allowlist_type("AIBinder_Weak")
        .allowlist_type("AIBinder_DeathRecipient")
        .allowlist_type("AParcel")
        .allowlist_type("binder_status_t")
        .blocklist_function("vprintf")
        .blocklist_function("strtold")
        .blocklist_function("_vtlog")
        .blocklist_function("vscanf")
        .blocklist_function("vfprintf_worker")
        .blocklist_function("vsprintf")
        .blocklist_function("vsnprintf")
        .blocklist_function("vsnprintf_filtered")
        .blocklist_function("vfscanf")
        .blocklist_function("vsscanf")
        .blocklist_function("vdprintf")
        .blocklist_function("vasprintf")
        .blocklist_function("strtold_l")
        .allowlist_function(".*")
        .generate()
        .expect("Couldn't generate bindings");
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings.write_to_file(out_path.join("bindings.rs")).expect("Couldn't write bindings.");
    println!("cargo::rustc-link-lib=binder_ndk");
}
+1 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ use std::error::Error;
use std::fmt;

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