Skip to content

Commit 496337b

Browse files
committed
Fixed bug 3651 - CMake build does not install CMake package configuration
[email protected] Most ironically, although autoconf/automake-based builds install (pretty half-assed) CMake package configuration files, they're missing in installations resulting from CMake-based builds entirely. A proper configuration file typically also loads target exports (implemented in patch 3572, also fixing this issue - see my comment on that issue for details). I believe it would be best to let the dinosaurs go extinct and redirect all build efforts to the CMake end for two reasons: 1. It potentially provides the best user experience, but you'd have to give it some love and ship with less quirky buildfiles. 2. It would force distros to build SDL via CMake and thus would ensure target exports are actually available everywhere. Various CMake patches I submitted today in summary (directly converted from the HG commits and `am`d onto a fork of a git mirror that happened to be on `tip`). https://github.com/tschw/SDL/commits/patched Fixing libsdl-org#2576 libsdl-org#3572, libsdl-org#3613, and this fresh ticket, which is almost entirely advertisement ;). These already do to make SDL much less of a quirky fella to have in your dependency tree...
1 parent d3af447 commit 496337b

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

CMakeLists.txt

+48-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
22
message(FATAL_ERROR "Prevented in-tree built. Please create a build directory outside of the SDL source code and call cmake from there")
33
endif()
44

5-
cmake_minimum_required(VERSION 2.8.5)
5+
cmake_minimum_required(VERSION 2.8.11)
66
project(SDL2 C)
77

88
# !!! FIXME: this should probably do "MACOSX_RPATH ON" as a target property
@@ -46,6 +46,12 @@ set(SDL_INTERFACE_AGE 1)
4646
set(SDL_BINARY_AGE 5)
4747
set(SDL_VERSION "${SDL_MAJOR_VERSION}.${SDL_MINOR_VERSION}.${SDL_MICRO_VERSION}")
4848

49+
# Set defaults preventing destination file conflicts
50+
set(CMAKE_DEBUG_POSTFIX "d"
51+
CACHE STRING "Name suffix for debug builds")
52+
53+
mark_as_advanced(CMAKE_IMPORT_LIBRARY_SUFFIX CMAKE_DEBUG_POSTFIX)
54+
4955
# Calculate a libtool-like version number
5056
math(EXPR LT_CURRENT "${SDL_MICRO_VERSION} - ${SDL_INTERFACE_AGE}")
5157
math(EXPR LT_AGE "${SDL_BINARY_AGE} - ${SDL_INTERFACE_AGE}")
@@ -1093,9 +1099,9 @@ elseif(WINDOWS)
10931099

10941100
if(MSVC)
10951101
# Prevent codegen that would use the VC runtime libraries.
1096-
add_definitions(/GS-)
1102+
set_property(DIRECTORY . APPEND PROPERTY COMPILE_OPTIONS "/GS-")
10971103
if(NOT ARCH_64)
1098-
add_definitions(/arch:SSE)
1104+
set_property(DIRECTORY . APPEND PROPERTY COMPILE_OPTIONS "/arch:SSE")
10991105
endif()
11001106
endif()
11011107

@@ -1631,6 +1637,7 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}")
16311637

16321638
# Always build SDLmain
16331639
add_library(SDL2main STATIC ${SDLMAIN_SOURCES})
1640+
target_include_directories(SDL2main PUBLIC $<INSTALL_INTERFACE:include>)
16341641
set(_INSTALL_LIBS "SDL2main")
16351642

