From 1fd85febee3cbbcc1a49d29740538f5761fbd52c Mon Sep 17 00:00:00 2001 From: Nate Bargmann Date: Mon, 29 Jul 2013 12:38:22 -0500 Subject: [PATCH] MinGW build: Fix sleep() substitution MinGW does not natively support the POSIX sleep() function so we have had an override that was a part of the GR_PWIN32 macro and included in the generated config.h file. When compiling for Windows on POSIX using MinGW, Autotools will detect sleep() and set HAVE_SLEEP which prevented the substitution from being included in the source. Adding a test for _WIN32 (set by MinGW's gcc) then caused a warning from src/network.c on POSIX about winsock2.h needing to be included before windows.h. As config.h needed to be included first, the solution to break out the substitution that includes windows.h into its own file. This patch provides that solution and allows the code to compile cleanly on POSIX, using MinGW on both POSIX and Windows, and on Cygwin. --- adat/adat.c | 14 ++++++++++++++ include/Makefile.am | 2 +- include/hl_sleep.h | 37 +++++++++++++++++++++++++++++++++++++ macros/gr_pwin32.m4 | 13 ++----------- tentec/paragon.c | 14 ++++++++++++++ tentec/tt550.c | 20 +++++++++++++++++--- tests/testrig.c | 15 +++++++++++++++ tests/testtrn.c | 15 +++++++++++++++ yaesu/ft757gx.c | 16 +++++++++++++++- 9 files changed, 130 insertions(+), 16 deletions(-) create mode 100644 include/hl_sleep.h diff --git a/adat/adat.c b/adat/adat.c index 5c40b4406..cab58b3b5 100644 --- a/adat/adat.c +++ b/adat/adat.c @@ -49,6 +49,20 @@ #include "register.h" #include "num_stdio.h" +/* HAVE_SSLEEP is defined when Windows Sleep is found + * HAVE_SLEEP is defined when POSIX sleep is found + * _WIN32 is defined when compiling with MinGW + * + * When cross-compiling from POSIX to Windows using MinGW, HAVE_SLEEP + * will often be defined by configure although it is not supported by + * MinGW. So substitute the sleep definition below in such a case and + * when compiling on Windows using MinGW where HAVE_SLEEP will be + * undefined. + */ +#if defined(HAVE_SSLEEP) && (!defined(HAVE_SLEEP) || defined(_WIN32)) +#include "hl_sleep.h" +#endif + // --------------------------------------------------------------------------- // ADAT INCLUDES // --------------------------------------------------------------------------- diff --git a/include/Makefile.am b/include/Makefile.am index 02510616b..92226ffc1 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,4 +1,4 @@ -noinst_HEADERS = config.h bandplan.h num_stdio.h +noinst_HEADERS = config.h bandplan.h num_stdio.h hl_sleep.h nobase_include_HEADERS = hamlib/rig.h hamlib/riglist.h hamlib/rig_dll.h \ hamlib/rotator.h hamlib/rotlist.h hamlib/rigclass.h hamlib/rotclass.h diff --git a/include/hl_sleep.h b/include/hl_sleep.h new file mode 100644 index 000000000..6aeea3f69 --- /dev/null +++ b/include/hl_sleep.h @@ -0,0 +1,37 @@ +/* + * Hamlib Interface - Replacement sleep() declaration for Windows + * Copyright (C) 2013 The Hamlib Group + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +/* Define missing sleep() prototype */ +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef HAVE_WINBASE_H +#include +#include +#endif + +/* TODO: what about SleepEx? */ +static inline unsigned int sleep(unsigned int nb_sec) { Sleep(nb_sec*1000); return 0; } + +#ifdef __cplusplus +} +#endif diff --git a/macros/gr_pwin32.m4 b/macros/gr_pwin32.m4 index eda01c517..6cae28347 100644 --- a/macros/gr_pwin32.m4 +++ b/macros/gr_pwin32.m4 @@ -56,7 +56,7 @@ AC_LINK_IFELSE([AC_LANG_PROGRAM([ #include #include ], [ Sleep(0); ])], - [AC_DEFINE(HAVE_SSLEEP,1,[Define to 1 if you have win32 Sleep]) + [AC_DEFINE(HAVE_SSLEEP,1,[Define to 1 if you have Windows Sleep]) AC_MSG_RESULT(yes)], AC_MSG_RESULT(no) ) @@ -73,6 +73,7 @@ int getopt (int argc, char * const argv[], const char * optstring); extern char * optarg; extern int optind, opterr, optopt; #endif + #ifndef HAVE_GETOPT_LONG struct option; int getopt_long (int argc, char * const argv[], const char * optstring, @@ -83,16 +84,6 @@ int getopt_long (int argc, char * const argv[], const char * optstring, int usleep(unsigned long usec); /* SUSv2 */ #endif -#if defined(HAVE_SSLEEP) && !defined(HAVE_SLEEP) -#ifdef HAVE_WINBASE_H -#include -#include -#endif -/* TODO: what about SleepEx? */ -/* static inline unsigned int sleep (unsigned int nb_sec) { Sleep(nb_sec*1000); return 0; } */ -#define sleep(seconds) Sleep((seconds)*1000) -#endif - #ifndef HAVE_GETTIMEOFDAY #ifdef HAVE_SYS_TIME_H #include diff --git a/tentec/paragon.c b/tentec/paragon.c index 1fe24eec9..cf351f129 100644 --- a/tentec/paragon.c +++ b/tentec/paragon.c @@ -36,6 +36,20 @@ #include "misc.h" #include "num_stdio.h" +/* HAVE_SSLEEP is defined when Windows Sleep is found + * HAVE_SLEEP is defined when POSIX sleep is found + * _WIN32 is defined when compiling with MinGW + * + * When cross-compiling from POSIX to Windows using MinGW, HAVE_SLEEP + * will often be defined by configure although it is not supported by + * MinGW. So substitute the sleep definition below in such a case and + * when compiling on Windows using MinGW where HAVE_SLEEP will be + * undefined. + */ +#if defined(HAVE_SSLEEP) && (!defined(HAVE_SLEEP) || defined(_WIN32)) +#include "hl_sleep.h" +#endif + struct tt585_priv_data { unsigned char status_data[30]; struct timeval status_tv; diff --git a/tentec/tt550.c b/tentec/tt550.c index 0bfc41e9d..7140023ae 100644 --- a/tentec/tt550.c +++ b/tentec/tt550.c @@ -33,12 +33,26 @@ #include #include -#include -#include -#include +#include "serial.h" +#include "misc.h" +#include "cal.h" #include "tt550.h" +/* HAVE_SSLEEP is defined when Windows Sleep is found + * HAVE_SLEEP is defined when POSIX sleep is found + * _WIN32 is defined when compiling with MinGW + * + * When cross-compiling from POSIX to Windows using MinGW, HAVE_SLEEP + * will often be defined by configure although it is not supported by + * MinGW. So substitute the sleep definition below in such a case and + * when compiling on Windows using MinGW where HAVE_SLEEP will be + * undefined. + */ +#if defined(HAVE_SSLEEP) && (!defined(HAVE_SLEEP) || defined(_WIN32)) +#include "hl_sleep.h" +#endif + /* * Filter table for 550 reciver support diff --git a/tests/testrig.c b/tests/testrig.c index 4d8ba6bd1..0c266c7c9 100644 --- a/tests/testrig.c +++ b/tests/testrig.c @@ -8,6 +8,21 @@ #include #include +/* HAVE_SSLEEP is defined when Windows Sleep is found + * HAVE_SLEEP is defined when POSIX sleep is found + * _WIN32 is defined when compiling with MinGW + * + * When cross-compiling from POSIX to Windows using MinGW, HAVE_SLEEP + * will often be defined by configure although it is not supported by + * MinGW. So substitute the sleep definition below in such a case and + * when compiling on Windows using MinGW where HAVE_SLEEP will be + * undefined. + */ +#if defined(HAVE_SSLEEP) && (!defined(HAVE_SLEEP) || defined(_WIN32)) +#include "hl_sleep.h" +#endif + + #define SERIAL_PORT "/dev/ttyS0" int main (int argc, char *argv[]) diff --git a/tests/testtrn.c b/tests/testtrn.c index 51291bba4..c056e6a53 100644 --- a/tests/testtrn.c +++ b/tests/testtrn.c @@ -8,6 +8,21 @@ #include #include +/* HAVE_SSLEEP is defined when Windows Sleep is found + * HAVE_SLEEP is defined when POSIX sleep is found + * _WIN32 is defined when compiling with MinGW + * + * When cross-compiling from POSIX to Windows using MinGW, HAVE_SLEEP + * will often be defined by configure although it is not supported by + * MinGW. So substitute the sleep definition below in such a case and + * when compiling on Windows using MinGW where HAVE_SLEEP will be + * undefined. + */ +#if defined(HAVE_SSLEEP) && (!defined(HAVE_SLEEP) || defined(_WIN32)) +#include "hl_sleep.h" +#endif + + #define SERIAL_PORT "/dev/ttyS0" int myfreq_event(RIG *rig, vfo_t vfo, freq_t freq, rig_ptr_t arg) diff --git a/yaesu/ft757gx.c b/yaesu/ft757gx.c index 641549a89..fde380dab 100644 --- a/yaesu/ft757gx.c +++ b/yaesu/ft757gx.c @@ -43,12 +43,26 @@ #include /* String function definitions */ #include /* UNIX standard function definitions */ -#include "hamlib/rig.h" +#include #include "serial.h" #include "misc.h" #include "yaesu.h" #include "ft757gx.h" +/* HAVE_SSLEEP is defined when Windows Sleep is found + * HAVE_SLEEP is defined when POSIX sleep is found + * _WIN32 is defined when compiling with MinGW + * + * When cross-compiling from POSIX to Windows using MinGW, HAVE_SLEEP + * will often be defined by configure although it is not supported by + * MinGW. So substitute the sleep definition below in such a case and + * when compiling on Windows using MinGW where HAVE_SLEEP will be + * undefined. + */ +#if defined(HAVE_SSLEEP) && (!defined(HAVE_SLEEP) || defined(_WIN32)) +#include "hl_sleep.h" +#endif + /* Private helper function prototypes */ static int ft757_get_update_data(RIG *rig);