CMake: Major cleanup to support find_package(raylib)

Remove that link_libraries_to_executable() hack and defines a proper
raylib target that can be used with target_link_libraries.

The same target is also available for external (user) code by using
find_package(raylib).

This results in:

- Remove hardcoded build directories from examples and games CMakeLists.txt
- Allow rlgl_standalone and other special examples to be built easily
- Allow CMake projects to find_package(raylib instead of fiddling with pkg-config
- Makes code a little more maintainable
- Fixes #471, #606.
- Makes code less confusing by removing the double use of PLATFORM (#584).

Note that this is still not _The Right Way_(TM), because normally
raylib-config.cmake (or its includes) would be automatically generated.
I didn't manage to get that to work though, so I went the easier route
of just wrapping pkg_check_modules for consumption by find_package.
This commit is contained in:
Ahmad Fatoum 2018-07-28 16:08:41 +02:00
parent 3e5093eab0
commit 3f09726331
No known key found for this signature in database
GPG Key ID: C3EAC3DE9321D59B
17 changed files with 321 additions and 264 deletions

View File

@ -46,12 +46,16 @@ matrix:
before_script:
before_install:
- if [ -z "$USE_EXTERNAL_GLFW" ]; then USE_EXTERNAL_GLFW=IF_POSSIBLE; fi
- if [ -z "$SHARED" ]; then SHARED=ON ; fi
- if [ -z "$STATIC" ]; then STATIC=ON ; fi
- if [ -z "$OPENAL" ]; then OPENAL=OFF ; fi
- if [ -z "$USE_EXTERNAL_GLFW" ]; then export USE_EXTERNAL_GLFW=IF_POSSIBLE; fi
- if [ -z "$SHARED" ]; then export SHARED=ON ; fi
- if [ -z "$STATIC" ]; then export STATIC=ON ; fi
- if [ -z "$OPENAL" ]; then export OPENAL=OFF; fi
- if [[ "$INSTALL_GLFW" == "YES" && "$USE_EXTERNAL_GLFW" != "OFF" ]]; then
export DONT_TEST=1;
fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then
if [[ "$ARCH" == *-android ]]; then
export DONT_TEST=1;
export RAYLIB_PACKAGE_SUFFIX="-Android-arm64";
wget https://dl.google.com/android/repository/android-ndk-r17-linux-x86_64.zip;
unzip -qq android-ndk*.zip;
@ -68,11 +72,12 @@ before_install:
export PATH=/tmp/android-toolchain/bin:$PATH;
export CC=${PREFIX}clang;
export CXX=${PREFIX}clang++;
CMAKE_ARCH_ARGS='-DPLATFORM=Android';
export CMAKE_ARCH_ARGS='-DPLATFORM=Android';
elif [ "$ARCH" == "html5" ]; then
export DONT_TEST=1;
export RAYLIB_PACKAGE_SUFFIX="-html5";
docker run --privileged=true -dit --name emscripten -v $(pwd):/src trzeci/emscripten:sdk-incoming-64bit bash;
CMAKE_ARCH_ARGS='-DPLATFORM=Web -DCMAKE_TOOLCHAIN_FILE=../cmake/emscripten.cmake';
export CMAKE_ARCH_ARGS='-DPLATFORM=Web -DCMAKE_TOOLCHAIN_FILE=../cmake/emscripten.cmake';
RUNNER='docker exec -it emscripten cmake -E chdir build';
else
sudo apt-get install -y gcc-multilib
@ -82,7 +87,7 @@ before_install:
libgl1-mesa-dev:$ARCH libglu1-mesa-dev:$ARCH;
if [ "$OPENAL" == "ON" ]; then sudo apt-get install -y libopenal-dev; fi;
if [ "$ARCH" == "i386" ]; then CMAKE_ARCH_ARGS='-DCMAKE_C_FLAGS=-m32 -DCMAKE_SYSTEM_LIBRARY_PATH=/usr/lib/i386-linux-gnu -DSUPPORT_FILEFORMAT_FLAC=OFF'; fi;
if [ "$ARCH" == "i386" ]; then export CMAKE_ARCH_ARGS='-DCMAKE_C_FLAGS=-m32 -DCMAKE_SYSTEM_LIBRARY_PATH=/usr/lib/i386-linux-gnu -DSUPPORT_FILEFORMAT_FLAC=OFF'; fi;
export RAYLIB_PACKAGE_SUFFIX="-Linux-$ARCH";
if [ "$INSTALL_GLFW" == "YES" ]; then
@ -121,7 +126,7 @@ script:
- $RUNNER make VERBOSE=1
- if [ "$RELEASE" != "NO" ]; then $RUNNER make package; fi
- sudo $RUNNER make install
- if [[ "$ARCH" != *-android && "$ARCH" != html5 ]]; then
- if [ ! "$DONT_TEST" ]; then
pkg-config --static --libs raylib;
nm -g release/libraylib.a | grep glfwGetProcAddress || (echo "libraylib.a doesn't contain GLFW symbols! Aborting..." && false);
ctest --output-on-failure;

View File

@ -6,7 +6,7 @@ option(BUILD_EXAMPLES "Build the examples." ON)
option(BUILD_GAMES "Build the example games." ON)
option(ENABLE_ASAN "Enable AddressSanitizer (ASAN) for debugging (degrades performance)" OFF)
option(ENABLE_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan) for debugging" OFF)
option(ENABLE_MSAN "Enable MemorySanitizer (MSan) for debugging (not recommended for run with ASAN)" OFF)
option(ENABLE_MSAN "Enable MemorySanitizer (MSan) for debugging (not recommended to run with ASAN)" OFF)
if(CMAKE_VERSION VERSION_LESS "3.1")
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")

View File

@ -0,0 +1,24 @@
function(library_path_to_linker_flags LD_FLAGS LIB_PATHS)
foreach(L ${LIB_PATHS})
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(${LD_FLAGS} ${${LD_FLAGS}} ${DIR_OPT} ${FILE_OPT} PARENT_SCOPE)
string (REPLACE ";" " " ${LD_FLAGS} "${${LD_FLAGS}}")
endforeach()
endfunction()

View File

@ -0,0 +1,11 @@
macro(populate_config_variables_locally target)
get_property(raylib_INCLUDE_DIRS TARGET ${target} PROPERTY INTERFACE_INCLUDE_DIRECTORIES)
#get_property(raylib_LIBRARIES TARGET ${target} PROPERTY LOCATION) # only works for SHARED
get_property(raylib_LDFLAGS TARGET ${target} PROPERTY INTERFACE_LINK_LIBRARIES)
get_property(raylib_DEFINITIONS TARGET ${target} PROPERTY DEFINITIONS)
set(raylib_INCLUDE_DIRS "${raylib_INCLUDE_DIRS}" PARENT_SCOPE)
#set(raylib_LIBRARIES "${raylib_INCLUDE_DIRS}" PARENT_SCOPE)
set(raylib_LDFLAGS "${raylib_LDFLAGS}" PARENT_SCOPE)
set(raylib_DEFINITIONS "${raylib_DEFINITIONS}" PARENT_SCOPE)
endmacro()

View File

@ -0,0 +1,21 @@
set(PACKAGE_VERSION "@PROJECT_VERSION@")
if(PACKAGE_FIND_VERSION VERSION_EQUAL PACKAGE_VERSION)
set(PACKAGE_VERSION_EXACT TRUE)
endif()
if(NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION)
set(PACKAGE_VERSION_COMPATIBLE TRUE)
else(NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION)
set(PACKAGE_VERSION_UNSUITABLE TRUE)
endif(NOT PACKAGE_FIND_VERSION VERSION_GREATER PACKAGE_VERSION)
# if the installed or the using project don't have CMAKE_SIZEOF_VOID_P set, ignore it:
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "" OR "@CMAKE_SIZEOF_VOID_P@" STREQUAL "")
return()
endif()
if(NOT CMAKE_SIZEOF_VOID_P STREQUAL "@CMAKE_SIZEOF_VOID_P@")
math(EXPR installedBits "8 * 8")
set(PACKAGE_VERSION "${PACKAGE_VERSION} (${installedBits}bit)")
set(PACKAGE_VERSION_UNSUITABLE TRUE)
endif()

68
cmake/raylib-config.cmake Normal file
View File

@ -0,0 +1,68 @@
# - Try to find raylib
# Options:
# raylib_USE_STATIC_LIBS - OFF by default
# raylib_VERBOSE - OFF by default
# Once done, this defines a raylib target that can be passed to
# target_link_libraries as well as following variables:
#
# raylib_FOUND - System has raylib installed
# raylib_INCLUDE_DIRS - The include directories for the raylib header(s)
# raylib_LIBRARIES - The libraries needed to use raylib
# raylib_LDFLAGS - The linker flags needed with raylib
# raylib_DEFINITIONS - Compiler switches required for using raylib
set(XPREFIX PC_RAYLIB)
if (raylib_USE_STATIC_LIBS)
set(XPREFIX ${XPREFIX}_STATIC)
endif()
find_package(PkgConfig)
pkg_check_modules(${XPREFIX} REQUIRED raylib)
set(raylib_DEFINITIONS ${${XPREFIX}_CFLAGS})
find_path(raylib_INCLUDE_DIR
NAMES raylib.h
HINTS ${${XPREFIX}_INCLUDE_DIRS}
)
find_library(raylib_LIBRARY
NAMES raylib
HINTS ${${XPREFIX}_LIBRARY_DIRS}
)
set(raylib_LIBRARIES ${raylib_LIBRARY})
set(raylib_LIBRARY_DIRS ${${XPREFIX}_LIBRARY_DIRS})
set(raylib_LIBRARY_DIR ${raylib_LIBRARY_DIRS})
set(raylib_INCLUDE_DIRS ${raylib_INCLUDE_DIR})
set(raylib_LDFLAGS ${${XPREFIX}_LDFLAGS})
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(raylib DEFAULT_MSG
raylib_LIBRARY
raylib_INCLUDE_DIR
)
mark_as_advanced(raylib_LIBRARY raylib_INCLUDE_DIR)
if (raylib_USE_STATIC_LIBS)
add_library(raylib STATIC IMPORTED GLOBAL)
else()
add_library(raylib SHARED IMPORTED GLOBAL)
endif()
string (REPLACE ";" " " raylib_LDFLAGS "${raylib_LDFLAGS}")
set_target_properties(raylib
PROPERTIES
IMPORTED_LOCATION "${raylib_LIBRARIES}"
INTERFACE_INCLUDE_DIRECTORIES "${raylib_INCLUDE_DIRS}"
INTERFACE_LINK_LIBRARIES "${raylib_LDFLAGS}"
INTERFACE_COMPILE_OPTIONS "${raylib_DEFINITIONS}"
)
if (raylib_VERBOSE)
message(STATUS "raylib_FOUND: ${raylib_FOUND}")
message(STATUS "raylib_INCLUDE_DIRS: ${raylib_INCLUDE_DIRS}")
message(STATUS "raylib_LIBRARIES: ${raylib_LIBRARIES}")
message(STATUS "raylib_LDFLAGS: ${raylib_LDFLAGS}")
message(STATUS "raylib_DEFINITIONS: ${raylib_DEFINITIONS}")
endif()

View File

@ -1,88 +0,0 @@
# All sorts of things that we need cross project
cmake_minimum_required(VERSION 2.8.0)
add_definitions("-DRAYLIB_CMAKE=1")
if (${USE_OPENAL_BACKEND})
find_package(OpenAL REQUIRED)
endif()
if(${PLATFORM} MATCHES "Android")
find_library(OPENGL_LIBRARY OpenGL)
set(LIBS_PRIVATE m log android EGL GLESv2 OpenSLES atomic c)
elseif(${PLATFORM} MATCHES "Web")
elseif(APPLE)
find_library(OPENGL_LIBRARY OpenGL)
set(LIBS_PRIVATE ${OPENGL_LIBRARY})
elseif(WIN32)
# no pkg-config --static on Windows yet...
else()
find_library(pthread NAMES pthread)
find_package(OpenGL QUIET)
if ("${OPENGL_LIBRARIES}" STREQUAL "")
set(OPENGL_LIBRARIES "GL")
endif()
if ("${CMAKE_SYSTEM_NAME}" MATCHES "(Net|Open)BSD")
find_library(OSS_LIBRARY ossaudio)
endif()
set(LIBS_PRIVATE m pthread ${OPENGL_LIBRARIES} ${OSS_LIBRARY})
endif()
include_directories(${OPENGL_INCLUDE_DIR} ${OPENAL_INCLUDE_DIR})
set(LIBS_PRIVATE ${LIBS_PRIVATE} ${OPENAL_LIBRARY})
if(${PLATFORM} MATCHES "Desktop")
if(USE_EXTERNAL_GLFW STREQUAL "ON")
find_package(glfw3 3.2.1 REQUIRED)
elseif(USE_EXTERNAL_GLFW STREQUAL "IF_POSSIBLE")
find_package(glfw3 3.2.1 QUIET)
endif()
if (glfw3_FOUND)
set(LIBS_PRIVATE ${LIBS_PRIVATE} glfw)
endif()
endif()
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
set(LINUX TRUE)
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)
# Link raylib
if (TARGET raylib_shared)
target_link_libraries(${executable} raylib_shared)
elseif(${PLATFORM} MATCHES "Web")
target_link_libraries(${executable} ${__PKG_CONFIG_LIBS_PRIVATE})
target_link_libraries(${executable} raylib)
else()
target_link_libraries(${executable} raylib ${__PKG_CONFIG_LIBS_PRIVATE})
endif()
endfunction()

