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

macos/windows/linux build using cc crate #168

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion rust-portaudio-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ name = "portaudio_sys"
crate-type = ["rlib"]

[build-dependencies]
pkg-config = "0.3.6"
cc = "1"
239 changes: 74 additions & 165 deletions rust-portaudio-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,173 +1,82 @@
// The MIT License (MIT)
//
// Copyright (c) 2013 Jeremy Letang ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

extern crate pkg_config;
extern crate cc;

use std::env;
use std::fmt::Display;
use std::path::Path;
use std::process::Command;

#[cfg(all(unix, not(target_os = "linux")))]
use unix_platform as platform;

fn main() {
println!("cargo:rerun-if-changed=build.rs");

println!("cargo:rerun-if-env-changed=PORTAUDIO_ONLY_STATIC");
if env::var("PORTAUDIO_ONLY_STATIC").is_err() {
// If pkg-config finds a library on the system, we are done
if pkg_config::Config::new().atleast_version("19").find("portaudio-2.0").is_ok() {
return;
}
}

build();
}

fn build() {
// retrieve cargo deps out dir
let out_dir_str = env::var("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir_str);

let static_lib = out_dir.join("lib/libportaudio.a");
if let Err(_) = ::std::fs::metadata(static_lib) {
platform::download();
platform::build(out_dir);
}

platform::print_libs(out_dir);
}

// Similar to unwrap, but panics on just the error value
#[allow(dead_code)]
fn err_to_panic<T, E: Display>(result: Result<T, E>) -> T {
match result {
Ok(x) => x,
Err(e) => panic!("{}", e)
}
}

fn run(command: &mut Command) {
let string = format!("{:?}", command);
let status = err_to_panic(command.status());
if !status.success() {
panic!("`{}` did not execute successfully", string);
}
}

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

use std::env;

use super::{err_to_panic, run};

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_FOLDER: &'static str = "portaudio";

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

pub fn build(out_dir: &Path) {
// untar portaudio sources
run(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
run(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)

// then make
run(&mut Command::new("make"));

// "install" on the outdir
run(Command::new("make").arg("install"));

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

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

pub fn print_libs(out_dir: &Path) {
let out_str = out_dir.to_str().unwrap();
println!("cargo:rustc-flags=-L native={}/lib -l static=portaudio", out_str);
}
}

#[cfg(target_os = "linux")]
mod platform {
use pkg_config;
use std::process::Command;
use super::unix_platform;
use std::path::Path;

use super::{run, err_to_panic};

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

pub fn build(out_dir: &Path) {
unix_platform::build(out_dir);
}

pub fn print_libs(out_dir: &Path) {
let portaudio_pc_file = out_dir.join("lib/pkgconfig/portaudio-2.0.pc");
let portaudio_pc_file = portaudio_pc_file.to_str().unwrap();

err_to_panic(pkg_config::Config::new().statik(true).find(portaudio_pc_file));
}
}

#[cfg(windows)]
mod platform {
use std::path::Path;

const PORTAUDIO_DOWNLOAD_URL: &'static str = "http://www.portaudio.com";

fn print_lib_url() {
panic!("Don't know how to build portaudio on Windows yet. Sources and build instructions available at: {}", PORTAUDIO_DOWNLOAD_URL);
}

pub fn download() {
print_lib_url();
}

pub fn build(_: &Path) {
print_lib_url();
let target = env::var("TARGET").unwrap();

let mut cc = cc::Build::new();

let src_common = [
"portaudio/src/common/pa_allocation.c",
"portaudio/src/common/pa_converters.c",
"portaudio/src/common/pa_cpuload.c",
"portaudio/src/common/pa_debugprint.c",
"portaudio/src/common/pa_dither.c",
"portaudio/src/common/pa_front.c",
"portaudio/src/common/pa_process.c",
"portaudio/src/common/pa_ringbuffer.c",
"portaudio/src/common/pa_stream.c",
"portaudio/src/common/pa_trace.c",
];

let src_win = [
"portaudio/src/hostapi/wasapi/pa_win_wasapi.c",
"portaudio/src/os/win/pa_win_hostapis.c",
"portaudio/src/os/win/pa_win_util.c",
"portaudio/src/os/win/pa_win_waveformat.c",
"portaudio/src/os/win/pa_win_wdmks_utils.c",
"portaudio/src/os/win/pa_win_coinitialize.c",
"portaudio/src/os/win/pa_x86_plain_converters.c",
];

let src_mac = [
"portaudio/src/hostapi/coreaudio/pa_mac_core.c",
"portaudio/src/hostapi/coreaudio/pa_mac_core_blocking.c",
"portaudio/src/hostapi/coreaudio/pa_mac_core_utilities.c",
];

let src_unix = [
"portaudio/src/os/unix/pa_unix_hostapis.c",
"portaudio/src/os/unix/pa_unix_util.c",
];

let src_alsa = ["portaudio/src/hostapi/alsa/pa_linux_alsa"];

cc.include("portaudio/include");
cc.include("portaudio/src/common");

for src in src_common.iter() {
cc.file(src);
}

pub fn print_libs(_: &Path) {
print_lib_url();
if target.contains("linux") {
println!("cargo:rustc-link-lib=asound");
for src in src_alsa.iter() {
cc.file(src);
}
} else if target.contains("apple") {
println!("cargo:rustc-link-lib=framework=AudioToolbox");
println!("cargo:rustc-link-lib=framework=AudioUnit");
println!("cargo:rustc-link-lib=framework=CoreAudio");
println!("cargo:rustc-link-lib=framework=CoreFoundation");
println!("cargo:rustc-link-lib=framework=Carbon");
for src in src_mac.iter() {
cc.file(src);
}
for src in src_unix.iter() {
cc.file(src);
}
cc.flag("-DPA_USE_COREAUDIO=1");
cc.flag("-mmacosx-version-min=10.4");
cc.flag("-std=c99");
} else if target.contains("windows") {
for src in src_win.iter() {
cc.file(src);
}
cc.flag("-DPA_USE_WASAPI=1");
cc.include("portaudio/src/os/win");
println!("cargo:rustc-link-lib=ole32");
}
cc.compile("libportaudio.a");
}
81 changes: 81 additions & 0 deletions rust-portaudio-sys/portaudio/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Portable header file to contain:
>>>>>
/*
* PortAudio Portable Real-Time Audio Library
* PortAudio API Header File
* Latest version available at: http://www.portaudio.com
*
* Copyright (c) 1999-2006 Ross Bencina and Phil Burk
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/*
* The text above constitutes the entire PortAudio license; however,
* the PortAudio community also makes the following non-binding requests:
*
* Any person wishing to distribute modifications to the Software is
* requested to send the modifications to the original developer so that
* they can be incorporated into the canonical version. It is also
* requested that these non-binding requests be included along with the
* license above.
*/
<<<<<


Implementation files to contain:
>>>>>
/*
* PortAudio Portable Real-Time Audio Library
* Latest version at: http://www.portaudio.com
* <platform> Implementation
* Copyright (c) 1999-2000 <author(s)>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/*
* The text above constitutes the entire PortAudio license; however,
* the PortAudio community also makes the following non-binding requests:
*
* Any person wishing to distribute modifications to the Software is
* requested to send the modifications to the original developer so that
* they can be incorporated into the canonical version. It is also
* requested that these non-binding requests be included along with the
* license above.
*/
<<<<<
Loading