[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    [PyQt] Howto: build PyQt against Qt5 on Windows
From:       Mathias.Born () gmx ! de
Date:       2013-01-27 22:56:54
Message-ID: 1682754403.20130127235654 () gmx ! de
[Download RAW message or body]

Hi,

I'm so impressed by QML scene graph performance, I want it now. So this is
what I had to do to make PyQt build on Windows against Qt5, without webkit.
(sip-4.14.3-snapshot-dc06058c99dd and PyQt-win-gpl-snapshot-4.10-f0118624625e)

Attached is a new configure.py for PyQt and and new siputils.py for sip
(sorry, diff yourself).

Necessary changes to files:


qnetworkreply.sip:147

%If (PyQt_OpenSSL)
%If (Qt_5_0_0 -)
    virtual void sslConfigurationImplementation(QSslConfiguration &) const;
%End
%If (Qt_5_0_0 -)
    virtual void setSslConfigurationImplementation(const QSslConfiguration &);
%End
%If (Qt_5_0_0 -)
    virtual void ignoreSslErrorsImplementation(const QList<QSslError> &);
%End
%End

qsslcertificateextension.sip:
Guard everything with
%If (PyQt_OpenSSL)
%End


qcoreapplication.sip:286

%If (- Qt_4_8_4)
    virtual bool winEventFilter(MSG *message, long *result);
%End

Note: Implement QCoreApplication::installNativeEventFilter and \
QCoreApplication::removeNativeEventFilter instead.


qabstracteventdispatcher.sip:62

%If (WS_WIN)
        virtual bool registerEventNotifier(QWinEventNotifier * notifier) = 0;
        virtual void unregisterEventNotifier(QWinEventNotifier * notifier) = 0;
%End


QtCoremod.sip:136

%Include qwineventnotifier.sip


qwidget.sip:368

%If (- Qt_4_8_4)
    virtual bool winEvent(MSG *message, long *result);
%End

409:
%If (- Qt_4_8_4)
    WId winId() const;
%End

439:
%If (Qt_4_4_0 - Qt_4_8_4)
    WId effectiveWinId() const;
%End

(The winId-methods still exist, but "WId" is an integral type in Qt5.)



qwizard.sip:145

%If (- Qt_4_8_4)
    bool winEvent(MSG *message, long *result);
%End


qsizegrip.sip:52

%If (- Qt_4_8_4)
    bool winEvent(MSG *m, long *result);
%End




qprinter.sip:178

%If (!WS_WIN)
    QString printerSelectionOption() const;
    void setPrinterSelectionOption(const QString &);
%End


qpixmap.sip:157

%If (- Qt_4_8_4)
%If (WS_WIN)
// Windows specific conversion functions.
enum HBitmapFormat
{
    NoAlpha,
    PremultipliedAlpha,
%If (Qt_4_5_0 -)
    Alpha,
%End
};

HBITMAP toWinHBITMAP(QPixmap::HBitmapFormat format = QPixmap::NoAlpha) const;
static QPixmap fromWinHBITMAP(HBITMAP bitmap, QPixmap::HBitmapFormat format = \
QPixmap::NoAlpha);

%If (Qt_4_6_0 -)
HICON toWinHICON() const;
static QPixmap fromWinHICON(HICON icon);
%End
%End
%End



In Qt5, the QtGui module depends on Qt5PrintSupportd.lib.

Add NOMINMAX to the DEFINES list of qmake.conf in the mkspecs folder corresponding to \
your compiler (e.g. build\mkspecs\win32-msvc2012 for VS2012).

Remove pylupdate from the main Makefile. Needs further attention.


Phil, may this help you get things to work on Windows.

Best Regards,
Mathias Born


["qwineventnotifier.sip" (APPLICATION/OCTET-STREAM)]
["siputils.py" (text/plain)]

# This module is intended to be used by the build/installation scripts of
# extension modules created with SIP.  It provides information about file
# locations, version numbers etc., and provides some classes and functions.
#
# Copyright (c) 2013 Riverbank Computing Limited <info@riverbankcomputing.com>
#
# This file is part of SIP.
#
# This copy of SIP is licensed for use under the terms of the SIP License
# Agreement.  See the file LICENSE for more details.
#
# This copy of SIP may also used under the terms of the GNU General Public
# License v2 or v3 as published by the Free Software Foundation which can be
# found in the files LICENSE-GPL2 and LICENSE-GPL3 included in this package.
#
# SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


import sys
import os
import stat
import string
import re


# These are installation specific values created when SIP was configured.
# @SIP_CONFIGURATION@

# The stack of configuration dictionaries.
_config_stack = []


class Configuration(object):
    """The class that represents SIP configuration values.
    """
    def __init__(self, sub_cfg=None):
        """Initialise an instance of the class.

        sub_cfg is the list of sub-class configurations.  It should be None
        when called normally.
        """
        # Find the build macros in the closest imported module from where this
        # was originally defined.
        self._macros = None

        for cls in self.__class__.__mro__:
            if cls is object:
                continue

            mod = sys.modules[cls.__module__]

            if hasattr(mod, "_default_macros"):
                self._macros = mod._default_macros
                break

        if sub_cfg:
            cfg = sub_cfg
        else:
            cfg = []

        cfg.append(_pkg_config)

        global _config_stack
        _config_stack = cfg

    def __getattr__(self, name):
        """Allow configuration values and user options to be handled as
        instance variables.

        name is the name of the configuration value or user option.
        """
        for cfg in _config_stack:
            try:
                return cfg[name]
            except KeyError:
                pass

        raise AttributeError("\"%s\" is not a valid configuration value or user \
option" % name)

    def build_macros(self):
        """Return the dictionary of platform specific build macros.
        """
        return self._macros

    def set_build_macros(self, macros):
        """Set the dictionary of build macros to be use when generating
        Makefiles.

        macros is the dictionary of platform specific build macros.
        """
        self._macros = macros


class _UniqueList:
    """A limited list that ensures all its elements are unique.
    """
    def __init__(self, value=None):
        """Initialise the instance.

        value is the initial value of the list.
        """
        if value is None:
            self._list = []
        else:
            self._list = value

    def append(self, value):
        """Append a value to the list if it isn't already present.

        value is the value to append.
        """
        if value not in self._list:
            self._list.append(value)

    def lextend(self, value):
        """A normal list extend ignoring the uniqueness.

        value is the list of elements to append.
        """
        self._list.extend(value)

    def extend(self, value):
        """Append each element of a value to a list if it isn't already
        present.

        value is the list of elements to append.
        """
        for el in value:
            self.append(el)

    def as_list(self):
        """Return the list as a raw list.
        """
        return self._list


class _Macro:
    """A macro that can be manipulated as a list.
    """
    def __init__(self, name, value):
        """Initialise the instance.

        name is the name of the macro.
        value is the initial value of the macro.
        """
        self._name = name
        self.set(value)

    def set(self, value):
        """Explicitly set the value of the macro.

        value is the new value.  It may be a string, a list of strings or a
        _UniqueList instance.
        """
        self._macro = []

        if isinstance(value, _UniqueList):
            value = value.as_list()

        if type(value) == list:
            self.extend(value)
        else:
            self.append(value)

    def append(self, value):
        """Append a value to the macro.

        value is the value to append.
        """
        if value:
            self._macro.append(value)

    def extend(self, value):
        """Append each element of a value to the macro.

        value is the list of elements to append.
        """
        for el in value:
            self.append(el)

    def remove(self, value): 
        """Remove a value from the macro.  It doesn't matter if the value 
        wasn't present. 
 
        value is the value to remove. 
        """ 
        try: 
            self._macro.remove(value) 
        except: 
            pass 

    def as_list(self):
        """Return the macro as a list.
        """
        return self._macro


class Makefile:
    """The base class for the different types of Makefiles.
    """
    def __init__(self, configuration, console=0, qt=0, opengl=0, python=0,
                 threaded=0, warnings=1, debug=0, dir=None,
                 makefile="Makefile", installs=None, universal=None,
                 arch=None, deployment_target=None):
        """Initialise an instance of the target.  All the macros are left
        unchanged allowing scripts to manipulate them at will.

        configuration is the current configuration.
        console is set if the target is a console (rather than windows) target.
        qt is set if the target uses Qt.  For Qt v4 a list of Qt libraries may
        be specified and a simple non-zero value implies QtCore and QtGui.
        opengl is set if the target uses OpenGL.
        python is set if the target #includes Python.h.
        debug is set to generated a debugging version of the target.
        threaded is set if the target requires thread support.  It is
        automatically set if the target uses Qt and Qt has thread support
        enabled.
        warnings is set if compiler warning messages are required.
        debug is set if debugging symbols should be generated.
        dir is the directory for build files and Makefiles.
        makefile is the name of the Makefile.
        installs is a list of extra install targets.  Each element is a two
        part list, the first of which is the source and the second is the
        destination.  If the source is another list then it is a set of source
        files and the destination is a directory.
        universal is the name of the SDK if the target is a MacOS/X universal
        binary.  If it is None then the value is taken from the configuration.
        arch is the space separated MacOS/X architectures to build.  If it is
        None then it is taken from the configuration.
        deployment_target MacOS/X deployment target.  If it is None then it is
        taken from the configuration.
        """
        if qt:
            if not hasattr(configuration, "qt_version"):
                error("The target uses Qt but pyqtconfig has not been imported.")

            # For Qt v4 interpret Qt support as meaning link against the core
            # and GUI libraries (which corresponds to the default qmake
            # configuration).  Also allow a list of Qt v4 modules to be
            # specified.
            if configuration.qt_version >= 0x040000:
                if type(qt) != list:
                    qt = ["QtCore", "QtGui"]

            self._threaded = configuration.qt_threaded
        else:
            self._threaded = threaded

        self.config = configuration
        self.console = console
        self._qt = qt
        self._opengl = opengl
        self._python = python
        self._warnings = warnings
        self._debug = debug
        self._makefile = makefile
        self._installs = installs
        self._infix = ""

        # Make sure the destination directory is an absolute path.
        if dir:
            self.dir = os.path.abspath(dir)
        else:
            self.dir = os.path.curdir

        # Assume we are building in the source tree.
        self._src_dir = self.dir

        if universal is None:
            self._universal = configuration.universal
        else:
            self._universal = universal

        if arch is None:
            self._arch = configuration.arch
        else:
            self._arch = arch

        if deployment_target is None:
            self._deployment_target = configuration.deployment_target
        else:
            self._deployment_target = deployment_target

        self._finalised = 0

        # Copy the macros and convert them all to instance lists.
        macros = configuration.build_macros()

        for m in list(macros.keys()):
            # Allow the user to override the default.
            try:
                val = getattr(configuration, m)
            except AttributeError:
                val = macros[m]

            # These require special handling as they are (potentially) a set of
            # space separated values rather than a single value that might
            # contain spaces.
            if m in ("DEFINES", "CONFIG") or m[:6] in ("INCDIR", "LIBDIR"):
                val = val.split()

            # We also want to treat lists of libraries in the same way so that
            # duplicates get eliminated.
            if m[:4] == "LIBS":
                val = val.split()

            self.__dict__[m] = _Macro(m, val)

        # This is used to alter the configuration more significantly than can
        # be done with just configuration files.
        self.generator = self.optional_string("MAKEFILE_GENERATOR", "UNIX")

        # These are what configuration scripts normally only need to change.
        self.extra_cflags = []
        self.extra_cxxflags = []
        self.extra_defines = []
        self.extra_include_dirs = []
        self.extra_lflags = []
        self.extra_lib_dirs = []
        self.extra_libs = []

        # Get these once and make them available to sub-classes.
        if sys.platform == "win32":
            def_copy = "copy"
            def_rm = "del"
            def_mkdir = "mkdir"
            def_chk_dir_exists = "if not exist"
        else:
            def_copy = "cp -f"
            def_rm = "rm -f"
            def_mkdir = "mkdir -p"
            def_chk_dir_exists = "test -d"

        self.copy = self.optional_string("COPY", def_copy)
        self.rm = self.optional_string("DEL_FILE", def_rm)
        self.mkdir = self.optional_string("MKDIR", def_mkdir)
        self.chkdir = self.optional_string("CHK_DIR_EXISTS", def_chk_dir_exists)


    def finalise(self):
        """Finalise the macros by doing any consolidation that isn't specific
        to a Makefile.
        """
        # Extract the things we might need from the Windows Qt configuration.
        # Note that we used to think that if Qt was built with exceptions, RTTI
        # and STL support enabled then anything that linked against it also
        # needed the same flags.  However, detecting this was broken for some
        # time and nobody complained.  For the moment we'll leave the code in
        # but it will never be used.
        if self._qt:
            wcfg = self.config.qt_winconfig.split()
            win_shared = ("shared" in wcfg)
            win_exceptions = ("exceptions" in wcfg)
            win_rtti = ("rtti" in wcfg)
            win_stl = ("stl" in wcfg)

            qt_version = self.config.qt_version
        else:
            win_shared = 1
            win_exceptions = 0
            win_rtti = 0
            win_stl = 0

            qt_version = 0

        # Get what we are going to transform.
        cflags = _UniqueList()
        cflags.extend(self.extra_cflags)
        cflags.extend(self.optional_list("CFLAGS"))

        cxxflags = _UniqueList()
        cxxflags.extend(self.extra_cxxflags)
        cxxflags.extend(self.optional_list("CXXFLAGS"))

        defines = _UniqueList()
        defines.extend(self.extra_defines)
        defines.extend(self.optional_list("DEFINES"))

        incdir = _UniqueList(["."])
        incdir.extend(self.extra_include_dirs)
        incdir.extend(self.optional_list("INCDIR"))

        lflags = _UniqueList()
        lflags.extend(self.extra_lflags)
        lflags.extend(self.optional_list("LFLAGS"))

        libdir = _UniqueList()
        libdir.extend(self.extra_lib_dirs)
        libdir.extend(self.optional_list("LIBDIR"))

        # Handle MacOS/X specific configuration.
        if sys.platform == 'darwin':
            mac_cflags = []
            mac_lflags = []

            for a in self._arch.split():
                aflag = '-arch ' + a
                mac_cflags.append(aflag)
                mac_lflags.append(aflag)

            if self._universal:
                mac_cflags.append('-isysroot %s' % self._universal)
                mac_lflags.append('-Wl,-syslibroot,%s' % self._universal)

            cflags.lextend(mac_cflags)
            cxxflags.lextend(mac_cflags)
            lflags.lextend(mac_lflags)

        # Don't use a unique list as libraries may need to be searched more
        # than once.  Also MacOS/X uses the form "-framework lib" so we don't
        # want to lose the multiple "-framework".
        libs = []

        for l in self.extra_libs:
            libs.append(self.platform_lib(l))

            if self._qt:
                libs.extend(self._dependent_libs(l))

        libs.extend(self.optional_list("LIBS"))

        rpaths = _UniqueList()

        for l in self.extra_lib_dirs:
            l_dir = os.path.dirname(l)

            # This is a hack to ignore PyQt's internal support libraries.
            if '/qpy/' in l_dir:
                continue

            # Ignore relative directories.  This is really a hack to handle
            # SIP v3 inter-module linking.
            if l_dir in ("", ".", ".."):
                continue

            rpaths.append(l)

        if self._python:
            incdir.append(self.config.py_inc_dir)
            incdir.append(self.config.py_conf_inc_dir)

            if sys.platform == "cygwin":
                libdir.append(self.config.py_lib_dir)

                py_lib = "python%u.%u" % ((self.config.py_version >> 16), \
((self.config.py_version >> 8) & 0xff))  libs.append(self.platform_lib(py_lib))
            elif sys.platform == "win32":
                libdir.append(self.config.py_lib_dir)

                py_lib = "python%u%u" % ((self.config.py_version >> 16), \
((self.config.py_version >> 8) & 0xff))

                # For Borland use the OMF version of the Python library if it
                # exists, otherwise assume that Python was built with Borland
                # and use the normal library.
                if self.generator == "BMAKE":
                    bpy_lib = py_lib + "_bcpp"
                    bpy_lib_path = os.path.join(self.config.py_lib_dir, \
self.platform_lib(bpy_lib))

                    if os.access(bpy_lib_path, os.F_OK):
                        py_lib = bpy_lib

                if self._debug:
                    py_lib = py_lib + "_d"

                    if self.generator != "MINGW":
                        cflags.append("/D_DEBUG")
                        cxxflags.append("/D_DEBUG")

                libs.append(self.platform_lib(py_lib))

        if self.generator in ("MSVC", "MSVC.NET", "MSBUILD", "BMAKE"):
            if win_exceptions:
                cflags_exceptions = "CFLAGS_EXCEPTIONS_ON"
                cxxflags_exceptions = "CXXFLAGS_EXCEPTIONS_ON"
            else:
                cflags_exceptions = "CFLAGS_EXCEPTIONS_OFF"
                cxxflags_exceptions = "CXXFLAGS_EXCEPTIONS_OFF"

            cflags.extend(self.optional_list(cflags_exceptions))
            cxxflags.extend(self.optional_list(cxxflags_exceptions))

            if win_rtti:
                cflags_rtti = "CFLAGS_RTTI_ON"
                cxxflags_rtti = "CXXFLAGS_RTTI_ON"
            else:
                cflags_rtti = "CFLAGS_RTTI_OFF"
                cxxflags_rtti = "CXXFLAGS_RTTI_OFF"

            cflags.extend(self.optional_list(cflags_rtti))
            cxxflags.extend(self.optional_list(cxxflags_rtti))

            if win_stl:
                cflags_stl = "CFLAGS_STL_ON"
                cxxflags_stl = "CXXFLAGS_STL_ON"
            else:
                cflags_stl = "CFLAGS_STL_OFF"
                cxxflags_stl = "CXXFLAGS_STL_OFF"

            cflags.extend(self.optional_list(cflags_stl))
            cxxflags.extend(self.optional_list(cxxflags_stl))

        if self._debug:
            if win_shared:
                cflags_mt = "CFLAGS_MT_DLLDBG"
                cxxflags_mt = "CXXFLAGS_MT_DLLDBG"
            else:
                cflags_mt = "CFLAGS_MT_DBG"
                cxxflags_mt = "CXXFLAGS_MT_DBG"

            cflags_debug = "CFLAGS_DEBUG"
            cxxflags_debug = "CXXFLAGS_DEBUG"
            lflags_debug = "LFLAGS_DEBUG"
        else:
            if win_shared:
                cflags_mt = "CFLAGS_MT_DLL"
                cxxflags_mt = "CXXFLAGS_MT_DLL"
            else:
                cflags_mt = "CFLAGS_MT"
                cxxflags_mt = "CXXFLAGS_MT"

            cflags_debug = "CFLAGS_RELEASE"
            cxxflags_debug = "CXXFLAGS_RELEASE"
            lflags_debug = "LFLAGS_RELEASE"

        if self.generator in ("MSVC", "MSVC.NET", "MSBUILD", "BMAKE"):
            if self._threaded:
                cflags.extend(self.optional_list(cflags_mt))
                cxxflags.extend(self.optional_list(cxxflags_mt))

            if self.console:
                cflags.extend(self.optional_list("CFLAGS_CONSOLE"))
                cxxflags.extend(self.optional_list("CXXFLAGS_CONSOLE"))

        cflags.extend(self.optional_list(cflags_debug))
        cxxflags.extend(self.optional_list(cxxflags_debug))
        lflags.extend(self.optional_list(lflags_debug))

        if self._warnings:
            cflags_warn = "CFLAGS_WARN_ON"
            cxxflags_warn = "CXXFLAGS_WARN_ON"
        else:
            cflags_warn = "CFLAGS_WARN_OFF"
            cxxflags_warn = "CXXFLAGS_WARN_OFF"

        cflags.extend(self.optional_list(cflags_warn))
        cxxflags.extend(self.optional_list(cxxflags_warn))

        if self._threaded:
            cflags.extend(self.optional_list("CFLAGS_THREAD"))
            cxxflags.extend(self.optional_list("CXXFLAGS_THREAD"))
            lflags.extend(self.optional_list("LFLAGS_THREAD"))

        if self._qt:
            # Get the name of the mkspecs directory.
            try:
                specd_base = self.config.qt_data_dir
            except AttributeError:
                specd_base = self.config.qt_dir

            mkspecs = os.path.join(specd_base, "mkspecs")

            if self.generator != "UNIX" and win_shared:
                defines.append("QT_DLL")

            if not self._debug:
                defines.append("QT_NO_DEBUG")

            if qt_version >= 0x040000:
                for mod in self._qt:
                    # Note that qmake doesn't define anything for QtHelp.
                    if mod == "QtCore":
                        defines.append("QT_CORE_LIB")
                    elif mod == "QtDeclarative":
                        defines.append("QT_DECLARATIVE_LIB")
                    elif mod == "QtGui":
                        defines.append("QT_GUI_LIB")
                    elif mod == "QtMultimedia":
                        defines.append("QT_MULTIMEDIA_LIB")
                    elif mod == "QtNetwork":
                        defines.append("QT_NETWORK_LIB")
                    elif mod == "QtOpenGL":
                        defines.append("QT_OPENGL_LIB")
                    elif mod == "QtScript":
                        defines.append("QT_SCRIPT_LIB")
                    elif mod == "QtScriptTools":
                        defines.append("QT_SCRIPTTOOLS_LIB")
                    elif mod == "QtSql":
                        defines.append("QT_SQL_LIB")
                    elif mod == "QtTest":
                        defines.append("QT_TEST_LIB")
                    elif mod == "QtWebKit":
                        defines.append("QT_WEBKIT_LIB")
                    elif mod == "QtXml":
                        defines.append("QT_XML_LIB")
                    elif mod == "QtXmlPatterns":
                        defines.append("QT_XMLPATTERNS_LIB")
                    elif mod == "phonon":
                        defines.append("QT_PHONON_LIB")

                    if qt_version >= 0x050000:
                        if mod == "QtTest":
                            defines.append("QT_GUI_LIB")

                        if mod in ("QtSql", "QtTest"):
                            defines.append("QT_WIDGETS_LIB")

            elif self._threaded:
                defines.append("QT_THREAD_SUPPORT")

            # Handle library directories.
            libdir_qt = self.optional_list("LIBDIR_QT")
            libdir.extend(libdir_qt)
            rpaths.extend(libdir_qt)

            if qt_version >= 0x040000:
                # Try and read QT_LIBINFIX from qconfig.pri.
                qconfig = os.path.join(mkspecs, "qconfig.pri")
                self._infix = self._extract_value(qconfig, "QT_LIBINFIX")

                # For Windows: the macros that define the dependencies on
                # Windows libraries.
                wdepmap = {
                    "QtCore":       "LIBS_CORE",
                    "QtGui":        "LIBS_GUI",
                    "QtNetwork":    "LIBS_NETWORK",
                    "QtOpenGL":     "LIBS_OPENGL",
                    "QtWebKit":     "LIBS_WEBKIT"
                }

                # For Windows: the dependencies between Qt libraries.
                qdepmap = {
                    "QtAssistant":      ("QtNetwork", "QtGui", "QtCore"),
                    "QtDeclarative":    ("QtNetwork", "QtGui", "QtCore", "QtWidgets", \
                "QtXmlPatterns", "QtScript", "QtSql"),
                    "QtGui":            ("QtCore", "QtWidgets", "QtPrintSupport"), # \
                QtWidgets wrong here 
                    "QtHelp":           ("QtSql", "QtGui", "QtCore", "QtWidgets", \
"QtNetwork"),  "QtMultimedia":     ("QtGui", "QtCore"),
                    "QtNetwork":        ("QtCore", ),
                    "QtOpenGL":         ("QtGui", "QtCore", "QtWidgets"),
                    "QtScript":         ("QtCore", ),
                    "QtScriptTools":    ("QtScript", "QtGui", "QtCore"),
                    "QtSql":            ("QtCore", ),
                    "QtSvg":            ("QtXml", "QtGui", "QtCore", "QtWidgets"),
                    "QtTest":           ("QtGui", "QtCore"),
                    "QtWebKit":         ("QtNetwork", "QtGui", "QtCore"),
                    "QtXml":            ("QtCore", ),
                    "QtXmlPatterns":    ("QtNetwork", "QtCore"),
                    "phonon":           ("QtGui", "QtCore"),
                    "QtDesigner":       ("QtGui", "QtCore"),
                    "QAxContainer":     ("QtGui", "QtCore")
                }

                # The QtSql .prl file doesn't include QtGui as a dependency (at
                # least on Linux) so we explcitly set the dependency here for
                # everything.
                if "QtSql" in self._qt:
                    if "QtGui" not in self._qt:
                        self._qt.append("QtGui")

                # With Qt v4.2.0, the QtAssistantClient library is now a shared
                # library on UNIX. The QtAssistantClient .prl file doesn't
                # include QtGui and QtNetwork as a dependency any longer.  This
                # seems to be a bug in Qt v4.2.0.  We explicitly set the
                # dependencies here.
                if qt_version >= 0x040200 and "QtAssistant" in self._qt:
                    if "QtGui" not in self._qt:
                        self._qt.append("QtGui")
                    if "QtNetwork" not in self._qt:
                        self._qt.append("QtNetwork")

                for mod in self._qt:
                    lib = self._qt4_module_to_lib(mod)
                    libs.append(self.platform_lib(lib, self._is_framework(mod)))

                    if sys.platform == "win32":
                        # On Windows the dependent libraries seem to be in
                        # qmake.conf rather than the .prl file and the
                        # inter-dependencies between Qt libraries don't seem to
                        # be anywhere.
                        deps = _UniqueList()

                        if mod in list(wdepmap.keys()):
                            deps.extend(self.optional_list(wdepmap[mod]))

                        if mod in list(qdepmap.keys()):
                            for qdep in qdepmap[mod]:
                                # Ignore the dependency if it is explicitly
                                # linked.
                                if qdep not in self._qt:
                                    \
libs.append(self.platform_lib(self._qt4_module_to_lib(qdep)))

                                    if qdep in list(wdepmap.keys()):
                                        \
deps.extend(self.optional_list(wdepmap[qdep]))

                        libs.extend(deps.as_list())
                    else:
                        libs.extend(self._dependent_libs(lib, \
self._is_framework(mod)))  else:
                # Windows needs the version number appended if Qt is a DLL.
                qt_lib = self.config.qt_lib

                if self.generator in ("MSVC", "MSVC.NET", "MSBUILD", "BMAKE") and \
                win_shared:
                    qt_lib = qt_lib + version_to_string(qt_version).replace(".", "")

                    if self.config.qt_edition == "non-commercial":
                        qt_lib = qt_lib + "nc"

                libs.append(self.platform_lib(qt_lib, self.config.qt_framework))
                libs.extend(self._dependent_libs(self.config.qt_lib))

            # Handle header directories.
            specd = os.path.join(mkspecs, "default")

            if not os.access(specd, os.F_OK):
                specd = os.path.join(mkspecs, self.config.platform)

            incdir.append(specd)

            qtincdir = self.optional_list("INCDIR_QT")

            if qtincdir:
                if qt_version >= 0x040000:
                    for mod in self._qt:
                        if mod == "QAxContainer":
                            incdir.append(os.path.join(qtincdir[0], "ActiveQt"))
                        elif self._is_framework(mod):
                            idir = libdir_qt[0]

                            if mod == "QtAssistant" and qt_version < 0x040202:
                                mod = "QtAssistantClient"

                            incdir.append(os.path.join(idir,
                                    mod + ".framework", "Headers"))

                            if qt_version >= 0x050000:
                                if mod == "QtGui":
                                    incdir.append(os.path.join(idir,
                                            "QtWidgets.framework", "Headers"))
                                    incdir.append(os.path.join(idir,
                                            "QtPrintSupport.framework",
                                            "Headers"))
                                elif mod == "QtWebKit":
                                    incdir.append(os.path.join(idir,
                                            "QtWebKitWidgets.framework",
                                            "Headers"))
                        else:
                            idir = qtincdir[0]

                            incdir.append(os.path.join(idir, mod))

                            if qt_version >= 0x050000:
                                if mod == "QtGui":
                                    incdir.append(os.path.join(idir,
                                            "QtWidgets"))
                                    incdir.append(os.path.join(idir,
                                            "QtPrintSupport"))
                                elif mod == "QtWebKit":
                                    incdir.append(os.path.join(idir,
                                            "QtWebKitWidgets"))

                # This must go after the module include directories.
                incdir.extend(qtincdir)

        if self._opengl:
            incdir.extend(self.optional_list("INCDIR_OPENGL"))
            lflags.extend(self.optional_list("LFLAGS_OPENGL"))
            libdir.extend(self.optional_list("LIBDIR_OPENGL"))
            libs.extend(self.optional_list("LIBS_OPENGL"))

        if self._qt or self._opengl:
            if qt_version < 0x040000 or self._opengl or "QtGui" in self._qt:
                incdir.extend(self.optional_list("INCDIR_X11"))
                libdir.extend(self.optional_list("LIBDIR_X11"))
                libs.extend(self.optional_list("LIBS_X11"))

        if self._threaded:
            libs.extend(self.optional_list("LIBS_THREAD"))
            libs.extend(self.optional_list("LIBS_RTMT"))
        else:
            libs.extend(self.optional_list("LIBS_RT"))

        if self.console:
            libs.extend(self.optional_list("LIBS_CONSOLE"))

        libs.extend(self.optional_list("LIBS_WINDOWS"))

        lflags.extend(self._platform_rpaths(rpaths.as_list()))

        # Save the transformed values.
        self.CFLAGS.set(cflags)
        self.CXXFLAGS.set(cxxflags)
        self.DEFINES.set(defines)
        self.INCDIR.set(incdir)
        self.LFLAGS.set(lflags)
        self.LIBDIR.set(libdir)
        self.LIBS.set(libs)

        # Don't do it again because it has side effects.
        self._finalised = 1

    def _add_manifest(self, target=None):
        """Add the link flags for creating a manifest file.
        """
        if target is None:
            target = "$(TARGET)"

        self.LFLAGS.append("/MANIFEST")
        self.LFLAGS.append("/MANIFESTFILE:%s.manifest" % target)

    def _is_framework(self, mod):
        """Return true if the given Qt module is a framework.
        """
        return (self.config.qt_framework and (self.config.qt_version >= 0x040200 or \
mod != "QtAssistant"))

    def _qt4_module_to_lib(self, mname):
        """Return the name of the Qt4 library corresponding to a module.

        mname is the name of the module.
        """
        qt_version = self.config.qt_version

        if mname == "QtAssistant":
            if qt_version >= 0x040202 and sys.platform == "darwin":
                lib = mname
            else:
                lib = "QtAssistantClient"
        elif mname == "QtWebKit" and qt_version >= 0x050000:
            lib = "QtWebKitWidgets"
        else:
            lib = mname

        lib += self._infix

        if self._debug:
            if sys.platform == "win32":
                lib = lib + "d"
            elif sys.platform == "darwin":
                if not self._is_framework(mname):
                    lib = lib + "_debug"
            elif qt_version < 0x040200:
                lib = lib + "_debug"

        if sys.platform == "win32" and "shared" in self.config.qt_winconfig.split():
            if (mname in ("QtCore", "QtDeclarative", "QtDesigner", "QtGui",
                          "QtHelp", "QtMultimedia", "QtNetwork", "QtOpenGL",
                          "QtScript", "QtScriptTools", "QtSql", "QtSvg",
                          "QtTest", "QtWebKit", "QtXml", "QtXmlPatterns",
                          "phonon", "QtWidgets", "QtPrintSupport") or
                (qt_version >= 0x040200 and mname == "QtAssistant")):
                #lib = lib + "4"
                lib = "Qt5" + lib[2:]

        if sys.platform.startswith("linux") and qt_version >= 0x050000:
            lib = "Qt5" + lib[2:]

        return lib

    def optional_list(self, name):
        """Return an optional Makefile macro as a list.

        name is the name of the macro.
        """
        return self.__dict__[name].as_list()

    def optional_string(self, name, default=""):
        """Return an optional Makefile macro as a string.

        name is the name of the macro.
        default is the default value
        """
        s = ' '.join(self.optional_list(name))

        if not s:
            s = default

        return s

    def required_string(self, name):
        """Return a required Makefile macro as a string.

        name is the name of the macro.
        """
        s = self.optional_string(name)

        if not s:
            raise ValueError("\"%s\" must have a non-empty value" % name)

        return s

    def _platform_rpaths(self, rpaths):
        """Return a list of platform specific rpath flags.

        rpaths is the cannonical list of rpaths.
        """
        flags = []
        prefix = self.optional_string("RPATH")

        if prefix == "":
            # This was renamed in Qt v4.7.
            prefix = self.optional_string("LFLAGS_RPATH")

        if prefix != "":
            for r in rpaths:
                flags.append(_quote(prefix + r))

        return flags

    def platform_lib(self, clib, framework=0):
        """Return a library name in platform specific form.

        clib is the library name in cannonical form.
        framework is set of the library is implemented as a MacOS framework.
        """
        if self.generator in ("MSVC", "MSVC.NET", "MSBUILD", "BMAKE"):
            plib = clib + ".lib"
        elif sys.platform == "darwin" and framework:
            plib = "-framework " + clib
        else:
            plib = "-l" + clib

        return plib

    def _dependent_libs(self, clib, framework=0):
        """Return a list of additional libraries (in platform specific form)
        that must be linked with a library.

        clib is the library name in cannonical form.
        framework is set of the library is implemented as a MacOS framework.
        """
        if self.generator in ("MSVC", "MSVC.NET", "MSBUILD", "BMAKE"):
            prl_name = os.path.join(self.config.qt_lib_dir, clib + ".prl")
        elif sys.platform == "darwin" and framework:
            prl_name = os.path.join(self.config.qt_lib_dir, clib + ".framework", clib \
+ ".prl")  else:
            prl_name = os.path.join(self.config.qt_lib_dir, "lib" + clib + ".prl")

        libs = self._extract_value(prl_name, "QMAKE_PRL_LIBS").split()

        if self.config.qt_version >= 0x050000 and clib in ("QtGui", "Qt5Gui"):
            for xtra in ("QtWidgets", "QtPrintSupport"):
                libs.extend(
                        self.platform_lib(
                                self._qt4_module_to_lib(xtra), framework).split())

        return libs

    def _extract_value(self, fname, vname):
        """Return the stripped value from a name=value line in a file.

        fname is the name of the file.
        vname is the name of the value.
        """
        value = ""

        if os.access(fname, os.F_OK):
            try:
                f = open(fname, "r")
            except IOError:
                error("Unable to open \"%s\"" % fname)

            line = f.readline()
            while line:
                line = line.strip()
                if line and line[0] != "#":
                    eq = line.find("=")
                    if eq > 0 and line[:eq].strip() == vname:
                        value = line[eq + 1:].strip()
                        break

                line = f.readline()

            f.close()

        return value

    def parse_build_file(self, filename):
        """
        Parse a build file and return the corresponding dictionary.

        filename is the name of the build file.  If it is a dictionary instead
        then its contents are validated.
        """
        if type(filename) == dict:
            bfname = "dictionary"
            bdict = filename
        else:
            if os.path.isabs(filename):
                # We appear to be building out of the source tree.
                self._src_dir = os.path.dirname(filename)
                bfname = filename
            else:
                bfname = os.path.join(self.dir, filename)

            bdict = {}

            try:
                f = open(bfname, "r")
            except IOError:
                error("Unable to open \"%s\"" % bfname)

            line_nr = 1
            line = f.readline()

            while line:
                line = line.strip()

                if line and line[0] != "#":
                    eq = line.find("=")

                    if eq <= 0:
                        error("\"%s\" line %d: Line must be in the form 'name = value \
value...'." % (bfname, line_nr))

                    bdict[line[:eq].strip()] = line[eq + 1:].strip()

                line_nr = line_nr + 1
                line = f.readline()

            f.close()

        # Check the compulsory values.
        for i in ("target", "sources"):
            try:
                bdict[i]
            except KeyError:
                error("\"%s\" is missing from \"%s\"." % (i, bfname))

        # Get the optional values.
        for i in ("headers", "moc_headers"):
            try:
                bdict[i]
            except KeyError:
                bdict[i] = ""

        # Generate the list of objects.
        if self.generator in ("MSVC", "MSVC.NET", "MSBUILD", "BMAKE"):
            ext = ".obj"
        else:
            ext = ".o"

        olist = []

        for f in bdict["sources"].split():
            root, discard = os.path.splitext(f)
            olist.append(root + ext)

        for f in bdict["moc_headers"].split():
            if not self._qt:
                error("\"%s\" defines \"moc_headers\" for a non-Qt module." % bfname)

            root, discard = os.path.splitext(f)
            olist.append("moc_" + root + ext)

        bdict["objects"] = ' '.join(olist)

        return bdict

    def clean_build_file_objects(self, mfile, build):
        """Generate the clean target.

        mfile is the file object.
        build is the dictionary created from the build file.
        """
        mfile.write("\t-%s $(TARGET)\n" % self.rm)

        for f in build["objects"].split():
            mfile.write("\t-%s %s\n" % (self.rm, f))

        for f in build["moc_headers"].split():
            root, discard = os.path.splitext(f)
            mfile.write("\t-%s moc_%s.cpp\n" % (self.rm, root))

    def ready(self):
        """The Makefile is now ready to be used.
        """
        if not self._finalised:
            self.finalise()

    def generate(self):
        """Generate the Makefile.
        """
        self.ready()

        # Make sure the destination directory exists.
        try:
            os.makedirs(self.dir)
        except:
            pass

        mfname = os.path.join(self.dir, self._makefile)

        try:
            mfile = open(mfname, "w")
        except IOError:
            error("Unable to create \"%s\"" % mfname)

        self.generate_macros_and_rules(mfile)
        self.generate_target_default(mfile)
        self.generate_target_install(mfile)

        if self._installs:
            if type(self._installs) != list:
                self._installs = [self._installs]

            for src, dst in self._installs:
                self.install_file(mfile, src, dst)

        self.generate_target_clean(mfile)

        mfile.close()

    def generate_macros_and_rules(self, mfile):
        """The default implementation of the macros and rules generation.

        mfile is the file object.
        """
        if self._deployment_target:
            mfile.write("export MACOSX_DEPLOYMENT_TARGET = %s\n" % \
self._deployment_target)

        mfile.write("CC = %s\n" % self.required_string("CC"))
        mfile.write("CXX = %s\n" % self.required_string("CXX"))
        mfile.write("LINK = %s\n" % self.required_string("LINK"))

        cppflags = []

        if not self._debug:
            cppflags.append("-DNDEBUG")

        for f in self.optional_list("DEFINES"):
            cppflags.append("-D" + f)

        for f in self.optional_list("INCDIR"):
            cppflags.append("-I" + _quote(f))

        libs = []

        if self.generator in ("MSVC", "MSVC.NET", "MSBUILD"):
            libdir_prefix = "/LIBPATH:"
        else:
            libdir_prefix = "-L"

        for ld in self.optional_list("LIBDIR"):
            if sys.platform == "darwin" and self.config.qt_framework:
                fflag = "-F" + _quote(ld)
                libs.append(fflag)
                cppflags.append(fflag)

            libs.append(libdir_prefix + _quote(ld))

        libs.extend(self.optional_list("LIBS"))

        mfile.write("CPPFLAGS = %s\n" % ' '.join(cppflags))

        mfile.write("CFLAGS = %s\n" % self.optional_string("CFLAGS"))
        mfile.write("CXXFLAGS = %s\n" % self.optional_string("CXXFLAGS"))
        mfile.write("LFLAGS = %s\n" % self.optional_string("LFLAGS"))

        mfile.write("LIBS = %s\n" % ' '.join(libs))

        if self._qt:
            mfile.write("MOC = %s\n" % _quote(self.required_string("MOC")))

        if self._src_dir != self.dir:
            mfile.write("VPATH = %s\n\n" % self._src_dir)

        # These probably don't matter.
        if self.generator == "MINGW":
            mfile.write(".SUFFIXES: .cpp .cxx .cc .C .c\n\n")
        elif self.generator == "UNIX":
            mfile.write(".SUFFIXES: .c .o .cpp .cc .cxx .C\n\n")
        else:
            mfile.write(".SUFFIXES: .c .cpp .cc .cxx .C\n\n")

        if self.generator in ("MSVC", "MSVC.NET", "MSBUILD"):
            mfile.write("""
{.}.cpp{}.obj::
\t$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -Fo @<<
\t$<
<<

{.}.cc{}.obj::
\t$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -Fo @<<
\t$<
<<

{.}.cxx{}.obj::
\t$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -Fo @<<
\t$<
<<

{.}.C{}.obj::
\t$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -Fo @<<
\t$<
<<

{.}.c{}.obj::
\t$(CC) -c $(CFLAGS) $(CPPFLAGS) -Fo @<<
\t$<
<<
""")
        elif self.generator == "BMAKE":
            mfile.write("""
.cpp.obj:
\t$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -o$@ $<

.cc.obj:
\t$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -o$@ $<

.cxx.obj:
\t$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -o$@ $<

.C.obj:
\t$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -o$@ $<

.c.obj:
\t$(CC) -c $(CFLAGS) $(CPPFLAGS) -o$@ $<
""")
        else:
            mfile.write("""
.cpp.o:
\t$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -o $@ $<

.cc.o:
\t$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -o $@ $<

.cxx.o:
\t$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -o $@ $<

.C.o:
\t$(CXX) -c $(CXXFLAGS) $(CPPFLAGS) -o $@ $<

.c.o:
\t$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
""")

    def generate_target_default(self, mfile):
        """The default implementation of the default target.

        mfile is the file object.
        """
        mfile.write("\nall:\n")

    def generate_target_install(self, mfile):
        """The default implementation of the install target.

        mfile is the file object.
        """
        mfile.write("\ninstall:\n")

    def generate_target_clean(self, mfile):
        """The default implementation of the clean target.

        mfile is the file object.
        """
        mfile.write("\nclean:\n")

    def install_file(self, mfile, src, dst, strip=0):
        """Install one or more files in a directory.

        mfile is the file object.
        src is the name of a single file to install, or the list of a number of
        files to install.
        dst is the name of the destination directory.
        strip is set if the files should be stripped after been installed.
        """
        # Help package builders.
        if self.generator == "UNIX":
            dst = "$(DESTDIR)" + dst

        mfile.write("\t@%s %s " % (self.chkdir, _quote(dst)))

        if self.generator == "UNIX":
            mfile.write("|| ")

        mfile.write("%s %s\n" % (self.mkdir, _quote(dst)))

        if type(src) != list:
            src = [src]

        # Get the strip command if needed.
        if strip:
            strip_cmd = self.optional_string("STRIP")

            if not strip_cmd:
                strip = 0

        for sf in src:
            target = _quote(os.path.join(dst, os.path.basename(sf)))

            mfile.write("\t%s %s %s\n" % (self.copy, _quote(sf), target))

            if strip:
                mfile.write("\t%s %s\n" % (strip_cmd, target))


class ParentMakefile(Makefile):
    """The class that represents a parent Makefile.
    """
    def __init__(self, configuration, subdirs, dir=None, makefile="Makefile",
                 installs=None):
        """Initialise an instance of a parent Makefile.

        subdirs is the sequence of subdirectories.
        """
        Makefile.__init__(self, configuration, dir=dir, makefile=makefile, \
installs=installs)

        self._subdirs = subdirs

    def generate_macros_and_rules(self, mfile):
        """Generate the macros and rules.

        mfile is the file object.
        """
        # We don't want them.
        pass

    def generate_target_default(self, mfile):
        """Generate the default target.

        mfile is the file object.
        """
        self._subdir_target(mfile)

    def generate_target_install(self, mfile):
        """Generate the install target.

        mfile is the file object.
        """
        self._subdir_target(mfile, "install")

    def generate_target_clean(self, mfile):
        """Generate the clean target.

        mfile is the file object.
        """
        self._subdir_target(mfile, "clean")

    def _subdir_target(self, mfile, target="all"):
        """Create a target for a list of sub-directories.

        mfile is the file object.
        target is the name of the target.
        """
        if target == "all":
            tname = ""
        else:
            tname = " " + target

        mfile.write("\n" + target + ":\n")

        for d in self._subdirs:
            if self.generator == "MINGW":
                mfile.write("\t@$(MAKE) -C %s%s\n" % (d, tname))
            elif self.generator == "UNIX":
                mfile.write("\t@(cd %s; $(MAKE)%s)\n" % (d, tname))
            else:
                mfile.write("\tcd %s\n" % d)
                mfile.write("\t$(MAKE)%s\n" % tname)
                mfile.write("\t@cd ..\n")


class PythonModuleMakefile(Makefile):
    """The class that represents a Python module Makefile.
    """
    def __init__(self, configuration, dstdir, srcdir=None, dir=None,
                 makefile="Makefile", installs=None):
        """Initialise an instance of a parent Makefile.

        dstdir is the name of the directory where the module's Python code will
        be installed.
        srcdir is the name of the directory (relative to the directory in which
        the Makefile will be created) containing the module's Python code.  It
        defaults to the same directory.
        """
        Makefile.__init__(self, configuration, dir=dir, makefile=makefile, \
installs=installs)

        if not srcdir:
            srcdir = "."

        if dir:
            self._moddir = os.path.join(dir, srcdir)
        else:
            self._moddir = srcdir

        self._srcdir = srcdir
        self._dstdir = dstdir

    def generate_macros_and_rules(self, mfile):
        """Generate the macros and rules.

        mfile is the file object.
        """
        # We don't want them.
        pass

    def generate_target_install(self, mfile):
        """Generate the install target.

        mfile is the file object.
        """
        Makefile.generate_target_install(self, mfile)

        for root, dirs, files in os.walk(self._moddir):
            # Do not recurse into certain directories.
            for skip in (".svn", "CVS"):
                if skip in dirs:
                    dirs.remove(skip)

            tail = root[len(self._moddir):]
            flist = []

            for f in files:
                if f == "Makefile":
                    continue

                if os.path.isfile(os.path.join(root, f)):
                    flist.append(os.path.join(self._srcdir + tail, f))

            self.install_file(mfile, flist, self._dstdir + tail)


class ModuleMakefile(Makefile):
    """The class that represents a Python extension module Makefile
    """
    def __init__(self, configuration, build_file, install_dir=None, static=0,
                 console=0, qt=0, opengl=0, threaded=0, warnings=1, debug=0,
                 dir=None, makefile="Makefile", installs=None, strip=1,
                 export_all=0, universal=None, arch=None,
                 deployment_target=None):
        """Initialise an instance of a module Makefile.

        build_file is the file containing the target specific information.  If
        it is a dictionary instead then its contents are validated.
        install_dir is the directory the target will be installed in.
        static is set if the module should be built as a static library.
        strip is set if the module should be stripped of unneeded symbols when
        installed.  The default is 1.
        export_all is set if all the module's symbols should be exported rather
        than just the module's initialisation function.  Exporting all symbols
        increases the size of the module and slows down module load times but
        may avoid problems with modules that use exceptions.  The default is 0.
        """
        Makefile.__init__(self, configuration, console, qt, opengl, 1, threaded, \
warnings, debug, dir, makefile, installs, universal, arch, deployment_target)

        self._build = self.parse_build_file(build_file)
        self._install_dir = install_dir
        self.static = static

        self._manifest = ("embed_manifest_dll" in self.optional_list("CONFIG"))

        # Don't strip or restrict the exports if this is a debug or static
        # build.
        if debug or static:
            self._strip = 0
            self._limit_exports = 0
        else:
            self._strip = strip
            self._limit_exports = not export_all

        # Save the target name for later.
        self._target = self._build["target"]

        # The name of the module entry point is Python version specific.
        if self.config.py_version >= 0x030000:
            self._entry_point = "PyInit_%s" % self._target
        else:
            self._entry_point = "init%s" % self._target

        if sys.platform != "win32" and static:
            self._target = "lib" + self._target

        if sys.platform == "win32" and debug:
            self._target = self._target + "_d"

    def finalise(self):
        """Finalise the macros common to all module Makefiles.
        """
        if self.console:
            lflags_console = "LFLAGS_CONSOLE"
        else:
            lflags_console = "LFLAGS_WINDOWS"

        if self.static:
            self.DEFINES.append("SIP_STATIC_MODULE")
        else:
            self.CFLAGS.extend(self.optional_list("CFLAGS_SHLIB"))
            self.CXXFLAGS.extend(self.optional_list("CXXFLAGS_SHLIB"))

            lflags_dll = self.optional_list("LFLAGS_DLL")

            if lflags_dll:
                self.LFLAGS.extend(lflags_dll)
            elif self.console:
                lflags_console = "LFLAGS_CONSOLE_DLL"
            else:
                lflags_console = "LFLAGS_WINDOWS_DLL"

            if self._manifest:
                self._add_manifest()

            # We use this to explictly create bundles on MacOS.  Apple's Python
            # can handle extension modules that are bundles or dynamic
            # libraries, but python.org versions need bundles (unless built
            # with DYNLOADFILE=dynload_shlib.o).
            if sys.platform == "darwin":
                lflags_plugin = ["-bundle"]
            else:
                lflags_plugin = self.optional_list("LFLAGS_PLUGIN")

            if not lflags_plugin:
                lflags_plugin = self.optional_list("LFLAGS_SHLIB")

            self.LFLAGS.extend(lflags_plugin)

        self.LFLAGS.extend(self.optional_list(lflags_console))

        if sys.platform == "darwin":
            from distutils.sysconfig import get_python_inc

            # The Python include directory seems to be the only one that uses
            # the real path even when using a virtual environment (eg. pyvenv).
            # Note that I can't remember why we need a framework build.
            dl = get_python_inc().split(os.sep)

            if "Python.framework" not in dl:
                error("SIP requires Python to be built as a framework")

            self.LFLAGS.append("-undefined dynamic_lookup")

        Makefile.finalise(self)

        if not self.static:
            if self.optional_string("AIX_SHLIB"):
                # AIX needs a lot of special handling.
                if self.required_string('LINK') == 'g++':
                    # g++ is used for linking.
                    # For SIP v4 and g++:
                    # 1.) Import the python symbols
                    aix_lflags = ['-Wl,-bI:%s/python.exp' % self.config.py_lib_dir]

                    if self._limit_exports:
                        aix_lflags.append('-Wl,-bnoexpall')
                        aix_lflags.append('-Wl,-bnoentry')
                        aix_lflags.append('-Wl,-bE:%s.exp' % self._target)
                else:
                    # IBM VisualAge C++ is used for linking.
                    # For SIP v4 and xlC:
                    # 1.) Create a shared object
                    # 2.) Import the python symbols
                    aix_lflags = ['-qmkshrobj',
                                  '-bI:%s/python.exp' % self.config.py_lib_dir]

                    if self._limit_exports:
                        aix_lflags.append('-bnoexpall')
                        aix_lflags.append('-bnoentry')
                        aix_lflags.append('-bE:%s.exp' % self._target)

                self.LFLAGS.extend(aix_lflags)
            else:
                if self._limit_exports:
                    if sys.platform[:5] == 'linux':
                        self.LFLAGS.extend(['-Wl,--version-script=%s.exp' % \
self._target])  elif sys.platform[:5] == 'sunos':
                        if self.required_string('LINK') == 'g++':
                            self.LFLAGS.extend(['-Wl,-z,noversion', '-Wl,-M,%s.exp' % \
self._target])  else:
                            self.LFLAGS.extend(['-z' 'noversion', '-M', '%s.exp' % \
self._target])  elif sys.platform[:5] == 'hp-ux':
                        self.LFLAGS.extend(['-Wl,+e,%s' % self._entry_point])
                    elif sys.platform[:5] == 'irix' and self.required_string('LINK') \
                != 'g++':
                        # Doesn't work when g++ is used for linking on IRIX.
                        self.LFLAGS.extend(['-Wl,-exported_symbol,%s' % \
self._entry_point])

                # Force the shared linker if there is one.
                link_shlib = self.optional_list("LINK_SHLIB")

                if link_shlib:
                    self.LINK.set(link_shlib)

        # This made an appearence in Qt v4.4rc1 and breaks extension modules so
        # remove it.  It was removed at my request but some stupid distros may
        # have kept it.
        self.LFLAGS.remove('-Wl,--no-undefined') 

    def module_as_lib(self, mname):
        """Return the name of a SIP v3.x module when it is used as a library.
        This will raise an exception when used with SIP v4.x modules.

        mname is the name of the module.
        """
        raise ValueError("module_as_lib() can only be used with SIP v3.x")

    def generate_macros_and_rules(self, mfile):
        """Generate the macros and rules generation.

        mfile is the file object.
        """
        if self.static:
            if sys.platform == "win32":
                ext = "lib"
            else:
                ext = "a"
        else:
            if sys.platform == "win32":
                ext = "pyd"
            elif sys.platform == "darwin":
                ext = "so"
            elif sys.platform == "cygwin":
                ext = "dll"
            else:
                ext = self.optional_string("EXTENSION_PLUGIN")
                if not ext:
                    ext = self.optional_string("EXTENSION_SHLIB", "so")

        mfile.write("TARGET = %s\n" % (self._target + "." + ext))
        mfile.write("OFILES = %s\n" % self._build["objects"])
        mfile.write("HFILES = %s %s\n" % (self._build["headers"], \
self._build["moc_headers"]))  mfile.write("\n")

        if self.static:
            if self.generator in ("MSVC", "MSVC.NET", "MSBUILD", "BMAKE"):
                mfile.write("LIB = %s\n" % self.required_string("LIB"))
            elif self.generator == "MINGW":
                mfile.write("AR = %s\n" % self.required_string("LIB"))
                self._ranlib = None
            else:
                mfile.write("AR = %s\n" % self.required_string("AR"))

                self._ranlib = self.optional_string("RANLIB")

                if self._ranlib:
                    mfile.write("RANLIB = %s\n" % self._ranlib)

        Makefile.generate_macros_and_rules(self, mfile)

    def generate_target_default(self, mfile):
        """Generate the default target.

        mfile is the file object.
        """
        # Do these first so that it's safe for a sub-class to append additional
        # commands to the real target, but make sure the default is correct.
        mfile.write("\nall: $(TARGET)\n")
        mfile.write("\n$(OFILES): $(HFILES)\n")

        for mf in self._build["moc_headers"].split():
            root, discard = os.path.splitext(mf)
            cpp = "moc_" + root + ".cpp"

            mfile.write("\n%s: %s\n" % (cpp, mf))
            mfile.write("\t$(MOC) -o %s %s\n" % (cpp, mf))

        mfile.write("\n$(TARGET): $(OFILES)\n")

        if self.generator in ("MSVC", "MSVC.NET", "MSBUILD"):
            if self.static:
                mfile.write("\t$(LIB) /OUT:$(TARGET) @<<\n")
                mfile.write("\t  $(OFILES)\n")
                mfile.write("<<\n")
            else:
                mfile.write("\t$(LINK) $(LFLAGS) /OUT:$(TARGET) @<<\n")
                mfile.write("\t  $(OFILES) $(LIBS)\n")
                mfile.write("<<\n")

                if self._manifest:
                    mfile.write("\tmt -nologo -manifest $(TARGET).manifest \
-outputresource:$(TARGET);2\n")  elif self.generator == "BMAKE":
            if self.static:
                mfile.write("\t-%s $(TARGET)\n" % (self.rm))
                mfile.write("\t$(LIB) $(TARGET) @&&|\n")

                for of in self._build["objects"].split():
                    mfile.write("+%s \\\n" % (of))

                mfile.write("|\n")
            else:
                mfile.write("\t$(LINK) @&&|\n")
                mfile.write("\t$(LFLAGS) $(OFILES) ,$(TARGET),,$(LIBS),%s\n" % \
(self._target))  mfile.write("|\n")

                # Create the .def file that renames the entry point.
                defname = os.path.join(self.dir, self._target + ".def")

                try:
                    dfile = open(defname, "w")
                except IOError:
                    error("Unable to create \"%s\"" % defname)

                dfile.write("EXPORTS\n")
                dfile.write("%s=_%s\n" % (self._entry_point, self._entry_point))

                dfile.close()

        else:
            if self.static:
                mfile.write("\t-%s $(TARGET)\n" % self.rm)
                mfile.write("\t$(AR) $(TARGET) $(OFILES)\n")

                if self._ranlib:
                    mfile.write("\t$(RANLIB) $(TARGET)\n")
            else:
                if self._limit_exports:
                    # Create an export file for AIX, Linux and Solaris.
                    if sys.platform[:5] == 'linux':
                        mfile.write("\t@echo '{ global: %s; local: *; };' > %s.exp\n" \
% (self._entry_point, self._target))  elif sys.platform[:5] == 'sunos':
                        mfile.write("\t@echo '{ global: %s; local: *; };' > %s.exp\n" \
% (self._entry_point, self._target))  elif sys.platform[:3] == 'aix':
                        mfile.write("\t@echo '#!' >%s.exp" % self._target)
                        mfile.write("; \\\n\t echo '%s' >>%s.exp\n" % \
(self._entry_point, self._target))

                mfile.write("\t$(LINK) $(LFLAGS) -o $(TARGET) $(OFILES) $(LIBS)\n")

    def generate_target_install(self, mfile):
        """Generate the install target.

        mfile is the file object.
        """
        if self._install_dir is None:
            self._install_dir = self.config.default_mod_dir

        mfile.write("\ninstall: $(TARGET)\n")
        self.install_file(mfile, "$(TARGET)", self._install_dir, self._strip)

    def generate_target_clean(self, mfile):
        """Generate the clean target.

        mfile is the file object.
        """
        mfile.write("\nclean:\n")
        self.clean_build_file_objects(mfile, self._build)

        if self._manifest and not self.static:
            mfile.write("\t-%s $(TARGET).manifest\n" % self.rm)

        # Remove any export file on AIX, Linux and Solaris.
        if self._limit_exports and (sys.platform[:5] == 'linux' or
                                    sys.platform[:5] == 'sunos' or
                                    sys.platform[:3] == 'aix'):
            mfile.write("\t-%s %s.exp\n" % (self.rm, self._target))


class SIPModuleMakefile(ModuleMakefile):
    """The class that represents a SIP generated module Makefile.
    """
    def __init__(self, configuration, build_file, install_dir=None, static=0,
                 console=0, qt=0, opengl=0, threaded=0, warnings=1, debug=0,
                 dir=None, makefile="Makefile", installs=None, strip=1,
                 export_all=0, universal=None, arch=None, prot_is_public=0,
                 deployment_target=None):
        """Initialise an instance of a SIP generated module Makefile.

        prot_is_public is set if "protected" is to be redefined as "public".
        If the platform's C++ ABI allows it this can significantly reduce the
        size of the generated code.

        For all other arguments see ModuleMakefile.
        """
        ModuleMakefile.__init__(self, configuration, build_file, install_dir,
                static, console, qt, opengl, threaded, warnings, debug, dir,
                makefile, installs, strip, export_all, universal, arch,
                deployment_target)

        self._prot_is_public = prot_is_public

    def finalise(self):
        """Finalise the macros for a SIP generated module Makefile.
        """
        if self._prot_is_public:
            self.DEFINES.append('SIP_PROTECTED_IS_PUBLIC')
            self.DEFINES.append('protected=public')

        self.INCDIR.append(self.config.sip_inc_dir)

        ModuleMakefile.finalise(self)


class ProgramMakefile(Makefile):
    """The class that represents a program Makefile.
    """
    def __init__(self, configuration, build_file=None, install_dir=None,
                 console=0, qt=0, opengl=0, python=0, threaded=0, warnings=1,
                 debug=0, dir=None, makefile="Makefile", installs=None,
                 universal=None, arch=None, deployment_target=None):
        """Initialise an instance of a program Makefile.

        build_file is the file containing the target specific information.  If
        it is a dictionary instead then its contents are validated.
        install_dir is the directory the target will be installed in.
        """
        Makefile.__init__(self, configuration, console, qt, opengl, python, threaded, \
warnings, debug, dir, makefile, installs, universal, arch, deployment_target)

        self._install_dir = install_dir

        self._manifest = ("embed_manifest_exe" in self.optional_list("CONFIG"))
        self._target = None

        if build_file:
            self._build = self.parse_build_file(build_file)
        else:
            self._build = None

    def build_command(self, source):
        """Create a command line that will build an executable.  Returns a
        tuple of the name of the executable and the command line.

        source is the name of the source file.
        """
        # The name of the executable.
        self._target, _ = os.path.splitext(source)

        if sys.platform in ("win32", "cygwin"):
            exe = self._target + ".exe"
        else:
            exe = self._target

        self.ready()

        # The command line.
        build = []

        build.append(self.required_string("CXX"))

        for a in self._arch.split():
            build.append('-arch ' + a)

        for f in self.optional_list("DEFINES"):
            build.append("-D" + f)

        for f in self.optional_list("INCDIR"):
            build.append("-I" + _quote(f))

        build.extend(self.optional_list("CXXFLAGS"))

        # This is for Qt5.
        build.extend(self.optional_list("CXXFLAGS_APP"))

        # Borland requires all flags to precede all file names.
        if self.generator != "BMAKE":
            build.append(source)

        if self.generator in ("MSVC", "MSVC.NET", "MSBUILD"):
            build.append("-Fe")
            build.append("/link")
            libdir_prefix = "/LIBPATH:"
        elif self.generator == "BMAKE":
            build.append("-e" + exe)
            libdir_prefix = "-L"
        else:
            build.append("-o")
            build.append(exe)
            libdir_prefix = "-L"

        for ld in self.optional_list("LIBDIR"):
            if sys.platform == "darwin" and self.config.qt_framework:
                build.append("-F" + _quote(ld))

            build.append(libdir_prefix + _quote(ld))

        lflags = self.optional_list("LFLAGS")

        # This is a huge hack demonstrating my lack of understanding of how the
        # Borland compiler works.
        if self.generator == "BMAKE":
            blflags = []

            for lf in lflags:
                for f in lf.split():
                    # Tell the compiler to pass the flags to the linker.
                    if f[-1] == "-":
                        f = "-l-" + f[1:-1]
                    elif f[0] == "-":
                        f = "-l" + f[1:]

                    # Remove any explicit object files otherwise the compiler
                    # will complain that they can't be found, but they don't
                    # seem to be needed.
                    if f[-4:].lower() != ".obj":
                        blflags.append(f)

            lflags = blflags

        build.extend(lflags)

        build.extend(self.optional_list("LIBS"))

        if self.generator == "BMAKE":
            build.append(source)

        return (exe, ' '.join(build))

    def finalise(self):
        """Finalise the macros for a program Makefile.
        """
        if self.generator in ("MSVC", "MSVC.NET", "MSBUILD"):
            self.LFLAGS.append("/INCREMENTAL:NO")

        if self._manifest:
            self._add_manifest(self._target)

        if self.console:
            lflags_console = "LFLAGS_CONSOLE"
        else:
            lflags_console = "LFLAGS_WINDOWS"

        self.LFLAGS.extend(self.optional_list(lflags_console))

        Makefile.finalise(self)

    def generate_macros_and_rules(self, mfile):
        """Generate the macros and rules generation.

        mfile is the file object.
        """
        if not self._build:
            raise ValueError("pass a filename as build_file when generating a \
Makefile")

        target = self._build["target"]

        if sys.platform in ("win32", "cygwin"):
            target = target + ".exe"

        mfile.write("TARGET = %s\n" % target)
        mfile.write("OFILES = %s\n" % self._build["objects"])
        mfile.write("HFILES = %s\n" % self._build["headers"])
        mfile.write("\n")

        Makefile.generate_macros_and_rules(self, mfile)

    def generate_target_default(self, mfile):
        """Generate the default target.

        mfile is the file object.
        """
        # Do these first so that it's safe for a sub-class to append additional
        # commands to the real target, but make sure the default is correct.
        mfile.write("\nall: $(TARGET)\n")
        mfile.write("\n$(OFILES): $(HFILES)\n")

        for mf in self._build["moc_headers"].split():
            root, _ = os.path.splitext(mf)
            cpp = "moc_" + root + ".cpp"

            if self._src_dir != self.dir:
                mf = os.path.join(self._src_dir, mf)

            mfile.write("\n%s: %s\n" % (cpp, mf))
            mfile.write("\t$(MOC) -o %s %s\n" % (cpp, mf))

        mfile.write("\n$(TARGET): $(OFILES)\n")

        if self.generator in ("MSVC", "MSVC.NET", "MSBUILD"):
            mfile.write("\t$(LINK) $(LFLAGS) /OUT:$(TARGET) @<<\n")
            mfile.write("\t  $(OFILES) $(LIBS)\n")
            mfile.write("<<\n")
        elif self.generator == "BMAKE":
            mfile.write("\t$(LINK) @&&|\n")
            mfile.write("\t$(LFLAGS) $(OFILES) ,$(TARGET),,$(LIBS),,\n")
            mfile.write("|\n")
        else:
            mfile.write("\t$(LINK) $(LFLAGS) -o $(TARGET) $(OFILES) $(LIBS)\n")

        if self._manifest:
            mfile.write("\tmt -nologo -manifest $(TARGET).manifest \
-outputresource:$(TARGET);1\n")

    def generate_target_install(self, mfile):
        """Generate the install target.

        mfile is the file object.
        """
        if self._install_dir is None:
            self._install_dir = self.config.default_bin_dir

        mfile.write("\ninstall: $(TARGET)\n")
        self.install_file(mfile, "$(TARGET)", self._install_dir)

    def generate_target_clean(self, mfile):
        """Generate the clean target.

        mfile is the file object.
        """
        mfile.write("\nclean:\n")
        self.clean_build_file_objects(mfile, self._build)

        if self._manifest:
            mfile.write("\t-%s $(TARGET).manifest\n" % self.rm)


def _quote(s):
    """Return a string surrounded by double quotes it if contains a space.

    s is the string.
    """
    if s.find(" ") >= 0:
        s = '"' + s + '"'

    return s


def version_to_string(v):
    """Convert a 3 part version number encoded as a hexadecimal value to a
    string.
    """
    return "%u.%u.%u" % (((v >> 16) & 0xff), ((v >> 8) & 0xff), (v & 0xff))


def read_version(filename, description, numdefine=None, strdefine=None):
    """Read the version information for a package from a file.  The information
    is specified as #defines of a numeric (hexadecimal or decimal) value and/or
    a string value.

    filename is the name of the file.
    description is the descriptive name of the package.
    numdefine is the name of the #define of the numeric version.  It is ignored
    if it is None.
    strdefine is the name of the #define of the string version.  It is ignored
    if it is None.

    Returns a tuple of the version as a number and as a string.
    """
    need_num = numdefine is not None
    need_str = strdefine is not None

    vers = None
    versstr = None

    f = open(filename)
    l = f.readline()

    while l and (need_num or need_str):
        wl = l.split()
        if len(wl) >= 3 and wl[0] == "#define":
            if need_num and wl[1] == numdefine:
                v = wl[2]

                if v[0:2] == "0x":
                    vers = int(v, 16)
                else:
                    dec = int(v)
                    maj = dec / 100
                    min = (dec % 100) / 10
                    bug = (dec % 10)
                    vers = (maj << 16) + (min << 8) + bug

                need_num = 0

            if need_str and wl[1] == strdefine:
                # Take account of embedded spaces.
                versstr = ' '.join(wl[2:])[1:-1]
                need_str = 0

        l = f.readline()

    f.close()

    if need_num or need_str:
        error("The %s version number could not be determined by parsing %s." % \
(description, filename))

    return (vers, versstr)


def create_content(cdict, macros=None):
    """Convert a dictionary to a string (typically to use as the content to a
    call to create_config_module()).  Dictionary values that are strings are
    quoted.  Dictionary values that are lists are converted to quoted strings.

    dict is the dictionary.
    macros is the optional dictionary of platform specific build macros.
    """
    content = "_pkg_config = {\n"

    keys = list(cdict.keys())
    keys.sort()

    # Format it nicely.
    width = 0

    for k in keys:
        klen = len(k)

        if width < klen:
            width = klen

    for k in keys:
        val = cdict[k]
        vtype = type(val)
        delim = None

        if val is None:
            val = "None"
        elif vtype == list:
            val = ' '.join(val)
            delim = "'"
        elif vtype == int:
            if k.find("version") >= 0:
                # Assume it's a hexadecimal version number.  It doesn't matter
                # if it isn't, we are just trying to make it look pretty.
                val = "0x%06x" % val
            else:
                val = str(val)
        else:
            val = str(val)
            delim = "'"

        if delim:
            if "'" in val:
                delim = "'''"

            val = delim + val + delim

        content = content + "    '" + k + "':" + (" " * (width - len(k) + 2)) + \
val.replace("\\", "\\\\")

        if k != keys[-1]:
            content = content + ","

        content = content + "\n"

    content = content + "}\n\n"

    # Format the optional macros.
    content = content + "_default_macros = "

    if macros:
        content = content + "{\n"

        names = list(macros.keys())
        names.sort()

        width = 0
        for c in names:
            clen = len(c)
            if width < clen:
                width = clen

        for c in names:
            if c == names[-1]:
                sep = ""
            else:
                sep = ","

            val = macros[c]
            if "'" in val:
                delim = "'''"
            else:
                delim = "'"

            k = "'" + c + "':"
            content = content + "    %-*s  %s%s%s%s\n" % (1 + width + 2, k, delim, \
val.replace("\\", "\\\\"), delim, sep)

        content = content + "}\n"
    else:
        content = content + "None\n"

    return content


def create_config_module(module, template, content, macros=None):
    """Create a configuration module by replacing "@" followed by
    "SIP_CONFIGURATION" followed by "@" in a template file with a content
    string.

    module is the name of the module file.
    template is the name of the template file.
    content is the content string.  If it is a dictionary it is first converted
    to a string using create_content().
    macros is an optional dictionary of platform specific build macros.  It is
    only used if create_content() is called to convert the content to a string.
    """
    if type(content) == dict:
        content = create_content(content, macros)

    # Allow this file to used as a template.
    key = "@" + "SIP_CONFIGURATION" + "@"

    df = open(module, "w")
    sf = open(template, "r")

    line = sf.readline()
    while line:
        if line.find(key) >= 0:
            line = content

        df.write(line)

        line = sf.readline()

    df.close()
    sf.close()


def version_to_sip_tag(version, tags, description):
    """Convert a version number to a SIP tag.

    version is the version number.  If it is negative then the latest version
    is assumed.  (This is typically useful if a snapshot is indicated by a
    negative version number.)
    tags is the dictionary of tags keyed by version number.  The tag used is
    the one with the smallest key (ie. earliest version) that is greater than
    the given version number.
    description is the descriptive name of the package used for error messages.

    Returns the corresponding tag.
    """
    vl = list(tags.keys())
    vl.sort()

    # For a snapshot use the latest tag.
    if version < 0:
        tag = tags[vl[-1]]
    else:
        for v in vl:
            if version < v:
                tag = tags[v]
                break
        else:
            error("Unsupported %s version: 0x%06x." % (description, version))

    return tag


def error(msg):
    """Display an error message and terminate.

    msg is the text of the error message.
    """
    sys.stderr.write(format("Error: " + msg) + "\n")
    sys.exit(1)

 
def inform(msg):
    """Display an information message.

    msg is the text of the error message.
    """
    sys.stdout.write(format(msg) + "\n")


def format(msg, leftmargin=0, rightmargin=78):
    """Format a message by inserting line breaks at appropriate places.

    msg is the text of the message.
    leftmargin is the position of the left margin.
    rightmargin is the position of the right margin.

    Return the formatted message.
    """
    curs = leftmargin
    fmsg = " " * leftmargin

    for w in msg.split():
        l = len(w)
        if curs != leftmargin and curs + l > rightmargin:
            fmsg = fmsg + "\n" + (" " * leftmargin)
            curs = leftmargin

        if curs > leftmargin:
            fmsg = fmsg + " "
            curs = curs + 1

        fmsg = fmsg + w
        curs = curs + l

    return fmsg


def parse_build_macros(filename, names, overrides=None, properties=None):
    """Parse a qmake compatible file of build system macros and convert it to a
    dictionary.  A macro is a name/value pair.  The dictionary is returned or
    None if any of the overrides was invalid.

    filename is the name of the file to parse.
    names is a list of the macro names to extract from the file.
    overrides is an optional list of macro names and values that modify those
    found in the file.  They are of the form "name=value" (in which case the
    value replaces the value found in the file) or "name+=value" (in which case
    the value is appended to the value found in the file).
    properties is an optional dictionary of property name and values that are
    used to resolve any expressions of the form "$[name]" in the file.
    """
    # Validate and convert the overrides to a dictionary.
    orides = {}

    if overrides is not None:
        for oride in overrides:
            prefix = ""
            name_end = oride.find("+=")

            if name_end >= 0:
                prefix = "+"
                val_start = name_end + 2
            else:
                name_end = oride.find("=")

                if name_end >= 0:
                    val_start = name_end + 1
                else:
                    return None

            name = oride[:name_end]

            if name not in names:
                return None

            orides[name] = prefix + oride[val_start:]

    # This class defines a file like object that handles the nested include()
    # directives in qmake files.
    class qmake_build_file_reader:
        def __init__(self, filename):
            self.filename = filename
            self.currentfile = None
            self.filestack = []
            self.pathstack = []
            self.cond_fname = None
            self._openfile(filename)

        def _openfile(self, filename):
            try:
                f = open(filename, 'r')
            except IOError:
                # If this file is conditional then don't raise an error.
                if self.cond_fname == filename:
                    return

                error("Unable to open %s" % filename)

            if self.currentfile:
                self.filestack.append(self.currentfile)
                self.pathstack.append(self.path)

            self.currentfile = f
            self.path = os.path.dirname(filename)

        def readline(self):
            line = self.currentfile.readline()
            sline = line.strip()

            if self.cond_fname and sline == '}':
                # The current condition is closed.
                self.cond_fname = None
                line = self.currentfile.readline()
            elif sline.startswith('exists(') and sline.endswith('{'):
                # A new condition is opened so extract the filename.
                self.cond_fname = self._normalise(sline[:-1].strip()[7:-1].strip())
                line = self.currentfile.readline()
            elif sline.startswith('include('):
                nextfile = self._normalise(sline[8:-1].strip())
                self._openfile(nextfile)
                return self.readline()

            if not line:
                self.currentfile.close()

                if self.filestack:
                    self.currentfile = self.filestack.pop()
                    self.path = self.pathstack.pop()
                    return self.readline()

            return line

        # Normalise a filename by expanding any environment variables and
        # making sure it is absolute.
        def _normalise(self, fname):
            if "$(" in fname:
                fname = os.path.normpath(self._expandvars(fname))

            if not os.path.isabs(fname):
                fname = os.path.join(self.path, fname)

            return fname

        # Expand the environment variables in a filename.
        def _expandvars(self, fname):
            i = 0
            while True:
                m = re.search(r'\$\((\w+)\)', fname[i:])
                if not m:
                    break

                i, j = m.span(0)
                name = m.group(1)
                if name in os.environ:
                    tail = fname[j:]
                    fname = fname[:i] + os.environ[name]
                    i = len(fname)
                    fname += tail
                else:
                    i = j

            return fname

    f = qmake_build_file_reader(filename)

    # Get everything into a dictionary.
    raw = {
        "DIR_SEPARATOR":        os.sep,
        "LITERAL_WHITESPACE":   " ",
        "LITERAL_DOLLAR":       "$",
        "LITERAL_HASH":         "#"
    }

    line = f.readline()
    while line:
        # Handle line continuations.
        while len(line) > 1 and line[-2] == "\\":
            line = line[:-2]

            next = f.readline()

            if next:
                line = line + next
            else:
                break

        line = line.strip()

        # Ignore comments.
        if line and line[0] != "#":
            assstart = line.find("+")
            if assstart > 0 and line[assstart + 1] == '=':
                adding = True
                assend = assstart + 1
            else:
                adding = False
                assstart = line.find("=")
                assend = assstart

            if assstart > 0:
                lhs = line[:assstart].strip()
                rhs = line[assend + 1:].strip()

                # Remove the escapes for any quotes.
                rhs = rhs.replace(r'\"', '"').replace(r"\'", "'")

                if adding and rhs != "":
                    orig_rhs = raw.get(lhs)
                    if orig_rhs is not None:
                        rhs = orig_rhs + " " + rhs

                raw[lhs] = _expand_macro_value(raw, rhs, properties)

        line = f.readline()

    # Go through the raw dictionary extracting the macros we need and
    # resolving any macro expansions.  First of all, make sure every macro has
    # a value.
    refined = {}

    for m in names:
        refined[m] = ""

    macro_prefix = "QMAKE_"

    for lhs in list(raw.keys()):
        # Strip any prefix.
        if lhs.startswith(macro_prefix):
            reflhs = lhs[len(macro_prefix):]
        else:
            reflhs = lhs

        # See if we are interested in this one.
        if reflhs not in names:
            continue

        rhs = raw[lhs]

        # Expand any POSIX style environment variables.
        pleadin = ["$$(", "$("]

        for pl in pleadin:
            estart = rhs.find(pl)

            if estart >= 0:
                nstart = estart + len(pl)
                break
        else:
            estart = -1

        while estart >= 0:
            eend = rhs[nstart:].find(")")

            if eend < 0:
                break

            eend = nstart + eend

            name = rhs[nstart:eend]

            try:
                env = os.environ[name]
            except KeyError:
                env = ""

            rhs = rhs[:estart] + env + rhs[eend + 1:]

            for pl in pleadin:
                estart = rhs.find(pl)

                if estart >= 0:
                    nstart = estart + len(pl)
                    break
            else:
                estart = -1

        # Expand any Windows style environment variables.
        estart = rhs.find("%")

        while estart >= 0:
            eend = rhs[estart + 1:].find("%")

            if eend < 0:
                break

            eend = estart + 1 + eend

            name = rhs[estart + 1:eend]

            try:
                env = os.environ[name]
            except KeyError:
                env = ""

            rhs = rhs[:estart] + env + rhs[eend + 1:]

            estart = rhs.find("%")

        refined[reflhs] = rhs

    # Handle the user overrides.
    for lhs in list(orides.keys()):
        rhs = refined[lhs]
        oride = orides[lhs]

        if oride.find("+") == 0:
            if rhs:
                rhs = rhs + " " + oride[1:]
            else:
                rhs = oride[1:]
        else:
            rhs = oride

        refined[lhs] = rhs

    return refined


def _expand_macro_value(macros, rhs, properties):
    """Expand the value of a macro based on ones seen so far."""
    estart = rhs.find("$$(")
    mstart = rhs.find("$$")

    while mstart >= 0 and mstart != estart:
        rstart = mstart + 2
        if rstart < len(rhs) and rhs[rstart] == "{":
            rstart = rstart + 1
            term = "}"
        elif rstart < len(rhs) and rhs[rstart] == "[":
            rstart = rstart + 1
            term = "]"
        else:
            term = string.whitespace

        mend = rstart
        while mend < len(rhs) and rhs[mend] not in term:
            mend = mend + 1

        lhs = rhs[rstart:mend]

        if term in "}]":
            mend = mend + 1

        if term == "]":
            # Assume a missing property expands to an empty string.
            if properties is None:
                value = ""
            else:
                value = properties.get(lhs, "")
        else:
            # We used to treat a missing value as an error, but Qt v4.3.0 has
            # at least one case that refers to an undefined macro.  If qmake
            # handles it then this must be the correct behaviour.
            value = macros.get(lhs, "")

        rhs = rhs[:mstart] + value + rhs[mend:]
        estart = rhs.find("$$(")
        mstart = rhs.find("$$")

    return rhs


def create_wrapper(script, wrapper, gui=0, use_arch=''):
    """Create a platform dependent executable wrapper around a Python script.

    script is the full pathname of the script.
    wrapper is the name of the wrapper file to create.
    gui is non-zero if a GUI enabled version of the interpreter should be used.
    use_arch is the MacOS/X architecture to invoke python with.

    Returns the platform specific name of the wrapper.
    """
    if sys.platform == "win32":
        wrapper = wrapper + ".bat"

    wf = open(wrapper, "w")

    if sys.platform == "win32":
        exe = sys.executable

        if gui:
            exe = exe[:-4] + "w.exe"

        wf.write("@\"%s\" \"%s\" %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 %%9\n" % (exe, \
script))  elif sys.platform == "darwin":
        # The installation of MacOS's python is a mess that changes from
        # version to version and where sys.executable is useless.

        if gui:
            exe = "pythonw"
        else:
            exe = "python"

        version = sys.version_info
        exe = "%s%d.%d" % (exe, version[0], version[1])

        if use_arch:
            # Note that this may not work with the "standard" interpreter but
            # should with the "pythonX.Y" version.
            exe = "arch -%s %s" % (use_arch, exe)

        wf.write("#!/bin/sh\n")
        wf.write("exec %s %s ${1+\"$@\"}\n" % (exe, script))
    else:
        wf.write("#!/bin/sh\n")
        wf.write("exec %s %s ${1+\"$@\"}\n" % (sys.executable, script))

    wf.close()

    if sys.platform != "win32":
        sbuf = os.stat(wrapper)
        mode = sbuf.st_mode
        mode |= (stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)

        os.chmod(wrapper, mode)

    return wrapper


["configure.py" (text/plain)]

# This script generates the PyQt configuration and generates the Makefiles.
#
# Copyright (c) 2013 Riverbank Computing Limited <info@riverbankcomputing.com>
# 
# This file is part of PyQt.
# 
# This file may be used under the terms of the GNU General Public
# License versions 2.0 or 3.0 as published by the Free Software
# Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
# included in the packaging of this file.  Alternatively you may (at
# your option) use any later version of the GNU General Public
# License if such license has been publicly approved by Riverbank
# Computing Limited (or its successors, if any) and the KDE Free Qt
# Foundation. In addition, as a special exception, Riverbank gives you
# certain additional rights. These rights are described in the Riverbank
# GPL Exception version 1.1, which can be found in the file
# GPL_EXCEPTION.txt in this package.
# 
# If you are unsure which license is appropriate for your use, please
# contact the sales department at sales@riverbankcomputing.com.
# 
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.


import sys
import os
import glob
import optparse
import shutil

import sipconfig


# Initialise the globals.
pyqt_version = 0x040a00
pyqt_version_str = "snapshot-4.10-f0118624625e"

sip_min_version = 0x040e03

qt_version = 0
qt_edition = ""
qt_licensee = None
qt_dir = None
qt_incdir = None
qt_libdir = None
qt_bindir = None
qt_datadir = None
qt_pluginsdir = None
qt_xfeatures = None
qt_shared = ""
qt_framework = 0

# This default value will be used by the code that bootstraps the Qt
# configuration.
qt_macx_spec = 'macx-g++'

qt_sip_flags = []

pyqt_modules = []
pyqt_modroot = None
src_dir = os.path.dirname(os.path.abspath(__file__))

# Get the SIP configuration.
sipcfg = sipconfig.Configuration()

pydbusmoddir = None
dbusincdirs = []
dbuslibdirs = []
dbuslibs = []


# Under Windows qmake and the Qt DLLs must be on the system PATH otherwise the
# dynamic linker won't be able to resolve the symbols.  On other systems we
# assume we can just run qmake by using its full pathname.
if sys.platform == 'win32':
    MSG_CHECK_QMAKE = "Make sure you have a working Qt qmake on your PATH."
else:
    MSG_CHECK_QMAKE = "Make sure you have a working Qt qmake on your PATH or use the \
-q argument to explicitly specify a working Qt qmake."


def find_default_qmake():
    """Find a default qmake, ie. the first on the path.
    """
    try:
        path = os.environ["PATH"]
    except KeyError:
        path = ""

    if sys.platform == 'win32':
        base_qmake = "qmake.exe"
    else:
        base_qmake = "qmake"

    for d in path.split(os.pathsep):
        qmake = os.path.join(d, base_qmake)
  
        if os.access(qmake, os.X_OK):
            return qmake

    return ""


def create_optparser():
    """Create the parser for the command line.
    """
    qmake = find_default_qmake()

    def store_abspath(option, opt_str, value, parser):
        setattr(parser.values, option.dest, os.path.abspath(value))

    def store_abspath_dir(option, opt_str, value, parser):
        if not os.path.isdir(value):
            raise optparse.OptionValueError("'%s' is not a directory" % value)
        setattr(parser.values, option.dest, os.path.abspath(value))
        
    def store_abspath_file(option, opt_str, value, parser):
        if not os.path.isfile(value):
            raise optparse.OptionValueError("'%s' is not a file" % value)
        setattr(parser.values, option.dest, os.path.abspath(value))

    p = optparse.OptionParser(usage="python %prog [opts] [macro=value] "
            "[macro+=value]", version=pyqt_version_str)

    # Note: we don't use %default to be compatible with Python 2.3.
    p.add_option("-k", "--static", action="store_true", default=False,
            dest="static", help="build modules as static libraries")
    p.add_option("--no-docstrings", action="store_true", default=False,
            dest="no_docstrings", help="disable the generation of docstrings")
    p.add_option("-r", "--trace", action="store_true", default=False,
            dest="tracing", help="build modules with tracing enabled")
    p.add_option("-u", "--debug", action="store_true", default=False,
            help="build modules with debugging symbols")
    p.add_option("-w", "--verbose", action="count", default=0, dest="verbose",
            help="verbose output during configuration")

    p.add_option("-c", "--concatenate", action="store_true", default=False,
            dest="concat", help="concatenate each module's C++ source files")
    p.add_option("-j", "--concatenate-split", type="int", default=1,
            metavar="N", dest="split",
            help="split the concatenated C++ source files into N pieces "
            "[default: 1]")
    p.add_option("-g", "--consolidate", action="store_true", default=False,
            dest="bigqt", help="create a single module which links against "
            "all the Qt libraries")

    # These are internal options used to build the mega Windows GPL package.
    p.add_option("--mwg-odbc", action="store_true", default=False,
            dest="mwg_odbc", help=optparse.SUPPRESS_HELP)
    p.add_option("--mwg-openssl", action="callback", default=None,
            dest="mwg_ssl_dir", metavar="DIR", callback=store_abspath_dir,
            type="string", help=optparse.SUPPRESS_HELP)
    p.add_option("--mwg-qsci", action="callback", default=None,
            dest="mwg_qsci_dir", metavar="DIR", callback=store_abspath_dir,
            type="string", help=optparse.SUPPRESS_HELP)
    p.add_option("--mwg-qwt", action="callback", default=None,
            dest="mwg_qwt_dir", metavar="DIR", callback=store_abspath_dir,
            type="string", help=optparse.SUPPRESS_HELP)

    # Configuration.
    g = optparse.OptionGroup(p, title="Configuration")
    g.add_option("--confirm-license", action="store_true", default=False,
            dest="license_confirmed", help="confirm acceptance of the license")
    g.add_option("-e", "--enable", action="append", default=[],
            metavar="MODULE", dest="enabled", help="enable checks for the "
            "specified MODULE [default: checks for all modules will be "
            "enabled]")
    g.add_option("--no-designer-plugin", action="store_false", default=True,
            dest="designer_plugin", help="disable the building of the "
            "Python plugin for Qt Designer [default: enabled]")
    g.add_option("-t", "--plugin", action="append", default=[],
            metavar="PLUGIN", dest="staticplugins", help="add PLUGIN to the "
            "list be linked (if Qt is built as static libraries)")
    g.add_option("--assume-shared", action="store_true", default=False,
            dest="assume_shared", help="assume that the Qt libraries have "
            "been built as shared libraries [default: check]")
    g.add_option("-T", "--no-timestamp", action="store_true", default=False,
            dest="no_timestamp", help="suppress timestamps in the header "
            "comments of generated code [default: include timestamps]")
    g.add_option("--no-deprecated", action="store_false", default=True,
            dest="with_deprecated", help="disable Qt v4 features deprecated "
            "in Qt v5 [default: enabled]")

    if sys.platform != 'win32':
        if sys.platform.startswith('linux') or sys.platform == 'darwin':
            pip_default = True
            pip_default_str = "enabled"
        else:
            pip_default = False
            pip_default_str = "disabled"

        g.add_option("--protected-is-public", action="store_true",
                default=pip_default, dest="prot_is_public",
                help="enable building with 'protected' redefined as 'public' "
                        "[default: %s]" % pip_default_str)
        g.add_option("--protected-not-public", action="store_false",
                dest="prot_is_public",
                help="disable building with 'protected' redefined as 'public'")
        g.add_option("-q", "--qmake", action="callback", metavar="FILE",
                default=qmake, dest="qmake", callback=store_abspath_file,
                type="string",
                help="the pathname of qmake [default: %s]" % (qmake or "none"))

    g.add_option("-s", "--dbus", action="callback", metavar="DIR",
            dest="pydbusincdir", callback=store_abspath_dir, type="string",
            help="the directory containing the dbus/dbus-python.h header file "
            "[default: supplied by pkg-config]")
    p.add_option_group(g)

    if sys.platform == 'darwin':
        g = optparse.OptionGroup(p, title="MacOS X Configuration")
        g.add_option("--use-arch", action="store", metavar="ARCH",
                dest="use_arch", choices=["i386", "x86_64", "ppc"],
                help="the architecture to use when running pyuic4 "
                        "[default: system default]")
        p.add_option_group(g)

    # Installation.
    g = optparse.OptionGroup(p, title="Installation")
    g.add_option("-b", "--bindir", action="callback",
            default=sipcfg.default_bin_dir, type="string", metavar="DIR",
            dest="pyqtbindir", callback=store_abspath, help="where pyuic4, "
            "pyrcc4 and pylupdate4 will be installed [default: %s]" %
            sipcfg.default_bin_dir)
    g.add_option("-d", "--destdir", action="callback",
            default=sipcfg.default_mod_dir, type="string", metavar="DIR",
            dest="pyqtmoddir", callback=store_abspath, help="where the PyQt4 "
            "Python package will be installed [default: %s]" %
            sipcfg.default_mod_dir)
    g.add_option("-p", "--plugin-destdir", action="callback", type="string",
            metavar="DIR", dest="plugindir", callback=store_abspath,
            help="where any plugins will be installed [default: "
            "QTDIR/plugins]")
    g.add_option("--no-sip-files", action="store_false", default=True,
            dest="install_sipfiles", help="disable the installation of the "
            ".sip files [default: enabled]")
    g.add_option("-v", "--sipdir", action="callback",
            default=os.path.join(sipcfg.default_sip_dir, "PyQt4"),
            metavar="DIR", dest="pyqtsipdir", callback=store_abspath,
            type="string", help="where the PyQt4 .sip files will be installed "
            "[default: %s]" % sipcfg.default_sip_dir)
    p.add_option_group(g)

    # Vendor ID.
    g = optparse.OptionGroup(p, title="VendorID support")
    g.add_option("-i", "--vendorid", action="store_true", default=False,
            dest="vendorcheck", help="enable checking of signed interpreters "
            "using the VendorID package [default: disabled]")
    g.add_option("-l", "--vendorid-incdir", action="callback",
            default=sipcfg.py_inc_dir, type="string", metavar="DIR",
            dest="vendincdir", callback=store_abspath_dir, help="the "
            "directory containing the VendorID header file [default: %s]" %
            sipcfg.py_inc_dir)
    g.add_option("-m", "--vendorid-libdir", action="callback",
            default=sipcfg.py_lib_dir, type="string", metavar="DIR",
            dest="vendlibdir", callback=store_abspath_dir, help="the "
            "directory containing the VendorID library [default: %s]" %
            sipcfg.py_lib_dir)
    p.add_option_group(g)

    # QScintilla.
    g = optparse.OptionGroup(p, title="QScintilla support")
    g.add_option("-a", "--qsci-api", action="store_true", default=None,
            dest="api", help="always install the PyQt API file for QScintilla "
            "[default: install only if QScintilla installed]")
    g.add_option("--no-qsci-api", action="store_false", default=None,
            dest="api", help="do not install the PyQt API file for QScintilla "
            "[default: install only if QScintilla installed]")
    g.add_option("-n", "--qsci-api-destdir", action="callback", dest="qscidir",
            metavar="DIR", callback=store_abspath, type="string", help="where "
            "the PyQt API file for QScintilla will be installed [default: "
            "QTDIR/qsci]")
    p.add_option_group(g)

    return p


class pyrccMakefile(sipconfig.ProgramMakefile):
    """This class implements the Makefile for pyrcc.  This is specialised so
    that pyrcc is automatically run against the examples.
    """

    def __init__(self):
        sipconfig.ProgramMakefile.__init__(self, configuration=sipcfg,
                build_file=os.path.join(src_dir, "pyrcc", "pyrcc.sbf"),
                dir="pyrcc", install_dir=opts.pyqtbindir, console=1,
                qt=["QtCore", "QtXml"], debug=opts.debug, warnings=1,
                universal=sipcfg.universal, arch=sipcfg.arch,
                deployment_target=sipcfg.deployment_target)

    def generate_target_default(self, mfile):
        """Generate the default target."""
        sipconfig.ProgramMakefile.generate_target_default(self, mfile)

        # The correct call to pyrcc depends on the Python version.
        if sys.hexversion >= 0x03000000:
            flag = "-py3"
        else:
            flag = "-py2"

        exe = "$(TARGET)"
        if sys.platform != 'win32':
            exe = "./" + exe

        # Find all the .qrc files in the examples.
        for root, _, files in os.walk("examples"):
            rel_root = os.path.join("..", root)

            for fn in files:
                if fn.endswith(".qrc"):
                    mfile.write("\t%s %s -o %s %s\n" % (exe, flag, \
os.path.join(rel_root, fn[:-4] + "_rc.py"), os.path.join(rel_root, fn)))


class ConfigurePyQt4:
    """This class defines the methods to configure PyQt4.
    """
    def __init__(self, generator):
        self.generator = generator

    def qt_version_tags(self):
        """Get the versions tags for the configuration.

        Returns a dictionary of versions and corresponding tags.
        """
        return {
            0x040101: None,
            0x040102: "Qt_4_1_1",
            0x040103: "Qt_4_1_2",
            0x040200: "Qt_4_1_3",
            0x040202: "Qt_4_2_0",
            0x040300: "Qt_4_2_2",
            0x040303: "Qt_4_3_0",
            0x040400: "Qt_4_3_3",
            0x040401: "Qt_4_4_0",
            0x040500: "Qt_4_4_1",
            0x040501: "Qt_4_5_0",
            0x040600: "Qt_4_5_1",
            0x040601: "Qt_4_6_0",
            0x040602: "Qt_4_6_1",
            0x040603: "Qt_4_6_2",
            0x040700: "Qt_4_6_3",
            0x040701: "Qt_4_7_0",
            0x040702: "Qt_4_7_1",
            0x040800: "Qt_4_7_2",
            0x040803: "Qt_4_8_0",
            0x040804: "Qt_4_8_3",
            0x050000: "Qt_4_8_4",
            0x060000: "Qt_5_0_0"
        }

    def check_modules(self):
        if opts.mwg_odbc:
            sql_libs = ["odbc32"]
        else:
            sql_libs = None

        if opts.mwg_ssl_dir:
            ass_lib_dirs = [os.path.join(opts.mwg_ssl_dir, "lib")]
            ass_libs = ["ssleay32", "libeay32"]
        else:
            ass_lib_dirs = None
            ass_libs = None

        # Note that the order in which we check is important for the
        # consolidated module - a module's dependencies must be checked first.
        pyqt_modules.append("QtCore")

        #import rpdb2; rpdb2.start_embedded_debugger('1')

        check_module("QtGui", "qwidget.h", "new QWidget()")
        check_module("QtHelp", "qhelpengine.h", "new QHelpEngine(\"foo\")")
        check_module("QtMultimedia", "QAudioDeviceInfo",
                "new QAudioDeviceInfo()")
        check_module("QtNetwork", "qhostaddress.h", "new QHostAddress()")

        # Qt v4.7 was current when we added support for QtDBus and we didn't
        # bother properly versioning its API.
        if qt_version >= 0x040700:
            check_module("QtDBus", "qdbusconnection.h",
                    "QDBusConnection::systemBus()")

        check_module("QtDeclarative", "qdeclarativeview.h",
                "new QDeclarativeView()")
        check_module("QtOpenGL", "qgl.h", "new QGLWidget()")
        check_module("QtScript", "qscriptengine.h", "new QScriptEngine()")
        check_module("QtScriptTools", "qscriptenginedebugger.h",
                "new QScriptEngineDebugger()")
        check_module("QtSql", "qsqldatabase.h", "new QSqlDatabase()",
                extra_libs=sql_libs)
        check_module("QtSvg", "qsvgwidget.h", "new QSvgWidget()")
        check_module("QtTest", "QtTest", "QTest::qSleep(0)")
        check_module("QtWebKit", "qwebpage.h", "new QWebPage()")
        check_module("QtXml", "qdom.h", "new QDomDocument()")
        check_module("QtXmlPatterns", "qxmlname.h", "new QXmlName()")
        check_module("phonon", "phonon/videowidget.h",
                "new Phonon::VideoWidget()")
        check_module("QtAssistant", "qassistantclient.h",
                "new QAssistantClient(\"foo\")", extra_lib_dirs=ass_lib_dirs,
                extra_libs=ass_libs)

        if not qt_shared:
            sipconfig.inform("QtDesigner module disabled with static Qt libraries.")
        elif sipcfg.universal:
            sipconfig.inform("QtDesigner module disabled with universal binaries.")
        else:
            check_module("QtDesigner", "QExtensionFactory",
                    "new QExtensionFactory()")

        check_module("QAxContainer", "qaxobject.h", "new QAxObject()",
                extra_libs=["QAxContainer"])

        if os.path.isdir(os.path.join(src_dir, "dbus")):
            check_dbus()

    def code(self):
        cons_xtra_incdirs = []
        cons_xtra_libdirs = []
        cons_xtra_libs = []

        sp_libs, sp_libdirs = self._static_plugins("QtCore")
        sp_incdirs = []

        qpy_inc_dir, qpy_lib_dir, qpy_lib = self._qpy_directories("QtCore", \
"qpycore")  sp_incdirs.append(qpy_inc_dir)
        sp_libdirs.append(qpy_lib_dir)
        sp_libs.append(qpy_lib)

        if opts.vendorcheck:
            sp_incdirs.append(opts.vendincdir)
            sp_libdirs.append(opts.vendlibdir)
            sp_libs.append("vendorid")

        if opts.bigqt:
            cons_xtra_incdirs.extend(sp_incdirs)
            cons_xtra_libdirs.extend(sp_libdirs)
            cons_xtra_libs.extend(sp_libs)

            generate_code("QtCore")
        else:
            generate_code("QtCore", extra_include_dirs=sp_incdirs,
                        extra_lib_dirs=sp_libdirs, extra_libs=sp_libs)

        if "QtDeclarative" in pyqt_modules:
            qpy_inc_dir, qpy_lib_dir, qpy_lib = \
self._qpy_directories("QtDeclarative", "qpydeclarative")

            if opts.bigqt:
                cons_xtra_incdirs.append(qpy_inc_dir)
                cons_xtra_libdirs.append(qpy_lib_dir)
                cons_xtra_libs.append(qpy_lib)

                generate_code("QtDeclarative")
            else:
                generate_code("QtDeclarative", extra_include_dirs=[qpy_inc_dir],
                        extra_lib_dirs=[qpy_lib_dir], extra_libs=[qpy_lib])

        if "QtGui" in pyqt_modules:
            sp_libs, sp_libdirs = self._static_plugins("QtGui")
            sp_incdirs = []

            qpy_inc_dir, qpy_lib_dir, qpy_lib = self._qpy_directories("QtGui", \
"qpygui")  sp_incdirs.append(qpy_inc_dir)
            sp_libdirs.append(qpy_lib_dir)
            sp_libs.append(qpy_lib)

            if opts.bigqt:
                cons_xtra_incdirs.extend(sp_incdirs)
                cons_xtra_libdirs.extend(sp_libdirs)
                cons_xtra_libs.extend(sp_libs)

                generate_code("QtGui")
            else:
                generate_code("QtGui", extra_include_dirs=sp_incdirs,
                        extra_lib_dirs=sp_libdirs, extra_libs=sp_libs)

        if "QtHelp" in pyqt_modules:
            generate_code("QtHelp")

        if "QtMultimedia" in pyqt_modules:
            generate_code("QtMultimedia")

        if "QtNetwork" in pyqt_modules:
            generate_code("QtNetwork")

        if "QtDBus" in pyqt_modules:
            qpy_inc_dir, qpy_lib_dir, qpy_lib = self._qpy_directories("QtDBus", \
"qpydbus")

            if opts.bigqt:
                cons_xtra_incdirs.append(qpy_inc_dir)
                cons_xtra_libdirs.append(qpy_lib_dir)
                cons_xtra_libs.append(qpy_lib)

                generate_code("QtDBus")
            else:
                generate_code("QtDBus", extra_include_dirs=[qpy_inc_dir],
                        extra_lib_dirs=[qpy_lib_dir], extra_libs=[qpy_lib])

        if "QtOpenGL" in pyqt_modules:
            generate_OpenGL_extras()

            qpy_inc_dir, qpy_lib_dir, qpy_lib = self._qpy_directories("QtOpenGL", \
"qpyopengl")

            if opts.bigqt:
                cons_xtra_incdirs.append(qpy_inc_dir)
                cons_xtra_libdirs.append(qpy_lib_dir)
                cons_xtra_libs.append(qpy_lib)

                generate_code("QtOpenGL")
            else:
                generate_code("QtOpenGL", extra_include_dirs=[qpy_inc_dir],
                        extra_lib_dirs=[qpy_lib_dir], extra_libs=[qpy_lib])

        if "QtScript" in pyqt_modules:
            generate_code("QtScript")

        if "QtScriptTools" in pyqt_modules:
            generate_code("QtScriptTools")

        if "QtSql" in pyqt_modules:
            sp_libs, sp_libdirs = self._static_plugins("QtSql")

            if opts.bigqt:
                cons_xtra_libdirs.extend(sp_libdirs)
                cons_xtra_libs.extend(sp_libs)

                generate_code("QtSql")
            else:
                generate_code("QtSql", extra_lib_dirs=sp_libdirs,
                        extra_libs=sp_libs)

        if "QtSvg" in pyqt_modules:
            generate_code("QtSvg")

        if "QtTest" in pyqt_modules:
            generate_code("QtTest")

        if "QtWebKit" in pyqt_modules:
            generate_code("QtWebKit")

        if "QtXml" in pyqt_modules:
            generate_code("QtXml")

        if "QtXmlPatterns" in pyqt_modules:
            generate_code("QtXmlPatterns")

        if "phonon" in pyqt_modules:
            generate_code("phonon")

        if "QtAssistant" in pyqt_modules:
            generate_code("QtAssistant")

        if "QtDesigner" in pyqt_modules:
            qpy_inc_dir, qpy_lib_dir, qpy_lib = self._qpy_directories("QtDesigner", \
"qpydesigner")

            if opts.bigqt:
                cons_xtra_incdirs.append(qpy_inc_dir)
                cons_xtra_libdirs.append(qpy_lib_dir)
                cons_xtra_libs.append(qpy_lib)

                generate_code("QtDesigner")
            else:
                generate_code("QtDesigner", extra_include_dirs=[qpy_inc_dir],
                        extra_lib_dirs=[qpy_lib_dir], extra_libs=[qpy_lib])

        if "QAxContainer" in pyqt_modules:
            generate_code("QAxContainer")

        # Generate the composite module.
        qtmod_sipdir = os.path.join("sip", "Qt")
        mk_clean_dir(qtmod_sipdir)

        qtmod_sipfile = os.path.join(qtmod_sipdir, "Qtmod.sip")
        f = open(qtmod_sipfile, "w")

        f.write("""%CompositeModule PyQt4.Qt

""")

        for m in pyqt_modules:
            f.write("%%Include %s/%smod.sip\n" % (m, m))

        f.close()

        generate_code("Qt")

        # Generate the consolidated module if required.
        if opts.bigqt:
            xtra_sip_flags = []

            _qtmod_sipdir = os.path.join("sip", "_qt")
            mk_clean_dir(_qtmod_sipdir)

            _qtmod_sipfile = os.path.join(_qtmod_sipdir, "_qtmod.sip")
            f = open(_qtmod_sipfile, "w")

            f.write("""%ConsolidatedModule PyQt4._qt

""")

            for m in pyqt_modules:
                f.write("%%Include %s/%smod.sip\n" % (m, m))

            if opts.mwg_qsci_dir:
                f.write("%Include Qsci/Qscimod.sip\n")
                cons_xtra_libs.append("qscintilla2")

                # Copy in the QScintilla .sip files and fix the main one.
                src_dir = os.path.join(opts.mwg_qsci_dir, "Python", "sip")
                dst_dir = os.path.join("sip", "Qsci")

                try:
                    shutil.rmtree(dst_dir);
                except:
                    pass

                shutil.copytree(src_dir, dst_dir)
                os.rename(os.path.join(dst_dir, "qscimod4.sip"), \
os.path.join(dst_dir, "Qscimod.sip"))

                generate_code("Qsci")

            if opts.mwg_qwt_dir:
                f.write("%Include Qwt5/Qwt5mod.sip\n")
                cons_xtra_incdirs.append(os.path.join(opts.mwg_qwt_dir, "support"))
                cons_xtra_libs.append("qwt")

                # Copy in the PyQwt .sip files and fix the main one.
                src_dir = os.path.join(opts.mwg_qwt_dir, "sip", "qwt5qt4")
                dst_dir = os.path.join("sip", "Qwt5")

                try:
                    shutil.rmtree(dst_dir);
                except:
                    pass

                shutil.copytree(src_dir, dst_dir)
                os.rename(os.path.join(dst_dir, "QwtModule.sip"), \
os.path.join(dst_dir, "Qwt5mod.sip"))

                xtra_sip_flags = ["-t", "Qwt_5_0_1",
                                  "-x", "CXX_DYNAMIC_CAST",
                                  "-x", "HAS_QWT4",
                                  "-x", "HAS_NUMARRAY",
                                  "-x", "HAS_NUMERIC",
                                  "-x", "HAS_NUMPY"]

                generate_code("Qwt5", extra_sip_flags=xtra_sip_flags)

            f.close()

            if opts.mwg_odbc:
                cons_xtra_libs.append("odbc32")

            if opts.mwg_ssl_dir:
                cons_xtra_libdirs.append(os.path.join(opts.mwg_ssl_dir, "lib"))
                cons_xtra_libs.extend(["ssleay32", "libeay32"])

            generate_code("_qt", extra_include_dirs=cons_xtra_incdirs,
                    extra_lib_dirs=cons_xtra_libdirs,
                    extra_libs=cons_xtra_libs, extra_sip_flags=xtra_sip_flags)

        # Tell the user about any plugins not found.
        if opts.staticplugins:
            sipconfig.inform("Unable to find the following static plugins: %s" % ", \
".join(opts.staticplugins))

        # Generate the QScintilla API file.
        sipconfig.inform("Creating QScintilla API file...")
        f = open("PyQt4.api", "w")

        for m in pyqt_modules:
            api = open(m + ".api")

            for l in api:
                f.write("PyQt4." + l)

            api.close()
            os.remove(m + ".api")

        f.close()

    def _qpy_directories(self, mname, lib_name):
        """Return a 3-tuple of the directories containing the header files, the
        directory containing the library, and the name of the support library
        for the given module.

        mname is the name of the module.
        lib_name is the normal name of the support library.
        """
        qpy_dir = os.path.join("qpy", mname)

        if sys.platform == 'win32':
            if opts.debug:
                qpy_lib_dir = os.path.join(qpy_dir, "debug")
                lib_name = 'd' + lib_name
            else:
                qpy_lib_dir = os.path.join(qpy_dir, "release")
        else:
            qpy_lib_dir = qpy_dir

            if sys.platform == 'darwin' and opts.debug:
                lib_name += '_debug'

        return os.path.join(src_dir, qpy_dir), os.path.abspath(qpy_lib_dir), lib_name

    def _static_plugins(self, mname):
        """Return a tuple of the libraries (in platform neutral format) and the
        directories they are contained in for all the requested static plugins
        for the given module.  Generate the additional .sip file needed to
        ensure the plugins get linked.

        mname is the name of the module.
        """
        plugin_dirs = {
            "QtCore":   ("codecs", ),
            # Note that we put iconengines after imageformats so that qsvg is
            # found in the latter rather than the former.  The name clash is
            # probably a Qt bug.
            "QtGui":    ("inputmethods", "imageformats", "iconengines"),
            "QtSql":    ("sqldrivers", )
        }

        libs = []
        libdirs = []

        for plug in opts.staticplugins:
            # Convert the plugin name to a platform specific filename.
            if self.generator in ("MSVC", "MSVC.NET", "BMAKE"):
                pfname = plug + ".lib"
            else:
                pfname = "lib" + plug + ".a"

            for pdir in plugin_dirs[mname]:
                ppath = os.path.join(qt_pluginsdir, pdir)

                # See if the plugin exists.
                if os.access(os.path.join(ppath, pfname), os.F_OK):
                    sipconfig.inform("Adding the %s static plugin to the %s \
module..." % (plug, mname))

                    libs.append(plug)

                    if ppath not in libdirs:
                        libdirs.append(ppath)

                    break

        # Remove those plugins we have handled.
        opts.staticplugins = [p for p in opts.staticplugins if p not in libs]

        # If we have any plugins for this module then generate a .sip file that
        # will include the code needed to ensure the plugin gets linked.
        if libs:
            sp_sipfile = os.path.join("sip", mname, "staticplugins.sip")

            f = open(sp_sipfile, "w")

            f.write("""%ModuleCode

#include <QtPlugin>

""")

            for l in libs:
                f.write("Q_IMPORT_PLUGIN(%s)\n" % l)

            f.write("""
%End
""")

            f.close()

        return libs, libdirs

    def module_installs(self):
        return [os.path.join(src_dir, "__init__.py"), "pyqtconfig.py"]

    def qpy_libs(self):
        # See which QPy support libraries to build.
        qpylibs = {}

        if "QtCore" in pyqt_modules:
            qpylibs["QtCore"] = "qpycore.pro"

        if "QtGui" in pyqt_modules:
            qpylibs["QtGui"] = "qpygui.pro"

        if "QtDBus" in pyqt_modules:
            qpylibs["QtDBus"] = "qpydbus.pro"

        if "QtDeclarative" in pyqt_modules:
            qpylibs["QtDeclarative"] = "qpydeclarative.pro"

        if "QtDesigner" in pyqt_modules:
            qpylibs["QtDesigner"] = "qpydesigner.pro"

        if "QtOpenGL" in pyqt_modules:
            qpylibs["QtOpenGL"] = "qpyopengl.pro"

        # Run qmake to generate the Makefiles.
        qmake_args = fix_qmake_args()
        cwd = os.getcwd()

        for qpy, pro in qpylibs.items():
            sipconfig.inform("Creating QPy support library for %s Makefile..." % qpy)

            qpydir = os.path.join("qpy", qpy)
            mk_clean_dir(qpydir, clean=0)
            os.chdir(qpydir)

            wrapped_pro = "w_" + pro

            f = open(wrapped_pro, 'w+')

            if sipcfg.arch:
                f.write(arch_config())

            if sipcfg.universal:
                f.write("QMAKE_MAC_SDK = %s\n" % sipcfg.universal)

            if sipcfg.deployment_target:
                f.write("QMAKE_MACOSX_DEPLOYMENT_TARGET = %s\n" % \
sipcfg.deployment_target)

            inc_path = [sipcfg.py_inc_dir]
            if qpy in ("QtCore", "QtDBus", "QtDeclarative", "QtOpenGL"):
                if sipcfg.sip_inc_dir != sipcfg.py_inc_dir:
                    inc_path.insert(0, sipcfg.sip_inc_dir)

                if sipcfg.py_conf_inc_dir != sipcfg.py_inc_dir:
                    inc_path.insert(0, sipcfg.py_conf_inc_dir)

                if opts.bigqt:
                    api_dir = "../../_qt"
                else:
                    api_dir = "../../" + qpy

                inc_path.append(api_dir)

            if opts.debug:
                pro_config = 'debug'
            else:
                pro_config = 'release'

            if src_dir != os.path.curdir:
                src_qpydir = os.path.join(src_dir, "qpy", qpy)
                pro = os.path.join(src_qpydir, pro)
                vpath = "VPATH = " + src_qpydir
                inc_path.append(src_qpydir)
            else:
                vpath = ""

            f.write(
"""# Tell the original .pro file about additional directories.
INCLUDEPATH = %s
CONFIG += %s
%s
include(%s)
""" % (" ".join(['"' + i + '"' for i in inc_path]), pro_config, vpath, pro))

            f.close()

            run_command("%s %s %s" % (opts.qmake, qmake_args, wrapped_pro))
            os.chdir(cwd)

        sipconfig.inform("Creating QPy support libraries Makefile...")

        sipconfig.ParentMakefile(
            configuration=sipcfg,
            dir="qpy",
            subdirs=list(qpylibs.keys())
        ).generate()

        return ["qpy"]

    def tools(self):
        tool = []

        if pydbusmoddir:
            sipconfig.inform("Creating dbus support module Makefile...")

            makefile = sipconfig.ModuleMakefile(
                configuration=sipcfg,
                build_file=os.path.join(src_dir, "dbus", "dbus.sbf"),
                dir="dbus",
                install_dir=pydbusmoddir,
                qt=["QtCore"],
                debug=opts.debug,
                universal=sipcfg.universal,
                arch=sipcfg.arch,
                deployment_target=sipcfg.deployment_target
            )

            add_makefile_extras(makefile, dbusincdirs, dbuslibdirs, dbuslibs)

            makefile.generate()
            tool.append("dbus")

        # Only include ElementTree for older versions of Python.
        if sipcfg.py_version < 0x020500:
            sipconfig.inform("Creating elementtree Makefile...")

            makefile = sipconfig.PythonModuleMakefile(
                configuration=sipcfg,
                dstdir=os.path.join(pyqt_modroot, "elementtree"),
                dir="elementtree"
            )

            makefile.generate()
            tool.append("elementtree")

        # Create the pyuic4 wrapper.  Use the GUI version on MacOS (so that
        # previews work properly and normal console use will work anyway), but
        # not on Windows (so that normal console use will work).
        sipconfig.inform("Creating pyuic4 wrapper...")

        if sys.platform == 'darwin':
            gui = True
            use_arch = opts.use_arch
        else:
            gui = False
            use_arch = ''

        # The pyuic directory may not exist if we are building away from the
        # source directory.
        try:
            os.mkdir("pyuic")
        except OSError:
            pass

        uicdir=os.path.join(pyqt_modroot, "uic")
        wrapper = sipconfig.create_wrapper(os.path.join(uicdir, "pyuic.py"), \
os.path.join("pyuic", "pyuic4"), gui, use_arch)

        sipconfig.inform("Creating pyuic4 Makefile...")

        makefile = sipconfig.PythonModuleMakefile(
            configuration=sipcfg,
            dstdir=uicdir,
            srcdir=os.path.join(src_dir, "pyuic", "uic"),
            dir="pyuic",
            installs=[[os.path.basename(wrapper), opts.pyqtbindir]]
        )

        makefile.generate()
        tool.append("pyuic")

        if "QtXml" in pyqt_modules:
            sipconfig.inform("Creating pylupdate4 Makefile...")

            cxxflags_app = sipcfg.build_macros().get("CXXFLAGS_APP", "")

            makefile = sipconfig.ProgramMakefile(
                configuration=sipcfg,
                build_file=os.path.join(src_dir, "pylupdate", "pylupdate.sbf"),
                dir="pylupdate",
                install_dir=opts.pyqtbindir,
                console=1,
                qt=["QtCore", "QtXml"],
                debug=opts.debug,
                warnings=1,
                universal=sipcfg.universal,
                arch=sipcfg.arch,
                deployment_target=sipcfg.deployment_target
            )

            makefile.extra_include_dirs.append(
                    os.path.join(src_dir, "pylupdate"))

            if cxxflags_app != "":
                makefile.extra_cxxflags.append(cxxflags_app)

            makefile.generate()
            tool.append("pylupdate")

            sipconfig.inform("Creating pyrcc4 Makefile...")

            makefile = pyrccMakefile()

            if cxxflags_app != "":
                makefile.extra_cxxflags.append(cxxflags_app)

            makefile.generate()
            tool.append("pyrcc")
        else:
            sipconfig.inform("pylupdate4 and pyrcc4 will not be built because the Qt \
XML module is missing.")

        if opts.designer_plugin and "QtDesigner" in pyqt_modules:
            py_major = sipcfg.py_version >> 16
            py_minor = (sipcfg.py_version >> 8) & 0x0ff

            abi = getattr(sys, 'abiflags', '')

            if sys.platform == 'win32':
                # Use abiflags in case it is supported in a future version.
                lib_dir_flag = quote("-L%s" % sipcfg.py_lib_dir)
                link = "%s -lpython%d%d%s" % (lib_dir_flag, py_major, py_minor, abi)
                pysh_lib = "python%d%d%s.dll" % (py_major, py_minor, abi)
            else:
                # Use distutils to get the additional configuration.
                from distutils.sysconfig import get_config_vars
                ducfg = get_config_vars()

                config_args = ducfg.get("CONFIG_ARGS", "")

                if sys.platform == "darwin":
                    dynamic_pylib = "--enable-framework" in config_args

                    # It's probably a Python bug that the library name doesn't
                    # include the ABI information.
                    abi = ""
                else:
                    dynamic_pylib = "--enable-shared" in config_args

                if dynamic_pylib:
                    if glob.glob("%s/lib/libpython%d.%d*" % (ducfg["exec_prefix"], \
                py_major, py_minor)):
                        lib_dir_flag = quote("-L%s/lib" % ducfg["exec_prefix"])
                    elif glob.glob("%s/libpython%d.%d*" % (ducfg["LIBDIR"], py_major, \
py_minor)):  lib_dir_flag = quote("-L%s" % ducfg["LIBDIR"])
                    else:
                        sipconfig.inform("Qt Designer plugin disabled because Python \
library couldn't be found")  lib_dir_flag = ''
                        opts.designer_plugin = False

                    link = "%s -lpython%d.%d%s" % (lib_dir_flag, py_major, py_minor, \
abi)  else:
                    sipconfig.inform("Qt Designer plugin disabled because Python \
library is static")  opts.designer_plugin = False

                pysh_lib = ducfg.get("LDLIBRARY", "")

            if opts.designer_plugin:
                sipconfig.inform("Creating Qt Designer plugin Makefile...")

                # Run qmake to generate the Makefile.
                qmake_args = fix_qmake_args()
                cwd = os.getcwd()

                mk_clean_dir("designer", clean=0)
                os.chdir("designer")

                # Create the qmake project file.
                fin = open(os.path.join(src_dir, "designer", "python.pro-in"))
                prj = fin.read()
                fin.close()

                prj = prj.replace("@PYINCDIR@", quote(sipcfg.py_inc_dir))
                prj = prj.replace("@PYINCDIR@", " \
".join((quote(sipcfg.py_conf_inc_dir), quote(sipcfg.py_inc_dir))))  prj = \
prj.replace("@PYLINK@", link)  prj = prj.replace("@PYSHLIB@", pysh_lib)
                prj = prj.replace("@QTPLUGINDIR@", quote(opts.plugindir + \
"/designer"))

                fout = open("python.pro", "w+")

                if sipcfg.arch:
                    fout.write(arch_config())

                if sipcfg.universal:
                    fout.write("QMAKE_MAC_SDK = %s\n" % sipcfg.universal)

                if sipcfg.deployment_target:
                    fout.write("QMAKE_MACOSX_DEPLOYMENT_TARGET = %s\n" % \
sipcfg.deployment_target)

                if src_dir != os.path.curdir:
                    fout.write("VPATH = %s\n" % os.path.join(src_dir, "designer"))

                fout.write(prj)
                fout.close()

                run_command("%s %s" % (opts.qmake, qmake_args))
                os.chdir(cwd)

                tool.append("designer")

        return tool


def arch_config():
    """Return the qmake CONFIG line for a MacOS binary."""

    qmake_archs = []
    for a in sipcfg.arch.split():
        if a == 'i386':
            qmake_archs.append('x86')
        elif a == 'x86_64':
            qmake_archs.append('x86_64')
        elif a == 'ppc':
            qmake_archs.append('ppc')

    return 'CONFIG += %s\n' % ' '.join(qmake_archs)


def quote(path):
    """Return a path with quotes added if it contains spaces."""
    if " " in path:
        path = '"%s"' % path

    return path


def inform_user():
    """Tell the user the option values that are going to be used.
    """
    if qt_edition:
        edstr = qt_edition + " edition "
    else:
        edstr = ""

    if qt_shared:
        lib_type = "shared"
    else:
        lib_type = "static"

    sipconfig.inform("Qt v%s %sis being used." % \
(sipconfig.version_to_string(qt_version), edstr))

    if qt_licensee:
        sipconfig.inform("Qt is licensed to %s." % qt_licensee)

    if sys.platform == "darwin" and qt_framework:
        sipconfig.inform("Qt is built as a framework.")

    sipconfig.inform("SIP %s is being used." % sipcfg.sip_version_str)
    sipconfig.inform("The Qt header files are in %s." % qt_incdir)
    sipconfig.inform("The %s Qt libraries are in %s." % (lib_type, qt_libdir))
    sipconfig.inform("The Qt binaries are in %s." % qt_bindir)
    sipconfig.inform("The Qt mkspecs directory is in %s." % qt_datadir)
    sipconfig.inform("These PyQt modules will be built: %s." % ", \
".join(pyqt_modules))  sipconfig.inform("The PyQt Python package will be installed in \
%s." % opts.pyqtmoddir)

    if opts.with_deprecated:
        sipconfig.inform("PyQt is being built with deprecated Qt v4 features.")
    else:
        sipconfig.inform("PyQt is being built without deprecated Qt v4 features.")

    if opts.no_docstrings:
        sipconfig.inform("PyQt is being built without generated docstrings.")
    else:
        sipconfig.inform("PyQt is being built with generated docstrings.")

    if opts.prot_is_public:
        sipconfig.inform("PyQt is being built with 'protected' redefined as \
'public'.")

    if opts.designer_plugin:
        sipconfig.inform("The Designer plugin will be installed in %s." % \
os.path.join(opts.plugindir, "designer"))

    if opts.api:
        sipconfig.inform("The QScintilla API file will be installed in %s." % \
os.path.join(opts.qscidir, "api", "python"))

    if pydbusmoddir:
        sipconfig.inform("The dbus support module will be installed in %s." % \
pydbusmoddir)

    sipconfig.inform("The PyQt .sip files will be installed in %s." % \
opts.pyqtsipdir)

    sipconfig.inform("pyuic4, pyrcc4 and pylupdate4 will be installed in %s." % \
opts.pyqtbindir)

    if opts.vendorcheck:
        sipconfig.inform("PyQt will only be usable with signed interpreters.")


def create_config(module, template, macros):
    """Create the PyQt configuration module so that it can be imported by build
    scripts.

    module is the module file name.
    template is the template file name.
    macros is the dictionary of platform specific build macros.
    """
    sipconfig.inform("Creating %s..." % module)

    content = {
        "pyqt_config_args":   sys.argv[1:],
        "pyqt_version":       pyqt_version,
        "pyqt_version_str":   pyqt_version_str,
        "pyqt_bin_dir":       opts.pyqtbindir,
        "pyqt_mod_dir":       pyqt_modroot,
        "pyqt_sip_dir":       opts.pyqtsipdir,
        "pyqt_modules":       pyqt_modules,
        "pyqt_sip_flags":     qt_sip_flags,
        "qt_version":         qt_version,
        "qt_edition":         qt_edition,
        "qt_winconfig":       qt_shared,
        "qt_framework":       qt_framework,
        "qt_threaded":        1,
        "qt_dir":             qt_dir,
        "qt_data_dir":        qt_datadir,
        "qt_inc_dir":         qt_incdir,
        "qt_lib_dir":         qt_libdir
    }

    sipconfig.create_config_module(module, template, content, macros)


def run_command(cmd, envvars=None):
    """Run a command and display the output if verbose mode is enabled.

    cmd is the command to run.
    """
    if opts.verbose:
        sys.stdout.write(cmd + "\n")

    fout = get_command_stdout(cmd, and_stderr=True, envvars=envvars)

    # Read stdout and stderr until there is no more output.
    lout = fout.readline()
    while lout:
        if opts.verbose:
            if sys.hexversion >= 0x03000000:
                sys.stdout.write(str(lout, encoding=sys.stdout.encoding))
            else:
                sys.stdout.write(lout)

        lout = fout.readline()

    fout.close()

    try:
        os.wait()
    except:
        pass


def remove_file(fname):
    """Remove a file which may or may not exist.

    fname is the name of the file.
    """
    try:
        os.remove(fname)
    except OSError:
        pass


def generate_OpenGL_extras():
    """Generate the extras needed by the QtOpenGL module (i.e. the .sip file
    defining the correct typedefs for the OpenGL data types.
    """
    sipconfig.inform("Determining the OpenGL data types...")

    src = "opengl_extras.cpp"

    f = open(src, "w")

    f.write(
"""#include <QFile>
#include <QTextStream>
#include <qgl.h>

int main(int, char **)
{
    QFile outf("./sip/QtOpenGL/opengl_types.sip");

    if (!outf.open(QIODevice::WriteOnly|QIODevice::Truncate|QIODevice::Text))
        return 1;

    QTextStream out(&outf);

    if (sizeof (long) == sizeof (GLint))
        out << "typedef long GLint;\\n";
    else
        out << "typedef int GLint;\\n";

    if (sizeof (unsigned long) == sizeof (GLuint))
        out << "typedef unsigned long GLuint;\\n";
    else
        out << "typedef unsigned GLuint;\\n";

    if (sizeof (unsigned long) == sizeof (GLenum))
        out << "typedef unsigned long GLenum;\\n";
    else
        out << "typedef unsigned GLenum;\\n";

    if (sizeof (unsigned long) == sizeof (GLbitfield))
        out << "typedef unsigned long GLbitfield;\\n";
    else
        out << "typedef unsigned GLbitfield;\\n";

    out << "typedef float GLfloat;\\n";

    return 0;
}
""")

    f.close()

    cmd = compile_qt_program(src, "QtOpenGL")

    if cmd is None:
        sipconfig.error("Unable to determine the OpenGL data types.")

    # SIP's build system (specifically ProgramMakefile.build_command()) assumes
    # that an executable can be created by running a single command.  In the
    # case of MSVC this is incorrect.  As a quick hack we do the extra step
    # here if it looks like it is needed.
    if os.access("opengl_extras.manifest", os.F_OK):
        run_command("mt -nologo -manifest opengl_extras.manifest \
-outputresource:opengl_extras.exe;1")

    run_command(cmd)


def compile_qt_program(name, mname, extra_include_dirs=None, extra_lib_dirs=None, \
extra_libs=None):  """Compile a simple Qt application.

    name is the name of the single source file.
    mname is the name of the Qt module.
    extra_include_dirs is an optional list of extra include directories.
    extra_lib_dirs is an optional list of extra library directories.
    extra_libs is an optional list of extra libraries.

    Returns the name of the executable suitable for running or None if it
    wasn't created.
    """
    opengl = (mname == "QtOpenGL")

    qt = [mname]
    if mname in ("QtAssistant", "QtHelp", "QtOpenGL", "QtWebKit"):
        qt.append("QtCore")

    makefile = sipconfig.ProgramMakefile(sipcfg, console=1, qt=qt, warnings=0,
            opengl=opengl, debug=opts.debug, arch=sipcfg.arch,
            deployment_target=sipcfg.deployment_target)

    add_makefile_extras(makefile, extra_include_dirs, extra_lib_dirs, extra_libs)

    exe, build = makefile.build_command(name)

    if sipcfg.deployment_target:
        envvars = {'MACOSX_DEPLOYMENT_TARGET': '%s' % sipcfg.deployment_target}
    else:
        envvars = None

    # Make sure the executable file doesn't exist.
    remove_file(exe)
    run_command(build, envvars=envvars)

    if not os.access(exe, os.X_OK):
        return None

    if sys.platform != 'win32':
        exe = "./" + exe

    return exe


def add_makefile_extras(makefile, extra_include_dirs, extra_lib_dirs, extra_libs):
    """Add any extra include or library directories or libraries to a makefile.

    makefile is the makefile.
    extra_include_dirs is the list of extra include directories.
    extra_lib_dirs is the list of extra library directories.
    extra_libs is the list of extra libraries.
    """
    if extra_include_dirs:
        makefile.extra_include_dirs.extend(extra_include_dirs)

    if extra_lib_dirs:
        makefile.extra_lib_dirs.extend(extra_lib_dirs)

    if extra_libs:
        makefile.extra_libs.extend(extra_libs)


def check_vendorid():
    """See if the VendorID library and include file can be found.
    """
    if opts.vendorcheck:
        if os.access(os.path.join(opts.vendincdir, "vendorid.h"), os.F_OK):
            if glob.glob(os.path.join(opts.vendlibdir, "*vendorid*")):
                sipconfig.inform("The VendorID package was found.")
            else:
                opts.vendorcheck = 0
                sipconfig.inform("The VendorID library could not be found in "
                                 "%s and so signed interpreter checking will "
                                 "be disabled. If the VendorID library is "
                                 "installed then use the -m argument to "
                                 "explicitly specify the correct "
                                 "directory." % opts.vendlibdir)
        else:
            opts.vendorcheck = 0
            sipconfig.inform("vendorid.h could not be found in %s and so "
                             "signed interpreter checking will be disabled. "
                             "If the VendorID package is installed then use "
                             "the -l argument to explicitly specify the "
                             "correct directory." % opts.vendincdir)


def get_command_stdout(cmd, and_stderr=False, envvars=None):
    """Return stdout (and optionally stderr) from the given command.
    """
    if envvars is not None:
        env = os.environ.copy()
        env.update(envvars)
    else:
        env = None

    try:
        import subprocess
    except ImportError:
        if and_stderr:
            _, sout = os.popen4(cmd)
        else:
            _, sout, _ = os.popen3(cmd)

        return sout

    if and_stderr:
        stderr = subprocess.STDOUT
    else:
        stderr = subprocess.PIPE

    p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
            stdout=subprocess.PIPE, stderr=stderr, env=env)

    return p.stdout