View File

@ -1,15 +1,6 @@
# Setup the project and settings
project(examples)
include("../cmake/utils.cmake")
# Make sure raylib has been built
# TODO `build` directory should maybe be something else...
# TODO place somewhere else?
include_directories("../build/release")
include_directories("../src/external")
include_directories("../src/external/glfw/include")
# Get the sources together
set(example_dirs audio core models others shaders shapes text textures)
set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=199309L)
@ -64,6 +55,9 @@ elseif(${PLATFORM} MATCHES "Web")
set(OUTPUT_EXT ".html")
endif()
if (NOT TARGET raylib)
find_package(raylib 2.0 REQUIRED)
endif()
# Do each example
foreach(example_source ${example_sources})
@ -75,7 +69,7 @@ foreach(example_source ${example_sources})
add_executable(${example_name} ${example_source})
# Link the libraries
link_libraries_to_executable(${example_name})
target_link_libraries(${example_name} raylib)
endforeach()
# Copy all of the resource files to the destination

View File

@ -1,17 +1,9 @@
# Setup the project and settings
project(games)
include("../cmake/utils.cmake")
# Make sure raylib has been built
# TODO `build` directory should maybe be something else...
# TODO place somewhere else?
include_directories("../build/release")
# Get the source toegher
file(GLOB sources *.c)
message("PLATFORM = ${PLATFORM}")
set(OUTPUT_EXT)
if(${PLATFORM} MATCHES "Web")
@ -20,18 +12,21 @@ if(${PLATFORM} MATCHES "Web")
set(OUTPUT_EXT ".html")
endif()
if (NOT TARGET raylib)
find_package(raylib 2.0 REQUIRED)
endif()
# Do each game
foreach(game_source ${sources})
# Create the basename for the game
get_filename_component(game_name ${game_source} NAME)
string(REPLACE ".c" "${OUTPUT_EXT}" game_name ${game_name})
# Setup the game
add_executable(${game_name} ${game_source})
# Link the libraries
link_libraries_to_executable(${game_name})
target_link_libraries(${game_name} raylib)
endforeach()
# Do the games with subdirectories

