summaryrefslogtreecommitdiff
path: root/buildtools/wafadmin/Tools/qt4.py
blob: 84d121a844f16439ed8c691c01e851efe371323c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/usr/bin/env python
# encoding: utf-8
# Thomas Nagy, 2006 (ita)

"""
Qt4 support

If QT4_ROOT is given (absolute path), the configuration will look in it first

This module also demonstrates how to add tasks dynamically (when the build has started)
"""

try:
	from xml.sax import make_parser
	from xml.sax.handler import ContentHandler
except ImportError:
	has_xml = False
	ContentHandler = object
else:
	has_xml = True

import os, sys
import ccroot, cxx
import TaskGen, Task, Utils, Runner, Options, Node, Configure
from TaskGen import taskgen, feature, after, extension
from Logs import error
from Constants import *

MOC_H = ['.h', '.hpp', '.hxx', '.hh']
EXT_RCC = ['.qrc']
EXT_UI  = ['.ui']
EXT_QT4 = ['.cpp', '.cc', '.cxx', '.C']

class qxx_task(Task.Task):
	"A cpp task that may create a moc task dynamically"

	before = ['cxx_link', 'static_link']

	def __init__(self, *k, **kw):
		Task.Task.__init__(self, *k, **kw)
		self.moc_done = 0

	def scan(self):
		(nodes, names) = ccroot.scan(self)
		# for some reasons (variants) the moc node may end in the list of node deps
		for x in nodes:
			if x.name.endswith('.moc'):
				nodes.remove(x)
				names.append(x.relpath_gen(self.inputs[0].parent))
		return (nodes, names)

	def runnable_status(self):
		if self.moc_done:
			# if there is a moc task, delay the computation of the file signature
			for t in self.run_after:
				if not t.hasrun:
					return ASK_LATER
			# the moc file enters in the dependency calculation
			# so we need to recompute the signature when the moc file is present
			self.signature()
			return Task.Task.runnable_status(self)
		else:
			# yes, really, there are people who generate cxx files
			for t in self.run_after:
				if not t.hasrun:
					return ASK_LATER
			self.add_moc_tasks()
			return ASK_LATER

	def add_moc_tasks(self):

		node = self.inputs[0]
		tree = node.__class__.bld

		try:
			# compute the signature once to know if there is a moc file to create
			self.signature()
		except KeyError:
			# the moc file may be referenced somewhere else
			pass
		else:
			# remove the signature, it must be recomputed with the moc task
			delattr(self, 'cache_sig')

		moctasks=[]
		mocfiles=[]
		variant = node.variant(self.env)
		try:
			tmp_lst = tree.raw_deps[self.unique_id()]
			tree.raw_deps[self.unique_id()] = []
		except KeyError:
			tmp_lst = []
		for d in tmp_lst:
			if not d.endswith('.moc'): continue
			# paranoid check
			if d in mocfiles:
				error("paranoia owns")
				continue

			# process that base.moc only once
			mocfiles.append(d)

			# find the extension (performed only when the .cpp has changes)
			base2 = d[:-4]
			for path in [node.parent] + self.generator.env['INC_PATHS']:
				tree.rescan(path)
				vals = getattr(Options.options, 'qt_header_ext', '') or MOC_H
				for ex in vals:
					h_node = path.find_resource(base2 + ex)
					if h_node:
						break
				else:
					continue
				break
			else:
				raise Utils.WafError("no header found for %s which is a moc file" % str(d))

			m_node = h_node.change_ext('.moc')
			tree.node_deps[(self.inputs[0].parent.id, self.env.variant(), m_node.name)] = h_node

			# create the task
			task = Task.TaskBase.classes['moc'](self.env, normal=0)
			task.set_inputs(h_node)
			task.set_outputs(m_node)

			generator = tree.generator
			generator.outstanding.insert(0, task)
			generator.total += 1

			moctasks.append(task)

		# remove raw deps except the moc files to save space (optimization)
		tmp_lst = tree.raw_deps[self.unique_id()] = mocfiles

		# look at the file inputs, it is set right above
		lst = tree.node_deps.get(self.unique_id(), ())
		for d in lst:
			name = d.name
			if name.endswith('.moc'):
				task = Task.TaskBase.classes['moc'](self.env, normal=0)
				task.set_inputs(tree.node_deps[(self.inputs[0].parent.id, self.env.variant(), name)]) # 1st element in a tuple
				task.set_outputs(d)

				generator = tree.generator
				generator.outstanding.insert(0, task)
				generator.total += 1

				moctasks.append(task)

		# simple scheduler dependency: run the moc task before others
		self.run_after = moctasks
		self.moc_done = 1

	run = Task.TaskBase.classes['cxx'].__dict__['run']

