Bindgen を使う

bindgen ツールは、C ヘッダーファイルからバインディングを自動生成できます。

ライブラリ用のラッパーヘッダーファイルを作成します(この例では厳密には必要ありません):

interoperability/bindgen/libbirthday_wrapper.h:

#include "libbirthday.h"

interoperability/bindgen/Android.bp:

rust_bindgen {
    name: "libbirthday_bindgen",
    crate_name: "birthday_bindgen",
    wrapper_src: "libbirthday_wrapper.h",
    source_stem: "bindings",
    static_libs: ["libbirthday"],
}

最後に、Rust プログラムでバインディングを使用できます:

interoperability/bindgen/Android.bp:

rust_binary {
    name: "print_birthday_card",
    srcs: ["main.rs"],
    rustlibs: ["libbirthday_bindgen"],
    static_libs: ["libbirthday"],
}

interoperability/bindgen/main.rs:

// Copyright 2022 Google LLC
// SPDX-License-Identifier: Apache-2.0

//! Bindgen demo.

use birthday_bindgen::{card, print_card};

fn main() {
    let name = std::ffi::CString::new("Peter").unwrap();
    let card = card { name: name.as_ptr(), years: 42 };
    // SAFETY: The pointer we pass is valid because it came from a Rust
    // reference, and the `name` it contains refers to `name` above which also
    // remains valid. `print_card` doesn't store either pointer to use later
    // after it returns.
    unsafe {
        print_card(&card);
    }
}
  • Android のビルドルールは、裏側で自動的に bindgen を呼び出します。

  • main 内の Rust コードは、依然として書くのが難しいことに注意してください。呼び出し元に安全なインターフェースを提供する Rust ライブラリの中に bindgen の出力をカプセル化するのが、よいプラクティスです。