View File

@ -1,18 +1,13 @@
# Setup the project and settings
cmake_minimum_required(VERSION 2.6)
project(drturtle)
include("../../cmake/utils.cmake")
# Make sure raylib has been built
# TODO `build` directory should maybe be something else...
include_directories("../../build/release")
# Executable & linking
add_executable(drturtle 06_drturtle_final.c)
link_libraries_to_executable(drturtle)
add_executable(${PROJECT_NAME} 06_drturtle_final.c)
if (NOT TARGET raylib)
find_package(raylib 2.0 REQUIRED)
endif()
target_link_libraries(${PROJECT_NAME} raylib)
# Resources
# Copy all of the resource files to the destination
file(COPY "resources/" DESTINATION "resources/")

View File

@ -1,21 +1,16 @@
# Setup the project and settings
cmake_minimum_required(VERSION 2.6)
project(just_do)
include("../../cmake/utils.cmake")
# Make sure raylib has been built
# TODO `build` directory should maybe be something else...
include_directories("../../build/release")
# Grab the screens
file(GLOB screen_sources "screens/*.c")
# Executable & linking
add_executable(just_do just_do.c ${screen_sources})
link_libraries_to_executable(just_do)
add_executable(${PROJECT_NAME} ${PROJECT_NAME}.c ${screen_sources})
if (NOT TARGET raylib)
find_package(raylib 2.0 REQUIRED)
endif()
target_link_libraries(${PROJECT_NAME} raylib)
# Resources
# Copy all of the resource files to the destination
file(COPY "resources/" DESTINATION "resources/")