def check_dbus():
    """See if the DBus support module should be built.
    """
    sipconfig.inform("Checking to see if the dbus support module should be built...")

    sout = get_command_stdout("pkg-config --cflags-only-I --libs dbus-1")
    iflags = sout.read().strip()

    if not iflags:
        sipconfig.inform("DBus v1 does not seem to be installed.")
        return

    if sys.hexversion >= 0x03000000:
        iflags = iflags.decode()

    for f in iflags.split():
        if f.startswith("-I"):
            dbusincdirs.append(f[2:])
        elif f.startswith("-L"):
            dbuslibdirs.append(f[2:])
        elif f.startswith("-l"):
            dbuslibs.append(f[2:])

    try:
        import dbus.mainloop
    except:
        sipconfig.inform("The Python dbus module doesn't seem to be installed.")
        return

    global pydbusmoddir
    pydbusmoddir = dbus.mainloop.__path__[0]

    # Try and find dbus-python.h.  We don't use pkg-config because it is broken
    # for dbus-python (at least for versions up to and including v0.81.0).
    # Instead we look where DBus itself is installed - which in most cases will
    # be where dbus-python is also installed.
    if opts.pydbusincdir:
        dlist = [opts.pydbusincdir]
    else:
        dlist = dbusincdirs

    for d in dlist:
        if os.access(os.path.join(d, "dbus", "dbus-python.h"), os.F_OK):
            if d not in dbusincdirs:
                dbusincdirs.append(d)

            break
    else:
        sipconfig.inform("dbus/dbus-python.h could not be found and so the "
                         "DBus support module will be disabled. If "
                         "dbus-python v0.80 or later is installed then use "
                         "the -s argument to explicitly specify the directory "
                         "containing dbus/dbus-python.h.")
        pydbusmoddir = None


