1#
2#  Copyright 2021 Northern.tech AS
3#
4#  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5#
6#  This program is free software; you can redistribute it and/or modify it
7#  under the terms of the GNU General Public License as published by the
8#  Free Software Foundation; version 3.
9#
10#  This program is distributed in the hope that it will be useful,
11#  but WITHOUT ANY WARRANTY; without even the implied warranty of
12#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#  GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18#
19# To the extent this program is licensed as part of the Enterprise
20# versions of CFEngine, the applicable Commercial Open Source License
21# (COSL) may apply to this file if you as a licensee so wish it. See
22# included file COSL.txt.
23#
24dnl
25dnl Arguments:
26dnl  $1 - function name
27dnl  $2 - headers (to compile $3)
28dnl  $3 - body for compilation
29dnl  $4 - function invocation
30dnl
31dnl This macro checks that the function (argument 1) is defined,
32dnl and that the code piece (arguments 2, 3, like in AC_LANG_PROGRAM) can be
33dnl compiled.
34dnl
35dnl If the code compiles successfully, it defines HAVE_$1_PROPER macro.
36dnl
37dnl If the code fails, it adds '$4' to $post_macros variable.
38dnl If you want rpl_$1.c to be compiled as a replacement, call
39dnl CF3_REPLACE_PROPER_FUNC with the same function name.
40dnl
41dnl  ** How to use **
42dnl
43dnl  CF3_CHECK_PROPER_FUNC(function, [#include <stdio.h>], [void function(FILE *);], [#define function rpl_function])
44dnl  CF3_REPLACE_PROPER_FUNC(function)
45dnl
46dnl  Then in libutils/platform.h:
47dnl
48dnl    #if !HAVE_FUNCTION_PROPER
49dnl    void rpl_function(FILE *);
50dnl    #endif
51dnl
52dnl  And libcompat/rpl_function.c:
53dnl
54dnl    #include "platform.h"
55dnl
56dnl    void rpl_function(FILE *) { ... }
57dnl
58
59AC_DEFUN([CF3_CHECK_PROPER_FUNC],
60[
61  AC_CHECK_FUNC([$1], [], [AC_MSG_ERROR([Unable to find function $1])])
62
63  AC_CACHE_CHECK([whether $1 is properly declared],
64    [hw_cv_func_$1_proper],
65    [AC_COMPILE_IFELSE(
66      [AC_LANG_PROGRAM([$2],[$3])],
67      [hw_cv_func_$1_proper=yes],
68      [hw_cv_func_$1_proper=no])])
69
70  AC_SUBST([hw_cv_func_$1_proper])
71
72  AS_IF([test "$hw_cv_func_$1_proper" = yes],
73    [AC_DEFINE([HAVE_$1_PROPER], [1], [Define to 1 if you have properly defined `$1' function])],
74    [post_macros="$post_macros
75$4"])
76])
77
78AC_DEFUN([CF3_REPLACE_PROPER_FUNC],
79[
80  AS_IF([test "$hw_cv_func_$1_proper" = "no"],
81    [AC_LIBOBJ(rpl_$1)]
82  )
83])
84