View File

@ -1,21 +1,16 @@
# Setup the project and settings
cmake_minimum_required(VERSION 2.6)
project(koala_seasons)
include("../../cmake/utils.cmake")
# Make sure raylib has been built
# TODO `build` directory should maybe be something else...
include_directories("../../build/release")
# Grab the screens
file(GLOB screen_sources "screens/*.c")
# Executable & linking
add_executable(koala_seasons koala_seasons.c ${screen_sources})
link_libraries_to_executable(koala_seasons)
add_executable(${PROJECT_NAME} ${PROJECT_NAME}.c ${screen_sources})
if (NOT TARGET raylib)
find_package(raylib 2.0 REQUIRED)
endif()
target_link_libraries(${PROJECT_NAME} raylib)
# Resources
# Copy all of the resource files to the destination
file(COPY "resources/" DESTINATION "resources/")

View File

@ -1,21 +1,16 @@
# Setup the project and settings
cmake_minimum_required(VERSION 2.6)
project(light_my_ritual)
include("../../cmake/utils.cmake")
# Make sure raylib has been built
# TODO `build` directory should maybe be something else...
include_directories("../../build/release")
# Grab the screens
file(GLOB screen_sources "screens/*.c")
# Executable & linking
add_executable(light_my_ritual light_my_ritual.c ${screen_sources})
link_libraries_to_executable(light_my_ritual)
add_executable(${PROJECT_NAME} ${PROJECT_NAME}.c ${screen_sources})
if (NOT TARGET raylib)
find_package(raylib 2.0 REQUIRED)
endif()
target_link_libraries(${PROJECT_NAME} raylib)
# Resources
# Copy all of the resource files to the destination
file(COPY "resources/" DESTINATION "resources/")