def translation_update(task):
	outs = [a.abspath(task.env) for a in task.outputs]
	outs = " ".join(outs)
	lupdate = task.env['QT_LUPDATE']

	for x in task.inputs:
		file = x.abspath(task.env)
		cmd = "%s %s -ts %s" % (lupdate, file, outs)
		Utils.pprint('BLUE', cmd)
		task.generator.bld.exec_command(cmd)

class XMLHandler(ContentHandler):
	def __init__(self):
		self.buf = []
		self.files = []
	def startElement(self, name, attrs):
		if name == 'file':
			self.buf = []
	def endElement(self, name):
		if name == 'file':
			self.files.append(''.join(self.buf))
	def characters(self, cars):
		self.buf.append(cars)

def scan(self):
	"add the dependency on the files referenced in the qrc"
	node = self.inputs[0]
	parser = make_parser()
	curHandler = XMLHandler()
	parser.setContentHandler(curHandler)
	fi = open(self.inputs[0].abspath(self.env))
	parser.parse(fi)
	fi.close()

	nodes = []
	names = []
	root = self.inputs[0].parent
	for x in curHandler.files:
		nd = root.find_resource(x)
		if nd: nodes.append(nd)
		else: names.append(x)

	return (nodes, names)

@extension(EXT_RCC)
def create_rcc_task(self, node):
	"hook for rcc files"
	rcnode = node.change_ext('_rc.cpp')
	rcctask = self.create_task('rcc', node, rcnode)
	cpptask = self.create_task('cxx', rcnode, rcnode.change_ext('.o'))
	self.compiled_tasks.append(cpptask)
	return cpptask

@extension(EXT_UI)
def create_uic_task(self, node):
	"hook for uic tasks"
	uictask = self.create_task('ui4', node)
	uictask.outputs = [self.path.find_or_declare(self.env['ui_PATTERN'] % node.name[:-3])]
	return uictask

class qt4_taskgen(cxx.cxx_taskgen):
	def __init__(self, *k, **kw):
		cxx.cxx_taskgen.__init__(self, *k, **kw)
		self.features.append('qt4')

@extension('.ts')
def add_lang(self, node):
	"""add all the .ts file into self.lang"""
	self.lang = self.to_list(getattr(self, 'lang', [])) + [node]

@feature('qt4')
@after('apply_link')
def apply_qt4(self):
	if getattr(self, 'lang', None):
		update = getattr(self, 'update', None)
		lst=[]
		trans=[]
		for l in self.to_list(self.lang):

			if not isinstance(l, Node.Node):
				l = self.path.find_resource(l+'.ts')

			t = self.create_task('ts2qm', l, l.change_ext('.qm'))
			lst.append(t.outputs[0])

			if update:
				trans.append(t.inputs[0])

		trans_qt4 = getattr(Options.options, 'trans_qt4', False)
		if update and trans_qt4:
			# we need the cpp files given, except the rcc task we create after
			# FIXME may be broken
			u = Task.TaskCmd(translation_update, self.env, 2)
			u.inputs = [a.inputs[0] for a in self.compiled_tasks]
			u.outputs = trans

		if getattr(self, 'langname', None):
			t = Task.TaskBase.classes['qm2rcc'](self.env)
			t.set_inputs(lst)
			t.set_outputs(self.path.find_or_declare(self.langname+'.qrc'))
			t.path = self.path
			k = create_rcc_task(self, t.outputs[0])
			self.link_task.inputs.append(k.outputs[0])

	self.env.append_value('MOC_FLAGS', self.env._CXXDEFFLAGS)
	self.env.append_value('MOC_FLAGS', self.env._CXXINCFLAGS)

@extension(EXT_QT4)
def cxx_hook(self, node):
	# create the compilation task: cpp or cc
	try: obj_ext = self.obj_ext
	except AttributeError: obj_ext = '_%d.o' % self.idx

	task = self.create_task('qxx', node, node.change_ext(obj_ext))
	self.compiled_tasks.append(task)
	return task

def process_qm2rcc(task):
	outfile = task.outputs[0].abspath(task.env)
	f = open(outfile, 'w')
	f.write('<!DOCTYPE RCC><RCC version="1.0">\n<qresource>\n')
	for k in task.inputs:
		f.write(' <file>')
		#f.write(k.name)
		f.write(k.path_to_parent(task.path))
		f.write('</file>\n')
	f.write('</qresource>\n</RCC>')
	f.close()

