summaryrefslogtreecommitdiff
path: root/m4/efl_find_x.m4
blob: dad401fcf8348d2ad7c7d843db724437fc8314aa (plain) (blame)
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
# efl_find.x.m4 - Macros to locate X11.                       -*- Autoconf -*-
# EFL_FIND_X(VARIABLE-PREFIX, [headers = "X11/Xlib.h"],
#            [libs-and-functions = "X11 XOpenDisplay"],
#            [action-if-found], [action-if-not-found])
# checks for X11 using, in order:
# 1) Xorg pkg-config files (using enviroment variables EFL_X11_CFLAGS
#                           and EFL_X11_LIBS if set, but only if x11.pc exists)
# 2) command line options: --x-includes=dir, --x-libraries=dir
#                          assume there is an X11 in the given directories
# 3) XMKMF environment variable if set
# 4) xmkmf executable if found
# 5) list of "standard" directories
#
# 2-5 is handled by A_PATH_X
#
# If a X11 is found, [action-if-success] is run and VARIABLE_cflags and
# VARIABLE_libs and VARIABLE_libdirs are defined and substituted.
# VARIABLE_libs will contain all of the libs listed in libs-and-functions.
# VARIABLE_libdirs will contain all -Lpath:s found in VARIABLE_libs
#
# headers is a list of headers to look for. libs-and-functions is a list of
# library and function pairs to look for.
# Each lib and function is checked in pairs, example:
# EFL_FIND_X([EVAS_X11], [X11/X.h], [X11 XCreateImage Xext XShmCreateImage])
# will look for XCreateImage in X11 and XShmCreateImage in Xext and include
# both -lX11 and -lXext in VARIABLE_libs
#
# action-if-found is only called if X11, all headers, all libraries and
# all functions are found.
# You can call EFL_FIND_X multiple times with different lists of headers, libs
# and functions.

