Skip to content
Permalink
Browse files

#5277: Improving the vsnprintf fix in r10126: moving the HAVE_VSNPRIN…

…TF check to top level CMakeLists since it is needed by both c99snprintf and jmutils. This allows the check for MSVC version to be kept entirely in the CMake files. The replacement functions in c99snprintf are renamed (from rpl_* to jm_rpl_*) to avoid conflicts when linking statically against other libraries that use c99snprintf.
  • Loading branch information
hlarsson
hlarsson committed Sep 13, 2017
1 parent 39048f4 commit 88df8777c010dcd0c0a4cbafc4c6c635cdb941fb
Showing with 66 additions and 59 deletions.
  1. +24 −4 CMakeLists.txt
  2. +7 −2 Config.cmake/config_fmilib.h.cmake
  3. +23 −33 ThirdParty/c99_snprintf/CMakeLists.txt
  4. +12 −20 src/Util/src/JM/jm_portability.c
@@ -175,6 +175,30 @@ if(NOT FMILIB_STATIC_LIB_ONLY)
add_definitions(-DFMILIB_BUILDING_LIBRARY)
endif()

include(CheckSymbolExists)
include(CheckFunctionExists)
CHECK_SYMBOL_EXISTS(va_copy stdarg.h HAVE_VA_COPY)
CHECK_SYMBOL_EXISTS(__va_copy stdarg.h HAVE___VA_COPY)

CHECK_SYMBOL_EXISTS(vsnprintf stdio.h HAVE_VSNPRINTF_SYM)
CHECK_FUNCTION_EXISTS(vsnprintf HAVE_VSNPRINTF_FCN)

MACRO(FMIL_EVAL_CONDITION var_to_store_cond_eval)
IF(${ARGN})
SET(${var_to_store_cond_eval} 1)
ELSE(${ARGN})
SET(${var_to_store_cond_eval} 0)
ENDIF(${ARGN})
ENDMACRO(FMIL_EVAL_CONDITION)

FMIL_EVAL_CONDITION(HAVE_VSNPRINTF HAVE_VSNPRINTF_SYM OR HAVE_VSNPRINTF_FCN)

# MSVC < 19.0 (Visual Studio 2015) have a non-standard vsnprintf
# implementation. Use the one provided by c99snprintf instead
if (MSVC AND (MSVC_VERSION LESS 1900))
set(HAVE_VSNPRINTF FALSE)
endif()

add_subdirectory(ThirdParty/c99_snprintf)

SET(CMAKE_MODULE_PATH ${FMILIBRARYHOME}/Config.cmake)
@@ -202,10 +226,6 @@ else()
set(FMI_PLATFORM ${FMILIB_FMI_PLATFORM})
endif()

include(CheckSymbolExists)
CHECK_SYMBOL_EXISTS(va_copy stdarg.h HAVE_VA_COPY)
CHECK_SYMBOL_EXISTS(__va_copy stdarg.h HAVE___VA_COPY)

