sip_configure.py
Go to the documentation of this file.
1 from copy import copy
2 from distutils.spawn import find_executable
3 import os
4 import re
5 import subprocess
6 import sys
7 
8 import sipconfig
9 from PyQt5 import QtCore
10 
11 
12 class Configuration(sipconfig.Configuration):
13 
14  def __init__(self):
15  env = copy(os.environ)
16  env['QT_SELECT'] = '5'
17  qmake_exe = 'qmake-qt5' if find_executable('qmake-qt5') else 'qmake'
18  qtconfig = subprocess.check_output(
19  [qmake_exe, '-query'], env=env, universal_newlines=True)
20  qtconfig = dict(line.split(':', 1) for line in qtconfig.splitlines())
21  pyqtconfig = {
22  'qt_archdata_dir': qtconfig['QT_INSTALL_DATA'],
23  'qt_data_dir': qtconfig['QT_INSTALL_DATA'],
24  'qt_dir': qtconfig['QT_INSTALL_PREFIX'],
25  'qt_inc_dir': qtconfig['QT_INSTALL_HEADERS'],
26  'qt_lib_dir': qtconfig['QT_INSTALL_LIBS'],
27  'qt_threaded': 1,
28  'qt_version': QtCore.QT_VERSION,
29  'qt_winconfig': 'shared exceptions',
30  }
31  if sys.platform == 'darwin':
32  pyqtconfig['qt_framework'] = 1
33  sipconfig.Configuration.__init__(self, [pyqtconfig])
34 
35  macros = sipconfig._default_macros.copy()
36  macros['INCDIR_QT'] = qtconfig['QT_INSTALL_HEADERS']
37  macros['LIBDIR_QT'] = qtconfig['QT_INSTALL_LIBS']
38  macros['MOC'] = 'moc-qt5'
39  self.set_build_macros(macros)
40 
41 
42 if len(sys.argv) != 8:
43  print('usage: %s build-dir sip-file output_dir include_dirs libs lib_dirs ldflags' %
44  sys.argv[0])
45  sys.exit(1)
46 
47 # The SIP build folder, the SIP file, the output directory, the include
48 # directories, the libraries, the library directories and the linker
49 # flags.
50 build_dir, sip_file, output_dir, include_dirs, libs, lib_dirs, ldflags = sys.argv[1:]
51 
52 # The name of the SIP build file generated by SIP and used by the build system.
53 build_file = 'pyqtscripting.sbf'
54 
55 # Get the PyQt configuration information.
56 config = Configuration()
57 
58 # Get the extra SIP flags needed by the imported qt module. Note that
59 # this normally only includes those flags (-x and -t) that relate to SIP's
60 # versioning system.
61 try:
62  sip_dir = config.pyqt_sip_dir
63  sip_flags = config.pyqt_sip_flags
64 except AttributeError:
65  # sipconfig.Configuration does not have a pyqt_sip_dir or pyqt_sip_flags attribute
66  sip_dir = os.path.join(sipconfig._pkg_config['default_sip_dir'], 'PyQt5')
67  sip_flags = QtCore.PYQT_CONFIGURATION['sip_flags']
68 
69 try:
70  os.makedirs(build_dir)
71 except OSError:
72  pass
73 
74 # Run SIP to generate the code. Note that we tell SIP where to find the qt
75 # module's specification files using the -I flag.
76 
77 sip_bin = config.sip_bin
78 # Without the .exe, this might actually be a directory in Windows
79 if sys.platform == 'win32' and os.path.isdir(sip_bin):
80  sip_bin += '.exe'
81 
82 cmd = [
83  sip_bin,
84  '-c', build_dir,
85  '-b', os.path.join(build_dir, build_file),
86  '-I', sip_dir,
87  '-w'
88 ]
89 cmd += sip_flags.split(' ')
90 cmd.append(sip_file)
91 subprocess.check_call(cmd)
92 
93 # Create the Makefile. The QtModuleMakefile class provided by the
94 # pyqtconfig module takes care of all the extra preprocessor, compiler and
95 # linker flags needed by the Qt library.
96 makefile = sipconfig.SIPModuleMakefile(
97  dir=build_dir,
98  configuration=config,
99  build_file=build_file,
100  qt=['QtCore', 'QtGui']
101 )
102 
103 # hack to override makefile behavior which always prepend -l to libraries
104 # which is wrong for absolute paths
105 default_platform_lib_function = sipconfig.SIPModuleMakefile.platform_lib
106 
107 
108 def custom_platform_lib_function(self, clib, framework=0):
109  # Only add '-l' if a library doesn't already start with '-l' and is not an absolute path
110  if os.path.isabs(clib) or clib.startswith('-l'):
111  return clib
112  return default_platform_lib_function(self, clib, framework)
113 
114 
115 sipconfig.SIPModuleMakefile.platform_lib = custom_platform_lib_function
116 
117 
118 # split paths on whitespace
119 # while dealing with whitespaces within the paths if they are escaped with backslashes
120 def split_paths(paths):
121  paths = re.split('(?<=[^\\\\]) ', paths)
122  return paths
123 
124 
125 for include_dir in split_paths(include_dirs):
126  include_dir = include_dir.replace('\\', '')
127  makefile.extra_include_dirs.append(include_dir)
128 for lib in split_paths(libs):
129  makefile.extra_libs.append(lib)
130 for lib_dir in split_paths(lib_dirs):
131  lib_dir = lib_dir.replace('\\', '')
132  makefile.extra_lib_dirs.append(lib_dir)
133 for ldflag in ldflags.split('\\ '):
134  makefile.LFLAGS.append(ldflag)
135 
136 # redirect location of generated library
137 makefile._target = '"%s"' % os.path.join(output_dir, makefile._target)
138 
139 # Force c++14
140 if sys.platform == 'win32':
141  makefile.extra_cxxflags.append('/std:c++14')
142  # The __cplusplus flag is not properly set on Windows for backwards
143  # compatibilty. This flag sets it correctly
144  makefile.CXXFLAGS.append('/Zc:__cplusplus')
145 else:
146  makefile.extra_cxxflags.append('-std=c++14')
147 
148 # Generate the Makefile itself
149 makefile.generate()
def custom_platform_lib_function(self, clib, framework=0)
def split_paths(paths)


python_qt_binding
Author(s): Dave Hershberger, Dorian Scholz, Dirk Thomas
autogenerated on Sat Mar 16 2019 03:01:22