sdl2-config.cmake.in
author James Le Cuirot
Sat, 11 Apr 2020 23:38:34 +0100
changeset 13729 89a457159af4
parent 13683 60f1e9fb5957
permissions -rw-r--r--
build: Merge pkg-config Libs.private into Libs for static-only builds

A project being built entirely statically will call pkg-config with
--static, which utilises the Libs.private field. Conversely it will
not use --static when not being built entirely statically, even if
there is only a static build of SDL available. This will most likely
cause the build to fail due to underlinking unless we merge the Libs
fields.

This is what the Meson build system does when it generates pkg-config
files. This also also follows the behaviour of sdl2-config.

At the same time, the runtime linker flags are not applicable to
static-only builds so only add them for shared builds.
slouken@9785
     1
# sdl2 cmake project-config input for ./configure scripts
slouken@9785
     2
slouken@9785
     3
set(prefix "@prefix@") 
slouken@9785
     4
set(exec_prefix "@exec_prefix@")
icculus@9978
     5
set(libdir "@libdir@")
slouken@9785
     6
set(SDL2_PREFIX "@prefix@")
slouken@9785
     7
set(SDL2_EXEC_PREFIX "@prefix@")
slouken@9785
     8
set(SDL2_LIBDIR "@libdir@")
slouken@9785
     9
set(SDL2_INCLUDE_DIRS "@includedir@/SDL2")
slouken@9785
    10
set(SDL2_LIBRARIES "-L${SDL2_LIBDIR} @SDL_RLD_FLAGS@ @SDL_LIBS@")
slouken@10474
    11
string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)
icculus@13543
    12
icculus@13543
    13
if(NOT TARGET SDL2::SDL2)
icculus@13543
    14
  # Remove -lSDL2 as that is handled by CMake, note the space at the end so it does not replace e.g. -lSDL2main
icculus@13543
    15
  # This may require "libdir" beeing set (from above)
icculus@13543
    16
  string(REPLACE "-lSDL2 " "" SDL2_EXTRA_LINK_FLAGS "@SDL_RLD_FLAGS@ @SDL_LIBS@ ")
icculus@13543
    17
  string(STRIP "${SDL2_EXTRA_LINK_FLAGS}" SDL2_EXTRA_LINK_FLAGS)
chewi@13729
    18
  string(REPLACE "-lSDL2 " "" SDL2_EXTRA_LINK_FLAGS_STATIC "@SDL_STATIC_LIBS@ ")
icculus@13543
    19
  string(STRIP "${SDL2_EXTRA_LINK_FLAGS_STATIC}" SDL2_EXTRA_LINK_FLAGS_STATIC)
icculus@13543
    20
icculus@13543
    21
  add_library(SDL2::SDL2 SHARED IMPORTED)
icculus@13543
    22
  set_target_properties(SDL2::SDL2 PROPERTIES
icculus@13543
    23
    INTERFACE_INCLUDE_DIRECTORIES "@includedir@/SDL2"
icculus@13543
    24
    IMPORTED_LINK_INTERFACE_LANGUAGES "C"
git@13683
    25
    IMPORTED_LOCATION "@libdir@/${CMAKE_SHARED_LIBRARY_PREFIX}SDL2${CMAKE_SHARED_LIBRARY_SUFFIX}"
icculus@13543
    26
    INTERFACE_LINK_LIBRARIES "${SDL2_EXTRA_LINK_FLAGS}")
icculus@13543
    27
icculus@13543
    28
  add_library(SDL2::SDL2-static STATIC IMPORTED)
icculus@13543
    29
  set_target_properties(SDL2::SDL2-static PROPERTIES
icculus@13543
    30
    INTERFACE_INCLUDE_DIRECTORIES "@includedir@/SDL2"
icculus@13543
    31
    IMPORTED_LINK_INTERFACE_LANGUAGES "C"
git@13683
    32
    IMPORTED_LOCATION "@libdir@/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2${CMAKE_STATIC_LIBRARY_SUFFIX}"
icculus@13543
    33
    INTERFACE_LINK_LIBRARIES "${SDL2_EXTRA_LINK_FLAGS_STATIC}")
icculus@13543
    34
icculus@13543
    35
  add_library(SDL2::SDL2main STATIC IMPORTED)
icculus@13543
    36
  set_target_properties(SDL2::SDL2main PROPERTIES
icculus@13543
    37
    IMPORTED_LINK_INTERFACE_LANGUAGES "C"
git@13683
    38
    IMPORTED_LOCATION "@libdir@/${CMAKE_STATIC_LIBRARY_PREFIX}SDL2main${CMAKE_STATIC_LIBRARY_SUFFIX}")
icculus@13543
    39
endif()