FindBrotli.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. # A simple FindBrotli package for Cmake's find_package function.
  2. # Note: This find package doesn't have version support, as the version file doesn't seem to be installed on most systems.
  3. #
  4. # If you want to find the static packages instead of shared (the default), define BROTLI_USE_STATIC_LIBS as TRUE.
  5. # The targets will have the same names, but it will use the static libs.
  6. #
  7. # Valid find_package COMPONENTS names: "decoder", "encoder", and "common"
  8. # Note that if you're requiring "decoder" or "encoder", then "common" will be automatically added as required.
  9. #
  10. # Defines the libraries (if found): Brotli::decoder, Brotli::encoder, Brotli::common
  11. # and the includes path variable: Brotli_INCLUDE_DIR
  12. #
  13. # If it's failing to find the libraries, try setting BROTLI_ROOT_DIR to the folder containing your library & include dir.
  14. # If they asked for a specific version, warn/fail since we don't support it.
  15. # TODO: if they start distributing the version somewhere, implement finding it.
  16. # But currently there's a version header that doesn't seem to get installed.
  17. if(Brotli_FIND_VERSION)
  18. set(_brotli_version_error_msg "FindBrotli.cmake doesn't have version support!")
  19. # If the package is required, throw a fatal error
  20. # Otherwise, if not running quietly, we throw a warning
  21. if(Brotli_FIND_REQUIRED)
  22. message(FATAL_ERROR "${_brotli_version_error_msg}")
  23. elseif(NOT Brotli_FIND_QUIETLY)
  24. message(WARNING "${_brotli_version_error_msg}")
  25. endif()
  26. endif()
  27. # Since both decoder & encoder require the common lib, force its requirement..
  28. # if the user is requiring either of those other libs.
  29. if(Brotli_FIND_REQUIRED_decoder OR Brotli_FIND_REQUIRED_encoder)
  30. set(Brotli_FIND_REQUIRED_common TRUE)
  31. endif()
  32. # Support preference of static libs by adjusting CMAKE_FIND_LIBRARY_SUFFIXES
  33. # Credit to FindOpenSSL.cmake for this
  34. if(BROTLI_USE_STATIC_LIBS)
  35. set(_brotli_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  36. if(WIN32)
  37. set(CMAKE_FIND_LIBRARY_SUFFIXES .lib .a ${CMAKE_FIND_LIBRARY_SUFFIXES})
  38. else()
  39. set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
  40. endif()
  41. endif()
  42. # Make PkgConfig optional, since some users (mainly Windows) don't have it.
  43. # But it's a lot more clean than manually using find_library.
  44. find_package(PkgConfig QUIET)
  45. # Only used if the PkgConfig libraries aren't used.
  46. find_path(Brotli_INCLUDE_DIR
  47. NAMES
  48. "brotli/decode.h"
  49. "brotli/encode.h"
  50. HINTS
  51. ${BROTLI_ROOT_DIR}
  52. PATH_SUFFIXES
  53. "include"
  54. "includes"
  55. DOC "The path to Brotli's include directory."
  56. )
  57. # Hides this var from the GUI
  58. mark_as_advanced(Brotli_INCLUDE_DIR)
  59. # Just used for PkgConfig stuff in the loop below
  60. set(_brotli_stat_str "")
  61. if(BROTLI_USE_STATIC_LIBS)
  62. set(_brotli_stat_str "_STATIC")
  63. endif()
  64. # Lets us know we are using the PkgConfig libraries
  65. # Will be set false if any non-pkgconf vars are used
  66. set(_brotli_using_pkgconf TRUE)
  67. # Each string here is "ComponentName;LiteralName" (the semi-colon is a delimiter)
  68. foreach(_listvar "common;common" "decoder;dec" "encoder;enc")
  69. # Split the component name and literal library name from the listvar
  70. list(GET _listvar 0 _component_name)
  71. list(GET _listvar 1 _libname)
  72. if(PKG_CONFIG_FOUND)
  73. # These need to be GLOBAL for MinGW when making ALIAS libraries against them.
  74. if(BROTLI_USE_STATIC_LIBS)
  75. # Have to use _STATIC to tell PkgConfig to find the static libs.
  76. pkg_check_modules(Brotli_${_component_name}_STATIC QUIET GLOBAL IMPORTED_TARGET libbrotli${_libname})
  77. else()
  78. pkg_check_modules(Brotli_${_component_name} QUIET GLOBAL IMPORTED_TARGET libbrotli${_libname})
  79. endif()
  80. endif()
  81. # Check if the target was already found by Pkgconf
  82. if(TARGET PkgConfig::Brotli_${_component_name} OR TARGET PkgConfig::Brotli_${_component_name}_STATIC)
  83. # Can't use generators for ALIAS targets, so you get this jank
  84. add_library(Brotli::${_component_name} ALIAS PkgConfig::Brotli_${_component_name}${_brotli_stat_str})
  85. # Tells HANDLE_COMPONENTS we found the component
  86. set(Brotli_${_component_name}_FOUND TRUE)
  87. if(Brotli_FIND_REQUIRED_${_component_name})
  88. # If the lib is required, we can add its literal path as a required var for FindPackageHandleStandardArgs
  89. # Since it won't accept the PkgConfig targets
  90. if(BROTLI_USE_STATIC_LIBS)
  91. list(APPEND _brotli_req_vars "Brotli_${_component_name}_STATIC_LIBRARIES")
  92. else()
  93. list(APPEND _brotli_req_vars "Brotli_${_component_name}_LINK_LIBRARIES")
  94. endif()
  95. endif()
  96. # Skip searching for the libs with find_library since it was already found by Pkgconf
  97. continue()
  98. endif()
  99. # Lets us know we aren't using the PkgConfig libraries
  100. set(_brotli_using_pkgconf FALSE)
  101. if(Brotli_FIND_REQUIRED_${_component_name})
  102. # If it's required, we can set the name used in find_library as a required var for FindPackageHandleStandardArgs
  103. list(APPEND _brotli_req_vars "Brotli_${_component_name}")
  104. endif()
  105. if(BROTLI_USE_STATIC_LIBS)
  106. list(APPEND _brotli_lib_names
  107. "brotli${_libname}-static"
  108. "libbrotli${_libname}-static"
  109. )
  110. else()
  111. list(APPEND _brotli_lib_names
  112. "brotli${_libname}"
  113. "libbrotli${_libname}"
  114. )
  115. endif()
  116. find_library(Brotli_${_component_name}
  117. NAMES ${_brotli_lib_names}
  118. HINTS ${BROTLI_ROOT_DIR}
  119. PATH_SUFFIXES
  120. "lib"
  121. "lib64"
  122. "libs"
  123. "libs64"
  124. "lib/x86_64-linux-gnu"
  125. )
  126. # Hide the library variable from the Cmake GUI
  127. mark_as_advanced(Brotli_${_component_name})
  128. # Unset since otherwise it'll stick around for the next loop and break things
  129. unset(_brotli_lib_names)
  130. # Check if find_library found the library
  131. if(Brotli_${_component_name})
  132. # Tells HANDLE_COMPONENTS we found the component
  133. set(Brotli_${_component_name}_FOUND TRUE)
  134. add_library("Brotli::${_component_name}" UNKNOWN IMPORTED)
  135. # Attach the literal library and include dir to the IMPORTED target for the end-user
  136. set_target_properties("Brotli::${_component_name}" PROPERTIES
  137. INTERFACE_INCLUDE_DIRECTORIES "${Brotli_INCLUDE_DIR}"
  138. IMPORTED_LOCATION "${Brotli_${_component_name}}"
  139. )
  140. else()
  141. # Tells HANDLE_COMPONENTS we found the component
  142. set(Brotli_${_component_name}_FOUND FALSE)
  143. endif()
  144. endforeach()
  145. include(FindPackageHandleStandardArgs)
  146. # Sets Brotli_FOUND, and fails the find_package(Brotli) call if it was REQUIRED but missing libs.
  147. find_package_handle_standard_args(Brotli
  148. FOUND_VAR
  149. Brotli_FOUND
  150. REQUIRED_VARS
  151. Brotli_INCLUDE_DIR
  152. ${_brotli_required_targets}
  153. HANDLE_COMPONENTS
  154. )
  155. # Restore the original find library ordering
  156. if(BROTLI_USE_STATIC_LIBS)
  157. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_brotli_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  158. endif()