Fixed bug 3651 - CMake build does not install CMake package configuration
authorSam Lantinga <slouken@libsdl.org>
Wed, 09 Aug 2017 19:03:10 -0700
changeset 11210c92070a96da5
parent 11209 1374cc1666db
child 11211 467898375b76
Fixed bug 3651 - CMake build does not install CMake package configuration

tschwinger@elitemail.org

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 #2576 #3572, #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...
CMakeLists.txt
Makefile.in
SDL2Config.cmake
     1.1 --- a/CMakeLists.txt	Wed Aug 09 18:47:33 2017 -0700
     1.2 +++ b/CMakeLists.txt	Wed Aug 09 19:03:10 2017 -0700
     1.3 @@ -2,7 +2,7 @@
     1.4    message(FATAL_ERROR "Prevented in-tree built. Please create a build directory outside of the SDL source code and call cmake from there")
     1.5  endif()
     1.6  
     1.7 -cmake_minimum_required(VERSION 2.8.5)
     1.8 +cmake_minimum_required(VERSION 2.8.11)
     1.9  project(SDL2 C)
    1.10  
    1.11  # !!! FIXME: this should probably do "MACOSX_RPATH ON" as a target property
    1.12 @@ -46,6 +46,12 @@
    1.13  set(SDL_BINARY_AGE 5)
    1.14  set(SDL_VERSION "${SDL_MAJOR_VERSION}.${SDL_MINOR_VERSION}.${SDL_MICRO_VERSION}")
    1.15  
    1.16 +# Set defaults preventing destination file conflicts
    1.17 +set(CMAKE_DEBUG_POSTFIX "d"
    1.18 +    CACHE STRING "Name suffix for debug builds")
    1.19 +
    1.20 +mark_as_advanced(CMAKE_IMPORT_LIBRARY_SUFFIX CMAKE_DEBUG_POSTFIX)
    1.21 +
    1.22  # Calculate a libtool-like version number
    1.23  math(EXPR LT_CURRENT "${SDL_MICRO_VERSION} - ${SDL_INTERFACE_AGE}")
    1.24  math(EXPR LT_AGE "${SDL_BINARY_AGE} - ${SDL_INTERFACE_AGE}")
    1.25 @@ -1093,9 +1099,9 @@
    1.26  
    1.27    if(MSVC)
    1.28      # Prevent codegen that would use the VC runtime libraries.
    1.29 -    add_definitions(/GS-)
    1.30 +    set_property(DIRECTORY . APPEND PROPERTY COMPILE_OPTIONS "/GS-")
    1.31      if(NOT ARCH_64)
    1.32 -      add_definitions(/arch:SSE)
    1.33 +      set_property(DIRECTORY . APPEND PROPERTY COMPILE_OPTIONS "/arch:SSE")
    1.34      endif()
    1.35    endif()
    1.36  
    1.37 @@ -1631,6 +1637,7 @@
    1.38  
    1.39  # Always build SDLmain
    1.40  add_library(SDL2main STATIC ${SDLMAIN_SOURCES})
    1.41 +target_include_directories(SDL2main PUBLIC $<INSTALL_INTERFACE:include>)
    1.42  set(_INSTALL_LIBS "SDL2main")
    1.43  
    1.44  if(SDL_SHARED)
    1.45 @@ -1654,12 +1661,19 @@
    1.46    endif()
    1.47    set(_INSTALL_LIBS "SDL2" ${_INSTALL_LIBS})
    1.48    target_link_libraries(SDL2 ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
    1.49 +  target_include_directories(SDL2 PUBLIC $<INSTALL_INTERFACE:include>)
    1.50  endif()
    1.51  
    1.52  if(SDL_STATIC)
    1.53    set (BUILD_SHARED_LIBS FALSE)
    1.54    add_library(SDL2-static STATIC ${SOURCE_FILES})
    1.55 -  set_target_properties(SDL2-static PROPERTIES OUTPUT_NAME "SDL2")
    1.56 +  if (NOT SDL_SHARED OR NOT WIN32)
    1.57 +    set_target_properties(SDL2-static PROPERTIES OUTPUT_NAME "SDL2")
    1.58 +    # Note: Apparently, OUTPUT_NAME must really be unique; even when
    1.59 +    # CMAKE_IMPORT_LIBRARY_SUFFIX or the like are given. Otherwise
    1.60 +    # the static build may race with the import lib and one will get
    1.61 +    # clobbered, when the suffix is realized via subsequent rename.
    1.62 +  endif()
    1.63    set_target_properties(SDL2-static PROPERTIES POSITION_INDEPENDENT_CODE ${SDL_STATIC_PIC})
    1.64    if(MSVC AND NOT LIBC)
    1.65      set_target_properties(SDL2-static PROPERTIES LINK_FLAGS_RELEASE "/NODEFAULTLIB")
    1.66 @@ -1670,14 +1684,43 @@
    1.67    # libraries - do we need to consider this?
    1.68    set(_INSTALL_LIBS "SDL2-static" ${_INSTALL_LIBS})
    1.69    target_link_libraries(SDL2-static ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
    1.70 +  target_include_directories(SDL2-static PUBLIC $<INSTALL_INTERFACE:include>)
    1.71  endif()
    1.72  
    1.73  ##### Installation targets #####
    1.74 -install(TARGETS ${_INSTALL_LIBS}
    1.75 +install(TARGETS ${_INSTALL_LIBS} EXPORT SDL2Targets
    1.76    LIBRARY DESTINATION "lib${LIB_SUFFIX}"
    1.77    ARCHIVE DESTINATION "lib${LIB_SUFFIX}"
    1.78    RUNTIME DESTINATION bin)
    1.79  
    1.80 +##### Export files #####
    1.81 +if (APPLE)
    1.82 +  set(PKG_PREFIX "SDL2.framework/Resources")
    1.83 +elseif (WINDOWS)
    1.84 +  set(PKG_PREFIX "cmake")
    1.85 +else ()
    1.86 +  set(PKG_PREFIX "lib/cmake/SDL2")
    1.87 +endif ()
    1.88 +
    1.89 +include(CMakePackageConfigHelpers)
    1.90 +write_basic_package_version_file("${CMAKE_BINARY_DIR}/SDL2ConfigVersion.cmake"
    1.91 +  VERSION ${SDL_VERSION}
    1.92 +  COMPATIBILITY AnyNewerVersion
    1.93 +)
    1.94 +
    1.95 +install(EXPORT SDL2Targets
    1.96 +  FILE SDL2Targets.cmake
    1.97 +  NAMESPACE SDL2::
    1.98 +  DESTINATION ${PKG_PREFIX}
    1.99 +)
   1.100 +install(
   1.101 +  FILES
   1.102 +    ${CMAKE_SOURCE_DIR}/SDL2Config.cmake
   1.103 +    ${CMAKE_BINARY_DIR}/SDL2ConfigVersion.cmake
   1.104 +  DESTINATION ${PKG_PREFIX}
   1.105 +  COMPONENT Devel
   1.106 +)
   1.107 +
   1.108  file(GLOB INCLUDE_FILES ${SDL2_SOURCE_DIR}/include/*.h)
   1.109  file(GLOB BIN_INCLUDE_FILES ${SDL2_BINARY_DIR}/include/*.h)
   1.110  foreach(_FNAME ${BIN_INCLUDE_FILES})
     2.1 --- a/Makefile.in	Wed Aug 09 18:47:33 2017 -0700
     2.2 +++ b/Makefile.in	Wed Aug 09 19:03:10 2017 -0700
     2.3 @@ -44,7 +44,7 @@
     2.4  
     2.5  WAYLAND_SCANNER = @WAYLAND_SCANNER@
     2.6  
     2.7 -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
     2.8 +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
     2.9  GEN_DIST = SDL2.spec
    2.10  
    2.11  ifneq ($V,1)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/SDL2Config.cmake	Wed Aug 09 19:03:10 2017 -0700
     3.3 @@ -0,0 +1,1 @@
     3.4 +include("${CMAKE_CURRENT_LIST_DIR}/SDL2Targets.cmake")