Base port to Windows (#2810)
[collectd.git] / build.sh
1 #!/bin/sh
2
3 GLOBAL_ERROR_INDICATOR=0
4
5 check_for_application()
6 {
7     for PROG in "$@"
8     do
9         which "$PROG" >/dev/null 2>&1
10         if test $? -ne 0; then
11             cat >&2 <<EOF
12 WARNING: \`$PROG' not found!
13     Please make sure that \`$PROG' is installed and is in one of the
14     directories listed in the PATH environment variable.
15 EOF
16             GLOBAL_ERROR_INDICATOR=1
17         fi
18     done
19 }
20
21 setup_libtool()
22 {
23     libtoolize=""
24     libtoolize --version >/dev/null 2>/dev/null
25     if test $? -eq 0; then
26         libtoolize=libtoolize
27     else
28         glibtoolize --version >/dev/null 2>/dev/null
29         if test $? -eq 0; then
30             libtoolize=glibtoolize
31         else
32             cat >&2 <<EOF
33 WARNING: Neither \`libtoolize' nor \`glibtoolize' have been found!
34     Please make sure that one of them is installed and is in one of the
35     directories listed in the PATH environment variable.
36 EOF
37             GLOBAL_ERROR_INDICATOR=1
38         fi
39     fi
40
41     if test "$GLOBAL_ERROR_INDICATOR" != "0"; then
42         exit 1
43     fi
44 }
45
46 build()
47 {
48     echo "Building..."
49     check_for_application lex bison autoheader aclocal automake autoconf pkg-config
50     setup_libtool
51
52     set -x
53     autoheader \
54     && aclocal -I m4 \
55     && $libtoolize --copy --force \
56     && automake --add-missing --copy \
57     && autoconf
58 }
59
60 build_cygwin()
61 {
62     echo "Building for Cygwin..."
63     check_for_application aclocal autoconf autoheader automake bison flex git make pkg-config x86_64-w64-mingw32-gcc
64     setup_libtool
65
66     set -e
67
68     : ${INSTALL_DIR:="C:/PROGRA~1/collectd"}
69     : ${LIBDIR:="${INSTALL_DIR}"}
70     : ${BINDIR:="${INSTALL_DIR}"}
71     : ${SBINDIR:="${INSTALL_DIR}"}
72     : ${SYSCONFDIR:="${INSTALL_DIR}"}
73     : ${LOCALSTATEDIR:="${INSTALL_DIR}"}
74     : ${DATAROOTDIR:="${INSTALL_DIR}"}
75     : ${DATADIR:="${INSTALL_DIR}"}
76
77     echo "Installing collectd to ${INSTALL_DIR}."
78     TOP_SRCDIR=$(pwd)
79     MINGW_ROOT="$(x86_64-w64-mingw32-gcc -print-sysroot)/mingw"
80     GNULIB_DIR="${TOP_SRCDIR}/gnulib/build/gllib"
81
82     export CC="x86_64-w64-mingw32-gcc"
83
84     if [ -d "${TOP_SRCDIR}/gnulib/build" ]; then
85         echo "Assuming that gnulib is already built, because gnulib/build exists."
86     else
87         git submodule init
88         git submodule update
89         cd gnulib
90         ./gnulib-tool \
91           --create-testdir \
92           --source-base=lib \
93           --dir=${TOP_SRCDIR}/gnulib/build \
94           canonicalize-lgpl \
95           fcntl-h \
96           getsockopt \
97           gettimeofday \
98           nanosleep \
99           netdb \
100           net_if \
101           poll \
102           recv \
103           regex \
104           sendto \
105           setlocale \
106           strtok_r \
107           sys_resource \
108           sys_socket \
109           sys_stat \
110           sys_wait \
111           time_r
112
113         cd ${TOP_SRCDIR}/gnulib/build
114         ./configure --host="mingw32" LIBS="-lws2_32 -lpthread"
115         make 
116         cd gllib
117
118         # We have to rebuild libgnu.a to get the list of *.o files to build a dll later
119         rm libgnu.a
120         OBJECT_LIST=`make V=1 | grep "ar" | cut -d' ' -f4-`
121         $CC -shared -o libgnu.dll $OBJECT_LIST -lws2_32 -lpthread
122         rm libgnu.a # get rid of it, to use libgnu.dll
123         fi
124     cd "${TOP_SRCDIR}"
125
126     set -x
127     autoreconf --install
128
129     export LDFLAGS="-L${GNULIB_DIR}"
130     export LIBS="-lgnu"
131     export CFLAGS="-Drestrict=__restrict -I${GNULIB_DIR}"
132
133     ./configure \
134       --prefix="${INSTALL_DIR}" \
135       --libdir="${LIBDIR}" \
136       --bindir="${BINDIR}" \
137       --sbindir="${SBINDIR}" \
138       --sysconfdir="${SYSCONFDIR}" \
139       --localstatedir="${LOCALSTATEDIR}" \
140       --datarootdir="${DATAROOTDIR}" \
141       --datarootdir="${DATADIR}" \
142       --disable-all-plugins \
143       --host="mingw32" \
144       --enable-logfile
145
146     cp ${GNULIB_DIR}/../config.h src/gnulib_config.h
147     echo "#include <config.h.in>" >> src/gnulib_config.h
148
149     cp libtool libtool_bak
150     sed -i "s%\$LTCC \$LTCFLAGS\(.*cwrapper.*\)%\$LTCC \1%" libtool
151
152     make
153     make install
154
155     cp "${GNULIB_DIR}/libgnu.dll" "${INSTALL_DIR}"
156     cp "${MINGW_ROOT}/bin/zlib1.dll" "${INSTALL_DIR}"
157     cp "${MINGW_ROOT}/bin/libwinpthread-1.dll" "${INSTALL_DIR}"
158     cp "${MINGW_ROOT}/bin/libdl.dll" "${INSTALL_DIR}"
159
160     echo "Done."
161 }
162
163 os_name="$(uname)"
164 if test "${os_name#CYGWIN}" != "$os_name"; then
165     build_cygwin
166 else
167     build
168 fi
169