]> git.lyx.org Git - lyx.git/blob - development/scons/qt4.py
Add visible space. After long discussion the solution is part of InsetSpace.
[lyx.git] / development / scons / qt4.py
1 import re
2 import os.path
3
4 import SCons.Defaults
5 import SCons.Tool
6 import SCons.Util
7
8
9 class ToolQtWarning(SCons.Warnings.Warning):
10         pass
11 class GeneratedMocFileNotIncluded(ToolQtWarning):
12         pass
13 class QtdirNotFound(ToolQtWarning):
14         pass
15 SCons.Warnings.enableWarningClass(ToolQtWarning)
16
17 qrcinclude_re = re.compile(r'<file>([^<]*)</file>', re.M)
18
19
20 header_extensions = [".h", ".hxx", ".hpp", ".hh"]
21 if SCons.Util.case_sensitive_suffixes('.h', '.H'):
22         header_extensions.append('.H')
23 #cplusplus = __import__('c++', globals(), locals(), ['Scons.Tools'])
24 #cxx_suffixes = cplusplus.CXXSuffixes
25 cxx_suffixes = [".C", ".c", ".cxx", ".cpp", ".cc"]
26
27 def _checkMocIncluded(target, source, env):
28         moc = target[0]
29         cpp = source[0]
30         # looks like cpp.includes is cleared before the build stage :-(
31         # not really sure about the path transformations (moc.cwd? cpp.cwd?) :-/
32         path = SCons.Defaults.CScan.path_function(env, moc.cwd)
33         includes = SCons.Defaults.CScan(cpp, env, path)
34         if not moc in includes:
35                 SCons.Warnings.warn(
36                         GeneratedMocFileNotIncluded,
37                         "Generated moc file '%s' is not included by '%s'" %
38                         (str(moc), str(cpp)))
39
40 def _find_file(filename, paths, node_factory):
41         retval = None
42         for dir in paths:
43                 node = node_factory(filename, dir)
44                 if node.rexists():
45                         return node
46         return None
47
48 class _Automoc:
49         """
50         Callable class, which works as an emitter for Programs, SharedLibraries and
51         StaticLibraries.
52         """
53
54         def __init__(self, objBuilderName):
55                 self.objBuilderName = objBuilderName
56                 
57         def __call__(self, target, source, env):
58                 """
59                 Smart autoscan function. Gets the list of objects for the Program
60                 or Lib. Adds objects and builders for the special qt files.
61                 """
62                 try:
63                         if int(env.subst('$QT_AUTOSCAN')) == 0:
64                                 return target, source
65                 except ValueError:
66                         pass
67                 try:
68                         debug = int(env.subst('$QT_DEBUG'))
69                 except ValueError:
70                         debug = 0
71                 
72                 # some shortcuts used in the scanner
73                 FS = SCons.Node.FS.default_fs
74                 splitext = SCons.Util.splitext
75                 objBuilder = getattr(env, self.objBuilderName)
76
77                 # some regular expressions:
78                 # Q_OBJECT detection
79                 q_object_search = re.compile(r'[^A-Za-z0-9]Q_OBJECT[^A-Za-z0-9]') 
80                 # cxx and c comment 'eater'
81                 #comment = re.compile(r'(//.*)|(/\*(([^*])|(\*[^/]))*\*/)')
82                 # CW: something must be wrong with the regexp. See also bug #998222
83                 #     CURRENTLY THERE IS NO TEST CASE FOR THAT
84                 
85                 # The following is kind of hacky to get builders working properly (FIXME)
86                 objBuilderEnv = objBuilder.env
87                 objBuilder.env = env
88                 mocBuilderEnv = env.Moc4.env
89                 env.Moc4.env = env
90                 
91                 # make a deep copy for the result; MocH objects will be appended
92                 out_sources = source[:]
93
94                 for obj in source:
95                         if not obj.has_builder():
96                                 # binary obj file provided
97                                 if debug:
98                                         print "scons: qt: '%s' seems to be a binary. Discarded." % str(obj)
99                                 continue
100                         cpp = obj.sources[0]
101                         if not splitext(str(cpp))[1] in cxx_suffixes:
102                                 if debug:
103                                         print "scons: qt: '%s' is no cxx file. Discarded." % str(cpp) 
104                                 # c or fortran source
105                                 continue
106                         #cpp_contents = comment.sub('', cpp.get_contents())
107                         cpp_contents = cpp.get_contents()
108                         h=None
109                         for h_ext in header_extensions:
110                                 # try to find the header file in the corresponding source
111                                 # directory
112                                 hname = splitext(cpp.name)[0] + h_ext
113                                 h = _find_file(hname,
114                                                           (cpp.get_dir(),),
115                                                           FS.File)
116                                 if h:
117                                         if debug:
118                                                 print "scons: qt: Scanning '%s' (header of '%s')" % (str(h), str(cpp))
119                                         #h_contents = comment.sub('', h.get_contents())
120                                         h_contents = h.get_contents()
121                                         break
122                         if not h and debug:
123                                 print "scons: qt: no header for '%s'." % (str(cpp))
124                         if h and q_object_search.search(h_contents):
125                                 # h file with the Q_OBJECT macro found -> add moc_cpp
126                                 moc_cpp = env.Moc4(h)
127                                 moc_o = objBuilder(moc_cpp)
128                                 out_sources.append(moc_o)
129                                 #moc_cpp.target_scanner = SCons.Defaults.CScan
130                                 if debug:
131                                         print "scons: qt: found Q_OBJECT macro in '%s', moc'ing to '%s'" % (str(h), str(moc_cpp))
132                         if cpp and q_object_search.search(cpp_contents):
133                                 # cpp file with Q_OBJECT macro found -> add moc
134                                 # (to be included in cpp)
135                                 moc = env.Moc4(cpp)
136                                 env.Ignore(moc, moc)
137                                 if debug:
138                                         print "scons: qt: found Q_OBJECT macro in '%s', moc'ing to '%s'" % (str(cpp), str(moc))
139                                 #moc.source_scanner = SCons.Defaults.CScan
140                 # restore the original env attributes (FIXME)
141                 objBuilder.env = objBuilderEnv
142                 env.Moc4.env = mocBuilderEnv
143
144                 return (target, out_sources)
145
146 AutomocShared = _Automoc('SharedObject')
147 AutomocStatic = _Automoc('StaticObject')
148
149 def _detect(env):
150         """Not really safe, but fast method to detect the QT library"""
151
152         QTDIR = env.get('QTDIR',None)
153         if QTDIR!=None : return QTDIR
154
155         QTDIR = os.environ.get('QTDIR',None)
156         if QTDIR!=None : return QTDIR
157
158         moc = env.WhereIs('moc-qt4') or env.WhereIs('moc')
159         if moc:
160                 SCons.Warnings.warn(
161                         QtdirNotFound,
162                         "QTDIR variable is not defined, using moc executable as a hint (QTDIR=%s)" % QTDIR)
163                 return os.path.dirname(os.path.dirname(moc))
164
165         SCons.Warnings.warn(
166                 QtdirNotFound,
167                 "Could not detect qt, using empty QTDIR")
168         return None
169
170 def generate(env):
171         """Add Builders and construction variables for qt to an Environment."""
172
173         print "Loading qt4 tool..."
174
175         def locateQt4Command(env, command, qtdir) :
176                 fullpath1 = os.path.join(qtdir,'bin',command +'-qt4')
177                 if os.access(fullpath1, os.X_OK) or \
178                         os.access(fullpath1+".exe", os.X_OK):
179                         return fullpath1
180                 fullpath2 = os.path.join(qtdir,'bin',command)
181                 if os.access(fullpath2, os.X_OK) or \
182                         os.access(fullpath2+".exe", os.X_OK):
183                         return fullpath2
184                 fullpath = env.Detect([command+'-qt4', command])
185                 if not (fullpath is None) : return fullpath
186                 raise "Qt4 command '" + command + "' not found. Tried: " + fullpath1 + " and "+ fullpath2
187                 
188
189         CLVar = SCons.Util.CLVar
190         Action = SCons.Action.Action
191         Builder = SCons.Builder.Builder
192         splitext = SCons.Util.splitext
193
194         # the basics
195         env['QTDIR']  = _detect(env)
196         env['QT4_MOC'] = locateQt4Command(env,'moc', env['QTDIR'])
197         env['QT4_UIC'] = locateQt4Command(env,'uic', env['QTDIR'])
198         env['QT4_RCC'] = locateQt4Command(env,'rcc', env['QTDIR'])
199         env['QT4_LUPDATE'] = locateQt4Command(env,'lupdate', env['QTDIR'])
200         env['QT4_LRELEASE'] = locateQt4Command(env,'lrelease', env['QTDIR'])
201
202         # Should the qt tool try to figure out, which sources are to be moc'ed ?
203         env['QT4_AUTOSCAN'] = 1
204
205         # Some QT specific flags. I don't expect someone wants to
206         # manipulate those ...
207         env['QT4_UICDECLFLAGS'] = CLVar('')
208         env['QT4_MOCFROMHFLAGS'] = CLVar('')
209         env['QT4_MOCFROMCXXFLAGS'] = CLVar('-i')
210         env['QT4_QRCFLAGS'] = '-name Resources'
211
212         # suffixes/prefixes for the headers / sources to generate
213         env['QT4_MOCHPREFIX'] = ''
214         env['QT4_MOCHSUFFIX'] = '$CXXFILESUFFIX'
215         env['QT4_MOCCXXPREFIX'] = 'moc_'
216         env['QT4_MOCCXXSUFFIX'] = '.moc'
217         env['QT4_UISUFFIX'] = '.ui'
218         env['QT4_UICDECLPREFIX'] = 'ui_'
219         env['QT4_UICDECLSUFFIX'] = '.h'
220         env['QT4_QRCSUFFIX'] = '.qrc',
221         env['QT4_QRCCXXSUFFIX'] = '$CXXFILESUFFIX'
222         env['QT4_QRCCXXPREFIX'] = 'qrc_'
223
224         env['QT4_LIB'] = '' # KLUDGE to avoid linking qt3 library
225
226         # Translation builder
227         tsbuilder = Builder(
228                 action ='$QT4_LUPDATE $SOURCES -ts $TARGETS',
229                 multi=1
230                 )
231         env.Append( BUILDERS = { 'Ts': tsbuilder } )
232         qmbuilder = Builder(
233                 action =[
234                         '$QT4_LRELEASE $SOURCE',
235                         ],
236                 src_suffix = '.ts',
237                 suffix = '.qm',
238                 single_source = True
239                 )
240         env.Append( BUILDERS = { 'Qm': qmbuilder } )
241
242         # Resource builder
243         def scanResources(node, env, path, arg):
244                 contents = node.get_contents()
245                 includes = qrcinclude_re.findall(contents)
246                 return includes
247         qrcscanner = env.Scanner(name = 'qrcfile',
248                 function = scanResources,
249                 argument = None,
250                 skeys = ['.qrc'])
251         qrcbuilder = Builder(
252                 action ='$QT4_RCC $QT4_QRCFLAGS $SOURCE -o $TARGET',
253                 source_scanner = qrcscanner,
254                 src_suffix = '$QT4_QRCSUFFIX',
255                 suffix = '$QT4_QRCCXXSUFFIX',
256                 prefix = '$QT4_QRCCXXPREFIX',
257                 single_source = True
258                 )
259         env.Append( BUILDERS = { 'Qrc': qrcbuilder } )
260
261         # Interface builder
262         #env['QT4_UIC4COM'] = [
263         #       CLVar('$QT4_UIC $QT4_UICDECLFLAGS -o ${TARGETS[0]} $SOURCE'),
264         #       ]
265         env['QT4_UIC4COM'] = '$QT4_UIC $QT4_UICDECLFLAGS -o $TARGET $SOURCE'
266         uic4builder = Builder(
267                 action='$QT4_UIC4COM',
268                 src_suffix='$QT4_UISUFFIX',
269                 suffix='$QT4_UICDECLSUFFIX',
270                 prefix='$QT4_UICDECLPREFIX',
271                 single_source = True
272                 )
273         env.Append( BUILDERS = { 'Uic4': uic4builder } )
274
275         # Metaobject builder
276         env['QT4_MOCFROMHCOM'] = (
277                 '$QT4_MOC $QT4_MOCFROMHFLAGS -o ${TARGETS[0]} $SOURCE')
278         env['QT4_MOCFROMCXXCOM'] = [
279                 CLVar('$QT4_MOC $QT4_MOCFROMCXXFLAGS -o ${TARGETS[0]} $SOURCE'),
280                 Action(_checkMocIncluded,None)]
281         mocBld = Builder(action={}, prefix={}, suffix={})
282         for h in header_extensions:
283                 mocBld.add_action(h, '$QT4_MOCFROMHCOM')
284                 mocBld.prefix[h] = '$QT4_MOCHPREFIX'
285                 mocBld.suffix[h] = '$QT4_MOCHSUFFIX'
286         for cxx in cxx_suffixes:
287                 mocBld.add_action(cxx, '$QT4_MOCFROMCXXCOM')
288                 mocBld.prefix[cxx] = '$QT4_MOCCXXPREFIX'
289                 mocBld.suffix[cxx] = '$QT4_MOCCXXSUFFIX'
290         env.Append( BUILDERS = { 'Moc4': mocBld } )
291
292         # er... no idea what that was for
293         static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
294         static_obj.src_builder.append('Uic4')
295         shared_obj.src_builder.append('Uic4')
296
297         # We use the emitters of Program / StaticLibrary / SharedLibrary
298         # to scan for moc'able files
299         # We can't refer to the builders directly, we have to fetch them
300         # as Environment attributes because that sets them up to be called
301         # correctly later by our emitter.
302         env.AppendUnique(PROGEMITTER =[AutomocStatic],
303                                          SHLIBEMITTER=[AutomocShared],
304                                          LIBEMITTER  =[AutomocStatic],
305                                          # Of course, we need to link against the qt libraries
306                                          CPPPATH=[os.path.join('$QTDIR', 'include')],
307                                          LIBPATH=[os.path.join('$QTDIR', 'lib')],
308                                          LIBS=['$QT4_LIB'])
309         
310         import new
311         method = new.instancemethod(enable_modules, env, SCons.Environment)
312         env.EnableQt4Modules=method
313
314 def enable_modules(self, modules, debug=False) :
315         import sys
316
317         validModules = [
318                 'QtCore',
319                 'QtGui',
320                 'QtOpenGL',
321                 'Qt3Support',
322                 # The next modules have not been tested yet so, please
323                 # maybe they require additional work on non Linux platforms
324                 'QtSql',
325                 'QtNetwork',
326                 'QtSvg',
327                 'QtTest',
328                 'QtXml',
329                 'QtUiTools',
330                 ]
331         pclessModules = [
332                 'QtUiTools',
333                 'QtUiTools_debug',
334         ]
335         invalidModules=[]
336         for module in modules:
337                 if module not in validModules :
338                         invalidModules.append(module)
339         if invalidModules :
340                 raise "Modules %s are not Qt4 modules. Valid Qt4 modules are: %s"% \
341                         (str(invalidModules),str(validModules))
342
343         # TODO: Check whether we should add QT_CORE_LIB, QT_XML_LIB, QT_NETWORK_LIB...
344         if 'QtGui' in modules:
345                 self.AppendUnique(CPPFLAGS='-DQT_GUI_LIB')
346
347         if sys.platform == "linux2" :
348                 if debug : modules = [module+"_debug" for module in modules]
349                 for module in modules :
350                         if module in pclessModules :
351                         #       self.AppendUnique(LIBS=[module])
352                                 self.AppendUnique(LIBPATH=[os.path.join(self["QTDIR"],"lib",module)])
353                                 self.AppendUnique(CPPPATH=[os.path.join(self["QTDIR"],"include","qt4",module)])
354                                 modules.remove(module)
355                 self.ParseConfig('PKG_CONFIG_PATH=%s/lib:%s/lib/pkgconfig pkg-config %s --libs --cflags'%
356                 (
357                         self['QTDIR'], self['QTDIR'],
358                         ' '.join(modules)))
359                 return
360         if sys.platform == "win32" :
361                 if debug : debugSuffix = 'd'
362                 else : debugSuffix = ''
363                 self.AppendUnique(LIBS=[lib+'4'+debugSuffix for lib in modules])
364                 if 'QtOpenGL' in modules:
365                         self.AppendUnique(LIBS=['opengl32'])
366                 self.AppendUnique(CPPPATH=[ '$QTDIR/include/'+module
367                         for module in modules])
368                 self.AppendUnique(LIBPATH=['$QTDIR/lib'])
369
370
371 def exists(env):
372         return _detect(env)
373