missing m4
[m6w6/ext-psi] / m4 / psi_decl.m4
1 # psi_add_redir(name, symbol)
2 # Add a function redirection to $PSI_REDIRS.
3 psi_add_redir() {
4 cat >>$PSI_REDIRS <<<" {\"$1\", (void(*)(void))$2}, "
5 }
6
7 # psi_add_decl(decl, options)
8 # Add a pre-defined decl to $PSI_VA_DECLS/$PSI_DECLS.
9 psi_add_decl() {
10 case "$2" in
11 *vararg*)
12 cat >>$PSI_VA_DECLS <<<" $1, {0}, "
13 ;;
14 *)
15 cat >>$PSI_DECLS <<<" $1, {0}, "
16 ;;
17 esac
18 }
19
20 dnl PSI_REDIR(name, custom symbol)
21 dnl Create a function redirection to an optional custom symbol.
22 AC_DEFUN(PSI_REDIR, [
23 psi_add_redir $1 ifelse([$2],[],[$1],[$2])
24 ])
25
26 dnl PSI_FUNC_LIBC_MAIN()
27 dnl Check for the platforms default stub in executables.
28 AC_DEFUN(PSI_FUNC_LIBC_MAIN, [
29 AC_REQUIRE([AC_PROG_NM])
30 AC_REQUIRE([AC_PROG_AWK])
31 AC_CACHE_CHECK(for libc start main symbol, psi_cv_libc_main, [
32 psi_libc_main=
33 AC_TRY_LINK(PSI_INCLUDES, [(void)0;], [
34 psi_libc_main=`$NM -g conftest$ac_exeext | $AWK -F" *|@" '/^@<:@@<:@:space:@:>@@:>@+U / {print$[]3; exit}'`
35 ])
36 psi_cv_libc_main=$psi_libc_main
37 ])
38 ])
39
40 dnl PSI_FUNC(fn, action-if-yes, action-if-no)
41 dnl Check for a possible function redirection.
42 AC_DEFUN(PSI_FUNC, [
43 AC_REQUIRE([PSI_FUNC_LIBC_MAIN])
44 psi_symbol=$1
45 AC_CACHE_CHECK(for $1, psi_cv_fn_$1, [
46 psi_symbol_redirect=
47 AC_TRY_LINK(PSI_INCLUDES, [
48 void (*fn)(void) = (void (*)(void)) $psi_symbol;
49 ], [
50 psi_symbol_redirect=`$NM -g conftest$ac_exeext | $AWK -F" *|@" '/^@<:@@<:@:space:@:>@@:>@+U '$psi_cv_libc_main'/ {next} /^@<:@@<:@:space:@:>@@:>@+U / {print$[]3; exit}'`
51 ])
52 psi_cv_fn_$1=$psi_symbol_redirect
53 ])
54 case "$psi_cv_fn_$1" in
55 "")
56 $3
57 ;;
58 "$psi_symbol"|"_$psi_symbol")
59 $2
60 ;;
61 *)
62 $2
63 PSI_REDIR($1)
64 ;;
65 esac
66 ])
67
68 dnl PSI_DECL_ARG(decl arg)
69 dnl INTERNAL: build psi_decl_args
70 AC_DEFUN(PSI_DECL_ARG, [
71 m4_define([member_name], PSI_VAR_NAME($1))
72 m4_define([member_type], PSI_VAR_TYPE($1))
73
74 PSI_TYPE_INDIRECTION([$1],, pl, as)
75 if test -n "$psi_decl_args"; then
76 psi_decl_args="$psi_decl_args, "
77 fi
78 psi_decl_args="[$psi_decl_args{]PSI_TYPE_PAIR(member_type)[, \"]member_name[\",] $pl, $as[}]"
79 ])
80
81 dnl PSI_DECL(type func, args, flags)
82 dnl Check for a function or macro declaration.
83 dnl Adds a pre-defined (vararg) decl to $PSI_VA_DECLS/$PSI_DECLS.
84 dnl Calls PSI_MACRO if PSI_FUNC fails.
85 AC_DEFUN(PSI_DECL, [
86 psi_decl_args=
87 PSI_DECL_ARG($1)
88 m4_case([$2],
89 [(void)], [],
90 [()], [],
91 [m4_map_args_sep([PSI_DECL_ARG(m4_normalize(], [))], [], m4_bregexp([$2], [(\(.*\))], [\1]))])
92 PSI_FUNC(PSI_VAR_NAME($1), [
93 psi_add_decl "$psi_decl_args" $3
94 ], [
95 PSI_MACRO($1, $2, [
96 ifelse([$3], vararg, [
97 AC_MSG_ERROR(varargs macro support is not implemented)
98 ],[
99 psi_add_decl "$psi_decl_args"
100 ])
101 ])
102 ])
103 ])