Skip to content

Commit 4ff7e9c

Browse files
authored
Merge pull request #968 from BVE-Reborn/include-from-sys-pr
Print Out SDL2 Include Directory in sdl2-sys Build Script
2 parents 144c483 + c02f7b3 commit 4ff7e9c

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,19 @@ If you don't have pkg-config or disabled the feature, it will try to get the hea
371371
If somehow you have your own headers that you want to use (use a beta version, an older version, ...),
372372
you can set the environment variable "SDL2_INCLUDE_PATH" and those headers will be used by bindgen instead.
373373

374+
# Using sdl2-sys to provide SDL2 headers/library
375+
376+
If you are creating a `*-sys` crate for a library which requires SDL2, you can use `sdl2-sys` to provide both the compiled library
377+
and the headers for SDL2.
378+
379+
Follow the following process to get the header directory. In the `Cargo.toml` for your crate, add `sdl2-sys` as a dependency (not a build-dependency).
380+
Cargo will then provide your build script with an environment variable `DEP_SDL2_INCLUDE` which is populated with the include directory for SDL2.
381+
If there is more than one directory, they are combined with `:` as a separator. Pass these directories to whatever is building your C/C++.
382+
383+
Once everything is linked together, there will be a single copy of SDL2 (the one provided by `sdl2-sys`) for all C, C++, and Rust code.
384+
385+
For more discussion see the corresponding [issue][dep-sdl2-include-issue]
386+
374387
# OpenGL
375388

376389
If you want to use OpenGL, you also need the
@@ -589,5 +602,6 @@ Any Pull Request is welcome, however small your contribution may be ! There are,
589602
[homebrew]: http://brew.sh/
590603
[crates]: http://crates.io/
591604
[examples]: https://github.com/jdeseno/rs-sdl2-examples
605+
[dep-sdl2-include-issue]: https://github.com/Rust-SDL2/rust-sdl2/pull/968
592606
[gl-rs]: https://github.com/bjz/gl-rs
593607
[pdev-issue]: https://github.com/PistonDevelopers/rust-empty/issues/175

changelog.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
In this file will be listed the changes, especially the breaking ones that one should be careful of
22
when upgrading from a version of rust-sdl2 to another.
33

4+
### unreleased
5+
6+
[PR #968](https://github.com/Rust-SDL2/rust-sdl2/pull/968)
7+
Pass SDL2 include directories to `sdl2-sys`'s dependant crates through `DEP_SDL2_INCLUDE`.
8+
49
### v0.33
510

611
[PR #956](https://github.com/Rust-SDL2/rust-sdl2/pull/956) + [PR #960](https://github.com/Rust-SDL2/rust-sdl2/pull/960) + [PR #951](https://github.com/Rust-SDL2/rust-sdl2/pull/951):

sdl2-sys/build.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ macro_rules! add_msvc_includes_to_bindings {
3636
};
3737
}
3838

39+
fn get_bundled_header_path() -> PathBuf {
40+
let mut include_path: PathBuf = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
41+
include_path.push(format!("SDL2-{}", SDL2_HEADERS_BUNDLED_VERSION));
42+
include_path.push("include");
43+
include_path
44+
}
45+
3946
#[cfg(feature = "bundled")]
4047
fn run_command(cmd: &str, args: &[&str]) {
4148
use std::process::Command;
@@ -468,8 +475,12 @@ fn main() {
468475

469476
#[cfg(feature = "bindgen")] {
470477
let include_paths = vec!(String::from(sdl2_downloaded_include_path.to_str().unwrap()));
478+
println!("cargo:include={}", include_paths.join(":"));
471479
generate_bindings(target.as_str(), host.as_str(), include_paths.as_slice())
472480
}
481+
#[cfg(not(feature = "bindgen"))] {
482+
println!("cargo:include={}", sdl2_downloaded_include_path.display());
483+
}
473484
};
474485

475486
#[cfg(all(not(feature = "bundled"), feature = "bindgen"))] {
@@ -479,6 +490,7 @@ fn main() {
479490

480491
#[cfg(not(feature = "bindgen"))] {
481492
copy_pregenerated_bindings();
493+
println!("cargo:include={}", get_bundled_header_path().display());
482494
}
483495

484496
link_sdl2(target_os);
@@ -526,7 +538,7 @@ fn copy_pregenerated_bindings() {
526538
#[cfg(feature = "bindgen")]
527539
// headers_path is a list of directories where the SDL2 headers are expected
528540
// to be found by bindgen (should point to the include/ directories)
529-
fn generate_bindings<S: AsRef<str> + ::std::fmt::Debug>(target: &str, host: &str, headers_paths: &[S]) {
541+
fn generate_bindings(target: &str, host: &str, headers_paths: &[String]) {
530542
let target_os = get_os_from_triple(target).unwrap();
531543
let mut bindings = bindgen::Builder::default()
532544
// enable no_std-friendly output by only using core definitions
@@ -600,9 +612,9 @@ fn generate_bindings<S: AsRef<str> + ::std::fmt::Debug>(target: &str, host: &str
600612

601613
if headers_paths.len() == 0 {
602614
// if no paths are being provided, fall back to the headers included in this repo
603-
let mut include_path: PathBuf = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
604-
include_path.push(format!("SDL2-{}", SDL2_HEADERS_BUNDLED_VERSION));
605-
include_path.push("include");
615+
let include_path = get_bundled_header_path();
616+
println!("cargo:include={}", include_path.display());
617+
606618
bindings = bindings.clang_arg(format!("-I{}", include_path.display()));
607619
if cfg!(feature = "image") {
608620
image_bindings = image_bindings.clang_arg(format!("-I{}", include_path.display()));
@@ -621,22 +633,23 @@ fn generate_bindings<S: AsRef<str> + ::std::fmt::Debug>(target: &str, host: &str
621633
}
622634
} else {
623635
// if paths are included, use them for bindgen. Bindgen should use the first one.
636+
println!("cargo:include={}", headers_paths.join(":"));
624637
for headers_path in headers_paths {
625-
bindings = bindings.clang_arg(format!("-I{}", headers_path.as_ref()));
638+
bindings = bindings.clang_arg(format!("-I{}", headers_path));
626639
if cfg!(feature = "image") {
627-
image_bindings = image_bindings.clang_arg(format!("-I{}", headers_path.as_ref()));
640+
image_bindings = image_bindings.clang_arg(format!("-I{}", headers_path));
628641
}
629642
if cfg!(feature = "ttf") {
630-
ttf_bindings = ttf_bindings.clang_arg(format!("-I{}", headers_path.as_ref()));
643+
ttf_bindings = ttf_bindings.clang_arg(format!("-I{}", headers_path));
631644
}
632645
if cfg!(feature = "mixer") {
633-
mixer_bindings = mixer_bindings.clang_arg(format!("-I{}", headers_path.as_ref()));
646+
mixer_bindings = mixer_bindings.clang_arg(format!("-I{}", headers_path));
634647
}
635648
if cfg!(feature = "gfx") {
636-
gfx_framerate_bindings = gfx_framerate_bindings.clang_arg(format!("-I{}", headers_path.as_ref()));
637-
gfx_primitives_bindings = gfx_primitives_bindings.clang_arg(format!("-I{}", headers_path.as_ref()));
638-
gfx_imagefilter_bindings = gfx_imagefilter_bindings.clang_arg(format!("-I{}", headers_path.as_ref()));
639-
gfx_rotozoom_bindings = gfx_rotozoom_bindings.clang_arg(format!("-I{}", headers_path.as_ref()));
649+
gfx_framerate_bindings = gfx_framerate_bindings.clang_arg(format!("-I{}", headers_path));
650+
gfx_primitives_bindings = gfx_primitives_bindings.clang_arg(format!("-I{}", headers_path));
651+
gfx_imagefilter_bindings = gfx_imagefilter_bindings.clang_arg(format!("-I{}", headers_path));
652+
gfx_rotozoom_bindings = gfx_rotozoom_bindings.clang_arg(format!("-I{}", headers_path));
640653
}
641654
}
642655
}

0 commit comments

Comments
 (0)