FindQt.cmake raw

   1  # Copyright (c) 2024-present The Limenka developers
   2  # Distributed under the MIT software license, see the accompanying
   3  # file COPYING or https://opensource.org/license/mit/.
   4  
   5  #[=======================================================================[
   6  FindQt
   7  ------
   8  
   9  Finds the Qt headers and libraries.
  10  
  11  This is a wrapper around find_package() command that:
  12   - facilitates searching in various build environments
  13   - prints a standard log message
  14  
  15  #]=======================================================================]
  16  
  17  set(_qt_homebrew_prefix)
  18  if(CMAKE_HOST_APPLE)
  19    find_program(HOMEBREW_EXECUTABLE brew)
  20    if(HOMEBREW_EXECUTABLE)
  21      execute_process(
  22        COMMAND ${HOMEBREW_EXECUTABLE} --prefix qt@${Qt_FIND_VERSION_MAJOR}
  23        OUTPUT_VARIABLE _qt_homebrew_prefix
  24        ERROR_QUIET
  25        OUTPUT_STRIP_TRAILING_WHITESPACE
  26      )
  27    endif()
  28  endif()
  29  
  30  # Save CMAKE_FIND_ROOT_PATH_MODE_LIBRARY state.
  31  unset(_qt_find_root_path_mode_library_saved)
  32  if(DEFINED CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
  33    set(_qt_find_root_path_mode_library_saved ${CMAKE_FIND_ROOT_PATH_MODE_LIBRARY})
  34  endif()
  35  
  36  # The Qt config files internally use find_library() calls for all
  37  # dependencies to ensure their availability. In turn, the find_library()
  38  # inspects the well-known locations on the file system; therefore, it must
  39  # be able to find platform-specific system libraries, for example:
  40  # /usr/x86_64-w64-mingw32/lib/libm.a or /usr/arm-linux-gnueabihf/lib/libm.a.
  41  set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
  42  
  43  find_package(Qt${Qt_FIND_VERSION_MAJOR} ${Qt_FIND_VERSION}
  44    COMPONENTS ${Qt_FIND_COMPONENTS}
  45    HINTS ${_qt_homebrew_prefix}
  46    PATH_SUFFIXES Qt${Qt_FIND_VERSION_MAJOR}  # Required on OpenBSD systems.
  47  )
  48  unset(_qt_homebrew_prefix)
  49  
  50  # Restore CMAKE_FIND_ROOT_PATH_MODE_LIBRARY state.
  51  if(DEFINED _qt_find_root_path_mode_library_saved)
  52    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ${_qt_find_root_path_mode_library_saved})
  53    unset(_qt_find_root_path_mode_library_saved)
  54  else()
  55    unset(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY)
  56  endif()
  57  
  58  include(FindPackageHandleStandardArgs)
  59  find_package_handle_standard_args(Qt
  60    REQUIRED_VARS Qt${Qt_FIND_VERSION_MAJOR}_DIR
  61    VERSION_VAR Qt${Qt_FIND_VERSION_MAJOR}_VERSION
  62  )
  63  
  64  foreach(component IN LISTS Qt_FIND_COMPONENTS ITEMS "")
  65    mark_as_advanced(Qt${Qt_FIND_VERSION_MAJOR}${component}_DIR)
  66  endforeach()
  67