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()
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__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
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_function(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):
80 node = node_factory(filename, dir)
87 Callable class, which works as an emitter for Programs, SharedLibraries and
91 def __init__(self, objBuilderName):
92 self.objBuilderName = objBuilderName
94 def __call__(self, target, source, env):
96 Smart autoscan function. Gets the list of objects for the Program
97 or Lib. Adds objects and builders for the special qt files.
100 if int(env.subst('$QT_AUTOSCAN')) == 0:
101 return target, source
105 debug = int(env.subst('$QT_DEBUG'))
109 # some shortcuts used in the scanner
110 splitext = SCons.Util.splitext
111 objBuilder = getattr(env, self.objBuilderName)
113 # some regular expressions:
115 q_object_search = re.compile(r'[^A-Za-z0-9]Q_OBJECT[^A-Za-z0-9]')
116 # cxx and c comment 'eater'
117 #comment = re.compile(r'(//.*)|(/\*(([^*])|(\*[^/]))*\*/)')
118 # CW: something must be wrong with the regexp. See also bug #998222
119 # CURRENTLY THERE IS NO TEST CASE FOR THAT
121 # The following is kind of hacky to get builders working properly (FIXME)
122 objBuilderEnv = objBuilder.env
124 mocBuilderEnv = env.Moc.env
127 # make a deep copy for the result; MocH objects will be appended
128 out_sources = source[:]
131 if not obj.has_builder():
132 # binary obj file provided
134 print "scons: qt: '%s' seems to be a binary. Discarded." % str(obj)
137 if not splitext(str(cpp))[1] in cxx_suffixes:
139 print "scons: qt: '%s' is no cxx file. Discarded." % str(cpp)
140 # c or fortran source
142 #cpp_contents = comment.sub('', cpp.get_contents())
143 cpp_contents = cpp.get_contents()
145 for h_ext in header_extensions:
146 # try to find the header file in the corresponding source
148 hname = splitext(cpp.name)[0] + h_ext
149 h = find_file(hname, (cpp.get_dir(),), env.File)
152 print "scons: qt: Scanning '%s' (header of '%s')" % (str(h), str(cpp))
153 #h_contents = comment.sub('', h.get_contents())
154 h_contents = h.get_contents()
157 print "scons: qt: no header for '%s'." % (str(cpp))
158 if h and q_object_search.search(h_contents):
159 # h file with the Q_OBJECT macro found -> add moc_cpp
161 moc_o = objBuilder(moc_cpp)
162 out_sources.append(moc_o)
163 #moc_cpp.target_scanner = SCons.Defaults.CScan
165 print "scons: qt: found Q_OBJECT macro in '%s', moc'ing to '%s'" % (str(h), str(moc_cpp))
166 if cpp and q_object_search.search(cpp_contents):
167 # cpp file with Q_OBJECT macro found -> add moc
168 # (to be included in cpp)
172 print "scons: qt: found Q_OBJECT macro in '%s', moc'ing to '%s'" % (str(cpp), str(moc))
173 #moc.source_scanner = SCons.Defaults.CScan
174 # restore the original env attributes (FIXME)
175 objBuilder.env = objBuilderEnv
176 env.Moc.env = mocBuilderEnv
178 return (target, out_sources)
180 AutomocShared = _Automoc('SharedObject')
181 AutomocStatic = _Automoc('StaticObject')
184 """Not really safe, but fast method to detect the QT library"""
187 QTDIR = env.get('QTDIR',None)
189 QTDIR = os.environ.get('QTDIR',None)
191 moc = env.WhereIs('moc')
193 QTDIR = os.path.dirname(os.path.dirname(moc))
196 "Could not detect qt, using moc executable as a hint (QTDIR=%s)" % QTDIR)
201 "Could not detect qt, using empty QTDIR")
204 def uicEmitter(target, source, env):
205 adjustixes = SCons.Util.adjustixes
206 bs = SCons.Util.splitext(str(source[0].name))[0]
207 bs = os.path.join(str(target[0].get_dir()),bs)
208 # first target (header) is automatically added by builder
210 # second target is implementation
211 target.append(adjustixes(bs,
212 env.subst('$QT_UICIMPLPREFIX'),
213 env.subst('$QT_UICIMPLSUFFIX')))
215 # third target is moc file
216 target.append(adjustixes(bs,
217 env.subst('$QT_MOCHPREFIX'),
218 env.subst('$QT_MOCHSUFFIX')))
219 return target, source
221 def uicScannerFunc(node, env, path):
222 #print "uicScannerFunc"
224 includes = re.findall("<include.*?>(.*?)</include>", node.get_contents())
226 for incFile in includes:
227 incNode = dir.File(incFile)
228 if incNode.rexists():
229 #print "uicdep: ", incNode
230 res.append(dir.File(incFile))
232 #print "uicdep: ", incNode, "not found"
236 uicScanner = SCons.Scanner.Scanner(uicScannerFunc,
238 node_class = SCons.Node.FS.File,
239 node_factory = SCons.Node.FS.File,
243 """Add Builders and construction variables for qt to an Environment."""
244 CLVar = SCons.Util.CLVar
245 Action = SCons.Action.Action
246 Builder = SCons.Builder.Builder
247 splitext = SCons.Util.splitext
249 env.SetDefault(QTDIR = _detect(env),
250 QT_BINPATH = os.path.join('$QTDIR', 'bin'),
251 QT_CPPPATH = os.path.join('$QTDIR', 'include'),
252 QT_LIBPATH = os.path.join('$QTDIR', 'lib'),
253 QT_MOC = os.path.join('$QT_BINPATH','moc'),
254 QT_UIC = os.path.join('$QT_BINPATH','uic'),
255 QT_LIB = 'qt', # may be set to qt-mt
257 QT_AUTOSCAN = 1, # scan for moc'able sources
259 # Some QT specific flags. I don't expect someone wants to
260 # manipulate those ...
261 QT_UICIMPLFLAGS = CLVar(''),
262 QT_UICDECLFLAGS = CLVar(''),
263 QT_MOCFROMHFLAGS = CLVar(''),
264 QT_MOCFROMCXXFLAGS = CLVar('-i'),
266 # suffixes/prefixes for the headers / sources to generate
267 QT_UICDECLPREFIX = '',
268 QT_UICDECLSUFFIX = '.h',
269 QT_UICIMPLPREFIX = 'uic_',
270 QT_UICIMPLSUFFIX = '$CXXFILESUFFIX',
271 QT_MOCHPREFIX = 'moc_',
272 QT_MOCHSUFFIX = '$CXXFILESUFFIX',
273 QT_MOCCXXPREFIX = '',
274 QT_MOCCXXSUFFIX = '.moc',
277 # Commands for the qt support ...
278 # command to generate header, implementation and moc-file
281 CLVar('$QT_UIC $QT_UICDECLFLAGS -o ${TARGETS[0]} $SOURCE'),
282 CLVar('$QT_UIC $QT_UICIMPLFLAGS -impl ${TARGETS[0].file} '
283 '-o ${TARGETS[1]} $SOURCE'),
284 CLVar('$QT_MOC $QT_MOCFROMHFLAGS -o ${TARGETS[2]} ${TARGETS[0]}')],
285 # command to generate meta object information for a class
286 # declarated in a header
288 '$QT_MOC $QT_MOCFROMHFLAGS -o ${TARGETS[0]} $SOURCE'),
289 # command to generate meta object information for a class
290 # declarated in a cpp file
292 CLVar('$QT_MOC $QT_MOCFROMCXXFLAGS -o ${TARGETS[0]} $SOURCE'),
293 Action(checkMocIncluded,None)])
295 # ... and the corresponding builders
296 uicBld = Builder(action=SCons.Action.Action('$QT_UICCOM', '$QT_UICCOMSTR'),
298 src_suffix='$QT_UISUFFIX',
299 suffix='$QT_UICDECLSUFFIX',
300 prefix='$QT_UICDECLPREFIX',
301 source_scanner=uicScanner)
302 mocBld = Builder(action={}, prefix={}, suffix={})
303 for h in header_extensions:
304 act = SCons.Action.Action('$QT_MOCFROMHCOM', '$QT_MOCFROMHCOMSTR')
305 mocBld.add_action(h, act)
306 mocBld.prefix[h] = '$QT_MOCHPREFIX'
307 mocBld.suffix[h] = '$QT_MOCHSUFFIX'
308 for cxx in cxx_suffixes:
309 act = SCons.Action.Action('$QT_MOCFROMCXXCOM', '$QT_MOCFROMCXXCOMSTR')
310 mocBld.add_action(cxx, act)
311 mocBld.prefix[cxx] = '$QT_MOCCXXPREFIX'
312 mocBld.suffix[cxx] = '$QT_MOCCXXSUFFIX'
314 # register the builders
315 env['BUILDERS']['Uic'] = uicBld
316 env['BUILDERS']['Moc'] = mocBld
317 static_obj, shared_obj = SCons.Tool.createObjBuilders(env)
318 static_obj.src_builder.append('Uic')
319 shared_obj.src_builder.append('Uic')
321 # We use the emitters of Program / StaticLibrary / SharedLibrary
322 # to scan for moc'able files
323 # We can't refer to the builders directly, we have to fetch them
324 # as Environment attributes because that sets them up to be called
325 # correctly later by our emitter.
326 env.AppendUnique(PROGEMITTER =[AutomocStatic],
327 SHLIBEMITTER=[AutomocShared],
328 LIBEMITTER =[AutomocStatic],
329 # Of course, we need to link against the qt libraries
330 CPPPATH=["$QT_CPPPATH"],
331 LIBPATH=["$QT_LIBPATH"],