Skip to content

Commit

Permalink
improve packaging of libprojectM source
Browse files Browse the repository at this point in the history
  • Loading branch information
revmischa committed Oct 31, 2024
1 parent 946b74f commit a35b603
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 135 deletions.
34 changes: 18 additions & 16 deletions projectm-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "projectm-sys"
version = "4.1.2"
version = "4.1.2-1"
edition = "2021"
rust-version = "1.65"
authors = ["AnomieVision <anomievision@gmail.com.com>", "Mischa Spiegelmock <me@mish.dev>"]
Expand All @@ -13,29 +13,31 @@ categories = ["multimedia", "multimedia::video", "multimedia::audio"]
readme = "README.md"
links = "projectm"

include = ["src/**",
include = [
"src/**",
"Cargo.toml",
"build.rs",
"README.md",
"LICENSE",
"build_bindgen.rs",
"libprojectM/CMakeLists.txt",
"libprojectM/src/**",
"libprojectM/include/**",
"libprojectM/presets/**",
"libprojectM/cmake/**",
"libprojectM/vendor/**",
"libprojectM/vendor/projectm-eval/**",
"libprojectM/vendor/**/CMakeLists.txt",
"libprojectM/vendor/**/cmake/**",
"libprojectM/**/*.cmake",
"libprojectM/**/*.h",
"libprojectM/**/*.hpp",
"libprojectM/config*",
"libprojectM/**",
"bindgen/**",
]
exclude = ["docs/**", "libprojectM/docs/**", "libprojectM/web/**", "libprojectM/screenshots/**"]

# Exclude unnecessary files to reduce crate size
exclude = [
"docs/**",
"libprojectM/docs/**",
"libprojectM/web/**",
"libprojectM/screenshots/**",
"libprojectM/tests/**",
"libprojectM/examples/**",
"libprojectM/.git/**",
"libprojectM/.github/**",
"libprojectM/*.md",
".git/**",
".gitmodules",
]

[dependencies]

Expand Down
164 changes: 45 additions & 119 deletions projectm-sys/build.rs
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();
}

0 comments on commit a35b603

Please sign in to comment.