Improve asio sys build script (#809)
* Make build script more robust * Make build dependencies available regardless of target * Fix error about the u32 i32 type mismatch * Update readme * Fix target_arch bug
This commit is contained in:
parent
3a449538b7
commit
efa0c7af6b
4 changed files with 401 additions and 112 deletions
48
README.md
48
README.md
|
|
@ -1,7 +1,7 @@
|
||||||
# CPAL - Cross-Platform Audio Library
|
# CPAL - Cross-Platform Audio Library
|
||||||
|
|
||||||
[](https://github.com/RustAudio/cpal/actions)
|
[](https://github.com/RustAudio/cpal/actions)
|
||||||
[](https://crates.io/crates/cpal) [](https://docs.rs/cpal/)
|
[](https://crates.io/crates/cpal) [](https://docs.rs/cpal/)
|
||||||
|
|
||||||
Low-level library for audio input and output in pure Rust.
|
Low-level library for audio input and output in pure Rust.
|
||||||
|
|
||||||
|
|
@ -51,19 +51,20 @@ WASAPI including access to large numbers of channels and lower-latency audio
|
||||||
processing.
|
processing.
|
||||||
|
|
||||||
CPAL allows for using the ASIO SDK as the audio host on Windows instead of
|
CPAL allows for using the ASIO SDK as the audio host on Windows instead of
|
||||||
WASAPI.
|
WASAPI.
|
||||||
|
|
||||||
### Locating the ASIO SDK
|
### Locating the ASIO SDK
|
||||||
|
|
||||||
The location of ASIO SDK is exposed to CPAL by setting the `CPAL_ASIO_DIR` environment variable.
|
The location of ASIO SDK is exposed to CPAL by setting the `CPAL_ASIO_DIR` environment variable.
|
||||||
|
|
||||||
The build script will try to find the ASIO SDK by following these steps in order:
|
The build script will try to find the ASIO SDK by following these steps in order:
|
||||||
|
|
||||||
1. Check if `CPAL_ASIO_DIR` is set and if so use the path to point to the SDK.
|
1. Check if `CPAL_ASIO_DIR` is set and if so use the path to point to the SDK.
|
||||||
2. Check if the ASIO SDK is already installed in the temporary directory, if so use that and set the path of `CPAL_ASIO_DIR` to the output of `std::env::temp_dir().join("asio_sdk")`.
|
2. Check if the ASIO SDK is already installed in the temporary directory, if so use that and set the path of `CPAL_ASIO_DIR` to the output of `std::env::temp_dir().join("asio_sdk")`.
|
||||||
3. If the ASIO SDK is not already installed, download it from <https://www.steinberg.net/asiosdk> and install it in the temporary directory. The path of `CPAL_ASIO_DIR` will be set to the output of `std::env::temp_dir().join("asio_sdk")`.
|
3. If the ASIO SDK is not already installed, download it from <https://www.steinberg.net/asiosdk> and install it in the temporary directory. The path of `CPAL_ASIO_DIR` will be set to the output of `std::env::temp_dir().join("asio_sdk")`.
|
||||||
|
|
||||||
In an ideal situation you don't need to worry about this step.
|
In an ideal situation you don't need to worry about this step.
|
||||||
|
|
||||||
### Preparing the build environment
|
### Preparing the build environment
|
||||||
|
|
||||||
1. `bindgen`, the library used to generate bindings to the C++ SDK, requires
|
1. `bindgen`, the library used to generate bindings to the C++ SDK, requires
|
||||||
|
|
@ -80,28 +81,33 @@ In an ideal situation you don't need to worry about this step.
|
||||||
and install ASIO4ALL**](http://www.asio4all.org/). Be sure to enable the
|
and install ASIO4ALL**](http://www.asio4all.org/). Be sure to enable the
|
||||||
"offline" feature during installation despite what the installer says about
|
"offline" feature during installation despite what the installer says about
|
||||||
it being useless.
|
it being useless.
|
||||||
4. Our build script assumes that Microsoft Visual Studio is installed. The script will try to find `vcvarsall.bat`
|
4. Our build script assumes that Microsoft Visual Studio is installed if the host OS for compilation is Windows. The script will try to find `vcvarsall.bat`
|
||||||
and execute it with the right machine architecture regardless of the Microsoft Visual Studio version.
|
and execute it with the right host and target machine architecture regardless of the Microsoft Visual Studio version.
|
||||||
If there are any errors encountered in this process which is unlikely,
|
If there are any errors encountered in this process which is unlikely,
|
||||||
you may find the `vcvarsall.bat` manually and execute it with your machine architecture as an argument.
|
you may find the `vcvarsall.bat` manually and execute it with your machine architecture as an argument.
|
||||||
The script will detect this and skip the step.
|
The script will detect this and skip the step.
|
||||||
|
|
||||||
A manually executed command example for 64 bit machines:
|
A manually executed command example for 64 bit machines:
|
||||||
|
|
||||||
```
|
```
|
||||||
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
|
"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64
|
||||||
```
|
```
|
||||||
|
|
||||||
|
For more information please refer to the documentation of [`vcvarsall.bat``](https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-160#vcvarsall-syntax).
|
||||||
|
|
||||||
5. Select the ASIO host at the start of our program with the following code:
|
5. Select the ASIO host at the start of our program with the following code:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
let host;
|
let host;
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
{
|
{
|
||||||
host = cpal::host_from_id(cpal::HostId::Asio).expect("failed to initialise ASIO host");
|
host = cpal::host_from_id(cpal::HostId::Asio).expect("failed to initialise ASIO host");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
If you run into compilations errors produced by `asio-sys` or `bindgen`, make
|
If you run into compilations errors produced by `asio-sys` or `bindgen`, make
|
||||||
sure that `CPAL_ASIO_DIR` is set correctly and try `cargo clean`.
|
sure that `CPAL_ASIO_DIR` is set correctly and try `cargo clean`.
|
||||||
|
|
||||||
6. Make sure to enable the `asio` feature when building CPAL:
|
6. Make sure to enable the `asio` feature when building CPAL:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
@ -114,5 +120,23 @@ In an ideal situation you don't need to worry about this step.
|
||||||
```toml
|
```toml
|
||||||
cpal = { version = "*", features = ["asio"] }
|
cpal = { version = "*", features = ["asio"] }
|
||||||
```
|
```
|
||||||
|
|
||||||
*Updated as of ASIO version 2.3.3.*
|
_Updated as of ASIO version 2.3.3._
|
||||||
|
|
||||||
|
### Cross compilation
|
||||||
|
|
||||||
|
When Windows is the host and the target OS, the build script of `asio-sys` supports all cross compilation targets
|
||||||
|
which are supported by the MSVC compiler. An exhaustive list of combinations could be found [here](https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-160#vcvarsall-syntax) with the addition of undocumented `arm64`, `arm64_x86`, `arm64_amd64` and `arm64_arm` targets. (5.11.2023)
|
||||||
|
|
||||||
|
It is also possible to compile Windows applications with ASIO support on Linux and macOS.
|
||||||
|
|
||||||
|
For both platforms the common way to do this is to use the [MinGW-w64](https://www.mingw-w64.org/) toolchain.
|
||||||
|
|
||||||
|
Make sure that you have included the `MinGW-w64` include directory in your `CPLUS_INCLUDE_PATH` environment variable.
|
||||||
|
Make sure that LLVM is installed and include directory is also included in your `CPLUS_INCLUDE_PATH` environment variable.
|
||||||
|
|
||||||
|
Example for macOS for the target of `x86_64-pc-windows-gnu` where `mingw-w64` is installed via brew:
|
||||||
|
|
||||||
|
```
|
||||||
|
export CPLUS_INCLUDE_PATH="$CPLUS_INCLUDE_PATH:/opt/homebrew/Cellar/mingw-w64/11.0.1/toolchain-x86_64/x86_64-w64-mingw32/include"
|
||||||
|
```
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,13 @@ license = "Apache-2.0"
|
||||||
keywords = ["audio", "sound", "asio", "steinberg"]
|
keywords = ["audio", "sound", "asio", "steinberg"]
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
|
|
||||||
[target.'cfg(any(target_os = "windows"))'.build-dependencies]
|
[build-dependencies]
|
||||||
bindgen = "0.68"
|
bindgen = "0.69"
|
||||||
walkdir = "2"
|
walkdir = "2"
|
||||||
cc = "1.0.83"
|
cc = "1.0.83"
|
||||||
|
parse_cfg = "4.1.1"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
once_cell = "1.12"
|
once_cell = "1.12"
|
||||||
num-derive = "0.3"
|
num-derive = "0.4"
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
extern crate bindgen;
|
extern crate bindgen;
|
||||||
extern crate cc;
|
extern crate cc;
|
||||||
|
extern crate parse_cfg;
|
||||||
extern crate walkdir;
|
extern crate walkdir;
|
||||||
|
|
||||||
|
use parse_cfg::*;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
|
|
@ -14,6 +16,30 @@ const ASIO_HEADER: &str = "asio.h";
|
||||||
const ASIO_SYS_HEADER: &str = "asiosys.h";
|
const ASIO_SYS_HEADER: &str = "asiosys.h";
|
||||||
const ASIO_DRIVERS_HEADER: &str = "asiodrivers.h";
|
const ASIO_DRIVERS_HEADER: &str = "asiodrivers.h";
|
||||||
|
|
||||||
|
/// Checks if the host OS is Windows
|
||||||
|
fn host_os_is_windows() -> bool {
|
||||||
|
std::env::consts::OS == "windows"
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if the target env is MSVC
|
||||||
|
fn is_msvc() -> bool {
|
||||||
|
let target: Target = std::env::var("TARGET")
|
||||||
|
.expect("Target not set.")
|
||||||
|
.parse()
|
||||||
|
.expect("Unable to parse target.");
|
||||||
|
|
||||||
|
let target_env = match target {
|
||||||
|
Target::Triple { env, .. } => env,
|
||||||
|
Target::Cfg(_) => panic!("cfg targets not supported"),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(env) = target_env {
|
||||||
|
env.contains("msvc")
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("cargo:rerun-if-env-changed={}", CPAL_ASIO_DIR);
|
println!("cargo:rerun-if-env-changed={}", CPAL_ASIO_DIR);
|
||||||
|
|
||||||
|
|
@ -29,9 +55,8 @@ fn main() {
|
||||||
let mut lib_path = out_dir.clone();
|
let mut lib_path = out_dir.clone();
|
||||||
lib_path.push("libasio.a");
|
lib_path.push("libasio.a");
|
||||||
if !lib_path.exists() {
|
if !lib_path.exists() {
|
||||||
if !vcvars_set() {
|
if is_msvc() {
|
||||||
println!("VCINSTALLDIR is not set. Attempting to invoke vcvarsall.bat..");
|
invoke_vcvars_if_not_set();
|
||||||
invoke_vcvars();
|
|
||||||
}
|
}
|
||||||
create_lib(&cpal_asio_dir);
|
create_lib(&cpal_asio_dir);
|
||||||
}
|
}
|
||||||
|
|
@ -48,19 +73,18 @@ fn main() {
|
||||||
let mut binding_path = out_dir.clone();
|
let mut binding_path = out_dir.clone();
|
||||||
binding_path.push("asio_bindings.rs");
|
binding_path.push("asio_bindings.rs");
|
||||||
if !binding_path.exists() {
|
if !binding_path.exists() {
|
||||||
if !vcvars_set() {
|
if is_msvc() {
|
||||||
println!("VCINSTALLDIR is not set. Attempting to invoke vcvarsall.bat..");
|
invoke_vcvars_if_not_set();
|
||||||
invoke_vcvars();
|
|
||||||
}
|
}
|
||||||
create_bindings(&cpal_asio_dir);
|
create_bindings(&cpal_asio_dir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_lib(cpal_asio_dir: &PathBuf) {
|
fn create_lib(cpal_asio_dir: &Path) {
|
||||||
let mut cpp_paths: Vec<PathBuf> = Vec::new();
|
let mut cpp_paths: Vec<PathBuf> = Vec::new();
|
||||||
let mut host_dir = cpal_asio_dir.clone();
|
let mut host_dir = cpal_asio_dir.to_path_buf();
|
||||||
let mut pc_dir = cpal_asio_dir.clone();
|
let mut pc_dir = cpal_asio_dir.to_path_buf();
|
||||||
let mut common_dir = cpal_asio_dir.clone();
|
let mut common_dir = cpal_asio_dir.to_path_buf();
|
||||||
host_dir.push("host");
|
host_dir.push("host");
|
||||||
common_dir.push("common");
|
common_dir.push("common");
|
||||||
pc_dir.push("host/pc");
|
pc_dir.push("host/pc");
|
||||||
|
|
@ -112,7 +136,7 @@ fn create_bindings(cpal_asio_dir: &PathBuf) {
|
||||||
let mut asio_drivers_header = None;
|
let mut asio_drivers_header = None;
|
||||||
|
|
||||||
// Recursively walk given cpal dir to find required headers
|
// Recursively walk given cpal dir to find required headers
|
||||||
for entry in WalkDir::new(&cpal_asio_dir) {
|
for entry in WalkDir::new(cpal_asio_dir) {
|
||||||
let entry = match entry {
|
let entry = match entry {
|
||||||
Err(_) => continue,
|
Err(_) => continue,
|
||||||
Ok(entry) => entry,
|
Ok(entry) => entry,
|
||||||
|
|
@ -206,12 +230,23 @@ fn create_bindings(cpal_asio_dir: &PathBuf) {
|
||||||
|
|
||||||
// Write the bindings to the $OUT_DIR/bindings.rs file.
|
// Write the bindings to the $OUT_DIR/bindings.rs file.
|
||||||
let out_path = PathBuf::from(env::var("OUT_DIR").expect("bad path"));
|
let out_path = PathBuf::from(env::var("OUT_DIR").expect("bad path"));
|
||||||
//panic!("path: {}", out_path.display());
|
|
||||||
bindings
|
bindings
|
||||||
.write_to_file(out_path.join("asio_bindings.rs"))
|
.write_to_file(out_path.join("asio_bindings.rs"))
|
||||||
.expect("Couldn't write bindings!");
|
.expect("Couldn't write bindings!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the ASIO SDK directory
|
||||||
|
///
|
||||||
|
/// If the CPAL_ASIO_DIR env var is set, it will use that.
|
||||||
|
///
|
||||||
|
/// If not set, it will check the temp directory for the ASIO SDK.
|
||||||
|
///
|
||||||
|
/// If not found, it will download the ASIO SDK to the temp directory.
|
||||||
|
///
|
||||||
|
/// It will then move the contents of the inner directory to the temp directory.
|
||||||
|
///
|
||||||
|
/// It will then return the path to the ASIO SDK directory.
|
||||||
fn get_asio_dir() -> PathBuf {
|
fn get_asio_dir() -> PathBuf {
|
||||||
// Check if CPAL_ASIO_DIR env var is set
|
// Check if CPAL_ASIO_DIR env var is set
|
||||||
if let Ok(path) = env::var(CPAL_ASIO_DIR) {
|
if let Ok(path) = env::var(CPAL_ASIO_DIR) {
|
||||||
|
|
@ -230,42 +265,7 @@ fn get_asio_dir() -> PathBuf {
|
||||||
// If not found, download ASIO SDK using PowerShell's Invoke-WebRequest
|
// 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}",);
|
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");
|
download_asio_sdk_to_temp_dir(&temp_dir);
|
||||||
let status = Command::new("powershell")
|
|
||||||
.args(&[
|
|
||||||
"-NoProfile",
|
|
||||||
"-Command",
|
|
||||||
&format!(
|
|
||||||
"Invoke-WebRequest -Uri {ASIO_SDK_URL} -OutFile {}",
|
|
||||||
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
|
// Move the contents of the inner directory to asio_dir
|
||||||
for entry in walkdir::WalkDir::new(&temp_dir).min_depth(1).max_depth(1) {
|
for entry in walkdir::WalkDir::new(&temp_dir).min_depth(1).max_depth(1) {
|
||||||
|
|
@ -279,37 +279,160 @@ fn get_asio_dir() -> PathBuf {
|
||||||
asio_dir
|
asio_dir
|
||||||
}
|
}
|
||||||
|
|
||||||
fn invoke_vcvars() {
|
/// Downloads the ASIO SDK to the temp directory of the host OS
|
||||||
|
///
|
||||||
|
/// It uses powershell's Invoke-WebRequest on Windows and curl on other platforms to download the SDK.
|
||||||
|
///
|
||||||
|
/// It then extracts the SDK using powershell's Expand-Archive on Windows and unzip on other platforms.
|
||||||
|
fn download_asio_sdk_to_temp_dir(temp_dir: &Path) {
|
||||||
|
let asio_zip_path = temp_dir.join("asio_sdk.zip");
|
||||||
|
if host_os_is_windows() {
|
||||||
|
let status = Command::new("powershell")
|
||||||
|
.args([
|
||||||
|
"-NoProfile",
|
||||||
|
"-Command",
|
||||||
|
&format!(
|
||||||
|
"Invoke-WebRequest -Uri {ASIO_SDK_URL} -OutFile {}",
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let status = Command::new("sh")
|
||||||
|
.arg("-c")
|
||||||
|
.arg(&format!(
|
||||||
|
"curl -L --fail --output {} {}",
|
||||||
|
asio_zip_path.display(),
|
||||||
|
"https://www.steinberg.net/asiosdk" // Replace with the actual ASIO SDK URL
|
||||||
|
))
|
||||||
|
.status()
|
||||||
|
.expect("Failed to execute curl command");
|
||||||
|
|
||||||
|
if !status.success() {
|
||||||
|
panic!("Failed to download ASIO SDK");
|
||||||
|
}
|
||||||
|
println!("Downloaded ASIO SDK successfully");
|
||||||
|
|
||||||
|
// Extract using `unzip`
|
||||||
|
println!("Extracting ASIO SDK..");
|
||||||
|
let status = Command::new("unzip")
|
||||||
|
.args([
|
||||||
|
"-o",
|
||||||
|
asio_zip_path.to_str().unwrap(),
|
||||||
|
"-d",
|
||||||
|
temp_dir.to_str().unwrap(),
|
||||||
|
])
|
||||||
|
.status()
|
||||||
|
.expect("Failed to execute unzip command for extracting ASIO SDK");
|
||||||
|
|
||||||
|
if !status.success() {
|
||||||
|
panic!("Failed to extract ASIO SDK");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Invokes `vcvarsall.bat` to initialize the environment for building with MSVC
|
||||||
|
///
|
||||||
|
/// This function is only meant to be called when the host OS is Windows.
|
||||||
|
fn invoke_vcvars_if_not_set() {
|
||||||
|
if vcvars_set() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
println!("VCINSTALLDIR is not set. Attempting to invoke vcvarsall.bat..");
|
||||||
|
|
||||||
println!("Invoking vcvarsall.bat..");
|
println!("Invoking vcvarsall.bat..");
|
||||||
println!("Determining system architecture..");
|
println!("Determining system architecture..");
|
||||||
|
|
||||||
// Determine the system architecture to be used as an argument to vcvarsall.bat
|
let arch_arg = determine_vcvarsall_bat_arch_arg();
|
||||||
let arch = if cfg!(target_arch = "x86_64") {
|
println!(
|
||||||
"amd64"
|
"Host architecture is detected as {}.",
|
||||||
} else if cfg!(target_arch = "x86") {
|
std::env::consts::ARCH
|
||||||
"x86"
|
);
|
||||||
} else if cfg!(target_arch = "arm") {
|
println!("Architecture argument for vcvarsall.bat will be used as: {arch_arg}.");
|
||||||
"arm"
|
|
||||||
} else if cfg!(target_arch = "aarch64") {
|
|
||||||
"arm64"
|
|
||||||
} else {
|
|
||||||
panic!("Unsupported architecture");
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("Architecture detected as {arch}.");
|
let vcvars_all_bat_path = search_vcvars_all_bat();
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"Found vcvarsall.bat at {}. Initializing environment..",
|
||||||
|
vcvars_all_bat_path.display()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Invoke vcvarsall.bat
|
||||||
|
let output = Command::new("cmd")
|
||||||
|
.args([
|
||||||
|
"/c",
|
||||||
|
vcvars_all_bat_path.to_str().unwrap(),
|
||||||
|
&arch_arg,
|
||||||
|
"&&",
|
||||||
|
"set",
|
||||||
|
])
|
||||||
|
.output()
|
||||||
|
.expect("Failed to execute command");
|
||||||
|
|
||||||
|
for line in String::from_utf8_lossy(&output.stdout).lines() {
|
||||||
|
// Filters the output of vcvarsall.bat to only include lines of the form "VARNAME=VALUE"
|
||||||
|
let parts: Vec<&str> = line.splitn(2, '=').collect();
|
||||||
|
if parts.len() == 2 {
|
||||||
|
env::set_var(parts[0], parts[1]);
|
||||||
|
println!("{}={}", parts[0], parts[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if vcvarsall.bat has been invoked
|
||||||
|
/// Assumes that it is very unlikely that the user would set `VCINSTALLDIR` manually
|
||||||
|
fn vcvars_set() -> bool {
|
||||||
|
env::var("VCINSTALLDIR").is_ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Searches for vcvarsall.bat in the default installation directories
|
||||||
|
///
|
||||||
|
/// If it is not found, it will search for it in the Program Files directories
|
||||||
|
///
|
||||||
|
/// If it is still not found, it will panic.
|
||||||
|
fn search_vcvars_all_bat() -> PathBuf {
|
||||||
|
if let Some(path) = guess_vcvars_all_bat() {
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
// Define search paths for vcvarsall.bat based on architecture
|
// Define search paths for vcvarsall.bat based on architecture
|
||||||
let paths = if arch == "amd64" {
|
let paths = &[
|
||||||
vec![
|
// Visual Studio 2022+
|
||||||
"C:\\Program Files (x86)\\Microsoft Visual Studio\\",
|
"C:\\Program Files\\Microsoft Visual Studio\\",
|
||||||
"C:\\Program Files\\Microsoft Visual Studio\\",
|
// <= Visual Studio 2019
|
||||||
]
|
"C:\\Program Files (x86)\\Microsoft Visual Studio\\",
|
||||||
} else {
|
];
|
||||||
vec!["C:\\Program Files\\Microsoft Visual Studio\\"]
|
|
||||||
};
|
|
||||||
|
|
||||||
// Search for vcvarsall.bat using walkdir
|
// Search for vcvarsall.bat using walkdir
|
||||||
println!("Searching for vcvarsall.bat in {paths:?}");
|
println!("Searching for vcvarsall.bat in {paths:?}");
|
||||||
|
|
||||||
|
let mut found = None;
|
||||||
|
|
||||||
for path in paths.iter() {
|
for path in paths.iter() {
|
||||||
for entry in WalkDir::new(path)
|
for entry in WalkDir::new(path)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|
@ -317,34 +440,156 @@ fn invoke_vcvars() {
|
||||||
.filter(|e| !e.file_type().is_dir())
|
.filter(|e| !e.file_type().is_dir())
|
||||||
{
|
{
|
||||||
if entry.path().ends_with("vcvarsall.bat") {
|
if entry.path().ends_with("vcvarsall.bat") {
|
||||||
println!(
|
found.replace(entry.path().to_path_buf());
|
||||||
"Found vcvarsall.bat at {}. Initializing environment..",
|
|
||||||
entry.path().display()
|
|
||||||
);
|
|
||||||
|
|
||||||
// Invoke vcvarsall.bat
|
|
||||||
let output = Command::new("cmd")
|
|
||||||
.args(&["/c", entry.path().to_str().unwrap(), &arch, "&&", "set"])
|
|
||||||
.output()
|
|
||||||
.expect("Failed to execute command");
|
|
||||||
|
|
||||||
for line in String::from_utf8_lossy(&output.stdout).lines() {
|
|
||||||
// Filters the output of vcvarsall.bat to only include lines of the form "VARNAME=VALUE"
|
|
||||||
let parts: Vec<&str> = line.splitn(2, '=').collect();
|
|
||||||
if parts.len() == 2 {
|
|
||||||
env::set_var(parts[0], parts[1]);
|
|
||||||
println!("{}={}", parts[0], parts[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
panic!("Could not find vcvarsall.bat. Please install the latest version of Visual Studio.");
|
match found {
|
||||||
|
Some(path) => path,
|
||||||
|
None => panic!(
|
||||||
|
"Could not find vcvarsall.bat. Please install the latest version of Visual Studio."
|
||||||
|
),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Checks if vcvarsall.bat has been invoked
|
|
||||||
// Assumes that it is very unlikely that the user would set VCINSTALLDIR manually
|
/// Guesses the location of vcvarsall.bat by searching it with certain heuristics.
|
||||||
fn vcvars_set() -> bool {
|
///
|
||||||
env::var("VCINSTALLDIR").is_ok()
|
/// It is meant to be executed before a top level search over Microsoft Visual Studio directories
|
||||||
|
/// to ensure faster execution in CI environments.
|
||||||
|
fn guess_vcvars_all_bat() -> Option<PathBuf> {
|
||||||
|
/// Checks if a string is a year
|
||||||
|
fn is_year(s: Option<&str>) -> Option<String> {
|
||||||
|
let Some(s) = s else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
if s.len() == 4 && s.chars().all(|c| c.is_ascii_digit()) {
|
||||||
|
Some(s.to_string())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if a string is an edition of Visual Studio
|
||||||
|
fn is_edition(s: Option<&str>) -> Option<String> {
|
||||||
|
let Some(s) = s else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
|
let editions = ["Enterprise", "Professional", "Community", "Express"];
|
||||||
|
if editions.contains(&s) {
|
||||||
|
Some(s.to_string())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Constructs a path to vcvarsall.bat based on a base path
|
||||||
|
fn construct_path(base: &Path) -> Option<PathBuf> {
|
||||||
|
let mut constructed = base.to_path_buf();
|
||||||
|
for entry in WalkDir::new(&constructed).max_depth(1) {
|
||||||
|
let entry = match entry {
|
||||||
|
Err(_) => continue,
|
||||||
|
Ok(entry) => entry,
|
||||||
|
};
|
||||||
|
if let Some(year) = is_year(entry.path().file_name().and_then(|s| s.to_str())) {
|
||||||
|
constructed = constructed.join(year);
|
||||||
|
for entry in WalkDir::new(&constructed).max_depth(1) {
|
||||||
|
let entry = match entry {
|
||||||
|
Err(_) => continue,
|
||||||
|
Ok(entry) => entry,
|
||||||
|
};
|
||||||
|
if let Some(edition) =
|
||||||
|
is_edition(entry.path().file_name().and_then(|s| s.to_str()))
|
||||||
|
{
|
||||||
|
constructed = constructed
|
||||||
|
.join(edition)
|
||||||
|
.join("VC")
|
||||||
|
.join("Auxiliary")
|
||||||
|
.join("Build")
|
||||||
|
.join("vcvarsall.bat");
|
||||||
|
|
||||||
|
return Some(constructed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
let vs_2022_and_onwards_base = PathBuf::from("C:\\Program Files\\Microsoft Visual Studio\\");
|
||||||
|
let vs_2019_and_2017_base = PathBuf::from("C:\\Program Files (x86)\\Microsoft Visual Studio\\");
|
||||||
|
|
||||||
|
construct_path(&vs_2022_and_onwards_base).map_or_else(
|
||||||
|
|| construct_path(&vs_2019_and_2017_base).map_or_else(|| None, Some),
|
||||||
|
Some,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Determines the right argument to pass to `vcvarsall.bat` based on the host and target architectures.
|
||||||
|
///
|
||||||
|
/// Windows on ARM is not supporting 32 bit arm processors.
|
||||||
|
/// Because of this there is no native or cross compilation is supported for 32 bit arm processors.
|
||||||
|
fn determine_vcvarsall_bat_arch_arg() -> String {
|
||||||
|
let host_architecture = std::env::consts::ARCH;
|
||||||
|
let target_architecture = std::env::var("CARGO_CFG_TARGET_ARCH").expect("Target not set.");
|
||||||
|
|
||||||
|
let arch_arg = if target_architecture == "x86_64" {
|
||||||
|
if host_architecture == "x86" {
|
||||||
|
// Arg for cross compilation from x86 to x64
|
||||||
|
"x86_amd64"
|
||||||
|
} else if host_architecture == "x86_64" {
|
||||||
|
// Arg for native compilation from x64 to x64
|
||||||
|
"amd64"
|
||||||
|
} else if host_architecture == "aarch64" {
|
||||||
|
// Arg for cross compilation from arm64 to amd64
|
||||||
|
"arm64_amd64"
|
||||||
|
} else {
|
||||||
|
panic!("Unsupported host architecture {}", host_architecture);
|
||||||
|
}
|
||||||
|
} else if target_architecture == "x86" {
|
||||||
|
if host_architecture == "x86" {
|
||||||
|
// Arg for native compilation from x86 to x86
|
||||||
|
"x86"
|
||||||
|
} else if host_architecture == "x86_64" {
|
||||||
|
// Arg for cross compilation from x64 to x86
|
||||||
|
"amd64_x86"
|
||||||
|
} else if host_architecture == "aarch64" {
|
||||||
|
// Arg for cross compilation from arm64 to x86
|
||||||
|
"arm64_x86"
|
||||||
|
} else {
|
||||||
|
panic!("Unsupported host architecture {}", host_architecture);
|
||||||
|
}
|
||||||
|
} else if target_architecture == "arm" {
|
||||||
|
if host_architecture == "x86" {
|
||||||
|
// Arg for cross compilation from x86 to arm
|
||||||
|
"x86_arm"
|
||||||
|
} else if host_architecture == "x86_64" {
|
||||||
|
// Arg for cross compilation from x64 to arm
|
||||||
|
"amd64_arm"
|
||||||
|
} else if host_architecture == "aarch64" {
|
||||||
|
// Arg for cross compilation from arm64 to arm
|
||||||
|
"arm64_arm"
|
||||||
|
} else {
|
||||||
|
panic!("Unsupported host architecture {}", host_architecture);
|
||||||
|
}
|
||||||
|
} else if target_architecture == "aarch64" {
|
||||||
|
if host_architecture == "x86" {
|
||||||
|
// Arg for cross compilation from x86 to arm
|
||||||
|
"x86_arm64"
|
||||||
|
} else if host_architecture == "x86_64" {
|
||||||
|
// Arg for cross compilation from x64 to arm
|
||||||
|
"amd64_arm64"
|
||||||
|
} else if host_architecture == "aarch64" {
|
||||||
|
// Arg for native compilation from arm64 to arm64
|
||||||
|
"arm64"
|
||||||
|
} else {
|
||||||
|
panic!("Unsupported host architecture {}", host_architecture);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
panic!("Unsupported target architecture.");
|
||||||
|
};
|
||||||
|
|
||||||
|
arch_arg.to_owned()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1022,7 +1022,26 @@ extern "C" fn buffer_switch(double_buffer_index: c_long, direct_process: c_long)
|
||||||
if let Ok(()) = asio_result!(res) {
|
if let Ok(()) = asio_result!(res) {
|
||||||
time.time_info.flags = (ai::AsioTimeInfoFlags::kSystemTimeValid
|
time.time_info.flags = (ai::AsioTimeInfoFlags::kSystemTimeValid
|
||||||
| ai::AsioTimeInfoFlags::kSamplePositionValid)
|
| ai::AsioTimeInfoFlags::kSamplePositionValid)
|
||||||
.0;
|
// Context about the cast:
|
||||||
|
//
|
||||||
|
// Cast was required to successfully compile with MinGW-w64.
|
||||||
|
//
|
||||||
|
// The flags defined will not create a value that exceeds the maximum value of an i32.
|
||||||
|
// The flags are intended to be non-negative, so the sign bit will not be used.
|
||||||
|
// The c_uint (flags) is being cast to i32 which is safe as long as the actual value fits within the i32 range, which is true in this case.
|
||||||
|
//
|
||||||
|
// The actual flags in asio sdk are defined as:
|
||||||
|
// typedef enum AsioTimeInfoFlags
|
||||||
|
// {
|
||||||
|
// kSystemTimeValid = 1, // must always be valid
|
||||||
|
// kSamplePositionValid = 1 << 1, // must always be valid
|
||||||
|
// kSampleRateValid = 1 << 2,
|
||||||
|
// kSpeedValid = 1 << 3,
|
||||||
|
//
|
||||||
|
// kSampleRateChanged = 1 << 4,
|
||||||
|
// kClockSourceChanged = 1 << 5
|
||||||
|
// } AsioTimeInfoFlags;
|
||||||
|
.0 as i32;
|
||||||
}
|
}
|
||||||
time
|
time
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue