Skip to content

Commit

Permalink
Base port to Windows (collectd#2810)
Browse files Browse the repository at this point in the history
* Update configure.ac / Makefile.am to build for Windows using Cygwin.
* Update build.sh to build for Windows.
* Base port of the daemon.
* Include gnulib as a submodule.
  • Loading branch information
SeanCampbell authored and tokkee committed Jul 27, 2018
1 parent 553925e commit df13592
Show file tree
Hide file tree
Showing 23 changed files with 469 additions and 108 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "gnulib"]
path = gnulib
url = git://git.savannah.gnu.org/gnulib.git
53 changes: 49 additions & 4 deletions Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
ACLOCAL_AMFLAGS = -I m4
AM_YFLAGS = -d

if BUILD_WIN32
pkgdatadir=$(datadir)
pkglibdir=$(libdir)/plugins
pkglocalstatedir=${localstatedir}
else
pkglocalstatedir=${localstatedir}/lib/${PACKAGE_NAME}
endif

BUILT_SOURCES = \
src/libcollectdclient/collectd/lcc_features.h \
Expand Down Expand Up @@ -99,7 +106,13 @@ pkginclude_HEADERS = \

lib_LTLIBRARIES = libcollectdclient.la

if BUILD_WIN32
# TODO: Build all executables on Windows as well.
sbin_PROGRAMS = \
collectd

bin_PROGRAMS =
else
sbin_PROGRAMS = \
collectd \
collectdmon
Expand All @@ -109,9 +122,10 @@ bin_PROGRAMS = \
collectd-nagios \
collectd-tg \
collectdctl
endif # BUILD_WIN32


noinst_LTLIBRARIES = \
EXTRA_LTLIBRARIES = \
libavltree.la \
libcmds.la \
libcommon.la \
Expand Down Expand Up @@ -160,20 +174,36 @@ PLUGIN_LDFLAGS = \
-module \
-avoid-version \
-export-symbols-regex '\<module_register\>'
if BUILD_WIN32
PLUGIN_LDFLAGS += -shared -no-undefined -lcollectd -L.
endif


AM_CPPFLAGS = \
-I$(top_srcdir)/src -I$(top_srcdir)/src/daemon \
-DPREFIX='"${prefix}"' \
-DCONFIGFILE='"${sysconfdir}/${PACKAGE_NAME}.conf"' \
-DLOCALSTATEDIR='"${localstatedir}"' \
-DPKGLOCALSTATEDIR='"${localstatedir}/lib/${PACKAGE_NAME}"' \
-DPKGLOCALSTATEDIR='"${pkglocalstatedir}"' \
-DPLUGINDIR='"${pkglibdir}"' \
-DPKGDATADIR='"${pkgdatadir}"'
if BUILD_WIN32
AM_CPPFLAGS += -DNOGDI
endif

COMMON_DEPS =
if BUILD_WIN32
COMMON_DEPS += collectd.exe
endif

# Link to these libraries..
COMMON_LIBS = $(PTHREAD_LIBS)
if BUILD_WIN32
COMMON_LIBS += -lws2_32
endif
if BUILD_WITH_GNULIB
COMMON_LIBS += -lgnu
endif
if BUILD_WITH_CAPABILITY
COMMON_LIBS += -lcap
endif
Expand All @@ -195,7 +225,6 @@ endif


collectd_SOURCES = \
src/daemon/cmd.c \
src/daemon/cmd.h \
src/daemon/collectd.c \
src/daemon/collectd.h \
Expand Down Expand Up @@ -239,6 +268,13 @@ collectd_LDADD = \
$(COMMON_LIBS) \
$(DLOPEN_LIBS)

if BUILD_WIN32
collectd_SOURCES += src/daemon/cmd_windows.c
collectd_LDFLAGS += -ldl -Wl,--out-implib,libcollectd.a
else
collectd_SOURCES += src/daemon/cmd.c
endif

if BUILD_FEATURE_DAEMON
collectd_CPPFLAGS += -DPIDFILE='"${localstatedir}/run/${PACKAGE_NAME}.pid"'
endif
Expand All @@ -250,6 +286,9 @@ collectd_CFLAGS += $(BUILD_WITH_LIBSTATGRAB_CFLAGS)
collectd_LDADD += $(BUILD_WITH_LIBSTATGRAB_LDFLAGS)
endif

if BUILD_WIN32
collectd_LDFLAGS += -Wl,--out-implib,libcollectd.a
endif

collectdmon_SOURCES = src/collectdmon.c

Expand Down Expand Up @@ -501,6 +540,10 @@ libcollectdclient_la_CPPFLAGS = \
-I$(srcdir)/src/daemon
libcollectdclient_la_LDFLAGS = -version-info 2:0:1
libcollectdclient_la_LIBADD = -lm
if BUILD_WIN32
libcollectdclient_la_LDFLAGS += -shared -no-undefined
libcollectdclient_la_LIBADD += -lgnu -lws2_32 -liphlpapi
endif
if BUILD_WITH_LIBGCRYPT
libcollectdclient_la_CPPFLAGS += $(GCRYPT_CPPFLAGS)
libcollectdclient_la_LDFLAGS += $(GCRYPT_LDFLAGS)
Expand Down Expand Up @@ -1042,6 +1085,7 @@ if BUILD_PLUGIN_LOGFILE
pkglib_LTLIBRARIES += logfile.la
logfile_la_SOURCES = src/logfile.c
logfile_la_LDFLAGS = $(PLUGIN_LDFLAGS)
logfile_la_DEPENDENCIES = $(COMMON_DEPS)
endif

if BUILD_PLUGIN_LOG_LOGSTASH
Expand Down Expand Up @@ -1471,7 +1515,7 @@ python_la_LDFLAGS = $(PLUGIN_LDFLAGS) $(LIBPYTHON_LDFLAGS)
endif

if HAVE_LIBMNL
noinst_LTLIBRARIES += libtaskstats.la
EXTRA_LTLIBRARIES += libtaskstats.la
libtaskstats_la_SOURCES = \
src/utils_taskstats.c \
src/utils_taskstats.h
Expand Down Expand Up @@ -2138,3 +2182,4 @@ generic-jmx.jar: $(JAVA_TIMESTAMP_FILE)

jar_DATA = collectd-api.jar generic-jmx.jar
endif

31 changes: 31 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,37 @@ To generate the `configure` script, you'll need the following dependencies:
The `build.sh' script takes no arguments.


Building on Windows
-----------------------------------------------

Collectd can be built on Windows using Cygwin, and the result is a binary that
runs natively on Windows. That is, Cygwin is only needed for building, not running,
collectd.

You will need to install the following Cygwin packages:
- automake
- bison
- flex
- git
- libtool
- make
- mingw64-x86_64-dlfcn
- mingw64-x86_64-gcc-core
- mingw64-x86_64-zlib
- pkg-config

To build, just run the `build.sh' script in your Cygwin terminal. By default, it installs
to "C:/Program Files/collectd". You can change the location by setting the INSTALL_DIR
variable:

$ export INSTALL_DIR="C:/some/other/install/directory"
$ ./build.sh

or:

$ INSTALL_DIR="C:/some/other/install/directory" ./build.sh


Crosscompiling
--------------

Expand Down
160 changes: 139 additions & 21 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,152 @@ EOF
done
}

check_for_application lex bison autoheader aclocal automake autoconf pkg-config

libtoolize=""
libtoolize --version >/dev/null 2>/dev/null
if test $? -eq 0; then
libtoolize=libtoolize
else
glibtoolize --version >/dev/null 2>/dev/null
setup_libtool()
{
libtoolize=""
libtoolize --version >/dev/null 2>/dev/null
if test $? -eq 0; then
libtoolize=glibtoolize
libtoolize=libtoolize
else
cat >&2 <<EOF
glibtoolize --version >/dev/null 2>/dev/null
if test $? -eq 0; then
libtoolize=glibtoolize
else
cat >&2 <<EOF
WARNING: Neither \`libtoolize' nor \`glibtoolize' have been found!
Please make sure that one of them is installed and is in one of the
directories listed in the PATH environment variable.
EOF
GLOBAL_ERROR_INDICATOR=1
GLOBAL_ERROR_INDICATOR=1
fi
fi
fi

if test "$GLOBAL_ERROR_INDICATOR" != "0"; then
exit 1
fi
if test "$GLOBAL_ERROR_INDICATOR" != "0"; then
exit 1
fi
}

build()
{
echo "Building..."
check_for_application lex bison autoheader aclocal automake autoconf pkg-config
setup_libtool

set -x
autoheader \
&& aclocal -I m4 \
&& $libtoolize --copy --force \
&& automake --add-missing --copy \
&& autoconf
}

build_cygwin()
{
echo "Building for Cygwin..."
check_for_application aclocal autoconf autoheader automake bison flex git make pkg-config x86_64-w64-mingw32-gcc
setup_libtool

set -e

: ${INSTALL_DIR:="C:/PROGRA~1/collectd"}
: ${LIBDIR:="${INSTALL_DIR}"}
: ${BINDIR:="${INSTALL_DIR}"}
: ${SBINDIR:="${INSTALL_DIR}"}
: ${SYSCONFDIR:="${INSTALL_DIR}"}
: ${LOCALSTATEDIR:="${INSTALL_DIR}"}
: ${DATAROOTDIR:="${INSTALL_DIR}"}
: ${DATADIR:="${INSTALL_DIR}"}

set -x
echo "Installing collectd to ${INSTALL_DIR}."
TOP_SRCDIR=$(pwd)
MINGW_ROOT="$(x86_64-w64-mingw32-gcc -print-sysroot)/mingw"
GNULIB_DIR="${TOP_SRCDIR}/gnulib/build/gllib"

export CC="x86_64-w64-mingw32-gcc"

if [ -d "${TOP_SRCDIR}/gnulib/build" ]; then
echo "Assuming that gnulib is already built, because gnulib/build exists."
else
git submodule init
git submodule update
cd gnulib
./gnulib-tool \
--create-testdir \
--source-base=lib \
--dir=${TOP_SRCDIR}/gnulib/build \
canonicalize-lgpl \
fcntl-h \
getsockopt \
gettimeofday \
nanosleep \
netdb \
net_if \
poll \
recv \
regex \
sendto \
setlocale \
strtok_r \
sys_resource \
sys_socket \
sys_stat \
sys_wait \
time_r

cd ${TOP_SRCDIR}/gnulib/build
./configure --host="mingw32" LIBS="-lws2_32 -lpthread"
make
cd gllib

# We have to rebuild libgnu.a to get the list of *.o files to build a dll later
rm libgnu.a
OBJECT_LIST=`make V=1 | grep "ar" | cut -d' ' -f4-`
$CC -shared -o libgnu.dll $OBJECT_LIST -lws2_32 -lpthread
rm libgnu.a # get rid of it, to use libgnu.dll
fi
cd "${TOP_SRCDIR}"

set -x
autoreconf --install

export LDFLAGS="-L${GNULIB_DIR}"
export LIBS="-lgnu"
export CFLAGS="-Drestrict=__restrict -I${GNULIB_DIR}"

./configure \
--prefix="${INSTALL_DIR}" \
--libdir="${LIBDIR}" \
--bindir="${BINDIR}" \
--sbindir="${SBINDIR}" \
--sysconfdir="${SYSCONFDIR}" \
--localstatedir="${LOCALSTATEDIR}" \
--datarootdir="${DATAROOTDIR}" \
--datarootdir="${DATADIR}" \
--disable-all-plugins \
--host="mingw32" \
--enable-logfile

cp ${GNULIB_DIR}/../config.h src/gnulib_config.h
echo "#include <config.h.in>" >> src/gnulib_config.h

cp libtool libtool_bak
sed -i "s%\$LTCC \$LTCFLAGS\(.*cwrapper.*\)%\$LTCC \1%" libtool

make
make install

cp "${GNULIB_DIR}/libgnu.dll" "${INSTALL_DIR}"
cp "${MINGW_ROOT}/bin/zlib1.dll" "${INSTALL_DIR}"
cp "${MINGW_ROOT}/bin/libwinpthread-1.dll" "${INSTALL_DIR}"
cp "${MINGW_ROOT}/bin/libdl.dll" "${INSTALL_DIR}"

echo "Done."
}

os_name="$(uname)"
if test "${os_name#CYGWIN}" != "$os_name"; then
build_cygwin
else
build
fi

autoheader \
&& aclocal -I m4 \
&& $libtoolize --copy --force \
&& automake --add-missing --copy \
&& autoconf
Loading

0 comments on commit df13592

Please sign in to comment.