FindBrotli.cmake 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. # See https://github.com/google/brotli/issues/773#issuecomment-579133187
  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. # Each string here is "ComponentName;LiteralName" (the semi-colon is a delimiter)
  65. foreach(_listvar "common;common" "decoder;dec" "encoder;enc")
  66. # Split the component name and literal library name from the listvar
  67. list(GET _listvar 0 _component_name)
  68. list(GET _listvar 1 _libname)
  69. # NOTE: We can't rely on PkgConf for static libs since the upstream static lib support is broken
  70. # See https://github.com/google/brotli/issues/795
  71. # TODO: whenever their issue is fixed upstream, remove this "AND NOT BROTLI_USE_STATIC_LIBS" check
  72. if(PKG_CONFIG_FOUND AND NOT BROTLI_USE_STATIC_LIBS)
  73. # These need to be GLOBAL for MinGW when making ALIAS libraries against them.
  74. # Have to postfix _STATIC on the name to tell PkgConfig to find the static libs.
  75. pkg_check_modules(Brotli_${_component_name}${_brotli_stat_str} QUIET GLOBAL IMPORTED_TARGET libbrotli${_libname})
  76. endif()
  77. # Check if the target was already found by Pkgconf
  78. if(TARGET PkgConfig::Brotli_${_component_name}${_brotli_stat_str})
  79. # ALIAS since we don't want the PkgConfig namespace on the Cmake library (for end-users)
  80. if (NOT TARGET Brotli::${_component_name})
  81. add_library(Brotli::${_component_name} ALIAS PkgConfig::Brotli_${_component_name}${_brotli_stat_str})
  82. endif()
  83. # Tells HANDLE_COMPONENTS we found the component
  84. set(Brotli_${_component_name}_FOUND TRUE)
  85. if(Brotli_FIND_REQUIRED_${_component_name})
  86. # If the lib is required, we can add its literal path as a required var for FindPackageHandleStandardArgs
  87. # Since it won't accept the PkgConfig targets
  88. if(BROTLI_USE_STATIC_LIBS)
  89. list(APPEND _brotli_req_vars "Brotli_${_component_name}_STATIC_LIBRARIES")
  90. else()
  91. list(APPEND _brotli_req_vars "Brotli_${_component_name}_LINK_LIBRARIES")
  92. endif()
  93. endif()
  94. # Skip searching for the libs with find_library since it was already found by Pkgconf
  95. continue()
  96. endif()
  97. if(Brotli_FIND_REQUIRED_${_component_name})
  98. # If it's required, we can set the name used in find_library as a required var for FindPackageHandleStandardArgs
  99. list(APPEND _brotli_req_vars "Brotli_${_component_name}")
  100. endif()
  101. list(APPEND _brotli_lib_names
  102. "brotli${_libname}"
  103. "libbrotli${_libname}"
  104. )
  105. if(BROTLI_USE_STATIC_LIBS)
  106. # Postfix "-static" to the libnames since we're looking for static libs
  107. list(TRANSFORM _brotli_lib_names APPEND "-static")
  108. endif()
  109. find_library(Brotli_${_component_name}
  110. NAMES ${_brotli_lib_names}
  111. HINTS ${BROTLI_ROOT_DIR}
  112. PATH_SUFFIXES
  113. "lib"
  114. "lib64"
  115. "libs"
  116. "libs64"
  117. "lib/x86_64-linux-gnu"
  118. )
  119. # Hide the library variable from the Cmake GUI
  120. mark_as_advanced(Brotli_${_component_name})
  121. # Unset since otherwise it'll stick around for the next loop and break things
  122. unset(_brotli_lib_names)
  123. # Check if find_library found the library
  124. if(Brotli_${_component_name})
  125. # Tells HANDLE_COMPONENTS we found the component
  126. set(Brotli_${_component_name}_FOUND TRUE)
  127. if (NOT TARGET Brotli::${_component_name})
  128. add_library("Brotli::${_component_name}" UNKNOWN IMPORTED)
  129. endif()
  130. # Attach the literal library and include dir to the IMPORTED target for the end-user
  131. set_target_properties("Brotli::${_component_name}" PROPERTIES
  132. INTERFACE_INCLUDE_DIRECTORIES "${Brotli_INCLUDE_DIR}"
  133. IMPORTED_LOCATION "${Brotli_${_component_name}}"
  134. )
  135. else()
  136. # Tells HANDLE_COMPONENTS we found the component
  137. set(Brotli_${_component_name}_FOUND FALSE)
  138. endif()
  139. endforeach()
  140. include(FindPackageHandleStandardArgs)
  141. # Sets Brotli_FOUND, and fails the find_package(Brotli) call if it was REQUIRED but missing libs.
  142. find_package_handle_standard_args(Brotli
  143. FOUND_VAR
  144. Brotli_FOUND
  145. REQUIRED_VARS
  146. Brotli_INCLUDE_DIR
  147. ${_brotli_req_vars}
  148. HANDLE_COMPONENTS
  149. )
  150. # Restore the original find library ordering
  151. if(BROTLI_USE_STATIC_LIBS)
  152. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_brotli_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  153. endif()