def check_module(mname, incfile, test, extra_include_dirs=None, extra_lib_dirs=None, \
extra_libs=None):  """See if a module can be built and, if so, add it to the global \
list of  modules.

    mname is the name of the module.
    incfile is the name of the include file needed for the test.
    test is a C++ statement being used for the test.
    extra_include_dirs is an optional list of extra include directories.
    extra_lib_dirs is an optional list of extra library directories.
    extra_libs is an optional list of extra libraries.
    """
    # Check that the module is enabled if we are not automatically enabling all
    # modules.
    if len(opts.enabled) > 0 and mname not in opts.enabled:
        return

    # Check the module's main .sip file exists.
    if os.access(os.path.join(src_dir, "sip", mname, mname + "mod.sip"), os.F_OK):
        sipconfig.inform("Checking to see if the %s module should be built..." % \
mname)

        if check_api(incfile, test, mname, extra_include_dirs=extra_include_dirs, \
extra_lib_dirs=extra_lib_dirs, extra_libs=extra_libs):  pyqt_modules.append(mname)


def check_api(incfile, test, mname, extra_include_dirs=None, extra_lib_dirs=None, \
extra_libs=None):  """Return non-zero if a class is available.

    incfile is the name of the include file needed for the test.
    test is a C++ statement being used for the test.
    mname is the name of the module.
    extra_include_dirs is an optional list of extra include directories.
    extra_lib_dirs is an optional list of extra library directories.
    extra_libs is an optional list of extra libraries.
    """
    # We use a module specific name to avoid a potential problem on Windows
    # where the operating system doesn't delete previous tests quickly enough.
    cfgtest = "cfgtest_%s.cpp" % mname

    f = open(cfgtest, "w")

    f.write("""#include <%s>

int main(int, char **)
{
    %s;
}
""" % (incfile, test))

    f.close()

    return compile_qt_program(cfgtest, mname,
            extra_include_dirs=extra_include_dirs,
            extra_lib_dirs=extra_lib_dirs, extra_libs=extra_libs)


