Suggestion: Add CMake CONFIG mode packaging

Hello everyone,

I want to suggest adding support for CONFIG mode packaging in the CMake
build system. Essentially it would give CMake users a simpler way to use
SDL2 within their project. Instead of having to manully add the files or
find and use a FindSDL2.cmake module, the user can use find_package(SDL2
CONFIG REQUIRED) and target_link_libraries( SDL2::SDL2-static) (or
any other SDL2 targets from the CMake build system) after installing SDL2
with CMake. If SDL2 was installed in a non-standard location the user can
simply set CMAKE_INSTALL_PREFIX and it will work fine.

I also included a patch which implements my suggestion.

Have a nice day,
Alex

patch

HG changeset patch

User Alex <@Alex_FL>

Date 1457298983 18000

Sun Mar 06 16:16:23 2016 -0500

Node ID cb9e95274b8eda7610366e1295dbc561fcd42f55

Parent 4cd67316b30889ed806dcda927474bf6b710915b

Added CONFIG mode packaging

diff -r 4cd67316b308 -r cb9e95274b8e CMakeLists.txt
— a/CMakeLists.txt Sat Mar 05 23:25:23 2016 -0500
+++ b/CMakeLists.txt Sun Mar 06 16:16:23 2016 -0500
@@ -1529,8 +1529,42 @@
target_link_libraries(SDL2-static ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
endif()

+##### Generate CMake config file #####
+include(CMakePackageConfigHelpers)+
+set(PROJECT_VERSION “${SDL_VERSION}”)
+set(generated_dir “${CMAKE_CURRENT_BINARY_DIR}/generated”)
+set(config_install_dir “lib/cmake/${PROJECT_NAME}”)
+
+set(version_config
"${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake")
+set(project_config “${generated_dir}/${PROJECT_NAME}Config.cmake”)
+set(targets_export_name “${PROJECT_NAME}Targets”)
+set(namespace “${PROJECT_NAME}::”)
+
+write_basic_package_version_file(

  • “${version_config}” COMPATIBILITY SameMajorVersion
    +)

+configure_package_config_file(

  • “cmake/Config.cmake.in”
  • “${project_config}”
  • INSTALL_DESTINATION “${config_install_dir}”
    +)

+install(

  • FILES “${project_config}” “${version_config}”
  • DESTINATION “${config_install_dir}”
    +)

+install(

  • EXPORT “${targets_export_name}”
  • NAMESPACE “${namespace}”
  • DESTINATION “${config_install_dir}”
    +)
Installation targets

install(TARGETS ${_INSTALL_LIBS}

  • EXPORT "${targets_export_name}"
    LIBRARY DESTINATION "lib${LIB_SUFFIX}"
    ARCHIVE DESTINATION "lib${LIB_SUFFIX}"
    RUNTIME DESTINATION bin)
    diff -r 4cd67316b308 -r cb9e95274b8e cmake/Config.cmake.in
    — /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/cmake/Config.cmake.in Sun Mar 06 16:16:23 2016 -0500
    @@ -0,0 +1,4 @@
  • at PACKAGE_INIT@

+include("${CMAKE_CURRENT_LIST_DIR}/@targets_export_name at .cmake")
+check_required_components("@PROJECT_NAME@")