FindSDL_net.cmake 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindSDL_net
  5. -----------
  6. Finds the SDL_net library, a cross-platform network library for use with the
  7. SDL (Simple DirectMedia Layer) applications.
  8. .. note::
  9. This module is specifically intended for SDL_net version 1. Starting with
  10. version 2.1, SDL_net provides a CMake package configuration file when built
  11. with CMake and should be found using ``find_package(SDL2_net)``. These
  12. newer versions provide :ref:`Imported Targets` that encapsulate usage
  13. requirements. Refer to the official SDL documentation for more information.
  14. Result Variables
  15. ^^^^^^^^^^^^^^^^
  16. This module defines the following variables:
  17. ``SDL_net_FOUND``
  18. Boolean indicating whether the (requested version of) SDL_net library is
  19. found. For backward compatibility, the ``SDL_NET_FOUND`` variable is also set
  20. to the same value.
  21. ``SDL_NET_VERSION_STRING``
  22. The human-readable string containing the version of SDL_net found.
  23. ``SDL_NET_INCLUDE_DIRS``
  24. Include directories containing headers needed to use the SDL_net library.
  25. ``SDL_NET_LIBRARIES``
  26. Libraries needed to link against to use the SDL_net library.
  27. Deprecated Variables
  28. ^^^^^^^^^^^^^^^^^^^^
  29. For backward compatibility the following variables are also set:
  30. ``SDLNET_FOUND``
  31. .. deprecated:: 2.8.10
  32. Use the ``SDL_net_FOUND``, which has the same value.
  33. ``SDLNET_INCLUDE_DIR``
  34. .. deprecated:: 2.8.10
  35. Use the ``SDL_NET_INCLUDE_DIRS``, which has the same value.
  36. ``SDLNET_LIBRARY``
  37. .. deprecated:: 2.8.10
  38. Use the ``SDL_NET_LIBRARIES``, which has the same value.
  39. Hints
  40. ^^^^^
  41. This module accepts the following variables:
  42. ``SDLDIR``
  43. Environment variable that can be set to help locate an SDL library installed
  44. in a custom location. It should point to the installation destination that
  45. was used when configuring, building, and installing SDL library:
  46. ``./configure --prefix=$SDLDIR``.
  47. Examples
  48. ^^^^^^^^
  49. Finding SDL_net library and creating an imported interface target for linking it
  50. to a project target:
  51. .. code-block:: cmake
  52. find_package(SDL_net)
  53. if(SDL_net_FOUND AND NOT TARGET SDL::SDL_net)
  54. add_library(SDL::SDL_net INTERFACE IMPORTED)
  55. set_target_properties(
  56. SDL::SDL_net
  57. PROPERTIES
  58. INTERFACE_INCLUDE_DIRECTORIES "${SDL_NET_INCLUDE_DIRS}"
  59. INTERFACE_LINK_LIBRARIES "${SDL_NET_LIBRARIES}"
  60. )
  61. endif()
  62. target_link_libraries(project_target PRIVATE SDL::SDL_net)
  63. When working with SDL_net version 2, the upstream package provides the
  64. ``SDL2_net::SDL2_net`` imported target directly. It can be used in a project
  65. without using this module:
  66. .. code-block:: cmake
  67. find_package(SDL2_net)
  68. target_link_libraries(project_target PRIVATE SDL2_net::SDL2_net)
  69. See Also
  70. ^^^^^^^^
  71. * The :module:`FindSDL` module to find the main SDL library.
  72. #]=======================================================================]
  73. cmake_policy(PUSH)
  74. cmake_policy(SET CMP0159 NEW) # file(STRINGS) with REGEX updates CMAKE_MATCH_<n>
  75. if(NOT SDL_NET_INCLUDE_DIR AND SDLNET_INCLUDE_DIR)
  76. set(SDL_NET_INCLUDE_DIR ${SDLNET_INCLUDE_DIR} CACHE PATH "directory cache
  77. entry initialized from old variable name")
  78. endif()
  79. find_path(SDL_NET_INCLUDE_DIR SDL_net.h
  80. HINTS
  81. ENV SDLNETDIR
  82. ENV SDLDIR
  83. PATH_SUFFIXES SDL
  84. # path suffixes to search inside ENV{SDLDIR}
  85. include/SDL include/SDL12 include/SDL11 include
  86. )
  87. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  88. set(VC_LIB_PATH_SUFFIX lib/x64)
  89. else()
  90. set(VC_LIB_PATH_SUFFIX lib/x86)
  91. endif()
  92. if(NOT SDL_NET_LIBRARY AND SDLNET_LIBRARY)
  93. set(SDL_NET_LIBRARY ${SDLNET_LIBRARY} CACHE FILEPATH "file cache entry
  94. initialized from old variable name")
  95. endif()
  96. find_library(SDL_NET_LIBRARY
  97. NAMES SDL_net
  98. HINTS
  99. ENV SDLNETDIR
  100. ENV SDLDIR
  101. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  102. )
  103. if(SDL_NET_INCLUDE_DIR AND EXISTS "${SDL_NET_INCLUDE_DIR}/SDL_net.h")
  104. file(STRINGS "${SDL_NET_INCLUDE_DIR}/SDL_net.h" SDL_NET_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_NET_MAJOR_VERSION[ \t]+[0-9]+$")
  105. file(STRINGS "${SDL_NET_INCLUDE_DIR}/SDL_net.h" SDL_NET_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_NET_MINOR_VERSION[ \t]+[0-9]+$")
  106. file(STRINGS "${SDL_NET_INCLUDE_DIR}/SDL_net.h" SDL_NET_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_NET_PATCHLEVEL[ \t]+[0-9]+$")
  107. string(REGEX REPLACE "^#define[ \t]+SDL_NET_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_NET_VERSION_MAJOR "${SDL_NET_VERSION_MAJOR_LINE}")
  108. string(REGEX REPLACE "^#define[ \t]+SDL_NET_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL_NET_VERSION_MINOR "${SDL_NET_VERSION_MINOR_LINE}")
  109. string(REGEX REPLACE "^#define[ \t]+SDL_NET_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL_NET_VERSION_PATCH "${SDL_NET_VERSION_PATCH_LINE}")
  110. set(SDL_NET_VERSION_STRING ${SDL_NET_VERSION_MAJOR}.${SDL_NET_VERSION_MINOR}.${SDL_NET_VERSION_PATCH})
  111. unset(SDL_NET_VERSION_MAJOR_LINE)
  112. unset(SDL_NET_VERSION_MINOR_LINE)
  113. unset(SDL_NET_VERSION_PATCH_LINE)
  114. unset(SDL_NET_VERSION_MAJOR)
  115. unset(SDL_NET_VERSION_MINOR)
  116. unset(SDL_NET_VERSION_PATCH)
  117. endif()
  118. set(SDL_NET_LIBRARIES ${SDL_NET_LIBRARY})
  119. set(SDL_NET_INCLUDE_DIRS ${SDL_NET_INCLUDE_DIR})
  120. include(FindPackageHandleStandardArgs)
  121. find_package_handle_standard_args(SDL_net
  122. REQUIRED_VARS SDL_NET_LIBRARIES SDL_NET_INCLUDE_DIRS
  123. VERSION_VAR SDL_NET_VERSION_STRING)
  124. # for backward compatibility
  125. set(SDLNET_LIBRARY ${SDL_NET_LIBRARIES})
  126. set(SDLNET_INCLUDE_DIR ${SDL_NET_INCLUDE_DIRS})
  127. set(SDLNET_FOUND ${SDL_net_FOUND})
  128. mark_as_advanced(SDL_NET_LIBRARY SDL_NET_INCLUDE_DIR)
  129. cmake_policy(POP)