def set_sip_flags(pyqt):
    """Set the SIP platform, version and feature flags.

    pyqt is the configuration instance.
    """
    # If we don't check for signed interpreters, we exclude the 'VendorID'
    # feature
    if not opts.vendorcheck:
        qt_sip_flags.append("-x")
        qt_sip_flags.append("VendorID")

    if not opts.with_deprecated:
        qt_sip_flags.append("-x")
        qt_sip_flags.append("PyQt_Deprecated_5_0")

    # Handle the platform tag.
    if sys.platform == 'win32':
        plattag = "WS_WIN"
    elif sys.platform == "darwin":
        if "__USE_WS_X11__" in sipcfg.build_macros()["DEFINES"]:
            plattag = "WS_X11"
        else:
            plattag = "WS_MACX"
    else:
        plattag = "WS_X11"

    qt_sip_flags.append("-t")
    qt_sip_flags.append(plattag)

    # Handle the Qt version tag.
    verstag = sipconfig.version_to_sip_tag(qt_version, pyqt.qt_version_tags(), "Qt")

    # Handle any feature flags.
    for xf in qt_xfeatures:
        qt_sip_flags.append("-x")
        qt_sip_flags.append(xf)

    if verstag:
        qt_sip_flags.append("-t")
        qt_sip_flags.append(verstag)

    # Handle the version specific Python features.
    if sipcfg.py_version < 0x020400:
        qt_sip_flags.append("-x")
        qt_sip_flags.append("Py_DateTime")

    if sipcfg.py_version < 0x030000:
        qt_sip_flags.append("-x")
        qt_sip_flags.append("Py_v3")

    # There is an issue creating QObjects while the GIL is held causing
    # deadlocks in multi-threaded applications.  We don't fully understand this
    # yet so we make sure we avoid the problem by always releasing the GIL.
    qt_sip_flags.append("-g")


