Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moved ffi from extern crate to internal mod #155

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ description = "PortAudio bindings for Rust."
license = "MIT"
homepage = "https://github.com/RustAudio/rust-portaudio"
repository = "https://github.com/RustAudio/rust-portaudio.git"
build = "build.rs"
links = "portaudio"

[dependencies]
bitflags = "0.7"
libc = "0.2.14"
num = { version = "0.1.34", default-features = false }
portaudio_sys = { path = "./rust-portaudio-sys", version = "0.1.0" }

[build-dependencies]
pkg-config = "0.3.6"
36 changes: 25 additions & 11 deletions rust-portaudio-sys/build.rs → build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,49 +62,62 @@ fn err_to_panic<T, E: Display>(result: Result<T, E>) -> T {
}
}

/// Executes the given command and prints it to stdout. (Stdout will only be displayed by cargo if the build
/// script panics).
/// Panics if the execution fails or the command returns non-successful.
#[allow(dead_code)]
fn execute_or_panic(cmd: &mut std::process::Command) {
let output = err_to_panic(cmd.output());
println!("{:?}", cmd);
if !output.status.success() {
panic!("{}", String::from_utf8_lossy(&output.stderr));
}
}

#[allow(dead_code)]
mod unix_platform {
use std::process::Command;
use std::path::Path;

use std::env;

use super::execute_or_panic;
use super::err_to_panic;

pub const PORTAUDIO_URL: &'static str = "http://www.portaudio.com/archives/pa_stable_v19_20140130.tgz";
pub const PORTAUDIO_TAR: &'static str = "pa_stable_v19_20140130.tgz";
pub const PORTAUDIO_URL: &'static str = "http://www.portaudio.com/archives/pa_stable_v190600_20161030.tgz";
pub const PORTAUDIO_TAR: &'static str = "pa_stable_v190600_20161030.tgz";
pub const PORTAUDIO_FOLDER: &'static str = "portaudio";

pub fn download() {
err_to_panic(Command::new("curl").arg(PORTAUDIO_URL).arg("-O").output());
execute_or_panic(Command::new("curl").arg(PORTAUDIO_URL).arg("-O"));
}

pub fn build(out_dir: &Path) {
// untar portaudio sources
err_to_panic(Command::new("tar").arg("xvf").arg(PORTAUDIO_TAR).output());
execute_or_panic(Command::new("tar").arg("xvf").arg(PORTAUDIO_TAR));

// change dir to the portaudio folder
err_to_panic(env::set_current_dir(PORTAUDIO_FOLDER));

// run portaudio autoconf
err_to_panic(Command::new("./configure")
execute_or_panic(Command::new("./configure")
.args(&["--disable-shared", "--enable-static"]) // Only build static lib
.args(&["--prefix", out_dir.to_str().unwrap()]) // Install on the outdir
.arg("--with-pic") // Build position-independent code (required by Rust)
.output());
);

// then make
err_to_panic(Command::new("make").output());
execute_or_panic(&mut Command::new("make"));

// "install" on the outdir
err_to_panic(Command::new("make").arg("install").output());
execute_or_panic(Command::new("make").arg("install"));

// return to rust-portaudio root
err_to_panic(env::set_current_dir(".."));

// cleaning portaudio sources
err_to_panic(Command::new("rm").arg("-rf")
.args(&[PORTAUDIO_TAR, PORTAUDIO_FOLDER]).output());
execute_or_panic(Command::new("rm").arg("-rf")
.args(&[PORTAUDIO_TAR, PORTAUDIO_FOLDER]));
}

pub fn print_libs(out_dir: &Path) {
Expand All @@ -120,10 +133,11 @@ mod platform {
use super::unix_platform;
use std::path::Path;

use super::execute_or_panic;
use super::err_to_panic;

pub fn download() {
err_to_panic(Command::new("wget").arg(unix_platform::PORTAUDIO_URL).output());
execute_or_panic(Command::new("wget").arg(unix_platform::PORTAUDIO_URL));
}

pub fn build(out_dir: &Path) {
Expand Down
30 changes: 0 additions & 30 deletions rust-portaudio-sys/.gitignore

This file was deleted.

19 changes: 0 additions & 19 deletions rust-portaudio-sys/Cargo.toml

This file was deleted.

10 changes: 5 additions & 5 deletions rust-portaudio-sys/src/lib.rs → src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
--blacklist-type PaStreamCallbackResult
*/

mod portaudio;

#[cfg(any(target_os="macos", target_os="linux", target_os="win32", target_os="windows"))]
mod c_library {
#[link(name = "portaudio")]
extern {}
}

mod portaudio;

pub use portaudio::*;
pub use self::portaudio::*;

pub const PA_NO_DEVICE : PaDeviceIndex = -1;

Expand Down Expand Up @@ -46,14 +46,14 @@ pub const OUTPUT_OVERFLOW : StreamCallbackFlags = 0x00000008;
pub const PRIMING_OUTPUT : StreamCallbackFlags = 0x00000010;

/// A function to convert C `*const char` arrays into Rust `&'a str`s.
pub fn c_str_to_str<'a>(c_str: *const std::os::raw::c_char) -> Result<&'a str, ::std::str::Utf8Error> {
pub fn c_str_to_str<'a>(c_str: *const ::std::os::raw::c_char) -> Result<&'a str, ::std::str::Utf8Error> {
unsafe {
::std::ffi::CStr::from_ptr(c_str).to_str()
}
}

/// A function to convert Rust strings to C strings
pub fn str_to_c_str(rust_str: &str) -> *const std::os::raw::c_char {
pub fn str_to_c_str(rust_str: &str) -> *const ::std::os::raw::c_char {
rust_str.as_ptr() as *const _
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]

/* automatically generated by rust-bindgen */

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
#[macro_use] extern crate bitflags;
extern crate libc;
extern crate num;
extern crate portaudio_sys as ffi;

use num::FromPrimitive;
use std::os::raw;
Expand Down Expand Up @@ -108,8 +107,9 @@ use std::ptr;
pub mod error;
pub mod ext;
pub mod stream;
mod types;

mod types;
mod ffi;

/// A type-safe wrapper around the PortAudio API.
///
Expand Down