From a1704be974e216b40cd40449989c688d079b1cc6 Mon Sep 17 00:00:00 2001 From: Marcin Radomski Date: Tue, 5 Mar 2024 11:00:10 +0000 Subject: libdrm: add bindgen-generated Rust bindings This provides the bindings necessary to use the Rust drm crate within Android source tree. Bindgen flags used in the rust_bindgen definition, as well as the workaround described in wrapper.h were based on drm-ffi crate's build.rs [1]. Looking at other rust_bindgen modules, they are generally defined alongside the native module they provide bindings to. Details: go/drm-gbm-rust-crates-for-android [1] https://github.com/Smithay/drm-rs/blob/328742fddc675b3370057b382eb54acbc9b48c79/drm-ffi/drm-sys/build.rs#L126 Bug: 328178122 Test: lunch aosp_cf_x86_64_only_phone-trunk_staging-userdebug && m Test: m libdrm_sys Change-Id: I18ea318dcd4a0734d647fe7a703e8c3ab7442a52 --- Android.bp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++---- rust/drm_wrapper.h | 22 ++++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 rust/drm_wrapper.h diff --git a/Android.bp b/Android.bp index 7ed9174e..ea471952 100644 --- a/Android.bp +++ b/Android.bp @@ -81,7 +81,10 @@ cc_library_headers { vendor_available: true, host_supported: true, defaults: ["libdrm_defaults"], - export_include_dirs: ["include/drm", "android"], + export_include_dirs: [ + "include/drm", + "android", + ], apex_available: [ "//apex_available:platform", "com.android.virt", @@ -92,14 +95,22 @@ cc_library_headers { cc_library { name: "libdrm", recovery_available: true, - vendor_available: true, host_supported: true, + vendor_available: true, + product_available: true, + apex_available: [ + "//apex_available:platform", + "//apex_available:anyapex", + ], defaults: [ "libdrm_defaults", "libdrm_sources", ], - export_include_dirs: ["include/drm", "android"], + export_include_dirs: [ + "include/drm", + "android", + ], cflags: [ "-Wno-enum-conversion", @@ -107,8 +118,50 @@ cc_library { "-Wno-sign-compare", "-Wno-tautological-compare", ], +} + +// Rust bindings for libdrm, generated in a way compatible with drm-ffi crate. +rust_bindgen { + name: "libdrm_sys", + crate_name: "drm_sys", + wrapper_src: "rust/drm_wrapper.h", + source_stem: "bindings", + bindgen_flags: [ + "--ctypes-prefix=core::ffi", + "--no-prepend-enum-name", + "--with-derive-default", + "--with-derive-hash", + "--with-derive-eq", + "--blocklist-type=drm_handle_t", + "--blocklist-type=__BINDGEN_TMP_.*", + "--blocklist-type=drm_control_DRM_ADD_COMMAND", + "--allowlist-type=_?DRM_.*|drm_.*|hdr_.*", + "--allowlist-var=_?DRM_.*|drm_.*", + "--constified-enum-module=drm_control_.*", + "--constified-enum-module=drm_buf_desc_.*", + "--constified-enum-module=drm_map_type", + "--constified-enum-module=drm_map_flags", + "--constified-enum-module=drm_stat_type", + "--constified-enum-module=drm_lock_flags", + "--constified-enum-module=drm_dma_flags", + "--constified-enum-module=drm_ctx_flags", + "--constified-enum-module=drm_drawable_info_type_t", + "--constified-enum-module=drm_vblank_seq_type", + "--constified-enum-module=drm_mode_subconnector", + ], + shared_libs: [ + "libdrm", + "libcutils", + "liblog", + ], + host_supported: true, + vendor_available: true, + product_available: true, apex_available: [ + "//apex_available:anyapex", "//apex_available:platform", - "com.android.virt", + ], + visibility: [ + "//external/rust/crates/drm-ffi", ], } diff --git a/rust/drm_wrapper.h b/rust/drm_wrapper.h new file mode 100644 index 00000000..98d877e1 --- /dev/null +++ b/rust/drm_wrapper.h @@ -0,0 +1,22 @@ +/* + * Copyright 2024 Google LLC + */ + +#include +#include +#include + +// bindgen doesn't create constants for macros that expand to other macros +// https://github.com/Smithay/drm-rs/blob/328742fddc675b3370057b382eb54acbc9b48c79/drm-ffi/drm-sys/build.rs#L110 + +const unsigned int __BINDGEN_TMP_DRM_MODE_PROP_OBJECT = DRM_MODE_PROP_OBJECT; +#undef DRM_MODE_PROP_OBJECT +const unsigned int DRM_MODE_PROP_OBJECT = __BINDGEN_TMP_DRM_MODE_PROP_OBJECT; +#define DRM_MODE_PROP_OBJECT DRM_MODE_PROP_OBJECT + +const unsigned int __BINDGEN_TMP_DRM_MODE_PROP_SIGNED_RANGE = + DRM_MODE_PROP_SIGNED_RANGE; +#undef DRM_MODE_PROP_SIGNED_RANGE +const unsigned int DRM_MODE_PROP_SIGNED_RANGE = + __BINDGEN_TMP_DRM_MODE_PROP_SIGNED_RANGE; +#define DRM_MODE_PROP_SIGNED_RANGE DRM_MODE_PROP_SIGNED_RANGE -- cgit v1.2.3