Skip to content

Commit

Permalink
Add FindPyQt6 cmake modules
Browse files Browse the repository at this point in the history
  • Loading branch information
manisandro authored and nyalldawson committed Nov 30, 2022
1 parent ebb606f commit 006c3c0
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
76 changes: 76 additions & 0 deletions cmake/FindPyQt6.cmake
@@ -0,0 +1,76 @@
# Find PyQt6
# ~~~~~~~~~~
# Copyright (c) 2007-2008, Simon Edwards <simon@simonzone.com>
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#
# PyQt6 website: http://www.riverbankcomputing.co.uk/pyqt/index.php
#
# Find the installed version of PyQt6. FindPyQt6 should only be called after
# Python has been found.
#
# This file defines the following variables:
#
# PYQT6_VERSION_STR - The version of PyQt6 as a human readable string.
#
# PYQT6_SIP_DIR - The directory holding the PyQt6 .sip files.
#
# PYQT6_SIP_FLAGS - The SIP flags used to build PyQt.

IF(EXISTS PYQT6_VERSION_STR)
# Already in cache, be silent
SET(PYQT6_FOUND TRUE)
ELSE(EXISTS PYQT6_VERSION_STR)

IF(SIP_BUILD_EXECUTABLE)
# SIP >= 5.0 path

FILE(GLOB _pyqt6_metadata "${Python_SITEARCH}/PyQt6-*.dist-info/METADATA")
IF(_pyqt6_metadata)
FILE(READ ${_pyqt6_metadata} _pyqt6_metadata_contents)
STRING(REGEX REPLACE ".*\nVersion: ([^\n]+).*$" "\\1" PYQT6_VERSION_STR ${_pyqt6_metadata_contents})
ELSE(_pyqt6_metadata)
EXECUTE_PROCESS(COMMAND ${Python_EXECUTABLE} -c "from PyQt6.QtCore import PYQT_VERSION_STR; print(PYQT_VERSION_STR)" OUTPUT_VARIABLE PYQT6_VERSION_STR)
ENDIF(_pyqt6_metadata)

IF(PYQT6_VERSION_STR)
SET(PYQT6_MOD_DIR "${Python_SITEARCH}/PyQt6")
SET(PYQT6_SIP_DIR "${Python_SITEARCH}/PyQt6/bindings")
FIND_PROGRAM(__pyuic6 "pyuic6")
GET_FILENAME_COMPONENT(PYQT6_BIN_DIR ${__pyuic6} DIRECTORY)

SET(PYQT6_FOUND TRUE)
ENDIF(PYQT6_VERSION_STR)

ELSE(SIP_BUILD_EXECUTABLE)
# SIP 4.x path

FIND_FILE(_find_pyqt6_py FindPyQt6.py PATHS ${CMAKE_MODULE_PATH} NO_CMAKE_FIND_ROOT_PATH)

EXECUTE_PROCESS(COMMAND ${Python_EXECUTABLE} ${_find_pyqt6_py} OUTPUT_VARIABLE pyqt_config)
IF(pyqt_config)
STRING(REGEX REPLACE "^pyqt_version_str:([^\n]+).*$" "\\1" PYQT6_VERSION_STR ${pyqt_config})
STRING(REGEX REPLACE ".*\npyqt_mod_dir:([^\n]+).*$" "\\1" PYQT6_MOD_DIR ${pyqt_config})
STRING(REGEX REPLACE ".*\npyqt_sip_dir:([^\n]+).*$" "\\1" PYQT6_SIP_DIR ${pyqt_config})
IF(EXISTS ${PYQT6_SIP_DIR}/Qt6)
SET(PYQT6_SIP_DIR ${PYQT6_SIP_DIR}/Qt6)
ENDIF(EXISTS ${PYQT6_SIP_DIR}/Qt6)
STRING(REGEX REPLACE ".*\npyqt_sip_flags:([^\n]+).*$" "\\1" PYQT6_SIP_FLAGS ${pyqt_config})
STRING(REGEX REPLACE ".*\npyqt_bin_dir:([^\n]+).*$" "\\1" PYQT6_BIN_DIR ${pyqt_config})
STRING(REGEX REPLACE ".*\npyqt_sip_module:([^\n]+).*$" "\\1" PYQT6_SIP_IMPORT ${pyqt_config})
SET(PYQT6_FOUND TRUE)
ENDIF(pyqt_config)

ENDIF(SIP_BUILD_EXECUTABLE)

IF(PYQT6_FOUND)
IF(NOT PyQt6_FIND_QUIETLY)
MESSAGE(STATUS "Found PyQt6 version: ${PYQT6_VERSION_STR}")
ENDIF(NOT PyQt6_FIND_QUIETLY)
ELSE(PYQT6_FOUND)
IF(PyQt6_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find PyQt6")
ENDIF(PyQt6_FIND_REQUIRED)
ENDIF(PYQT6_FOUND)

ENDIF(EXISTS PYQT6_VERSION_STR)
59 changes: 59 additions & 0 deletions cmake/FindPyQt6.py
@@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2007, Simon Edwards <simon@simonzone.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the Simon Edwards <simon@simonzone.com> nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY Simon Edwards <simon@simonzone.com> ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL Simon Edwards <simon@simonzone.com> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# FindPyQt.py
# Copyright (c) 2007, Simon Edwards <simon@simonzone.com>
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.

import os.path
import PyQt6.QtCore
import sipconfig
import sys

cfg = sipconfig.Configuration()
sip_dir = cfg.default_sip_dir
for p in (os.path.join(sip_dir, "PyQt6"),
os.path.join(sip_dir, "PyQt6-3"),
sip_dir,
os.path.join(cfg.default_mod_dir, "PyQt6", "bindings")):
if os.path.exists(os.path.join(p, "QtCore", "QtCoremod.sip")):
sip_dir = p
break

print("pyqt_version_str:%s" % PyQt6.QtCore.PYQT_VERSION_STR)
print("pyqt_mod_dir:%s" % os.path.join(cfg.default_mod_dir, "PyQt6"))
print("pyqt_sip_dir:%s" % sip_dir)
print("pyqt_sip_flags:%s" % PyQt6.QtCore.PYQT_CONFIGURATION['sip_flags'])
print("pyqt_bin_dir:%s" % cfg.default_bin_dir)

try:
import PyQt6.sip

print("pyqt_sip_module:PyQt6.sip")
except Exception as e:
print("pyqt_sip_module:sip")

0 comments on commit 006c3c0

Please sign in to comment.