b = Task.simple_task_type
b('moc', '${QT_MOC} ${MOC_FLAGS} ${SRC} ${MOC_ST} ${TGT}', color='BLUE', vars=['QT_MOC', 'MOC_FLAGS'], shell=False)
cls = b('rcc', '${QT_RCC} -name ${SRC[0].name} ${SRC[0].abspath(env)} ${RCC_ST} -o ${TGT}', color='BLUE', before='cxx moc qxx_task', after="qm2rcc", shell=False)
cls.scan = scan
b('ui4', '${QT_UIC} ${SRC} -o ${TGT}', color='BLUE', before='cxx moc qxx_task', shell=False)
b('ts2qm', '${QT_LRELEASE} ${QT_LRELEASE_FLAGS} ${SRC} -qm ${TGT}', color='BLUE', before='qm2rcc', shell=False)

Task.task_type_from_func('qm2rcc', vars=[], func=process_qm2rcc, color='BLUE', before='rcc', after='ts2qm')

def detect_qt4(conf):
	env = conf.env
	opt = Options.options

	qtdir = getattr(opt, 'qtdir', '')
	qtbin = getattr(opt, 'qtbin', '')
	qtlibs = getattr(opt, 'qtlibs', '')
	useframework = getattr(opt, 'use_qt4_osxframework', True)

	paths = []

	# the path to qmake has been given explicitely
	if qtbin:
		paths = [qtbin]

	# the qt directory has been given - we deduce the qt binary path
	if not qtdir:
		qtdir = conf.environ.get('QT4_ROOT', '')
		qtbin = os.path.join(qtdir, 'bin')
		paths = [qtbin]

	# no qtdir, look in the path and in /usr/local/Trolltech
	if not qtdir:
		paths = os.environ.get('PATH', '').split(os.pathsep)
		paths.append('/usr/share/qt4/bin/')
		try:
			lst = os.listdir('/usr/local/Trolltech/')
		except OSError:
			pass
		else:
			if lst:
				lst.sort()
				lst.reverse()

				# keep the highest version
				qtdir = '/usr/local/Trolltech/%s/' % lst[0]
				qtbin = os.path.join(qtdir, 'bin')
				paths.append(qtbin)

	# at the end, try to find qmake in the paths given
	# keep the one with the highest version
	cand = None
	prev_ver = ['4', '0', '0']
	for qmk in ['qmake-qt4', 'qmake4', 'qmake']:
		qmake = conf.find_program(qmk, path_list=paths)
		if qmake:
			try:
				version = Utils.cmd_output([qmake, '-query', 'QT_VERSION']).strip()
			except ValueError:
				pass
			else:
				if version:
					new_ver = version.split('.')
					if new_ver > prev_ver:
						cand = qmake
						prev_ver = new_ver
	if cand:
		qmake = cand
	else:
		conf.fatal('could not find qmake for qt4')

	conf.env.QMAKE = qmake
	qtincludes = Utils.cmd_output([qmake, '-query', 'QT_INSTALL_HEADERS']).strip()
	qtdir = Utils.cmd_output([qmake, '-query', 'QT_INSTALL_PREFIX']).strip() + os.sep
	qtbin = Utils.cmd_output([qmake, '-query', 'QT_INSTALL_BINS']).strip() + os.sep

	if not qtlibs:
		try:
			qtlibs = Utils.cmd_output([qmake, '-query', 'QT_INSTALL_LIBS']).strip() + os.sep
		except ValueError:
			qtlibs = os.path.join(qtdir, 'lib')

	def find_bin(lst, var):
		for f in lst:
			ret = conf.find_program(f, path_list=paths)
			if ret:
				env[var]=ret
				break

	vars = "QtCore QtGui QtUiTools QtNetwork QtOpenGL QtSql QtSvg QtTest QtXml QtWebKit Qt3Support".split()

	find_bin(['uic-qt3', 'uic3'], 'QT_UIC3')
	find_bin(['uic-qt4', 'uic'], 'QT_UIC')
	if not env['QT_UIC']:
		conf.fatal('cannot find the uic compiler for qt4')

	try:
		version = Utils.cmd_output(env['QT_UIC'] + " -version 2>&1").strip()
	except ValueError:
		conf.fatal('your uic compiler is for qt3, add uic for qt4 to your path')

	version = version.replace('Qt User Interface Compiler ','')
	version = version.replace('User Interface Compiler for Qt', '')
	if version.find(" 3.") != -1:
		conf.check_message('uic version', '(too old)', 0, option='(%s)'%version)
		sys.exit(1)
	conf.check_message('uic version', '', 1, option='(%s)'%version)

	find_bin(['moc-qt4', 'moc'], 'QT_MOC')
	find_bin(['rcc'], 'QT_RCC')
	find_bin(['lrelease-qt4', 'lrelease'], 'QT_LRELEASE')
	find_bin(['lupdate-qt4', 'lupdate'], 'QT_LUPDATE')

	env['UIC3_ST']= '%s -o %s'
	env['UIC_ST'] = '%s -o %s'
	env['MOC_ST'] = '-o'
	env['ui_PATTERN'] = 'ui_%s.h'
	env['QT_LRELEASE_FLAGS'] = ['-silent']

	vars_debug = [a+'_debug' for a in vars]

	try:
		conf.find_program('pkg-config', var='pkgconfig', path_list=paths, mandatory=True)

	except Configure.ConfigurationError:

		for lib in vars_debug+vars:
			uselib = lib.upper()

			d = (lib.find('_debug') > 0) and 'd' or ''

			# original author seems to prefer static to shared libraries
			for (pat, kind) in ((conf.env.staticlib_PATTERN, 'STATIC'), (conf.env.shlib_PATTERN, '')):

				conf.check_message_1('Checking for %s %s' % (lib, kind))

				for ext in ['', '4']:
					path = os.path.join(qtlibs, pat % (lib + d + ext))
					if os.path.exists(path):
						env.append_unique(kind + 'LIB_' + uselib, lib + d + ext)
						conf.check_message_2('ok ' + path, 'GREEN')
						break
					path = os.path.join(qtbin, pat % (lib + d + ext))
					if os.path.exists(path):
						env.append_unique(kind + 'LIB_' + uselib, lib + d + ext)
						conf.check_message_2('ok ' + path, 'GREEN')
						break
				else:
					conf.check_message_2('not found', 'YELLOW')
					continue
				break

			env.append_unique('LIBPATH_' + uselib, qtlibs)
			env.append_unique('CPPPATH_' + uselib, qtincludes)
			env.append_unique('CPPPATH_' + uselib, qtincludes + os.sep + lib)
	else:
		for i in vars_debug+vars:
			try:
				conf.check_cfg(package=i, args='--cflags --libs --silence-errors', path=conf.env.pkgconfig)
			except ValueError:
				pass

	# the libpaths are set nicely, unfortunately they make really long command-lines
	# remove the qtcore ones from qtgui, etc
	def process_lib(vars_, coreval):
		for d in vars_:
			var = d.upper()
			if var == 'QTCORE': continue

			value = env['LIBPATH_'+var]
			if value:
				core = env[coreval]
				accu = []
				for lib in value:
					if lib in core: continue
					accu.append(lib)
				env['LIBPATH_'+var] = accu

	process_lib(vars, 'LIBPATH_QTCORE')
	process_lib(vars_debug, 'LIBPATH_QTCORE_DEBUG')

	# rpath if wanted
	want_rpath = getattr(Options.options, 'want_rpath', 1)
	if want_rpath:
		def process_rpath(vars_, coreval):
			for d in vars_:
				var = d.upper()
				value = env['LIBPATH_'+var]
				if value:
					core = env[coreval]
					accu = []
					for lib in value:
						if var != 'QTCORE':
							if lib in core:
								continue
						accu.append('-Wl,--rpath='+lib)
					env['RPATH_'+var] = accu
		process_rpath(vars, 'LIBPATH_QTCORE')
		process_rpath(vars_debug, 'LIBPATH_QTCORE_DEBUG')

	env['QTLOCALE'] = str(env['PREFIX'])+'/share/locale'

def detect(conf):
	detect_qt4(conf)

def set_options(opt):
	opt.add_option('--want-rpath', type='int', default=1, dest='want_rpath', help='set rpath to 1 or 0 [Default 1]')

	opt.add_option('--header-ext',
		type='string',
		default='',
		help='header extension for moc files',
		dest='qt_header_ext')

	for i in 'qtdir qtbin qtlibs'.split():
		opt.add_option('--'+i, type='string', default='', dest=i)

	if sys.platform == "darwin":
		opt.add_option('--no-qt4-framework', action="store_false", help='do not use the framework version of Qt4 in OS X', dest='use_qt4_osxframework',default=True)

	opt.add_option('--translate', action="store_true", help="collect translation strings", dest="trans_qt4", default=False)