FindBrotli.cmake 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. add_library(Brotli::${_component_name} ALIAS PkgConfig::Brotli_${_component_name}${_brotli_stat_str})
  81. # Tells HANDLE_COMPONENTS we found the component
  82. set(Brotli_${_component_name}_FOUND TRUE)
  83. if(Brotli_FIND_REQUIRED_${_component_name})
  84. # If the lib is required, we can add its literal path as a required var for FindPackageHandleStandardArgs
  85. # Since it won't accept the PkgConfig targets
  86. if(BROTLI_USE_STATIC_LIBS)
  87. list(APPEND _brotli_req_vars "Brotli_${_component_name}_STATIC_LIBRARIES")
  88. else()
  89. list(APPEND _brotli_req_vars "Brotli_${_component_name}_LINK_LIBRARIES")
  90. endif()
  91. endif()
  92. # Skip searching for the libs with find_library since it was already found by Pkgconf
  93. continue()
  94. endif()
  95. if(Brotli_FIND_REQUIRED_${_component_name})
  96. # If it's required, we can set the name used in find_library as a required var for FindPackageHandleStandardArgs
  97. list(APPEND _brotli_req_vars "Brotli_${_component_name}")
  98. endif()
  99. list(APPEND _brotli_lib_names
  100. "brotli${_libname}"
  101. "libbrotli${_libname}"
  102. )
  103. if(BROTLI_USE_STATIC_LIBS)
  104. # Postfix "-static" to the libnames since we're looking for static libs
  105. list(TRANSFORM _brotli_lib_names APPEND "-static")
  106. endif()
  107. find_library(Brotli_${_component_name}
  108. NAMES ${_brotli_lib_names}
  109. HINTS ${BROTLI_ROOT_DIR}
  110. PATH_SUFFIXES
  111. "lib"
  112. "lib64"
  113. "libs"
  114. "libs64"
  115. "lib/x86_64-linux-gnu"
  116. )
  117. # Hide the library variable from the Cmake GUI
  118. mark_as_advanced(Brotli_${_component_name})
  119. # Unset since otherwise it'll stick around for the next loop and break things
  120. unset(_brotli_lib_names)
  121. # Check if find_library found the library
  122. if(Brotli_${_component_name})
  123. # Tells HANDLE_COMPONENTS we found the component
  124. set(Brotli_${_component_name}_FOUND TRUE)
  125. add_library("Brotli::${_component_name}" UNKNOWN IMPORTED)
  126. # Attach the literal library and include dir to the IMPORTED target for the end-user
  127. set_target_properties("Brotli::${_component_name}" PROPERTIES
  128. INTERFACE_INCLUDE_DIRECTORIES "${Brotli_INCLUDE_DIR}"
  129. IMPORTED_LOCATION "${Brotli_${_component_name}}"
  130. )
  131. else()
  132. # Tells HANDLE_COMPONENTS we found the component
  133. set(Brotli_${_component_name}_FOUND FALSE)
  134. endif()
  135. endforeach()
  136. include(FindPackageHandleStandardArgs)
  137. # Sets Brotli_FOUND, and fails the find_package(Brotli) call if it was REQUIRED but missing libs.
  138. find_package_handle_standard_args(Brotli
  139. FOUND_VAR
  140. Brotli_FOUND
  141. REQUIRED_VARS
  142. Brotli_INCLUDE_DIR
  143. ${_brotli_req_vars}
  144. HANDLE_COMPONENTS
  145. )
  146. # Restore the original find library ordering
  147. if(BROTLI_USE_STATIC_LIBS)
  148. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_brotli_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  149. endif()