# ______ _           _ _____ _   _____ _____              __ _
# |  ___(_)         | |  _  | | |  ___/  __ \            / _(_)
# | |_   _ _ __   __| | | | | |_|___ \| /  \/ ___  _ __ | |_ _  __ _
# |  _| | | '_ \ / _` | | | | __|   \ \ |    / _ \| '_ \|  _| |/ _` |
# | |   | | | | | (_| \ \/' / |_/\__/ / \__/\ (_) | | | | | | | (_| |
# \_|   |_|_| |_|\__,_|\_/\_\\__\____/ \____/\___/|_| |_|_| |_|\__, |
# zhengrr                                                       __/ |
# 2016-10-21 – 2021-08-30                                      |___/
# Unlicense

cmake_minimum_required(VERSION 3.12)
cmake_policy(VERSION 3.12)

include_guard()  # 3.10

if(NOT COMMAND find_package_handle_standard_args)
  include(FindPackageHandleStandardArgs)
endif()

#[=======================================================================[.rst:
FindQt5Config
-------------

Find the `Qt5 <https://qt.io/>`_ CMake packages config.

Result Variables
^^^^^^^^^^^^^^^^

This module defines the following variables:

``Qt5Config_FOUND``
  Found the Qt5 CMake packages config.

``Qt5Config_PREFIX_PATH``
  The directory containing the Qt5 CMake packages config.

Cache variables
^^^^^^^^^^^^^^^

The following cache variables may also be set:

``QT5_ROOT``
  The root directory of the Qt5 installation (may also be set as an environment variable)::

    v QT6_ROOT
      > bin
      > doc
      > include
      v lib
        v cmake
          v Qt5
              Qt5Config.cmake
              ...
          > Qt5AxBase
          > ...
          libEGL.lib
          ...
      > mkspecs
      > ...
  
  See also `find_package § Search Procedure <https://cmake.org/cmake/help/latest/command/find_package.html#search-procedure>`_.
#]=======================================================================]

if(Qt5Config_FOUND)
  return()
endif()

if(0 LESS CMAKE_SIZEOF_VOID_P)
  math(EXPR nBits "${CMAKE_SIZEOF_VOID_P} * 8")
else()
  message(FATAL_ERROR "The value of CMAKE_SIZEOF_VOID_P could not be obtained.")
endif()

# Hints (Comment with a 64-bit example.)
set(zHints "${QT5_${nBits}_ROOT}"    #[[QT5_64_ROOT]] "${QT5_${nBits}_DIR}"    #[[QT5_64_DIR]]   "${QTDIR${nBits}}"    #[[QTDIR64]]
           "$ENV{QT5_${nBits}_ROOT}"                  "$ENV{QT5_${nBits}_DIR}"                   "$ENV{QTDIR${nBits}}"
           "${QT5_ROOT}"             #[[QT5_ROOT]]    "${QT5_DIR}"             #[[QT5_DIR]]      "${QTDIR}"            #[[QTDIR]]
           "$ENV{QT5_ROOT}"                           "$ENV{QT5_DIR}"                            "$ENV{QTDIR}")

# Prefix
find_path(Qt5Config_PREFIX_PATH
  NAMES "lib/cmake/Qt5/Qt5Config.cmake"
  HINTS ${zHints}
  NO_DEFAULT_PATH)
mark_as_advanced(Qt5Config_PREFIX_PATH)

# Package
find_package_handle_standard_args(
  Qt5Config
  DEFAULT_MSG
  Qt5Config_PREFIX_PATH)

if(Qt5Config_FOUND)
  # Append
  if(NOT Qt5Config_PREFIX_PATH IN_LIST CMAKE_PREFIX_PATH)
    list(APPEND CMAKE_PREFIX_PATH "${Qt5Config_PREFIX_PATH}")
  endif()
  mark_as_advanced(FORCE Qt5_ROOT)

else()
  # Hints
  set(Qt5_ROOT "${Qt5_ROOT}" CACHE PATH "The root directory of the Qt5 installation.")
  mark_as_advanced(CLEAR Qt5_ROOT)

endif()
