CMake: Add tristate option for using system GLFW (#455)

-DWITH_SYSTEM_GLFW=ON: Link against system glfw and fail otherwise
-DWITH_SYSTEM_GLFW=OFF: Use embedded rglfw.c
-DWITH_SYSTEM_GLFW=IF_POSSIBLE: Probe for system glfw but fallback to
                                rglfw if unavailable

Also change Linux 64-bit CI build to install system glfw and use it,
so this doesn't bitrot.

Addresses #453.
This commit is contained in:
Ahmad Fatoum 2018-02-03 10:17:51 +01:00 committed by GitHub
parent 007ae1b7b3
commit 7f5fa4d49c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 40 deletions

View file

@ -1,6 +1,9 @@
# All sorts of things that we need cross project
cmake_minimum_required(VERSION 2.8.0)
set(WITH_SYSTEM_GLFW OFF CACHE STRING "Link raylib against system GLFW instead of embedded one")
set_property(CACHE WITH_SYSTEM_GLFW PROPERTY STRINGS ON OFF IF_POSSIBLE)
# Linking for OS X -framework options
# Will do nothing on other OSes
if(APPLE)
@ -27,24 +30,59 @@ else()
find_library(XINERAMA_LIBRARY Xinerama)
find_library(XXF86VM_LIBRARY Xxf86vm)
find_library(XCURSOR_LIBRARY Xcursor)
include_directories(${OPENGL_INCLUDE_DIR})
set(LIBS_PRIVATE m ${pthread} ${OPENGL_LIBRARIES} ${X11_LIBRARIES} ${XRANDR_LIBRARY} ${XINERAMA_LIBRARY} ${XI_LIBRARY} ${XXF86VM_LIBRARY} ${XCURSOR_LIBRARY})
endif()
endif()
if(WITH_SYSTEM_GLFW STREQUAL "ON")
find_package(glfw3 3.2.1 REQUIRED)
else(WITH_SYSTEM_GLFW STREQUAL "IF_POSSIBLE")
find_package(glfw3 3.2.1)
endif()
if (glfw3_FOUND)
set(LIBS_PRIVATE ${LIBS_PRIVATE} glfw)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
set(LINUX TRUE)
set(LIBS_PRIVATE dl ${LIBS_PRIVATE})
endif()
foreach(L ${LIBS_PRIVATE})
get_filename_component(DIR ${L} PATH)
get_filename_component(LIBFILE ${L} NAME_WE)
STRING(REGEX REPLACE "^lib" "" FILE ${LIBFILE})
if (${L} MATCHES "[.]framework$")
set(FILE_OPT "-framework ${FILE}")
set(DIR_OPT "-F${DIR}")
else()
set(FILE_OPT "-l${FILE}")
set(DIR_OPT "-L${DIR}")
endif()
if ("${DIR}" STREQUAL "" OR "${DIR}" STREQUAL "${LASTDIR}")
set (DIR_OPT "")
endif()
set(LASTDIR ${DIR})
set(PKG_CONFIG_LIBS_PRIVATE ${PKG_CONFIG_LIBS_PRIVATE} ${DIR_OPT} ${FILE_OPT})
string (REPLACE ";" " " PKG_CONFIG_LIBS_PRIVATE "${PKG_CONFIG_LIBS_PRIVATE}")
endforeach(L)
# Do the linking for executables that are meant to link raylib
function(link_libraries_to_executable executable)
# And raylib
target_link_libraries(${executable} raylib)
# Link the libraries
target_link_libraries(${executable} ${LIBS_PRIVATE})
# Link raylib
if (TARGET raylib_shared)
target_link_libraries(${executable} raylib_shared)
else()
target_link_libraries(${executable} raylib ${PKG_CONFIG_LIBS_PRIVATE})
endif()
endfunction()