CMake: (Properly) build glfw separately with CMake

This reverts commit 2d6fb5c628,
and adds a fix for Alien::raylib's test failures.

The tests failed because the resulting static library didn't reexport
GLFW symbols. As a fix, we now have GLFW create a CMake "object library"
target that we can link with both the static and shared raylib.

This is arguably ugly... Proper fix would probably be a GLFW upstream
object library target.

Closes #536.
This commit is contained in:
Ahmad Fatoum 2018-05-12 15:39:13 +02:00
parent a09d6fd428
commit 0f1aaa474a
No known key found for this signature in database
GPG Key ID: C3EAC3DE9321D59B
4 changed files with 63 additions and 71 deletions

View File

@ -18,19 +18,18 @@ matrix:
- os: linux
env: ARCH=amd64 GLFW=SYSTEM
sudo: required
- os: linux
env: USE_WAYLAND=ON ARCH=amd64
sudo: required
addons:
apt:
packages:
- libwayland-dev
- libxkbcommon-dev
- libegl1-mesa-dev
- os: osx
env: ARCH=universal
# - os: linux
# env: USE_WAYLAND=ON ARCH=amd64
# sudo: required
# addons:
# apt:
# packages:
# - libwayland-dev
# - libxkbcommon-dev
# - libegl1-mesa-dev
before_script:
before_install:
@ -38,11 +37,12 @@ before_install:
sudo apt-get install -y gcc-multilib
libasound2-dev:$ARCH
libxcursor-dev:$ARCH libxinerama-dev:$ARCH mesa-common-dev:$ARCH
libx11-dev:$ARCH libxrandr-dev:$ARCH libxi-dev:$ARCH
libx11-dev:$ARCH libxrandr-dev:$ARCH libxrandr2:$ARCH libxi-dev:$ARCH
libgl1-mesa-dev:$ARCH libglu1-mesa-dev:$ARCH;
if [ "$ARCH" == "i386" ]; then
export CMAKE_ARCH_ARGS='-DCMAKE_C_FLAGS=-m32 -DCMAKE_SYSTEM_LIBRARY_PATH=/usr/lib/i386-linux-gnu';
fi;
export RAYLIB_PACKAGE_SUFFIX="-Linux-$ARCH";
if [ "$ARCH" == "i386" ]; then export CFLAGS="-m32"; fi;
if [ "$ARCH" == "amd64" ]; then export CFLAGS="-m64"; fi;
if [ "$GLFW" == "SYSTEM" ]; then
wget 'http://ftp.de.debian.org/debian/pool/main/g/glfw3/libglfw3_3.2.1-1_amd64.deb';
wget 'http://ftp.de.debian.org/debian/pool/main/g/glfw3/libglfw3-dev_3.2.1-1_amd64.deb';
@ -71,6 +71,7 @@ script:
- if [ "$GLFW" != "SYSTEM" ]; then make package; fi;
- sudo make install
- pkg-config --static --libs raylib
- nm -g release/libraylib.a | grep glfwGetProcAddress || (echo "libraylib.a doesn't contain GLFW symbols! Aborting..." && false)
deploy:
provider: releases

40
src/CMakeLists.txt Normal file → Executable file
View File