def needed_qt_libs(mname, qt_libs):
    """Add any additional Qt libraries needed by a module to an existing list.

    mname is the name of the module.
    qt_libs is the current list of libraries.
    """

    # The dependencies between the different Qt libraries.  The order within
    # each list is important.  Note that this affects the include directories
    # as well as the libraries.
    LIB_DEPS = {
        "QtCore": [],
        "QtDBus": ["QtCore"],
        "QtDeclarative": ["QtNetwork", "QtGui"],
        "QtGui": ["QtCore"],
        "QtHelp": ["QtGui"],
        "QtMultimedia": ["QtGui"],
        "QtNetwork": ["QtCore"],
        "QtOpenGL": ["QtGui"],
        "QtScript": ["QtCore"],
        "QtScriptTools": ["QtScript", "QtGui", "QtCore"],
        "QtSql": ["QtGui"],
        "QtSvg": ["QtGui"],
        "QtTest": ["QtGui"],
        "QtWebKit": ["QtNetwork", "QtGui"],
        "QtXml": ["QtCore"],
        "QtXmlPatterns": ["QtNetwork", "QtCore"],
        "phonon": ["QtGui"],
        "QtAssistant": ["QtNetwork", "QtGui"],
        "QtDesigner": ["QtGui"],
        "QAxContainer": ["QtGui"]
    }

    # Handle the dependencies first.
    for d in LIB_DEPS[mname]:
        needed_qt_libs(d, qt_libs)

    if mname not in qt_libs:
        qt_libs.insert(0, mname)


