diwic / alsa-sys

Rust raw FFI bindings for ALSA
MIT License
15 stars 11 forks source link

[feature request] Add a feature to generate bindings at build-time #2

Closed derekdreery closed 4 years ago

derekdreery commented 5 years ago

Using bindgen, the build.rs would look something like

extern crate bindgen;
extern crate pkg_config;

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

fn main() {
    if let Err(e) = pkg_config::probe_library("alsa") {
        match e {
            pkg_config::Error::Failure { .. } => panic! (
                "Pkg-config failed - usually this is because alsa development headers are not installed.\n\n\
                For Fedora users:\n# dnf install alsa-lib-devel\n\n\
                For Debian/Ubuntu users:\n# apt-get install libasound2-dev\n\n\
                pkg_config details:\n{}",
                e
            ),
            _ => panic!("{}", e)
        }
    }
    if env::var_os("CARGO_FEATURE_USE_BINDGEN").is_some() {
        let bindings = bindgen::Builder::default()
            .header("wrapper.h")
            .prepend_enum_name(false)
            .generate()
            .expect("Unable to 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!");
    }
}

I'm happy to knock up a PR if you accept this feature.

diwic commented 5 years ago

Hi!

This sounds like a good idea, but I have one question before you start. Is it possible to avoid the build dependency of bindgen (and thus the 20 other crates that comes with it), if the bindgen feature is disabled?

Thanks!

diwic commented 5 years ago

It looks like it should be possible to have optional build-dependencies using a strategy similar to what's described here: https://docs.rs/ispc/1.0.6/ispc/

diwic commented 4 years ago

Closing due to inactivity.

derekdreery commented 4 years ago

yeah sorry for never getting round to this

cyberia-ng commented 3 years ago

I've done a patch that does this, but I have a couple of open questions. I'll do a PR and we can discuss it there?