16361643
if(SDL_SHARED)
@@ -1654,12 +1661,19 @@ if(SDL_SHARED)
16541661
endif()
16551662
set(_INSTALL_LIBS "SDL2" ${_INSTALL_LIBS})
16561663
target_link_libraries(SDL2 ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
1664+
target_include_directories(SDL2 PUBLIC $<INSTALL_INTERFACE:include>)
16571665
endif()
16581666

16591667
if(SDL_STATIC)
16601668
set (BUILD_SHARED_LIBS FALSE)
16611669
add_library(SDL2-static STATIC ${SOURCE_FILES})
1662-
set_target_properties(SDL2-static PROPERTIES OUTPUT_NAME "SDL2")
1670+
if (NOT SDL_SHARED OR NOT WIN32)
1671+
set_target_properties(SDL2-static PROPERTIES OUTPUT_NAME "SDL2")
1672+
# Note: Apparently, OUTPUT_NAME must really be unique; even when
1673+
# CMAKE_IMPORT_LIBRARY_SUFFIX or the like are given. Otherwise
1674+
# the static build may race with the import lib and one will get
1675+
# clobbered, when the suffix is realized via subsequent rename.
1676+
endif()
16631677
set_target_properties(SDL2-static PROPERTIES POSITION_INDEPENDENT_CODE ${SDL_STATIC_PIC})
16641678
if(MSVC AND NOT LIBC)
16651679
set_target_properties(SDL2-static PROPERTIES LINK_FLAGS_RELEASE "/NODEFAULTLIB")
@@ -1670,14 +1684,43 @@ if(SDL_STATIC)
16701684
# libraries - do we need to consider this?
16711685
set(_INSTALL_LIBS "SDL2-static" ${_INSTALL_LIBS})
16721686
target_link_libraries(SDL2-static ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
1687+
target_include_directories(SDL2-static PUBLIC $<INSTALL_INTERFACE:include>)
16731688
endif()
16741689

16751690
##### Installation targets #####
1676-
install(TARGETS ${_INSTALL_LIBS}
1691+
install(TARGETS ${_INSTALL_LIBS} EXPORT SDL2Targets
16771692
LIBRARY DESTINATION "lib${LIB_SUFFIX}"
16781693
ARCHIVE DESTINATION "lib${LIB_SUFFIX}"
16791694
RUNTIME DESTINATION bin)
16801695

1696+
##### Export files #####
1697+
if (APPLE)
1698+
set(PKG_PREFIX "SDL2.framework/Resources")
1699+
elseif (WINDOWS)
1700+
set(PKG_PREFIX "cmake")
1701+
else ()
1702+
set(PKG_PREFIX "lib/cmake/SDL2")
1703+
endif ()
1704+
1705+
include(CMakePackageConfigHelpers)
1706+
write_basic_package_version_file("${CMAKE_BINARY_DIR}/SDL2ConfigVersion.cmake"
1707+
VERSION ${SDL_VERSION}
1708+
COMPATIBILITY AnyNewerVersion
1709+
)
1710+
1711+
install(EXPORT SDL2Targets
1712+
FILE SDL2Targets.cmake
1713+
NAMESPACE SDL2::
1714+
DESTINATION ${PKG_PREFIX}
1715+
)
1716+
install(
1717+
FILES
1718+
${CMAKE_SOURCE_DIR}/SDL2Config.cmake
1719+
${CMAKE_BINARY_DIR}/SDL2ConfigVersion.cmake
1720+
DESTINATION ${PKG_PREFIX}
1721+
COMPONENT Devel
1722+
)
1723+
16811724
file(GLOB INCLUDE_FILES ${SDL2_SOURCE_DIR}/include/*.h)
16821725
file(GLOB BIN_INCLUDE_FILES ${SDL2_BINARY_DIR}/include/*.h)
16831726
foreach(_FNAME ${BIN_INCLUDE_FILES})

Makefile.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ SDLTEST_OBJECTS = @SDLTEST_OBJECTS@
4444

4545
WAYLAND_SCANNER = @WAYLAND_SCANNER@
4646

47-
SRC_DIST = *.txt acinclude Android.mk autogen.sh android-project build-scripts cmake cmake_uninstall.cmake.in configure configure.in debian docs include Makefile.* sdl2-config.cmake.in sdl2-config.in sdl2.m4 sdl2.pc.in SDL2.spec.in src test VisualC.html VisualC VisualC-WinRT Xcode Xcode-iOS
47+
SRC_DIST = *.txt acinclude Android.mk autogen.sh android-project build-scripts cmake cmake_uninstall.cmake.in configure configure.in debian docs include Makefile.* sdl2-config.cmake.in sdl2-config.in sdl2.m4 sdl2.pc.in SDL2.spec.in SDL2Config.cmake src test VisualC.html VisualC VisualC-WinRT Xcode Xcode-iOS
4848
GEN_DIST = SDL2.spec
4949

5050
ifneq ($V,1)

SDL2Config.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include("${CMAKE_CURRENT_LIST_DIR}/SDL2Targets.cmake")

0 commit comments

Comments
 (0)