-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve packaging of libprojectM source
- Loading branch information
Showing
2 changed files
with
63 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,141 +1,67 @@ | ||
extern crate lazy_static; | ||
|
||
use lazy_static::lazy_static; | ||
use std::env; | ||
use std::path::PathBuf; | ||
use std::process::{Command, Stdio}; | ||
|
||
mod build_bindgen; | ||
use crate::build_bindgen::bindgen; | ||
|
||
fn update_submodules() -> Result<(), Box<dyn std::error::Error>> { | ||
let status = Command::new("git") | ||
.args(["submodule", "update", "--init", "--recursive"]) | ||
.stdout(Stdio::inherit()) // Optionally output stdout/stderr to help with debugging | ||
.stderr(Stdio::inherit()) | ||
.status()?; | ||
|
||
if !status.success() { | ||
return Err(Box::new(std::io::Error::new( | ||
std::io::ErrorKind::Other, | ||
"Submodule update failed", | ||
))); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
let projectm_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("libprojectM"); | ||
|
||
// Ensure the submodule is updated and initialized | ||
// Check if the libprojectM source code exists | ||
if !projectm_path.exists() { | ||
println!("cargo:warning=The libprojectM submodule is not checked out. Please run 'git submodule update --init --recursive' and try building again."); | ||
std::process::exit(1); | ||
} | ||
|
||
// Attempt to update and initialize submodules recursively | ||
if let Err(e) = update_submodules() { | ||
println!("cargo:warning=Failed to update submodules: {}", e); | ||
println!("cargo:warning=The libprojectM source code is missing."); | ||
println!("cargo:warning=If you are building from a git clone, please run 'git submodule update --init --recursive'."); | ||
println!("cargo:warning=If you downloaded this crate from crates.io, please ensure that the crate was packaged correctly."); | ||
std::process::exit(1); | ||
} | ||
|
||
// Feature: enable-playlist | ||
fn enable_playlist() -> String { | ||
if cfg!(feature = "playlist") { | ||
"ON".to_string() | ||
} else { | ||
"OFF".to_string() | ||
} | ||
} | ||
|
||
#[cfg(target_os = "windows")] | ||
let dst = cmake::Config::new(&*projectm_path) | ||
.generator("Visual Studio 17 2022") | ||
.define( | ||
"CMAKE_TOOLCHAIN_FILE", | ||
format!( | ||
"{}/scripts/buildsystems/vcpkg.cmake", | ||
env::var("VCPKG_INSTALLATION_ROOT").unwrap() | ||
), | ||
) | ||
.define("VCPKG_TARGET_TRIPLET", "x64-windows-static-md") | ||
.define( | ||
"CMAKE_MSVC_RUNTIME_LIBRARY", | ||
"MultiThreaded$<$<CONFIG:Debug>:Debug>DLL", | ||
) | ||
.define("ENABLE_PLAYLIST", enable_playlist().as_str()) | ||
.build(); | ||
|
||
#[cfg(target_os = "linux")] | ||
let dst = cmake::Config::new(&*projectm_path) | ||
.define("ENABLE_PLAYLIST", enable_playlist().as_str()) | ||
.build(); | ||
|
||
#[cfg(target_os = "macos")] | ||
let dst = cmake::Config::new(&*projectm_path) | ||
.define("ENABLE_PLAYLIST", enable_playlist().as_str()) | ||
.build(); | ||
|
||
#[cfg(target_os = "emscripten")] | ||
let dst = cmake::Config::new(&*projectm_path) | ||
.define("ENABLE_PLAYLIST", enable_playlist().as_str()) | ||
.define("ENABLE_EMSCRIPTEN", "ON") | ||
.build(); | ||
|
||
println!("cargo:rustc-link-search=native={}/lib", dst.display()); | ||
|
||
#[cfg(target_os = "windows")] | ||
if Ok("release".to_owned()) == env::var("PROFILE") { | ||
println!("cargo:rustc-link-lib=dylib=projectM-4"); | ||
|
||
#[cfg(feature = "playlist")] | ||
println!("cargo:rustc-link-lib=dylib=projectM-4-playlist"); | ||
} else { | ||
println!("cargo:rustc-link-lib=dylib=projectM-4d"); | ||
|
||
#[cfg(feature = "playlist")] | ||
println!("cargo:rustc-link-lib=dylib=projectM-4-playlistd"); | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
if Ok("release".to_owned()) == env::var("PROFILE") { | ||
println!("cargo:rustc-link-lib=dylib=libprojectM=4"); | ||
|
||
#[cfg(feature = "playlist")] | ||
println!("cargo:rustc-link-lib=dylib=projectM-4-playlist"); | ||
// Determine if the 'playlist' feature is enabled | ||
let enable_playlist = if cfg!(feature = "playlist") { | ||
"ON" | ||
} else { | ||
println!("cargo:rustc-link-lib=dylib=projectM-4d"); | ||
|
||
#[cfg(feature = "playlist")] | ||
println!("cargo:rustc-link-lib=dylib=projectM-4-playlistd"); | ||
"OFF" | ||
}; | ||
|
||
// Configure and build libprojectM using CMake | ||
let mut cmake_config = cmake::Config::new(&projectm_path); | ||
cmake_config.define("ENABLE_PLAYLIST", enable_playlist); | ||
|
||
// Platform-specific configurations | ||
if cfg!(target_os = "windows") { | ||
// Ensure VCPKG is set up correctly | ||
let vcpkg_root = env::var("VCPKG_INSTALLATION_ROOT").expect("VCPKG_INSTALLATION_ROOT is not set"); | ||
cmake_config | ||
.generator("Visual Studio 17 2022") | ||
.define( | ||
"CMAKE_TOOLCHAIN_FILE", | ||
format!("{}/scripts/buildsystems/vcpkg.cmake", vcpkg_root), | ||
) | ||
.define("VCPKG_TARGET_TRIPLET", "x64-windows-static-md") | ||
.define( | ||
"CMAKE_MSVC_RUNTIME_LIBRARY", | ||
"MultiThreaded$<$<CONFIG:Debug>:Debug>DLL", | ||
); | ||
} else if cfg!(target_os = "emscripten") { | ||
cmake_config.define("ENABLE_EMSCRIPTEN", "ON"); | ||
} | ||
|
||
#[cfg(target_os = "macos")] | ||
if Ok("release".to_owned()) == env::var("PROFILE") { | ||
println!("cargo:rustc-link-lib=dylib=projectM-4"); | ||
|
||
#[cfg(feature = "playlist")] | ||
println!("cargo:rustc-link-lib=dylib=projectM-4-playlist"); | ||
} else { | ||
println!("cargo:rustc-link-lib=dylib=projectM-4d"); | ||
let dst = cmake_config.build(); | ||
|
||
#[cfg(feature = "playlist")] | ||
println!("cargo:rustc-link-lib=dylib=projectM-4-playlistd"); | ||
} | ||
println!("cargo:rustc-link-search=native={}/lib", dst.display()); | ||
|
||
#[cfg(target_os = "emscripten")] | ||
if Ok("release".to_owned()) == env::var("PROFILE") { | ||
println!("cargo:rustc-link-lib=static=projectM-4"); | ||
// Determine the library name based on the build profile | ||
let profile = env::var("PROFILE").unwrap(); | ||
let lib_suffix = if profile == "release" { "" } else { "d" }; | ||
let lib_name = format!("projectM-4{}", lib_suffix); | ||
|
||
#[cfg(feature = "playlist")] | ||
println!("cargo:rustc-link-lib=dylib=projectM-4-playlist"); | ||
} else { | ||
println!("cargo:rustc-link-lib=static=projectM-4d"); | ||
println!("cargo:rustc-link-lib=dylib={}", lib_name); | ||
|
||
#[cfg(feature = "playlist")] | ||
println!("cargo:rustc-link-lib=dylib=projectM-4-playlistd"); | ||
// Handle the 'playlist' feature | ||
if cfg!(feature = "playlist") { | ||
let playlist_lib_name = format!("{}-playlist", lib_name); | ||
println!("cargo:rustc-link-lib=dylib={}", playlist_lib_name); | ||
} | ||
|
||
bindgen() | ||
} | ||
// Run bindgen to generate Rust bindings | ||
bindgen(); | ||
} |