FindSDL2_mixer.cmake 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # Copyright 2019 Amine Ben Hassouna <[email protected]>
  4. # Copyright 2000-2019 Kitware, Inc. and Contributors
  5. # All rights reserved.
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # * Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # * Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. # * Neither the name of Kitware, Inc. nor the names of Contributors
  15. # may be used to endorse or promote products derived from this
  16. # software without specific prior written permission.
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. #[=======================================================================[.rst:
  29. FindSDL2_mixer
  30. --------------
  31. Locate SDL2_mixer library
  32. This module defines the following 'IMPORTED' target:
  33. ::
  34. SDL2::Mixer
  35. The SDL2_mixer library, if found.
  36. Have SDL2::SDL2 as a link dependency.
  37. This module will set the following variables in your project:
  38. ::
  39. SDL2_MIXER_LIBRARIES, the name of the library to link against
  40. SDL2_MIXER_INCLUDE_DIRS, where to find the headers
  41. SDL2_MIXER_FOUND, if false, do not try to link against
  42. SDL2_MIXER_VERSION_STRING - human-readable string containing the
  43. version of SDL2_mixer
  44. This module responds to the following cache variables:
  45. ::
  46. SDL2_MIXER_PATH
  47. Set a custom SDL2_mixer Library path (default: empty)
  48. SDL2_MIXER_NO_DEFAULT_PATH
  49. Disable search SDL2_mixer Library in default path.
  50. If SDL2_MIXER_PATH (default: ON)
  51. Else (default: OFF)
  52. SDL2_MIXER_INCLUDE_DIR
  53. SDL2_mixer headers path.
  54. SDL2_MIXER_LIBRARY
  55. SDL2_mixer Library (.dll, .so, .a, etc) path.
  56. Additional Note: If you see an empty SDL2_MIXER_LIBRARY in your project
  57. configuration, it means CMake did not find your SDL2_mixer library
  58. (SDL2_mixer.dll, libsdl2_mixer.so, etc). Set SDL2_MIXER_LIBRARY to point
  59. to your SDL2_mixer library, and configure again. This value is used to
  60. generate the final SDL2_MIXER_LIBRARIES variable and the SDL2::Mixer target,
  61. but when this value is unset, SDL2_MIXER_LIBRARIES and SDL2::Mixer does not
  62. get created.
  63. $SDL2MIXERDIR is an environment variable that would correspond to the
  64. ./configure --prefix=$SDL2MIXERDIR used in building SDL2_mixer.
  65. $SDL2DIR is an environment variable that would correspond to the
  66. ./configure --prefix=$SDL2DIR used in building SDL2.
  67. Created by Amine Ben Hassouna:
  68. Adapt FindSDL_mixer.cmake to SDL2_mixer (FindSDL2_mixer.cmake).
  69. Add cache variables for more flexibility:
  70. SDL2_MIXER_PATH, SDL2_MIXER_NO_DEFAULT_PATH (for details, see doc above).
  71. Add SDL2 as a required dependency.
  72. Modernize the FindSDL2_mixer.cmake module by creating a specific target:
  73. SDL2::Mixer (for details, see doc above).
  74. Original FindSDL_mixer.cmake module:
  75. Created by Eric Wing. This was influenced by the FindSDL.cmake
  76. module, but with modifications to recognize OS X frameworks and
  77. additional Unix paths (FreeBSD, etc).
  78. #]=======================================================================]
  79. # SDL2 Library required
  80. find_package(SDL2 QUIET)
  81. if(NOT SDL2_FOUND)
  82. set(SDL2_MIXER_SDL2_NOT_FOUND "Could NOT find SDL2 (SDL2 is required by SDL2_mixer).")
  83. if(SDL2_mixer_FIND_REQUIRED)
  84. message(FATAL_ERROR ${SDL2_MIXER_SDL2_NOT_FOUND})
  85. else()
  86. if(NOT SDL2_mixer_FIND_QUIETLY)
  87. message(STATUS ${SDL2_MIXER_SDL2_NOT_FOUND})
  88. endif()
  89. return()
  90. endif()
  91. unset(SDL2_MIXER_SDL2_NOT_FOUND)
  92. endif()
  93. # Define options for searching SDL2_mixer Library in a custom path
  94. set(SDL2_MIXER_PATH "" CACHE STRING "Custom SDL2_mixer Library path")
  95. set(_SDL2_MIXER_NO_DEFAULT_PATH OFF)
  96. if(SDL2_MIXER_PATH)
  97. set(_SDL2_MIXER_NO_DEFAULT_PATH ON)
  98. endif()
  99. set(SDL2_MIXER_NO_DEFAULT_PATH ${_SDL2_MIXER_NO_DEFAULT_PATH}
  100. CACHE BOOL "Disable search SDL2_mixer Library in default path")
  101. unset(_SDL2_MIXER_NO_DEFAULT_PATH)
  102. set(SDL2_MIXER_NO_DEFAULT_PATH_CMD)
  103. if(SDL2_MIXER_NO_DEFAULT_PATH)
  104. set(SDL2_MIXER_NO_DEFAULT_PATH_CMD NO_DEFAULT_PATH)
  105. endif()
  106. # Search for the SDL2_mixer include directory
  107. find_path(SDL2_MIXER_INCLUDE_DIR SDL_mixer.h
  108. HINTS
  109. ENV SDL2MIXERDIR
  110. ENV SDL2DIR
  111. ${SDL2_MIXER_NO_DEFAULT_PATH_CMD}
  112. PATH_SUFFIXES SDL2
  113. # path suffixes to search inside ENV{SDL2DIR}
  114. # and ENV{SDL2MIXERDIR}
  115. include/SDL2 include
  116. PATHS ${SDL2_MIXER_PATH}
  117. DOC "Where the SDL2_mixer headers can be found"
  118. )
  119. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  120. set(VC_LIB_PATH_SUFFIX lib/x64)
  121. else()
  122. set(VC_LIB_PATH_SUFFIX lib/x86)
  123. endif()
  124. # Search for the SDL2_mixer library
  125. find_library(SDL2_MIXER_LIBRARY
  126. NAMES SDL2_mixer
  127. HINTS
  128. ENV SDL2MIXERDIR
  129. ENV SDL2DIR
  130. ${SDL2_MIXER_NO_DEFAULT_PATH_CMD}
  131. PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
  132. PATHS ${SDL2_MIXER_PATH}
  133. DOC "Where the SDL2_mixer Library can be found"
  134. )
  135. # Read SDL2_mixer version
  136. if(SDL2_MIXER_INCLUDE_DIR AND EXISTS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h")
  137. file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h" SDL2_MIXER_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_MIXER_MAJOR_VERSION[ \t]+[0-9]+$")
  138. file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h" SDL2_MIXER_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_MIXER_MINOR_VERSION[ \t]+[0-9]+$")
  139. file(STRINGS "${SDL2_MIXER_INCLUDE_DIR}/SDL_mixer.h" SDL2_MIXER_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_MIXER_PATCHLEVEL[ \t]+[0-9]+$")
  140. string(REGEX REPLACE "^#define[ \t]+SDL_MIXER_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_MAJOR "${SDL2_MIXER_VERSION_MAJOR_LINE}")
  141. string(REGEX REPLACE "^#define[ \t]+SDL_MIXER_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_MINOR "${SDL2_MIXER_VERSION_MINOR_LINE}")
  142. string(REGEX REPLACE "^#define[ \t]+SDL_MIXER_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_MIXER_VERSION_PATCH "${SDL2_MIXER_VERSION_PATCH_LINE}")
  143. set(SDL2_MIXER_VERSION_STRING ${SDL2_MIXER_VERSION_MAJOR}.${SDL2_MIXER_VERSION_MINOR}.${SDL2_MIXER_VERSION_PATCH})
  144. unset(SDL2_MIXER_VERSION_MAJOR_LINE)
  145. unset(SDL2_MIXER_VERSION_MINOR_LINE)
  146. unset(SDL2_MIXER_VERSION_PATCH_LINE)
  147. unset(SDL2_MIXER_VERSION_MAJOR)
  148. unset(SDL2_MIXER_VERSION_MINOR)
  149. unset(SDL2_MIXER_VERSION_PATCH)
  150. endif()
  151. set(SDL2_MIXER_LIBRARIES ${SDL2_MIXER_LIBRARY})
  152. set(SDL2_MIXER_INCLUDE_DIRS ${SDL2_MIXER_INCLUDE_DIR})
  153. include(FindPackageHandleStandardArgs)
  154. FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_mixer
  155. REQUIRED_VARS SDL2_MIXER_LIBRARIES SDL2_MIXER_INCLUDE_DIRS
  156. VERSION_VAR SDL2_MIXER_VERSION_STRING)
  157. mark_as_advanced(SDL2_MIXER_PATH
  158. SDL2_MIXER_NO_DEFAULT_PATH
  159. SDL2_MIXER_LIBRARY
  160. SDL2_MIXER_INCLUDE_DIR)
  161. if(SDL2_MIXER_FOUND)
  162. # SDL2::Mixer target
  163. if(SDL2_MIXER_LIBRARY AND NOT TARGET SDL2::Mixer)
  164. add_library(SDL2::Mixer UNKNOWN IMPORTED)
  165. set_target_properties(SDL2::Mixer PROPERTIES
  166. IMPORTED_LOCATION "${SDL2_MIXER_LIBRARY}"
  167. INTERFACE_INCLUDE_DIRECTORIES "${SDL2_MIXER_INCLUDE_DIR}"
  168. INTERFACE_LINK_LIBRARIES SDL2::SDL2)
  169. if (APPLE_IOS)
  170. target_link_libraries(SDL2::Mixer INTERFACE
  171. "-framework AudioToolbox"
  172. "-framework CoreServices"
  173. )
  174. endif()
  175. endif()
  176. endif()