1dnl -------------------------------------------------------------------------
2dnl Try to find a file (or one of more files in a list of dirs).
3dnl -------------------------------------------------------------------------
4
5AC_DEFUN(MY_FIND_FILE,
6    [
7    $3=NO
8    for i in $2;
9    do
10        for j in $1;
11        do
12	        if test -r "$i/$j"; then
13		        $3=$i
14                break 2
15            fi
16        done
17    done
18    ]
19)
20
21dnl -------------------------------------------------------------------------
22dnl Try to find SIDPLAY includes and library.
23dnl $my_have_sidplay will be "yes" or "no"
24dnl $(sidplay_libdir) will be path to libsidplay.*
25dnl $(sidplay_incdir) will be path to sidplay/*.h
26dnl @SIDPLAY_LDFLAGS@ will be substituted with -L$sidplay_libdir
27dnl @SIDPLAY_INCLUDES@ will be substituted with -I$sidplay_incdir
28dnl -------------------------------------------------------------------------
29
30AC_DEFUN(MY_PATH_LIBSIDPLAY,
31[
32    AC_MSG_CHECKING([for working SIDPLAY library and headers])
33
34    dnl Be pessimistic.
35    my_sidplay_library=NO
36    my_sidplay_includes=NO
37    sidplay_libdir=""
38    sidplay_incdir=""
39
40    AC_ARG_WITH(sidplay-includes,
41        [  --with-sidplay-includes=DIR
42                          where the sidplay includes are located],
43        [my_sidplay_includes="$withval"]
44    )
45
46    AC_ARG_WITH(sidplay-library,
47        [  --with-sidplay-library=DIR
48                          where the sidplay library is installed],
49        [my_sidplay_library="$withval"]
50    )
51
52    # Test compilation with library and headers in standard path.
53    my_sidplay_libadd=""
54    my_sidplay_incadd=""
55
56    # Use library path given by user (if any).
57    if test "$my_sidplay_library" != NO; then
58        my_sidplay_libadd="-L$my_sidplay_library"
59    fi
60
61    # Use include path given by user (if any).
62    if test "$my_sidplay_includes" != NO; then
63        my_sidplay_incadd="-I$my_sidplay_includes"
64    fi
65
66    # Run test compilation.
67    AC_CACHE_VAL(my_cv_libsidplay_works,
68    [
69        MY_TRY_LIBSIDPLAY
70        my_cv_libsidplay_works=$my_libsidplay_works
71    ])
72
73    if test "$my_cv_libsidplay_works" = no; then
74
75        # Test compilation failed.
76        # Need to search for library and headers.
77        AC_CACHE_VAL(my_cv_have_sidplay,
78        [
79            # Search common locations where header files might be stored.
80            sidplay_incdirs="$my_sidplay_includes /usr/include /usr/local/include /usr/lib/sidplay/include /usr/local/lib/sidplay/include"
81            MY_FIND_FILE(sidplay/sidtune.h,$sidplay_incdirs,sidplay_foundincdir)
82            my_sidplay_includes=$sidplay_foundincdir
83
84            # Search common locations where library might be stored.
85            sidplay_libdirs="$my_sidplay_library /usr/lib /usr/local/lib /usr/lib/sidplay /usr/local/lib/sidplay"
86            MY_FIND_FILE(libsidplay.so libsidplay.so.1 libsidplay.so.1.36 libsidplay.so.1.37,$sidplay_libdirs,sidplay_foundlibdir)
87            my_sidplay_library=$sidplay_foundlibdir
88
89            if test "$my_sidplay_includes" = NO || test "$my_sidplay_library" = NO; then
90                my_cv_have_sidplay="my_have_sidplay=no"
91            else
92                my_cv_have_sidplay="my_have_sidplay=yes"
93            fi
94        ])
95
96        eval "$my_cv_have_sidplay"
97        if test "$my_have_sidplay" = yes; then
98            sidplay_libadd="-L$my_sidplay_library"
99            sidplay_incadd="-I$my_sidplay_includes"
100
101            # Test compilation with found paths.
102            AC_CACHE_VAL(my_cv_found_libsidplay_works,
103            [
104                MY_TRY_LIBSIDPLAY
105                my_cv_found_libsidplay_works=$my_libsidplay_works
106            ])
107            my_have_sidplay=$my_cv_found_libsidplay_works
108
109            if test "$my_have_sidplay" = no; then
110                # Found library does not link without errors.
111                my_have_sidplay=no
112                AC_MSG_RESULT([$my_have_sidplay]);
113            else
114                AC_MSG_RESULT([library $my_sidplay_library, headers $my_sidplay_includes])
115            fi
116        else
117            # Either library or headers not found.
118            AC_MSG_RESULT([$my_have_sidplay]);
119        fi
120    else
121        # Simply print 'yes' without printing the standard path.
122        my_have_sidplay=yes
123        AC_MSG_RESULT([$my_have_sidplay]);
124    fi
125
126    AC_SUBST(sidplay_libdir)
127    AC_SUBST(sidplay_incdir)
128
129    SIDPLAY_LDFLAGS="$my_sidplay_libadd"
130    SIDPLAY_INCLUDES="$my_sidplay_incadd"
131
132    AC_SUBST(SIDPLAY_LDFLAGS)
133    AC_SUBST(SIDPLAY_INCLUDES)
134])
135
136dnl Function used by MY_PATH_LIBSIDPLAY.
137
138AC_DEFUN(MY_TRY_LIBSIDPLAY,
139[
140    my_cxxflags_save=$CXXFLAGS
141    my_ldflags_save=$LDFLAGS
142    my_libs_save=$LIBS
143
144    CXXFLAGS="$CXXFLAGS $my_sidplay_incadd"
145    LDFLAGS="$LDFLAGS $my_sidplay_libadd"
146    LIBS="-lsidplay"
147
148    AC_TRY_LINK(
149        [#include <sidplay/sidtune.h>],
150        [sidTune* myTest;],
151        [my_libsidplay_works=yes],
152        [my_libsidplay_works=no]
153    )
154
155    CXXFLAGS="$my_cxxflags_save"
156    LDFLAGS="$my_ldflags_save"
157    LIBS="$my_libs_save"
158])
159
160