Discussion:
[SDL] [PATCH] Export libraries with CMake
David Demelier
2017-01-27 10:40:25 UTC
Permalink
# HG changeset patch
# User David Demelier <***@gmail.com>
# Date 1485512982 -3600
# Fri Jan 27 11:29:42 2017 +0100
# Node ID 7c72c6e972816750131cde1324fe1ac1b1bc0e0d
# Parent ca523d8f1af2a197289c330833a8c602bf1c55a9
Export SDL2 libraries through CMake configuration packages

This patch export the SDL2 libraries using CMake configuration packages as
defined in the cmake-packages guide [0].

The purpose of that configuration packages is to provide convenient and much
safer packages than own made. They also offer imported targets which add
include directories, compile definition, transient libraries and such.

Adding that support will help developers importing SDL2 in their project and
make future use of CMake in SDL_net, mixer, ttf, rtf and image.

Sample project that compiles out of the box on Linux:

cmake_minimum_required(VERSION 3.7)
project(sample)
find_package(SDL2 2.0.5 REQUIRED)
add_executable(main main.c)
target_link_libraries(main SDL2::SDL2)

[0]:
https://cmake.org/cmake/help/v3.7/manual/cmake-packages.7.html#creating-packages

diff -r ca523d8f1af2 -r 7c72c6e97281 CMakeLists.txt
--- a/CMakeLists.txt Wed Jan 25 22:24:54 2017 -0800
+++ b/CMakeLists.txt Fri Jan 27 11:29:42 2017 +0100
@@ -1585,6 +1585,7 @@
endif()
set(_INSTALL_LIBS "SDL2" ${_INSTALL_LIBS})
target_link_libraries(SDL2 ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
+ target_include_directories(SDL2 PUBLIC $<INSTALL_INTERFACE:include/SDL2>)
endif()

if(SDL_STATIC)
@@ -1601,14 +1602,43 @@
# libraries - do we need to consider this?
set(_INSTALL_LIBS "SDL2-static" ${_INSTALL_LIBS})
target_link_libraries(SDL2-static ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
+ target_include_directories(SDL2-static PUBLIC
$<INSTALL_INTERFACE:include/SDL2>)
endif()

##### Installation targets #####
-install(TARGETS ${_INSTALL_LIBS}
+install(TARGETS ${_INSTALL_LIBS} EXPORT SDL2Targets
LIBRARY DESTINATION "lib${LIB_SUFFIX}"
ARCHIVE DESTINATION "lib${LIB_SUFFIX}"
RUNTIME DESTINATION bin)

+##### Export files #####
+if (APPLE)
+ set(PKG_PREFIX "SDL2.framework/Resources")
+elseif (WINDOWS)
+ set(PKG_PREFIX "cmake/SDL2")
+else ()
+ set(PKG_PREFIX "lib/cmake/SDL2")
+endif ()
+
+include(CMakePackageConfigHelpers)
+write_basic_package_version_file("${CMAKE_BINARY_DIR}/SDL2ConfigVersion.cmake"
+ VERSION ${SDL_VERSION}
+ COMPATIBILITY AnyNewerVersion
+)
+
+install(EXPORT SDL2Targets
+ FILE SDL2Targets.cmake
+ NAMESPACE SDL2::
+ DESTINATION ${PKG_PREFIX}
+)
+install(
+ FILES
+ ${CMAKE_SOURCE_DIR}/SDL2Config.cmake
+ ${CMAKE_BINARY_DIR}/SDL2ConfigVersion.cmake
+ DESTINATION ${PKG_PREFIX}
+ COMPONENT Devel
+)
+
file(GLOB INCLUDE_FILES ${SDL2_SOURCE_DIR}/include/*.h)
file(GLOB BIN_INCLUDE_FILES ${SDL2_BINARY_DIR}/include/*.h)
foreach(_FNAME ${BIN_INCLUDE_FILES})
diff -r ca523d8f1af2 -r 7c72c6e97281 SDL2Config.cmake
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SDL2Config.cmake Fri Jan 27 11:29:42 2017 +0100
@@ -0,0 +1,1 @@
+include("${CMAKE_CURRENT_LIST_DIR}/SDL2Targets.cmake")

Loading...