Skip to content

Commit 1aec020

Browse files
Merge pull request #341 from kesyog/build-flac-feature
Allow dynamic linkage of FLAC and ogg
2 parents 177f0cf + 66d0aaa commit 1aec020

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
lines changed

.github/workflows/linux.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
- name: Install deps
3737
run: |
3838
sudo apt-get update
39-
sudo apt-get install libpthread-stubs0-dev libgl1-mesa-dev libx11-dev libx11-xcb-dev libxcb-image0-dev libxrandr-dev libxcb-randr0-dev libudev-dev libfreetype6-dev libglew-dev libjpeg8-dev libgpgme11-dev libsndfile1-dev libopenal-dev libjpeg62 libxcursor-dev cmake libclang-dev clang
39+
sudo apt-get install libpthread-stubs0-dev libgl1-mesa-dev libx11-dev libx11-xcb-dev libxcb-image0-dev libxrandr-dev libxcb-randr0-dev libudev-dev libfreetype6-dev libglew-dev libjpeg8-dev libgpgme11-dev libsndfile1-dev libopenal-dev libjpeg62 libxcursor-dev cmake libclang-dev clang libflac-dev
4040
- name: Build
4141
run: |
4242
git submodule update --init
@@ -49,6 +49,7 @@ jobs:
4949
# Also test non-default feature combinations
5050
cargo test --no-default-features --features=ci-headless --verbose
5151
cargo test --no-default-features --features=ci-headless,audio --verbose
52+
cargo test --no-default-features --features=ci-headless,audio,build-flac-ogg --verbose
5253
cargo test --no-default-features --features=ci-headless,window --verbose
5354
cargo test --no-default-features --features=ci-headless,graphics --verbose
5455
# Test packaging (building from .crate archive, without SFML submodule)

.github/workflows/macos.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
- uses: actions/checkout@v4
3636
- name: Install deps
3737
run: |
38-
brew install freetype libvorbis
38+
brew install freetype libvorbis flac libogg
3939
- name: Build
4040
run: |
4141
git submodule update --init
@@ -47,7 +47,9 @@ jobs:
4747
cargo test --release --features=ci-headless --verbose
4848
# Also test non-default feature combinations
4949
cargo test --no-default-features --features=ci-headless --verbose
50-
cargo test --no-default-features --features=ci-headless,audio --verbose
50+
# Specify search paths for dynamically-linked FLAC and ogg
51+
LIBRARY_PATH=/opt/homebrew/lib cargo test --no-default-features --features=ci-headless,audio --verbose
52+
cargo test --no-default-features --features=ci-headless,audio,build-flac-ogg --verbose
5153
cargo test --no-default-features --features=ci-headless,window --verbose
5254
cargo test --no-default-features --features=ci-headless,graphics --verbose
5355
# Test packaging (building from .crate archive, without SFML submodule)

.github/workflows/windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ jobs:
4646
cargo test --release --features=ci-headless --verbose &&
4747
# Also test non-default feature combinations
4848
cargo test --no-default-features --features=ci-headless --verbose &&
49-
cargo test --no-default-features --features=ci-headless,audio --verbose &&
49+
cargo test --no-default-features --features=ci-headless,audio,build-flac-ogg --verbose &&
5050
cargo test --no-default-features --features=ci-headless,window --verbose &&
5151
cargo test --no-default-features --features=ci-headless,graphics --verbose &&
5252
# Test packaging (building from .crate archive, without SFML submodule) (allow dirty because of openal32.dll)

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples"]
3939
rustdoc-args = ["-Zunstable-options", "--generate-link-to-definition"]
4040

4141
[features]
42-
default = ["graphics", "audio"]
42+
default = ["graphics", "audio", "build-flac-ogg"]
4343
window = ["dep:bitflags"]
4444
graphics = ["window"]
4545
audio = ["dep:libflac-sys"]
4646
serde = ["dep:serde"]
4747
# Used to skip running certain tests on CI, since it's running in a headless environment.
4848
ci-headless = []
49+
# When enabled, libFLAC and libogg will be built from source and statically linked
50+
# Otherwise, they will be dynamically linked
51+
build-flac-ogg = ["libflac-sys?/build-flac", "libflac-sys?/build-ogg"]
4952

5053
[dependencies]
5154
link-cplusplus = "1.0.9"
@@ -65,6 +68,7 @@ optional = true
6568

6669
[dependencies.libflac-sys]
6770
version = "0.3"
71+
default-features = false
6872
optional = true
6973

7074
[build-dependencies]

build.rs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,32 @@ use std::{
33
path::{Path, PathBuf},
44
};
55