@ -10,17 +10,37 @@ include("CMakeOptions.txt")
configure_file(config.h.in ${CMAKE_BINARY_DIR}/cmake/config.h)
include_directories(${CMAKE_BINARY_DIR})
include("../utils.cmake")
if(MACOS_FATLIB)
if (CMAKE_OSX_ARCHITECTURES)
message(FATAL_ERROR "User supplied -DCMAKE_OSX_ARCHITECTURES overrides -DMACOS_FATLIB=ON")
else()
SET(CMAKE_OSX_ARCHITECTURES "x86_64;i386")
endif()
endif()
# Get the sources together
file(GLOB raylib_sources *.c)
list(REMOVE_ITEM raylib_sources ${CMAKE_CURRENT_SOURCE_DIR}/rglfw.c)
if(glfw3_FOUND)
list(REMOVE_ITEM raylib_sources ${CMAKE_CURRENT_SOURCE_DIR}/rglfw.c)
else()
if(NOT glfw3_FOUND)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL " " FORCE)
set(GLFW_USE_WAYLAND ${USE_WAYLAND} CACHE BOOL "" FORCE)
if (USE_PIC OR SHARED)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
add_subdirectory(external/glfw)
include_directories(external/glfw/include)
list(APPEND raylib_sources $<TARGET_OBJECTS:glfw_objlib>)
endif()
include("../utils.cmake")
if(USE_AUDIO)
file(GLOB stb_vorbis external/stb_vorbis.c)
file(GLOB mini_al external/mini_al.c ${stb_vorbis})
@ -51,7 +71,6 @@ if(${PLATFORM} MATCHES "Desktop")
# See: https://github.com/raysan5/raylib/issues/341
if(APPLE)
set(GRAPHICS "GRAPHICS_API_OPENGL_33")
set_source_files_properties(rglfw.c PROPERTIES COMPILE_FLAGS "-x objective-c")
link_libraries("${LIBS_PRIVATE}")
elseif(WIN32)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
@ -76,14 +95,6 @@ elseif(${PLATFORM} MATCHES "Raspberry Pi")
set(GRAPHICS "GRAPHICS_API_OPENGL_ES2")
endif()
if(MACOS_FATLIB)
if (CMAKE_OSX_ARCHITECTURES)
message(FATAL_ERROR "User supplied -DCMAKE_OSX_ARCHITECTURES overrides -DMACOS_FATLIB=ON")
else()
SET(CMAKE_OSX_ARCHITECTURES "x86_64;i386")
endif()
endif()
# Which platform?
if(${PLATFORM} MATCHES "PLATFORM_DESKTOP")
@ -100,7 +111,7 @@ if(${PLATFORM} MATCHES "PLATFORM_DESKTOP")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_MACOSX_RPATH ON)
target_link_libraries(${RAYLIB}_shared ${LIBS_PRIVATE})
target_link_libraries(${RAYLIB}_shared ${LIBS_PRIVATE} glfw ${GLFW_LIBRARIES})
if (UNIX AND ${FILESYSTEM_LACKS_SYMLINKS})
MESSAGE(WARNING "Can't version UNIX shared library on file system without symlink support")
else()
@ -137,6 +148,7 @@ if(${PLATFORM} MATCHES "PLATFORM_DESKTOP")
)
set(PKG_CONFIG_LIBS_PRIVATE ${__PKG_CONFIG_LIBS_PRIVATE})
target_link_libraries(${RAYLIB} glfw ${GLFW_LIBRARIES})
if (WITH_PIC)
set_property(TARGET ${RAYLIB} PROPERTY POSITION_INDEPENDENT_CODE ON)

View File

