From 4df8a8de850da14aaadad596709bf9b2506748ea Mon Sep 17 00:00:00 2001 From: "A. Jesse Jiryu Davis" Date: Sat, 14 Nov 2015 22:36:12 -0500 Subject: [PATCH] CDRIVER-995 config check for _set_output_format, timespec --- CMakeLists.txt | 27 ++++++++++++++++++--------- build/autotools/FindDependencies.m4 | 8 ++++++++ src/bson/bson-config.h.in | 17 +++++++++++++++++ src/bson/bson.c | 9 ++------- tests/TestSuite.c | 2 +- 5 files changed, 46 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 70c2f35d..2bb4a5ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,8 @@ project (libbson) include(CheckFunctionExists) include(CheckIncludeFile) +include(CheckStructHasMember) +include(CheckSymbolExists) include(TestBigEndian) include(InstallRequiredSystemLibraries) @@ -34,14 +36,25 @@ if (RT_LIBRARY) set(CMAKE_REQUIRED_LIBRARIES ${RT_LIBRARY}) endif() +# See https://public.kitware.com/Bug/view.php?id=15659 +CHECK_SYMBOL_EXISTS(snprintf stdio.h BSON_HAVE_SNPRINTF) +if (NOT BSON_HAVE_SNPRINTF) + set(BSON_HAVE_SNPRINTF 0) +endif () + +CHECK_FUNCTION_EXISTS(_set_output_format BSON_NEEDS_SET_OUTPUT_FORMAT) +if (NOT BSON_NEEDS_SET_OUTPUT_FORMAT) + set(BSON_NEEDS_SET_OUTPUT_FORMAT 0) +endif () + +CHECK_STRUCT_HAS_MEMBER("struct timespec" tv_sec time.h BSON_HAVE_TIMESPEC) +if (NOT BSON_HAVE_TIMESPEC) + set(BSON_HAVE_TIMESPEC 0) +endif () + if (MSVC) set (BSON_OS 2) set (BSON_HAVE_CLOCK_GETTIME 0) - if (MSVC14) - set (BSON_HAVE_SNPRINTF 1) - else() - set (BSON_HAVE_SNPRINTF 0) - endif() set (BSON_HAVE_STDBOOL_H 0) set (BSON_HAVE_STRNLEN 0) set (BSON_EXTRA_ALIGN 1) @@ -57,10 +70,6 @@ else () if (NOT BSON_HAVE_STRNLEN) set(BSON_HAVE_STRNLEN 0) endif () - CHECK_FUNCTION_EXISTS(snprintf BSON_HAVE_SNPRINTF) - if (NOT BSON_HAVE_SNPRINTF) - set(BSON_HAVE_SNPRINTF 0) - endif () CHECK_INCLUDE_FILE(stdbool.h BSON_HAVE_STDBOOL_H) if (NOT BSON_HAVE_STDBOOL_H) set(BSON_HAVE_STDBOOL_H 0) diff --git a/build/autotools/FindDependencies.m4 b/build/autotools/FindDependencies.m4 index 05dc514f..680796a6 100644 --- a/build/autotools/FindDependencies.m4 +++ b/build/autotools/FindDependencies.m4 @@ -6,6 +6,14 @@ AC_CHECK_FUNC(strnlen, [AC_SUBST(BSON_HAVE_STRNLEN, 1)]) AC_SUBST(BSON_HAVE_SNPRINTF, 0) AC_CHECK_FUNC(snprintf, [AC_SUBST(BSON_HAVE_SNPRINTF, 1)]) +# Check for _set_output_format (unlikely, only Visual Studio 2013 and older) +AC_SUBST(BSON_NEEDS_SET_OUTPUT_FORMAT, 0) +AC_CHECK_FUNCS(_set_output_format, [AC_SUBST(BSON_NEEDS_SET_OUTPUT_FORMAT, 1)]) + +# Check for struct timespec +AC_SUBST(BSON_HAVE_TIMESPEC, 0) +AC_CHECK_TYPES([struct timespec], [AC_SUBST(BSON_HAVE_TIMESPEC, 1)]) + # Check for clock_gettime and if it needs -lrt AC_SUBST(BSON_HAVE_CLOCK_GETTIME, 0) AC_SEARCH_LIBS([clock_gettime], [rt], [AC_SUBST(BSON_HAVE_CLOCK_GETTIME, 1)]) diff --git a/src/bson/bson-config.h.in b/src/bson/bson-config.h.in index 12efb0ab..82bab2c7 100644 --- a/src/bson/bson-config.h.in +++ b/src/bson/bson-config.h.in @@ -79,6 +79,23 @@ #endif +/* + * Define to 1 if you have _set_output_format (VS2013 and older). + */ +#define BSON_NEEDS_SET_OUTPUT_FORMAT @BSON_NEEDS_SET_OUTPUT_FORMAT@ +#if BSON_NEEDS_SET_OUTPUT_FORMAT != 1 +# undef BSON_NEEDS_SET_OUTPUT_FORMAT +#endif + +/* + * Define to 1 if you have struct timespec available on your platform. + */ +#define BSON_HAVE_TIMESPEC @BSON_HAVE_TIMESPEC@ +#if BSON_HAVE_TIMESPEC != 1 +# undef BSON_HAVE_TIMESPEC +#endif + + /* * Define to 1 if you want extra aligned types in libbson */ diff --git a/src/bson/bson.c b/src/bson/bson.c index cfad21a8..af5043f5 100644 --- a/src/bson/bson.c +++ b/src/bson/bson.c @@ -15,11 +15,6 @@ */ -#if defined(_MSC_VER) && (_MSC_VER >= 1400 && _MSC_VER < 1900) -#define NEEDS_SET_TWO_DIGIT_EXPONENT -#include // _set_output_format and _TWO_DIGIT_EXPONENT Added in vs2005 and removed in vs2015. -#endif - #include "bson.h" #include "b64_ntop.h" #include "bson-private.h" @@ -2336,13 +2331,13 @@ _bson_as_json_visit_double (const bson_iter_t *iter, { bson_json_state_t *state = data; -#ifdef NEEDS_SET_TWO_DIGIT_EXPONENT +#if BSON_NEEDS_SET_OUTPUT_FORMAT unsigned int current_format = _set_output_format(_TWO_DIGIT_EXPONENT); #endif bson_string_append_printf (state->str, "%.15g", v_double); -#ifdef NEEDS_SET_TWO_DIGIT_EXPONENT +#if BSON_NEEDS_SET_OUTPUT_FORMAT _set_output_format(current_format); #endif diff --git a/tests/TestSuite.c b/tests/TestSuite.c index e87ca270..dd5edec8 100644 --- a/tests/TestSuite.c +++ b/tests/TestSuite.c @@ -85,7 +85,7 @@ # define AtomicInt_DecrementAndTest(p) (bson_atomic_int_add(p, -1) == 0) -#if defined(_MSC_VER) && _MSC_VER < 1900 +#if !defined(BSON_HAVE_TIMESPEC) struct timespec { time_t tv_sec;