def mk_clean_dir(name, clean=1):
    """Create a clean (ie. empty) directory.

    name is the name of the directory.
    """
    if clean:
        try:
            shutil.rmtree(name)
        except:
            pass

    try:
        os.makedirs(name)
    except:
        if clean:
            sipconfig.error("Unable to create the %s directory." % name)


def generate_code(mname, extra_include_dirs=None, extra_lib_dirs=None, \
extra_libs=None, extra_sip_flags=None):  """Generate the code for a module.

    mname is the name of the module to generate the code for.
    extra_include_dirs is an optional list of additional directories to add to
    the list of include directories.
    extra_lib_dirs is an optional list of additional directories to add to the
    list of library directories.
    extra_libs is an optional list of additional libraries to add to the list
    of libraries.
    extra_sip_flags is an optional list of additional flags to pass to SIP.
    """
    sipconfig.inform("Generating the C++ source for the %s module..." % mname)

    mk_clean_dir(mname)

    # Work out what Qt libraries need to be linked against and how SIP is
    # supposed to handle the consolidated module and its components.
    cons_args = []

    if opts.bigqt:
        if mname == "_qt":
            qt_libs = []

            for m in pyqt_modules:
                needed_qt_libs(m, qt_libs)
        else:
            if mname != "Qt":
                cons_args.append("-p")
                cons_args.append("PyQt4._qt")

            qt_libs = 0
    else:
        if mname == "Qt":
            qt_libs = 0
        else:
            qt_libs = []
            needed_qt_libs(mname, qt_libs)

    # Build the SIP command line.
    argv = ['"' + sipcfg.sip_bin + '"', '-w']

    if opts.no_timestamp:
        argv.append("-T")

    if not opts.no_docstrings:
        argv.append("-o");

    if opts.prot_is_public:
        argv.append("-P");

    argv.extend(qt_sip_flags)
    argv.extend(cons_args)

    if extra_sip_flags:
        argv.extend(extra_sip_flags)

    if opts.concat:
        argv.append("-j")
        argv.append(str(opts.split))

    if opts.tracing:
        argv.append("-r")

    if mname not in ("Qt", "_qt", "Qsci"):
        argv.append("-a")
        argv.append(mname + ".api")

    # Pass the absolute pathname so that #line files are absolute.
    argv.append("-c")
    argv.append(os.path.abspath(mname))

    buildfile = os.path.join(mname, mname + ".sbf")
    argv.append("-b")
    argv.append(buildfile)

    argv.append("-I")
    argv.append(os.path.join(src_dir, "sip"))

    # Add the name of the .sip file keeping in mind SIP assumes POSIX style
    # path separators.  The Qt module's .sip file is generated by this script
    # and so will be in a different place if this is an out-of-tree build.
    if mname == "Qt":
        argv.append("sip/Qt/Qtmod.sip")
    else:
        drive, path = os.path.splitdrive(src_dir)
        parts = path.split(os.pathsep)
        parts.extend(["sip", mname, mname + "mod.sip"])
        argv.append(drive + "/".join(parts))

    cmd = " ".join(argv)

    if opts.verbose:
        sys.stdout.write(cmd + "\n")

    os.system(cmd)

    # Check the result.
    if not os.access(buildfile, os.F_OK):
        sipconfig.error("Unable to create the C++ code.")

    # Generate the Makefile.
    sipconfig.inform("Creating the Makefile for the %s module..." % mname)

    installs = []

    if opts.install_sipfiles:
        sipfiles = []

        sipdir = os.path.join("sip", mname)
        if mname != "Qt":
            sipdir = os.path.join(src_dir, sipdir)
            rel_sipdir = sipdir
        else:
            rel_sipdir = os.path.join("..", sipdir)

        for s in glob.glob(os.path.join(sipdir, "*.sip")):
            sipfiles.append(os.path.join(rel_sipdir, os.path.basename(s)))

        installs.append([sipfiles, os.path.join(opts.pyqtsipdir, mname)])

    opengl = (mname == "QtOpenGL")

    makefile = sipconfig.SIPModuleMakefile(
        configuration=sipcfg,
        build_file=mname + ".sbf",
        dir=mname,
        install_dir=pyqt_modroot,
        installs=installs,
        qt=qt_libs,
        opengl=opengl,
        warnings=1,
        static=opts.static,
        debug=opts.debug,
        universal=sipcfg.universal,
        arch=sipcfg.arch,
        prot_is_public=opts.prot_is_public,
        deployment_target=sipcfg.deployment_target
    )

    add_makefile_extras(makefile, extra_include_dirs, extra_lib_dirs, extra_libs)

    if qt_version >= 0x050000 and opts.with_deprecated:
        # The name of this macro is very confusing.
        makefile.extra_defines.append('QT_DISABLE_DEPRECATED_BEFORE=0x040900')

    makefile.generate()


