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, 2008 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__ = "src/engine/SCons/Tool/qt.py 3842 2008/12/20 22:59:52 scons"
47 class ToolQtWarning(SCons.Warnings.Warning):
50 class GeneratedMocFileNotIncluded(ToolQtWarning):
53 class QtdirNotFound(ToolQtWarning):
56 SCons.Warnings.enableWarningClass(ToolQtWarning)
58 header_extensions = [".h", ".hxx", ".hpp", ".hh"]
59 if SCons.Util.case_sensitive_suffixes('.h', '.H'):
60 header_extensions.append('.H')
61 cplusplus = __import__('c++', globals(), locals(), [])
62 cxx_suffixes = cplusplus.CXXSuffixes
64 def checkMocIncluded(target, source, env):
67 # looks like cpp.includes is cleared before the build stage :-(
68 # not really sure about the path transformations (moc.cwd? cpp.cwd?) :-/
69 path = SCons.Defaults.CScan.path(env, moc.cwd)
70 includes = SCons.Defaults.CScan(cpp, env, path)
71 if not moc in includes:
73 GeneratedMocFileNotIncluded,
74 "Generated moc file '%s' is not included by '%s'" %
77 def find_file(filename, paths, node_factory):
79 node = node_factory(filename, dir)
86 Callable class, which works as an emitter for Programs, SharedLibraries and
90 def __init__(self, objBuilderName):
91 self.objBuilderName = objBuilderName
93 def __call__(self, target, source, env):
95 Smart autoscan function. Gets the list of objects for the Program
96 or Lib. Adds objects and builders for the special qt files.
99 if int(env.subst('$QT_AUTOSCAN')) == 0:
100 return target, source
104 debug = int(env.subst('$QT_DEBUG'))
108 # some shortcuts used in the scanner
109 splitext = SCons.Util.splitext
110 objBuilder = getattr(env, self.objBuilderName)
112 # some regular expressions:
114 q_object_search = re.compile(r'[^A-Za-z0-9]Q_OBJECT[^A-Za-z0-9]')
115 # cxx and c comment 'eater'
116 #comment = re.compile(r'(//.*)|(/\*(([^*])|(\*[^/]))*\*/)')
117 # CW: something must be wrong with the regexp. See also bug #998222
118 # CURRENTLY THERE IS NO TEST CASE FOR THAT
120 # The following is kind of hacky to get builders working properly (FIXME)
121 objBuilderEnv = objBuilder.env
123 mocBuilderEnv = env.Moc.env
126 # make a deep copy for the result; MocH objects will be appended
127 out_sources = source[:]
130 if not obj.has_builder():
131 # binary obj file provided
133 print "scons: qt: '%s' seems to be a binary. Discarded." % str(obj)
136 if not splitext(str(cpp))[1] in cxx_suffixes:
138 print "scons: qt: '%s' is no cxx file. Discarded." % str(cpp)
139 # c or fortran source
141 #cpp_contents = comment.sub('', cpp.get_contents())
142 cpp_contents = cpp.get_contents()
144 for h_ext in header_extensions:
145 # try to find the header file in the corresponding source
147 hname = splitext(cpp.name)[0] + h_ext
148 h = find_file(hname, (cpp.get_dir(),), env.File)
151 print "scons: qt: Scanning '%s' (header of '%s')" % (str(h), str(cpp))
152 #h_contents = comment.sub('', h.get_contents())
153 h_contents = h.get_contents()
156 print "scons: qt: no header for '%s'." % (str(cpp))
157 if h and q_object_search.search(h_contents):
158 # h file with the Q_OBJECT macro found -> add moc_cpp
160 moc_o = objBuilder(moc_cpp)
161 out_sources.append(moc_o)
162 #moc_cpp.target_scanner = SCons.Defaults.CScan
164 print "scons: qt: found Q_OBJECT macro in '%s', moc'ing to '%s'" % (str(h), str(moc_cpp))
165 if cpp and q_object_search.search(cpp_contents):
166 # cpp file with Q_OBJECT macro found -> add moc
167 # (to be included in cpp)
171 print "scons: qt: found Q_OBJECT macro in '%s', moc'ing to '%s'" % (str(cpp), str(moc))
172 #moc.source_scanner = SCons.Defaults.CScan
173 # restore the original env attributes (FIXME)
174 objBuilder.env = objBuilderEnv
175 env.Moc.env = mocBuilderEnv
177 return (target, out_sources)
179 AutomocShared = _Automoc('SharedObject')
180 AutomocStatic = _Automoc('StaticObject')
183 """Not really safe, but fast method to detect the QT library"""
186 QTDIR = env.get('QTDIR',None)
188 QTDIR = os.environ.get('QTDIR',None)
190 moc = env.WhereIs('moc')
192 QTDIR = os.path.dirname(os.path.dirname(moc))
195 "Could not detect qt, using moc executable as a hint (QTDIR=%s)" % QTDIR)
200 "Could not detect qt, using empty QTDIR")
203 def uicEmitter(target, source, env):
204 adjustixes = SCons.Util.adjustixes
205 bs = SCons.Util.splitext(str(source[0].name))[0]
206 bs = os.path.join(str(target[0].get_dir()),bs)
207 # first target (header) is automatically added by builder
209 # second target is implementation
210 target.append(adjustixes(bs,
211 env.subst('$QT_UICIMPLPREFIX'),
212 env.subst('$QT_UICIMPLSUFFIX')))
214 # third target is moc file
215 target.append(adjustixes(bs,
216 env.subst('$QT_MOCHPREFIX'),
217 env.subst('$QT_MOCHSUFFIX')))
218 return target, source
220 def uicScannerFunc(node, env, path):
222 lookout.extend(env['CPPPATH'])
223 lookout.append(str(node.rfile().dir))
224 includes = re.findall("<include.*?>(.*?)</include>", node.get_contents())
226 for incFile in includes:
227 dep = env.FindFile(incFile,lookout)
232 uicScanner = SCons.Scanner.Base(uicScannerFunc,
234 node_class = SCons.Node.FS.File,
235 node_factory = SCons.Node.FS.File,
239 """Add Builders and construction variables for qt to an Environment."""
240 CLVar = SCons.Util.CLVar
241 Action = SCons.Action.Action
242 Builder = SCons.Builder.Builder
244 env.SetDefault(QTDIR = _detect(env),
245 QT_BINPATH = os.path.join('$QTDIR', 'bin'),
246 QT_CPPPATH = os.path.join('$QTDIR', 'include'),
247 QT_LIBPATH = os.path.join('$QTDIR', 'lib'),
248 QT_MOC = os.path.join('$QT_BINPATH','moc'),
249 QT_UIC = os.path.join('$QT_BINPATH','uic'),
250 QT_LIB = 'qt', # may be set to qt-mt
252 QT_AUTOSCAN = 1, # scan for moc'able sources
254 # Some QT specific flags. I don't expect someone wants to
255 # manipulate those ...
256 QT_UICIMPLFLAGS = CLVar(''),
257 QT_UICDECLFLAGS = CLVar(''),
258 QT_MOCFROMHFLAGS = CLVar(''),
259 QT_MOCFROMCXXFLAGS = CLVar('-i'),
261 # suffixes/prefixes for the headers / sources to generate
262 QT_UICDECLPREFIX = '',
263 QT_UICDECLSUFFIX = '.h',
264 QT_UICIMPLPREFIX = 'uic_',
265 QT_UICIMPLSUFFIX = '$CXXFILESUFFIX',
266 QT_MOCHPREFIX = 'moc_',
267 QT_MOCHSUFFIX = '$CXXFILESUFFIX',
268 QT_MOCCXXPREFIX = '',
269 QT_MOCCXXSUFFIX = '.moc',
272 # Commands for the qt support ...
273 # command to generate header, implementation and moc-file
276 CLVar('$QT_UIC $QT_UICDECLFLAGS -o ${TARGETS[0]} $SOURCE'),
277 CLVar('$QT_UIC $QT_UICIMPLFLAGS -impl ${TARGETS[0].file} '
278 '-o ${TARGETS[1]} $SOURCE'),
279 CLVar('$QT_MOC $QT_MOCFROMHFLAGS -o ${TARGETS[2]} ${TARGETS[0]}')],
280 # command to generate meta object information for a class
281 # declarated in a header
283 '$QT_MOC $QT_MOCFROMHFLAGS -o ${TARGETS[0]} $SOURCE'),
284 # command to generate meta object information for a class
285 # declarated in a cpp file
287 CLVar('$QT_MOC $QT_MOCFROMCXXFLAGS -o ${TARGETS[0]} $SOURCE'),
288 Action(checkMocIncluded,None)])
290 # ... and the corresponding builders
291 uicBld = Builder(action=SCons.Action.Action('$QT_UICCOM', '$QT_UICCOMSTR'),
293 src_suffix='$QT_UISUFFIX',
294 suffix='$QT_UICDECLSUFFIX',
295 prefix='$QT_UICDECLPREFIX',
296 source_scanner=uicScanner)
297 mocBld = Builder(action={}, prefix={}, suffix={})
298 for h in header_extensions:
299 act = SCons.Action.Action('$QT_MOCFROMHCOM', '$QT_MOCFROMHCOMSTR')
300 mocBld.add_action(h, act)
301 mocBld.prefix[h] = '$QT_MOCHPREFIX'
302 mocBld.suffix[h] = '$QT_MOCHSUFFIX'
303 for cxx in cxx_suffixes:
304 act = SCons.Action.Action('$QT_MOCFROMCXXCOM', '$QT_MOCFROMCXXCOMSTR')
305 mocBld.add_action(cxx, act)
306 mocBld.prefix[cxx] = '$QT_MOCCXXPREFIX'
307 mocBld.suffix[cxx] = '$QT_MOCCXXSUFFIX'
309 # register the builders
310 env['BUILDERS']['Uic'] = uicBld
311 env['BUILDERS']['Moc'] = mocBld
312 static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
313 static_obj.add_src_builder('Uic')
314 shared_obj.add_src_builder('Uic')
316 # We use the emitters of Program / StaticLibrary / SharedLibrary
317 # to scan for moc'able files
318 # We can't refer to the builders directly, we have to fetch them
319 # as Environment attributes because that sets them up to be called
320 # correctly later by our emitter.
321 env.AppendUnique(PROGEMITTER =[AutomocStatic],
322 SHLIBEMITTER=[AutomocShared],
323 LIBEMITTER =[AutomocStatic],
324 # Of course, we need to link against the qt libraries
325 CPPPATH=["$QT_CPPPATH"],
326 LIBPATH=["$QT_LIBPATH"],