glew/build/cmake/CMakeLists.txt
Marvin Schmidt c8a2ddfbe1 cmake: Link libGLX when X11 support is requested
Using cmake (version 3.23.0) the build fails to link the utils
```
[...]
[ 87%] Linking C executable bin/visualinfo                                                                                                                      [113/5249]
/usr/x86_64-pc-linux-gnu/bin/cmake -E cmake_link_script CMakeFiles/visualinfo.dir/link.txt --verbose=1
/usr/host/bin/x86_64-pc-linux-gnu-cc -Wall -g -ggdb3 -O0 -rdynamic CMakeFiles/visualinfo.dir/home/marv/scm/github/glew/src/visualinfo.c.o -o bin/visualinfo  -Wl,-rpath,/h
ome/marv/scm/github/glew/build-obj/lib: lib/libGLEW.so.2.2.0 -lSM -lICE -lX11 -lXext -lOpenGL -lSM -lICE -lX11 -lXext
/usr/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-ld: CMakeFiles/visualinfo.dir/home/marv/scm/github/glew/src/visualinfo.c.o: in function `main':
/home/marv/scm/github/glew/src/visualinfo.c:198: undefined reference to `glXQueryExtensionsString'
/usr/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-ld: CMakeFiles/visualinfo.dir/home/marv/scm/github/glew/src/visualinfo.c.o: in function `CreateContext':
/home/marv/scm/github/glew/src/visualinfo.c:1204: undefined reference to `glXQueryExtension'
/usr/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-ld: /home/marv/scm/github/glew/src/visualinfo.c:1206: undefined reference to `glXChooseVisual'
/usr/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-ld: /home/marv/scm/github/glew/src/visualinfo.c:1209: undefined reference to `glXCreateContext'
/usr/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-ld: /home/marv/scm/github/glew/src/visualinfo.c:1221: undefined reference to `glXMakeCurrent'
/usr/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-ld: CMakeFiles/visualinfo.dir/home/marv/scm/github/glew/src/visualinfo.c.o: in function `DestroyContext':
/home/marv/scm/github/glew/src/visualinfo.c:1227: undefined reference to `glXDestroyContext'
/usr/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-ld: lib/libGLEW.so.2.2.0: undefined reference to `glXGetProcAddressARB'
/usr/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-ld: lib/libGLEW.so.2.2.0: undefined reference to `glXQueryVersion'
/usr/x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu-ld: lib/libGLEW.so.2.2.0: undefined reference to `glXGetClientString'
collect2: error: ld returned 1 exit status
```

Commit 2b50f4a ("CMake: Prefer GLVND if available") changed the cmake
policy CMP0072 to `NEW` in order to make the FindOpenGL module prefer the
GLVND libraries (libOpenGL and libGLX) over the legacy GL library (libGL).

The help of the CMP0072 policy states:

> CMP0072
> -------
>
> .. versionadded:: 3.11
>
> ``FindOpenGL`` prefers GLVND by default when available.
>
> The ``FindOpenGL`` module provides an ``OpenGL::GL`` target and an
> ``OPENGL_LIBRARIES`` variable for projects to use for legacy GL interfaces.
> When both a legacy GL library (e.g. ``libGL.so``) and GLVND libraries
> for OpenGL and GLX (e.g. ``libOpenGL.so`` and ``libGLX.so``) are available,
> the module must choose between them.  It documents an ``OpenGL_GL_PREFERENCE``
> variable that can be used to specify an explicit preference.  When no such
> preference is set, the module must choose a default preference.
>
> CMake 3.11 and above prefer to choose GLVND libraries.  This policy provides
> compatibility with projects that expect the legacy GL library to be used.
>
> The ``OLD`` behavior for this policy is to set ``OpenGL_GL_PREFERENCE`` to
> ``LEGACY``.  The ``NEW`` behavior for this policy is to set
> ``OpenGL_GL_PREFERENCE`` to ``GLVND``.
>
> This policy was introduced in CMake version 3.11.  CMake version
> 3.23.0 warns when the policy is not set and uses ``OLD`` behavior.
> Use the ``cmake_policy()`` command to set it to ``OLD`` or ``NEW``
> explicitly.
>
> .. note::
>   The ``OLD`` behavior of a policy is
>   ``deprecated by definition``
>   and may be removed in a future version of CMake.

