1 # -*- coding: utf-8 -*-
4 Tool-specific initialization for Qt.
6 There normally shouldn't be any need to import this module directly.
7 It will usually be imported through the generic SCons.Tool.Tool()
13 # Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 The SCons Foundation
15 # Permission is hereby granted, free of charge, to any person obtaining
16 # a copy of this software and associated documentation files (the
17 # "Software"), to deal in the Software without restriction, including
18 # without limitation the rights to use, copy, modify, merge, publish,
19 # distribute, sublicense, and/or sell copies of the Software, and to
20 # permit persons to whom the Software is furnished to do so, subject to
21 # the following conditions:
23 # The above copyright notice and this permission notice shall be included
24 # in all copies or substantial portions of the Software.
26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
27 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
28 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 __revision__ = "/home/scons/scons/branch.0/branch.96/baseline/src/engine/SCons/Tool/qt.py 0.96.92.D001 2006/04/10 23:13:27 knight"
49 class ToolQtWarning(SCons.Warnings.Warning):
52 class GeneratedMocFileNotIncluded(ToolQtWarning):
55 class QtdirNotFound(ToolQtWarning):
58 SCons.Warnings.enableWarningClass(ToolQtWarning)
60 qrcinclude_re = re.compile(r'<file>([^<]*)</file>', re.M)
62 def transformToWinePath(path) :
63 return os.popen('winepath -w "%s"'%path).read().strip().replace('\\','/')
65 header_extensions = [".h", ".hxx", ".hpp", ".hh"]
66 if SCons.Util.case_sensitive_suffixes('.h', '.H'):
67 header_extensions.append('.H')
68 # TODO: The following two lines will work when integrated back to SCons
69 # TODO: Meanwhile the third line will do the work
70 #cplusplus = __import__('c++', globals(), locals(), [])
71 #cxx_suffixes = cplusplus.CXXSuffixes
72 cxx_suffixes = [".c", ".cxx", ".cpp", ".cc"]
74 def checkMocIncluded(target, source, env):
77 # looks like cpp.includes is cleared before the build stage :-(
78 # not really sure about the path transformations (moc.cwd? cpp.cwd?) :-/
79 path = SCons.Defaults.CScan.path_function(env, moc.cwd)
80 includes = SCons.Defaults.CScan(cpp, env, path)
81 if not moc in includes:
83 GeneratedMocFileNotIncluded,
84 "Generated moc file '%s' is not included by '%s'" %
87 def find_file(filename, paths, node_factory):
89 node = node_factory(filename, dir)
96 Callable class, which works as an emitter for Programs, SharedLibraries and
100 def __init__(self, objBuilderName):
101 self.objBuilderName = objBuilderName
103 def __call__(self, target, source, env):
105 Smart autoscan function. Gets the list of objects for the Program
106 or Lib. Adds objects and builders for the special qt files.
109 if int(env.subst('$QT4_AUTOSCAN')) == 0:
110 return target, source
114 debug = int(env.subst('$QT4_DEBUG'))
118 # some shortcuts used in the scanner
119 splitext = SCons.Util.splitext
120 objBuilder = getattr(env, self.objBuilderName)
122 # some regular expressions:
124 q_object_search = re.compile(r'[^A-Za-z0-9]Q_OBJECT[^A-Za-z0-9]')
125 # cxx and c comment 'eater'
126 #comment = re.compile(r'(//.*)|(/\*(([^*])|(\*[^/]))*\*/)')
127 # CW: something must be wrong with the regexp. See also bug #998222
128 # CURRENTLY THERE IS NO TEST CASE FOR THAT
130 # The following is kind of hacky to get builders working properly (FIXME)
131 objBuilderEnv = objBuilder.env
133 mocBuilderEnv = env.Moc4.env
136 # make a deep copy for the result; MocH objects will be appended
137 out_sources = source[:]
140 if isinstance(obj,basestring): # big kludge!
141 print "scons: qt4: '%s' MAYBE USING AN OLD SCONS VERSION AND NOT CONVERTED TO¨ \
142 ¨ 'File'. Discarded." % str(obj)
144 if not obj.has_builder():
145 # binary obj file provided
147 print "scons: qt: '%s' seems to be a binary. Discarded." % str(obj)
150 if not splitext(str(cpp))[1] in cxx_suffixes:
152 print "scons: qt: '%s' is no cxx file. Discarded." % str(cpp)
153 # c or fortran source
155 #cpp_contents = comment.sub('', cpp.get_contents())
157 cpp_contents = cpp.get_contents()
158 except: continue # may be an still not generated source
160 for h_ext in header_extensions:
161 # try to find the header file in the corresponding source
163 hname = splitext(cpp.name)[0] + h_ext
164 h = find_file(hname, (cpp.get_dir(),), env.File)
167 print "scons: qt: Scanning '%s' (header of '%s')" % (str(h), str(cpp))
168 #h_contents = comment.sub('', h.get_contents())
169 h_contents = h.get_contents()
172 print "scons: qt: no header for '%s'." % (str(cpp))
173 if h and q_object_search.search(h_contents):
174 # h file with the Q_OBJECT macro found -> add moc_cpp
175 moc_cpp = env.Moc4(h)
176 moc_o = objBuilder(moc_cpp)
177 out_sources.append(moc_o)
178 #moc_cpp.target_scanner = SCons.Defaults.CScan
180 print "scons: qt: found Q_OBJECT macro in '%s', moc'ing to '%s'" \
181 % (str(h), str(moc_cpp))
182 if cpp and q_object_search.search(cpp_contents):
183 # cpp file with Q_OBJECT macro found -> add moc
184 # (to be included in cpp)
188 print "scons: qt: found Q_OBJECT macro in '%s', moc'ing to '%s'" \
189 % (str(cpp), str(moc))
190 #moc.source_scanner = SCons.Defaults.CScan
191 # restore the original env attributes (FIXME)
192 objBuilder.env = objBuilderEnv
193 env.Moc4.env = mocBuilderEnv
195 return (target, out_sources)
197 AutomocShared = _Automoc('SharedObject')
198 AutomocStatic = _Automoc('StaticObject')
201 """Not really safe, but fast method to detect the QT library"""
202 try: return env['QTDIR']
203 except KeyError: pass
205 try: return os.environ['QTDIR']
206 except KeyError: pass
208 moc = env.WhereIs('moc-qt4') or env.WhereIs('moc4') or env.WhereIs('moc')
210 QTDIR = os.path.dirname(os.path.dirname(moc))
211 # SCons.Warnings.warn(
213 # "QTDIR variable is not defined, using moc executable as a hint (QTDIR=%s)" % QTDIR)
216 raise SCons.Errors.StopError(
218 "Could not detect Qt 4 installation")
222 """Add Builders and construction variables for qt to an Environment."""
224 def locateQt4Command(env, command, qtdir) :
234 for suffix in suffixes :
235 fullpath = os.path.join(qtdir,'bin',command + suffix)
236 if os.access(fullpath, os.X_OK) :
238 triedPaths.append(fullpath)
240 fullpath = env.Detect([command+'-qt4', command+'4', command])
241 if not (fullpath is None) : return fullpath
243 raise Exception("Qt4 command '" + command + "' not found. Tried: " + ', '.join(triedPaths))
246 CLVar = SCons.Util.CLVar
247 Action = SCons.Action.Action
248 Builder = SCons.Builder.Builder
249 splitext = SCons.Util.splitext
251 env['QTDIR'] = _detect(env)
252 # TODO: 'Replace' should be 'SetDefault'
255 QTDIR = _detect(env),
256 QT4_BINPATH = os.path.join('$QTDIR', 'bin'),
257 # TODO: This is not reliable to QTDIR value changes but needed in order to support '-qt4'
259 QT4_MOC = locateQt4Command(env,'moc', env['QTDIR']),
260 QT4_UIC = locateQt4Command(env,'uic', env['QTDIR']),
261 QT4_RCC = locateQt4Command(env,'rcc', env['QTDIR']),
262 QT4_LUPDATE = locateQt4Command(env,'lupdate', env['QTDIR']),
263 QT4_LRELEASE = locateQt4Command(env,'lrelease', env['QTDIR']),
265 QT4_AUTOSCAN = 1, # Should the qt tool try to figure out, which sources are to be moc'ed?
267 # Some QT specific flags. I don't expect someone wants to
268 # manipulate those ...
269 QT4_UICFLAGS = CLVar(''),
270 QT4_MOCFROMHFLAGS = CLVar(''),
271 QT4_MOCFROMCXXFLAGS = CLVar('-i'),
274 # suffixes/prefixes for the headers / sources to generate
275 QT4_UISUFFIX = '.ui',
276 QT4_UICDECLPREFIX = 'ui_',
277 QT4_UICDECLSUFFIX = '.h',
278 QT4_MOCINCPREFIX = '-I',
279 QT4_MOCHPREFIX = 'moc_',
280 QT4_MOCHSUFFIX = '$CXXFILESUFFIX',
281 QT4_MOCCXXPREFIX = '',
282 QT4_MOCCXXSUFFIX = '.moc',
283 QT4_QRCSUFFIX = '.qrc',
284 QT4_QRCCXXSUFFIX = '$CXXFILESUFFIX',
285 QT4_QRCCXXPREFIX = 'qrc_',
288 '$( ${_concat(QT4_MOCINCPREFIX, QT4_MOCCPPPATH, INCSUFFIX, __env__, RDirs)} $)',
290 # Commands for the qt support ...
291 QT4_UICCOM = '$QT4_UIC $QT4_UICFLAGS -o $TARGET $SOURCE',
292 QT4_MOCFROMHCOM = '$QT4_MOC $QT4_MOCFROMHFLAGS $QT4_MOCINCFLAGS -o $TARGET $SOURCE',
293 QT4_MOCFROMCXXCOM = [
294 '$QT4_MOC $QT4_MOCFROMCXXFLAGS $QT4_MOCINCFLAGS -o $TARGET $SOURCE',
295 Action(checkMocIncluded,None)],
296 QT4_LUPDATECOM = '$QT4_LUPDATE $SOURCE -ts $TARGET',
297 QT4_LRELEASECOM = '$QT4_LRELEASE $SOURCE',
298 QT4_RCCCOM = '$QT4_RCC $QT4_QRCFLAGS -name ${SOURCE.filebase} $SOURCE -o $TARGET',
301 # Translation builder
303 action = SCons.Action.Action('$QT4_LUPDATECOM'), #,'$QT4_LUPDATECOMSTR'),
306 env.Append( BUILDERS = { 'Ts': tsbuilder } )
308 action = SCons.Action.Action('$QT4_LRELEASECOM'),# , '$QT4_LRELEASECOMSTR'),
313 env.Append( BUILDERS = { 'Qm': qmbuilder } )
316 def scanResources(node, env, path, arg):
317 # I've being careful on providing names relative to the qrc file
318 # If that was not needed that code could be simplified a lot
319 def recursiveFiles(basepath, path) :
321 for item in os.listdir(os.path.join(basepath, path)) :
322 itemPath = os.path.join(path, item)
323 if os.path.isdir(os.path.join(basepath, itemPath)) :
324 result += recursiveFiles(basepath, itemPath)
326 result.append(itemPath)
328 contents = node.get_contents()
329 includes = qrcinclude_re.findall(contents)
330 qrcpath = os.path.dirname(node.path)
331 dirs = [included for included in includes if os.path.isdir(os.path.join(qrcpath,included))]
332 # dirs need to include files recursively
335 includes+=recursiveFiles(qrcpath,dir)
337 qrcscanner = SCons.Scanner.Scanner(name = 'qrcfile',
338 function = scanResources,
341 qrcaction = SCons.Action.Action('$QT4_RCCCOM', '$QT4_RCCCOMSTR')
342 qrcbuilder = Builder(
344 source_scanner = qrcscanner,
345 src_suffix = '$QT4_QRCSUFFIX',
346 suffix = '$QT4_QRCCXXSUFFIX',
347 prefix = '$QT4_QRCCXXPREFIX',
350 env.Append( BUILDERS = { 'Qrc': qrcbuilder } )
352 #c_file, cxx_file = SCons.Tool.createCFileBuilders(env)
353 #cxx_file.add_action('.qrc', qrcaction)
356 uic4builder = Builder(
357 action = SCons.Action.Action('$QT4_UICCOM', '$QT4_UICCOMSTR'),
358 src_suffix='$QT4_UISUFFIX',
359 suffix='$QT4_UICDECLSUFFIX',
360 prefix='$QT4_UICDECLPREFIX',
362 #TODO: Consider the uiscanner on new scons version
364 env['BUILDERS']['Uic4'] = uic4builder
367 mocBld = Builder(action={}, prefix={}, suffix={})
368 for h in header_extensions:
369 act = SCons.Action.Action('$QT4_MOCFROMHCOM', '$QT4_MOCFROMHCOMSTR')
370 mocBld.add_action(h, act)
371 mocBld.prefix[h] = '$QT4_MOCHPREFIX'
372 mocBld.suffix[h] = '$QT4_MOCHSUFFIX'
373 for cxx in cxx_suffixes:
374 act = SCons.Action.Action('$QT4_MOCFROMCXXCOM', '$QT4_MOCFROMCXXCOMSTR')
375 mocBld.add_action(cxx, act)
376 mocBld.prefix[cxx] = '$QT4_MOCCXXPREFIX'
377 mocBld.suffix[cxx] = '$QT4_MOCCXXSUFFIX'
378 env['BUILDERS']['Moc4'] = mocBld
380 # er... no idea what that was for
381 # Answer: This lets the Object (and therefore the Program builder) automatically
382 # recognize and build uic and qrc files
383 static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
384 static_obj.src_builder.append('Uic4')
385 shared_obj.src_builder.append('Uic4')
386 static_obj.src_builder.append('Qrc')
387 shared_obj.src_builder.append('Qrc')
389 # We use the emitters of Program / StaticLibrary / SharedLibrary
390 # to scan for moc'able files
391 # We can't refer to the builders directly, we have to fetch them
392 # as Environment attributes because that sets them up to be called
393 # correctly later by our emitter.
394 env.AppendUnique( PROGEMITTER =[AutomocStatic],
395 SHLIBEMITTER=[AutomocShared],
396 LIBEMITTER =[AutomocStatic] )
398 # TODO: Does dbusxml2cpp need an adapter
399 env.AddMethod(enable_modules, "EnableQt4Modules")
401 resourceDirectoryBuilder = Builder(
402 action = Action(resourceDirectory),
403 source_factory = env.Dir)
404 env.Append( BUILDERS = { 'Qt4ResourceFromDirectory' : resourceDirectoryBuilder } )
407 env.AddMethod(qt4glob, "Qt4Glob")
409 def enable_modules(self, modules, debug=False, crosscompiling=False) :
417 'QtAssistant', # deprecated
423 # The next modules have not been tested yet so, please
424 # maybe they require additional work on non Linux platforms
431 'QtDesignerComponents',
439 # in qt <= 4.3 designer and designerComponents are pcless, on qt4.4 they are not, so removed.
441 # 'QtDesignerComponents',
447 for module in modules:
448 if module not in validModules :
449 invalidModules.append(module)
451 raise Exception("Modules %s are not Qt4 modules. Valid Qt4 modules are: %s"% (
452 str(invalidModules),str(validModules)))
455 'QtScript' : ['QT_SCRIPT_LIB'],
456 'QtSvg' : ['QT_SVG_LIB'],
457 'Qt3Support' : ['QT_QT3SUPPORT_LIB','QT3_SUPPORT'],
458 'QtSql' : ['QT_SQL_LIB'],
459 'QtXml' : ['QT_XML_LIB'],
460 'QtOpenGL' : ['QT_OPENGL_LIB'],
461 'QtGui' : ['QT_GUI_LIB'],
462 'QtNetwork' : ['QT_NETWORK_LIB'],
463 'QtCore' : ['QT_CORE_LIB'],
465 for module in modules :
466 try : self.AppendUnique(CPPDEFINES=moduleDefines[module])
469 if sys.platform in ["darwin", "linux2"] and not crosscompiling :
470 if debug : debugSuffix = '_debug'
471 for module in modules :
472 if module not in pclessModules : continue
473 self.AppendUnique(LIBS=[module+debugSuffix])
474 self.AppendUnique(LIBPATH=[os.path.join("$QTDIR","lib")])
475 self.AppendUnique(CPPPATH=[os.path.join("$QTDIR","include","qt4")])
476 self.AppendUnique(CPPPATH=[os.path.join("$QTDIR","include","qt4",module)])
477 pcmodules = [module+debugSuffix for module in modules if module not in pclessModules ]
478 if 'QtDBus' in pcmodules:
479 self.AppendUnique(CPPPATH=[os.path.join("$QTDIR","include","qt4","QtDBus")])
480 if "QtAssistant" in pcmodules:
481 self.AppendUnique(CPPPATH=[os.path.join("$QTDIR","include","qt4","QtAssistant")])
482 pcmodules.remove("QtAssistant")
483 pcmodules.append("QtAssistantClient")
484 self.ParseConfig('pkg-config %s --libs --cflags'% ' '.join(pcmodules))
485 self["QT4_MOCCPPPATH"] = self["CPPPATH"]
487 if sys.platform == "win32" or crosscompiling :
489 transformedQtdir = transformToWinePath(self['QTDIR'])
490 self['QT4_MOC'] = "QTDIR=%s %s"%( transformedQtdir, self['QT4_MOC'])
491 self.AppendUnique(CPPPATH=[os.path.join("$QTDIR","include")])
492 try: modules.remove("QtDBus")
494 if debug : debugSuffix = 'd'
495 if "QtAssistant" in modules:
496 self.AppendUnique(CPPPATH=[os.path.join("$QTDIR","include","QtAssistant")])
497 modules.remove("QtAssistant")
498 modules.append("QtAssistantClient")
499 self.AppendUnique(LIBS=[lib+'4'+debugSuffix for lib in modules if lib not in staticModules])
500 self.PrependUnique(LIBS=[lib+debugSuffix for lib in modules if lib in staticModules])
501 if 'QtOpenGL' in modules:
502 self.AppendUnique(LIBS=['opengl32'])
503 self.AppendUnique(CPPPATH=[ '$QTDIR/include/'])
504 self.AppendUnique(CPPPATH=[ '$QTDIR/include/'+module for module in modules])
506 self["QT4_MOCCPPPATH"] = [
507 path.replace('$QTDIR', transformedQtdir)
508 for path in self['CPPPATH'] ]
510 self["QT4_MOCCPPPATH"] = self["CPPPATH"]
511 self.AppendUnique(LIBPATH=[os.path.join('$QTDIR','lib')])
514 if sys.platform=="darwin" :
515 # TODO: Test debug version on Mac
516 self.AppendUnique(LIBPATH=[os.path.join('$QTDIR','lib')])
517 self.AppendUnique(LINKFLAGS="-F$QTDIR/lib")
518 self.AppendUnique(LINKFLAGS="-L$QTDIR/lib") #TODO clean!
519 if debug : debugSuffix = 'd'
520 for module in modules :
521 # self.AppendUnique(CPPPATH=[os.path.join("$QTDIR","include")])
522 # self.AppendUnique(CPPPATH=[os.path.join("$QTDIR","include",module)])
524 self.AppendUnique(CPPPATH=[os.path.join("$QTDIR","include", "qt4")])
525 self.AppendUnique(CPPPATH=[os.path.join("$QTDIR","include", "qt4", module)])
526 if module in staticModules :
527 self.AppendUnique(LIBS=[module+debugSuffix]) # TODO: Add the debug suffix
528 self.AppendUnique(LIBPATH=[os.path.join("$QTDIR","lib")])
530 # self.Append(LINKFLAGS=['-framework', module])
532 self.Append(LIBS=module)
533 if 'QtOpenGL' in modules:
534 self.AppendUnique(LINKFLAGS="-F/System/Library/Frameworks")
535 self.Append(LINKFLAGS=['-framework', 'AGL']) #TODO ughly kludge to avoid quotes
536 self.Append(LINKFLAGS=['-framework', 'OpenGL'])
537 self["QT4_MOCCPPPATH"] = self["CPPPATH"]
539 # This should work for mac but doesn't
540 # env.AppendUnique(FRAMEWORKPATH=[os.path.join(env['QTDIR'],'lib')])
541 # env.AppendUnique(FRAMEWORKS=['QtCore','QtGui','QtOpenGL', 'AGL'])
545 resources = env.Glob("*.qrc")
546 sources = [ f for f in env.Glob("*.cc")
547 if not f.name.startswith("qrc_") and not f.name.startswith("moc_") ]
548 return sources + resources
550 def scanResourceDirectory(directory, prefix="", exclude=[]):
553 # '<?xml version="1.0" encoding="utf-8"?>',
554 '<RCC version="1.0">' ]
555 for base, subdirs, files in os.walk(str(directory)):
556 rv.append('<qresource prefix="%s">' % os.path.join(prefix, base[len(str(directory)):]))
558 if not [True for pattern in exclude if fnmatch.fnmatch(f,pattern)]:
559 rv.append('<file alias="%s">%s</file>' % (f, os.path.join(base, f)))
560 rv.append("'</qresource>'")
561 rv.append('</RCC>\n')
564 def resourceDirectory(env, target, source):
565 file(str(target[0]), 'w').write(scanResourceDirectory(
566 source[0], prefix=env.get('RESOURCE_PREFIX', ''), exclude=env.get('EXCLUDE', [])))