FindSDL_net.cmake 5.9 KB

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