6+
#[derive(Copy, Clone)]
7+
enum LinkageKind {
8+
Static,
9+
Dynamic,
10+
}
11+
12+
impl LinkageKind {
13+
fn link(self, library: &str) {
14+
match self {
15+
LinkageKind::Static => {
16+
println!("cargo:rustc-link-lib=static={library}");
17+
}
18+
LinkageKind::Dynamic => {
19+
println!("cargo:rustc-link-lib=dylib={library}");
20+
}
21+
}
22+
}
23+
}
24+
625
fn static_link_windows(
726
feat_window: bool,
827
feat_audio: bool,
928
feat_graphics: bool,
1029
env: WinEnv,
1130
build_lib_path: &Path,
31+
flac_ogg_linkage: LinkageKind,
1232
) {
1333
let arch = match env::var("CARGO_CFG_TARGET_ARCH").as_deref() {
1434
Ok("x86") => "x86",
@@ -42,15 +62,20 @@ fn static_link_windows(
4262
}
4363
if feat_audio {
4464
println!("cargo:rustc-link-lib=static=openal32");
45-
println!("cargo:rustc-link-lib=static=FLAC");
65+
flac_ogg_linkage.link("FLAC");
4666
println!("cargo:rustc-link-lib=static=vorbisenc");
4767
println!("cargo:rustc-link-lib=static=vorbisfile");
4868
println!("cargo:rustc-link-lib=static=vorbis");
49-
println!("cargo:rustc-link-lib=static=ogg");
69+
flac_ogg_linkage.link("ogg");
5070
}
5171
}
5272

53-
fn static_link_linux(feat_window: bool, feat_audio: bool, feat_graphics: bool) {
73+
fn static_link_linux(
74+
feat_window: bool,
75+
feat_audio: bool,
76+
feat_graphics: bool,
77+
flac_ogg_linkage: LinkageKind,
78+
) {
5479
println!("cargo:rustc-link-lib=dylib=udev");
5580
if feat_window {
5681
println!("cargo:rustc-link-lib=dylib=GL");
@@ -64,7 +89,7 @@ fn static_link_linux(feat_window: bool, feat_audio: bool, feat_graphics: bool) {
6489
if feat_audio {
6590
// We link openal separately because Mac Os has its own OpenAL framework
6691
pkgconfig_probe_with_fallback("openal", "rustc-link-lib=dylib=openal");
67-
unix_audio_link_support_libs();
92+
unix_audio_link_support_libs(flac_ogg_linkage);
6893
}
6994
}
7095

@@ -80,13 +105,13 @@ fn unix_graphics_link_support_libs() {
80105
pkgconfig_probe_with_fallback("freetype2", "rustc-link-lib=dylib=freetype");
81106
}
82107

83-
fn unix_audio_link_support_libs() {
108+
fn unix_audio_link_support_libs(flac_ogg_linkage: LinkageKind) {
84109
pkgconfig_probe_with_fallback("vorbisenc", "rustc-link-lib=dylib=vorbisenc");
85110
pkgconfig_probe_with_fallback("vorbisfile", "rustc-link-lib=dylib=vorbisfile");
86111
pkgconfig_probe_with_fallback("vorbis", "rustc-link-lib=dylib=vorbis");
87112
// Odd that we have to do this, I thought that libflac-sys would do this for us
88-
println!("cargo:rustc-link-lib=static=FLAC");
89-
println!("cargo:rustc-link-lib=static=ogg");
113+
flac_ogg_linkage.link("FLAC");
114+
flac_ogg_linkage.link("ogg");
90115
}
91116

92117
enum WinEnv {
@@ -131,6 +156,11 @@ fn main() {
131156
let feat_audio = env::var("CARGO_FEATURE_AUDIO").is_ok();
132157
let feat_window = env::var("CARGO_FEATURE_WINDOW").is_ok();
133158
let feat_graphics = env::var("CARGO_FEATURE_GRAPHICS").is_ok();
159+
let flac_ogg_linkage = if env::var("CARGO_FEATURE_BUILD_FLAC_OGG").is_ok() {
160+
LinkageKind::Static
161+
} else {
162+
LinkageKind::Dynamic
163+
};
134164
let mut cmake = cmake::Config::new("SFML");
135165
let win_env = WinEnv::get();
136166
let release_profile = env::var("PROFILE").is_ok_and(|prof| prof == "release");
@@ -159,21 +189,22 @@ fn main() {
159189
}
160190
_ => ("/lib", "/build/src/libFLAC"),
161191
};
162-
match env::var("DEP_FLAC_ROOT") {
163-
Ok(libflac_root) => {
192+
match (env::var("DEP_FLAC_ROOT"), flac_ogg_linkage) {
193+
(Ok(libflac_root), LinkageKind::Static) => {
164194
cmake.define(
165195
"CMAKE_PREFIX_PATH",
166196
// We add both the path to libogg and libFLAC. Two separate paths, separated by `;`.
167197
[&libflac_root, libogg_loc, ";", &libflac_root, libflac_loc].concat(),
168198
);
169199
}
170-
Err(e) => {
200+
(Err(e), LinkageKind::Static) => {
171201
println!(
172202
"cargo:warning=Failed to get DEP_FLAC_ROOT: {e}.\n\
173203
Now the build will horribly break.\n\
174204
Except maybe on CI. ;)"
175205
);
176206
}
207+
(_, LinkageKind::Dynamic) => (),
177208
}
178209
}
179210
if !feat_window {
@@ -286,11 +317,18 @@ fn main() {
286317
println!("cargo:rustc-link-lib=static=rcsfml");
287318
link_sfml_subsystem("system");
288319
if is_unix && is_linux {
289-
static_link_linux(feat_window, feat_audio, feat_graphics);
320+
static_link_linux(feat_window, feat_audio, feat_graphics, flac_ogg_linkage);
290321
} else if is_windows {
291322
match win_env {
292323
Some(env) => {
293-
static_link_windows(feat_window, feat_audio, feat_graphics, env, &build_lib_path);
324+
static_link_windows(
325+
feat_window,
326+
feat_audio,
327+
feat_graphics,
328+
env,
329+
&build_lib_path,
330+
flac_ogg_linkage,
331+
);
294332
}
295333
None => {
296334
panic!("Failed to determine windows environment (MSVC/Mingw)");
@@ -302,7 +340,7 @@ fn main() {
302340
unix_graphics_link_support_libs();
303341
}
304342
if feat_audio {
305-
unix_audio_link_support_libs();
343+
unix_audio_link_support_libs(flac_ogg_linkage);
306344
}
307345
// SFML contains Objective-C source files on OSX
308346
// https://github.com/SFML/SFML/issues/2920

0 commit comments

Comments
 (0)