The changes from the mentioned commit combined with the new behaviour of
the FindOpenGL module resulted in the glew library being linked against
`libOpenGL` instead of `libGL`, but not `libGLX`. Since `libOpenGL`
doesn't link against GLX compared to `libGL` the linker errors above
surfaced. Fix this by explicitly linking against libGLX if GLEW_X11 is
enabled
2022-06-13 12:22:14 +10:00

288 lines
8.9 KiB
CMake

if ( NOT DEFINED CMAKE_BUILD_TYPE )
set( CMAKE_BUILD_TYPE Release CACHE STRING "Build type" )
endif ()
project (glew C)
cmake_minimum_required (VERSION 2.8.12)
include(GNUInstallDirs)
if(POLICY CMP0003)
cmake_policy (SET CMP0003 NEW)
endif()
if(POLICY CMP0042)
cmake_policy (SET CMP0042 NEW)
endif()
if (POLICY CMP0072)
cmake_policy (SET CMP0072 NEW)
endif(POLICY CMP0072)
set(CMAKE_DEBUG_POSTFIX d)
option (BUILD_UTILS "utilities" ON)
option (GLEW_REGAL "Regal mode" OFF)
option (GLEW_OSMESA "OSMesa mode" OFF)
if (APPLE)
option (BUILD_FRAMEWORK "Build Framework bundle for OSX" OFF)
endif ()
option (GLEW_X11 "X11 mode" ON)
option (GLEW_EGL "EGL mode" OFF)
option (BUILD_SHARED_LIBS "install/link shared instead of static libs" ON)
set (GLEW_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../..)
# get version from config/version
file (STRINGS ${GLEW_DIR}/config/version _VERSION_MAJOR_STRING REGEX "GLEW_MAJOR[ ]*=[ ]*[0-9]+.*")
string (REGEX REPLACE "GLEW_MAJOR[ ]*=[ ]*([0-9]+)" "\\1" CPACK_PACKAGE_VERSION_MAJOR ${_VERSION_MAJOR_STRING})
file (STRINGS ${GLEW_DIR}/config/version _VERSION_MINOR_STRING REGEX "GLEW_MINOR[ ]*=[ ]*[0-9]+.*")
string (REGEX REPLACE "GLEW_MINOR[ ]*=[ ]*([0-9]+)" "\\1" CPACK_PACKAGE_VERSION_MINOR ${_VERSION_MINOR_STRING})
file (STRINGS ${GLEW_DIR}/config/version _VERSION_PATCH_STRING REGEX "GLEW_MICRO[ ]*=[ ]*[0-9]+.*")
string (REGEX REPLACE "GLEW_MICRO[ ]*=[ ]*([0-9]+)" "\\1" CPACK_PACKAGE_VERSION_PATCH ${_VERSION_PATCH_STRING})
set (GLEW_VERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH})
if (NOT GLEW_CUSTOM_OUTPUT_DIRS)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
endif()
find_package (OpenGL REQUIRED)
# cmake<3.10 doesn't detect EGL/GLX
if (CMAKE_VERSION VERSION_LESS 3.10)
find_library(OPENGL_egl_LIBRARY NAMES EGL)
if (OPENGL_egl_LIBRARY)
set (OpenGL_EGL_FOUND TRUE)
endif ()
endif ()
# prefer GLVND
if (OPENGL_opengl_LIBRARY)
set (GLEW_LIBRARIES ${OPENGL_opengl_LIBRARY})
else ()
set (GLEW_LIBRARIES ${OPENGL_gl_LIBRARY})
endif ()
# X11 required except for Windows and Apple OSX platforms
if (GLEW_X11 AND NOT WIN32 AND NOT APPLE)
find_package (X11)
list (APPEND GLEW_LIBRARIES ${OPENGL_glx_LIBRARY} ${X11_LIBRARIES})
endif()
if (WIN32)
set (GLEW_LIB_NAME glew32)
else ()
set (GLEW_LIB_NAME GLEW)
set (DLL_PREFIX lib)
endif ()
add_definitions (-DGLEW_NO_GLU)
#### Regal mode ####
if (GLEW_REGAL)
if (WIN32)
set (REGAL_LIB_NAME regal32)
else ()
set (REGAL_LIB_NAME Regal)
endif ()
add_definitions (-DGLEW_REGAL)
set (GLEW_LIBRARIES ${REGAL_LIB_NAME})
endif ()
#### OSMesa mode ####
if (GLEW_OSMESA)
if (WIN32)
set (OSMESA_LIB_NAME osmesa)
else ()
set (OSMESA_LIB_NAME OSMesa)
endif ()
add_definitions (-DGLEW_OSMESA)
list (APPEND GLEW_LIBRARIES ${OSMESA_LIB_NAME})
set (X11_LIBRARIES)
endif ()
#### EGL ####
if (GLEW_EGL AND UNIX)
add_definitions (-DGLEW_EGL)
if (NOT OpenGL_EGL_FOUND)
message (FATAL_ERROR "EGL library set but not found.")
endif ()
list (APPEND GLEW_LIBRARIES ${OPENGL_egl_LIBRARY})
endif ()
#### GLEW ####
include_directories (${GLEW_DIR}/include ${X11_INCLUDE_DIR})
set (GLEW_PUBLIC_HEADERS_FILES
${GLEW_DIR}/include/GL/wglew.h
${GLEW_DIR}/include/GL/glew.h
${GLEW_DIR}/include/GL/glxew.h
${GLEW_DIR}/include/GL/eglew.h
)
set (GLEW_SRC_FILES ${GLEW_DIR}/src/glew.c)
if (WIN32)
list (APPEND GLEW_SRC_FILES ${GLEW_DIR}/build/glew.rc)
endif ()
add_library (glew SHARED ${GLEW_PUBLIC_HEADERS_FILES} ${GLEW_SRC_FILES})
set_target_properties (glew PROPERTIES COMPILE_DEFINITIONS "GLEW_BUILD" OUTPUT_NAME "${GLEW_LIB_NAME}" PREFIX "${DLL_PREFIX}"
VERSION ${GLEW_VERSION}
SOVERSION ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR})
add_library (glew_s STATIC ${GLEW_PUBLIC_HEADERS_FILES} ${GLEW_SRC_FILES})
set_target_properties (glew_s PROPERTIES COMPILE_DEFINITIONS "GLEW_STATIC" OUTPUT_NAME "${GLEW_LIB_NAME}" PREFIX lib)
if (MSVC)
# add options from visual studio project
target_compile_definitions (glew PRIVATE "GLEW_BUILD;VC_EXTRALEAN")
target_compile_definitions (glew_s PRIVATE "GLEW_STATIC;VC_EXTRALEAN")
target_link_libraries (glew LINK_PRIVATE -BASE:0x62AA0000)
# kill security checks which are dependent on stdlib
target_compile_options (glew PRIVATE -GS-)
target_compile_options (glew_s PRIVATE -GS-)
# remove stdlib dependency
target_link_libraries (glew LINK_PRIVATE -nodefaultlib -noentry)
target_link_libraries (glew LINK_PRIVATE libvcruntime.lib)
target_link_libraries (glew LINK_PRIVATE msvcrt.lib )
string(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
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)
target_compile_options (glew PRIVATE -fno-builtin -fno-stack-protector)
target_compile_options (glew_s PRIVATE -fno-builtin -fno-stack-protector)
target_link_libraries (glew LINK_PRIVATE -nostdlib)
endif ()
if (BUILD_FRAMEWORK)
set_target_properties(glew PROPERTIES
FRAMEWORK TRUE
FRAMEWORK_VERSION ${GLEW_VERSION}
MACOSX_FRAMEWORK_IDENTIFIER net.sourceforge.glew
MACOSX_FRAMEWORK_SHORT_VERSION_STRING ${GLEW_VERSION}
MACOSX_FRAMEWORK_BUNDLE_VERSION ${GLEW_VERSION}
XCODE_ATTRIBUTE_INSTALL_PATH "@rpath"
PUBLIC_HEADER "${GLEW_PUBLIC_HEADERS_FILES}"
OUTPUT_NAME GLEW
)
endif()
target_link_libraries (glew LINK_PUBLIC ${GLEW_LIBRARIES})
target_link_libraries (glew_s ${GLEW_LIBRARIES})
if(CMAKE_VERSION VERSION_LESS 2.8.12)
set(MAYBE_EXPORT "")
else()
target_compile_definitions(glew_s INTERFACE "GLEW_STATIC")
foreach(t glew glew_s)
target_include_directories(${t} PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
endforeach()
set(MAYBE_EXPORT EXPORT glew-targets)
endif()
set(targets_to_install "")
if(BUILD_SHARED_LIBS)
list(APPEND targets_to_install glew)
else ()
list(APPEND targets_to_install glew_s)
endif()
install ( TARGETS ${targets_to_install}
${MAYBE_EXPORT}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
FRAMEWORK DESTINATION ${CMAKE_INSTALL_PREFIX}
)
if (BUILD_UTILS)
set (GLEWINFO_SRC_FILES ${GLEW_DIR}/src/glewinfo.c)
if (WIN32)
list (APPEND GLEWINFO_SRC_FILES ${GLEW_DIR}/build/glewinfo.rc)
endif ()
add_executable (glewinfo ${GLEWINFO_SRC_FILES})
if(BUILD_SHARED_LIBS)
target_link_libraries (glewinfo glew)
else()
target_link_libraries (glewinfo glew_s)
endif()
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})
if(BUILD_SHARED_LIBS)
target_link_libraries (visualinfo glew)
else()
target_link_libraries (visualinfo glew_s)
endif()
if (NOT WIN32)
target_link_libraries(visualinfo ${X11_LIBRARIES})
endif ()
install ( TARGETS glewinfo visualinfo
DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()
set (prefix ${CMAKE_INSTALL_PREFIX})
set (exec_prefix ${CMAKE_INSTALL_PREFIX})
set (libdir ${CMAKE_INSTALL_FULL_LIBDIR})
set (includedir ${CMAKE_INSTALL_FULL_INCLUDEDIR})
set (version ${GLEW_VERSION})
set (libname ${GLEW_LIB_NAME})
set (cflags)
set (requireslib glu)
# Mac OSX has no glu.pc unless optional X11/GLX is installed
if (APPLE)
set (requireslib)
endif ()
configure_file (${GLEW_DIR}/glew.pc.in ${CMAKE_CURRENT_BINARY_DIR}/glew.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/glew.pc
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
if(WIN32 AND MSVC AND (NOT MSVC_VERSION LESS 1600) AND (NOT CMAKE_VERSION VERSION_LESS "3.1"))
install(
FILES $<TARGET_PDB_FILE:glew>
DESTINATION ${CMAKE_INSTALL_LIBDIR}
CONFIGURATIONS Debug RelWithDebInfo
)
endif()
install (
FILES ${GLEW_PUBLIC_HEADERS_FILES}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/GL)
if(MAYBE_EXPORT)
install(EXPORT glew-targets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/glew
NAMESPACE GLEW::)
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/glew-config.cmake
${CMAKE_CURRENT_SOURCE_DIR}/CopyImportedTargetProperties.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/glew)
endif()
if(NOT TARGET uninstall)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
IMMEDIATE @ONLY)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()