On Windows, the Intel Fortran compiler (ifort) uses -object: instead of -o as the...
[scons.git] / src / engine / SCons / Tool / qt.py
1
2 """SCons.Tool.qt
3
4 Tool-specific initialization for Qt.
5
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()
8 selection method.
9
10 """
11
12 #
13 # __COPYRIGHT__
14 #
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:
22 #
23 # The above copyright notice and this permission notice shall be included
24 # in all copies or substantial portions of the Software.
25 #
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.
33 #
34
35 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
36
37 import os.path
38 import re
39
40 import SCons.Action
41 import SCons.Builder
42 import SCons.Defaults
43 import SCons.Scanner
44 import SCons.Tool
45 import SCons.Util
46
47 class ToolQtWarning(SCons.Warnings.Warning):
48     pass
49
50 class GeneratedMocFileNotIncluded(ToolQtWarning):
51     pass
52
53 class QtdirNotFound(ToolQtWarning):
54     pass
55
56 SCons.Warnings.enableWarningClass(ToolQtWarning)
57
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
63
64 def checkMocIncluded(target, source, env):
65     moc = target[0]
66     cpp = source[0]
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:
72         SCons.Warnings.warn(
73             GeneratedMocFileNotIncluded,
74             "Generated moc file '%s' is not included by '%s'" %
75             (str(moc), str(cpp)))
76
77 def find_file(filename, paths, node_factory):
78     retval = None
79     for dir in paths:
80         node = node_factory(filename, dir)
81         if node.rexists():
82             return node
83     return None
84
85 class _Automoc:
86     """
87     Callable class, which works as an emitter for Programs, SharedLibraries and
88     StaticLibraries.
89     """
90
91     def __init__(self, objBuilderName):
92         self.objBuilderName = objBuilderName
93         
94     def __call__(self, target, source, env):
95         """
96         Smart autoscan function. Gets the list of objects for the Program
97         or Lib. Adds objects and builders for the special qt files.
98         """
99         try:
100             if int(env.subst('$QT_AUTOSCAN')) == 0:
101                 return target, source
102         except ValueError:
103             pass
104         try:
105             debug = int(env.subst('$QT_DEBUG'))
106         except ValueError:
107             debug = 0
108         
109         # some shortcuts used in the scanner
110         splitext = SCons.Util.splitext
111         objBuilder = getattr(env, self.objBuilderName)
112   
113         # some regular expressions:
114         # Q_OBJECT detection
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
120         
121         # The following is kind of hacky to get builders working properly (FIXME)
122         objBuilderEnv = objBuilder.env
123         objBuilder.env = env
124         mocBuilderEnv = env.Moc.env
125         env.Moc.env = env
126         
127         # make a deep copy for the result; MocH objects will be appended
128         out_sources = source[:]
129
130         for obj in source:
131             if not obj.has_builder():
132                 # binary obj file provided
133                 if debug:
134                     print "scons: qt: '%s' seems to be a binary. Discarded." % str(obj)
135                 continue
136             cpp = obj.sources[0]
137             if not splitext(str(cpp))[1] in cxx_suffixes:
138                 if debug:
139                     print "scons: qt: '%s' is no cxx file. Discarded." % str(cpp) 
140                 # c or fortran source
141                 continue
142             #cpp_contents = comment.sub('', cpp.get_contents())
143             cpp_contents = cpp.get_contents()
144             h=None
145             for h_ext in header_extensions:
146                 # try to find the header file in the corresponding source
147                 # directory
148                 hname = splitext(cpp.name)[0] + h_ext
149                 h = find_file(hname, (cpp.get_dir(),), env.File)
150                 if h:
151                     if debug:
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()
155                     break
156             if not h and debug:
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
160                 moc_cpp = env.Moc(h)
161                 moc_o = objBuilder(moc_cpp)
162                 out_sources.append(moc_o)
163                 #moc_cpp.target_scanner = SCons.Defaults.CScan
164                 if debug:
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)
169                 moc = env.Moc(cpp)
170                 env.Ignore(moc, moc)
171                 if debug:
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
177
178         return (target, out_sources)
179
180 AutomocShared = _Automoc('SharedObject')
181 AutomocStatic = _Automoc('StaticObject')
182
183 def _detect(env):
184     """Not really safe, but fast method to detect the QT library"""
185     QTDIR = None
186     if not QTDIR:
187         QTDIR = env.get('QTDIR',None)
188     if not QTDIR:
189         QTDIR = os.environ.get('QTDIR',None)
190     if not QTDIR:
191         moc = env.WhereIs('moc')
192         if moc:
193             QTDIR = os.path.dirname(os.path.dirname(moc))
194             SCons.Warnings.warn(
195                 QtdirNotFound,
196                 "Could not detect qt, using moc executable as a hint (QTDIR=%s)" % QTDIR)
197         else:
198             QTDIR = None
199             SCons.Warnings.warn(
200                 QtdirNotFound,
201                 "Could not detect qt, using empty QTDIR")
202     return QTDIR
203
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
209     if len(target) < 2:
210         # second target is implementation
211         target.append(adjustixes(bs,
212                                  env.subst('$QT_UICIMPLPREFIX'),
213                                  env.subst('$QT_UICIMPLSUFFIX')))
214     if len(target) < 3:
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
220
221 def uicScannerFunc(node, env, path):
222     #print "uicScannerFunc"
223     dir = node.dir
224     includes = re.findall("<include.*?>(.*?)</include>", node.get_contents())
225     res = []
226     for incFile in includes:
227         incNode = dir.File(incFile)
228         if incNode.rexists():
229             #print "uicdep: ", incNode
230             res.append(dir.File(incFile))
231         else:
232             #print "uicdep: ", incNode, "not found"
233             pass
234     return res
235
236 uicScanner = SCons.Scanner.Scanner(uicScannerFunc,
237                                    name = "UicScanner", 
238                                    node_class = SCons.Node.FS.File,
239                                    node_factory = SCons.Node.FS.File,
240                                    recursive = 0)
241
242 def generate(env):
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
248
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
256
257                    QT_AUTOSCAN = 1, # scan for moc'able sources
258
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'),
265
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',
275                    QT_UISUFFIX = '.ui',
276
277                    # Commands for the qt support ...
278                    # command to generate header, implementation and moc-file
279                    # from a .ui file
280                    QT_UICCOM = [
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
287                    QT_MOCFROMHCOM = (
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
291                    QT_MOCFROMCXXCOM = [
292                     CLVar('$QT_MOC $QT_MOCFROMCXXFLAGS -o ${TARGETS[0]} $SOURCE'),
293                     Action(checkMocIncluded,None)])
294
295     # ... and the corresponding builders
296     uicBld = Builder(action=SCons.Action.Action('$QT_UICCOM', '$QT_UICCOMSTR'),
297                      emitter=uicEmitter,
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'
313
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')
320
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"],
332                      LIBS=['$QT_LIB'])
333
334 def exists(env):
335     return _detect(env)