Skip to content

Commit ef36bb8

Browse files
authored
Merge pull request #797 from reynisdrangar/patch_bundled_sdl2
Extend Travis build matrix, patch SDL2 2.0.8
2 parents 2b533d9 + 765d564 commit ef36bb8

File tree

6 files changed

+220
-40
lines changed

6 files changed

+220
-40
lines changed

.travis.yml

+28-38
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,37 @@
11
language: rust
22
sudo: required
33
rust:
4-
- beta
5-
- nightly
6-
- stable
4+
- beta
5+
- nightly
6+
- stable
77
os:
8-
- linux
9-
- osx
8+
- linux
9+
- osx
10+
env:
11+
matrix:
12+
- CI_BUILD_FEATURES="bundled"
13+
- CI_BUILD_FEATURES="gfx image ttf mixer"
14+
global:
15+
- RUST_TEST_THREADS=1
16+
- TRAVIS_CARGO_NIGHTLY_FEATURE=""
17+
- LD_LIBRARY_PATH: "/usr/local/lib"
18+
- secure: MJhmVnQ2IM7+sVmc3vU4ndKOcQgLLeHUPW3qaQBQHKQmvoswCwQK60N17uSgWn1Ln8teqvSRHq4KclIjdMHI+VuQXJHQKHDgjcYbHxwmc3AM1Whnp0XB44ksKUmD109BGWSfZQxzF+6dA+YNOQ+mti+bpydMu8n2FMVjA/SXwQ8=
19+
1020
install:
11-
- wget https://www.libsdl.org/release/SDL2-2.0.8.tar.gz -O sdl2.tar.gz
12-
- tar xzf sdl2.tar.gz
13-
- pushd SDL2-* && ./configure && make && sudo make install && popd
14-
- wget -q https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.14.tar.gz
15-
- wget -q https://www.libsdl.org/projects/SDL_image/release/SDL2_image-2.0.1.tar.gz
16-
- wget -q https://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.2.tar.gz
17-
- wget -q -O SDL2_gfx-1.0.1.tar.gz https://sourceforge.net/projects/sdl2gfx/files/SDL2_gfx-1.0.1.tar.gz/download
18-
- tar xzf SDL2_ttf-*.tar.gz
19-
- tar xzf SDL2_image-*.tar.gz
20-
- tar xzf SDL2_mixer-*.tar.gz
21-
- tar xzf SDL2_gfx-*.tar.gz
22-
- pushd SDL2_ttf-* && ./configure && make && sudo make install && popd
23-
- pushd SDL2_image-* && ./configure && make && sudo make install && popd
24-
- pushd SDL2_mixer-* && ./configure && make && sudo make install && popd
25-
- pushd SDL2_gfx-* && ./autogen.sh && ./configure && make && sudo make install && popd
21+
- if [[ $CI_BUILD_FEATURES != *"bundled"* ]]; then bash scripts/travis-install-sdl2.sh; fi
2622

2723
before_script:
28-
- shopt -s expand_aliases
29-
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then alias pip=pip2; fi
30-
- |
31-
pip install 'travis-cargo<0.2' --user &&
32-
export PATH=$HOME/.local/bin:$PATH &&
33-
export PATH=~/Library/Python/2.7/bin:$PATH
24+
- shopt -s expand_aliases
25+
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then alias pip=pip2; fi
26+
- |
27+
pip install 'travis-cargo<0.2' --user &&
28+
export PATH=$HOME/.local/bin:$PATH &&
29+
export PATH=~/Library/Python/2.7/bin:$PATH
3430
script:
35-
- |
36-
travis-cargo build -- --features "gfx image ttf mixer" &&
37-
travis-cargo build -- --examples --features "gfx image ttf mixer" &&
38-
travis-cargo test -- --features "gfx image ttf mixer" &&
39-
travis-cargo --only stable doc -- --features "gfx image ttf mixer"
31+
- |
32+
travis-cargo build -- --features "${CI_BUILD_FEATURES}" &&
33+
travis-cargo build -- --examples --features "${CI_BUILD_FEATURES}" &&
34+
travis-cargo test -- --features "${CI_BUILD_FEATURES}" &&
35+
travis-cargo --only stable doc -- --features "${CI_BUILD_FEATURES}"
4036
after_success:
41-
- travis-cargo --only stable doc-upload
42-
env:
43-
global:
44-
- RUST_TEST_THREADS=1
45-
- TRAVIS_CARGO_NIGHTLY_FEATURE=""
46-
- LD_LIBRARY_PATH: "/usr/local/lib"
47-
- secure: MJhmVnQ2IM7+sVmc3vU4ndKOcQgLLeHUPW3qaQBQHKQmvoswCwQK60N17uSgWn1Ln8teqvSRHq4KclIjdMHI+VuQXJHQKHDgjcYbHxwmc3AM1Whnp0XB44ksKUmD109BGWSfZQxzF+6dA+YNOQ+mti+bpydMu8n2FMVjA/SXwQ8=
37+
- travis-cargo --only stable doc-upload

