sdl3-image-sys 0.2.0+SDL3-image-3.2.4

Low level Rust bindings for SDL3_image
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// to edit, edit `build-common.rs` in the repo root and run generate-and-check.sh

use std::{env, error::Error};

#[cfg(feature = "build-from-source")]
type Config = cmake::Config;
#[cfg(not(feature = "build-from-source"))]
type Config = ();

#[allow(unused)]
macro_rules! cmake_vars {
    ($config:ident => $($cvar:ident),* $(,)?) => {
        $(
            let cvar = stringify!($cvar);
            if env::var_os(format!("CARGO_FEATURE_NO_{cvar}")).is_some() {
                $config.define(cvar, "OFF");
            }
            if env::var_os(format!("CARGO_FEATURE_{cvar}")).is_some() {
                $config.define(cvar, "ON");
            }
        )*
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum LinkKind {
    Default,
    Static,
}

#[derive(Debug)]
enum LinkFlag {
    SearchLib(String),
    SearchFramework(String),
    Lib(String),
    StaticLib(String),
    Framework(String),
    #[allow(dead_code)]
    WeakFramework(String),
}

#[derive(Default)]
struct LinkFlags(Vec<LinkFlag>);

impl LinkFlags {
    fn search_lib(&mut self, s: impl ToString) {
        self.0.push(LinkFlag::SearchLib(s.to_string()));
    }

    fn search_framework(&mut self, s: impl ToString) {
        self.0.push(LinkFlag::SearchFramework(s.to_string()));
    }

    fn link_lib(&mut self, s: impl ToString) {
        self.0.push(LinkFlag::Lib(s.to_string()));
    }

    fn link_static_lib(&mut self, s: impl ToString) {
        self.0.push(LinkFlag::StaticLib(s.to_string()));
    }

    fn link_framework(&mut self, s: impl ToString) {
        self.0.push(LinkFlag::Framework(s.to_string()));
    }

    #[allow(dead_code)]
    fn link_weak_framework(&mut self, s: impl ToString) {
        self.0.push(LinkFlag::WeakFramework(s.to_string()));
    }

    fn send_to_cargo_link_flags(self) {
        for flag in self.0.iter() {
            match flag {
                LinkFlag::SearchLib(path) => {
                    println!("cargo::rustc-link-search=native={path}");
                }
                LinkFlag::SearchFramework(path) => {
                    println!("cargo::rustc-link-search=framework={path}");
                }
                LinkFlag::Lib(lib) => {
                    println!("cargo::rustc-link-lib={lib}");
                }
                LinkFlag::StaticLib(lib) => {
                    println!("cargo::rustc-link-lib=static={lib}");
                }
                LinkFlag::Framework(lib) | LinkFlag::WeakFramework(lib) => {
                    // FIXME: rust doesn't support weak linking to frameworks for normal crates
                    println!("cargo::rustc-link-lib=framework={lib}");
                }
            }
        }
    }

    fn send_to_cargo_metadata(self) {
        let mut search_libs = String::new();
        let mut search_frameworks = String::new();
        let mut libs = String::new();
        let mut static_libs = String::new();
        let mut frameworks = String::new();
        let mut weak_frameworks = String::new();
        let mut clang = String::new();
        let sep = ';';
        for flag in self.0.iter() {
            match flag {
                LinkFlag::SearchLib(path) => {
                    assert!(
                        !path.contains(sep),
                        "library search path contains `{sep}`: {path}"
                    );
                    if !search_libs.is_empty() {
                        search_libs.push(sep);
                    }
                    search_libs.push_str(path);
                    if !clang.is_empty() {
                        clang.push(sep);
                    }
                    clang.push_str("-L");
                    clang.push_str(path);
                }
                LinkFlag::SearchFramework(path) => {
                    assert!(
                        !path.contains(sep),
                        "framework search path contains `{sep}`: {path}"
                    );
                    if !search_frameworks.is_empty() {
                        search_frameworks.push(sep);
                    }
                    search_frameworks.push_str(path);
                    if !clang.is_empty() {
                        clang.push(sep);
                    }
                    clang.push_str("-F");
                    clang.push_str(path);
                }
                LinkFlag::Lib(lib) => {
                    assert!(!lib.contains(sep), "library name contains `{sep}`: {lib}");
                    if !libs.is_empty() {
                        libs.push(sep);
                    }
                    libs.push_str(lib);
                    if !clang.is_empty() {
                        clang.push(sep);
                    }
                    clang.push_str("-l");
                    clang.push_str(lib);
                }
                LinkFlag::StaticLib(lib) => {
                    assert!(
                        !lib.contains(sep),
                        "static library name contains `{sep}`: {lib}"
                    );
                    if !static_libs.is_empty() {
                        static_libs.push(sep);
                    }
                    static_libs.push_str(lib);
                    if !clang.is_empty() {
                        clang.push(sep);
                    }
                    clang.push_str("-Wl,-Bstatic");
                    clang.push(sep);
                    clang.push_str("-l");
                    clang.push(sep);
                    clang.push_str(lib);
                    clang.push(sep);
                    clang.push_str("-Wl,-Bdynamic");
                }
                LinkFlag::Framework(lib) => {
                    assert!(!lib.contains(sep), "framework name contains `{sep}`: {lib}");
                    if !frameworks.is_empty() {
                        frameworks.push(sep);
                    }
                    frameworks.push_str(lib);
                    if !clang.is_empty() {
                        clang.push(sep);
                    }
                    clang.push_str("-framework");
                    clang.push(sep);
                    clang.push_str(lib);
                }
                LinkFlag::WeakFramework(lib) => {
                    assert!(
                        !lib.contains(sep),
                        "weak framework name contains `{sep}`: {lib}"
                    );
                    if !weak_frameworks.is_empty() {
                        weak_frameworks.push(sep);
                    }
                    weak_frameworks.push_str(lib);
                    if !clang.is_empty() {
                        clang.push(sep);
                    }
                    clang.push_str("-weak_framework");
                    clang.push(sep);
                    clang.push_str(lib);
                }
            }
        }
        println!("cargo::metadata=LINK_FLAGS_SEARCH_LIBS={search_libs}");
        println!("cargo::metadata=LINK_FLAGS_SEARCH_FRAMEWORKS={search_frameworks}");
        println!("cargo::metadata=LINK_FLAGS_LIBS={libs}");
        println!("cargo::metadata=LINK_FLAGS_STATIC_LIBS={static_libs}");
        println!("cargo::metadata=LINK_FLAGS_FRAMEWORKS={frameworks}");
        println!("cargo::metadata=LINK_FLAGS_WEAK_FRAMEWORKS={weak_frameworks}");
        println!("cargo::metadata=LINK_FLAGS_CLANG={clang}");
    }
}

#[cfg(all(windows, feature = "build-from-source", not(feature = "link-static")))]
// based on find_cargo_target_dir from sdl2-sys
fn top_level_cargo_target_dir() -> std::path::PathBuf {
    use std::path::PathBuf;
    let pkg_name = env::var("CARGO_PKG_NAME").unwrap();
    let out_dir = env::var_os("OUT_DIR").unwrap();
    let mut target = PathBuf::from(&out_dir);
    let pop = |target: &mut PathBuf| assert!(target.pop(), "malformed OUT_DIR: {:?}", out_dir);
    while !target
        .file_name()
        .unwrap()
        .to_string_lossy()
        .contains(&pkg_name)
    {
        pop(&mut target);
    }
    pop(&mut target);
    pop(&mut target);
    target
}

#[cfg(feature = "build-from-source")]
fn find_and_output_cmake_dir_metadata(out_dir: &std::path::Path) -> Result<(), Box<dyn Error>> {
    use std::path::{Path, PathBuf};
    fn try_dir(dir: &Path) -> bool {
        if dir.join(format!("{LIB_NAME}Config.cmake")).exists() {
            println!("cargo::metadata=CMAKE_DIR={}", dir.display());
            true
        } else {
            false
        }
    }
    if try_dir(&out_dir.join(PathBuf::from_iter(["lib", "cmake", LIB_NAME])))
        || try_dir(&out_dir.join(PathBuf::from_iter(["lib64", "cmake", LIB_NAME])))
        || try_dir(&out_dir.join("cmake"))
    {
        Ok(())
    } else {
        Err(format!("cmake dir not found in {}", out_dir.display()).into())
    }
}

fn build(f: impl FnOnce(&mut Config) -> Result<(), Box<dyn Error>>) -> Result<(), Box<dyn Error>> {
    let _ = &f;
    let _ = PACKAGE_NAME;
    let _ = LIB_NAME;
    let _ = LIB_MIN_VERSION;

    if env::var("DOCS_RS").is_ok() {
        // don't build/link on docs.rs
    } else {
        let do_link = !cfg!(feature = "no-link");
        let mut link_flags = LinkFlags::default();

        let link_kind = if cfg!(feature = "link-static") {
            LinkKind::Static
        } else {
            LinkKind::Default
        };

        #[cfg(feature = "build-from-source")]
        {
            use rpkg_config::{Link, PkgConfig};
            use std::path::Path;

            let mut config = Config::new(SOURCE_DIR);
            f(&mut config)?;
            let out_dir = config.build();
            println!("cargo::metadata=OUT_DIR={}", out_dir.display());

            if let Ok(cfg) = PkgConfig::open(
                &out_dir.join(format!("lib/pkgconfig/{PACKAGE_NAME}.pc")),
            )
            .or_else(|_| {
                PkgConfig::open(&out_dir.join(format!("lib64/pkgconfig/{PACKAGE_NAME}.pc")))
            }) {
                for link in cfg.libs_with_private(cfg!(feature = "link-static"))? {
                    match link {
                        Link::SearchLib(path) => {
                            link_flags.search_lib(path.display());
                        }

                        Link::SearchFramework(path) => {
                            link_flags.search_framework(path.display());
                        }

                        Link::Lib(path) => {
                            if path == Path::new(LIB_NAME) {
                                match link_kind {
                                    LinkKind::Static => {
                                        if env::var("CARGO_CFG_TARGET_ENV").unwrap() == "msvc" {
                                            link_flags
                                                .link_static_lib(format!("{LIB_NAME}-static"));
                                        } else {
                                            link_flags.link_static_lib(path.display());
                                        }
                                    }
                                    LinkKind::Default => {
                                        link_flags.link_lib(path.display());
                                    }
                                }
                            } else {
                                link_flags.link_lib(path.display());
                            }
                        }

                        Link::Framework(path) => {
                            link_flags.link_framework(path.display());
                        }

                        Link::WeakFramework(path) => {
                            link_flags.link_weak_framework(path.display());
                        }

                        _ => (),
                    };
                }
            } else if LINK_FRAMEWORK {
                link_flags.search_framework(out_dir.display());
                link_flags.link_framework(LIB_NAME);
            } else {
                link_flags.search_lib(out_dir.display());
                link_flags.search_lib(format!("{}/lib", out_dir.display()));
                link_flags.search_lib(format!("{}/lib64", out_dir.display()));
                match link_kind {
                    LinkKind::Static => link_flags.link_static_lib(LIB_NAME),
                    LinkKind::Default => link_flags.link_lib(LIB_NAME),
                }
            }

            find_and_output_cmake_dir_metadata(&out_dir)?;

            #[cfg(all(windows, not(feature = "link-static")))]
            {
                // Windows can't find the built dll when run, so copy it to the target dir
                std::fs::copy(
                    out_dir.join("bin").join(format!("{LIB_NAME}.dll")),
                    top_level_cargo_target_dir().join(format!("{LIB_NAME}.dll")),
                )?;
            }
        }
        #[cfg(not(feature = "build-from-source"))]
        {
            if LINK_FRAMEWORK {
                // FIXME: rust doesn't support linking to xcframeworks
                let home = env::var("HOME");
                let link_search = |name| {
                    if let Ok(home) = home {
                        link_flags.search_framework(format!(
                            "{home}/Library/Frameworks/{LIB_NAME}.xcframework/{name}"
                        ));
                    }
                    link_flags.search_framework(format!(
                        "/Library/Frameworks/{LIB_NAME}.xcframework/{name}"
                    ));
                };
                if env::var("CARGO_CFG_TARGET_OS").unwrap() == "macos" {
                    link_search("macos-arm64_x86_64");
                } else if env::var("CARGO_CFG_TARGET_OS").unwrap() == "ios" {
                    if env::var("CARGO_CFG_TARGET_ABI").unwrap() == "sim" {
                        link_search("ios-arm64_x86_64-simulator");
                    } else {
                        link_search("ios-arm64");
                    }
                } else if env::var("CARGO_CFG_TARGET_OS").unwrap() == "tvos" {
                    if env::var("CARGO_CFG_TARGET_ABI").unwrap() == "sim" {
                        link_search("tvos-arm64_x86_64-simulator");
                    } else {
                        link_search("tvos-arm64");
                    }
                }
                link_flags.link_framework(LIB_NAME);
            } else {
                #[allow(unused_mut)]
                let mut handled = false;

                #[cfg(feature = "use-pkg-config")]
                if !handled {
                    if let Ok(lib) = pkg_config::Config::new()
                        .statik(cfg!(feature = "link-static"))
                        .atleast_version(LIB_MIN_VERSION)
                        .probe(PACKAGE_NAME)
                    {
                        handled = true;
                        for path in lib.link_paths.iter() {
                            link_flags.search_lib(path.display());
                        }
                        for path in lib.framework_paths.iter() {
                            link_flags.search_framework(path.display());
                        }
                        for s in lib.libs.iter() {
                            if s == LIB_NAME {
                                match link_kind {
                                    LinkKind::Static => link_flags.link_static_lib(s),
                                    LinkKind::Default => link_flags.link_lib(s),
                                }
                            } else {
                                link_flags.link_lib(s);
                            }
                        }
                        for s in lib.frameworks.iter() {
                            link_flags.link_framework(s);
                        }
                    }
                }

                #[cfg(feature = "use-vcpkg")]
                if !handled {
                    handled = vcpkg::find_package(PACKAGE_NAME).is_ok();
                }

                if !handled {
                    // yolo
                    if cfg!(target_os = "macos") {
                        link_flags.search_lib("/opt/homebrew/lib");
                    }
                    match link_kind {
                        LinkKind::Static => link_flags.link_static_lib(LIB_NAME),
                        LinkKind::Default => link_flags.link_lib(LIB_NAME),
                    }
                }
            }
        }

        if do_link {
            link_flags.send_to_cargo_link_flags();
        } else {
            link_flags.send_to_cargo_metadata();
        }
    }

    Ok(())
}