@ -92,7 +92,9 @@ if (${CMAKE_C_COMPILER_ID} STREQUAL GNU OR ${CMAKE_C_COMPILER_ID} STREQUAL Clang
COMPILE_FLAGS -Wdeclaration-after-statement)
endif()
add_library(glfw ${glfw_SOURCES} ${glfw_HEADERS})
add_library(glfw_objlib OBJECT ${glfw_SOURCES} ${glfw_HEADERS})
add_library(glfw $<TARGET_OBJECTS:glfw_objlib>)
set_target_properties(glfw PROPERTIES
OUTPUT_NAME ${GLFW_LIB_NAME}
VERSION ${GLFW_VERSION}
@ -100,11 +102,11 @@ set_target_properties(glfw PROPERTIES
POSITION_INDEPENDENT_CODE ON
FOLDER "GLFW3")
target_compile_definitions(glfw PRIVATE _GLFW_USE_CONFIG_H)
target_include_directories(glfw PUBLIC
target_compile_definitions(glfw_objlib PRIVATE _GLFW_USE_CONFIG_H)
target_include_directories(glfw_objlib PUBLIC
"$<BUILD_INTERFACE:${GLFW_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include>")
target_include_directories(glfw PRIVATE
target_include_directories(glfw_objlib PRIVATE
"${GLFW_SOURCE_DIR}/src"
"${GLFW_BINARY_DIR}/src"
${glfw_INCLUDE_DIRS})
@ -113,11 +115,11 @@ target_include_directories(glfw PRIVATE
# the inclusion of stddef.h (by glfw3.h), which is itself included before
# win32_platform.h. We define them here until a saner solution can be found
# NOTE: MinGW-w64 and Visual C++ do /not/ need this hack.
target_compile_definitions(glfw PRIVATE
target_compile_definitions(glfw_objlib PRIVATE
"$<$<BOOL:${MINGW}>:UNICODE;WINVER=0x0501>")
# Enable a reasonable set of warnings (no, -Wextra is not reasonable)
target_compile_options(glfw PRIVATE
target_compile_options(glfw_objlib PRIVATE
"$<$<C_COMPILER_ID:Clang>:-Wall>"
"$<$<C_COMPILER_ID:GNU>:-Wall>")
@ -135,7 +137,7 @@ if (BUILD_SHARED_LIBS)
endif()
elseif (APPLE)
# Add -fno-common to work around a bug in Apple's GCC
target_compile_options(glfw PRIVATE "-fno-common")
target_compile_options(glfw_objlib PRIVATE "-fno-common")
set_target_properties(glfw PROPERTIES
INSTALL_NAME_DIR "lib${LIB_SUFFIX}")

51
utils.cmake Normal file → Executable file
View File

@ -17,47 +17,24 @@ if(APPLE)
elseif(WIN32)
# no pkg-config --static on Windows yet...
else()
if(USE_WAYLAND)
set(_GLFW_WAYLAND 1)
else()
set(_GLFW_X11 1)
endif()
find_library(pthread NAMES pthread)
find_package(OpenGL QUIET)
if ("${OPENGL_LIBRARIES}" STREQUAL "")
if(NOT USE_WAYLAND)
# CFLAGS=-m32 cmake on Linux fails for some reason, so fallback to hardcoding
set(LIBS_PRIVATE m pthread GL X11 Xrandr Xinerama Xi Xxf86vm Xcursor)
else()
# CFLAGS=-m32 cmake on Linux fails for some reason, so fallback to hardcoding
set(LIBS_PRIVATE m pthread GL wayland-client wayland-cursor wayland-egl)
endif()
set(OPENGL_LIBRARIES "GL")
endif()
include_directories(${OPENGL_INCLUDE_DIR})
if ("${CMAKE_SYSTEM_NAME}" MATCHES "(Net|Open)BSD")
find_library(OSS_LIBRARY ossaudio)
endif()
set(LIBS_PRIVATE m pthread ${OPENGL_LIBRARIES} ${OSS_LIBRARY})
# TODO: maybe read those out of glfw's cmake config?
if(USE_WAYLAND)
set(LIBS_PRIVATE ${LIBS_PRIVATE} wayland-client wayland-cursor wayland-egl)
else()
if(NOT USE_WAYLAND)
find_package(X11 REQUIRED X11)
find_library(XRANDR_LIBRARY Xrandr)
find_library(XI_LIBRARY Xi)
find_library(XINERAMA_LIBRARY Xinerama)
find_library(XXF86VM_LIBRARY Xxf86vm)
find_library(XCURSOR_LIBRARY Xcursor)
else()
find_library(WAYLAND_CLIENT_LIBRARY wayland-client)
find_library(WAYLAND_CURSOR_LIBRARY wayland-cursor)
find_library(WAYLAND_EGL_LIBRARY wayland-egl)
endif()
include_directories(${OPENGL_INCLUDE_DIR})
if ("${CMAKE_SYSTEM_NAME}" MATCHES "(Net|Open)BSD")
find_library(OSS_LIBRARY ossaudio)
endif()
if(NOT USE_WAYLAND)
set(LIBS_PRIVATE m ${pthread} ${OPENGL_LIBRARIES} ${X11_LIBRARIES} ${XRANDR_LIBRARY} ${XINERAMA_LIBRARY} ${XI_LIBRARY} ${XXF86VM_LIBRARY} ${XCURSOR_LIBRARY} ${OSS_LIBRARY})
else()
set(LIBS_PRIVATE m ${pthread} ${OPENGL_LIBRARIES} ${WAYLAND_CLIENT_LIBRARY} ${WAYLAND_CURSOR_LIBRARY} ${WAYLAND_EGL_LIBRARY} ${OSS_LIBRARY})
endif()
set(LIBS_PRIVATE ${LIBS_PRIVATE} X11 Xrandr Xinerama Xi Xxf86vm Xcursor)
endif()
endif()