View File

@ -1,21 +1,16 @@
# Setup the project and settings
cmake_minimum_required(VERSION 2.6)
project(skully_escape)
include("../../cmake/utils.cmake")
# Make sure raylib has been built
# TODO `build` directory should maybe be something else...
include_directories("../../build/release")
# Grab the screens
file(GLOB screen_sources "screens/*.c")
# Executable & linking
add_executable(skully_escape skully_escape.c player.c monster.c ${screen_sources})
link_libraries_to_executable(skully_escape)
add_executable(${PROJECT_NAME} skully_escape.c player.c monster.c ${screen_sources})
if (NOT TARGET raylib)
find_package(raylib 2.0 REQUIRED)
endif()
target_link_libraries(${PROJECT_NAME} raylib)
# Resources
# Copy all of the resource files to the destination
file(COPY "resources/" DESTINATION "resources/")

View File

@ -1,21 +1,16 @@
# Setup the project and settings
cmake_minimum_required(VERSION 2.6)
project(wave_collector)
include("../../cmake/utils.cmake")
# Make sure raylib has been built
# TODO `build` directory should maybe be something else...
include_directories("../../build/release")
# Grab the screens
file(GLOB screen_sources "screens/*.c")
# Executable & linking
add_executable(wave_collector wave_collector.c ${screen_sources})
link_libraries_to_executable(wave_collector)
add_executable(${PROJECT_NAME} ${PROJECT_NAME}.c ${screen_sources})
if (NOT TARGET raylib)
find_package(raylib 2.0 REQUIRED)
endif()
target_link_libraries(${PROJECT_NAME} raylib)
# Resources
# Copy all of the resource files to the destination
file(COPY "resources/" DESTINATION "resources/")

View File