def fix_license(name):
    """ Fix the license file, if there is one, so that it conforms to the SIP
    v5 syntax.
    """

    try:
        f = open(name, "r")
    except IOError:
        sipconfig.error("Failed to open license file %s." % name)

    f5 = open(name + "5", "w")

    for line in f:
        if line.startswith("%License"):
            anno_start = line.find("/")
            anno_end = line.rfind("/")

            if anno_start < 0 or anno_end < 0 or anno_start == anno_end:
                sipconfig.error("%s has missing annotations." % name)

            annos = line[anno_start + 1:anno_end].split(", ")
            annos5 = [anno[0].lower() + anno[1:] for anno in annos]

            f5.write("%License(")
            f5.write(", ".join(annos5))
            f5.write(")\n")
        else:
            f5.write(line)

    f5.close()
    f.close()


def check_license():
    """Handle the validation of the PyQt license.
    """
    try:
        import license
        ltype = license.LicenseType
        lname = license.LicenseName

        try:
            lfile = license.LicenseFile
        except AttributeError:
            lfile = None
    except ImportError:
        ltype = None

    if ltype is None:
        ltype = "GPL"
        lname = "GNU General Public License"
        lfile = "pyqt-gpl.sip"

    sipconfig.inform("This is the %s version of PyQt %s (licensed under the %s) for \
Python %s on %s." % (ltype, pyqt_version_str, lname, sys.version.split()[0], \
sys.platform))

    # Common checks.
    if qt_licensee and ltype == "GPL":
        sipconfig.error("This version of PyQt and the commercial version of Qt have \
incompatible licenses.")

    # Confirm the license if not already done.
    if not opts.license_confirmed:
        if ltype == "GPL":
            loptions = """
Type '2' to view the GPL v2 license.
Type '3' to view the GPL v3 license.
"""
        else:
            loptions = """
Type 'L' to view the license.
"""

        sys.stdout.write(loptions)
        sys.stdout.write("""Type 'yes' to accept the terms of the license.
Type 'no' to decline the terms of the license.

""")

        while 1:
            sys.stdout.write("Do you accept the terms of the license? ")
            sys.stdout.flush()

            try:
                resp = sys.stdin.readline()
            except KeyboardInterrupt:
                raise SystemExit
            except:
                resp = ""

            resp = resp.strip().lower()

            if resp == "yes":
                break

            if resp == "no":
                sys.exit(0)

            if ltype == "GPL":
                if resp == "2":
                    os.system("more LICENSE.GPL2")
                elif resp == "3":
                    os.system("more LICENSE.GPL3")
            else:
                if resp == "l":
                    os.system("more LICENSE")

    # Check that the license file exists and fix its syntax.
    lfile_path = os.path.join(src_dir, "sip", lfile)

    if os.access(lfile_path, os.F_OK):
        sipconfig.inform("Found the license file %s." % lfile)
        fix_license(lfile_path)
    else:
        sipconfig.error("Please copy the license file %s to the sip directory." % \
lfile)


