55isEmpty( PYTHON_VERSION ) {
66 PYTHON_VERSION=$$(PYTHON_VERSION)
77}
8- isEmpty( PYTHON_VERSION ) {
9- win32:PYTHON_VERSION=27
10- unix:PYTHON_VERSION=2.7
8+
9+ isEmpty(PYTHON_DIR):PYTHON_DIR=$$(PYTHON_DIR)
10+ !isEmpty(PYTHON_DIR):error(Use PYTHON_PATH to choose exact Python installation instead of PYTHON_DIR)
11+
12+ isEmpty( PYTHON_PATH ) {
13+ PYTHON_PATH=$$(PYTHON_PATH)
14+ }
15+
16+ !isEmpty( PYTHON_PATH ) {
17+ PYTHON_PATH=$$system_path($${PYTHON_PATH}$${QMAKE_DIR_SEP})
1118}
1219
13- isEmpty( PYTHON_DIR ) {
14- PYTHON_DIR=$$(PYTHON_DIR)
20+ isEmpty(PYTHON_CMD):PYTHON_CMD=$$(PYTHON_CMD)
21+ isEmpty(PYTHON_CMD):PYTHON_CMD=python
22+ !isEmpty(PYTHON_PATH):PYTHON_CMD=$${PYTHON_PATH}$${QMAKE_DIR_SEP}$${PYTHON_CMD}
23+ contains(QMAKE_HOST.os, Windows):PYTHON_CMD=$${PYTHON_CMD}.exe
24+ PYTHON_CMD=$$system_path($$PYTHON_CMD)
25+ !isEmpty(PYTHON_PATH):!exists($$PYTHON_CMD):error("Incorrect PYTHON_PATH was set, missing $$PYTHON_CMD")
26+
27+ isEmpty(PYTHON_PATH): PYTHON_PATH=$$system($$system_quote($${PYTHON_CMD}) -c \
28+ $$system_quote(import os;import sys;print(os.path.dirname(sys.executable))))$${QMAKE_DIR_SEP}
29+
30+ #Not just "python --version" because Python 2 outputs `--version` to the stderr
31+ #And there is no simple way known for me to generally redirect stderr to stdout
32+ PYTHON_VERSION_DETECTED=$$system($$system_quote($${PYTHON_CMD}) -c $$system_quote(import sysconfig;print(sysconfig.get_python_version())))
33+
34+
35+ isEmpty( PYTHON_VERSION ) {
36+ warning(PYTHON_VERSION should be set explicitly from qmake command-line or via environment.\
37+ $$escape_expand(\\n\\t) F.e.: $$shell_quote(qmake PYTHON_VERSION=3.6 CONFIG+=release) \
38+ or $$shell_quote(set PYTHON_VERSION=2.7))
39+ message(Trying $${PYTHON_CMD} executable to guess the version)
40+ PYTHON_VERSION=$$PYTHON_VERSION_DETECTED
1541}
16- !isEmpty( PYTHON_DIR ) {
17- PYTHON_DIR=$${PYTHON_DIR}/
42+
43+ !equals(PYTHON_VERSION, $$PYTHON_VERSION_DETECTED){
44+ error("Found Python version $$PYTHON_VERSION_DETECTED, but $$PYTHON_VERSION was requested")
1845}
1946
2047PYTHON_VERSION_MAJOR=$$section(PYTHON_VERSION, ., 0, 0)
@@ -26,18 +53,22 @@ PYTHON_VERSION_MINOR=$$section(PYTHON_VERSION, ., 1, 1)
2653 message(Using Python version $${PYTHON_VERSION})
2754}
2855
29-
3056# Python 2.x has problems:
3157# 1) https://wiki.gentoo.org/wiki/Project:Python/Strict_aliasing
3258# 2) deprecated implicit cast of string literals to char*
33- equals(PYTHON_VERSION_MAJOR, 2):gcc:QMAKE_CXXFLAGS *= -fno-strict-aliasing -Wno-error=write-strings
59+ equals(PYTHON_VERSION_MAJOR, 2) {
60+ gcc:QMAKE_CXXFLAGS *= -fno-strict-aliasing -Wno-error=write-strings
61+ # Qt 5.4 adds this option, but this is not compatible with the Python API
62+ msvc: QMAKE_CXXFLAGS -= -Zc:strictStrings
63+ }
64+
3465contains(PKGCONFIG, "python.*"){
3566 CONFIG += link_pkgconfig
3667 PYTHON_PKGCONFIG = $$member($$unique($$find(PKGCONFIG, "python.*")), 1, 1)
3768 # add rpath
3869 PYTHON_LIBDIR = $$system($$pkgConfigExecutable() --libs-only-L $$PYTHON_PKGCONFIG)
3970 QMAKE_RPATHDIR += $$replace(PYTHON_LIBDIR,-L,)
40- } else:macx:isEmpty(PYTHON_DIR ){
71+ } else:macx:isEmpty(PYTHON_PATH ){
4172 # for macx you need to have the Python development kit installed as framework
4273 INCLUDEPATH += /System/Library/Frameworks/Python.framework/Headers
4374 LIBS += -F/System/Library/Frameworks -framework Python
@@ -63,13 +94,21 @@ contains(PKGCONFIG, "python.*"){
6394 DEBUG_EXT =
6495 }
6596
66- win32:INCLUDEPATH += $$(PYTHON_PATH)/PC $$(PYTHON_PATH)/include
67- win32:LIBS += $$(PYTHON_LIB)/python$${PYTHON_VERSION}$${DEBUG_EXT}.lib
97+ INCLUDEPATH += $$system($$system_quote($${PYTHON_CMD}) -c \
98+ $$system_quote(import sysconfig;print(\'\"%s\" \"%s\"\'%(sysconfig.get_paths()[\'include\'],sysconfig.get_paths()[\'platinclude\']))))
99+
100+
101+ LIBS += -L$${PYTHON_PATH}libs
102+ LIBS += -lpython$${PYTHON_VERSION_MAJOR}$${PYTHON_VERSION_MINOR}
103+
104+ # Hack for "CONFIG+=testcase" and 'make check' to add python's dll to PATH
105+ deppath += $$PYTHON_PATH
106+
68107} else:unix {
69108 # on linux, python-config is used to autodetect Python.
70109 # make sure that you have installed a matching python-dev package.
71110
72- PYTHON_CONFIG = $${PYTHON_DIR}/bin/ python$${PYTHON_VERSION}-config
111+ PYTHON_CONFIG = $${PYTHON_PATH} python$${PYTHON_VERSION}-config
73112 PYTHON_CONFIG_OPTIONS_LIBS = --libs
74113 equals(PYTHON_VERSION_MAJOR, 3):!lessThan(PYTHON_VERSION_MINOR, 8) {
75114 # Since 3.8 `--embed` is needed
0 commit comments