@ -5,20 +5,28 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
set(PROJECT_VERSION 2.0.0)
set(API_VERSION 2)
set(RAYLIB raylib) # Name of the generated library
include("CMakeOptions.txt")
include(BuildType)
configure_file(config.h.in ${CMAKE_BINARY_DIR}/cmake/config.h)
include_directories(${CMAKE_BINARY_DIR})
include_directories(SYSTEM .)
include_directories(${CMAKE_BINARY_DIR} .)
# Get the sources together
file(GLOB raylib_sources *.c)
list(REMOVE_ITEM raylib_sources ${CMAKE_CURRENT_SOURCE_DIR}/rglfw.c)
if(USE_EXTERNAL_GLFW STREQUAL "ON")
find_package(glfw3 3.2.1 REQUIRED)
elseif(USE_EXTERNAL_GLFW STREQUAL "IF_POSSIBLE")
find_package(glfw3 3.2.1 QUIET)
endif()
if (glfw3_FOUND)
set(LIBS_PRIVATE ${LIBS_PRIVATE} glfw)
endif()
# Explicitly check against "ON", because USE_EXTERNAL_GLFW is a tristate option
if(NOT glfw3_FOUND AND NOT USE_EXTERNAL_GLFW STREQUAL "ON" AND "${PLATFORM}" MATCHES "Desktop")
MESSAGE(STATUS "Using raylib's GLFW")
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
@ -30,20 +38,22 @@ if(NOT glfw3_FOUND AND NOT USE_EXTERNAL_GLFW STREQUAL "ON" AND "${PLATFORM}" MAT
endif()
add_subdirectory(external/glfw)
include_directories(external/glfw/include)
include_directories(BEFORE SYSTEM external/glfw/include)
list(APPEND raylib_sources $<TARGET_OBJECTS:glfw_objlib>)
else()
MESSAGE(STATUS "Using external GLFW")
set(GLFW_PKG_DEPS glfw)
endif()
include(utils)
add_definitions("-DRAYLIB_CMAKE=1")
if(USE_AUDIO)
if (NOT USE_OPENAL_BACKEND)
file(GLOB mini_al external/mini_al.c)
MESSAGE(STATUS "Audio Backend: mini_al")
else()
find_package(OpenAL REQUIRED)
MESSAGE(STATUS "Audio Backend: OpenAL")
endif()
file(GLOB stb_vorbis external/stb_vorbis.c)
@ -55,35 +65,36 @@ else()
set(sources ${raylib_sources})
endif()
include(AddIfFlagCompiles)
### Config options ###
# Translate the config options to what raylib wants
if(${PLATFORM} MATCHES "Desktop")
set(PLATFORM "PLATFORM_DESKTOP")
# OpenGL version
if (${OPENGL_VERSION} MATCHES "3.3")
set(GRAPHICS "GRAPHICS_API_OPENGL_33")
elseif (${OPENGL_VERSION} MATCHES "2.1")
set(GRAPHICS "GRAPHICS_API_OPENGL_21")
elseif (${OPENGL_VERSION} MATCHES "1.1")
set(GRAPHICS "GRAPHICS_API_OPENGL_11")
elseif (${OPENGL_VERSION} MATCHES "ES 2.0")
set(GRAPHICS "GRAPHICS_API_OPENGL_ES2")
endif()
set(PLATFORM_CPP "PLATFORM_DESKTOP")
if(APPLE)
# Need to force OpenGL 3.3 on OS X
# See: https://github.com/raysan5/raylib/issues/341
set(GRAPHICS "GRAPHICS_API_OPENGL_33")
find_library(OPENGL_LIBRARY OpenGL)
set(LIBS_PRIVATE ${OPENGL_LIBRARY})
link_libraries("${LIBS_PRIVATE}")
elseif(WIN32)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
else()
find_library(pthread NAMES pthread)
find_package(OpenGL QUIET)
if ("${OPENGL_LIBRARIES}" STREQUAL "")
set(OPENGL_LIBRARIES "GL")
endif()
if ("${CMAKE_SYSTEM_NAME}" MATCHES "(Net|Open)BSD")
find_library(OSS_LIBRARY ossaudio)
endif()
set(LIBS_PRIVATE m pthread ${OPENGL_LIBRARIES} ${OSS_LIBRARY})
endif()
elseif(${PLATFORM} MATCHES "Web")
set(PLATFORM "PLATFORM_WEB")
set(PLATFORM_CPP "PLATFORM_WEB")
set(GRAPHICS "GRAPHICS_API_OPENGL_ES2")
set(CMAKE_C_FLAGS "-s USE_GLFW=3 -s ASSERTIONS=1 --profiling")
@ -91,8 +102,9 @@ elseif(${PLATFORM} MATCHES "Web")
# Change the name of the output library
elseif(${PLATFORM} MATCHES "Android")
set(PLATFORM "PLATFORM_ANDROID")
set(PLATFORM_CPP "PLATFORM_ANDROID")
set(GRAPHICS "GRAPHICS_API_OPENGL_ES2")
include(AddIfFlagCompiles)
add_if_flag_compiles(-ffunction-sections CMAKE_C_FLAGS)
add_if_flag_compiles(-funwind-tables CMAKE_C_FLAGS)
add_if_flag_compiles(-fstack-protector-strong CMAKE_C_FLAGS)
@ -103,94 +115,139 @@ elseif(${PLATFORM} MATCHES "Android")
include_directories(external/android/native_app_glue)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--exclude-libs,libatomic.a -Wl,--build-id -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--warn-shared-textrel -Wl,--fatal-warnings -uANativeActivity_onCreate")
find_library(OPENGL_LIBRARY OpenGL)
set(LIBS_PRIVATE m log android EGL GLESv2 OpenSLES atomic c)
elseif(${PLATFORM} MATCHES "Raspberry Pi")
set(PLATFORM "PLATFORM_RPI")
set(PLATFORM_CPP "PLATFORM_RPI")
set(GRAPHICS "GRAPHICS_API_OPENGL_ES2")
endif()
if(${SHARED})
add_library(${RAYLIB}_shared SHARED ${sources})
target_compile_definitions(${RAYLIB}_shared
PUBLIC ${PLATFORM}
PUBLIC ${GRAPHICS}
)
set(PKG_CONFIG_LIBS_EXTRA "")
set_property(TARGET ${RAYLIB}_shared PROPERTY POSITION_INDEPENDENT_CODE ON)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_MACOSX_RPATH ON)
target_link_libraries(${RAYLIB}_shared ${LIBS_PRIVATE})
if (${PLATFORM} MATCHES "PLATFORM_DESKTOP")
target_link_libraries(${RAYLIB}_shared glfw)
if (${OPENGL_VERSION})
set(${SUGGESTED_GRAPHICS} "${GRAPHICS}")
if (${OPENGL_VERSION} MATCHES "3.3")
set(GRAPHICS "GRAPHICS_API_OPENGL_33")
elseif (${OPENGL_VERSION} MATCHES "2.1")
set(GRAPHICS "GRAPHICS_API_OPENGL_21")
elseif (${OPENGL_VERSION} MATCHES "1.1")
set(GRAPHICS "GRAPHICS_API_OPENGL_11")
elseif (${OPENGL_VERSION} MATCHES "ES 2.0")
set(GRAPHICS "GRAPHICS_API_OPENGL_ES2")
endif()
if (UNIX AND ${FILESYSTEM_LACKS_SYMLINKS})
MESSAGE(WARNING "Can't version UNIX shared library on file system without symlink support")
else()
set_target_properties(${RAYLIB}_shared PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${API_VERSION}
)
endif()
set_target_properties(${RAYLIB}_shared PROPERTIES
PUBLIC_HEADER "raylib.h"
)
if(WIN32)
install(
TARGETS ${RAYLIB}_shared
RUNTIME DESTINATION lib
PUBLIC_HEADER DESTINATION include
)
else() # Keep lib*.(a|dll) name, but avoid *.lib files overwriting each other on Windows
set_target_properties(${RAYLIB}_shared PROPERTIES OUTPUT_NAME ${RAYLIB})
install(
TARGETS ${RAYLIB}_shared
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
if (${SUGGESTED_GRAPHICS} AND NOT "${SUGGESTED_GRAPHICS}" STREQUAL "${GRAPHICS}")
message(WARNING "You are overriding the suggested GRAPHICS=${SUGGESTED_GRAPHICS} with ${GRAPHICS}! This may fail")
endif()
endif()
add_test("pkg-config" ${PROJECT_SOURCE_DIR}/../cmake/test-pkgconfig.sh)
endif(${SHARED})
if(NOT GRAPHICS)
set(GRAPHICS "GRAPHICS_API_OPENGL_33")
endif()
if(${STATIC})
if(${PLATFORM} MATCHES "PLATFORM_WEB")
include_directories(${OPENGL_INCLUDE_DIR} ${OPENAL_INCLUDE_DIR})
set(LIBS_PRIVATE ${LIBS_PRIVATE} ${OPENAL_LIBRARY})
include(LibraryPathToLinkerFlags)
library_path_to_linker_flags(__PKG_CONFIG_LIBS_PRIVATE "${LIBS_PRIVATE}")
if(STATIC)
if(${PLATFORM} MATCHES "Web")
set(CMAKE_STATIC_LIBRARY_SUFFIX ".bc")
endif()
add_library(${RAYLIB} STATIC ${sources})
add_library(raylib_static STATIC ${sources})
target_compile_definitions(${RAYLIB}
PUBLIC ${PLATFORM}
target_compile_definitions(raylib_static
PUBLIC ${PLATFORM_CPP}
PUBLIC ${GRAPHICS}
)
set(PKG_CONFIG_LIBS_PRIVATE ${__PKG_CONFIG_LIBS_PRIVATE} ${GLFW_PKG_LIBS})
string (REPLACE ";" " " PKG_CONFIG_LIBS_PRIVATE "${PKG_CONFIG_LIBS_PRIVATE}")
if (${PLATFORM} MATCHES "PLATFORM_DESKTOP")
target_link_libraries(${RAYLIB} glfw ${GLFW_LIBRARIES})
if (${PLATFORM} MATCHES "Desktop")
target_link_libraries(raylib_static glfw ${GLFW_LIBRARIES} ${LIBS_PRIVATE})
endif()
if (WITH_PIC)
set_property(TARGET ${RAYLIB} PROPERTY POSITION_INDEPENDENT_CODE ON)
set_property(TARGET raylib_static PROPERTY POSITION_INDEPENDENT_CODE ON)
endif()
set_target_properties(${RAYLIB} PROPERTIES PUBLIC_HEADER "raylib.h")
install(TARGETS ${RAYLIB}
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
set_target_properties(raylib_static PROPERTIES PUBLIC_HEADER "raylib.h")
if(NOT WIN32) # Keep lib*.(a|dll) name, but avoid *.lib files overwriting each other on Windows
set_target_properties(raylib_static PROPERTIES OUTPUT_NAME raylib)
endif()
install(
TARGETS raylib_static
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
set_target_properties(raylib_static PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_BINARY_DIR}/release")
add_test("pkg-config--static" ${PROJECT_SOURCE_DIR}/../cmake/test-pkgconfig.sh --static)
endif(${STATIC})
endif(STATIC)
if(SHARED)
add_library(raylib SHARED ${sources})
target_compile_definitions(raylib
PUBLIC ${PLATFORM_CPP}
PUBLIC ${GRAPHICS}
)
set(PKG_CONFIG_LIBS_EXTRA "")
set_property(TARGET raylib PROPERTY POSITION_INDEPENDENT_CODE ON)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_MACOSX_RPATH ON)
target_link_libraries(raylib ${LIBS_PRIVATE})
if (${PLATFORM} MATCHES "Desktop")
target_link_libraries(raylib glfw)
endif()
if (UNIX AND ${FILESYSTEM_LACKS_SYMLINKS})
MESSAGE(WARNING "Can't version UNIX shared library on file system without symlink support")
else()
set_target_properties(raylib PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${API_VERSION}
)
endif()
set_target_properties(raylib PROPERTIES
PUBLIC_HEADER "raylib.h"
)
if (WIN32)
install(
TARGETS raylib
RUNTIME DESTINATION "lib"
PUBLIC_HEADER DESTINATION "include"
)
else()
install(
TARGETS raylib
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)
endif()
set_target_properties(raylib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_BINARY_DIR}/release")
add_test("pkg-config" ${PROJECT_SOURCE_DIR}/../cmake/test-pkgconfig.sh)
else(SHARED)
add_library(raylib ALIAS raylib_static)
endif(SHARED)
if (NOT DEFINED PKG_CONFIG_LIBS_EXTRA)
set(PKG_CONFIG_LIBS_EXTRA "${PKG_CONFIG_LIBS_PRIVATE}")
endif()
configure_file(../raylib.pc.in raylib.pc @ONLY)
install(FILES ${CMAKE_BINARY_DIR}/release/raylib.pc DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
configure_file(../cmake/raylib-config-version.cmake raylib-config-version.cmake @ONLY)
install(FILES ${CMAKE_BINARY_DIR}/release/raylib-config-version.cmake DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/raylib")
install(FILES ${PROJECT_SOURCE_DIR}/../cmake/raylib-config.cmake DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/raylib")
# populates raylib_{FOUND, INCLUDE_DIRS, LIBRARIES, LDFLAGS, DEFINITIONS}
include(PopulateConfigVariablesLocally)
populate_config_variables_locally(raylib)
# Copy the header files to the build directory
file(COPY "raylib.h" DESTINATION ".")
@ -201,7 +258,7 @@ file(COPY "audio.h" DESTINATION ".")
# Print the flags for the user
message(STATUS "Compiling with the flags:")
message(STATUS " PLATFORM=" ${PLATFORM})
message(STATUS " PLATFORM=" ${PLATFORM_CPP})
message(STATUS " GRAPHICS=" ${GRAPHICS})
# Packaging

View File

@ -4,7 +4,7 @@ include(EnumOption)
enum_option(PLATFORM "Desktop;Web;Android;Raspberry Pi" "Platform to build for.")
enum_option(OPENGL_VERSION "3.3;2.1;1.1;ES 2.0" "OpenGL Version to build raylib with")
enum_option(OPENGL_VERSION "OFF;3.3;2.1;1.1;ES 2.0" "Force a specific OpenGL Version?")
# Shared library is always PIC. Static library should be PIC too if linked into a shared library
option(WITH_PIC "Compile static library as position-independent code" OFF)