def get_build_macros(overrides):
    """Return the dictionary of platform specific build macros from the Qt
    installation.  Return None if any of the overrides was invalid.

    overrides is a list of macros overrides from the user.
    """
    global qt_macx_spec

    # Get the name of the qmake configuration file to take the macros from.
    if "QMAKESPEC" in list(os.environ.keys()):
        fname = os.environ["QMAKESPEC"]

        if not os.path.dirname(fname):
            qt_macx_spec = fname
            fname = os.path.join(qt_datadir, "mkspecs", fname)
    elif sys.platform == "darwin":
        # The Qt Mac binary installer defaults to xcode which we don't want.
        # Use Qt5's macx-clang if it is available, otherwise fall back to
        # macx-g++.
        fname = os.path.join(qt_datadir, "mkspecs", "macx-clang")
        if os.path.isdir(fname):
            qt_macx_spec = "macx-clang"
        else:
            fname = os.path.join(qt_datadir, "mkspecs", "macx-g++")
            qt_macx_spec = "macx-g++"
    else:
        fname = os.path.join(qt_datadir, "mkspecs", "default")

        # Qt5 doesn't have the 'default' link.
        if not os.path.isdir(fname):
            f = get_command_stdout(opts.qmake + " -query QMAKE_SPEC")
            fname = f.read().strip().decode()
            f.close()

            fname = os.path.join(qt_datadir, "mkspecs", fname)

    fname = os.path.join(fname, "qmake.conf")

    if not os.access(fname, os.F_OK):
        sipconfig.error("Unable to find the qmake configuration file %s. Use the \
QMAKESPEC environment variable to specify the correct platform." % fname)

    # Add the Qt specific macros to the default.
    names = list(sipcfg.build_macros().keys())
    names.append("INCDIR_QT")
    names.append("LIBDIR_QT")
    names.append("MOC")

    properties = {
        "QT_INSTALL_BINS":      qt_bindir,
        "QT_INSTALL_HEADERS":   qt_incdir,
        "QT_INSTALL_LIBS":      qt_libdir
    }

    macros = sipconfig.parse_build_macros(fname, names, overrides, properties)

    if macros is None:
        return None

    # Qt5 doesn't seem to support the specific macros so add them if they are
    # missing.
    if macros.get("INCDIR_QT", "") == "":
        macros["INCDIR_QT"] = qt_incdir

    if macros.get("LIBDIR_QT", "") == "":
        macros["LIBDIR_QT"] = qt_libdir

    if macros.get("MOC", "") == "":
        default_moc = os.path.join(qt_bindir, "moc")
        if sys.platform == 'win32':
            default_moc += ".exe"

        macros["MOC"] = default_moc

    return macros


def check_qt_installation(macros):
    """Check the Qt installation and get the version number and edition and
    return the configuration instance.

    macros is the dictionary of build macros.
    """
    # Check the Qt version number.
    if qt_version < 0x040100:
        sipconfig.error("PyQt v4 requires Qt v4.1.0 or later.")

    # Starting with v4.7, Qt (when built with MinGW) assumes that stack frames
    # are 16 byte aligned because it uses SSE.  However the Python Windows
    # installers are built with 4 byte aligned stack frames.  We therefore need
    # to tweak the g++ flags to deal with it.
    if sipcfg.platform == 'win32-g++' and qt_version >= 0x040700:
        macros['CFLAGS'] += ' -mstackrealign'
        macros['CXXFLAGS'] += ' -mstackrealign'

    # Work out how Qt was built on MacOS.
    if sys.platform == "darwin":
        if os.access(os.path.join(qt_libdir, "QtCore.framework"), os.F_OK):
            global qt_framework
            qt_framework = 1

    # Get the Makefile generator.
    generator = macros["MAKEFILE_GENERATOR"]

    # We haven't yet factored out sipconfig's knowledge of how to build Qt
    # binaries and it is expecting to find these in the configuration when it
    # generates the Makefiles.
    sipcfg.qt_version = qt_version
    sipcfg.qt_edition = qt_edition
    sipcfg.qt_winconfig = qt_shared
    sipcfg.qt_framework = qt_framework
    sipcfg.qt_threaded = 1
    sipcfg.qt_dir = qt_dir
    sipcfg.qt_lib_dir = qt_libdir

    return ConfigurePyQt4(generator)


def fix_qmake_args(args=""):
    """Make any platform specific adjustments to the arguments passed to qmake.
    """
    if sys.platform == "darwin":
        # The Qt binary installer has macx-xcode as the default.
        args = "-spec %s %s" % (qt_macx_spec, args)

    return args


def get_qt_configuration():
    """Set the qt_dir, qt_incdir, qt_libdir, qt_bindir, qt_datadir,
    qt_pluginsdir and qt_xfeatures globals for the Qt installation.
    """
    sipconfig.inform("Determining the layout of your Qt installation...")

    # The file names we will use to get the directory information.
    app = "qtdirs"
    pro_file = app + ".pro"
    make_file = app + ".mk"
    make_target = ""
    cpp_file = app + ".cpp"
    out_file = app + ".out"
    qmake_args = fix_qmake_args("-o " + make_file)

    if sys.platform == 'win32':
        if opts.debug:
            exe_file = os.path.join("debug", app + ".exe")
            make_target = " debug"
        else:
            exe_file = os.path.join("release", app + ".exe")
            make_target = " release"
    elif sys.platform == "darwin":
        exe_file = os.path.join(app + ".app", "Contents", "MacOS", app)
    else:
        exe_file = os.path.join(".", app)

    # Generate the qmake project file.
    f = open(pro_file, "w")

    if sipcfg.arch:
        f.write(arch_config())

    f.write(
"""QT = core
# This is for certain broken Linux distros and is needed to make sure that
# QT_SHARED is properly defined.
CONFIG += link_prl
TARGET = %s
SOURCES = %s
""" % (app, cpp_file))

    f.close()

    # Generate the source code.
    f = open(cpp_file, "w")

    f.write(
"""#include <QCoreApplication>
#include <QFile>
#include <QLibraryInfo>
#include <QTextStream>

// These seem to be missing from the Qt v5 beta.
#if !defined(QT_EDITION_DESKTOP)
#define QT_EDITION_DESKTOP      8
#endif
#if !defined(QT_EDITION_OPENSOURCE)
#define QT_EDITION_OPENSOURCE   8
#endif

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    QFile outf("%s");

    if (!outf.open(QIODevice::WriteOnly|QIODevice::Truncate|QIODevice::Text))
        return 1;

    QTextStream out(&outf);

    out << QLibraryInfo::location(QLibraryInfo::PrefixPath) << '\\n';
    out << QLibraryInfo::location(QLibraryInfo::HeadersPath) << '\\n';
    out << QLibraryInfo::location(QLibraryInfo::LibrariesPath) << '\\n';
    out << QLibraryInfo::location(QLibraryInfo::BinariesPath) << '\\n';
    out << QLibraryInfo::location(QLibraryInfo::DataPath) << '\\n';
    out << QLibraryInfo::location(QLibraryInfo::PluginsPath) << '\\n';

    out << QT_VERSION << '\\n';
    out << QT_EDITION << '\\n';

    out << QLibraryInfo::licensee() << '\\n';

#if defined(QT_SHARED) || defined(QT_DLL)
    out << "shared\\n";
#else
    out << "\\n";
#endif

    // Determine which features should be disabled.

#if defined(QT_NO_ACCESSIBILITY)
    out << "PyQt_Accessibility\\n";
#endif

#if defined(QT_NO_SESSIONMANAGER)
    out << "PyQt_SessionManager\\n";
#endif

#if defined(QT_NO_STATUSTIP)
    out << "PyQt_StatusTip\\n";
#endif

#if defined(QT_NO_TOOLTIP)
    out << "PyQt_ToolTip\\n";
#endif

#if defined(QT_NO_WHATSTHIS)
    out << "PyQt_WhatsThis\\n";
#endif

#if defined(QT_NO_OPENSSL)
    out << "PyQt_OpenSSL\\n";
#endif

#if defined(QT_NO_SIZEGRIP)
    out << "PyQt_SizeGrip\\n";
#endif

#if defined(QT_NO_SYSTEMTRAYICON)
    out << "PyQt_SystemTrayIcon\\n";
#endif

#if defined(QT_NO_PRINTDIALOG)
    out << "PyQt_PrintDialog\\n";
#endif

#if defined(QT_NO_PRINTER)
    out << "PyQt_Printer\\n";
#endif

#if defined(QT_NO_PRINTPREVIEWDIALOG)
    out << "PyQt_PrintPreviewDialog\\n";
#endif

#if defined(QT_NO_PRINTPREVIEWWIDGET)
    out << "PyQt_PrintPreviewWidget\\n";
#endif

#if defined(QT_NO_RAWFONT)
    out << "PyQt_RawFont\\n";
#endif

#if !defined(QT3_SUPPORT) || QT_VERSION >= 0x040200
    out << "PyQt_NoPrintRangeBug\\n";
#endif

#if defined(QT_OPENGL_ES)
    out << "PyQt_NoOpenGLES\\n";
#endif

    if (sizeof (qreal) != sizeof (double))
        out << "PyQt_qreal_double\\n";

    return 0;
}
""" % out_file)

    f.close()

    # Create the makefile, first making sure it doesn't already exist.
    remove_file(make_file)
    run_command("%s %s %s" % (opts.qmake, qmake_args, pro_file))

    if not os.access(make_file, os.F_OK):
        sipconfig.error("%s failed to create a makefile. %s" % (opts.qmake, \
MSG_CHECK_QMAKE))

    # Try and work out the name of make.
    if sipcfg.platform.startswith("win32-msvc"):
        make = "nmake"
    elif sipcfg.platform == "win32-borland":
        make = "bmake"
    elif sipcfg.platform == "win32-g++":
        make = "mingw32-make"
    else:
        make = "make"

    # Create the executable, first making sure it doesn't exist.
    remove_file(exe_file)
    run_command("%s -f %s%s" % (make, make_file, make_target))

    if not os.access(exe_file, os.X_OK):
        sipconfig.error("Failed to determine the layout of your Qt installation. Try \
again using the --verbose flag to see more detail about the problem.")

    # Create the output file, first making sure it doesn't exist.
    remove_file(out_file)
    run_command(exe_file)

    if not os.access(out_file, os.F_OK):
        sipconfig.error("%s failed to create %s. Make sure your Qt installation is \
correct." % (exe_file, out_file))

    # Read the directories.
    f = open(out_file, "r")
    lines = f.read().strip().split("\n")
    f.close()

    global qt_dir, qt_incdir, qt_libdir, qt_bindir, qt_datadir, qt_pluginsdir
    global qt_version, qt_edition, qt_licensee, qt_shared, qt_xfeatures

    qt_dir = lines[0]
    qt_incdir = lines[1]
    qt_libdir = lines[2]
    qt_bindir = lines[3]
    qt_datadir = lines[4]
    qt_pluginsdir = lines[5]
    qt_version = lines[6]
    qt_edition = lines[7]
    qt_licensee = lines[8]
    qt_shared = lines[9]
    qt_xfeatures = lines[10:]

    if opts.assume_shared:
        qt_shared = "shared"

    # 'Nokia' is the value that is used by Maemo's version of Qt.
    if qt_licensee in ('Open Source', 'Nokia'):
        qt_licensee = None

    try:
        qt_version = int(qt_version)
    except ValueError:
        sipconfig.error("QT_VERSION should be a number but \"%s\" was found." % \
qt_version)

    try:
        qt_edition = int(qt_edition)
    except ValueError:
        sipconfig.error("QT_EDITION should be a number but \"%s\" was found." % \
qt_edition)

    # Now convert the edition to a descriptive string.  The order of testing is
    # important.
    if qt_edition & 0x200:
        # It has ActiveQt.
        qt_edition = "Desktop"

        # ActiveQt became part of the open source version in v4.5.2.
        if qt_version >= 0x040502 and qt_licensee is None:
            qt_edition = "free"
    elif qt_edition & 0x008:
        # It has OpenGL.
        qt_edition = "free"
    elif qt_edition & 0x002:
        # It has GUI.
        qt_edition = "Desktop Light"
    else:
        qt_edition = "Console"

    if qt_shared:
        if opts.staticplugins:
            sipconfig.inform("Static plugins are disabled because Qt has been built \
as shared libraries.")  opts.staticplugins = []
    else:
        if opts.static or opts.bigqt:
            pass
        else:
            sipconfig.error("Qt has been built as static libraries so either the -g \
or -k argument should be used.")


def main():
    """Create the configuration module module.
    """
    # Check SIP is new enough.
    if sipcfg.sip_version < sip_min_version:
        sipconfig.error("This version of PyQt requires SIP v%s or later" % \
sipconfig.version_to_string(sip_min_version))

    global opts

    # Parse the command line.
    p = create_optparser()
    opts, args = p.parse_args()

    # Provide defaults for platform-specific options.
    if sys.platform == 'win32':
        opts.qmake = find_default_qmake()
        opts.prot_is_public = False

    if not opts.qmake:
        sipconfig.error(MSG_CHECK_QMAKE)

    # Where the modules will be placed.
    global pyqt_modroot
    pyqt_modroot = os.path.join(opts.pyqtmoddir, "PyQt4")

    # When building static libraries, signed interpreter checking makes no
    # sense.
    if opts.vendorcheck and opts.static:
        sipconfig.error("Using the VendorID package when building static libraries \
makes no sense.")

    # Get the details of the Qt installation.
    get_qt_configuration()

    # Provide some defaults that are based on the Qt configuration.
    if not opts.plugindir:
        opts.plugindir = qt_pluginsdir

    if opts.qscidir:
        # An explicit directory implies installing the API file.
        opts.api = True
    else:
        opts.qscidir = os.path.join(qt_datadir, "qsci")

        if opts.api is None:
            # Install the API file if the default directory exists.
            opts.api = os.path.isdir(opts.qscidir)

    # Replace the existing build macros with the ones from the Qt installation.
    macros = get_build_macros(args)

    if macros is None:
        p.print_help()
        sys.exit(2)

    sipcfg.set_build_macros(macros)

    # Check Qt is what we need.
    pyqt = check_qt_installation(macros)

    # Check the licenses are compatible.
    check_license()

    # Check which modules to build.
    pyqt.check_modules()

    # Check for the VendorID package.
    check_vendorid()

    # Set the SIP platform, version and feature flags.
    set_sip_flags(pyqt)

    # Tell the user what's been found.
    inform_user()

    # Generate the code.
    pyqt.code()

    # Create the additional Makefiles.
    sipconfig.inform("Creating top level Makefile...")

    installs=[(pyqt.module_installs(), pyqt_modroot)]

    if opts.api:
        installs.append(("PyQt4.api", os.path.join(opts.qscidir, "api", "python")))

    xtra_modules = ["Qt"]

    if opts.bigqt:
        xtra_modules.append("_qt")

        if opts.mwg_qsci_dir:
            xtra_modules.append("Qsci")

        if opts.mwg_qwt_dir:
            xtra_modules.append("Qwt5")

    sipconfig.ParentMakefile(
        configuration=sipcfg,
        subdirs=pyqt.qpy_libs() + pyqt_modules + xtra_modules + pyqt.tools(),
        installs=installs
    ).generate()

    # Install the configuration module.
    create_config("pyqtconfig.py", os.path.join(src_dir, "pyqtconfig.py.in"),
            macros)


###############################################################################
# The script starts here.
###############################################################################

if __name__ == "__main__":
    try:
        main()
    except SystemExit:
        raise
    except:
        sys.stderr.write(
"""An internal error occured.  Please report all the output from the program,
including the following traceback, to support@riverbankcomputing.com.
""")
        raise



_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic