# profile to include and link Python # Change this variable to your python version (3.7, 3.8, ...) isEmpty( PYTHON_VERSION ) { PYTHON_VERSION=$$(PYTHON_VERSION) } isEmpty( PYTHON_VERSION ) { PYTHON_VERSION=3.10 } isEmpty( PYTHON_DIR ) { PYTHON_DIR=$$(PYTHON_DIR) } !isEmpty( PYTHON_DIR ) { PYTHON_DIR=$${PYTHON_DIR}/ } PYTHON_VERSION_MAJOR=$$section(PYTHON_VERSION, ., 0, 0) PYTHON_VERSION_MINOR=$$section(PYTHON_VERSION, ., 1, 1) !equals(PYTHON_VERSION, $${PYTHON_VERSION_MAJOR}.$${PYTHON_VERSION_MINOR}) { error("Failed to parse PYTHON_VERSION:\"$$PYTHON_VERSION\"") } else { message(Using Python version $${PYTHON_VERSION}) } # Python 2.x has problems: # 1) https://wiki.gentoo.org/wiki/Project:Python/Strict_aliasing # 2) deprecated implicit cast of string literals to char* equals(PYTHON_VERSION_MAJOR, 2) { gcc:QMAKE_CXXFLAGS *= -fno-strict-aliasing -Wno-write-strings # Qt 5.4 adds this option, but this is not compatible with the Python API msvc: QMAKE_CXXFLAGS -= -Zc:strictStrings } contains(PKGCONFIG, "python.*"){ # If `pkg-config` is configured, use `qmake PKGCONFIG+=python3.10-embed CONFIG+=...` # or `PKGCONFIG+=python2.7m`-like form for older versions, # see `pkg-config --list-all | grep python` for details. # This can help with GNU/Linux (including macOS with Homebrew), MSYS2/MinGW environment, # and also with OpenEmbedded and other cross-builds CONFIG += link_pkgconfig PYTHON_PKGCONFIG = $$member($$unique($$find(PKGCONFIG, "python.*")), 1, 1) # add rpath PYTHON_LIBDIR = $$system($$pkgConfigExecutable() --libs-only-L $$PYTHON_PKGCONFIG) QMAKE_RPATHDIR += $$replace(PYTHON_LIBDIR,-L,) } else:macx:isEmpty(PYTHON_DIR){ # for macx you need to have the Python development kit installed as framework INCLUDEPATH += /System/Library/Frameworks/Python.framework/Headers LIBS += -F/System/Library/Frameworks -framework Python } else:win32 { # for windows install a Python development kit or build Python yourself from the sources # Make sure that you set the environment variable PYTHON_PATH to point to your # python installation (or the python sources/header files when building from source). # # When using the prebuild Python installer, this will be: # set PYTHON_PATH = c:\Python310 # # When using the python sources, this will be something like: # set PYTHON_PATH = c:\yourDir\Python-3.10.12\ # check if debug or release CONFIG(debug, debug|release) { DEBUG_EXT = _d } else { DEBUG_EXT = } isEmpty(PYTHON_PATH):PYTHON_PATH=$(PYTHON_PATH) isEmpty(PYTHON_PATH)|!exists("$$PYTHON_PATH\\include") | !exists("$$PYTHON_PATH\\libs\\") { error("PYTHON_PATH must be set to correct folder with \\libs and \\include subfolders ") } #We need to destinguish 64-bit build to add a workaround option #The only known problematic case is MinGW with external (MSVC-built) Python2 mingw:equals(PYTHON_VERSION_MAJOR, 2): isEmpty(QMAKE_TARGET.arch):system(\ $$system_quote($$system_path($${PYTHON_PATH}/python.exe)) -c \ $$system_quote(import sysconfig;exit(0 if 0 <= sysconfig.get_platform().find(\'win-amd64\') else 1))\ ):DEFINES += MS_WIN64 INCLUDEPATH += $$shell_path($${PYTHON_PATH}/include) LIBS += $$shell_path(-L$${PYTHON_PATH}/libs) LIBS += -lpython$${PYTHON_VERSION_MAJOR}$${PYTHON_VERSION_MINOR}$${DEBUG_EXT} # Hack for "CONFIG+=testcase" and 'make check' to add python's dll to PATH deppath += $$shell_path($${PYTHON_PATH}) } else:unix { # on linux, python-config is used to autodetect Python. # make sure that you have installed a matching python-dev package. PYTHON_CONFIG = $${PYTHON_DIR}/bin/python$${PYTHON_VERSION}-config PYTHON_CONFIG_OPTIONS_LIBS = --libs equals(PYTHON_VERSION_MAJOR, 3):!lessThan(PYTHON_VERSION_MINOR, 8) { # Since 3.8 `--embed` is needed PYTHON_CONFIG_OPTIONS_LIBS += --embed } LIBS += $$system($${PYTHON_CONFIG} $${PYTHON_CONFIG_OPTIONS_LIBS}) QMAKE_CXXFLAGS += $$system($${PYTHON_CONFIG} --includes) PYTHON_LFLAGS = $$system($${PYTHON_CONFIG} --ldflags) QMAKE_LFLAGS += $${PYTHON_LFLAGS} # add rpath PYTHON_LIBDIR = $$find(PYTHON_LFLAGS,-L.*) PYTHON_RPATH = $$replace(PYTHON_LIBDIR,-L,) QMAKE_RPATHDIR += $$PYTHON_RPATH }