# ______ _           _ _____ _    ____ _____              __ _       
# |  ___(_)         | |  _  | |  / ___/  __ \            / _(_)      
# | |_   _ _ __   __| | | | | |_/ /___| /  \/ ___  _ __ | |_ _  __ _ 
# |  _| | | '_ \ / _` | | | | __| ___ \ |    / _ \| '_ \|  _| |/ _` |
# | |   | | | | | (_| \ \/' / |_| \_/ | \__/\ (_) | | | | | | | (_| |
# \_|   |_|_| |_|\__,_|\_/\_\\__\_____/\____/\___/|_| |_|_| |_|\__, |
# zhengrr                                                       __/ |
# 2021-08-30 – 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:
FindQt6Config
-------------

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

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

This module defines the following variables:

``Qt6Config_FOUND``
  Found the Qt6 CMake packages config.

``Qt6Config_PREFIX_PATH``
  The directory containing the Qt6 CMake packages config.

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

The following cache variables may also be set:

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

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

if(Qt6Config_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 "${QT6_${nBits}_ROOT}"    #[[QT6_64_ROOT]] "${QT6_${nBits}_DIR}"    #[[QT6_64_DIR]]   "${QTDIR${nBits}}"    #[[QTDIR64]]
           "$ENV{QT6_${nBits}_ROOT}"                  "$ENV{QT6_${nBits}_DIR}"                   "$ENV{QTDIR${nBits}}"
           "${QT6_ROOT}"             #[[QT6_ROOT]]    "${QT6_DIR}"             #[[QT6_DIR]]      "${QTDIR}"            #[[QTDIR]]
           "$ENV{QT6_ROOT}"                           "$ENV{QT6_DIR}"                            "$ENV{QTDIR}")

# Prefix
find_path(Qt6Config_PREFIX_PATH
  NAMES "lib/cmake/Qt6/Qt6Config.cmake"
  HINTS ${zHints}
  NO_DEFAULT_PATH)
mark_as_advanced(Qt6Config_PREFIX_PATH)

# Package
find_package_handle_standard_args(
  Qt6Config
  DEFAULT_MSG
  Qt6Config_PREFIX_PATH)

if(Qt6Config_FOUND)
  # Append
  if(NOT Qt6Config_PREFIX_PATH IN_LIST CMAKE_PREFIX_PATH)
    list(APPEND CMAKE_PREFIX_PATH "${Qt6Config_PREFIX_PATH}")
  endif()
  mark_as_advanced(FORCE Qt6_ROOT)

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

endif()
