DolphinLibraryTools.cmake 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # like add_library(new ALIAS old) but avoids add_library cannot create ALIAS target "new" because target "old" is imported but not globally visible. on older cmake
  2. # This can be replaced with a direct alias call once our minimum is cmake 3.18
  3. function(dolphin_alias_library new old)
  4. string(REPLACE "::" "" library_no_namespace ${old})
  5. if (NOT TARGET _alias_${library_no_namespace})
  6. add_library(_alias_${library_no_namespace} INTERFACE)
  7. target_link_libraries(_alias_${library_no_namespace} INTERFACE ${old})
  8. endif()
  9. add_library(${new} ALIAS _alias_${library_no_namespace})
  10. endfunction()
  11. # Makes an imported target if it doesn't exist. Useful for when find scripts from older versions of cmake don't make the targets you need
  12. function(dolphin_make_imported_target_if_missing target lib)
  13. if(${lib}_FOUND AND NOT TARGET ${target})
  14. add_library(_${lib} INTERFACE)
  15. target_link_libraries(_${lib} INTERFACE "${${lib}_LIBRARIES}")
  16. target_include_directories(_${lib} INTERFACE "${${lib}_INCLUDE_DIRS}")
  17. add_library(${target} ALIAS _${lib})
  18. endif()
  19. endfunction()
  20. function(dolphin_optional_system_library library)
  21. string(TOUPPER ${library} upperlib)
  22. set(USE_SYSTEM_${upperlib} "" CACHE STRING "Use system ${library} instead of bundled. ON - Always use system and fail if unavailable, OFF - Always use bundled, AUTO - Use system if available, otherwise use bundled, blank - Delegate to USE_SYSTEM_LIBS. Default is blank.")
  23. if("${USE_SYSTEM_${upperlib}}" STREQUAL "")
  24. if(APPROVED_VENDORED_DEPENDENCIES)
  25. string(TOLOWER ${library} lowerlib)
  26. if(lowerlib IN_LIST APPROVED_VENDORED_DEPENDENCIES)
  27. set(RESOLVED_USE_SYSTEM_${upperlib} AUTO PARENT_SCOPE)
  28. else()
  29. set(RESOLVED_USE_SYSTEM_${upperlib} ON PARENT_SCOPE)
  30. endif()
  31. else()
  32. set(RESOLVED_USE_SYSTEM_${upperlib} ${USE_SYSTEM_LIBS} PARENT_SCOPE)
  33. endif()
  34. else()
  35. set(RESOLVED_USE_SYSTEM_${upperlib} ${USE_SYSTEM_${upperlib}} PARENT_SCOPE)
  36. endif()
  37. endfunction()
  38. function(dolphin_add_bundled_library library bundled_path)
  39. string(TOUPPER ${library} upperlib)
  40. if (${RESOLVED_USE_SYSTEM_${upperlib}} STREQUAL "AUTO")
  41. message(STATUS "No system ${library} was found. Using static ${library} from Externals.")
  42. else()
  43. message(STATUS "Using static ${library} from Externals")
  44. endif()
  45. if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${bundled_path}/CMakeLists.txt")
  46. message(FATAL_ERROR "No bundled ${library} was found. Did you forget to checkout submodules?")
  47. endif()
  48. add_subdirectory(${bundled_path} EXCLUDE_FROM_ALL)
  49. endfunction()
  50. function(dolphin_find_optional_system_library library bundled_path)
  51. dolphin_optional_system_library(${library})
  52. string(TOUPPER ${library} upperlib)
  53. if(RESOLVED_USE_SYSTEM_${upperlib})
  54. find_package(${library} ${ARGN})
  55. # Yay for cmake packages being inconsistent
  56. if(DEFINED ${library}_FOUND)
  57. set(prefix ${library})
  58. else()
  59. set(prefix ${upperlib})
  60. endif()
  61. if((NOT ${found}) AND (NOT ${RESOLVED_USE_SYSTEM_${upperlib}} STREQUAL "AUTO"))
  62. message(FATAL_ERROR "No system ${library} was found. Please install it or set USE_SYSTEM_${upperlib} to AUTO or OFF.")
  63. endif()
  64. endif()
  65. if(${prefix}_FOUND)
  66. message(STATUS "Using system ${library}")
  67. set(${prefix}_TYPE "System" PARENT_SCOPE)
  68. else()
  69. dolphin_add_bundled_library(${library} ${bundled_path})
  70. set(${prefix}_TYPE "Bundled" PARENT_SCOPE)
  71. endif()
  72. endfunction()
  73. function(dolphin_find_optional_system_library_pkgconfig library search alias bundled_path)
  74. dolphin_optional_system_library(${library})
  75. string(TOUPPER ${library} upperlib)
  76. if(RESOLVED_USE_SYSTEM_${upperlib})
  77. pkg_search_module(${library} ${search} ${ARGN} IMPORTED_TARGET)
  78. if((NOT ${library}_FOUND) AND (NOT ${RESOLVED_USE_SYSTEM_${upperlib}} STREQUAL "AUTO"))
  79. message(FATAL_ERROR "No system ${library} was found. Please install it or set USE_SYSTEM_${upperlib} to AUTO or OFF.")
  80. endif()
  81. endif()
  82. if(${library}_FOUND)
  83. message(STATUS "Using system ${library}")
  84. dolphin_alias_library(${alias} PkgConfig::${library})
  85. set(${library}_TYPE "System" PARENT_SCOPE)
  86. else()
  87. dolphin_add_bundled_library(${library} ${bundled_path})
  88. set(${library}_TYPE "Bundled" PARENT_SCOPE)
  89. endif()
  90. endfunction()