Fixed bug 2795 - SDL library detection selects the wrong lib
authorSam Lantinga <slouken@libsdl.org>
Sat, 29 Nov 2014 14:41:43 -0800
changeset 4672b0ada991468
parent 466 55801d955e3a
child 468 3700789b0895
Fixed bug 2795 - SDL library detection selects the wrong lib

Chris Beck

When creating a homebrew recipe for wesnoth, I discovered that the SDL image configuration routine does not detect libpng properly -- if you have multiple instances of libpng on your system, and you use environment variables to select an instance which is not in your system directory, the build can be broken, because it will run configuration tests against the system installed version, but deduce that it should use the filename of the system-installed version. In a vanilla build of wesnoth using homebrew, this results in segfaults at runtime, because you end up linking against two different versions of libpng, which is also needed independently of SDL.

The problem is essentially in the "find_lib" routine in the configure file:



find_lib()
{
gcc_bin_path=[`$CC -print-search-dirs 2>/dev/null | fgrep programs: | sed 's/[^=]*=\(.*\)/\1/' | sed 's/:/ /g'`]
gcc_lib_path=[`$CC -print-search-dirs 2>/dev/null | fgrep libraries: | sed 's/[^=]*=\(.*\)/\1/' | sed 's/:/ /g'`]
env_lib_path=[`echo $LIBS $LDFLAGS | sed 's/-L[ ]*//g'`]
for path in $gcc_bin_path $gcc_lib_path $env_lib_path /usr/lib /usr/local/lib; do
lib=[`ls -- $path/$1 2>/dev/null | sort -r | sed 's/.*\/\(.*\)/\1/; q'`]
if test x$lib != x; then
echo $lib
return
fi
done
}



Because the for loop goes over the system directories before the environment directories, any system-installed lib will shadow the lib selected via environment variables. This is contrary to the behavior of the configuration tests earlier in the script, which prefers the environment variable libs over the system-installed libs. The 'for' loop should instead be:



for path in $env_lib_path $gcc_bin_path $gcc_lib_path /usr/lib /usr/local/lib; do



You can see the full discussion on the Homebrew / linuxbrew issue tracker here: https://github.com/Homebrew/linuxbrew/issues/172

I have checked that this bug also affects SDL 1.2.15, SDL_mixer and SDL_ttf 1.2, which all use this same "find_lib" routine. I have not determined if the bug affects SDL 2.0, which seems not to use this exact routine.
configure
configure.in
     1.1 --- a/configure	Sat Nov 29 12:06:05 2014 -0800
     1.2 +++ b/configure	Sat Nov 29 14:41:43 2014 -0800
     1.3 @@ -12058,7 +12058,7 @@
     1.4      else
     1.5          host_lib_path="/usr/$base_libdir /usr/local/$base_libdir"
     1.6      fi
     1.7 -    for path in $gcc_bin_path $gcc_lib_path $env_lib_path $host_lib_path; do
     1.8 +    for path in $env_lib_path $gcc_bin_path $gcc_lib_path $host_lib_path; do
     1.9          lib=`ls -- $path/$1 2>/dev/null | sed -e '/\.so\..*\./d' -e 's,.*/,,' | sort | tail -1`
    1.10          if test x$lib != x; then
    1.11              echo $lib
     2.1 --- a/configure.in	Sat Nov 29 12:06:05 2014 -0800
     2.2 +++ b/configure.in	Sat Nov 29 14:41:43 2014 -0800
     2.3 @@ -103,7 +103,7 @@
     2.4      else
     2.5          host_lib_path="/usr/$base_libdir /usr/local/$base_libdir"
     2.6      fi
     2.7 -    for path in $gcc_bin_path $gcc_lib_path $env_lib_path $host_lib_path; do
     2.8 +    for path in $env_lib_path $gcc_bin_path $gcc_lib_path $host_lib_path; do
     2.9          lib=[`ls -- $path/$1 2>/dev/null | sed -e '/\.so\..*\./d' -e 's,.*/,,' | sort | tail -1`]
    2.10          if test x$lib != x; then
    2.11              echo $lib