aboutsummaryrefslogtreecommitdiff
path: root/m4/erlang-extra.m4
blob: 4a7311bad3c4791517813404fe8ea8f76762130f (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
dnl erlang-extra.m4

AC_DEFUN([ERLANG_SUBST_LIB_VER],
[AC_ERLANG_CHECK_LIB([$1])
ERLANG_LIB_VER_SUBST="$ERLANG_LIB_VER_SUBST -e 's,[@]ERLANG_LIB_VER_$1[@],\$(ERLANG_LIB_VER_$1),g'"
AC_SUBST([ERLANG_LIB_VER_SUBST])
]) # ERLANG_SUBST_LIB_VER

AC_DEFUN([ERLANG_VERSION_CHECK],
[		AC_MSG_CHECKING([Erlang/OTP version])
		cat > conftest.erl <<EOF
-module(conftest).
-export([[start/0]]).

start() ->
    ERTS = erlang:system_info(version),
    RequiredMin = "$1",
    RequiredMax = "$2",
    Status =
        case {string:tokens(RequiredMin, " "),
              string:tokens(RequiredMax, " ")} of
	    {[[MinStr | _]], [[MaxStr | _]]} ->
                case check(ERTS, {MinStr, MaxStr}) of
                    less ->
                         list_to_binary([[ERTS, " found, ", RequiredMin, " required"]]);
                    greater ->
                         list_to_binary([[ERTS, " found, ", RequiredMax, " or earlier required"]]);
                    ok ->
	                 <<"ok">>
                end;
	    _ ->
	        list_to_binary([[ERTS, " found, ", RequiredMin, " required"]])
	end,
    file:write_file("conftest.out", Status),
    halt().

check(CurStr, {MinStr, MaxStr}) ->
    Cur = parse(CurStr),
    Min = parse(MinStr),
    Max = parse(MaxStr),
    case {less_or_equal(Min, Cur), less_or_equal(Cur, Max)} of
        {false, true} -> less;
        {true, true} -> ok;
        {true, false} -> greater
    end.

parse(Version) ->
    lists:map(fun(A) -> {Int,[[]]} = string:to_integer(A), Int end,
              string:tokens(Version, ".")).

less_or_equal([[]], [[]]) ->
    true;
less_or_equal([[]], _Any) ->
    true;
less_or_equal(_Any, [[]]) ->
    false;
less_or_equal([[Left| Rl]], [[Right| Rr]]) ->
    case {Left < Right, Left == Right} of
        {true, _}  ->
            true;
        {false, false} ->
            false;
        {false, true} ->
            less_or_equal(Rl, Rr)
    end.

EOF

	$ERLC conftest.erl || AC_MSG_ERROR(["Could not compile Erlang/OTP version check program using '$ERLC'"])

	if ! $ERL -s conftest -noshell -o ! -f conftest.out ; then
	   AC_MSG_ERROR(["Could not run Erlang/OTP version check program using '$ERL'"])
	fi

	if test "x`cat conftest.out`" != "xok"; then
	   AC_MSG_RESULT([failed])
	   X="`cat conftest.out`"
	   if test "[$3]" == "warn"; then
	      AC_MSG_WARN([$X])
	   else
	      AC_MSG_FAILURE([$X])
	   fi
	else
	   AC_MSG_RESULT([ok])
	fi
]) dnl ERLANG_VERSION_CHECK