configure_file (
"${FMILibrary_SOURCE_DIR}/Config.cmake/config_fmilib.h.cmake"
"${FMILibrary_BINARY_DIR}/fmilib_config.h"
@@ -43,7 +43,7 @@

It is redefined to "." for Makefile based build environments. Otherwise is set by MSVC. */
#endif


/** \brief Platform folder separator */
#define FMI_FILE_SEP "@FMI_FILE_SEP@"

@@ -54,7 +54,7 @@ It is redefined to "." for Makefile based build environments. Otherwise is set b
#define FMI_PLATFORM "@FMI_PLATFORM@"

/** \brief Folder name inside FMU where binaries are */
#define FMI_BINARIES "binaries"
#define FMI_BINARIES "binaries"
/** \brief Model description XML file name */
#define FMI_MODEL_DESCRIPTION_XML "modelDescription.xml"

@@ -155,4 +155,9 @@ FMILIB_EXPORT const char* fmilib_get_build_stamp(void);
#cmakedefine HAVE_VA_COPY
#cmakedefine HAVE___VA_COPY

#cmakedefine HAVE_VSNPRINTF
#ifdef HAVE_VSNPRINTF
#define jm_rpl_vsnprintf vsnprintf
#endif

#endif
@@ -26,23 +26,13 @@ CHECK_INCLUDE_FILES(locale.h HAVE_LOCALE_H)

INCLUDE (CheckSymbolExists)
INCLUDE (CheckFunctionExists)
CHECK_SYMBOL_EXISTS(vsnprintf stdio.h HAVE_VSNPRINTF_SYM)
CHECK_SYMBOL_EXISTS(snprintf stdio.h HAVE_SNPRINTF_SYM)
CHECK_SYMBOL_EXISTS(vasprintf stdio.h HAVE_VASPRINTF_SYM)
CHECK_SYMBOL_EXISTS(asprintf stdio.h HAVE_ASPRINTF_SYM)
CHECK_FUNCTION_EXISTS(vsnprintf HAVE_VSNPRINTF_FCN)
CHECK_FUNCTION_EXISTS(snprintf HAVE_SNPRINTF_FCN)
CHECK_FUNCTION_EXISTS(vasprintf HAVE_VASPRINTF_FCN)
CHECK_FUNCTION_EXISTS(asprintf HAVE_ASPRINTF_FCN)

MACRO(FMIL_EVAL_CONDITION var_to_store_cond_eval)
IF(${ARGN})
SET(${var_to_store_cond_eval} 1)
ELSE(${ARGN})
SET(${var_to_store_cond_eval} 0)
ENDIF(${ARGN})
ENDMACRO(FMIL_EVAL_CONDITION)
FMIL_EVAL_CONDITION(HAVE_VSNPRINTF HAVE_VSNPRINTF_SYM OR HAVE_VSNPRINTF_FCN)

FMIL_EVAL_CONDITION(HAVE_SNPRINTF HAVE_SNPRINTF_SYM OR HAVE_SNPRINTF_FCN)
FMIL_EVAL_CONDITION(HAVE_VASPRINTF HAVE_VASPRINTF_SYM OR HAVE_VASPRINTF_FCN)
FMIL_EVAL_CONDITION(HAVE_ASPRINTF HAVE_ASPRINTF_SYM OR HAVE_ASPRINTF_FCN)
@@ -97,34 +87,34 @@ set(CHECKS
HAVE_VA_COPY
HAVE___VA_COPY)

if (MSVC AND (MSVC_VERSION LESS 1900))
set(HAVE_VSNPRINTF FALSE)
else()
set(CHECKS ${CHECKS} HAVE_VSNPRINTF)
endif()

foreach(check ${CHECKS})
if(${${check}})
set(DEFINES ${DEFINES} -D${check}=${${check}})
endif()
endforeach()

if(NOT HAVE_VSNPRINTF)
set(DEFINES ${DEFINES} -Dvsnprintf=rpl_vsnprintf)
endif()

if(NOT HAVE_SNPRINTF)
set(DEFINES ${DEFINES} -Dsnprintf=rpl_snprintf)
endif()

if(NOT HAVE_VASPRINTF)
set(DEFINES ${DEFINES} -Dvasprintf=rpl_vasprintf)
endif()

if(NOT HAVE_ASPRINTF)
set(DEFINES ${DEFINES} -Dasprintf=rpl_asprintf)
endif()

if(NOT HAVE_VSNPRINTF)
set(DEFINES ${DEFINES} -Drpl_vsnprintf=jm_rpl_vsnprintf)
endif()

if(NOT HAVE_SNPRINTF)
set(DEFINES ${DEFINES}
-Dsnprintf=jm_rpl_snprintf
-Drpl_snprintf=jm_rpl_snprintf)
endif()

if(NOT HAVE_VASPRINTF)
set(DEFINES ${DEFINES}
-Dvasprintf=jm_rpl_vasprintf
-Drpl_vasprintf=jm_rpl_vasprintf)
endif()

if(NOT HAVE_ASPRINTF)
set(DEFINES ${DEFINES}
-Dasprintf=jm_rpl_asprintf
-Drpl_asprintf=jm_rpl_asprintf)
endif()

add_definitions(${DEFINES})

file(WRITE ${CMAKE_BINARY_DIR}/c99snprintf_defs "${DEFINES}")
@@ -129,12 +129,12 @@ const char* jm_get_system_temp_dir() {
return "/tmp/";
#endif
}

#ifdef WIN32
#include <io.h>
#else

#ifdef WIN32
#include <io.h>
#else
#include <stdlib.h>
#endif
#endif
char *jm_mkdtemp(jm_callbacks *cb, char *tmplt)
{
#ifdef WIN32
@@ -152,8 +152,8 @@ char *jm_mkdtemp(jm_callbacks *cb, char *tmplt)
#else
return mkdtemp(tmplt);
#endif
}

}

#ifdef WIN32
#include <direct.h>
#define MKDIR(dir) _mkdir(dir)
@@ -275,7 +275,7 @@ char* jm_mk_temp_dir(jm_callbacks* cb, const char* systemTempDir, const char* te
return 0;
}
sprintf(tmpPath,"%s%sXXXXXX",tmpDir,tempPrefix);/*safe*/


if (jm_mkdtemp(cb, tmpPath) == NULL) {
jm_log_fatal(cb, module,"Could not create a unique temporary directory");
}
@@ -338,19 +338,11 @@ char* jm_create_URL_from_abs_path(jm_callbacks* cb, const char* path) {
strcpy(url, buffer);
return url;
}


/*
* MSVC < 19.0 (Visual Studio 2015) have a non-standard vsnprintf
* implementation. Use the one provided by c99snprintf instead.
*/
#if defined(_MSC_VER) && _MSC_VER < 1900
#define vsnprintf rpl_vsnprintf
#endif
int vsnprintf(char *, size_t, const char *, va_list);

int jm_rpl_vsnprintf(char *, size_t, const char *, va_list);

int jm_vsnprintf(char * str, size_t size, const char * fmt, va_list al) {
return vsnprintf(str, size, fmt, al);
return jm_rpl_vsnprintf(str, size, fmt, al);
}

int jm_snprintf(char * str, size_t size, const char * fmt, ...) {
@@ -360,4 +352,4 @@ int jm_snprintf(char * str, size_t size, const char * fmt, ...) {
ret = jm_vsnprintf(str, size, fmt, args);
va_end (args);
return ret;
}
}

0 comments on commit 88df877

Please sign in to comment.
You can’t perform that action at this time.