]> git.draconx.ca Git - dxcommon.git/blob - m4/flex.m4
Avoid nonportable ${x##y} substitutions in program tests.
[dxcommon.git] / m4 / flex.m4
1 dnl Copyright © 2020 Nick Bowler
2 dnl License WTFPL2: Do What the Fuck You Want To Public License, version 2.
3 dnl This is free software: you are free to do what the fuck you want to.
4 dnl There is NO WARRANTY, to the extent permitted by law.
5
6 dnl DX_PROG_FLEX([min-version], [action-if-found], [action-if-not-found])
7 dnl
8 dnl Search PATH for a working flex utility with the given minimum version,
9 dnl which may be empty to accept any version.  If found, the FLEX
10 dnl variable (which is substituted by AC_SUBST) is set with the result,
11 dnl and action-if-found is executed.  Otherwise, action-if-not-found
12 dnl is executed if nonempty, else configure will exit with a fatal error.
13 AC_DEFUN([DX_PROG_FLEX], [AC_PREREQ([2.62])dnl
14 AC_ARG_VAR([FLEX], [Fast Lexical Analyzer command])dnl
15 AC_ARG_VAR([FLEXFLAGS], [Additional options to pass to flex])dnl
16 cat >conftest.l <<'EOF'
17 m4_apply([_DX_FLEX_TEST_PROGRAM], m4_split([$1], [\.]))
18 EOF
19 AC_CACHE_CHECK([for flex], [ac_cv_path_FLEX],
20 [dx_cv_flex_found=:
21 AC_PATH_PROGS_FEATURE_CHECK([FLEX], [flex lex],
22 [_DX_FLEX_DO_TEST([$ac_path_FLEX],
23 [DX_BASENAME([flex_relcmd], ["$ac_path_FLEX"])
24 flex_bypath=`{ command -v "$flex_relcmd"; } 2>/dev/null` #''
25 ac_cv_path_FLEX=$ac_path_FLEX
26 test x"$flex_bypath" = x"$ac_path_FLEX" && ac_cv_path_FLEX=$flex_relcmd
27 ac_path_FLEX_found=:
28 dx_cv_flex_works=yes])], [ac_cv_path_FLEX=no dx_cv_flex_found=false])])
29 AS_IF([$dx_cv_flex_found],
30 [FLEX=$ac_cv_path_FLEX
31 AC_CACHE_CHECK([whether $FLEX works], [dx_cv_flex_works],
32 [_DX_FLEX_DO_TEST([$FLEX], [dx_cv_flex_works=yes], [dx_cv_flex_works=no])])])
33 AS_IF([test x"$dx_cv_flex_works" = x"yes"],
34   [$2], [m4_default([$3], [AC_MSG_FAILURE(
35 [flex m4_ifnblank([$1], [version at least $1 ])is required.
36
37 The latest version may be found at <https://github.com/westes/flex/>.
38 ])])])
39 rm -f conftest.c conftest.inc conftest.l
40 ])
41
42 m4_define([_DX_FLEX_TEST_PROGRAM],
43 [%option noyywrap
44 %{
45 #ifndef FLEX_SCANNER
46 DX_LIB_COMPILE_ERROR([FLEX_SCANNER not defined])
47 #endif
48 m4_ifnblank([$1], [dnl
49 #if YY_FLEX_MAJOR_VERSION < $1
50 DX_LIB_COMPILE_ERROR([major version insufficient])
51 m4_ifnblank([$2], [dnl
52 #elif YY_FLEX_MAJOR_VERSION == $1
53 #  if YY_FLEX_MINOR_VERSION < $2
54 DX_LIB_COMPILE_ERROR([minor version insufficient])
55 m4_ifnblank([$3], [dnl
56 #  elif YY_FLEX_MINOR_VERSION == $2
57 #    if YY_FLEX_SUBMINOR_VERSION < $3
58 DX_LIB_COMPILE_ERROR([subminor version insufficient])
59 #    endif
60 ])dnl
61 #  endif
62 ])dnl
63 #endif
64 ])dnl
65 %}
66 %%
67 %%
68 int main(void)
69 {
70   return yylex();
71 }])
72
73 AC_DEFUN([_DX_FLEX_DO_TEST], [rm -f conftest.c
74 AS_IF([$1 -o conftest.inc conftest.l 2>&AS_MESSAGE_LOG_FD],
75 [AC_LINK_IFELSE([AC_LANG_SOURCE([[#include "conftest.inc"]])], [$2], [$3])],
76 [$3])])