DolphinLibraryTools.cmake 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 out_use_system 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(${out_use_system} AUTO PARENT_SCOPE)
  28. else()
  29. set(${out_use_system} ON PARENT_SCOPE)
  30. endif()
  31. else()
  32. set(${out_use_system} ${USE_SYSTEM_LIBS} PARENT_SCOPE)
  33. endif()
  34. else()
  35. set(${out_use_system} ${USE_SYSTEM_${upperlib}} PARENT_SCOPE)
  36. endif()
  37. endfunction()
  38. function(dolphin_add_bundled_library library use_system bundled_path)
  39. if (${use_system} STREQUAL "AUTO")
  40. message(STATUS "No system ${library} was found. Using static ${library} from Externals.")
  41. else()
  42. message(STATUS "Using static ${library} from Externals")
  43. endif()
  44. if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${bundled_path}/CMakeLists.txt")
  45. message(FATAL_ERROR "No bundled ${library} was found. Did you forget to checkout submodules?")
  46. endif()
  47. add_subdirectory(${bundled_path} EXCLUDE_FROM_ALL)
  48. endfunction()
  49. function(dolphin_find_optional_system_library library bundled_path)
  50. dolphin_optional_system_library(use_system ${library})
  51. string(TOUPPER ${library} upperlib)
  52. if(use_system)
  53. find_package(${library} ${ARGN})
  54. # Yay for cmake packages being inconsistent
  55. if(DEFINED ${library}_FOUND)
  56. set(prefix ${library})
  57. else()
  58. set(prefix ${upperlib})
  59. endif()
  60. if((NOT ${prefix}_FOUND) AND (NOT ${use_system} STREQUAL "AUTO"))
  61. message(FATAL_ERROR "No system ${library} was found. Please install it or set USE_SYSTEM_${upperlib} to AUTO or OFF.")
  62. endif()
  63. endif()
  64. if(${prefix}_FOUND)
  65. message(STATUS "Using system ${library}")
  66. set(${prefix}_TYPE "System" PARENT_SCOPE)
  67. else()
  68. dolphin_add_bundled_library(${library} ${use_system} ${bundled_path})
  69. set(${prefix}_TYPE "Bundled" PARENT_SCOPE)
  70. endif()
  71. endfunction()
  72. function(dolphin_find_optional_system_library_pkgconfig library search alias bundled_path)
  73. dolphin_optional_system_library(use_system ${library})
  74. string(TOUPPER ${library} upperlib)
  75. if(use_system)
  76. pkg_search_module(${library} ${search} ${ARGN} IMPORTED_TARGET)
  77. if((NOT ${library}_FOUND) AND (NOT ${use_system} STREQUAL "AUTO"))
  78. message(FATAL_ERROR "No system ${library} was found. Please install it or set USE_SYSTEM_${upperlib} to AUTO or OFF.")
  79. endif()
  80. endif()
  81. if(${library}_FOUND)
  82. message(STATUS "Using system ${library}")
  83. dolphin_alias_library(${alias} PkgConfig::${library})
  84. set(${library}_TYPE "System" PARENT_SCOPE)
  85. else()
  86. dolphin_add_bundled_library(${library} ${use_system} ${bundled_path})
  87. set(${library}_TYPE "Bundled" PARENT_SCOPE)
  88. endif()
  89. endfunction()