Skip to content

Commit b6fd3ab

Browse files
Build Script Refinements/Static Linking (#16)
* fix broken release build on linux * simplify build script and add static linking feature * add cargo config for linux * static linking flag logic fix * fix lifetimes --------- Co-authored-by: Mischa Spiegelmock <[email protected]>
1 parent 7fb2aea commit b6fd3ab

File tree

4 files changed

+104
-45
lines changed

4 files changed

+104
-45
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ readme = "README.md"
1414
[dependencies]
1515
libc = "0.2"
1616
#projectm-sys = { path = "projectm-sys", version = "1.0.9-rc.1", features = ["playlist"] }
17-
projectm-sys = { version = "1.0.10" }
17+
projectm-sys = { version = "1.1.0-alpha.1" }
1818
rand = "0.8"
1919

2020
[features]
2121
default = ["playlist"]
22-
playlist = ["projectm-sys/playlist"]
22+
playlist = ["projectm-sys/playlist"]
23+
static = ["projectm-sys/static"]

projectm-sys/.cargo/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
[target.wasm32-unknown-emscripten]
22
linker = ".cargo/linker-emscripten"
3+
4+
[target.x86_64-unknown-linux-gnu]
5+
rustflags = "-C link-arg=-lGL -C link-arg=-lstdc++ -C link-arg=-lgomp"

projectm-sys/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "projectm-sys"
3-
version = "1.0.10"
3+
version = "1.1.0-alpha.1"
44
edition = "2021"
55
rust-version = "1.65"
66
authors = ["AnomieVision <[email protected]>", "Mischa Spiegelmock <[email protected]>"]
@@ -47,3 +47,5 @@ lazy_static = "1.4.0"
4747
[features]
4848
default = ["playlist"]
4949
playlist = []
50+
static = []
51+

projectm-sys/build.rs

Lines changed: 95 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,29 @@ use std::path::PathBuf;
44
mod build_bindgen;
55
use crate::build_bindgen::bindgen;
66

7+
// Functions to determine feature flags
8+
fn enable_playlist() -> &'static str {
9+
if cfg!(feature = "playlist") {
10+
"ON"
11+
} else {
12+
"OFF"
13+
}
14+
}
15+
16+
// Are we linking to shared or static libraries?
17+
fn build_shared_libs_flag() -> &'static str {
18+
if cfg!(feature = "static") {
19+
"OFF" // Disable shared libs to enable static linking
20+
} else {
21+
"ON" // Enable shared libs
22+
}
23+
}
24+
725
fn main() {
8-
// Get the path to the projectM source code
26+
// Path to the projectM source code
927
let projectm_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("libprojectM");
1028

11-
// Check if the libprojectM source code exists
29+
// Verify the existence of the libprojectM directory
1230
if !projectm_path.exists() {
1331
println!("cargo:warning=The libprojectM source code is missing.");
1432
println!(
@@ -18,16 +36,13 @@ fn main() {
1836
std::process::exit(1);
1937
}
2038

21-
// Determine if the 'playlist' feature is enabled
22-
let enable_playlist = if cfg!(feature = "playlist") {
23-
"ON"
24-
} else {
25-
"OFF"
26-
};
39+
// Determine feature flags
40+
let enable_playlist_flag = enable_playlist();
41+
let build_shared_libs = build_shared_libs_flag();
2742

2843
let dst;
2944

30-
// Platform-specific configurations
45+
// Platform-specific CMake configurations
3146
if cfg!(target_os = "windows") {
3247
// Ensure VCPKG installation root is set
3348
let vcpkg_root = match env::var("VCPKG_INSTALLATION_ROOT") {
@@ -55,80 +70,118 @@ fn main() {
5570
// Set VCPKG_ROOT for CMake
5671
env::set_var("VCPKG_ROOT", &vcpkg_root);
5772

58-
// Optionally set CMAKE_PREFIX_PATH
73+
// Define the installation path for vcpkg
5974
let vcpkg_installed = vcpkg_root.join("installed").join("x64-windows-static-md");
75+
let vcpkg_installed_str = vcpkg_installed.to_str().unwrap();
76+
77+
// Define projectM_Eval_DIR and store in a variable
78+
let projectm_eval_dir = projectm_path.join("vendor").join("projectm-eval");
79+
let projectm_eval_dir_str = projectm_eval_dir.to_str().unwrap();
80+
81+
// Convert vcpkg_toolchain to string
82+
let vcpkg_toolchain_str = vcpkg_toolchain.to_str().unwrap();
6083

6184
// Configure and build libprojectM using CMake for Windows
62-
dst = cmake::Config::new(&projectm_path)
85+
let mut cmake_config = cmake::Config::new(&projectm_path);
86+
cmake_config
6387
.generator("Visual Studio 17 2022")
64-
.define("CMAKE_TOOLCHAIN_FILE", &vcpkg_toolchain)
88+
.define("CMAKE_TOOLCHAIN_FILE", vcpkg_toolchain_str)
6589
.define("VCPKG_TARGET_TRIPLET", "x64-windows-static-md")
6690
.define(
6791
"CMAKE_MSVC_RUNTIME_LIBRARY",
6892
"MultiThreaded$<$<CONFIG:Debug>:Debug>DLL",
6993
)
70-
.define("ENABLE_PLAYLIST", enable_playlist)
71-
.define(
72-
"projectM_Eval_DIR",
73-
&projectm_path.join("vendor").join("projectm-eval"),
74-
)
75-
.define("CMAKE_PREFIX_PATH", &vcpkg_installed)
94+
.define("ENABLE_PLAYLIST", enable_playlist_flag)
95+
.define("projectM_Eval_DIR", projectm_eval_dir_str)
96+
.define("CMAKE_PREFIX_PATH", vcpkg_installed_str)
7697
.define("CMAKE_VERBOSE_MAKEFILE", "ON")
7798
.define("BUILD_TESTING", "OFF")
7899
.define("BUILD_EXAMPLES", "OFF")
79-
.build();
100+
.define("BUILD_SHARED_LIBS", build_shared_libs); // static/dynamic
101+
102+
dst = cmake_config.build();
80103
} else if cfg!(target_os = "emscripten") {
81104
// Configure and build libprojectM using CMake for Emscripten
82105
dst = cmake::Config::new(&projectm_path)
83-
.define("ENABLE_PLAYLIST", enable_playlist)
106+
.define("ENABLE_PLAYLIST", enable_playlist_flag)
84107
.define("BUILD_TESTING", "OFF")
85108
.define("BUILD_EXAMPLES", "OFF")
86109
.define("ENABLE_EMSCRIPTEN", "ON")
110+
.define("BUILD_SHARED_LIBS", build_shared_libs) // static/dynamic
87111
.build();
88112
} else {
89-
// Configure and build libprojectM using CMake for other platforms
113+
// Configure and build libprojectM using CMake for other platforms (Linux, macOS)
90114
dst = cmake::Config::new(&projectm_path)
91-
.define("ENABLE_PLAYLIST", enable_playlist)
115+
.define("ENABLE_PLAYLIST", enable_playlist_flag)
92116
.define("BUILD_TESTING", "OFF")
93117
.define("BUILD_EXAMPLES", "OFF")
118+
.define("BUILD_SHARED_LIBS", build_shared_libs) // static/dynamic
94119
.build();
95120
}
96121

97122
// Specify the library search path
98123
println!("cargo:rustc-link-search=native={}/lib", dst.display());
99124

100-
// Determine the library name based on the build profile
125+
// Determine the build profile (release or debug)
101126
let profile = env::var("PROFILE").unwrap_or_else(|_| "release".to_string());
102127

103-
// Platform-specific library linking
128+
// Platform and feature-specific library linking
104129
if cfg!(target_os = "windows") || cfg!(target_os = "emscripten") {
105-
// Use static linking for Windows and Emscripten
106-
if profile == "release" {
107-
println!("cargo:rustc-link-lib=static=projectM-4");
108-
if cfg!(feature = "playlist") {
109-
println!("cargo:rustc-link-lib=static=projectM-4-playlist");
130+
// Static or Dynamic linking based on 'static' feature
131+
if cfg!(feature = "static") {
132+
if profile == "release" {
133+
println!("cargo:rustc-link-lib=static=projectM-4");
134+
if cfg!(feature = "playlist") {
135+
println!("cargo:rustc-link-lib=static=projectM-4-playlist");
136+
}
137+
} else {
138+
println!("cargo:rustc-link-lib=static=projectM-4d");
139+
if cfg!(feature = "playlist") {
140+
println!("cargo:rustc-link-lib=static=projectM-4-playlistd");
141+
}
110142
}
111143
} else {
112-
println!("cargo:rustc-link-lib=static=projectM-4d");
113-
if cfg!(feature = "playlist") {
114-
println!("cargo:rustc-link-lib=static=projectM-4-playlistd");
144+
if profile == "release" {
145+
println!("cargo:rustc-link-lib=dylib=projectM-4");
146+
if cfg!(feature = "playlist") {
147+
println!("cargo:rustc-link-lib=dylib=projectM-4-playlist");
148+
}
149+
} else {
150+
println!("cargo:rustc-link-lib=dylib=projectM-4d");
151+
if cfg!(feature = "playlist") {
152+
println!("cargo:rustc-link-lib=dylib=projectM-4-playlistd");
153+
}
115154
}
116155
}
117156
} else {
118-
// Use dynamic linking for other platforms (Linux, macOS)
119-
if profile == "release" {
120-
println!("cargo:rustc-link-lib=dylib=projectM-4");
121-
if cfg!(feature = "playlist") {
122-
println!("cargo:rustc-link-lib=dylib=projectM-4-playlist");
157+
// For other platforms (Linux, macOS)
158+
if cfg!(feature = "static") {
159+
if profile == "release" {
160+
println!("cargo:rustc-link-lib=static=projectM-4");
161+
if cfg!(feature = "playlist") {
162+
println!("cargo:rustc-link-lib=static=projectM-4-playlist");
163+
}
164+
} else {
165+
println!("cargo:rustc-link-lib=static=projectM-4d");
166+
if cfg!(feature = "playlist") {
167+
println!("cargo:rustc-link-lib=static=projectM-4-playlistd");
168+
}
123169
}
124170
} else {
125-
println!("cargo:rustc-link-lib=dylib=projectM-4d");
126-
if cfg!(feature = "playlist") {
127-
println!("cargo:rustc-link-lib=dylib=projectM-4-playlistd");
171+
if profile == "release" {
172+
println!("cargo:rustc-link-lib=dylib=projectM-4");
173+
if cfg!(feature = "playlist") {
174+
println!("cargo:rustc-link-lib=dylib=projectM-4-playlist");
175+
}
176+
} else {
177+
println!("cargo:rustc-link-lib=dylib=projectM-4d");
178+
if cfg!(feature = "playlist") {
179+
println!("cargo:rustc-link-lib=dylib=projectM-4-playlistd");
180+
}
128181
}
129182
}
130183
}
131184

132-
// Run bindgen to generate Rust bindings
185+
// Generate Rust bindings using bindgen
133186
bindgen();
134-
}
187+
}

0 commit comments

Comments
 (0)