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.
This commit is contained in:
Wes Tarro 2016-02-05 02:53:25 -05:00
parent b3440c0979
commit 5efdbaefc8
5 changed files with 47 additions and 28 deletions

View File

@ -10,8 +10,6 @@
#endif #endif
#include <stddef.h> /* For size_t */ #include <stddef.h> /* For size_t */
#include <stdlib.h> /* For bsearch */
#include <string.h> /* For memset */
#if defined(GLEW_REGAL) #if defined(GLEW_REGAL)

View File

@ -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 */ /* 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) while (*s1 || *s2)
{ {
if (*s1 > *s2) if (*s1 > *s2)
@ -17,31 +15,32 @@ static int _glewExtensionCompare(const void *a, const void *b)
return 0; 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) static GLboolean *_glewGetExtensionString(const char *name)
{ {
const char **n = (const char **) bsearch(name, _glewExtensionLookup, sizeof(_glewExtensionLookup)/sizeof(char *)-1, sizeof(char *), _glewExtensionCompare); ptrdiff_t n = _glewBsearchExtension(name);
ptrdiff_t i; if (n >= 0) return &_glewExtensionString[n];
if (n)
{
i = n-_glewExtensionLookup;
return _glewExtensionString+i;
}
return NULL; return NULL;
} }
static GLboolean *_glewGetExtensionEnable(const char *name) static GLboolean *_glewGetExtensionEnable(const char *name)
{ {
const char **n = (const char **) bsearch(name, _glewExtensionLookup, sizeof(_glewExtensionLookup)/sizeof(char *)-1, sizeof(char *), _glewExtensionCompare); ptrdiff_t n = _glewBsearchExtension(name);
ptrdiff_t i; if (n >= 0) return _glewExtensionEnabled[n];
if (n)
{
i = n-_glewExtensionLookup;
return _glewExtensionEnabled[i];
}
return NULL; 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; 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) if (GLEW_VERSION_3_0)
{ {

View File

@ -81,10 +81,22 @@ if (WIN32)
endif () endif ()
add_library (glew SHARED ${GLEW_SRC_FILES}) 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}) 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) 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}) target_link_libraries (glew_s ${GLEW_LIBRARIES})
if(CMAKE_VERSION VERSION_LESS 2.8.12) 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) list (APPEND GLEWINFO_SRC_FILES ${GLEW_DIR}/build/glewinfo.rc)
endif () endif ()
add_executable (glewinfo ${GLEWINFO_SRC_FILES}) 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) set (VISUALINFO_SRC_FILES ${GLEW_DIR}/src/visualinfo.c)
if (WIN32) if (WIN32)
list (APPEND VISUALINFO_SRC_FILES ${GLEW_DIR}/build/visualinfo.rc) list (APPEND VISUALINFO_SRC_FILES ${GLEW_DIR}/build/visualinfo.rc)
endif () endif ()
add_executable (visualinfo ${VISUALINFO_SRC_FILES}) 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 install ( TARGETS glewinfo visualinfo
DESTINATION bin) DESTINATION bin)

View File

@ -280,6 +280,7 @@
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<AdditionalIncludeDirectories>$(INCLUDE_DIR)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>$(INCLUDE_DIR)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;WIN32_LEAN_AND_MEAN;VC_EXTRALEAN;GLEW_BUILD;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;WIN32_LEAN_AND_MEAN;VC_EXTRALEAN;GLEW_BUILD;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BufferSecurityCheck>false</BufferSecurityCheck>
</ClCompile> </ClCompile>
<Midl> <Midl>
<SuppressStartupBanner>true</SuppressStartupBanner> <SuppressStartupBanner>true</SuppressStartupBanner>
@ -304,6 +305,8 @@
<AdditionalOptions> /ignore:4089</AdditionalOptions> <AdditionalOptions> /ignore:4089</AdditionalOptions>
<AdditionalLibraryDirectories> <AdditionalLibraryDirectories>
</AdditionalLibraryDirectories> </AdditionalLibraryDirectories>
<IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
<NoEntryPoint>true</NoEntryPoint>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug MX|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug MX|Win32'">

View File

@ -262,6 +262,7 @@
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../../include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;WIN32_LEAN_AND_MEAN;GLEW_STATIC;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;WIN32_LEAN_AND_MEAN;GLEW_STATIC;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<BufferSecurityCheck>false</BufferSecurityCheck>
</ClCompile> </ClCompile>
<ResourceCompile> <ResourceCompile>
<Culture>0x0409</Culture> <Culture>0x0409</Culture>