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:
parent
3e5093eab0
commit
3f09726331
17 changed files with 321 additions and 264 deletions
24
cmake/LibraryPathToLinkerFlags.cmake
Normal file
24
cmake/LibraryPathToLinkerFlags.cmake
Normal 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()
|
11
cmake/PopulateConfigVariablesLocally.cmake
Normal file
11
cmake/PopulateConfigVariablesLocally.cmake
Normal 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()
|
21
cmake/raylib-config-version.cmake
Normal file
21
cmake/raylib-config-version.cmake
Normal 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
68
cmake/raylib-config.cmake
Normal 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()
|
|
@ -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()
|
Loading…
Add table
Add a link
Reference in a new issue