AC_DEFUN([EFL_FIND_X],
[
  # Must print something as AC_CACHE_VAL writes (cached) if the value is cached
  AC_MSG_CHECKING([how to find X])
  efl_x11_need_result=1
  AC_CACHE_VAL(efl_cv_x11_cache,
    [
      # this is ugly and breaks that AC_CACHE_VAL may not have side effects
      # but I can't think of a better way right now
      efl_x11_need_result=0
      PKG_CHECK_EXISTS([x11],
        [
          AC_MSG_RESULT([use pkg-config])
          PKG_CHECK_MODULES([EFL_X11],[x11],
            [
              efl_cv_have_x11=yes
              efl_cv_x11_pkgconf=yes
              efl_cv_x11_cflags=$EFL_X11_CFLAGS
              efl_cv_x11_libs_pre=$EFL_X11_LIBS
              efl_cv_x11_libs_post=
            ])
        ],
        [
          AC_MSG_RESULT([use xmkmf])
          # Fallback to old AC_PATH_XTRA
          AC_PATH_X
          AC_PATH_XTRA
          if test "$no_x" = yes; then
            efl_cv_have_x11=no
          else
            efl_cv_have_x11=yes
            efl_cv_x11_pkgconf=no
            efl_cv_x11_cflags=$X_CFLAGS
            efl_cv_x11_libs_pre="$X_PRE_LIBS $X_LIBS"
            efl_cv_x11_libs_post=$X_EXTRA_LIBS
          fi
         ])
      # Record where we found X for the cache.
      if test "x$efl_cv_have_x11" = "xno"; then
        efl_cv_x11_cache="efl_cv_have_x11=no"
      else
        efl_cv_x11_cache="efl_cv_have_x11=yes\
          efl_cv_x11_pkgconf='$efl_cv_x11_pkgconf'\
          efl_cv_x11_cflags='$efl_cv_x11_cflags'\
          efl_cv_x11_libs_pre='$efl_cv_x11_libs_pre'\
          efl_cv_x11_libs_post='$efl_cv_x11_libs_post'"
      fi
    ])
  if test "x$efl_x11_need_result" = "x1"; then
    AC_MSG_RESULT([already found])
  fi
  eval "$efl_cv_x11_cache"

  if test "x$efl_cv_have_x11" = "xyes"; then
    ELF_X11_CFLAGS_save="$CFLAGS"
    ELF_X11_CPPFLAGS_save="$CPPFLAGS"
    CFLAGS="$CFLAGS $efl_cv_x11_cflags"
    CPPFLAGS="$CPPFLAGS $efl_cv_x11_cflags"
    efl_x11_found_all=1
    for efl_x11_header in ifelse([$2], , "X11/Xlib.h", [$2]); do
      AC_CHECK_HEADER([$efl_x11_header],,[
        efl_x11_found_all=0
        break])
    done
    CPPFLAGS="$ELF_X11_CPPFLAGS_save"
    CFLAGS="$ELF_X11_CFLAGS_save"

    if test "x$efl_x11_found_all" = "x1"; then
      EFL_X11_LIBS_save="$LIBS"
      if test "x$efl_cv_x11_pkgconf" = "xyes"; then
        efl_x11_modules="x11"
        efl_x11_lib=""
        for efl_x11_lib_function in ifelse([$3], , "X11 XOpenDisplay", [$3]); do
          if test -z "$efl_x11_lib"; then
            efl_x11_lib="$efl_x11_lib_function"
            case $efl_x11_lib in
              X11)
                ;;
              Xss)
                efl_x11_modules="$efl_x11_modules xscrnsaver"
                ;;
              *)
                efl_x11_lib=`echo $efl_x11_lib | tr '[A-Z]' '[a-z]'`
                efl_x11_modules="$efl_x11_modules $efl_x11_lib"
                ;;
            esac
          else
            efl_x11_lib=
          fi
        done

        PKG_CHECK_EXISTS([$efl_x11_modules],
          [
            PKG_CHECK_MODULES([$1],[$efl_x11_modules],
              [
                efl_x11_cflags=$[]$1[]_CFLAGS
                efl_x11_libs=$[]$1[]_LIBS

                LIBS="$LIBS $[]$1[]_LIBS"
                efl_x11_lib=""
                for efl_x11_lib_function in ifelse([$3], , "X11 XOpenDisplay", [$3]); do
                  if test -z "$efl_x11_lib"; then
                    efl_x11_lib="$efl_x11_lib_function"
                  else
                    # This is a ugly way of using AC_CHECK_FUNC with different
                    # LIBS
                    eval "unset ac_cv_func_$efl_x11_lib_function"
                    AC_CHECK_FUNC([$efl_x11_lib_function],,
                      [
                        efl_x11_found_all=0
                        break])
                    efl_x11_lib=
                  fi
                done
              ])
           ],[efl_x11_found_all=0])
      else
        LIBS="$LIBS $efl_cv_x11_libs_pre"
        efl_x11_libs="$efl_cv_x11_libs_pre"
        efl_x11_lib=""
        for efl_x11_lib_function in ifelse([$3], , "X11 XOpenDisplay", [$3]); do
          if test -z "$efl_x11_lib"; then
            efl_x11_lib="$efl_x11_lib_function"
          else
            AC_CHECK_LIB([$efl_x11_lib], [$efl_x11_lib_function],,[
              efl_x11_found_all=0
              break],["$efl_cv_x11_libs_post"])
            efl_x11_libs="$efl_x11_libs -l$efl_x11_lib"
            efl_x11_lib=
          fi
        done
        if test -n "$efl_cv_x11_libs_post"; then
          efl_x11_libs="$efl_x11_libs $efl_cv_x11_libs_post"
        fi
      fi
      LIBS="$EFL_X11_LIBS_save"
    fi
  fi

  if test "x$efl_x11_found_all" = "x1"; then
    efl_x11_libdirs=""
    for efl_x11_option in "$efl_x11_libs"; do
      case $efl_x11_option in
        -L*)
          efl_x11_libdirs="$efl_x11_libdirs $efl_x11_option"
          ;;
        *)
          ;;
      esac
    done

    AC_SUBST([$1][_cflags],[$efl_cv_x11_cflags])
    AC_SUBST([$1][_libs],[$efl_x11_libs])
    AC_SUBST([$1][_libdirs],[$efl_x11_libdirs])

    ifelse([$4], , :, [$4])
  else
    ifelse([$5], , :, [$5])
  fi
])