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