buildtools: fix build for some mktemp
[dpdk.git] / buildtools / auto-config-h.sh
1 #!/bin/sh
2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright 2014-2015 6WIND S.A.
4 #
5 # Crude script to detect whether particular types, macros and functions are
6 # defined by trying to compile a file with a given header. Can be used to
7 # perform cross-platform checks since the resulting object file is not
8 # executed.
9 #
10 # Set VERBOSE=1 in the environment to display compiler output and errors.
11 #
12 # CC, CPPFLAGS, CFLAGS, EXTRA_CPPFLAGS and EXTRA_CFLAGS are taken from the
13 # environment.
14 #
15 # AUTO_CONFIG_CFLAGS may append additional CFLAGS without modifying the
16 # above variables.
17
18 file=${1:?output file name required (config.h)}
19 macro=${2:?output macro name required (HAVE_*)}
20 include=${3:?include name required (foo.h)}
21 type=${4:?object type required (define, enum, type, field, func)}
22 name=${5:?define/type/function name required}
23
24 : ${CC:=cc}
25
26 temp=$(mktemp -t dpdk.${0##*/}.c.XXXXXX)
27
28 case $type in
29 define)
30         code="\
31 #ifndef $name
32 #error $name not defined
33 #endif
34 "
35         ;;
36 enum)
37         code="\
38 long test____ = $name;
39 "
40         ;;
41 type)
42         code="\
43 $name test____;
44 "
45         ;;
46 field)
47         code="\
48 void test____(void)
49 {
50         ${name%%.*} test_____;
51
52         (void)test_____.${name#*.};
53 }
54 "
55         ;;
56 func)
57         code="\
58 void (*test____)() = (void (*)())$name;
59 "
60         ;;
61 *)
62         unset error
63         : ${error:?unknown object type \"$type\"}
64         exit
65 esac
66
67 if [ "${VERBOSE}" = 1 ]
68 then
69         err=2
70         out=1
71         eol='
72 '
73 else
74         exec 3> /dev/null ||
75         exit
76         err=3
77         out=3
78         eol=' '
79 fi &&
80 printf 'Looking for %s %s in %s.%s' \
81         "${name}" "${type}" "${include}" "${eol}" &&
82 printf "\
83 #include <%s>
84
85 %s
86 " "$include" "$code" > "${temp}" &&
87 if ${CC} ${CPPFLAGS} ${EXTRA_CPPFLAGS} ${CFLAGS} ${EXTRA_CFLAGS} \
88         ${AUTO_CONFIG_CFLAGS} \
89         -xc -c -o ${temp}.o "${temp}" 1>&${out} 2>&${err}
90 then
91         rm -f "${temp}" "${temp}.o"
92         printf "\
93 #ifndef %s
94 #define %s 1
95 #endif /* %s */
96
97 " "${macro}" "${macro}" "${macro}" >> "${file}" &&
98         printf 'Defining %s.\n' "${macro}"
99 else
100         rm -f "${temp}" "${temp}.o"
101         printf "\
102 /* %s is not defined. */
103
104 " "${macro}" >> "${file}" &&
105         printf 'Not defining %s.\n' "${macro}"
106 fi
107
108 exit