windows build fixes
This commit is contained in:
+16
-4
@@ -1,8 +1,21 @@
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
/// Canonicalize a path without the \\?\ prefix on Windows
|
||||
fn clean_canonicalize(p: &Path) -> String {
|
||||
let canon = p.canonicalize().unwrap();
|
||||
let s = canon.to_str().unwrap();
|
||||
s.strip_prefix(r"\\?\").unwrap_or(s).to_string()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let dst = cmake::Config::new("../vendor/NeuralAudio")
|
||||
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
|
||||
let wrapper_dir = Path::new(&manifest_dir).join("cmake");
|
||||
let neural_audio_dir = Path::new(&manifest_dir).join("../vendor/NeuralAudio");
|
||||
|
||||
let dst = cmake::Config::new(&wrapper_dir)
|
||||
.define("CMAKE_BUILD_TYPE", "Release")
|
||||
.define("NEURALAUDIO_SOURCE_DIR", clean_canonicalize(&neural_audio_dir))
|
||||
.define("BUILD_NAMCORE", "OFF")
|
||||
.define("BUILD_STATIC_RTNEURAL", "OFF")
|
||||
.define("BUILD_UTILS", "OFF")
|
||||
@@ -14,9 +27,8 @@ fn main() {
|
||||
|
||||
let build_dir = dst.join("build");
|
||||
|
||||
// Static libraries land in the build subdirectories
|
||||
println!("cargo:rustc-link-search=native={}", build_dir.join("NeuralAudioCAPI").display());
|
||||
println!("cargo:rustc-link-search=native={}", build_dir.join("NeuralAudio").display());
|
||||
println!("cargo:rustc-link-search=native={}", build_dir.join("NeuralAudio").join("NeuralAudioCAPI").display());
|
||||
println!("cargo:rustc-link-search=native={}", build_dir.join("NeuralAudio").join("NeuralAudio").display());
|
||||
|
||||
println!("cargo:rustc-link-lib=static=NeuralAudioCAPI");
|
||||
println!("cargo:rustc-link-lib=static=NeuralAudio");
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(NeuralAudioStatic)
|
||||
|
||||
# Override add_library to force STATIC even when SHARED is specified
|
||||
macro(add_library name)
|
||||
set(_args ${ARGN})
|
||||
list(FIND _args "SHARED" _shared_idx)
|
||||
if(NOT _shared_idx EQUAL -1)
|
||||
list(REMOVE_AT _args ${_shared_idx})
|
||||
_add_library(${name} STATIC ${_args})
|
||||
else()
|
||||
_add_library(${name} ${_args})
|
||||
endif()
|
||||
endmacro()
|
||||
|
||||
add_subdirectory(${NEURALAUDIO_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/NeuralAudio)
|
||||
+25
-9
@@ -1,9 +1,14 @@
|
||||
use std::ffi::CString;
|
||||
use std::path::Path;
|
||||
|
||||
#[cfg(windows)]
|
||||
type WChar = u16;
|
||||
#[cfg(not(windows))]
|
||||
type WChar = u32;
|
||||
|
||||
#[allow(dead_code)]
|
||||
mod ffi {
|
||||
use std::os::raw::{c_char, c_float, c_int};
|
||||
use super::WChar;
|
||||
use std::os::raw::{c_float, c_int};
|
||||
|
||||
#[repr(C)]
|
||||
pub struct NeuralModel {
|
||||
@@ -11,7 +16,7 @@ mod ffi {
|
||||
}
|
||||
|
||||
unsafe extern "C" {
|
||||
pub fn CreateModelFromFile(model_path: *const c_char) -> *mut NeuralModel;
|
||||
pub fn CreateModelFromFile(model_path: *const WChar) -> *mut NeuralModel;
|
||||
pub fn DeleteModel(model: *mut NeuralModel);
|
||||
|
||||
pub fn SetLSTMLoadMode(load_mode: c_int);
|
||||
@@ -37,14 +42,12 @@ mod ffi {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum NamError {
|
||||
NullPath,
|
||||
ModelLoadFailed(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for NamError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
NamError::NullPath => write!(f, "Path contains null byte"),
|
||||
NamError::ModelLoadFailed(path) => write!(f, "Failed to load NAM model: {}", path),
|
||||
}
|
||||
}
|
||||
@@ -56,12 +59,25 @@ pub struct NamModel {
|
||||
|
||||
impl NamModel {
|
||||
pub fn from_file(path: &Path) -> Result<Self, NamError> {
|
||||
let path_str = path.to_string_lossy();
|
||||
let c_path = CString::new(path_str.as_bytes()).map_err(|_| NamError::NullPath)?;
|
||||
let wide: Vec<WChar> = {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
path.as_os_str().encode_wide().chain(std::iter::once(0)).collect()
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
path.to_string_lossy()
|
||||
.chars()
|
||||
.map(|c| c as WChar)
|
||||
.chain(std::iter::once(0))
|
||||
.collect()
|
||||
}
|
||||
};
|
||||
|
||||
let ptr = unsafe { ffi::CreateModelFromFile(c_path.as_ptr()) };
|
||||
let ptr = unsafe { ffi::CreateModelFromFile(wide.as_ptr()) };
|
||||
if ptr.is_null() {
|
||||
return Err(NamError::ModelLoadFailed(path_str.into_owned()));
|
||||
return Err(NamError::ModelLoadFailed(path.display().to_string()));
|
||||
}
|
||||
|
||||
Ok(NamModel { ptr })
|
||||
|
||||
Reference in New Issue
Block a user