Skip to content

Commit

Permalink
Reduce new dependencies in the build script
Browse files Browse the repository at this point in the history
  • Loading branch information
alisomay committed Oct 17, 2023
1 parent 48ff0ee commit 1d086b3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 46 deletions.
2 changes: 0 additions & 2 deletions asio-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ build = "build.rs"
bindgen = "0.68"
walkdir = "2"
cc = "1.0.83"
reqwest = { version = "0.11" , features = ["blocking"] }
zip = "0.6"

[dependencies]
once_cell = "1.12"
Expand Down
97 changes: 53 additions & 44 deletions asio-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
extern crate bindgen;
extern crate cc;
extern crate reqwest;
extern crate walkdir;
extern crate zip;

use reqwest::blocking::Client;
use std::env;
use std::fs;
use std::io::{Cursor, Read};
use std::path::PathBuf;
use std::process::{exit, Command};
use std::process::Command;
use walkdir::WalkDir;
use zip::read::ZipArchive;

const CPAL_ASIO_DIR: &str = "CPAL_ASIO_DIR";
const ASIO_SDK_URL: &str = "https://www.steinberg.net/asiosdk";
Expand Down Expand Up @@ -222,7 +216,7 @@ fn create_bindings(cpal_asio_dir: &PathBuf) {
fn get_asio_dir() -> PathBuf {
// Check if CPAL_ASIO_DIR env var is set
if let Ok(path) = env::var(CPAL_ASIO_DIR) {
println!("CPAL_ASIO_DIR is set at {CPAL_ASIO_DIR}");
println!("CPAL_ASIO_DIR is set at {}", path);
return PathBuf::from(path);
}

Expand All @@ -234,41 +228,57 @@ fn get_asio_dir() -> PathBuf {
return asio_dir;
}

// If not found, download ASIO SDK
println!("CPAL_ASIO_DIR is not set or contents are cached downloading from {ASIO_SDK_URL}");
let response = Client::new().get(ASIO_SDK_URL).send();

match response {
Ok(mut resp) => {
if resp.status().is_success() {
println!("Downloaded ASIO SDK successfully");
println!("Extracting ASIO SDK..");
// Unzip the archive
let mut archive_bytes = Vec::new();
resp.read_to_end(&mut archive_bytes).unwrap();

let cursor = Cursor::new(archive_bytes);
let mut archive = ZipArchive::new(cursor).expect("Failed to read zip contents");
archive.extract(&temp_dir).expect("Failed to extract zip");

// Move the contents of the inner directory to asio_dir
for entry in walkdir::WalkDir::new(&temp_dir).min_depth(1).max_depth(1) {
let entry = entry.unwrap();
if entry.file_type().is_dir()
&& entry.file_name().to_string_lossy().starts_with("asio")
{
fs::rename(entry.path(), &asio_dir).expect("Failed to rename directory");
break;
}
}
println!("CPAL_ASIO_DIR is set at {}", asio_dir.display());
asio_dir
} else {
panic!("Failed to download ASIO SDK")
}
// If not found, download ASIO SDK using PowerShell's Invoke-WebRequest
println!("CPAL_ASIO_DIR is not set or contents are cached downloading from {}", ASIO_SDK_URL);

let asio_zip_path = temp_dir.join("asio_sdk.zip");
let status = Command::new("powershell")
.args(&[
"-NoProfile",
"-Command",
&format!(
"Invoke-WebRequest -Uri {} -OutFile {}",
ASIO_SDK_URL,
asio_zip_path.display()
),
])
.status()
.expect("Failed to execute PowerShell command");

if !status.success() {
panic!("Failed to download ASIO SDK");
}
println!("Downloaded ASIO SDK successfully");

// Unzip using PowerShell's Expand-Archive
println!("Extracting ASIO SDK..");
let status = Command::new("powershell")
.args(&[
"-NoProfile",
"-Command",
&format!(
"Expand-Archive -Path {} -DestinationPath {} -Force",
asio_zip_path.display(),
temp_dir.display()
),
])
.status()
.expect("Failed to execute PowerShell command for extracting ASIO SDK");

if !status.success() {
panic!("Failed to extract ASIO SDK");
}

// Move the contents of the inner directory to asio_dir
for entry in walkdir::WalkDir::new(&temp_dir).min_depth(1).max_depth(1) {
let entry = entry.unwrap();
if entry.file_type().is_dir() && entry.file_name().to_string_lossy().starts_with("asio") {
std::fs::rename(entry.path(), &asio_dir).expect("Failed to rename directory");
break;
}
Err(_) => panic!("Failed to download ASIO SDK"),
}
println!("CPAL_ASIO_DIR is set at {}", asio_dir.display());
asio_dir
}

fn invoke_vcvars() {
Expand Down Expand Up @@ -333,8 +343,7 @@ fn invoke_vcvars() {
}
}

eprintln!(
"Error: Could not find vcvarsall.bat. Please install the latest version of Visual Studio."
panic!(
"Could not find vcvarsall.bat. Please install the latest version of Visual Studio."
);
exit(1);
}

0 comments on commit 1d086b3

Please sign in to comment.