scripts/travis-install-sdl2.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
set -xueo pipefail
4+
5+
wget https://www.libsdl.org/release/SDL2-2.0.8.tar.gz -O sdl2.tar.gz
6+
tar xzf sdl2.tar.gz
7+
pushd SDL2-* && ./configure && make && sudo make install && popd
8+
wget -q https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.14.tar.gz
9+
wget -q https://www.libsdl.org/projects/SDL_image/release/SDL2_image-2.0.1.tar.gz
10+
wget -q https://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.2.tar.gz
11+
wget -q -O SDL2_gfx-1.0.1.tar.gz https://sourceforge.net/projects/sdl2gfx/files/SDL2_gfx-1.0.1.tar.gz/download
12+
tar xzf SDL2_ttf-*.tar.gz
13+
tar xzf SDL2_image-*.tar.gz
14+
tar xzf SDL2_mixer-*.tar.gz
15+
tar xzf SDL2_gfx-*.tar.gz
16+
pushd SDL2_ttf-* && ./configure && make && sudo make install && popd
17+
pushd SDL2_image-* && ./configure && make && sudo make install && popd
18+
pushd SDL2_mixer-* && ./configure && make && sudo make install && popd
19+
pushd SDL2_gfx-* && ./autogen.sh && ./configure && make && sudo make install && popd

sdl2-sys/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ optional = true
3939
version = "0.2"
4040
optional = true
4141

42+
[build-dependencies.unidiff]
43+
version = "0.2.1"
44+
optional = true
45+
4246
[build-dependencies]
4347
cfg-if = "0.1"
4448

@@ -49,7 +53,7 @@ use-pkgconfig = ["pkg-config"]
4953
use-bindgen = ["bindgen"]
5054
static-link = []
5155
use_mac_framework = []
52-
bundled = ["cmake", "reqwest", "tar", "flate2"]
56+
bundled = ["cmake", "reqwest", "tar", "flate2", "unidiff"]
5357
mixer = []
5458
image = []
5559
ttf = []

sdl2-sys/build.rs

+123
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ extern crate tar;
1212
extern crate flate2;
1313
#[cfg(feature="bundled")]
1414
extern crate reqwest;
15+
#[cfg(feature="bundled")]
16+
extern crate unidiff;
1517

1618
#[macro_use]
1719
extern crate cfg_if;
@@ -112,6 +114,126 @@ fn download_sdl2() -> PathBuf {
112114
sdl2_build_path
113115
}
114116

