Salome HOME
[bos #32481] [CEA] PyQt5 qobject.sip detection Fedora 36
[tools/configuration.git] / cmake / FindPyQt5.cmake
1 # Copyright (C) 2013-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 # - Find PyQt5 installation
21 # Sets the following variables:
22 #    PYQT_PYUIC_EXECUTABLE  - path to the pyuic executable
23 #    PYQT_PYRCC_EXECUTABLE  - path to the pyrcc executable
24 #    PYQT_PYUIC_PATH        - command to launch pyuic with the correct PYTHONPATH
25 #    PYQT_PYRCC_PATH        - command to launch pyrcc with the correct PYTHONPATH
26 #    PYQT_PYTHONPATH        - path to the PyQt Python modules
27 #    PYQT_SIPS_DIR          - path to main include directory (which contains several sub folders)
28 #    PYQT_SIPFLAGS          - compilation flags extracted from PyQt
29 #
30 #  The executables
31 #      pyuic5 (pyuic5.bat)
32 #  are searched.
33 #  The Python command 
34 #      pyqtconfig.Configuration().pyqt_sip_flags
35 #  is called to get the compilation flags.
36 #  Headers are located by looking for the header file
37 #      qobject.sip
38
39 IF(NOT PyQt5_FIND_QUIETLY)
40   MESSAGE(STATUS "Looking for PyQt5 ...")
41 ENDIF()
42
43 IF(NOT SIP_FOUND AND NOT PyQt5_FIND_QUIETLY)
44    MESSAGE(WARNING "PyQt5 needs SIP to be detected correctly!")
45 ENDIF()
46
47 FIND_PROGRAM(PYQT_PYUIC_EXECUTABLE NAMES pyuic5 pyuic5.bat)
48 FIND_PROGRAM(PYQT_PYRCC_EXECUTABLE NAMES pyrcc5 pyrcc5.bat)
49
50 # Get root dir locally, going up two levels from the exec:
51 GET_FILENAME_COMPONENT(_tmp_ROOT_DIR "${PYQT_PYUIC_EXECUTABLE}" PATH)
52 GET_FILENAME_COMPONENT(_tmp_ROOT_DIR "${_tmp_ROOT_DIR}" PATH)
53
54
55 # Typical locations of qobject.sip are: 
56 #   - /usr/share/sip/PyQt5/QtCore, for a system install
57 #   - or <xyz>/sip/QtCore, for a custom install
58 #   - or <xyz>/share/sip/QtCore, for a custom install
59 FIND_FILE(PYQT_SIP_MAIN_FILE qobject.sip PATH_SUFFIXES share/sip/QtCore sip/QtCore share/sip/PyQt5/QtCore lib/python3/dist-packages/PyQt5/bindings/QtCore lib64/python3.10/site-packages/PyQt5/bindings/QtCore)
60
61 IF(PYQT_SIP_MAIN_FILE)
62   GET_FILENAME_COMPONENT(PYQT_SIPS_DIR "${PYQT_SIP_MAIN_FILE}" PATH)
63   GET_FILENAME_COMPONENT(PYQT_SIPS_DIR "${PYQT_SIPS_DIR}" PATH)
64 ENDIF()
65 MARK_AS_ADVANCED(PYQT_SIP_MAIN_FILE)
66
67 # Get PyQt compilation flags:
68 SET(PYQT_PYTHONPATH "${_tmp_ROOT_DIR};${_tmp_ROOT_DIR}/PyQt5;${_tmp_ROOT_DIR}/lib/python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}/site-packages/PyQt5")
69 SET(PYQT_SIPFLAGS)
70
71 EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c "import sys; 
72 sys.path[:0] = '${PYQT_PYTHONPATH}'.split(';');
73 sys.path[:0] = '${SIP_PYTHONPATH}'.split(';');
74 from PyQt5.QtCore import PYQT_CONFIGURATION;
75 sys.stdout.write(PYQT_CONFIGURATION['sip_flags'])"
76   OUTPUT_VARIABLE PYQT_SIPFLAGS)
77 SEPARATE_ARGUMENTS(PYQT_SIPFLAGS)
78
79 SET(PYQT_SIPFLAGS ${PYQT_SIPFLAGS} -I "${PYQT_SIPS_DIR}")
80
81 INCLUDE(FindPackageHandleStandardArgs)
82 FIND_PACKAGE_HANDLE_STANDARD_ARGS(PyQt5 REQUIRED_VARS PYQT_PYUIC_EXECUTABLE PYQT_PYRCC_EXECUTABLE PYQT_SIPS_DIR PYQT_SIPFLAGS )
83
84 # Wrap the final executables so that they always use the proper environment (PYTHONPATH):
85 # The results are put to variables:
86 #   PYQT_PYUIC_PATH - command to launch pyuic with the correct PYTHONPATH
87 #   PYQT_PYRCC_PATH - command to launch pyrcc with the correct PYTHONPATH
88 # TODO: should be done like Sphinx in KERNEL (i.e. generating a shell script)?
89
90 IF(WIN32 AND NOT CYGWIN)
91   SET(PYQT_PYUIC_PATH set PYTHONPATH=${PYQT_PYTHONPATH}\\\;${SIP_PYTHONPATH}\\\;%PYTHONPATH% && ${PYQT_PYUIC_EXECUTABLE})
92   SET(PYQT_PYRCC_PATH set PYTHONPATH=${PYQT_PYTHONPATH}\\\;${SIP_PYTHONPATH}\\\;%PYTHONPATH% && ${PYQT_PYRCC_EXECUTABLE})
93 ELSE()
94   STRING(REPLACE ";" ":" PYQT_PYTHONPATH "${PYQT_PYTHONPATH}")
95   SET(PYQT_PYUIC_PATH /usr/bin/env PYTHONPATH="${PYQT_PYTHONPATH}:${SIP_PYTHONPATH}:$$PYTHONPATH" ${PYQT_PYUIC_EXECUTABLE})
96   SET(PYQT_PYRCC_PATH /usr/bin/env PYTHONPATH="${PYQT_PYTHONPATH}:${SIP_PYTHONPATH}:$$PYTHONPATH" ${PYQT_PYRCC_EXECUTABLE})
97 ENDIF()