FindUrho3D.cmake 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #
  2. # Copyright (c) 2008-2015 the Urho3D project.
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. # Find Urho3D include directories and library in source & build tree or installed Urho3D SDK.
  23. #
  24. # URHO3D_FOUND
  25. # URHO3D_INCLUDE_DIRS
  26. # URHO3D_LIBRARIES
  27. # URHO3D_LIBRARIES_REL (WIN32 only)
  28. # URHO3D_LIBRARIES_DBG (WIN32 Only)
  29. #
  30. #
  31. # For internal Urho3D project, the Urho3D "build tree" path is already known.
  32. #
  33. # For external project that attempts to use the Urho3D build tree or installed Urho3D SDK,
  34. # use URHO3D_HOME environment variable or build option to specify the path (not needed when the path is a system-wide default location).
  35. # When setting URHO3D_HOME variable, it should be set to a parent directory containing both the "include" or "lib" subdirectories.
  36. # For example: set URHO3D_HOME=/home/john/usr/local, if the SDK is installed using DESTDIR=/home/john and CMAKE_INSTALL_PREFIX=/usr/local
  37. #
  38. if (URHO3D_FOUND)
  39. # All the subprojects should use the same Urho3D library, so only need to search on the first (sub)project that requires Urho3D library
  40. return ()
  41. endif ()
  42. # If the URHO3D_LIB_TYPE build option changes then invalidate the found library cache
  43. if (NOT URHO3D_LIB_TYPE STREQUAL URHO3D_FOUND_LIB_TYPE)
  44. unset (URHO3D_LIBRARIES CACHE)
  45. set (URHO3D_FOUND_LIB_TYPE ${URHO3D_LIB_TYPE} CACHE INTERNAL "Lib type when Urho3D library was last found")
  46. # Urho3D prefers static library type by default while CMake prefers shared one, so we need to change CMake preference to agree with Urho3D
  47. if (NOT URHO3D_LIB_TYPE STREQUAL SHARED)
  48. list (REVERSE CMAKE_FIND_LIBRARY_SUFFIXES)
  49. endif ()
  50. endif ()
  51. set (PATH_SUFFIX Urho3D)
  52. if (CMAKE_PROJECT_NAME STREQUAL Urho3D AND TARGET Urho3D)
  53. # Library location is already known to be in the build tree
  54. set (URHO3D_HOME ${CMAKE_BINARY_DIR})
  55. set (URHO3D_INCLUDE_DIRS ${URHO3D_HOME}/include ${URHO3D_HOME}/include/${PATH_SUFFIX}/ThirdParty)
  56. if (URHO3D_PHYSICS)
  57. # Bullet library depends on its own include dir to be added in the header search path
  58. # This is more practical than patching its header files in many places to make them work with relative path
  59. list (APPEND URHO3D_INCLUDE_DIRS ${URHO3D_HOME}/include/${PATH_SUFFIX}/ThirdParty/Bullet)
  60. endif ()
  61. if (URHO3D_LUA)
  62. # ditto for Lua/LuaJIT
  63. list (APPEND URHO3D_INCLUDE_DIRS ${URHO3D_HOME}/include/${PATH_SUFFIX}/ThirdParty/Lua${JIT})
  64. endif ()
  65. set (URHO3D_LIBRARIES Urho3D)
  66. set (FOUND_MESSAGE "Found Urho3D: as CMake target")
  67. else ()
  68. # Library location would be searched (based on URHO3D_HOME variable if provided and in system-wide default location)
  69. if (NOT URHO3D_HOME AND DEFINED ENV{URHO3D_HOME})
  70. file (TO_CMAKE_PATH "$ENV{URHO3D_HOME}" URHO3D_HOME)
  71. endif ()
  72. # URHO3D_HOME variable should be an absolute path, so use a non-rooted search even when we are cross-compiling
  73. if (URHO3D_HOME)
  74. list (APPEND CMAKE_PREFIX_PATH ${URHO3D_HOME})
  75. set (SEARCH_OPT NO_CMAKE_FIND_ROOT_PATH)
  76. endif ()
  77. # For Android platform, search in path similar to ANDROID_LIBRARY_OUTPUT_PATH variable
  78. if (ANDROID)
  79. if (URHO3D_HOME)
  80. set (URHO3D_LIB_SEARCH_HINT HINTS ${URHO3D_HOME}/libs/${ANDROID_NDK_ABI_NAME})
  81. else ()
  82. set (URHO3D_LIB_SEARCH_HINT HINTS /usr/local/libs/${ANDROID_NDK_ABI_NAME})
  83. endif ()
  84. endif ()
  85. if (NOT URHO3D_64BIT)
  86. # For 32-bit, force to search in 'lib' path even when the host system defaulted to use 'lib64'
  87. set_property (GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS FALSE)
  88. elseif (WIN32)
  89. # For 64-bit, force to search in 'lib64' path even when the Windows platform is not defaulted to use it
  90. set_property (GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS TRUE)
  91. endif ()
  92. find_path (URHO3D_INCLUDE_DIRS Urho3D.h PATH_SUFFIXES ${PATH_SUFFIX} ${SEARCH_OPT})
  93. find_library (URHO3D_LIBRARIES NAMES Urho3D ${URHO3D_LIB_SEARCH_HINT} PATH_SUFFIXES ${PATH_SUFFIX} ${SEARCH_OPT})
  94. if (WIN32)
  95. # For Windows platform, give a second chance to search for a debug version of the library
  96. find_library (URHO3D_LIBRARIES_DBG NAMES Urho3D_d ${URHO3D_LIB_SEARCH_HINT} PATH_SUFFIXES ${PATH_SUFFIX} ${SEARCH_OPT})
  97. # If both the non-debug and debug version of the libraries are found then use them both
  98. set (URHO3D_LIBRARIES_REL ${URHO3D_LIBRARIES})
  99. # Otherwise, URHO3D_LIBRARIES variable should have the path to either one of the version
  100. if (URHO3D_LIBRARIES)
  101. if (URHO3D_LIBRARIES_DBG)
  102. list (APPEND URHO3D_LIBRARIES ${URHO3D_LIBRARIES_DBG})
  103. endif ()
  104. else ()
  105. set (URHO3D_LIBRARIES ${URHO3D_LIBRARIES_DBG})
  106. endif ()
  107. endif ()
  108. if (URHO3D_INCLUDE_DIRS)
  109. set (BASE_INCLUDE_DIR ${URHO3D_INCLUDE_DIRS}) # Preserve the base include dir because the original variable will be turned into a list below
  110. get_filename_component (PATH ${URHO3D_INCLUDE_DIRS} PATH)
  111. set (URHO3D_INCLUDE_DIRS ${PATH} ${URHO3D_INCLUDE_DIRS}/ThirdParty)
  112. if (URHO3D_PHYSICS)
  113. list (APPEND URHO3D_INCLUDE_DIRS ${BASE_INCLUDE_DIR}/ThirdParty/Bullet)
  114. endif ()
  115. if (URHO3D_LUA)
  116. list (APPEND URHO3D_INCLUDE_DIRS ${BASE_INCLUDE_DIR}/ThirdParty/Lua${JIT})
  117. endif ()
  118. if (NOT URHO3D_HOME)
  119. # URHO3D_HOME is not set when using SDK installed on system-wide default location, so set it now
  120. get_filename_component (PATH ${PATH} PATH)
  121. set (URHO3D_HOME ${PATH})
  122. endif ()
  123. endif ()
  124. endif ()
  125. if (URHO3D_INCLUDE_DIRS AND URHO3D_LIBRARIES)
  126. set (URHO3D_FOUND 1)
  127. if (NOT FOUND_MESSAGE)
  128. set (FOUND_MESSAGE "Found Urho3D: ${URHO3D_LIBRARIES}")
  129. endif ()
  130. include (FindPackageMessage)
  131. FIND_PACKAGE_MESSAGE (Urho3D ${FOUND_MESSAGE} "[${URHO3D_LIBRARIES}][${URHO3D_INCLUDE_DIRS}]")
  132. set (URHO3D_HOME ${URHO3D_HOME} CACHE PATH "Path to Urho3D build tree or SDK installation location (external project only)" FORCE)
  133. elseif (Urho3D_FIND_REQUIRED)
  134. if (ANDROID)
  135. set (NOT_FOUND_MESSAGE "For Android platform, double check if you have specified to use the same ANDROID_ABI as the Urho3D library in the build tree or SDK.")
  136. endif ()
  137. message (FATAL_ERROR
  138. "Could not find Urho3D library in Urho3D build tree or SDK installation. "
  139. "Use URHO3D_HOME environment variable or build option to specify the location of the build tree or SDK installation. ${NOT_FOUND_MESSAGE}")
  140. endif ()
  141. mark_as_advanced (URHO3D_INCLUDE_DIRS URHO3D_LIBRARIES URHO3D_LIBRARIES_REL URHO3D_LIBRARIES_DBG URHO3D_HOME)