117+
// apply patches to sdl2 source
118+
#[cfg(feature = "bundled")]
119+
fn patch_sdl2(sdl2_source_path: &Path) {
120+
// vector of <(patch_file_name, patch_file_contents)>
121+
let patches: Vec<(&str, &'static str)> = vec![
122+
// This patch fixes a CMake installation bug introduced in SDL2 2.0.4 on
123+
// the Mac OS platform. Without this patch, the libSDL2.dylib generated
124+
// during the SDL2 build phase will be overwritten by a symlink pointing
125+
// to nothing. A variation of this patch was accepted upstream and
126+
// should be included in SDL2 2.0.9.
127+
// https://bugzilla.libsdl.org/show_bug.cgi?id=4234
128+
("SDL2-2.0.8-4234-mac-os-dylib-fix.patch", include_str!("patches/SDL2-2.0.8-4234-mac-os-dylib-fix.patch")),
129+
];
130+
let sdl_version = format!("SDL2-{}", LASTEST_SDL2_VERSION);
131+
132+
for patch in &patches {
133+
// Only apply patches whose file name is prefixed with the currently
134+
// targeted version of SDL2.
135+
if !patch.0.starts_with(&sdl_version) {
136+
continue;
137+
}
138+
let mut patch_set = unidiff::PatchSet::new();
139+
patch_set.parse(patch.1).expect("Error parsing diff");
140+
141+
// For every modified file, copy the existing file to <file_name>_old,
142+
// open a new copy of <file_name>. and fill the new file with a
143+
// combination of the unmodified contents, and the patched sections.
144+
// TOOD: This code is untested (save for the immediate application), and
145+
// probably belongs in the unidiff (or similar) package.
146+
for modified_file in patch_set.modified_files() {
147+
use std::io::{Write, BufRead};
148+
149+
let file_path = sdl2_source_path.join(modified_file.path());
150+
let old_path = sdl2_source_path.join(format!("{}_old", modified_file.path()));
151+
fs::rename(&file_path, &old_path)
152+
.expect(&format!(
153+
"Rename of {} to {} failed",
154+
file_path.to_string_lossy(),
155+
old_path.to_string_lossy()));
156+
157+
let dst_file = fs::File::create(file_path).unwrap();
158+
let mut dst_buf = io::BufWriter::new(dst_file);
159+
let old_file = fs::File::open(old_path).unwrap();
160+
let mut old_buf = io::BufReader::new(old_file);
161+
let mut cursor = 0;
162+
163+
for (i, hunk) in modified_file.into_iter().enumerate() {
164+
// Write old lines from cursor to the start of this hunk.
165+
let num_lines = hunk.source_start - cursor - 1;
166+
for _ in 0..num_lines {
167+
let mut line = String::new();
168+
old_buf.read_line(&mut line).unwrap();
169+
dst_buf.write_all(line.as_bytes()).unwrap();
170+
}
171+
cursor += num_lines;
172+
173+
// Skip lines in old_file, and verify that what we expect to
174+
// replace is present in the old_file.
175+
for expected_line in hunk.source_lines() {
176+
let mut actual_line = String::new();
177+
old_buf.read_line(&mut actual_line).unwrap();
178+
actual_line.pop(); // Remove the trailing newline.
179+
if expected_line.value != actual_line {
180+
panic!("Can't apply patch; mismatch between expected and actual in hunk {}", i);
181+
}
182+
}
183+
cursor += hunk.source_length;
184+
185+
// Write the new lines into the destination.
186+
for line in hunk.target_lines() {
187+
dst_buf.write_all(line.value.as_bytes()).unwrap();
188+
dst_buf.write_all(b"\n").unwrap();
189+
}
190+
}
191+
192+
// Write all remaining lines from the old file into the new.
193+
for line in old_buf.lines() {
194+
dst_buf.write_all(&line.unwrap().into_bytes()).unwrap();
195+
dst_buf.write_all(b"\n").unwrap();
196+
}
197+
}
198+
// For every removed file, simply delete the original.
199+
// TODO: This is entirely untested code. There are likely bugs here, and
200+
// this really should be part of the unidiff library, not a function
201+
// defined here. Hopefully this gets moved somewhere else before it
202+
// bites someone.
203+
for removed_file in patch_set.removed_files() {
204+
fs::remove_file(sdl2_source_path.join(removed_file.path()))
205+
.expect(
206+
&format!("Failed to remove file {} from {}",
207+
removed_file.path(),
208+
sdl2_source_path.to_string_lossy()));
209+
}
210+
// For every new file, copy the entire contents of the patched file into
211+
// a newly created <file_name>.
212+
// TODO: This is entirely untested code. There are likely bugs here, and
213+
// this really should be part of the unidiff library, not a function
214+
// defined here. Hopefully this gets moved somewhere else before it
215+
// bites someone.
216+
for added_file in patch_set.added_files() {
217+
use std::io::Write;
218+
219+
// This should be superfluous. I don't know how a new file would
220+
// ever have more than one hunk.
221+
assert!(added_file.len() == 1);
222+
let file_path = sdl2_source_path.join(added_file.path());
223+
let mut dst_file = fs::File::create(&file_path)
224+
.expect(&format!(
225+
"Failed to create file {}",
226+
file_path.to_string_lossy()));
227+
let mut dst_buf = io::BufWriter::new(&dst_file);
228+
229+
for line in added_file.into_iter().nth(0).unwrap().target_lines() {
230+
dst_buf.write_all(line.value.as_bytes()).unwrap();
231+
dst_buf.write_all(b"\n").unwrap();
232+
}
233+
}
234+
}
235+
}
236+
115237
// compile a shared or static lib depending on the feature
116238
#[cfg(feature = "bundled")]
117239
fn compile_sdl2(sdl2_build_path: &Path, target_os: &str) -> PathBuf {
@@ -333,6 +455,7 @@ fn main() {
333455
let sdl2_compiled_path: PathBuf;
334456
#[cfg(feature = "bundled")] {
335457
let sdl2_source_path = download_sdl2();
458+
patch_sdl2(sdl2_source_path.as_path());
336459
sdl2_compiled_path = compile_sdl2(sdl2_source_path.as_path(), target_os);
337460

338461
let sdl2_downloaded_include_path = sdl2_source_path.join("include");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# HG changeset patch
2+
# User Drew Pirrone-Brusse <[email protected]>
3+
# Date 1537744393 14400
4+
# Sun Sep 23 19:13:13 2018 -0400
5+
# Node ID b66fb83b6897137c1c2b857ee5490e602f8c31b0
6+
# Parent f1084c419f33610cf274e309a8b2798d2ae665c7
7+
Correct the name of the SDL shared library in CMake for Mac OS
8+
9+
diff -r f1084c419f33 -r b66fb83b6897 CMakeLists.txt
10+
--- a/CMakeLists.txt Thu Mar 01 08:26:10 2018 -0800
11+
+++ b/CMakeLists.txt Sun Sep 23 19:13:13 2018 -0400
12+
@@ -1704,7 +1704,9 @@
13+
if(SDL_SHARED)
14+
add_library(SDL2 SHARED ${SOURCE_FILES} ${VERSION_SOURCES})
15+
if(APPLE)
16+
- set_target_properties(SDL2 PROPERTIES MACOSX_RPATH 1)
17+
+ set_target_properties(SDL2 PROPERTIES
18+
+ MACOSX_RPATH 1
19+
+ OUTPUT_NAME "SDL2-${LT_RELEASE}")
20+
elseif(UNIX AND NOT ANDROID)
21+
set_target_properties(SDL2 PROPERTIES
22+
VERSION ${LT_VERSION}
23+
@@ -1810,16 +1812,14 @@
24+
25+
if(NOT (WINDOWS OR CYGWIN))
26+
if(SDL_SHARED)
27+
- if (APPLE)
28+
- set(SOEXT "dylib")
29+
- else()
30+
- set(SOEXT "so")
31+
- endif()
32+
+ set(SOEXT ${CMAKE_SHARED_LIBRARY_SUFFIX}) # ".so", ".dylib", etc.
33+
+ get_target_property(SONAME SDL2 OUTPUT_NAME)
34+
if(NOT ANDROID)
35+
install(CODE "
36+
execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink
37+
- \"libSDL2-2.0.${SOEXT}\" \"libSDL2.${SOEXT}\")")
38+
- install(FILES ${SDL2_BINARY_DIR}/libSDL2.${SOEXT} DESTINATION "lib${LIB_SUFFIX}")
39+
+ \"lib${SONAME}${SOPOSTFIX}${SOEXT}\" \"libSDL2${SOPOSTFIX}${SOEXT}\")"
40+
+ WORKING_DIR "${SDL2_BINARY_DIR}")
41+
+ install(FILES ${SDL2_BINARY_DIR}/libSDL2${SOPOSTFIX}${SOEXT} DESTINATION "lib${LIB_SUFFIX}")
42+
endif()
43+
endif()
44+
if(FREEBSD)

src/sdl2/audio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ mod test {
823823
assert!(cvt.is_conversion_needed());
824824

825825
// since we're going from mono to stereo, our capacity must be at least twice the original (255) vec size
826-
assert!(cvt.capacity(255) > 255*2, "capacity must be able to hold the converted audio sample");
826+
assert!(cvt.capacity(255) >= 255*2, "capacity must be able to hold the converted audio sample");
827827

828828
let new_buffer = cvt.convert(buffer);
829829
assert_eq!(new_buffer.len(), new_buffer_expected.len(), "capacity must be exactly equal to twice the original vec size");

0 commit comments

Comments
 (0)