1dnl autoconf macros for OCaml
2dnl from http://forge.ocamlcore.org/
3dnl
4dnl Copyright © 2009      Richard W.M. Jones
5dnl Copyright © 2009      Stefano Zacchiroli
6dnl Copyright © 2000-2005 Olivier Andrieu
7dnl Copyright © 2000-2005 Jean-Christophe Filliâtre
8dnl Copyright © 2000-2005 Georges Mariano
9dnl
10dnl For documentation, please read the ocaml.m4 man page.
11
12AC_DEFUN([AC_PROG_OCAML],
13[dnl
14  # checking for ocamlc
15  AC_CHECK_TOOL([OCAMLC],[ocamlc],[no])
16
17  if test "$OCAMLC" != "no"; then
18     OCAMLVERSION=`$OCAMLC -v | sed -n -e 's|.*version* *\(.*\)$|\1|p'`
19     AC_MSG_RESULT([OCaml version is $OCAMLVERSION])
20     # If OCAMLLIB is set, use it
21     if test "$OCAMLLIB" = ""; then
22        OCAMLLIB=`$OCAMLC -where 2>/dev/null || $OCAMLC -v|tail -1|cut -d ' ' -f 4`
23     else
24        AC_MSG_RESULT([OCAMLLIB previously set; preserving it.])
25     fi
26     AC_MSG_RESULT([OCaml library path is $OCAMLLIB])
27
28     AC_SUBST([OCAMLVERSION])
29     AC_SUBST([OCAMLLIB])
30
31     # checking for ocamlopt
32     AC_CHECK_TOOL([OCAMLOPT],[ocamlopt],[no])
33     OCAMLBEST=byte
34     if test "$OCAMLOPT" = "no"; then
35	AC_MSG_WARN([Cannot find ocamlopt; bytecode compilation only.])
36     else
37	TMPVERSION=`$OCAMLOPT -v | sed -n -e 's|.*version* *\(.*\)$|\1|p' `
38	if test "$TMPVERSION" != "$OCAMLVERSION" ; then
39	    AC_MSG_RESULT([versions differs from ocamlc; ocamlopt discarded.])
40	    OCAMLOPT=no
41	else
42	    OCAMLBEST=opt
43	fi
44     fi
45
46     AC_SUBST([OCAMLBEST])
47
48     # checking for ocamlc.opt
49     AC_CHECK_TOOL([OCAMLCDOTOPT],[ocamlc.opt],[no])
50     if test "$OCAMLCDOTOPT" != "no"; then
51	TMPVERSION=`$OCAMLCDOTOPT -v | sed -n -e 's|.*version* *\(.*\)$|\1|p' `
52	if test "$TMPVERSION" != "$OCAMLVERSION" ; then
53	    AC_MSG_RESULT([versions differs from ocamlc; ocamlc.opt discarded.])
54	else
55	    OCAMLC=$OCAMLCDOTOPT
56	fi
57     fi
58
59     # checking for ocamlopt.opt
60     if test "$OCAMLOPT" != "no" ; then
61	AC_CHECK_TOOL([OCAMLOPTDOTOPT],[ocamlopt.opt],[no])
62	if test "$OCAMLOPTDOTOPT" != "no"; then
63	   TMPVERSION=`$OCAMLOPTDOTOPT -v | sed -n -e 's|.*version* *\(.*\)$|\1|p' `
64	   if test "$TMPVERSION" != "$OCAMLVERSION" ; then
65	      AC_MSG_RESULT([version differs from ocamlc; ocamlopt.opt discarded.])
66	   else
67	      OCAMLOPT=$OCAMLOPTDOTOPT
68	   fi
69        fi
70     fi
71
72     AC_SUBST([OCAMLOPT])
73  fi
74
75  AC_SUBST([OCAMLC])
76
77  # checking for ocaml toplevel
78  AC_CHECK_TOOL([OCAML],[ocaml],[no])
79
80  # checking for ocamldep
81  AC_CHECK_TOOL([OCAMLDEP],[ocamldep],[no])
82
83  # checking for ocamlmktop
84  AC_CHECK_TOOL([OCAMLMKTOP],[ocamlmktop],[no])
85
86  # checking for ocamlmklib
87  AC_CHECK_TOOL([OCAMLMKLIB],[ocamlmklib],[no])
88
89  # checking for ocamldoc
90  AC_CHECK_TOOL([OCAMLDOC],[ocamldoc],[no])
91
92  # checking for ocamlbuild
93  AC_CHECK_TOOL([OCAMLBUILD],[ocamlbuild],[no])
94])
95
96
97AC_DEFUN([AC_PROG_OCAMLLEX],
98[dnl
99  # checking for ocamllex
100  AC_CHECK_TOOL([OCAMLLEX],[ocamllex],[no])
101  if test "$OCAMLLEX" != "no"; then
102    AC_CHECK_TOOL([OCAMLLEXDOTOPT],[ocamllex.opt],[no])
103    if test "$OCAMLLEXDOTOPT" != "no"; then
104	OCAMLLEX=$OCAMLLEXDOTOPT
105    fi
106  fi
107  AC_SUBST([OCAMLLEX])
108])
109
110AC_DEFUN([AC_PROG_OCAMLYACC],
111[dnl
112  AC_CHECK_TOOL([OCAMLYACC],[ocamlyacc],[no])
113  AC_SUBST([OCAMLYACC])
114])
115
116
117AC_DEFUN([AC_PROG_CAMLP4],
118[dnl
119  AC_REQUIRE([AC_PROG_OCAML])dnl
120
121  # checking for camlp4
122  AC_CHECK_TOOL([CAMLP4],[camlp4],[no])
123  if test "$CAMLP4" != "no"; then
124     TMPVERSION=`$CAMLP4 -v 2>&1| sed -n -e 's|.*version *\(.*\)$|\1|p'`
125     if test "$TMPVERSION" != "$OCAMLVERSION" ; then
126	AC_MSG_RESULT([versions differs from ocamlc])
127        CAMLP4=no
128     fi
129  fi
130  AC_SUBST([CAMLP4])
131
132  # checking for companion tools
133  AC_CHECK_TOOL([CAMLP4BOOT],[camlp4boot],[no])
134  AC_CHECK_TOOL([CAMLP4O],[camlp4o],[no])
135  AC_CHECK_TOOL([CAMLP4OF],[camlp4of],[no])
136  AC_CHECK_TOOL([CAMLP4OOF],[camlp4oof],[no])
137  AC_CHECK_TOOL([CAMLP4ORF],[camlp4orf],[no])
138  AC_CHECK_TOOL([CAMLP4PROF],[camlp4prof],[no])
139  AC_CHECK_TOOL([CAMLP4R],[camlp4r],[no])
140  AC_CHECK_TOOL([CAMLP4RF],[camlp4rf],[no])
141  AC_SUBST([CAMLP4BOOT])
142  AC_SUBST([CAMLP4O])
143  AC_SUBST([CAMLP4OF])
144  AC_SUBST([CAMLP4OOF])
145  AC_SUBST([CAMLP4ORF])
146  AC_SUBST([CAMLP4PROF])
147  AC_SUBST([CAMLP4R])
148  AC_SUBST([CAMLP4RF])
149])
150
151
152AC_DEFUN([AC_PROG_FINDLIB],
153[dnl
154  AC_REQUIRE([AC_PROG_OCAML])dnl
155
156  # checking for ocamlfind
157  AC_CHECK_TOOL([OCAMLFIND],[ocamlfind],[no])
158  AC_SUBST([OCAMLFIND])
159])
160
161
162dnl Thanks to Jim Meyering for working this next bit out for us.
163dnl XXX We should define AS_TR_SH if it's not defined already
164dnl (eg. for old autoconf).
165AC_DEFUN([AC_CHECK_OCAML_PKG],
166[dnl
167  AC_REQUIRE([AC_PROG_FINDLIB])dnl
168
169  AC_MSG_CHECKING([for OCaml findlib package $1])
170
171  unset found
172  unset pkg
173  found=no
174  for pkg in $1 $2 ; do
175    if $OCAMLFIND query $pkg >/dev/null 2>/dev/null; then
176      AC_MSG_RESULT([found])
177      AS_TR_SH([OCAML_PKG_$1])=$pkg
178      found=yes
179      break
180    fi
181  done
182  if test "$found" = "no" ; then
183    AC_MSG_RESULT([not found])
184    AS_TR_SH([OCAML_PKG_$1])=no
185  fi
186
187  AC_SUBST(AS_TR_SH([OCAML_PKG_$1]))
188])
189
190
191AC_DEFUN([AC_CHECK_OCAML_MODULE],
192[dnl
193  AC_MSG_CHECKING([for OCaml module $2])
194
195  cat > conftest.ml <<EOF
196open $3
197EOF
198  unset found
199  for $1 in $$1 $4 ; do
200    if $OCAMLC -c -I "$$1" conftest.ml >&5 2>&5 ; then
201      found=yes
202      break
203    fi
204  done
205
206  if test "$found" ; then
207    AC_MSG_RESULT([$$1])
208  else
209    AC_MSG_RESULT([not found])
210    $1=no
211  fi
212  AC_SUBST([$1])
213])
214
215
216dnl XXX Cross-compiling
217AC_DEFUN([AC_CHECK_OCAML_WORD_SIZE],
218[dnl
219  AC_REQUIRE([AC_PROG_OCAML])dnl
220  AC_MSG_CHECKING([for OCaml compiler word size])
221  cat > conftest.ml <<EOF
222  print_endline (string_of_int Sys.word_size)
223  EOF
224  OCAML_WORD_SIZE=`$OCAML conftest.ml`
225  AC_MSG_RESULT([$OCAML_WORD_SIZE])
226  AC_SUBST([OCAML_WORD_SIZE])
227])
228
229AC_DEFUN([AC_CHECK_OCAML_OS_TYPE],
230[dnl
231  AC_REQUIRE([AC_PROG_OCAML])dnl
232  AC_MSG_CHECKING([OCaml Sys.os_type])
233
234  cat > conftest.ml <<EOF
235  print_string(Sys.os_type);;
236EOF
237
238  OCAML_OS_TYPE=`$OCAML conftest.ml`
239  AC_MSG_RESULT([$OCAML_OS_TYPE])
240  AC_SUBST([OCAML_OS_TYPE])
241])
242