Skip to content

Commit

Permalink
generate bindings during build and update the version 0.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
rosetta-jpn authored Oct 21, 2024
1 parent 0f37d0c commit fda656e
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 17,690 deletions.
17 changes: 15 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ on:

env:
CARGO_TERM_COLOR: always

CROS_LIBVA_H_PATH: ${{ github.workspace }}/work/out/include
CROS_LIBVA_LIB_PATH: ${{ github.workspace }}/work/out/lib
RUSTFLAGS: '-L ${{ github.workspace }}/work/out/lib'
RUSTDOCFLAGS: '-L ${{ github.workspace }}/work/out/lib'
jobs:
build:

Expand All @@ -17,9 +20,19 @@ jobs:
steps:
- uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: libva-dev
packages: libdrm-dev
version: 1.0
- uses: actions/checkout@v3
- name: "checkout libva"
run: |
curl -O -L https://github.com/intel/libva/archive/refs/tags/2.22.0.tar.gz && \
tar xf 2.22.0.tar.gz --one-top-level=work --strip-components 1
- name: "build libva"
run: |
cd work; \
./autogen.sh --enable-drm --disable-x11 --disable-glx --disable-wayland --prefix=${{ github.workspace }}/work/out && \
make && \
make install
- name: Build
run: cargo build --all-features --verbose --workspace --tests --examples
- name: Clippy
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cros-libva"
version = "0.0.7"
version = "0.0.8"
license = "BSD-3-Clause"
description = "Safe bindings over libva"
repository = "https://github.com/chromeos/cros-libva"
Expand All @@ -13,6 +13,7 @@ bitflags = "2.5"
log = { version = "0", features = ["release_max_level_debug"] }

[build-dependencies]
bindgen = "0.70.1"
pkg-config = "0.3.26"

[dev-dependencies]
Expand Down
23 changes: 0 additions & 23 deletions bindgen.sh

This file was deleted.

73 changes: 69 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,82 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::env::{self};
use std::path::{Path, PathBuf};

/// Environment variable that can be set to point to the directory containing the `va.h`, `va_drm.h` and `va_drmcommon.h`
/// files to use to generate the bindings.
const CROS_LIBVA_H_PATH_ENV: &str = "CROS_LIBVA_H_PATH";
const CROS_LIBVA_LIB_PATH_ENV: &str = "CROS_LIBVA_LIB_PATH";

/// Wrapper file to use as input of bindgen.
const WRAPPER_PATH: &str = "libva-wrapper.h";

/// Allow list
const ALLOW_LIST_TYPE : &str = ".*ExternalBuffers.*|.*PRIME.*|.*MPEG2.*|.*VP8.*|.*VP9.*|.*H264.*|.*HEVC.*|VACodedBufferSegment|.*AV1.*|VAEncMisc.*|VASurfaceDecodeMBErrors|VADecodeErrorType";

fn main() {
// Do not require dependencies when generating docs.
if std::env::var("CARGO_DOC").is_ok() || std::env::var("DOCS_RS").is_ok() {
return;
}

match pkg_config::probe_library("libva") {
Ok(_) => (),
Err(e) => panic!("libva not found: {}", e),
};
let va_h_path = env::var(CROS_LIBVA_H_PATH_ENV).unwrap_or_default();
let va_lib_path = env::var(CROS_LIBVA_LIB_PATH_ENV).unwrap_or_default();
// Check the path exists.
if !va_h_path.is_empty() {
assert!(
Path::new(&va_h_path).exists(),
"{} doesn't exist",
va_h_path
);
}

if !va_lib_path.is_empty() {
assert!(
Path::new(&va_h_path).exists(),
"{} doesn't exist",
va_h_path
);
println!("cargo:rustc-link-arg=-Wl,-rpath={}", va_lib_path);
}

// Tell cargo to link va and va-drm objects dynamically.
println!("cargo:rustc-link-lib=dylib=va");
println!("cargo:rustc-link-lib=dylib=va-drm"); // for the vaGetDisplayDRM entrypoint

let mut bindings_builder = bindgen::builder()
.header(WRAPPER_PATH)
.raw_line("pub mod constants;")
.derive_default(true)
.derive_eq(true)
.layout_tests(false)
.constified_enum_module("VA.*")
.allowlist_function("va.*")
.allowlist_type(ALLOW_LIST_TYPE);
if !va_h_path.is_empty() {
bindings_builder = bindings_builder.clang_arg(format!("-I{}", va_h_path));
}
let bindings = bindings_builder
.generate()
.expect("unable to generate bindings");

let out_path = PathBuf::from(env::var("OUT_DIR").expect("`OUT_DIR` is not set"));

bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");

let mut bindings_builder = bindgen::builder()
.header(WRAPPER_PATH)
.allowlist_var("VA.*");
if !va_h_path.is_empty() {
bindings_builder = bindings_builder.clang_arg(format!("-I{}", va_h_path));
}
let bindings = bindings_builder
.generate()
.expect("unable to generate bindings");
bindings
.write_to_file(out_path.join("constants.rs"))
.expect("Couldn't write bindings!");
}
17 changes: 8 additions & 9 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

//! This module implements the bindgen C FFI bindings for use within this crate
#[allow(missing_docs)]
#[allow(clippy::useless_transmute)]
#[allow(clippy::too_many_arguments)]
#[allow(non_upper_case_globals)]
#[allow(non_snake_case)]
#[allow(non_camel_case_types)]
#[allow(dead_code)]
pub mod va;
#![allow(missing_docs)]
#![allow(clippy::useless_transmute)]
#![allow(clippy::too_many_arguments)]
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
#![allow(dead_code)]

pub use va::*;
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
Loading

0 comments on commit fda656e

Please sign in to comment.