From 5efdbaefc8ed57eb3ce306b5aa4c593dd05238ad Mon Sep 17 00:00:00 2001 From: Wes Tarro Date: Fri, 5 Feb 2016 02:53:25 -0500 Subject: [PATCH] Makes GLEW truly independent of C runtime library on Windows to prevent any issues with mixing compilers and library versions. The Visual Studio Projects need some touching up to get all of the settings synced up for all of the build variants. --- auto/src/glew_head.c | 2 -- auto/src/glew_init_gl.c | 43 +++++++++++++++++----------------- build/cmake/CMakeLists.txt | 26 ++++++++++++++++---- build/vc12/glew_shared.vcxproj | 3 +++ build/vc12/glew_static.vcxproj | 1 + 5 files changed, 47 insertions(+), 28 deletions(-) diff --git a/auto/src/glew_head.c b/auto/src/glew_head.c index 99947de..491156d 100644 --- a/auto/src/glew_head.c +++ b/auto/src/glew_head.c @@ -10,8 +10,6 @@ #endif #include /* For size_t */ -#include /* For bsearch */ -#include /* For memset */ #if defined(GLEW_REGAL) diff --git a/auto/src/glew_init_gl.c b/auto/src/glew_init_gl.c index 6fd86ce..9eb2fff 100644 --- a/auto/src/glew_init_gl.c +++ b/auto/src/glew_init_gl.c @@ -1,10 +1,8 @@ /* ------------------------------------------------------------------------- */ -static int _glewExtensionCompare(const void *a, const void *b) +static int _glewExtensionCompare(const char *s1, const char *s2) { /* http://www.chanduthedev.com/2012/07/strcmp-implementation-in-c.html */ - const char *s1 = (const char *) a; - const char *s2 = *(const char * const *) b; while (*s1 || *s2) { if (*s1 > *s2) @@ -17,31 +15,32 @@ static int _glewExtensionCompare(const void *a, const void *b) return 0; } +static ptrdiff_t _glewBsearchExtension(const char* name) +{ + ptrdiff_t lo = 0, hi = sizeof(_glewExtensionLookup) / sizeof(char*) - 2; + + while (lo <= hi) + { + ptrdiff_t mid = (lo + hi) / 2; + const int cmp = _glewExtensionCompare(name, _glewExtensionLookup[mid]); + if (cmp < 0) hi = mid - 1; + else if (cmp > 0) lo = mid + 1; + else return mid; + } + return -1; +} + static GLboolean *_glewGetExtensionString(const char *name) { - const char **n = (const char **) bsearch(name, _glewExtensionLookup, sizeof(_glewExtensionLookup)/sizeof(char *)-1, sizeof(char *), _glewExtensionCompare); - ptrdiff_t i; - - if (n) - { - i = n-_glewExtensionLookup; - return _glewExtensionString+i; - } - + ptrdiff_t n = _glewBsearchExtension(name); + if (n >= 0) return &_glewExtensionString[n]; return NULL; } static GLboolean *_glewGetExtensionEnable(const char *name) { - const char **n = (const char **) bsearch(name, _glewExtensionLookup, sizeof(_glewExtensionLookup)/sizeof(char *)-1, sizeof(char *), _glewExtensionCompare); - ptrdiff_t i; - - if (n) - { - i = n-_glewExtensionLookup; - return _glewExtensionEnabled[i]; - } - + ptrdiff_t n = _glewBsearchExtension(name); + if (n >= 0) return _glewExtensionEnabled[n]; return NULL; } @@ -117,7 +116,7 @@ static GLenum GLEWAPIENTRY glewContextInit () GLEW_VERSION_1_1 = GLEW_VERSION_1_2 == GL_TRUE || ( major == 1 && minor >= 1 ) ? GL_TRUE : GL_FALSE; } - memset(_glewExtensionString,0,sizeof(_glewExtensionString)); + for (size_t i = 0; i < sizeof(_glewExtensionLookup) / sizeof(char*); ++i) _glewExtensionString[i] = GL_FALSE; if (GLEW_VERSION_3_0) { diff --git a/build/cmake/CMakeLists.txt b/build/cmake/CMakeLists.txt index a8cfec7..95d95bf 100644 --- a/build/cmake/CMakeLists.txt +++ b/build/cmake/CMakeLists.txt @@ -81,10 +81,22 @@ if (WIN32) endif () add_library (glew SHARED ${GLEW_SRC_FILES}) -set_target_properties (glew PROPERTIES COMPILE_DEFINITIONS "GLEW_BUILD" OUTPUT_NAME "${GLEW_LIB_NAME}" PREFIX "${DLL_PREFIX}") +set_target_properties (glew PROPERTIES COMPILE_DEFINITIONS "GLEW_BUILD;VC_EXTRALEAN" OUTPUT_NAME "${GLEW_LIB_NAME}" PREFIX "${DLL_PREFIX}") add_library (glew_s STATIC ${GLEW_SRC_FILES}) +if (MSVC) + # add options from visual studio project and remove stdlib dependency + # kill security checks which are dependent on stdlib + target_compile_options (glew PRIVATE -GS-) + target_compile_options (glew_s PRIVATE -GS-) + target_link_libraries (glew PRIVATE -BASE:0x62AA0000 -nodefaultlib -noentry) +elseif (WIN32 AND ((CMAKE_C_COMPILER_ID MATCHES "GNU") OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))) + # remove stdlib dependency on windows with GCC and Clang (for similar reasons + # as to MSVC - to allow it to be used with any Windows compiler) + # not thoroughly tested yet + target_link_libraries (glew PRIVATE -nostdlibs -lgcc) +endif () set_target_properties (glew_s PROPERTIES COMPILE_DEFINITIONS "GLEW_STATIC" OUTPUT_NAME "${GLEW_LIB_NAME}" PREFIX lib) -target_link_libraries (glew ${GLEW_LIBRARIES}) +target_link_libraries (glew PUBLIC ${GLEW_LIBRARIES}) target_link_libraries (glew_s ${GLEW_LIBRARIES}) if(CMAKE_VERSION VERSION_LESS 2.8.12) @@ -119,14 +131,20 @@ if (BUILD_UTILS) list (APPEND GLEWINFO_SRC_FILES ${GLEW_DIR}/build/glewinfo.rc) endif () add_executable (glewinfo ${GLEWINFO_SRC_FILES}) - target_link_libraries (glewinfo glew ${X11_LIBRARIES}) + target_link_libraries (glewinfo glew) + if (NOT WIN32) + target_link_libraries(glewinfo ${X11_LIBRARIES}) + endif () set (VISUALINFO_SRC_FILES ${GLEW_DIR}/src/visualinfo.c) if (WIN32) list (APPEND VISUALINFO_SRC_FILES ${GLEW_DIR}/build/visualinfo.rc) endif () add_executable (visualinfo ${VISUALINFO_SRC_FILES}) - target_link_libraries (visualinfo glew ${X11_LIBRARIES}) + target_link_libraries (visualinfo glew) + if (NOT WIN32) + target_link_libraries(visualinfo ${X11_LIBRARIES}) + endif () install ( TARGETS glewinfo visualinfo DESTINATION bin) diff --git a/build/vc12/glew_shared.vcxproj b/build/vc12/glew_shared.vcxproj index af9d7b7..8a78a90 100644 --- a/build/vc12/glew_shared.vcxproj +++ b/build/vc12/glew_shared.vcxproj @@ -280,6 +280,7 @@ Level3 $(INCLUDE_DIR) WIN32;WIN32_LEAN_AND_MEAN;VC_EXTRALEAN;GLEW_BUILD;%(PreprocessorDefinitions) + false true @@ -304,6 +305,8 @@ /ignore:4089 + true + true diff --git a/build/vc12/glew_static.vcxproj b/build/vc12/glew_static.vcxproj index 85c4be5..9bcbad1 100644 --- a/build/vc12/glew_static.vcxproj +++ b/build/vc12/glew_static.vcxproj @@ -262,6 +262,7 @@ Level3 ../../include;%(AdditionalIncludeDirectories) WIN32;NDEBUG;_LIB;WIN32_LEAN_AND_MEAN;GLEW_STATIC;%(PreprocessorDefinitions) + false 0x0409